View Javadoc

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