View Javadoc

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