1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2006-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  It is licensed under the following two licenses as alternatives:
7    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
8    *    2. Apache License (the "AL") Version 2.0
9    *
10   *  You may not use this file except in compliance with at least one of
11   *  the above two licenses.
12   *
13   *  You may obtain a copy of the LGPL at
14   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
15   *
16   *  You may obtain a copy of the AL at
17   *      http://www.apache.org/licenses/LICENSE-2.0
18   *
19   *  Unless required by applicable law or agreed to in writing, software
20   *  distributed under the License is distributed on an "AS IS" BASIS,
21   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   *  See the LGPL or the AL for the specific language governing permissions and
23   *  limitations.
24   *
25   *  @version $Id: HttpsConnectionTest.java 1605 2008-02-25 21:07:14Z barteo $
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          	// Produce java.io.IOException: HTTPS hostname wrong:  should be <www.pyx4me.com>
88          	hc.getResponseCode();
89          	fail("Should produce IOException");
90          } catch (IOException e) {
91  		} finally {
92  			hc.close();
93  		}
94      }
95  }