Activate code generation
[bgpcep.git] / framework / src / test / java / org / opendaylight / protocol / framework / SecureServerTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.framework;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
12
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.net.InetSocketAddress;
16 import java.security.KeyManagementException;
17 import java.security.KeyStoreException;
18 import java.security.NoSuchAlgorithmException;
19 import java.security.UnrecoverableKeyException;
20 import java.security.cert.CertificateException;
21 import java.util.Random;
22 import java.util.Timer;
23 import java.util.concurrent.Executors;
24
25 import javax.net.ssl.SSLContext;
26
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.opendaylight.protocol.util.SSLUtil;
31
32 public class SecureServerTest {
33         static final Random rnd = new Random();
34
35         private static final int MAX_MSGSIZE = 500;
36         final SimpleSessionListener pce = new SimpleSessionListener();
37         ProtocolSession session;
38         ProtocolServer server;
39         DispatcherImpl dispatcher;
40         SSLContext context;
41         int port;
42
43         @Before
44         public void setUp() throws Exception {
45                 final InputStream keyStore = SecureServerTest.class.getResourceAsStream("/keystore.jks");
46                 final InputStream trustStore = SecureServerTest.class.getResourceAsStream("/keystore.jks");
47                 this.context = SSLUtil.initializeSecureContext("keystore", keyStore, trustStore, "SunX509");
48                 keyStore.close();
49                 trustStore.close();
50
51                 this.dispatcher = new DispatcherImpl(Executors.defaultThreadFactory());
52                 this.port = rnd.nextInt(10000) + 20000;
53         }
54
55         @After
56         public void tearDown() throws IOException {
57                 this.dispatcher.onSessionClosed(this.session);
58                 this.server.close();
59                 this.dispatcher.stop();
60                 try {
61                         Thread.sleep(100);
62                 } catch (final InterruptedException e) {
63                         e.printStackTrace();
64                 }
65         }
66
67         @Test
68         public void testServerConnection() throws Exception {
69
70                 this.server = this.dispatcher.createServer(new InetSocketAddress("127.0.0.3", this.port), new ProtocolConnectionFactory() {
71                         @Override
72                         public ProtocolConnection createProtocolConnection(final InetSocketAddress address) {
73                                 return new ProtocolConnection() {
74                                         @Override
75                                         public SessionPreferencesChecker getProposalChecker() {
76                                                 return new SimpleSessionProposalChecker();
77                                         }
78
79                                         @Override
80                                         public SessionPreferences getProposal() {
81                                                 return new SimpleSessionPreferences();
82                                         }
83
84                                         @Override
85                                         public InetSocketAddress getPeerAddress() {
86                                                 return address;
87                                         }
88
89                                         @Override
90                                         public SessionListener getListener() {
91                                                 return new SimpleSessionListener();
92                                         }
93                                 };
94                         }
95                 }, new SimpleSessionFactory(MAX_MSGSIZE), SimpleInputStream.FACTORY, this.context);
96
97                 try {
98                         this.server = this.dispatcher.createServer(new InetSocketAddress("127.0.0.3", this.port), null, null, null, null);
99                         fail("Exception should have occured.");
100                 } catch (final IllegalStateException e) {
101                         assertTrue(e.getMessage().startsWith("Server with this address:") && e.getMessage().endsWith("was already created."));
102                 }
103
104                 this.session = this.dispatcher.createClient(new ProtocolConnection() {
105                         @Override
106                         public SessionPreferencesChecker getProposalChecker() {
107                                 return new SimpleSessionProposalChecker();
108                         }
109
110                         @Override
111                         public SessionPreferences getProposal() {
112                                 return new SimpleSessionPreferences();
113                         }
114
115                         @Override
116                         public InetSocketAddress getPeerAddress() {
117                                 return new InetSocketAddress("127.0.0.3", SecureServerTest.this.port);
118                         }
119
120                         @Override
121                         public SessionListener getListener() {
122                                 return SecureServerTest.this.pce;
123                         }
124                 }, new SimpleSessionFactory(MAX_MSGSIZE), SimpleInputStream.FACTORY, this.context);
125
126                 try {
127                         this.session = this.dispatcher.createClient(new ProtocolConnection() {
128                                 @Override
129                                 public SessionPreferencesChecker getProposalChecker() {
130                                         return new SimpleSessionProposalChecker();
131                                 }
132
133                                 @Override
134                                 public SessionPreferences getProposal() {
135                                         return new SimpleSessionPreferences();
136                                 }
137
138                                 @Override
139                                 public InetSocketAddress getPeerAddress() {
140                                         return new InetSocketAddress("127.0.0.3", SecureServerTest.this.port);
141                                 }
142
143                                 @Override
144                                 public SessionListener getListener() {
145                                         return SecureServerTest.this.pce;
146                                 }
147                         }, null, null, null);
148                         fail("Exception should have occured.");
149                 } catch (final IllegalStateException e) {
150                         assertTrue(e.getMessage().startsWith("Attempt to create duplicate client session to the same address:"));
151                 }
152
153                 synchronized (this.pce) {
154                         while (!this.pce.up)
155                                 this.pce.wait();
156                 }
157         }
158
159         @Test
160         public void testIO() throws IOException, InterruptedException, KeyManagementException, UnrecoverableKeyException,
161                         NoSuchAlgorithmException, KeyStoreException, CertificateException {
162                 this.server = this.dispatcher.createServer(new InetSocketAddress("127.0.0.3", this.port), new ProtocolConnectionFactory() {
163                         @Override
164                         public ProtocolConnection createProtocolConnection(final InetSocketAddress address) {
165                                 return new ProtocolConnection() {
166
167                                         @Override
168                                         public SessionPreferencesChecker getProposalChecker() {
169                                                 return new SimpleSessionProposalChecker();
170                                         }
171
172                                         @Override
173                                         public SessionPreferences getProposal() {
174                                                 return new SimpleSessionPreferences();
175                                         }
176
177                                         @Override
178                                         public InetSocketAddress getPeerAddress() {
179                                                 return address;
180                                         }
181
182                                         @Override
183                                         public SessionListener getListener() {
184                                                 return new SimpleSessionListener();
185                                         }
186                                 };
187                         }
188                 }, new ProtocolSessionFactory() {
189                         @Override
190                         public ProtocolSession getProtocolSession(final SessionParent parent, final Timer timer, final ProtocolConnection connection,
191                                         final int sessionId) {
192                                 return new Session(parent, MAX_MSGSIZE);
193                         }
194                 }, SimpleInputStream.FACTORY, this.context);
195
196                 this.session = this.dispatcher.createClient(new ProtocolConnection() {
197                         @Override
198                         public SessionPreferencesChecker getProposalChecker() {
199                                 return new SimpleSessionProposalChecker();
200                         }
201
202                         @Override
203                         public SessionPreferences getProposal() {
204                                 return new SimpleSessionPreferences();
205                         }
206
207                         @Override
208                         public InetSocketAddress getPeerAddress() {
209                                 return new InetSocketAddress("127.0.0.3", SecureServerTest.this.port);
210                         }
211
212                         @Override
213                         public SessionListener getListener() {
214                                 return new SimpleSessionListener();
215                         }
216                 }, new ProtocolSessionFactory() {
217                         @Override
218                         public ProtocolSession getProtocolSession(final SessionParent parent, final Timer timer, final ProtocolConnection connection,
219                                         final int sessionId) {
220                                 return new Session(parent, MAX_MSGSIZE);
221                         }
222                 }, SimpleInputStream.FACTORY, this.context);
223         }
224
225 }