1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.example.echoserver.ssl;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24
25 import java.net.InetSocketAddress;
26 import java.net.Socket;
27 import java.nio.charset.StandardCharsets;
28 import java.security.cert.CertificateException;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import javax.net.ssl.SSLContext;
33 import javax.net.ssl.SSLSocket;
34 import javax.net.ssl.TrustManager;
35 import javax.net.ssl.X509TrustManager;
36
37 import org.apache.mina.core.service.IoHandlerAdapter;
38 import org.apache.mina.core.session.IoSession;
39 import org.apache.mina.filter.codec.ProtocolCodecFilter;
40 import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
41 import org.apache.mina.filter.ssl.SslFilter;
42 import org.apache.mina.transport.socket.SocketAcceptor;
43 import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
44 import org.junit.After;
45 import org.junit.Before;
46 import org.junit.Test;
47
48
49
50
51
52
53 public class SslFilterTest {
54
55 private int port;
56 private SocketAcceptor acceptor;
57
58 @Before
59 public void setUp() throws Exception {
60 acceptor = new NioSocketAcceptor();
61 }
62
63 @After
64 public void tearDown() throws Exception {
65 acceptor.setCloseOnDeactivation(true);
66 acceptor.dispose();
67 }
68
69 @Test
70 public void testMessageSentIsCalled() throws Exception {
71 testMessageSentIsCalled(false);
72 }
73
74 @Test
75 public void testMessageSentIsCalled_With_SSL() throws Exception {
76 testMessageSentIsCalled(true);
77 }
78
79 private void testMessageSentIsCalled(boolean useSSL) throws Exception {
80
81 java.lang.System.setProperty( "sun.security.ssl.allowUnsafeRenegotiation", "true" );
82
83 SslFilter sslFilter = null;
84 if (useSSL) {
85 sslFilter = new SslFilter(BogusSslContextFactory.getInstance(true));
86 acceptor.getFilterChain().addLast("sslFilter", sslFilter);
87 }
88 acceptor.getFilterChain().addLast(
89 "codec",
90 new ProtocolCodecFilter(new TextLineCodecFactory(StandardCharsets.UTF_8)));
91
92 EchoHandler handler = new EchoHandler();
93 acceptor.setHandler(handler);
94 acceptor.bind(new InetSocketAddress(0));
95 port = acceptor.getLocalAddress().getPort();
96
97
98 Socket socket = getClientSocket(useSSL);
99 int bytesSent = 0;
100 bytesSent += writeMessage(socket, "test-1\n");
101
102 if (useSSL) {
103
104 SSLSocket ss = (SSLSocket) socket;
105
106 ss.startHandshake();
107 }
108
109 bytesSent += writeMessage(socket, "test-2\n");
110
111 int[] response = new int[bytesSent];
112 for (int i = 0; i < response.length; i++) {
113 response[i] = socket.getInputStream().read();
114 }
115
116 if (useSSL) {
117
118 while (socket.getInputStream().read() >= 0) {
119 continue;
120 }
121 }
122
123 socket.close();
124 while (acceptor.getManagedSessions().size() != 0) {
125 Thread.sleep(100);
126 }
127
128
129 assertEquals("handler should have sent 2 messages:", 2,
130 handler.sentMessages.size());
131 assertTrue(handler.sentMessages.contains("test-1"));
132 assertTrue(handler.sentMessages.contains("test-2"));
133 }
134
135 private int writeMessage(Socket socket, String message) throws Exception {
136 byte request[] = message.getBytes(StandardCharsets.UTF_8);
137 socket.getOutputStream().write(request);
138 return request.length;
139 }
140
141 private Socket getClientSocket(boolean ssl) throws Exception {
142 if (ssl) {
143 SSLContext ctx = SSLContext.getInstance("TLS");
144 ctx.init(null, trustManagers, null);
145 return ctx.getSocketFactory().createSocket("localhost", port);
146 }
147 return new Socket("localhost", port);
148 }
149
150 private static class EchoHandler extends IoHandlerAdapter {
151
152 List<String> sentMessages = new ArrayList<String>();
153
154 @Override
155 public void exceptionCaught(IoSession session, Throwable cause)
156 throws Exception {
157
158 }
159
160 @Override
161 public void messageReceived(IoSession session, Object message)
162 throws Exception {
163 session.write(message);
164 }
165
166 @Override
167 public void messageSent(IoSession session, Object message)
168 throws Exception {
169 sentMessages.add(message.toString());
170
171 if (sentMessages.size() >= 2) {
172 session.closeNow();
173 }
174 }
175 }
176
177 TrustManager[] trustManagers = new TrustManager[] { new TrustAnyone() };
178
179 private static class TrustAnyone implements X509TrustManager {
180 public void checkClientTrusted(
181 java.security.cert.X509Certificate[] x509Certificates, String s)
182 throws CertificateException {
183 }
184
185 public void checkServerTrusted(
186 java.security.cert.X509Certificate[] x509Certificates, String s)
187 throws CertificateException {
188 }
189
190 public java.security.cert.X509Certificate[] getAcceptedIssuers() {
191 return new java.security.cert.X509Certificate[0];
192 }
193 }
194
195 }