Make MockSmtpServer use KeyStoreProvider
This commit is contained in:
parent
51839cc51f
commit
59fda3c4d7
1 changed files with 12 additions and 23 deletions
|
@ -23,6 +23,7 @@ import java.util.zip.InflaterInputStream;
|
|||
|
||||
import android.annotation.SuppressLint;
|
||||
|
||||
import com.fsck.k9.mail.helpers.KeyStoreProvider;
|
||||
import com.jcraft.jzlib.JZlib;
|
||||
import com.jcraft.jzlib.ZOutputStream;
|
||||
import javax.net.ssl.KeyManagerFactory;
|
||||
|
@ -37,15 +38,13 @@ import org.apache.commons.io.IOUtils;
|
|||
|
||||
@SuppressLint("NewApi")
|
||||
public class MockSmtpServer {
|
||||
private static final String KEYSTORE_PASSWORD = "password";
|
||||
private static final String KEYSTORE_RESOURCE = "/keystore.jks";
|
||||
|
||||
private static final byte[] CRLF = { '\r', '\n' };
|
||||
|
||||
|
||||
private final Deque<SmtpInteraction> interactions = new ConcurrentLinkedDeque<>();
|
||||
private final CountDownLatch waitForConnectionClosed = new CountDownLatch(1);
|
||||
private final CountDownLatch waitForAllExpectedCommands = new CountDownLatch(1);
|
||||
private final KeyStoreProvider keyStoreProvider;
|
||||
private final Logger logger;
|
||||
|
||||
private MockServerThread mockServerThread;
|
||||
|
@ -54,10 +53,11 @@ public class MockSmtpServer {
|
|||
|
||||
|
||||
public MockSmtpServer() {
|
||||
this(new DefaultLogger());
|
||||
this(KeyStoreProvider.getInstance(), new DefaultLogger());
|
||||
}
|
||||
|
||||
public MockSmtpServer(Logger logger) {
|
||||
public MockSmtpServer(KeyStoreProvider keyStoreProvider, Logger logger) {
|
||||
this.keyStoreProvider = keyStoreProvider;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
|
@ -86,7 +86,7 @@ public class MockSmtpServer {
|
|||
port = serverSocket.getLocalPort();
|
||||
|
||||
mockServerThread = new MockServerThread(serverSocket, interactions, waitForConnectionClosed,
|
||||
waitForAllExpectedCommands, logger);
|
||||
waitForAllExpectedCommands, logger, keyStoreProvider);
|
||||
mockServerThread.start();
|
||||
}
|
||||
|
||||
|
@ -230,6 +230,7 @@ public class MockSmtpServer {
|
|||
private final CountDownLatch waitForConnectionClosed;
|
||||
private final CountDownLatch waitForAllExpectedCommands;
|
||||
private final Logger logger;
|
||||
private final KeyStoreProvider keyStoreProvider;
|
||||
|
||||
private volatile boolean shouldStop = false;
|
||||
private volatile Socket clientSocket;
|
||||
|
@ -240,13 +241,15 @@ public class MockSmtpServer {
|
|||
|
||||
|
||||
public MockServerThread(ServerSocket serverSocket, Deque<SmtpInteraction> interactions,
|
||||
CountDownLatch waitForConnectionClosed, CountDownLatch waitForAllExpectedCommands, Logger logger) {
|
||||
CountDownLatch waitForConnectionClosed, CountDownLatch waitForAllExpectedCommands, Logger logger,
|
||||
KeyStoreProvider keyStoreProvider) {
|
||||
super("MockSmtpServer");
|
||||
this.serverSocket = serverSocket;
|
||||
this.interactions = interactions;
|
||||
this.waitForConnectionClosed = waitForConnectionClosed;
|
||||
this.waitForAllExpectedCommands = waitForAllExpectedCommands;
|
||||
this.logger = logger;
|
||||
this.keyStoreProvider = keyStoreProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -347,11 +350,11 @@ public class MockSmtpServer {
|
|||
private void upgradeToTls(Socket socket) throws KeyStoreException, IOException, NoSuchAlgorithmException,
|
||||
CertificateException, UnrecoverableKeyException, KeyManagementException {
|
||||
|
||||
KeyStore keyStore = loadKeyStore();
|
||||
KeyStore keyStore = keyStoreProvider.getKeyStore();
|
||||
|
||||
String defaultAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
|
||||
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(defaultAlgorithm);
|
||||
keyManagerFactory.init(keyStore, KEYSTORE_PASSWORD.toCharArray());
|
||||
keyManagerFactory.init(keyStore, keyStoreProvider.getPassword());
|
||||
|
||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||
sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
|
||||
|
@ -366,20 +369,6 @@ public class MockSmtpServer {
|
|||
output = Okio.buffer(Okio.sink(sslSocket.getOutputStream()));
|
||||
}
|
||||
|
||||
private KeyStore loadKeyStore() throws KeyStoreException, IOException, NoSuchAlgorithmException,
|
||||
CertificateException {
|
||||
KeyStore keyStore = KeyStore.getInstance("JKS");
|
||||
|
||||
InputStream keyStoreInputStream = getClass().getResourceAsStream(KEYSTORE_RESOURCE);
|
||||
try {
|
||||
keyStore.load(keyStoreInputStream, KEYSTORE_PASSWORD.toCharArray());
|
||||
} finally {
|
||||
keyStoreInputStream.close();
|
||||
}
|
||||
|
||||
return keyStore;
|
||||
}
|
||||
|
||||
private void readAdditionalCommands() throws IOException {
|
||||
String command = input.readUtf8Line();
|
||||
if (command == null) {
|
||||
|
|
Loading…
Reference in a new issue