1   /*
2    * $Id: SslConnectorFunctionalTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.ssl;
12  
13  import org.mule.MuleManager;
14  import org.mule.impl.ResponseOutputStream;
15  import org.mule.impl.endpoint.MuleEndpointURI;
16  import org.mule.tck.functional.AbstractProviderFunctionalTestCase;
17  import org.mule.tck.functional.EventCallback;
18  import org.mule.tck.functional.FunctionalTestComponent;
19  import org.mule.umo.UMOEventContext;
20  import org.mule.umo.endpoint.MalformedEndpointException;
21  import org.mule.umo.endpoint.UMOEndpointURI;
22  import org.mule.umo.provider.UMOConnector;
23  
24  import java.io.BufferedInputStream;
25  import java.io.BufferedOutputStream;
26  import java.io.DataInputStream;
27  import java.io.DataOutputStream;
28  import java.net.Socket;
29  import java.net.URI;
30  
31  import javax.net.ssl.SSLContext;
32  import javax.net.ssl.SSLSocketFactory;
33  
34  /**
35   * Note that this test doesn't test the socket from the connector itself (and so ran
36   * with no problems when the connector was not using SSL).  Rather than alter this
37   * test case (which I don't completely understand, and which may be useful in other
38   * ways) I have added an additional test in {@link org.mule.providers.ssl.SslFunctionalTestCase}
39   */
40  public class SslConnectorFunctionalTestCase extends AbstractProviderFunctionalTestCase
41  {
42      private int port = 61655;
43      private Socket s;
44  
45      protected UMOEndpointURI getInDest()
46      {
47          try
48          {
49              logger.debug("Using port " + port);
50              return new MuleEndpointURI("ssl://localhost:" + port);
51          }
52          catch (MalformedEndpointException e)
53          {
54              fail(e.getMessage());
55              return null;
56          }
57      }
58  
59      protected UMOEndpointURI getOutDest()
60      {
61          return null;
62      }
63  
64      public UMOConnector createConnector() throws Exception
65      {
66          return SslConnectorTestCase.createConnector(false);
67      }
68  
69      protected Socket createSocket(URI uri) throws Exception
70      {
71          SslConnector conn = (SslConnector)connector;
72          SSLContext context;
73          context = SSLContext.getInstance(conn.getProtocol());
74          context.init(conn.getKeyManagerFactory().getKeyManagers(), conn.getTrustManagerFactory()
75              .getTrustManagers(), null);
76          SSLSocketFactory factory = context.getSocketFactory();
77          Socket socket = factory.createSocket(uri.getHost(), uri.getPort());
78  
79          // this will force open the socket and start SSL/TLS negotiation
80          // sslSocket.startHandshake();
81  
82          return socket;
83      }
84  
85      protected void doTearDown() throws Exception
86      {
87          if (s != null)
88          {
89              s.close();
90              s = null;
91          }
92      }
93  
94      protected void sendTestData(int iterations) throws Exception
95      {
96          MuleManager.getConfiguration().setSynchronous(false);
97          URI uri = getInDest().getUri();
98          for (int i = 0; i < iterations; i++)
99          {
100             s = createSocket(uri);
101             DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
102             dos.write("Hello".getBytes());
103             dos.flush();
104             logger.info("Sent message: " + i);
105             dos.close();
106         }
107     }
108 
109     protected void receiveAndTestResults() throws Exception
110     {
111         Thread.sleep(3000);
112         assertEquals(100, callbackCount);
113 
114     }
115 
116     public void testDispatchAndReply() throws Exception
117     {
118         MuleManager.getConfiguration().setSynchronous(false);
119         descriptor = getTestDescriptor("testComponent", FunctionalTestComponent.class.getName());
120 
121         initialiseComponent(descriptor, new EventCallback()
122         {
123             public void eventReceived(UMOEventContext context, Object component) throws Exception
124             {
125                 callbackCount++;
126                 String result = "Received Async event: " + context.getMessageAsString();
127                 assertNotNull(context.getOutputStream());
128                 assertNotNull(context.getMessage().getProperty(SslConnector.LOCAL_CERTIFICATES));
129 
130                 if (!((ResponseOutputStream)context.getOutputStream()).getSocket().isClosed())
131                 {
132                     context.getOutputStream().write(result.getBytes());
133                     context.getOutputStream().flush();
134                 }
135 
136                 callbackCalled = true;
137             }
138         });
139         // Start the server
140         MuleManager.getInstance().start();
141 
142         URI uri = getInDest().getUri();
143         s = createSocket(uri);
144         DataOutputStream dos = new DataOutputStream((s.getOutputStream()));
145         dos.write("Hello".getBytes());
146         dos.flush();
147 
148         afterInitialise();
149 
150         DataInputStream dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
151         byte[] buf = new byte[32];
152         int x = dis.read(buf);
153         assertTrue(x > -1);
154         assertTrue(new String(buf, 0, x).startsWith("Received Async event"));
155         assertEquals(1, callbackCount);
156 
157     }
158 }