KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > tcp > SslTransportServerTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.activemq.transport.tcp;
20
21 import junit.framework.TestCase;
22
23 import java.io.IOException JavaDoc;
24 import java.net.URI JavaDoc;
25
26 public class SslTransportServerTest extends TestCase {
27     private SslTransportServer sslTransportServer = null;
28     private StubSSLServerSocket sslServerSocket = null;
29     
30     protected void setUp() throws Exception JavaDoc {
31     }
32
33     protected void tearDown() throws Exception JavaDoc {
34         super.tearDown();
35     }
36     
37     private void createAndBindTransportServer(boolean wantClientAuth, boolean needClientAuth, String JavaDoc options)
38             throws IOException JavaDoc {
39         sslServerSocket = new StubSSLServerSocket();
40         
41         StubSSLSocketFactory socketFactory = new StubSSLSocketFactory(sslServerSocket);
42         
43         try {
44             sslTransportServer =
45                 new SslTransportServer(null, new URI JavaDoc("ssl://localhost:61616?" + options), socketFactory);
46         } catch (Exception JavaDoc e) {
47             fail("Unable to create SslTransportServer.");
48         }
49         
50         sslTransportServer.setWantClientAuth(wantClientAuth);
51         sslTransportServer.setNeedClientAuth(needClientAuth);
52         
53         sslTransportServer.bind();
54     }
55     
56     public void testWantAndNeedClientAuthSetters() throws IOException JavaDoc {
57         for (int i = 0; i < 4; ++i) {
58             final boolean wantClientAuth = ((i & 0x1) == 1);
59             final boolean needClientAuth = ((i & 0x2) == 1);
60             
61             final int expectedWantStatus = (wantClientAuth ? StubSSLServerSocket.TRUE : StubSSLServerSocket.FALSE );
62             final int expectedNeedStatus = (needClientAuth ? StubSSLServerSocket.TRUE : StubSSLServerSocket.FALSE );
63             
64             createAndBindTransportServer(wantClientAuth, needClientAuth, "");
65             
66             assertEquals("Created ServerSocket did not have correct wantClientAuth status.",
67                 sslServerSocket.getWantClientAuthStatus(), expectedWantStatus);
68             
69             assertEquals("Created ServerSocket did not have correct needClientAuth status.",
70                 sslServerSocket.getNeedClientAuthStatus(), expectedNeedStatus);
71             }
72     }
73     
74     public void testWantAndNeedAuthReflection() throws IOException JavaDoc {
75         for (int i = 0; i < 4; ++i) {
76             final boolean wantClientAuth = ((i & 0x1) == 1);
77             final boolean needClientAuth = ((i & 0x2) == 1);
78             
79             final int expectedWantStatus = (wantClientAuth ? StubSSLServerSocket.TRUE : StubSSLServerSocket.FALSE );
80             final int expectedNeedStatus = (needClientAuth ? StubSSLServerSocket.TRUE : StubSSLServerSocket.FALSE );
81             
82             String JavaDoc options = "wantClientAuth=" + (wantClientAuth ? "true" : "false") +
83                 "&needClientAuth=" + (needClientAuth ? "true" : "false");
84             
85             createAndBindTransportServer(wantClientAuth, needClientAuth, options);
86             
87             assertEquals("Created ServerSocket did not have correct wantClientAuth status.",
88                 sslServerSocket.getWantClientAuthStatus(), expectedWantStatus);
89             
90             assertEquals("Created ServerSocket did not have correct needClientAuth status.",
91                 sslServerSocket.getNeedClientAuthStatus(), expectedNeedStatus);
92         }
93     }
94 }
95
Popular Tags