1
2
3
4
5
6
7
8
9
10
11 package org.mule.providers.ssl;
12
13 import org.mule.MuleManager;
14 import org.mule.impl.ResponseOutputStream;
15 import org.mule.impl.endpoint.MuleEndpointURI;
16 import org.mule.tck.functional.AbstractProviderFunctionalTestCase;
17 import org.mule.tck.functional.EventCallback;
18 import org.mule.tck.functional.FunctionalTestComponent;
19 import org.mule.umo.UMOEventContext;
20 import org.mule.umo.endpoint.MalformedEndpointException;
21 import org.mule.umo.endpoint.UMOEndpointURI;
22 import org.mule.umo.provider.UMOConnector;
23
24 import java.io.BufferedInputStream;
25 import java.io.BufferedOutputStream;
26 import java.io.DataInputStream;
27 import java.io.DataOutputStream;
28 import java.net.Socket;
29 import java.net.URI;
30
31 import javax.net.ssl.SSLContext;
32 import javax.net.ssl.SSLSocketFactory;
33
34
35
36
37
38
39
40 public class SslConnectorFunctionalTestCase extends AbstractProviderFunctionalTestCase
41 {
42 private int port = 61655;
43 private Socket s;
44
45 protected UMOEndpointURI getInDest()
46 {
47 try
48 {
49 logger.debug("Using port " + port);
50 return new MuleEndpointURI("ssl://localhost:" + port);
51 }
52 catch (MalformedEndpointException e)
53 {
54 fail(e.getMessage());
55 return null;
56 }
57 }
58
59 protected UMOEndpointURI getOutDest()
60 {
61 return null;
62 }
63
64 public UMOConnector createConnector() throws Exception
65 {
66 return SslConnectorTestCase.createConnector(false);
67 }
68
69 protected Socket createSocket(URI uri) throws Exception
70 {
71 SslConnector conn = (SslConnector)connector;
72 SSLContext context;
73 context = SSLContext.getInstance(conn.getProtocol());
74 context.init(conn.getKeyManagerFactory().getKeyManagers(), conn.getTrustManagerFactory()
75 .getTrustManagers(), null);
76 SSLSocketFactory factory = context.getSocketFactory();
77 Socket socket = factory.createSocket(uri.getHost(), uri.getPort());
78
79
80
81
82 return socket;
83 }
84
85 protected void doTearDown() throws Exception
86 {
87 if (s != null)
88 {
89 s.close();
90 s = null;
91 }
92 }
93
94 protected void sendTestData(int iterations) throws Exception
95 {
96 MuleManager.getConfiguration().setSynchronous(false);
97 URI uri = getInDest().getUri();
98 for (int i = 0; i < iterations; i++)
99 {
100 s = createSocket(uri);
101 DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
102 dos.write("Hello".getBytes());
103 dos.flush();
104 logger.info("Sent message: " + i);
105 dos.close();
106 }
107 }
108
109 protected void receiveAndTestResults() throws Exception
110 {
111 Thread.sleep(3000);
112 assertEquals(100, callbackCount);
113
114 }
115
116 public void testDispatchAndReply() throws Exception
117 {
118 MuleManager.getConfiguration().setSynchronous(false);
119 descriptor = getTestDescriptor("testComponent", FunctionalTestComponent.class.getName());
120
121 initialiseComponent(descriptor, new EventCallback()
122 {
123 public void eventReceived(UMOEventContext context, Object component) throws Exception
124 {
125 callbackCount++;
126 String result = "Received Async event: " + context.getMessageAsString();
127 assertNotNull(context.getOutputStream());
128
129 if (!((ResponseOutputStream)context.getOutputStream()).getSocket().isClosed())
130 {
131 context.getOutputStream().write(result.getBytes());
132 context.getOutputStream().flush();
133 }
134
135 callbackCalled = true;
136 }
137 });
138
139 MuleManager.getInstance().start();
140
141 URI uri = getInDest().getUri();
142 s = createSocket(uri);
143 DataOutputStream dos = new DataOutputStream((s.getOutputStream()));
144 dos.write("Hello".getBytes());
145 dos.flush();
146
147 afterInitialise();
148
149 DataInputStream dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
150 byte[] buf = new byte[32];
151 int x = dis.read(buf);
152 assertTrue(x > -1);
153 assertTrue(new String(buf, 0, x).startsWith("Received Async event"));
154 assertEquals(1, callbackCount);
155
156 }
157 }