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.IOException;
30
31 import javax.microedition.pki.Certificate;
32
33 public class HttpsConnectionTest extends BaseTestHttpConnection {
34
35 private static final String testInetHTTPUrl = "https://" + TEST_HOST + testFile;
36
37 protected void setUp() throws Exception {
38 super.setUp();
39 super.setupSSLContext();
40 }
41
42 protected HttpConnection openHttpConnection(String query) throws IOException {
43 return (HttpsConnection)Connector.open("https://" + TEST_HOST + query, Connector.READ, true);
44 }
45
46 public void testConnection() throws IOException {
47 HttpsConnection hc = (HttpsConnection)Connector.open(testInetHTTPUrl, Connector.READ, true);
48 try {
49 assertEquals("getResponseCode()", HttpConnection.HTTP_OK, hc.getResponseCode());
50 assertEquals("getPort()", 443, hc.getPort());
51 assertEquals("getProtocol()", "https", hc.getProtocol());
52 assertEquals("getURL()", testInetHTTPUrl, hc.getURL());
53 assertHttpConnectionMethods(hc);
54 } finally {
55 hc.close();
56 }
57 }
58
59 public void testSecurityInfo() throws IOException {
60 HttpsConnection hc = (HttpsConnection)Connector.open(testInetHTTPUrl, Connector.READ, true);
61 try {
62 assertEquals("getResponseCode()", HttpConnection.HTTP_OK, hc.getResponseCode());
63 SecurityInfo si = hc.getSecurityInfo();
64 assertNotNull("HttpsConnection.getSecurityInfo()", si);
65 assertNotNull("SecurityInfo.getProtocolVersion()", si.getProtocolVersion());
66 assertNotNull("SecurityInfo.getProtocolName()", si.getProtocolName());
67 assertNotNull("SecurityInfo.getCipherSuite()", si.getCipherSuite());
68 Certificate cert = si.getServerCertificate();
69 assertNotNull("SecurityInfo.getServerCertificate()", cert);
70 assertNotNull("Certificate.getSubject()", cert.getSubject());
71 assertNotNull("Certificate.getIssuer()", cert.getIssuer());
72 assertNotNull("Certificate.getType()", cert.getType());
73 assertNotNull("Certificate.getVersion()", cert.getVersion());
74 assertNotNull("Certificate.getSigAlgName()", cert.getSigAlgName());
75 assertTrue("Certificate.getNotBefore()", cert.getNotBefore() >= 0);
76 assertTrue("Certificate.getNotAfter()", cert.getNotAfter() >= 0);
77 String serialNumber = cert.getSerialNumber();
78 assertNotNull("Certificate.getSerialNumber()", serialNumber);
79 } finally {
80 hc.close();
81 }
82 }
83
84 public void testWrongCertificate() throws IOException {
85 HttpsConnection hc = (HttpsConnection)Connector.open("https://www.pyx4me.com" + testFile, Connector.READ, true);
86 try {
87
88 hc.getResponseCode();
89 fail("Should produce IOException");
90 } catch (IOException e) {
91 } finally {
92 hc.close();
93 }
94 }
95 }