Remove default port information from ServerSettings.Type

This commit is contained in:
cketti 2018-07-17 22:35:26 +02:00
parent 83f3ac06b6
commit 50e955d2da
6 changed files with 47 additions and 46 deletions

View file

@ -55,17 +55,30 @@ public class AccountCreator {
}
public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) {
switch (securityType) {
case NONE:
case STARTTLS_REQUIRED: {
return storeType.defaultPort;
}
case SSL_TLS_REQUIRED: {
return storeType.defaultTlsPort;
}
switch (storeType) {
case IMAP: return getImapDefaultPort(securityType);
case WebDAV: return getWebDavDefaultPort(securityType);
case POP3: return getPop3DefaultPort(securityType);
case SMTP: return getSmtpDefaultPort(securityType);
}
throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType);
throw new AssertionError("Unhandled case: " + storeType);
}
public static int getImapDefaultPort(ConnectionSecurity connectionSecurity) {
return connectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED ? 993 : 143;
}
public static int getPop3DefaultPort(ConnectionSecurity connectionSecurity) {
return connectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED ? 995 : 110;
}
public static int getWebDavDefaultPort(ConnectionSecurity connectionSecurity) {
return connectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED ? 443 : 80;
}
public static int getSmtpDefaultPort(ConnectionSecurity connectionSecurity) {
return connectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED ? 465 : 587;
}
/*

View file

@ -42,20 +42,20 @@ public class AccountCreatorTest extends RobolectricTest {
public void getDefaultPort_withNoConnectionSecurityAndImap_shouldReturnDefaultPort() {
int result = AccountCreator.getDefaultPort(ConnectionSecurity.NONE, Type.IMAP);
assertEquals(Type.IMAP.defaultPort, result);
assertEquals(143, result);
}
@Test
public void getDefaultPort_withStartTlsAndImap_shouldReturnDefaultPort() {
int result = AccountCreator.getDefaultPort(ConnectionSecurity.STARTTLS_REQUIRED, Type.IMAP);
assertEquals(Type.IMAP.defaultPort, result);
assertEquals(143, result);
}
@Test
public void getDefaultPort_withTlsAndImap_shouldReturnDefaultTlsPort() {
int result = AccountCreator.getDefaultPort(ConnectionSecurity.SSL_TLS_REQUIRED, Type.IMAP);
assertEquals(Type.IMAP.defaultTlsPort, result);
assertEquals(993, result);
}
}

View file

@ -7,13 +7,15 @@ import java.net.URISyntaxException;
import com.fsck.k9.mail.AuthType;
import com.fsck.k9.mail.ConnectionSecurity;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mail.ServerSettings.Type;
import com.fsck.k9.mail.store.imap.ImapStoreSettings;
import static com.fsck.k9.mail.helper.UrlEncodingHelper.decodeUtf8;
public class ImapStoreUriDecoder {
private static final int DEFAULT_PORT = 143;
private static final int DEFAULT_TLS_PORT = 993;
/**
* Decodes an ImapStore URI.
*
@ -62,13 +64,13 @@ public class ImapStoreUriDecoder {
*/
if (scheme.equals("imap")) {
connectionSecurity = ConnectionSecurity.NONE;
port = Type.IMAP.defaultPort;
port = DEFAULT_PORT;
} else if (scheme.startsWith("imap+tls")) {
connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED;
port = Type.IMAP.defaultPort;
port = DEFAULT_PORT;
} else if (scheme.startsWith("imap+ssl")) {
connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED;
port = Type.IMAP.defaultTlsPort;
port = DEFAULT_TLS_PORT;
} else {
throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")");
}

View file

@ -7,12 +7,14 @@ import java.net.URISyntaxException;
import com.fsck.k9.mail.AuthType;
import com.fsck.k9.mail.ConnectionSecurity;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mail.ServerSettings.Type;
import static com.fsck.k9.mail.helper.UrlEncodingHelper.decodeUtf8;
public class Pop3StoreUriDecoder {
private static final int DEFAULT_PORT = 110;
private static final int DEFAULT_TLS_PORT = 995;
/**
* Decodes a Pop3Store URI.
*
@ -59,13 +61,13 @@ public class Pop3StoreUriDecoder {
*/
if (scheme.equals("pop3")) {
connectionSecurity = ConnectionSecurity.NONE;
port = Type.POP3.defaultPort;
port = DEFAULT_PORT;
} else if (scheme.startsWith("pop3+tls")) {
connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED;
port = Type.POP3.defaultPort;
port = DEFAULT_PORT;
} else if (scheme.startsWith("pop3+ssl")) {
connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED;
port = Type.POP3.defaultTlsPort;
port = DEFAULT_TLS_PORT;
} else {
throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")");
}

View file

@ -18,28 +18,10 @@ import java.util.Map;
public class ServerSettings {
public enum Type {
IMAP(143, 993),
SMTP(587, 465),
WebDAV(80, 443),
POP3(110, 995);
public final int defaultPort;
/**
* Note: port for connections using TLS (=SSL) immediately
* from the initial TCP connection.
*
* STARTTLS uses the defaultPort, then upgrades.
*
* See https://www.fastmail.com/help/technical/ssltlsstarttls.html.
*/
public final int defaultTlsPort;
private Type(int defaultPort, int defaultTlsPort) {
this.defaultPort = defaultPort;
this.defaultTlsPort = defaultTlsPort;
}
IMAP,
SMTP,
WebDAV,
POP3
}
/**
@ -237,4 +219,4 @@ public class ServerSettings {
(clientCertificateAlias == null ? that.clientCertificateAlias == null :
clientCertificateAlias.equals(that.clientCertificateAlias));
}
}
}

View file

@ -13,6 +13,8 @@ import com.fsck.k9.mail.ServerSettings.Type;
public class SmtpTransportUriDecoder {
private static final int DEFAULT_PORT = 587;
private static final int DEFAULT_TLS_PORT = 465;
/**
* Decodes a SmtpTransport URI.
@ -57,13 +59,13 @@ public class SmtpTransportUriDecoder {
*/
if (scheme.equals("smtp")) {
connectionSecurity = ConnectionSecurity.NONE;
port = Type.SMTP.defaultPort;
port = DEFAULT_PORT;
} else if (scheme.startsWith("smtp+tls")) {
connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED;
port = Type.SMTP.defaultPort;
port = DEFAULT_PORT;
} else if (scheme.startsWith("smtp+ssl")) {
connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED;
port = Type.SMTP.defaultTlsPort;
port = DEFAULT_TLS_PORT;
} else {
throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")");
}