View Javadoc

1   /*
2    * $Id: HttpsHandshakeTimingTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transport.http.functional;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MessagingException;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.config.MuleProperties;
17  import org.mule.api.endpoint.InboundEndpoint;
18  import org.mule.api.lifecycle.CreateException;
19  import org.mule.api.service.Service;
20  import org.mule.api.transport.Connector;
21  import org.mule.config.DefaultMuleConfiguration;
22  import org.mule.service.ServiceCompositeMessageSource;
23  import org.mule.tck.AbstractMuleTestCase;
24  import org.mule.transport.http.HttpsConnector;
25  import org.mule.transport.http.HttpsMessageReceiver;
26  import org.mule.transport.ssl.MockHandshakeCompletedEvent;
27  import org.mule.transport.ssl.MockSslSocket;
28  
29  import com.mockobjects.dynamic.Mock;
30  
31  import java.io.IOException;
32  import java.lang.reflect.InvocationTargetException;
33  import java.lang.reflect.Method;
34  import java.net.Socket;
35  import java.util.Map;
36  
37  import javax.net.ssl.HandshakeCompletedEvent;
38  import javax.resource.spi.work.Work;
39  
40  import edu.emory.mathcs.backport.java.util.Collections;
41  
42  /**
43   * Test for SSL handshake timeouts. Unfortunately, there is no easy way to blackbox-test this 
44   * as it would require a SSLSocket implementation that could actually add arbitrary delays to 
45   * the SSL handshake.
46   * <p/>
47   * The approach chosen here is based on reflection and massive subclassing/stubbing to make things
48   * work. Yes, this is hacky and fragile but this seems to be the only reasonable alternative
49   * for now.
50   */
51  public class HttpsHandshakeTimingTestCase extends AbstractMuleTestCase
52  {
53      public void testHttpsHandshakeExceedsTimeout() throws Exception
54      {
55          MockHttpsMessageReceiver messageReceiver = setupMockHttpsMessageReceiver();
56          
57          MockSslSocket socket = new MockSslSocket();
58          Work work = messageReceiver.createWork(socket);
59          assertNotNull(work);
60          
61          MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
62          try
63          {
64              // note how preRouteMessage is invoked here without a prior handshakeComplete
65              // which would count down the latch that's used in HttpsWorker
66              invokePreRouteMessage(work, message);
67              fail();
68          }
69          catch (InvocationTargetException ite)
70          {
71              assertTrue(ite.getCause() instanceof MessagingException);
72              assertTrue(ite.getCause().getMessage().contains("handshake"));
73          }
74      }
75          
76      public void testHttpsHandshakeCompletesBeforeProcessingMessage() throws Exception
77      {
78          MockHttpsMessageReceiver messageReceiver = setupMockHttpsMessageReceiver();
79          
80          MockSslSocket socket = new MockSslSocket();
81          Work work = messageReceiver.createWork(socket);
82          assertNotNull(work);
83  
84          invokeHandshakeCompleted(work, socket);
85  
86          MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
87          invokePreRouteMessage(work, message);
88          assertNotNull(message.<Object>getInboundProperty(MuleProperties.MULE_REMOTE_CLIENT_ADDRESS));
89      }
90  
91      private void invokeHandshakeCompleted(Work work, MockSslSocket socket) throws Exception
92      {
93          Method handshakeCompleted = work.getClass().getDeclaredMethod("handshakeCompleted", HandshakeCompletedEvent.class);
94          assertNotNull(handshakeCompleted);
95          handshakeCompleted.setAccessible(true);
96          HandshakeCompletedEvent event = new MockHandshakeCompletedEvent(socket);
97          handshakeCompleted.invoke(work, new Object[] { event });
98      }
99  
100     private void invokePreRouteMessage(Work work, MuleMessage message) throws Exception
101     {
102         Method preRouteMessage = work.getClass().getDeclaredMethod("preRouteMessage", MuleMessage.class);
103         assertNotNull(preRouteMessage);
104         preRouteMessage.setAccessible(true);
105         preRouteMessage.invoke(work, new Object[] { message });
106     }
107 
108     private MockHttpsMessageReceiver setupMockHttpsMessageReceiver() throws CreateException
109     {
110         HttpsConnector httpsConnector = new HttpsConnector(muleContext);
111         httpsConnector.setSslHandshakeTimeout(1000);
112         
113         Map properties = Collections.emptyMap();
114         
115         Mock mockEndpoint = new Mock(InboundEndpoint.class);
116         mockEndpoint.expectAndReturn("getConnector", httpsConnector);
117         mockEndpoint.expectAndReturn("getEncoding", new DefaultMuleConfiguration().getDefaultEncoding());
118         mockEndpoint.expectAndReturn("getProperties", properties);
119         mockEndpoint.expectAndReturn("getProperties", properties);
120         InboundEndpoint inboundEndpoint = (InboundEndpoint) mockEndpoint.proxy();
121         
122         Mock mockService = new Mock(Service.class);
123         mockService.expectAndReturn("getResponseRouter", null);
124         mockService.expectAndReturn("getInboundRouter", new ServiceCompositeMessageSource());
125         Service service = (Service) mockService.proxy();
126         
127         MockHttpsMessageReceiver messageReceiver = new MockHttpsMessageReceiver(httpsConnector, service, inboundEndpoint);
128         return messageReceiver;
129     }
130 
131     private static class MockHttpsMessageReceiver extends HttpsMessageReceiver
132     {
133         public MockHttpsMessageReceiver(Connector connector, Service service, InboundEndpoint endpoint)
134             throws CreateException
135         {
136             super(connector, service, endpoint);
137         }
138 
139         /**
140          * Open up access for unit test
141          */
142         @Override
143         public Work createWork(Socket socket) throws IOException
144         {
145             return super.createWork(socket);
146         }
147     }
148 
149 }