View Javadoc

1   // Copyright (C) 2005 - 2011 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.tools.tcpproxy;
23  
24  import static net.grinder.testutility.SocketUtilities.findFreePort;
25  
26  import java.io.File;
27  import java.io.FileOutputStream;
28  import java.net.ConnectException;
29  import java.net.ServerSocket;
30  import java.net.Socket;
31  
32  import javax.net.ssl.SSLServerSocket;
33  
34  import net.grinder.testutility.AbstractFileTestCase;
35  import net.grinder.util.StreamCopier;
36  
37  
38  /**
39   * Unit test case for {@link TCPProxySocketFactoryImplementation} and
40   * {@link TCPProxySSLSocketFactoryImplementation}.
41   *
42   * @author Philip Aston
43   */
44  public class TestTCPProxySocketFactories extends AbstractFileTestCase {
45  
46    private int m_freeLocalPort;
47  
48    protected void setUp() throws Exception {
49      super.setUp();
50  
51      m_freeLocalPort = findFreePort();
52    }
53  
54    public void testTCPProxySocketFactoryImplementation() throws Exception {
55      final TCPProxySocketFactory socketFactory =
56        new TCPProxySocketFactoryImplementation();
57  
58      final ServerSocket createdServerSocket =
59        socketFactory.createServerSocket(new EndPoint("localhost",
60                                                      m_freeLocalPort),
61                                         100);
62  
63      final Socket createdLocalSocket =
64        socketFactory.createClientSocket(new EndPoint("localhost",
65                                                      m_freeLocalPort));
66      createdServerSocket.close();
67      createdLocalSocket.close();
68    }
69  
70    public void testTCPProxySSLSocketFactoryImplementation() throws Exception {
71      final TCPProxySSLSocketFactory socketFactory =
72        new TCPProxySSLSocketFactoryImplementation();
73  
74      final ServerSocket createdServerSocket =
75        socketFactory.createServerSocket(new EndPoint("localhost",
76                                                      m_freeLocalPort),
77                                         100);
78  
79      assertTrue(createdServerSocket instanceof SSLServerSocket);
80  
81      final Socket createdLocalSocket =
82        socketFactory.createClientSocket(new EndPoint("localhost",
83                                                      m_freeLocalPort));
84      createdServerSocket.close();
85  
86      final Socket createdLocalSocket2 =
87        socketFactory.createClientSocket(createdLocalSocket,
88                                         new EndPoint("localhost",
89                                                      m_freeLocalPort));
90  
91      createdLocalSocket2.close();
92    }
93  
94    public void testTCPProxySSLSocketFactoryImplementationWithFileStore()
95      throws Exception {
96  
97      final File keyStoreFile = new File(getDirectory(), "keystore");
98  
99      new StreamCopier(2000, true).copy(
100       getClass().getResourceAsStream("resources/default.keystore"),
101       new FileOutputStream(keyStoreFile));
102 
103     final TCPProxySSLSocketFactory socketFactory =
104       new TCPProxySSLSocketFactoryImplementation(keyStoreFile,
105                                                  "passphrase".toCharArray(),
106                                                  null);
107 
108     final ServerSocket createdServerSocket =
109       socketFactory.createServerSocket(new EndPoint("localhost",
110                                                     m_freeLocalPort),
111                                        100);
112 
113     assertTrue(createdServerSocket instanceof SSLServerSocket);
114 
115     final Socket createdLocalSocket =
116       socketFactory.createClientSocket(new EndPoint("localhost",
117                                                     m_freeLocalPort));
118     createdServerSocket.close();
119     createdLocalSocket.close();
120 
121     final TCPProxySSLSocketFactory socketFactory2 =
122       new TCPProxySSLSocketFactoryImplementation(keyStoreFile,
123                                                  "passphrase".toCharArray(),
124                                                  "jks");
125 
126     final ServerSocket createdServerSocket2 =
127       socketFactory2.createServerSocket(new EndPoint("localhost",
128                                                      m_freeLocalPort),
129                                        100);
130 
131     assertTrue(createdServerSocket2 instanceof SSLServerSocket);
132 
133     final Socket createdLocalSocket2 =
134       socketFactory2.createClientSocket(new EndPoint("localhost",
135                                                      m_freeLocalPort));
136     createdServerSocket2.close();
137     createdLocalSocket2.close();
138   }
139 
140   public void testUnhappyCases() throws Exception {
141 
142     final TCPProxySocketFactory socketFactory =
143       new TCPProxySocketFactoryImplementation();
144 
145     try {
146       socketFactory.createClientSocket(
147         new EndPoint("localhost", m_freeLocalPort));
148       fail("Expected VerboseConnectException");
149     }
150     catch (VerboseConnectException e) {
151       assertTrue(e.getCause() instanceof ConnectException);
152     }
153 
154     final TCPProxySSLSocketFactory sslSocketFactory =
155       new TCPProxySSLSocketFactoryImplementation();
156 
157     try {
158       sslSocketFactory.createClientSocket(
159         new EndPoint("localhost", m_freeLocalPort));
160       fail("Expected VerboseConnectException");
161     }
162     catch (VerboseConnectException e) {
163       assertTrue(e.getCause() instanceof ConnectException);
164     }
165   }
166 }