1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package javax.microedition.io;
23
24 import java.io.EOFException;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28
29 import javax.microedition.pki.Certificate;
30
31 public class SSLConnectionTest extends BaseGCFTestCase {
32
33 private static final String TEST_PORT = "443";
34
35 public static final String CRLF = "\r\n";
36
37
38 private void write(OutputStream os, String txt) throws IOException {
39 os.write(txt.getBytes());
40 os.write(CRLF.getBytes());
41 }
42
43 private void validateHTTPReply(InputStream is) throws IOException {
44 StringBuffer buf = new StringBuffer();
45 int ch, ch1 = 0;
46 while ((ch = is.read()) != -1) {
47 if ((ch == '\n') && (ch1 == '\r')) {
48 break;
49 }
50 buf.append((char) ch);
51 ch1 = ch;
52 }
53 if ((!buf.toString().startsWith("HTTP/1.0 200")) &&
54 (!buf.toString().startsWith("HTTP/1.1 200"))){
55 buf.append("[EOF]");
56 throw new IOException(buf.toString());
57 }
58 if (ch == -1) {
59 throw new EOFException("No http header from proxy");
60 }
61 int crCount = 0;
62 while ((ch = is.read()) != -1) {
63 if ((ch == '\n') && (ch1 == '\r')) {
64 crCount++;
65 if (crCount == 2) {
66 break;
67 }
68 } else if (ch != '\r') {
69 crCount = 0;
70 }
71 ch1 = ch;
72 }
73 if (ch == -1) {
74 throw new EOFException("No data after http header from proxy");
75 }
76 }
77
78 public void testSecureConnection() throws IOException {
79
80 SecureConnection sc = (SecureConnection) Connector.open("ssl://" + TEST_HOST + ":" + TEST_PORT);
81
82 sc.setSocketOption(SocketConnection.LINGER, 5);
83
84 OutputStream os = sc.openOutputStream();
85 write(os, "GET /robots.txt HTTP/1.0");
86 write(os, "User-Agent: UNTRUSTED/1.0");
87 write(os, "Host: " + TEST_HOST);
88 os.write(CRLF.getBytes());
89 os.flush();
90
91 InputStream is = sc.openInputStream();
92 validateHTTPReply(is);
93 sc.close();
94 }
95
96 public void testSecurityInfo() throws IOException {
97 SecureConnection sc = (SecureConnection) Connector.open("ssl://" + TEST_HOST + ":" + TEST_PORT);
98 try {
99 SecurityInfo si = sc.getSecurityInfo();
100 assertNotNull("SecureConnection.getSecurityInfo()", si);
101 assertNotNull("SecurityInfo.getProtocolVersion()", si.getProtocolVersion());
102 assertNotNull("SecurityInfo.getProtocolName()", si.getProtocolName());
103 assertNotNull("SecurityInfo.getCipherSuite()", si.getCipherSuite());
104 Certificate cert = si.getServerCertificate();
105 assertNotNull("SecurityInfo.getServerCertificate()", cert);
106
107 assertNotNull("Certificate.getIssuer()", cert.getIssuer());
108 assertNotNull("Certificate.getType()", cert.getType());
109 assertNotNull("Certificate.getVersion()", cert.getVersion());
110
111
112
113
114
115 } finally {
116 sc.close();
117 }
118 }
119 }