1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2006-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   *  @version $Id: SSLConnectionTest.java 887 2007-02-13 08:16:30Z vlads $
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  		// Basicaly Performs HTTPS GET
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 			//TODO assertNotNull("Certificate.getSubject()", cert.getSubject());
107 			assertNotNull("Certificate.getIssuer()", cert.getIssuer());
108 			assertNotNull("Certificate.getType()", cert.getType());
109 			assertNotNull("Certificate.getVersion()", cert.getVersion());
110 			//TODO assertNotNull("Certificate.getSigAlgName()", cert.getSigAlgName());
111 			//TODO assertTrue("Certificate.getNotBefore()", cert.getNotBefore() >= 0);
112 			//TODO assertTrue("Certificate.getNotAfter()", cert.getNotAfter() >= 0);
113 			//TODO String serialNumber = cert.getSerialNumber();
114 			//TODO assertNotNull("Certificate.getSerialNumber()", serialNumber);
115 		} finally {
116 			sc.close();
117 		}
118     }
119 }