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