2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.framework;
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
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;
25 import javax.net.ssl.SSLContext;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.opendaylight.protocol.util.SSLUtil;
32 public class SecureServerTest {
33 static final Random rnd = new Random();
35 private static final int MAX_MSGSIZE = 500;
36 final SimpleSessionListener pce = new SimpleSessionListener();
37 ProtocolSession session;
38 ProtocolServer server;
39 DispatcherImpl dispatcher;
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");
51 this.dispatcher = new DispatcherImpl(Executors.defaultThreadFactory());
52 this.port = rnd.nextInt(10000) + 20000;
56 public void tearDown() throws IOException {
57 this.dispatcher.onSessionClosed(this.session);
59 this.dispatcher.stop();
62 } catch (final InterruptedException e) {
68 public void testServerConnection() throws Exception {
70 this.server = this.dispatcher.createServer(new InetSocketAddress("127.0.0.3", this.port), new ProtocolConnectionFactory() {
72 public ProtocolConnection createProtocolConnection(final InetSocketAddress address) {
73 return new ProtocolConnection() {
75 public SessionPreferencesChecker getProposalChecker() {
76 return new SimpleSessionProposalChecker();
80 public SessionPreferences getProposal() {
81 return new SimpleSessionPreferences();
85 public InetSocketAddress getPeerAddress() {
90 public SessionListener getListener() {
91 return new SimpleSessionListener();
95 }, new SimpleSessionFactory(MAX_MSGSIZE), SimpleInputStream.FACTORY, this.context);
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."));
104 this.session = this.dispatcher.createClient(new ProtocolConnection() {
106 public SessionPreferencesChecker getProposalChecker() {
107 return new SimpleSessionProposalChecker();
111 public SessionPreferences getProposal() {
112 return new SimpleSessionPreferences();
116 public InetSocketAddress getPeerAddress() {
117 return new InetSocketAddress("127.0.0.3", SecureServerTest.this.port);
121 public SessionListener getListener() {
122 return SecureServerTest.this.pce;
124 }, new SimpleSessionFactory(MAX_MSGSIZE), SimpleInputStream.FACTORY, this.context);
127 this.session = this.dispatcher.createClient(new ProtocolConnection() {
129 public SessionPreferencesChecker getProposalChecker() {
130 return new SimpleSessionProposalChecker();
134 public SessionPreferences getProposal() {
135 return new SimpleSessionPreferences();
139 public InetSocketAddress getPeerAddress() {
140 return new InetSocketAddress("127.0.0.3", SecureServerTest.this.port);
144 public SessionListener getListener() {
145 return SecureServerTest.this.pce;
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:"));
153 synchronized (this.pce) {
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() {
164 public ProtocolConnection createProtocolConnection(final InetSocketAddress address) {
165 return new ProtocolConnection() {
168 public SessionPreferencesChecker getProposalChecker() {
169 return new SimpleSessionProposalChecker();
173 public SessionPreferences getProposal() {
174 return new SimpleSessionPreferences();
178 public InetSocketAddress getPeerAddress() {
183 public SessionListener getListener() {
184 return new SimpleSessionListener();
188 }, new ProtocolSessionFactory() {
190 public ProtocolSession getProtocolSession(final SessionParent parent, final Timer timer, final ProtocolConnection connection,
191 final int sessionId) {
192 return new Session(parent, MAX_MSGSIZE);
194 }, SimpleInputStream.FACTORY, this.context);
196 this.session = this.dispatcher.createClient(new ProtocolConnection() {
198 public SessionPreferencesChecker getProposalChecker() {
199 return new SimpleSessionProposalChecker();
203 public SessionPreferences getProposal() {
204 return new SimpleSessionPreferences();
208 public InetSocketAddress getPeerAddress() {
209 return new InetSocketAddress("127.0.0.3", SecureServerTest.this.port);
213 public SessionListener getListener() {
214 return new SimpleSessionListener();
216 }, new ProtocolSessionFactory() {
218 public ProtocolSession getProtocolSession(final SessionParent parent, final Timer timer, final ProtocolConnection connection,
219 final int sessionId) {
220 return new Session(parent, MAX_MSGSIZE);
222 }, SimpleInputStream.FACTORY, this.context);