1
2
3
4
5
6
7
8
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.endpoint.InboundEndpoint;
18 import org.mule.api.lifecycle.CreateException;
19 import org.mule.api.service.Service;
20 import org.mule.api.transport.Connector;
21 import org.mule.config.DefaultMuleConfiguration;
22 import org.mule.service.ServiceCompositeMessageSource;
23 import org.mule.tck.AbstractMuleTestCase;
24 import org.mule.transport.http.HttpsConnector;
25 import org.mule.transport.http.HttpsMessageReceiver;
26 import org.mule.transport.ssl.MockHandshakeCompletedEvent;
27 import org.mule.transport.ssl.MockSslSocket;
28
29 import com.mockobjects.dynamic.Mock;
30
31 import java.io.IOException;
32 import java.lang.reflect.InvocationTargetException;
33 import java.lang.reflect.Method;
34 import java.net.Socket;
35 import java.util.Map;
36
37 import javax.net.ssl.HandshakeCompletedEvent;
38 import javax.resource.spi.work.Work;
39
40 import edu.emory.mathcs.backport.java.util.Collections;
41
42
43
44
45
46
47
48
49
50
51 public class HttpsHandshakeTimingTestCase extends AbstractMuleTestCase
52 {
53 public void testHttpsHandshakeExceedsTimeout() throws Exception
54 {
55 MockHttpsMessageReceiver messageReceiver = setupMockHttpsMessageReceiver();
56
57 MockSslSocket socket = new MockSslSocket();
58 Work work = messageReceiver.createWork(socket);
59 assertNotNull(work);
60
61 MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
62 try
63 {
64
65
66 invokePreRouteMessage(work, message);
67 fail();
68 }
69 catch (InvocationTargetException ite)
70 {
71 assertTrue(ite.getCause() instanceof MessagingException);
72 assertTrue(ite.getCause().getMessage().contains("handshake"));
73 }
74 }
75
76 public void testHttpsHandshakeCompletesBeforeProcessingMessage() throws Exception
77 {
78 MockHttpsMessageReceiver messageReceiver = setupMockHttpsMessageReceiver();
79
80 MockSslSocket socket = new MockSslSocket();
81 Work work = messageReceiver.createWork(socket);
82 assertNotNull(work);
83
84 invokeHandshakeCompleted(work, socket);
85
86 MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
87 invokePreRouteMessage(work, message);
88 assertNotNull(message.<Object>getInboundProperty(MuleProperties.MULE_REMOTE_CLIENT_ADDRESS));
89 }
90
91 private void invokeHandshakeCompleted(Work work, MockSslSocket socket) throws Exception
92 {
93 Method handshakeCompleted = work.getClass().getDeclaredMethod("handshakeCompleted", HandshakeCompletedEvent.class);
94 assertNotNull(handshakeCompleted);
95 handshakeCompleted.setAccessible(true);
96 HandshakeCompletedEvent event = new MockHandshakeCompletedEvent(socket);
97 handshakeCompleted.invoke(work, new Object[] { event });
98 }
99
100 private void invokePreRouteMessage(Work work, MuleMessage message) throws Exception
101 {
102 Method preRouteMessage = work.getClass().getDeclaredMethod("preRouteMessage", MuleMessage.class);
103 assertNotNull(preRouteMessage);
104 preRouteMessage.setAccessible(true);
105 preRouteMessage.invoke(work, new Object[] { message });
106 }
107
108 private MockHttpsMessageReceiver setupMockHttpsMessageReceiver() throws CreateException
109 {
110 HttpsConnector httpsConnector = new HttpsConnector(muleContext);
111 httpsConnector.setSslHandshakeTimeout(1000);
112
113 Map properties = Collections.emptyMap();
114
115 Mock mockEndpoint = new Mock(InboundEndpoint.class);
116 mockEndpoint.expectAndReturn("getConnector", httpsConnector);
117 mockEndpoint.expectAndReturn("getEncoding", new DefaultMuleConfiguration().getDefaultEncoding());
118 mockEndpoint.expectAndReturn("getProperties", properties);
119 mockEndpoint.expectAndReturn("getProperties", properties);
120 InboundEndpoint inboundEndpoint = (InboundEndpoint) mockEndpoint.proxy();
121
122 Mock mockService = new Mock(Service.class);
123 mockService.expectAndReturn("getResponseRouter", null);
124 mockService.expectAndReturn("getInboundRouter", new ServiceCompositeMessageSource());
125 Service service = (Service) mockService.proxy();
126
127 MockHttpsMessageReceiver messageReceiver = new MockHttpsMessageReceiver(httpsConnector, service, inboundEndpoint);
128 return messageReceiver;
129 }
130
131 private static class MockHttpsMessageReceiver extends HttpsMessageReceiver
132 {
133 public MockHttpsMessageReceiver(Connector connector, Service service, InboundEndpoint endpoint)
134 throws CreateException
135 {
136 super(connector, service, endpoint);
137 }
138
139
140
141
142 @Override
143 public Work createWork(Socket socket) throws IOException
144 {
145 return super.createWork(socket);
146 }
147 }
148
149 }