You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2007/02/04 09:10:11 UTC
svn commit: r503371 [4/5] - in
/harmony/enhanced/classlib/trunk/modules/x-net: ./ META-INF/
src/main/java/org/apache/harmony/xnet/provider/jsse/
src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/
src/test/impl/java.injected/org/apache/harmo...
Added: harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSessionImplTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSessionImplTest.java?view=auto&rev=503371
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSessionImplTest.java (added)
+++ harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSessionImplTest.java Sun Feb 4 00:10:09 2007
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.xnet.provider.jsse;
+
+import java.security.SecureRandom;
+import java.util.Arrays;
+
+import javax.net.ssl.SSLPeerUnverifiedException;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests for <code>SSLSessionContextImp</code> constructor and methods
+ *
+ */
+public class SSLSessionImplTest extends TestCase {
+
+ /*
+ * Class under test for void SSLSessionImpl(CipherSuite, SecureRandom)
+ */
+ public void testSSLSessionImplCipherSuiteSecureRandom() {
+ SSLSessionImpl session = new SSLSessionImpl(null, null);
+ assertEquals(session.getCipherSuite(),
+ CipherSuite.TLS_NULL_WITH_NULL_NULL.getName());
+
+ session = new SSLSessionImpl(CipherSuite.TLS_RSA_WITH_NULL_MD5,
+ new SecureRandom());
+ session.protocol = ProtocolVersion.TLSv1;
+ assertEquals("Incorrect protocol", session.getProtocol(), "TLSv1");
+ assertEquals("Incorrect cipher suite", session.getCipherSuite(),
+ CipherSuite.TLS_RSA_WITH_NULL_MD5.getName());
+ assertEquals("Incorrect id", session.getId().length, 32);
+ assertTrue("Incorrect isValid", session.isValid());
+ assertTrue("Incorrect isServer", session.isServer);
+ long time = session.getCreationTime();
+ assertTrue("Incorrect CreationTime", time <= System.currentTimeMillis());
+ assertEquals("Incorrect LastAccessedTime", time, session.getLastAccessedTime());
+ assertEquals("Incorrect LocalCertificates", session.getLocalCertificates(), null);
+ assertEquals("Incorrect LocalPrincipal", session.getLocalPrincipal(), null);
+ assertEquals(session.getPeerHost(), null);
+ assertEquals(session.getPeerPort(), -1);
+ assertEquals(session.getSessionContext(), null);
+
+ try {
+ session.getPeerCertificateChain();
+ fail("getPeerCertificateChain: No expected SSLPeerUnverifiedException");
+ } catch (SSLPeerUnverifiedException e) {
+ }
+
+ try {
+ session.getPeerCertificates();
+ fail("getPeerCertificates: No expected SSLPeerUnverifiedException");
+ } catch (SSLPeerUnverifiedException e) {
+ }
+
+ try {
+ session.getPeerPrincipal();
+ fail("getPeerPrincipal: No expected SSLPeerUnverifiedException");
+ } catch (SSLPeerUnverifiedException e) {
+ }
+ }
+
+ public void testGetApplicationBufferSize() {
+ assertEquals(SSLSessionImpl.NULL_SESSION.getApplicationBufferSize(),
+ SSLRecordProtocol.MAX_DATA_LENGTH);
+ }
+
+ public void testGetPacketBufferSize() {
+ assertEquals(SSLSessionImpl.NULL_SESSION.getPacketBufferSize(),
+ SSLRecordProtocol.MAX_SSL_PACKET_SIZE);
+ }
+
+ public void testInvalidate() {
+ SSLSessionImpl session = new SSLSessionImpl(
+ CipherSuite.TLS_RSA_WITH_NULL_MD5, new SecureRandom());
+ session.invalidate();
+ assertFalse("Incorrect isValid", session.isValid());
+
+ }
+
+ public void testSetPeer() {
+ SSLSessionImpl session = new SSLSessionImpl(null);
+ session.setPeer("someHost", 8080);
+ assertEquals(session.getPeerHost(), "someHost");
+ assertEquals(session.getPeerPort(), 8080);
+ }
+
+
+ public void testGetValue() {
+ SSLSessionImpl session = new SSLSessionImpl(null);
+
+ assertEquals(session.getValueNames().length, 0);
+
+ try {
+ session.getValue(null);
+ fail("No expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ }
+ assertEquals(session.getValue("abc"), null);
+
+ try {
+ session.removeValue(null);
+ fail("No expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ }
+ session.removeValue("abc");
+
+ try {
+ session.putValue(null, "1");
+ fail("No expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ }
+
+ try {
+ session.putValue("abc", null);
+ fail("No expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ }
+
+ Object o = new Object();
+ session.putValue("abc", o);
+ assertSame(session.getValue("abc"), o);
+ assertEquals(session.getValueNames()[0], "abc");
+
+ session.removeValue("abc");
+ assertEquals(session.getValue("abc"), null);
+ }
+
+ public void testClone() {
+ SSLSessionImpl session1 = new SSLSessionImpl(
+ CipherSuite.TLS_RSA_WITH_NULL_MD5, new SecureRandom());
+ SSLSessionImpl session2 = (SSLSessionImpl)session1.clone();
+ assertTrue(Arrays.equals(session1.getId(), session2.getId()));
+ }
+
+}
\ No newline at end of file
Propchange: harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSessionImplTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketFactoriesTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketFactoriesTest.java?view=auto&rev=503371
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketFactoriesTest.java (added)
+++ harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketFactoriesTest.java Sun Feb 4 00:10:09 2007
@@ -0,0 +1,464 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.xnet.provider.jsse;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.util.Arrays;
+import javax.net.ssl.SSLServerSocket;
+import javax.net.ssl.SSLServerSocketFactory;
+import javax.net.ssl.SSLSocket;
+import javax.net.ssl.SSLSocketFactory;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * SSLSocketImplTest
+ */
+public class SSLSocketFactoriesTest extends TestCase {
+
+ // turn on/off the debug logging
+ private boolean doLog = false;
+
+ /**
+ * Sets up the test case.
+ */
+ public void setUp() throws Exception {
+ super.setUp();
+ if (doLog) {
+ System.out.println("========================");
+ System.out.println("====== Running the test: " + getName());
+ System.out.println("========================");
+ }
+ }
+
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ /**
+ * Tests default initialized factories.
+ */
+ public void testDefaultInitialized() throws Exception {
+
+ SSLServerSocketFactory ssfactory =
+ (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
+ SSLSocketFactory sfactory =
+ (SSLSocketFactory) SSLSocketFactory.getDefault();
+
+ assertNotNull(ssfactory.getDefaultCipherSuites());
+ assertNotNull(ssfactory.getSupportedCipherSuites());
+ assertNotNull(ssfactory.createServerSocket());
+
+ assertNotNull(sfactory.getDefaultCipherSuites());
+ assertNotNull(sfactory.getSupportedCipherSuites());
+ assertNotNull(sfactory.createSocket());
+ }
+
+ public void testSocketCreation() throws Throwable {
+ SSLSocketFactory socketFactory
+ = new SSLSocketFactoryImpl(JSSETestData.getSSLParameters());
+ SSLServerSocketFactory serverSocketFactory
+ = new SSLServerSocketFactoryImpl(JSSETestData.getSSLParameters());
+
+ String[] enabled = {"TLS_RSA_WITH_RC4_128_MD5"};
+ for (int i=0; i<4; i++) {
+ SSLServerSocket ssocket;
+ switch (i) {
+ case 0:
+ if (doLog) {
+ System.out.println(
+ "*** ServerSocketFactory.createServerSocket()");
+ }
+ ssocket = (SSLServerSocket)
+ serverSocketFactory.createServerSocket();
+ ssocket.bind(null);
+ break;
+ case 1:
+ if (doLog) {
+ System.out.println(
+ "*** ServerSocketFactory.createServerSocket(int)");
+ }
+ ssocket = (SSLServerSocket)
+ serverSocketFactory.createServerSocket(0);
+ break;
+ case 2:
+ if (doLog) {
+ System.out.println(
+ "*** ServerSocketFactory.createServerSocket(int,int)");
+ }
+ ssocket = (SSLServerSocket)
+ serverSocketFactory.createServerSocket(0, 6);
+ break;
+ default:
+ if (doLog) {
+ System.out.println("*** ServerSocketFactory."
+ + "createServerSocket(int,int,InetAddress)");
+ }
+ ssocket = (SSLServerSocket)
+ serverSocketFactory.createServerSocket(0, 6, null);
+ break;
+ }
+ ssocket.setUseClientMode(false);
+ ssocket.setEnabledCipherSuites(enabled);
+ for (int j=0; j<6; j++) {
+ SSLSocket csocket;
+ switch (j) {
+ case 0:
+ if (doLog) {
+ System.out.println(
+ "=== SocketFactory.createSocket()");
+ }
+ csocket = (SSLSocket) socketFactory.createSocket();
+ csocket.connect(
+ new InetSocketAddress("localhost",
+ ssocket.getLocalPort()));
+ break;
+ case 1:
+ if (doLog) {
+ System.out.println(
+ "=== SocketFactory.createSocket(String,int)");
+ }
+ csocket = (SSLSocket)
+ socketFactory.createSocket("localhost",
+ ssocket.getLocalPort());
+ break;
+ case 2:
+ if (doLog) {
+ System.out.println("=== SocketFactory.createSocket("
+ + "String,int,InetAddress,int)");
+ }
+ csocket = (SSLSocket)
+ socketFactory.createSocket("localhost",
+ ssocket.getLocalPort(),
+ InetAddress.getByName("localhost"), 0);
+ break;
+ case 3:
+ if (doLog) {
+ System.out.println("=== SocketFactory.createSocket("
+ + "InetAddress,int)");
+ }
+ csocket = (SSLSocket) socketFactory.createSocket(
+ InetAddress.getByName("localhost"),
+ ssocket.getLocalPort());
+ break;
+ case 4:
+ if (doLog) {
+ System.out.println("=== SocketFactory.createSocket("
+ + "InetAddress,int,InetAddress,int)");
+ }
+ csocket = (SSLSocket) socketFactory.createSocket(
+ InetAddress.getByName("localhost"),
+ ssocket.getLocalPort(),
+ InetAddress.getByName("localhost"),
+ 0);
+ break;
+ default:
+ if (doLog) {
+ System.out.println(
+ "=== SSLSocketFactory.createSocket("
+ + "socket,String,int,boolean)");
+ }
+ Socket socket = new Socket(
+ InetAddress.getByName("localhost"),
+ ssocket.getLocalPort());
+ csocket = (SSLSocket) socketFactory.createSocket(
+ socket, "localhost", ssocket.getLocalPort(),
+ true);
+ break;
+
+ }
+ csocket.setUseClientMode(true);
+ csocket.setEnabledCipherSuites(enabled);
+ doTest(ssocket, csocket);
+ }
+ }
+ }
+
+ /**
+ * SSLSocketFactory.getSupportedCipherSuites() method testing.
+ */
+ public void testGetSupportedCipherSuites1() throws Exception {
+ SSLSocketFactory socketFactory
+ = new SSLSocketFactoryImpl(JSSETestData.getSSLParameters());
+ String[] supported = socketFactory.getSupportedCipherSuites();
+ assertNotNull(supported);
+ supported[0] = "NOT_SUPPORTED_CIPHER_SUITE";
+ supported = socketFactory.getSupportedCipherSuites();
+ for (int i=0; i<supported.length; i++) {
+ if ("NOT_SUPPORTED_CIPHER_SUITE".equals(supported[i])) {
+ fail("Modification of the returned result "
+ + "causes the modification of the internal state");
+ }
+ }
+ }
+
+ /**
+ * SSLServerSocketFactory.getSupportedCipherSuites() method testing.
+ */
+ public void testGetSupportedCipherSuites2() throws Exception {
+ SSLServerSocketFactory serverSocketFactory
+ = new SSLServerSocketFactoryImpl(JSSETestData.getSSLParameters());
+ String[] supported = serverSocketFactory.getSupportedCipherSuites();
+ assertNotNull(supported);
+ supported[0] = "NOT_SUPPORTED_CIPHER_SUITE";
+ supported = serverSocketFactory.getSupportedCipherSuites();
+ for (int i=0; i<supported.length; i++) {
+ if ("NOT_SUPPORTED_CIPHER_SUITE".equals(supported[i])) {
+ fail("Modification of the returned result "
+ + "causes the modification of the internal state");
+ }
+ }
+ }
+
+ /**
+ * SSLSocketFactory.getDefaultCipherSuites() method testing.
+ */
+ public void testGetDefaultCipherSuites1() throws Exception {
+ SSLSocketFactory socketFactory
+ = new SSLSocketFactoryImpl(JSSETestData.getSSLParameters());
+ String[] supported = socketFactory.getSupportedCipherSuites();
+ String[] defaultcs = socketFactory.getDefaultCipherSuites();
+ assertNotNull(supported);
+ assertNotNull(defaultcs);
+ for (int i=0; i<defaultcs.length; i++) {
+ found: {
+ for (int j=0; j<supported.length; j++) {
+ if (defaultcs[i].equals(supported[j])) {
+ break found;
+ }
+ }
+ fail("Default suite does not belong to the set "
+ + "of supported cipher suites: " + defaultcs[i]);
+ }
+ }
+ }
+
+ /**
+ * SSLServerSocketFactory.getDefaultCipherSuites() method testing.
+ */
+ public void testGetDefaultCipherSuites2() throws Exception {
+ SSLServerSocketFactory serverSocketFactory
+ = new SSLServerSocketFactoryImpl(JSSETestData.getSSLParameters());
+ String[] supported = serverSocketFactory.getSupportedCipherSuites();
+ String[] defaultcs = serverSocketFactory.getDefaultCipherSuites();
+ assertNotNull(supported);
+ assertNotNull(defaultcs);
+ for (int i=0; i<defaultcs.length; i++) {
+ found: {
+ for (int j=0; j<supported.length; j++) {
+ if (defaultcs[i].equals(supported[j])) {
+ break found;
+ }
+ }
+ fail("Default suite does not belong to the set "
+ + "of supported cipher suites: " + defaultcs[i]);
+ }
+ }
+ }
+
+ /**
+ * Performs SSL connection between the sockets
+ * @return
+ */
+ public void doTest(SSLServerSocket ssocket, SSLSocket csocket)
+ throws Throwable {
+ final String server_message = "Hello from SSL Server Socket!";
+ final String client_message = "Hello from SSL Socket!";
+ Thread server = null;
+ Thread client = null;
+ final Throwable[] throwed = new Throwable[1];
+ try {
+ final SSLServerSocket ss = ssocket;
+ final SSLSocket s = csocket;
+ server = new Thread() {
+ public void run() {
+ InputStream is = null;
+ OutputStream os = null;
+ SSLSocket s = null;
+ try {
+ s = (SSLSocket) ss.accept();
+ if (doLog) {
+ System.out.println("Socket accepted: " + s);
+ }
+ is = s.getInputStream();
+ os = s.getOutputStream();
+ // send the message to the client
+ os.write(server_message.getBytes());
+ // read the response
+ byte[] buff = new byte[client_message.length()];
+ int len = is.read(buff);
+ if (doLog) {
+ System.out.println("Received message of length "
+ + len + ": '" + new String(buff, 0, len)+"'");
+ }
+ assertTrue("Read message does not equal to expected",
+ Arrays.equals(client_message.getBytes(), buff));
+ os.write(-1);
+ assertTrue("Read data differs from expected",
+ is.read() == 255);
+ if (doLog) {
+ System.out.println("Server is closed: "
+ +s.isClosed());
+ }
+ assertTrue("Returned value should be -1",
+ // initiate an exchange of closure alerts
+ is.read() == -1);
+ if (doLog) {
+ System.out.println("Server is closed: "
+ +s.isClosed());
+ }
+ assertTrue("Returned value should be -1",
+ // initiate an exchange of closure alerts
+ is.read() == -1);
+ } catch (Throwable e) {
+ synchronized (throwed) {
+ if (doLog) {
+ e.printStackTrace();
+ }
+ if (throwed[0] == null) {
+ throwed[0] = e;
+ }
+ }
+ } finally {
+ try {
+ if (is != null) {
+ is.close();
+ }
+ } catch (IOException ex) {}
+ try {
+ if (os != null) {
+ os.close();
+ }
+ } catch (IOException ex) {}
+ try {
+ if (s != null) {
+ s.close();
+ }
+ } catch (IOException ex) {}
+ }
+ }
+ };
+
+ client = new Thread() {
+ public void run() {
+ InputStream is = null;
+ OutputStream os = null;
+ try {
+ assertTrue("Client was not connected", s.isConnected());
+ if (doLog) {
+ System.out.println("Client connected");
+ }
+ is = s.getInputStream();
+ os = s.getOutputStream();
+ s.startHandshake();
+ if (doLog) {
+ System.out.println("Client: HS was done");
+ }
+ // read the message from the server
+ byte[] buff = new byte[server_message.length()];
+ int len = is.read(buff);
+ if (doLog) {
+ System.out.println("Received message of length "
+ + len + ": '" + new String(buff, 0, len)+"'");
+ }
+ assertTrue("Read message does not equal to expected",
+ Arrays.equals(server_message.getBytes(), buff));
+ // send the response
+ buff = (" "+client_message+" ").getBytes();
+ os.write(buff, 1, buff.length-2);
+ assertTrue("Read data differs from expected",
+ is.read() == 255);
+ os.write(-1);
+ if (doLog) {
+ System.out.println("Client is closed: "
+ +s.isClosed());
+ }
+ s.close();
+ if (doLog) {
+ System.out.println("Client is closed: "
+ +s.isClosed());
+ }
+ } catch (Throwable e) {
+ synchronized (throwed) {
+ if (doLog) {
+ e.printStackTrace();
+ }
+ if (throwed[0] == null) {
+ throwed[0] = e;
+ }
+ }
+ } finally {
+ try {
+ if (is != null) {
+ is.close();
+ }
+ } catch (IOException ex) {}
+ try {
+ if (os != null) {
+ os.close();
+ }
+ } catch (IOException ex) {}
+ try {
+ if (s != null) {
+ s.close();
+ }
+ } catch (IOException ex) {}
+ }
+ }
+ };
+
+ server.start();
+ client.start();
+
+ while (server.isAlive() || client.isAlive()) {
+ if (throwed[0] != null) {
+ throw throwed[0];
+ }
+ try {
+ Thread.sleep(500);
+ } catch (Exception e) { }
+ }
+ } finally {
+ if (server != null) {
+ server.stop();
+ }
+ if (client != null) {
+ client.stop();
+ }
+ }
+ if (throwed[0] != null) {
+ throw throwed[0];
+ }
+ }
+
+ public static Test suite() {
+ return new TestSuite(SSLSocketFactoriesTest.class);
+ }
+
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+}
+
Propchange: harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketFactoriesTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketFunctionalTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketFunctionalTest.java?view=auto&rev=503371
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketFunctionalTest.java (added)
+++ harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketFunctionalTest.java Sun Feb 4 00:10:09 2007
@@ -0,0 +1,361 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.xnet.provider.jsse;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Arrays;
+import javax.net.ssl.HandshakeCompletedEvent;
+import javax.net.ssl.HandshakeCompletedListener;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLServerSocket;
+import javax.net.ssl.SSLSocket;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * SSLSocketImplTest
+ */
+public class SSLSocketFunctionalTest extends TestCase {
+
+ /**
+ * The cipher suites used for functionality testing.
+ */
+ private String[] cipher_suites = {
+ "RSA_WITH_RC4_128_MD5",
+ "RSA_WITH_DES_CBC_SHA",
+ "DH_anon_EXPORT_WITH_DES40_CBC_SHA"
+ };
+
+ // turn on/off the debug logging
+ private boolean doLog = false;
+
+ /**
+ * Sets up the test case.
+ */
+ public void setUp() throws Exception {
+ if (doLog) {
+ System.out.println("========================");
+ System.out.println("====== Running the test: " + getName());
+ System.out.println("========================");
+ }
+ }
+
+ public void testContextInitialized2() throws Throwable {
+ doTestSelfInteraction(JSSETestData.getContext());
+ }
+
+ public void doTestInteraction(SSLContext context, SSLContext ctx_other)
+ throws Throwable {
+ SSLContext ctx1, ctx2;
+
+ ctx1 = context;
+ ctx2 = ctx_other;
+
+ int k=1;
+
+ SSLServerSocket ssocket = (SSLServerSocket) ctx1
+ .getServerSocketFactory().createServerSocket(0);
+ ssocket.setUseClientMode(false);
+ ssocket.setEnabledCipherSuites(
+ ((k & 1) > 0)
+ ? new String[] {"TLS_"+cipher_suites[0]}
+ : new String[] {"SSL_"+cipher_suites[0]});
+
+ SSLSocket csocket = (SSLSocket) ctx2
+ .getSocketFactory().createSocket("localhost",
+ ssocket.getLocalPort());
+ csocket.setEnabledProtocols(new String[] {"TLSv1"});
+ csocket.setUseClientMode(true);
+ csocket.setEnabledCipherSuites(
+ (((k & 2) >> 1) > 0)
+ ? new String[] {"TLS_"+cipher_suites[0]}
+ : new String[] {"SSL_"+cipher_suites[0]});
+ doTest(ssocket, csocket);
+ }
+
+ public void _doTestInteraction(SSLContext context, SSLContext ctx_other)
+ throws Throwable {
+ for (int i=0; i<cipher_suites.length; i++) {
+ if (doLog) {
+ System.out.println("======== Checking the work on cipher: "
+ + cipher_suites[i]);
+ }
+ SSLContext ctx1, ctx2;
+ // k: 00, 01, 10, 11;
+ // where 1 means implementation under the test,
+ // 0 - another implementation to interract with
+ for (int k=0; k<4; k++) {
+ if (doLog) {
+ System.out.println("======== "+(k & 1)+" "+((k & 2) >> 1));
+ }
+ ctx1 = ((k & 1) > 0) ? context : ctx_other;
+ ctx2 = (((k & 2) >> 1) > 0) ? context : ctx_other;
+
+ SSLServerSocket ssocket = (SSLServerSocket) ctx1
+ .getServerSocketFactory().createServerSocket(0);
+ ssocket.setUseClientMode(false);
+ ssocket.setEnabledCipherSuites(
+ ((k & 1) > 0)
+ ? new String[] {"TLS_"+cipher_suites[i]}
+ : new String[] {"SSL_"+cipher_suites[i]});
+
+ SSLSocket csocket = (SSLSocket) ctx2
+ .getSocketFactory().createSocket("localhost",
+ ssocket.getLocalPort());
+ csocket.setEnabledProtocols(new String[] {"TLSv1"});
+ csocket.setUseClientMode(true);
+ csocket.setEnabledCipherSuites(
+ (((k & 2) >> 1) > 0)
+ ? new String[] {"TLS_"+cipher_suites[i]}
+ : new String[] {"SSL_"+cipher_suites[i]});
+ doTest(ssocket, csocket);
+ }
+ }
+ }
+
+ /**
+ * Tests the interaction with other implementation.
+ */
+ public void doTestSelfInteraction(SSLContext context)
+ throws Throwable {
+ String[] protocols = {"SSLv3", "TLSv1"};
+ for (int i=0; i<cipher_suites.length; i++) {
+ for (int j=0; j<2; j++) {
+ if (doLog) {
+ System.out.println("======= " + cipher_suites[i]);
+ }
+ SSLServerSocket ssocket = (SSLServerSocket) context
+ .getServerSocketFactory().createServerSocket(0);
+ ssocket.setUseClientMode(false);
+ ssocket.setEnabledProtocols(new String[] {protocols[j]});
+ ssocket.setEnabledCipherSuites(
+ new String[] {"TLS_"+cipher_suites[i]});
+
+ SSLSocket csocket = (SSLSocket) context
+ .getSocketFactory().createSocket("localhost",
+ ssocket.getLocalPort());
+ csocket.setEnabledProtocols(new String[] {protocols[j]});
+ csocket.setUseClientMode(true);
+ csocket.setEnabledCipherSuites(
+ new String[] {"TLS_"+cipher_suites[i]});
+
+ doTest(ssocket, csocket);
+ }
+ }
+ }
+
+ private static class HandshakeListener
+ implements HandshakeCompletedListener {
+ boolean compleated = false;
+
+ public void handshakeCompleted(HandshakeCompletedEvent event) {
+ compleated = true;
+ }
+ }
+
+ /**
+ * Performs SSL connection between the sockets
+ * @return
+ */
+ public void doTest(SSLServerSocket ssocket, SSLSocket csocket)
+ throws Throwable {
+ final String server_message = "Hello from SSL Server Socket!";
+ final String client_message = "Hello from SSL Socket!";
+ Thread server = null;
+ Thread client = null;
+ final Throwable[] throwed = new Throwable[1];
+ try {
+ final SSLServerSocket ss = ssocket;
+ final SSLSocket s = csocket;
+ server = new Thread() {
+ public void run() {
+ InputStream is = null;
+ OutputStream os = null;
+ SSLSocket s = null;
+ try {
+ s = (SSLSocket) ss.accept();
+ if (doLog) {
+ System.out.println("Socket accepted: " + s);
+ }
+ is = s.getInputStream();
+ os = s.getOutputStream();
+ // send the message to the client
+ os.write(server_message.getBytes());
+ // read the response
+ byte[] buff = new byte[client_message.length()];
+ int len = is.read(buff);
+ if (doLog) {
+ System.out.println("Received message of length "
+ + len + ": '" + new String(buff, 0, len)+"'");
+ }
+ assertTrue("Read message does not equal to expected",
+ Arrays.equals(client_message.getBytes(), buff));
+ os.write(-1);
+ assertTrue("Read data differs from expected",
+ is.read() == 255);
+ if (doLog) {
+ System.out.println("Server is closed: "
+ +s.isClosed());
+ }
+ assertTrue("Returned value should be -1",
+ // initiate an exchange of closure alerts
+ is.read() == -1);
+ if (doLog) {
+ System.out.println("Server is closed: "
+ +s.isClosed());
+ }
+ assertTrue("Returned value should be -1",
+ // initiate an exchange of closure alerts
+ is.read() == -1);
+ } catch (Throwable e) {
+ synchronized (throwed) {
+ if (doLog) {
+ e.printStackTrace();
+ }
+ if (throwed[0] == null) {
+ throwed[0] = e;
+ }
+ }
+ } finally {
+ try {
+ if (is != null) {
+ is.close();
+ }
+ } catch (IOException ex) {}
+ try {
+ if (os != null) {
+ os.close();
+ }
+ } catch (IOException ex) {}
+ try {
+ if (s != null) {
+ s.close();
+ }
+ } catch (IOException ex) {}
+ }
+ }
+ };
+
+ client = new Thread() {
+ public void run() {
+ InputStream is = null;
+ OutputStream os = null;
+ try {
+ assertTrue("Client was not connected", s.isConnected());
+ if (doLog) {
+ System.out.println("Client connected");
+ }
+ is = s.getInputStream();
+ os = s.getOutputStream();
+ s.startHandshake();
+ if (doLog) {
+ System.out.println("Client: HS was done");
+ }
+ // read the message from the server
+ byte[] buff = new byte[server_message.length()];
+ int len = is.read(buff);
+ if (doLog) {
+ System.out.println("Received message of length "
+ + len + ": '" + new String(buff, 0, len)+"'");
+ }
+ assertTrue("Read message does not equal to expected",
+ Arrays.equals(server_message.getBytes(), buff));
+ // send the response
+ buff = (" "+client_message+" ").getBytes();
+ os.write(buff, 1, buff.length-2);
+ assertTrue("Read data differs from expected",
+ is.read() == 255);
+ os.write(-1);
+ if (doLog) {
+ System.out.println("\n======== Closing ========");
+ }
+ if (doLog) {
+ System.out.println("Client is closed: "
+ +s.isClosed());
+ }
+ s.close();
+ if (doLog) {
+ System.out.println("Client is closed: "
+ +s.isClosed());
+ }
+ } catch (Throwable e) {
+ synchronized (throwed) {
+ if (doLog) {
+ e.printStackTrace();
+ }
+ if (throwed[0] == null) {
+ throwed[0] = e;
+ }
+ }
+ } finally {
+ try {
+ if (is != null) {
+ is.close();
+ }
+ } catch (IOException ex) {}
+ try {
+ if (os != null) {
+ os.close();
+ }
+ } catch (IOException ex) {}
+ try {
+ if (s != null) {
+ s.close();
+ }
+ } catch (IOException ex) {}
+ }
+ }
+ };
+
+ server.start();
+ client.start();
+
+ while (server.isAlive() || client.isAlive()) {
+ if (throwed[0] != null) {
+ throw throwed[0];
+ }
+ try {
+ Thread.sleep(500);
+ } catch (Exception e) { }
+ }
+ } finally {
+ if (server != null) {
+ server.stop();
+ }
+ if (client != null) {
+ client.stop();
+ }
+ }
+ if (throwed[0] != null) {
+ throw throwed[0];
+ }
+ }
+
+ public static Test suite() {
+ return new TestSuite(SSLSocketFunctionalTest.class);
+ }
+
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+}
+
Propchange: harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketFunctionalTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketImplTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketImplTest.java?view=auto&rev=503371
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketImplTest.java (added)
+++ harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketImplTest.java Sun Feb 4 00:10:09 2007
@@ -0,0 +1,1158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.xnet.provider.jsse;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import javax.net.ssl.HandshakeCompletedEvent;
+import javax.net.ssl.HandshakeCompletedListener;
+import javax.net.ssl.SSLSession;
+import javax.net.ssl.SSLSocket;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * SSLSocketImplTest test
+ */
+public class SSLSocketImplTest extends TestCase {
+
+ // turn on/off the debug logging
+ private static boolean doLog = false;
+
+ /**
+ * Sets up the test case.
+ */
+ public void setUp() throws Exception {
+ if (doLog) {
+ System.out.println("");
+ System.out.println("========================");
+ System.out.println("====== Running the test: " + getName());
+ }
+ }
+
+ private SSLSocket createSSLSocket() throws Exception {
+ return new SSLSocketImpl(JSSETestData.getSSLParameters());
+ }
+
+ private SSLSocket createSSLSocket(int port) throws Exception {
+ return new SSLSocketImpl("localhost", port,
+ JSSETestData.getSSLParameters());
+ }
+
+ /**
+ * SSLSocketImpl(SSLParameters sslParameters) method testing.
+ */
+ public void testSSLSocketImpl1() throws Exception {
+ Server server = null;
+ SSLSocket socket = null;
+ try {
+ server = new Server();
+
+ socket = new SSLSocketImpl(JSSETestData.getSSLParameters());
+ socket.connect(
+ new InetSocketAddress("localhost", server.getPort()));
+ ((SSLSocketImpl) socket).init();
+ socket.setUseClientMode(true);
+
+ server.start();
+ final SSLSocket s = socket;
+ Thread thread = new Thread() {
+ public void run() {
+ try {
+ s.startHandshake();
+ } catch (Exception e) { }
+ }
+ };
+
+ thread.start();
+
+ int timeout = 10; // wait no more than 10*500 ms for handshake
+ while (!server.handshakeStarted()) {
+ // wait for handshake start
+ try {
+ Thread.sleep(500);
+ } catch (Exception e) { }
+ timeout--;
+ if (timeout < 0) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ fail("Handshake was not started");
+ }
+ }
+ } finally {
+ if (server != null) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ }
+ }
+ }
+
+ /**
+ * SSLSocketImpl(String host, int port, SSLParameters sslParameters) method
+ * testing.
+ */
+ public void testSSLSocketImpl2() throws Exception {
+ Server server = null;
+ SSLSocket socket = null;
+ try {
+ server = new Server();
+
+ socket = new SSLSocketImpl("localhost",
+ server.getPort(), JSSETestData.getSSLParameters());
+ socket.setUseClientMode(true);
+
+ server.start();
+ final SSLSocket s = socket;
+ Thread thread = new Thread() {
+ public void run() {
+ try {
+ s.startHandshake();
+ } catch (Exception e) { }
+ }
+ };
+
+ thread.start();
+
+ int timeout = 10; // wait no more than 5 seconds for handshake
+ while (!server.handshakeStarted()) {
+ // wait for handshake start
+ try {
+ Thread.sleep(500);
+ } catch (Exception e) { }
+ timeout--;
+ if (timeout < 0) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ fail("Handshake was not started");
+ }
+ }
+ } finally {
+ if (server != null) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ }
+ }
+ }
+
+ /**
+ * SSLSocketImpl(String host, int port, InetAddress localHost, int
+ * localPort, SSLParameters sslParameters) method testing.
+ */
+ public void testSSLSocketImpl3() throws Exception {
+ Server server = null;
+ SSLSocket socket = null;
+ try {
+ server = new Server();
+
+ socket = new SSLSocketImpl(
+ "localhost",
+ server.getPort(),
+ InetAddress.getByName("localhost"),
+ 0, JSSETestData.getSSLParameters());
+ socket.setUseClientMode(true);
+
+ server.start();
+ final SSLSocket s = socket;
+ Thread thread = new Thread() {
+ public void run() {
+ try {
+ s.startHandshake();
+ } catch (Exception e) { }
+ }
+ };
+
+ thread.start();
+
+ int timeout = 10; // wait no more than 5 seconds for handshake
+ while (!server.handshakeStarted()) {
+ // wait for handshake start
+ try {
+ Thread.sleep(500);
+ } catch (Exception e) { }
+ timeout--;
+ if (timeout < 0) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ fail("Handshake was not started");
+ }
+ }
+ } finally {
+ if (server != null) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ }
+ }
+ }
+
+ /**
+ * SSLSocketImpl(InetAddress host, int port, SSLParameters sslParameters)
+ * method testing.
+ */
+ public void testSSLSocketImpl4() throws Exception {
+ Server server = null;
+ SSLSocket socket = null;
+ try {
+ server = new Server();
+
+ socket = new SSLSocketImpl(
+ InetAddress.getByName("localhost"),
+ server.getPort(),
+ JSSETestData.getSSLParameters());
+ socket.setUseClientMode(true);
+
+ server.start();
+ final SSLSocket s = socket;
+ Thread thread = new Thread() {
+ public void run() {
+ try {
+ s.startHandshake();
+ } catch (Exception e) { }
+ }
+ };
+
+ thread.start();
+
+ int timeout = 10; // wait no more than 5 seconds for handshake
+ while (!server.handshakeStarted()) {
+ // wait for handshake start
+ try {
+ Thread.sleep(500);
+ } catch (Exception e) { }
+ timeout--;
+ if (timeout < 0) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ fail("Handshake was not started");
+ }
+ }
+ } finally {
+ if (server != null) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ }
+ }
+ }
+
+ /**
+ * SSLSocketImpl(InetAddress address, int port, InetAddress localAddress,
+ * int localPort, SSLParameters sslParameters) method testing.
+ */
+ public void testSSLSocketImpl5() throws Exception {
+ Server server = null;
+ SSLSocket socket = null;
+ try {
+ server = new Server();
+
+ socket = new SSLSocketImpl(
+ InetAddress.getByName("localhost"),
+ server.getPort(),
+ InetAddress.getByName("localhost"),
+ 0, JSSETestData.getSSLParameters());
+ socket.setUseClientMode(true);
+
+ server.start();
+ final SSLSocket s = socket;
+ Thread thread = new Thread() {
+ public void run() {
+ try {
+ s.startHandshake();
+ } catch (Exception e) { }
+ }
+ };
+
+ thread.start();
+
+ int timeout = 10; // wait no more than 5 seconds for handshake
+ while (!server.handshakeStarted()) {
+ // wait for handshake start
+ try {
+ Thread.sleep(500);
+ } catch (Exception e) { }
+ timeout--;
+ if (timeout < 0) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ fail("Handshake was not started");
+ }
+ }
+ } finally {
+ if (server != null) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ }
+ }
+ }
+
+ /**
+ * getSupportedCipherSuites() method testing.
+ */
+ public void testGetSupportedCipherSuites() throws Exception {
+ SSLSocket socket = createSSLSocket();
+ String[] supported = socket.getSupportedCipherSuites();
+ assertNotNull(supported);
+ supported[0] = "NOT_SUPPORTED_CIPHER_SUITE";
+ supported = socket.getEnabledCipherSuites();
+ for (int i=0; i<supported.length; i++) {
+ if ("NOT_SUPPORTED_CIPHER_SUITE".equals(supported[i])) {
+ fail("Modification of the returned result "
+ + "causes the modification of the internal state");
+ }
+ }
+ }
+
+ /**
+ * getEnabledCipherSuites() method testing.
+ */
+ public void testGetEnabledCipherSuites() throws Exception {
+ SSLSocket socket = createSSLSocket();
+ String[] enabled = socket.getEnabledCipherSuites();
+ assertNotNull(enabled);
+ String[] supported = socket.getSupportedCipherSuites();
+ for (int i=0; i<enabled.length; i++) {
+ found: {
+ for (int j=0; j<supported.length; j++) {
+ if (enabled[i].equals(supported[j])) {
+ break found;
+ }
+ }
+ fail("Enabled suite does not belong to the set "
+ + "of supported cipher suites: " + enabled[i]);
+ }
+ }
+ socket.setEnabledCipherSuites(supported);
+ for (int i=0; i<supported.length; i++) {
+ enabled = new String[supported.length - i];
+ System.arraycopy(supported, 0,
+ enabled, 0, supported.length-i);
+ socket.setEnabledCipherSuites(enabled);
+ String[] result = socket.getEnabledCipherSuites();
+ if (result.length != enabled.length) {
+ fail("Returned result does not correspond to expected.");
+ }
+ for (int k=0; k<result.length; k++) {
+ found: {
+ for (int n=0; n<enabled.length; n++) {
+ if (result[k].equals(enabled[n])) {
+ break found;
+ }
+ }
+ if (result.length != enabled.length) {
+ fail("Returned result does not correspond "
+ + "to expected.");
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * setEnabledCipherSuites(String[] suites) method testing.
+ */
+ public void testSetEnabledCipherSuites() throws Exception {
+ SSLSocket socket = createSSLSocket();
+ String[] enabled = socket.getEnabledCipherSuites();
+ assertNotNull(enabled);
+ String[] supported = socket.getSupportedCipherSuites();
+ for (int i=0; i<enabled.length; i++) {
+ found: {
+ for (int j=0; j<supported.length; j++) {
+ if (enabled[i].equals(supported[j])) {
+ break found;
+ }
+ }
+ fail("Enabled suite does not belong to the set "
+ + "of supported cipher suites: " + enabled[i]);
+ }
+ }
+ socket.setEnabledCipherSuites(supported);
+ socket.setEnabledCipherSuites(enabled);
+ socket.setEnabledCipherSuites(supported);
+ String[] more_than_supported = new String[supported.length+1];
+ for (int i=0; i<supported.length+1; i++) {
+ more_than_supported[i]
+ = "NOT_SUPPORTED_CIPHER_SUITE";
+ System.arraycopy(supported, 0,
+ more_than_supported, 0, i);
+ System.arraycopy(supported, i,
+ more_than_supported, i+1, supported.length-i);
+ try {
+ socket.setEnabledCipherSuites(more_than_supported);
+ fail("Expected IllegalArgumentException was not thrown");
+ } catch (IllegalArgumentException e) { }
+ }
+ enabled = socket.getEnabledCipherSuites();
+ enabled[0] = "NOT_SUPPORTED_CIPHER_SUITE";
+ enabled = socket.getEnabledCipherSuites();
+ for (int i=0; i<enabled.length; i++) {
+ if ("NOT_SUPPORTED_CIPHER_SUITE".equals(enabled[i])) {
+ fail("Modification of the returned result "
+ + "causes the modification of the internal state");
+ }
+ }
+ }
+
+ /**
+ * getSupportedProtocols() method testing.
+ */
+ public void testGetSupportedProtocols() throws Exception {
+ SSLSocket socket = createSSLSocket();
+ String[] supported = socket.getSupportedProtocols();
+ assertNotNull(supported);
+ assertFalse(supported.length == 0);
+ supported[0] = "NOT_SUPPORTED_PROTOCOL";
+ supported = socket.getSupportedProtocols();
+ for (int i=0; i<supported.length; i++) {
+ if ("NOT_SUPPORTED_PROTOCOL".equals(supported[i])) {
+ fail("Modification of the returned result "
+ + "causes the modification of the internal state");
+ }
+ }
+ }
+
+ /**
+ * getEnabledProtocols() method testing.
+ */
+ public void testGetEnabledProtocols() throws Exception {
+ SSLSocket socket = createSSLSocket();
+ String[] enabled = socket.getEnabledProtocols();
+ assertNotNull(enabled);
+ String[] supported = socket.getSupportedProtocols();
+ for (int i=0; i<enabled.length; i++) {
+ found: {
+ for (int j=0; j<supported.length; j++) {
+ if (enabled[i].equals(supported[j])) {
+ break found;
+ }
+ }
+ fail("Enabled protocol does not belong to the set "
+ + "of supported protocols: " + enabled[i]);
+ }
+ }
+ socket.setEnabledProtocols(supported);
+ for (int i=0; i<supported.length; i++) {
+ enabled = new String[supported.length - i];
+ System.arraycopy(supported, i,
+ enabled, 0, supported.length-i);
+ socket.setEnabledProtocols(enabled);
+ String[] result = socket.getEnabledProtocols();
+ if (result.length != enabled.length) {
+ fail("Returned result does not correspond to expected.");
+ }
+ for (int k=0; k<result.length; k++) {
+ found: {
+ for (int n=0; n<enabled.length; n++) {
+ if (result[k].equals(enabled[n])) {
+ break found;
+ }
+ }
+ if (result.length != enabled.length) {
+ fail("Returned result does not correspond "
+ + "to expected.");
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * setEnabledProtocols(String[] protocols) method testing.
+ */
+ public void testSetEnabledProtocols() throws Exception {
+ SSLSocket socket = createSSLSocket();
+ String[] enabled = socket.getEnabledProtocols();
+ assertNotNull(enabled);
+ String[] supported = socket.getSupportedProtocols();
+ for (int i=0; i<enabled.length; i++) {
+ //System.out.println("Checking of "+enabled[i]);
+ found: {
+ for (int j=0; j<supported.length; j++) {
+ if (enabled[i].equals(supported[j])) {
+ break found;
+ }
+ }
+ fail("Enabled suite does not belong to the set "
+ + "of supported cipher suites: " + enabled[i]);
+ }
+ }
+ socket.setEnabledProtocols(supported);
+ socket.setEnabledProtocols(enabled);
+ socket.setEnabledProtocols(supported);
+ String[] more_than_supported = new String[supported.length+1];
+ for (int i=0; i<supported.length+1; i++) {
+ more_than_supported[i]
+ = "NOT_SUPPORTED_PROTOCOL";
+ System.arraycopy(supported, 0,
+ more_than_supported, 0, i);
+ System.arraycopy(supported, i,
+ more_than_supported, i+1, supported.length-i);
+ try {
+ socket.setEnabledProtocols(more_than_supported);
+ fail("Expected IllegalArgumentException was not thrown");
+ } catch (IllegalArgumentException e) { }
+ }
+ enabled = socket.getEnabledProtocols();
+ enabled[0] = "NOT_SUPPORTED_PROTOCOL";
+ enabled = socket.getEnabledProtocols();
+ for (int i=0; i<enabled.length; i++) {
+ if ("NOT_SUPPORTED_PROTOCOL".equals(enabled[i])) {
+ fail("Modification of the returned result "
+ + "causes the modification of the internal state");
+ }
+ }
+ }
+
+ private static class Server extends Thread {
+
+ private final ServerSocket server;
+ private boolean closed;
+ private boolean handshake_started = false;
+ private Socket endpoint = null;
+
+ public Server() throws IOException {
+ super();
+ server = new ServerSocket(0);
+ server.setSoTimeout(1000);
+ }
+
+ public int getPort() {
+ return server.getLocalPort();
+ }
+
+ public void run() {
+ while (!closed) {
+ try {
+ if (doLog) {
+ System.out.print(".");
+ }
+ if (!handshake_started) {
+ endpoint = server.accept();
+ endpoint.getInputStream().read();
+ handshake_started = true;
+ }
+ Thread.sleep(1000);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ if (endpoint != null) {
+ try {
+ endpoint.close();
+ } catch (IOException e) { }
+ }
+ }
+
+ public boolean handshakeStarted() {
+ return handshake_started;
+ }
+
+ public void close() throws IOException {
+ closed = true;
+ server.close();
+ }
+
+ };
+
+ /**
+ * setUseClientMode(boolean mode) method testing.
+ * getUseClientMode() method testing.
+ */
+ public void testSetGetUseClientMode() throws Exception {
+ Server server = null;
+ SSLSocket socket = null;
+ try {
+ server = new Server();
+
+ socket = createSSLSocket(server.getPort());
+
+ socket.setUseClientMode(false);
+ assertFalse("Result does not correspond to expected",
+ socket.getUseClientMode());
+ socket.setUseClientMode(true);
+ assertTrue("Result does not correspond to expected",
+ socket.getUseClientMode());
+
+ server.start();
+ final SSLSocket s = socket;
+ new Thread() {
+ public void run() {
+ try {
+ s.startHandshake();
+ } catch (IOException e) {
+ //e.printStackTrace();
+ }
+ }
+ }.start();
+
+ while (!server.handshakeStarted()) {
+ // wait for handshake start
+ try {
+ Thread.sleep(500);
+ } catch (Exception e) { }
+ }
+
+ try {
+ socket.setUseClientMode(false);
+ server.close();
+ socket.close();
+ fail("Expected IllegalArgumentException was not thrown");
+ } catch (IllegalArgumentException e) { }
+ } finally {
+ if (server != null) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ }
+ }
+ }
+
+ /**
+ * setNeedClientAuth(boolean need) method testing.
+ * getNeedClientAuth() method testing.
+ */
+ public void testSetGetNeedClientAuth() throws Exception {
+ SSLSocket socket = createSSLSocket();
+
+ socket.setWantClientAuth(true);
+ socket.setNeedClientAuth(false);
+ assertFalse("Result does not correspond to expected",
+ socket.getNeedClientAuth());
+ assertFalse("Socket did not reset its want client auth state",
+ socket.getWantClientAuth());
+ socket.setWantClientAuth(true);
+ socket.setNeedClientAuth(true);
+ assertTrue("Result does not correspond to expected",
+ socket.getNeedClientAuth());
+ assertFalse("Socket did not reset its want client auth state",
+ socket.getWantClientAuth());
+ }
+
+ /**
+ * setWantClientAuth(boolean want) method testing.
+ * getWantClientAuth() method testing.
+ */
+ public void testSetGetWantClientAuth() throws Exception {
+ SSLSocket socket = createSSLSocket();
+
+ socket.setNeedClientAuth(true);
+ socket.setWantClientAuth(false);
+ assertFalse("Result does not correspond to expected",
+ socket.getWantClientAuth());
+ assertFalse("Socket did not reset its want client auth state",
+ socket.getNeedClientAuth());
+ socket.setNeedClientAuth(true);
+ socket.setWantClientAuth(true);
+ assertTrue("Result does not correspond to expected",
+ socket.getWantClientAuth());
+ assertFalse("Socket did not reset its want client auth state",
+ socket.getNeedClientAuth());
+ }
+
+ /**
+ * setEnableSessionCreation(boolean flag) method testing.
+ * getEnableSessionCreation() method testing.
+ */
+ public void testSetGetEnableSessionCreation() throws Exception {
+ SSLSocket socket = createSSLSocket();
+
+ socket.setEnableSessionCreation(false);
+ assertFalse("Result does not correspond to expected",
+ socket.getEnableSessionCreation());
+ socket.setEnableSessionCreation(true);
+ assertTrue("Result does not correspond to expected",
+ socket.getEnableSessionCreation());
+ }
+
+ /**
+ * getSession() method testing.
+ */
+ public void testGetSession() throws Exception {
+ Server server = null;
+ SSLSocket socket = null;
+ try {
+ server = new Server();
+
+ socket = createSSLSocket(server.getPort());
+ socket.setUseClientMode(true);
+
+ server.start();
+ final SSLSocket s = socket;
+ final SSLSession[] session = new SSLSession[1];
+ Thread thread = new Thread() {
+ public void run() {
+ try {
+ session[0] = s.getSession();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ };
+
+ thread.start();
+
+ int timeout = 10; // wait no more than 5 seconds for handshake
+ while (!server.handshakeStarted()) {
+ // wait for handshake start
+ try {
+ Thread.sleep(500);
+ } catch (Exception e) { }
+ timeout--;
+ if (timeout < 0) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ fail("getSession method did not start a handshake");
+ }
+ }
+
+ server.close(); // makes error during the handshake
+ try {
+ thread.join();
+ if ((session[0] == null) ||
+ (!session[0].getCipherSuite()
+ .endsWith("_NULL_WITH_NULL_NULL"))) {
+ fail("Returned session is null "
+ + "or not TLS_NULL_WITH_NULL_NULL");
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail("Unexpected exception: "+e.getMessage());
+ }
+ } finally {
+ if (server != null) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ }
+ }
+ }
+
+ /**
+ * addHandshakeCompletedListener( HandshakeCompletedListener listener)
+ * method testing.
+ * removeHandshakeCompletedListener( HandshakeCompletedListener listener)
+ * method testing.
+ */
+ public void testAddRemoveHandshakeCompletedListener() throws Exception {
+ HandshakeCompletedListener listener =
+ new HandshakeCompletedListener() {
+ public void handshakeCompleted(
+ HandshakeCompletedEvent event) { }
+ };
+ SSLSocket socket = createSSLSocket();
+ socket.addHandshakeCompletedListener(listener);
+ try {
+ socket.addHandshakeCompletedListener(null);
+ fail("Expected IllegalArgumentException was not thrown.");
+ } catch (IllegalArgumentException e) { }
+ try {
+ socket.removeHandshakeCompletedListener(null);
+ fail("Expected IllegalArgumentException was not thrown.");
+ } catch (IllegalArgumentException e) { }
+ try {
+ socket.removeHandshakeCompletedListener(
+ new HandshakeCompletedListener() {
+ public void handshakeCompleted(
+ HandshakeCompletedEvent event) { }
+ });
+ fail("Expected IllegalArgumentException was not thrown.");
+ } catch (IllegalArgumentException e) { }
+ try {
+ socket.removeHandshakeCompletedListener(listener);
+ } catch (IllegalArgumentException e) {
+ fail("Unxpected IllegalArgumentException was thrown.");
+ }
+ }
+
+ /**
+ * startHandshake() method testing.
+ */
+ public void testStartHandshake() throws Exception {
+ Server server = null;
+ SSLSocket socket = null;
+ try {
+ server = new Server();
+
+ socket = createSSLSocket(server.getPort());
+ socket.setUseClientMode(true);
+
+ server.start();
+ final SSLSocket s = socket;
+ final Exception[] exception = new Exception[1];
+ Thread thread = new Thread() {
+ public void run() {
+ try {
+ s.startHandshake();
+ } catch (Exception e) {
+ exception[0] = e;
+ }
+ }
+ };
+
+ thread.start();
+
+ int timeout = 10; // wait no more than 5 seconds for handshake
+ while (!server.handshakeStarted()) {
+ // wait for handshake start
+ try {
+ Thread.sleep(500);
+ } catch (Exception e) { }
+ timeout--;
+ if (timeout < 0) {
+ fail("Handshake was not started");
+ }
+ }
+
+ server.close(); // makes error during the handshake
+ try {
+ thread.join();
+ if (exception[0] == null) {
+ fail("Expected IOException was not thrown");
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail("Unexpected exception: "+e.getMessage());
+ }
+ } finally {
+ if (server != null) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ }
+ }
+ }
+
+ /**
+ * getInputStream() method testing.
+ */
+ public void testGetInputStream() throws Exception {
+ Server server = null;
+ SSLSocket socket = null;
+ try {
+ server = new Server();
+
+ socket = createSSLSocket(server.getPort());
+ socket.setUseClientMode(true);
+
+ server.start();
+ final SSLSocket s = socket;
+ Thread thread = new Thread() {
+ public void run() {
+ try {
+ s.getInputStream().read(); // should start handshake
+ } catch (Exception e) { }
+ }
+ };
+
+ thread.start();
+
+ int timeout = 10; // wait no more than 5 seconds for handshake
+ while (!server.handshakeStarted()) {
+ // wait for handshake start
+ try {
+ Thread.sleep(500);
+ } catch (Exception e) { }
+ timeout--;
+ if (timeout < 0) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ fail("Handshake was not started");
+ }
+ }
+ } finally {
+ if (server != null) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ }
+ }
+ }
+
+ /**
+ * getOutputStream() method testing.
+ */
+ public void testGetOutputStream() throws Exception {
+ Server server = null;
+ SSLSocket socket = null;
+ try {
+ server = new Server();
+
+ socket = createSSLSocket(server.getPort());
+ socket.setUseClientMode(true);
+
+ server.start();
+ final SSLSocket s = socket;
+ Thread thread = new Thread() {
+ public void run() {
+ try {
+ s.getOutputStream().write(0); // should start handshake
+ } catch (Exception e) { }
+ }
+ };
+
+ thread.start();
+
+ int timeout = 10; // wait no more than 5 seconds for handshake
+ while (!server.handshakeStarted()) {
+ // wait for handshake start
+ try {
+ Thread.sleep(500);
+ } catch (Exception e) { }
+ timeout--;
+ if (timeout < 0) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ fail("Handshake was not started");
+ }
+ }
+ } finally {
+ if (server != null) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ }
+ }
+ }
+
+ /**
+ * sendUrgentData(int data) method testing.
+ */
+ public void testSendUrgentData() {
+ Server server = null;
+ SSLSocket socket = null;
+ try {
+ server = new Server();
+ socket = createSSLSocket(server.getPort());
+
+ socket.sendUrgentData(0);
+ fail("Expected exception was not thrown");
+ } catch (Exception e) {
+ if (doLog) {
+ System.out.println("Trowed exception: "+e.getMessage());
+ }
+ } finally {
+ if (server != null) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ }
+ }
+ }
+
+ /**
+ * setOOBInline(boolean on) method testing.
+ */
+ public void testSetOOBInline() {
+ Server server = null;
+ SSLSocket socket = null;
+ try {
+ server = new Server();
+ socket = createSSLSocket(server.getPort());
+
+ socket.setOOBInline(true);
+ fail("Expected exception was not thrown");
+ } catch (Exception e) {
+ if (doLog) {
+ System.out.println("Trowed exception: "+e.getMessage());
+ }
+ } finally {
+ if (server != null) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ }
+ }
+ }
+
+ /**
+ * shutdownOutput() method testing.
+ */
+ public void testShutdownOutput() {
+ Server server = null;
+ SSLSocket socket = null;
+ try {
+ server = new Server();
+ socket = createSSLSocket(server.getPort());
+
+ socket.shutdownOutput();
+ fail("Expected exception was not thrown");
+ } catch (Exception e) {
+ if (doLog) {
+ System.out.println("Trowed exception: "+e.getMessage());
+ }
+ } finally {
+ if (server != null) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ }
+ }
+ }
+
+ /**
+ * shutdownInput() method testing.
+ */
+ public void testShutdownInput() {
+ Server server = null;
+ SSLSocket socket = null;
+ try {
+ server = new Server();
+ socket = createSSLSocket(server.getPort());
+
+ socket.shutdownInput();
+ fail("Expected exception was not thrown");
+ } catch (Exception e) {
+ if (doLog) {
+ System.out.println("Trowed exception: "+e.getMessage());
+ }
+ } finally {
+ if (server != null) {
+ try {
+ server.close();
+ } catch (IOException ex) { }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException ex) { }
+ }
+ }
+ }
+
+ /**
+ * toString() method testing.
+ */
+ public void testToString() throws Exception {
+ SSLSocket socket = createSSLSocket();
+ assertNotNull("String representation is null", socket.toString());
+ }
+
+ public static Test suite() {
+ return new TestSuite(SSLSocketImplTest.class);
+ }
+
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+}
+
Propchange: harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketImplTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLStreamedInputTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLStreamedInputTest.java?view=auto&rev=503371
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLStreamedInputTest.java (added)
+++ harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLStreamedInputTest.java Sun Feb 4 00:10:09 2007
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.xnet.provider.jsse;
+
+import java.io.ByteArrayInputStream;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * SSLStreamedInputTest test
+ */
+public class SSLStreamedInputTest extends TestCase {
+
+ /**
+ * SSLStreamedInput(InputStream in) method testing.
+ */
+ public void testSSLStreamedInput() throws Exception {
+ byte[] data = {1, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 5};
+ ByteArrayInputStream bis = new ByteArrayInputStream(data);
+ SSLStreamedInput sslsi = new SSLStreamedInput(bis);
+ assertTrue(sslsi.available() == bis.available());
+ assertTrue(sslsi.read() == 1);
+ assertTrue(sslsi.readUint32() == 1);
+ sslsi.skip(3);
+ assertTrue(sslsi.readUint64() == 5);
+ try {
+ sslsi.read();
+ fail("Expected EndOfSourceException was not thrown");
+ } catch (EndOfSourceException e) {
+ }
+ }
+
+ public static Test suite() {
+ return new TestSuite(SSLStreamedInputTest.class);
+ }
+
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+}
+
Propchange: harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLStreamedInputTest.java
------------------------------------------------------------------------------
svn:eol-style = native