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.ssl;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.endpoint.InboundEndpoint;
12  import org.mule.api.service.Service;
13  import org.mule.config.DefaultMuleConfiguration;
14  import org.mule.service.ServiceCompositeMessageSource;
15  import org.mule.tck.junit4.AbstractMuleContextTestCase;
16  
17  import com.mockobjects.dynamic.Mock;
18  
19  import java.lang.reflect.InvocationTargetException;
20  import java.lang.reflect.Method;
21  import java.util.Collections;
22  import java.util.Map;
23  
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertNotNull;
27  import static org.junit.Assert.assertTrue;
28  import static org.junit.Assert.fail;
29  
30  /**
31   * Whitebox test for the SSL latch in SslMessageReceiver. The use of reflection here is hacky
32   * but the alternative would be stubbing large parts of the JSSE classes in order to influence
33   * timing while establishing the SSL handshake (wich sounds even hackier than this test).
34   */
35  public class SslHandshakeTimingTestCase extends AbstractMuleContextTestCase
36  {
37  
38      @Test
39      public void testSslHandshakeTimeout() throws Exception
40      {
41          SslMessageReceiver receiver = setupMockSslMessageReciever();
42          
43          // note how we call preRoute without a prior handshakeCompleted ... this must
44          // run into a timeout
45          try
46          {
47              MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
48              callPreRoute(receiver, message);
49              fail();
50          }
51          catch (InvocationTargetException ite)
52          {
53              Throwable cause = ite.getCause();
54              assertTrue(cause instanceof IllegalStateException);
55          }
56      }
57      
58      @Test
59      public void testSslHandshakeSuccessful() throws Exception
60      {
61          SslMessageReceiver receiver = setupMockSslMessageReciever();
62                  
63          MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
64          receiver.handshakeCompleted(new MockHandshakeCompletedEvent());
65          callPreRoute(receiver, message);
66          
67          assertNotNull(message.getOutboundProperty(SslConnector.PEER_CERTIFICATES));
68          assertNotNull(message.getOutboundProperty(SslConnector.LOCAL_CERTIFICATES));
69      }
70  
71      private SslMessageReceiver setupMockSslMessageReciever() throws Exception
72      {
73          SslConnector connector = new SslConnector(muleContext);
74          connector.setSslHandshakeTimeout(1000);
75          
76          Mock mockService = new Mock(Service.class);
77          mockService.expect("getResponseRouter");
78          mockService.expectAndReturn("getInboundRouter", new ServiceCompositeMessageSource());
79          Service service = (Service) mockService.proxy();
80          
81          Map<String, Object> properties = Collections.emptyMap();
82  
83          Mock mockEndpoint = new Mock(InboundEndpoint.class);
84          mockEndpoint.expectAndReturn("getConnector", connector);
85          mockEndpoint.expectAndReturn("getEncoding", new DefaultMuleConfiguration().getDefaultEncoding());
86          mockEndpoint.expectAndReturn("getProperties", properties);
87          mockEndpoint.expectAndReturn("getProperties", properties);
88          InboundEndpoint endpoint = (InboundEndpoint) mockEndpoint.proxy();
89  
90          return new SslMessageReceiver(connector, service, endpoint);
91      }
92  
93      private void callPreRoute(SslMessageReceiver receiver, MuleMessage message) throws Exception
94      {
95          Method preRouteMessage = receiver.getClass().getDeclaredMethod("preRoute", MuleMessage.class);
96          assertNotNull(preRouteMessage);
97          preRouteMessage.setAccessible(true);
98                  
99          preRouteMessage.invoke(receiver, new Object[] { message });
100     }
101 }