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: SSLConnectionTest.java 1605 2008-02-25 21:07:14Z barteo $
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  		// Basicaly Performs HTTPS GET
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 			//TODO assertNotNull("Certificate.getSubject()", cert.getSubject());
112 			assertNotNull("Certificate.getIssuer()", cert.getIssuer());
113 			assertNotNull("Certificate.getType()", cert.getType());
114 			assertNotNull("Certificate.getVersion()", cert.getVersion());
115 			//TODO assertNotNull("Certificate.getSigAlgName()", cert.getSigAlgName());
116 			//TODO assertTrue("Certificate.getNotBefore()", cert.getNotBefore() >= 0);
117 			//TODO assertTrue("Certificate.getNotAfter()", cert.getNotAfter() >= 0);
118 			//TODO String serialNumber = cert.getSerialNumber();
119 			//TODO assertNotNull("Certificate.getSerialNumber()", serialNumber);
120 		} finally {
121 			sc.close();
122 		}
123     }
124 }