01: package org.bouncycastle.crypto.tls.test;
02:
03: import junit.framework.TestCase;
04: import junit.framework.TestSuite;
05: import org.bouncycastle.crypto.tls.AlwaysValidVerifyer;
06: import org.bouncycastle.crypto.tls.TlsProtocolHandler;
07: import org.bouncycastle.util.Arrays;
08: import org.bouncycastle.util.encoders.Hex;
09:
10: import java.io.IOException;
11: import java.io.InputStream;
12: import java.io.OutputStream;
13: import java.net.Socket;
14:
15: public class BasicTlsTest extends TestCase {
16: private static final int PORT_NO = 8003;
17: private static final String CLIENT = "client";
18: private static final char[] CLIENT_PASSWORD = "clientPassword"
19: .toCharArray();
20: private static final char[] SERVER_PASSWORD = "serverPassword"
21: .toCharArray();
22: private static final char[] TRUST_STORE_PASSWORD = "trustPassword"
23: .toCharArray();
24:
25: public void testConnection() throws Exception {
26: Thread server = new HTTPSServerThread();
27:
28: server.start();
29:
30: Thread.yield();
31:
32: AlwaysValidVerifyer verifyer = new AlwaysValidVerifyer();
33: Socket s = null;
34:
35: for (int i = 0; s == null && i != 3; i++) {
36: Thread.sleep(1000);
37:
38: try {
39: s = new Socket("localhost", PORT_NO);
40: } catch (IOException e) {
41: // ignore
42: }
43: }
44:
45: if (s == null) {
46: throw new IOException("unable to connect");
47: }
48:
49: long time = System.currentTimeMillis();
50: TlsProtocolHandler handler = new TlsProtocolHandler(s
51: .getInputStream(), s.getOutputStream());
52: handler.connect(verifyer);
53: InputStream is = handler.getTlsInputStream();
54: OutputStream os = handler.getTlsOuputStream();
55:
56: os.write("GET / HTTP/1.1\r\n\r\n".getBytes());
57:
58: time = System.currentTimeMillis();
59: byte[] buf = new byte[4096];
60: int read = 0;
61: int total = 0;
62:
63: while ((read = is.read(buf, total, buf.length - total)) > 0) {
64: total += read;
65: }
66:
67: is.close();
68:
69: byte[] expected = Hex
70: .decode("485454502f312e3120323030204f4b0d0a436f6e74656e742d547970653a20746578742f68"
71: + "746d6c0d0a0d0a3c68746d6c3e0d0a3c626f64793e0d0a48656c6c6f20576f726c64210d0a3c2f626f64793e0d0a3c2f"
72: + "68746d6c3e0d0a");
73: assertEquals(total, expected.length);
74:
75: byte[] tmp = new byte[expected.length];
76: System.arraycopy(buf, 0, tmp, 0, total);
77: assertTrue(Arrays.areEqual(expected, tmp));
78: }
79:
80: public static TestSuite suite() {
81: return new TestSuite(BasicTlsTest.class);
82: }
83:
84: public static void main(String[] args) throws Exception {
85: junit.textui.TestRunner.run(suite());
86: }
87: }
|