forked from bigjosh/subethasmtp
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathStartTLSFullTest.java
120 lines (103 loc) · 4.65 KB
/
StartTLSFullTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package org.subethamail.smtp;
import static org.subethamail.smtp.TestUtil.EMAIL_FROM;
import static org.subethamail.smtp.TestUtil.EMAIL_TO;
import static org.subethamail.smtp.TestUtil.PORT;
import static org.subethamail.smtp.TestUtil.createTlsSslContext;
import static org.subethamail.smtp.TestUtil.getKeyManagers;
import static org.subethamail.smtp.TestUtil.getTrustManagers;
import static org.subethamail.smtp.TestUtil.send;
import java.io.InputStream;
import java.util.concurrent.Executors;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.InOrder;
import org.mockito.Mockito;
import org.subethamail.smtp.TestUtil.ConnectionType;
import org.subethamail.smtp.auth.LoginFailedException;
import org.subethamail.smtp.auth.PlainAuthenticationHandlerFactory;
import org.subethamail.smtp.server.SMTPServer;
public class StartTLSFullTest {
// the server is started using keyStore.jks and trustStore.jks on the
// classpath
// the trustStore contains the keyStore certificate (the server trusts
// itself)
// the send method uses the same trustStore (and the default keyStore?)
// to send
@Test
public void testStartTLS() throws Exception {
// System.setProperty("javax.net.debug", "all");
System.out.println("======= testStartTLS ==========");
KeyManager[] keyManagers = getKeyManagers();
TrustManager[] trustManagers = getTrustManagers();
SSLContext sslContext = createTlsSslContext(keyManagers, trustManagers);
// mock a MessageHandlerFactory to check for delivery
MessageHandlerFactory mhf = Mockito.mock(MessageHandlerFactory.class);
MessageHandler mh = Mockito.mock(MessageHandler.class);
Mockito.when(mhf.create(ArgumentMatchers.any(MessageContext.class))).thenReturn(mh);
SMTPServer server = SMTPServer //
.port(PORT) //
.hostName("email-server.me.com") //
.requireTLS() //
.messageHandlerFactory(mhf) //
.executorService(Executors.newSingleThreadExecutor()) //
.startTlsSocketFactory(sslContext) //
.build();
try {
server.start();
send(trustManagers, ConnectionType.START_TLS);
} finally {
server.stop();
}
InOrder o = Mockito.inOrder(mhf, mh);
o.verify(mhf).create(ArgumentMatchers.any(MessageContext.class));
o.verify(mh).from(EMAIL_FROM);
o.verify(mh).recipient(EMAIL_TO);
o.verify(mh).data(ArgumentMatchers.any(InputStream.class));
o.verify(mh).done();
o.verifyNoMoreInteractions();
}
@Test
public void testStartTLSAuthenticated() throws Exception {
// System.setProperty("javax.net.debug", "all");
System.out.println("======= testStartTLSAuthenticated ==========");
KeyManager[] keyManagers = getKeyManagers();
TrustManager[] trustManagers = getTrustManagers();
SSLContext sslContext = createTlsSslContext(keyManagers, trustManagers);
// mock a MessageHandlerFactory to check for delivery
MessageHandlerFactory mhf = Mockito.mock(MessageHandlerFactory.class);
MessageHandler mh = Mockito.mock(MessageHandler.class);
Mockito.when(mhf.create(ArgumentMatchers.any(MessageContext.class))).thenReturn(mh);
SMTPServer server = SMTPServer //
.port(PORT) //
.hostName("email-server.me.com") //
.requireTLS() //
.messageHandlerFactory(mhf) //
.requireAuth() //
.authenticationHandlerFactory(createAuthenticationHandlerFactory())
.executorService(Executors.newSingleThreadExecutor()) //
.startTlsSocketFactory(sslContext) //
.build();
try {
server.start();
send(trustManagers, ConnectionType.START_TLS, "me", "password");
} finally {
server.stop();
}
InOrder o = Mockito.inOrder(mhf, mh);
o.verify(mhf).create(ArgumentMatchers.any(MessageContext.class));
o.verify(mh).from(EMAIL_FROM);
o.verify(mh).recipient(EMAIL_TO);
o.verify(mh).data(ArgumentMatchers.any(InputStream.class));
o.verify(mh).done();
o.verifyNoMoreInteractions();
}
private static AuthenticationHandlerFactory createAuthenticationHandlerFactory() {
return new PlainAuthenticationHandlerFactory((username, password, context) -> {
if (!username.equals("me"))
throw new LoginFailedException();
});
}
}