View Javadoc

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