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