Return-Path: Delivered-To: apmail-harmony-commits-archive@www.apache.org Received: (qmail 51780 invoked from network); 4 Feb 2007 08:10:43 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 4 Feb 2007 08:10:43 -0000 Received: (qmail 83244 invoked by uid 500); 4 Feb 2007 08:10:48 -0000 Delivered-To: apmail-harmony-commits-archive@harmony.apache.org Received: (qmail 83044 invoked by uid 500); 4 Feb 2007 08:10:48 -0000 Mailing-List: contact commits-help@harmony.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@harmony.apache.org Delivered-To: mailing list commits@harmony.apache.org Received: (qmail 82921 invoked by uid 99); 4 Feb 2007 08:10:47 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 04 Feb 2007 00:10:47 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 04 Feb 2007 00:10:35 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id 99F721A981A; Sun, 4 Feb 2007 00:10:13 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r503371 [1/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... Date: Sun, 04 Feb 2007 08:10:11 -0000 To: commits@harmony.apache.org From: tellison@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070204081013.99F721A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tellison Date: Sun Feb 4 00:10:09 2007 New Revision: 503371 URL: http://svn.apache.org/viewvc?view=rev&rev=503371 Log: Apply contribution HARMONY-2371 ([classlib][x-net]JSSE provider tests contribution) Added: harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/KeyManagerFactory1Test.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/KeyManagerFactory2Test.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/SSLContext1Test.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/SSLContext2Test.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/TrustManagerFactory1Test.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/TrustManagerFactory2Test.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/CipherSuiteTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/DelegatedTaskTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/DigitalSignatureTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/HandshakeProtocolTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/KeyManagerImplTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLEngineImplTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLServerSocketImplTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSessionContextImplTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSessionImplTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketFactoriesTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketFunctionalTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketImplTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLStreamedInputTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/ServerHandshakeImplTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/TrustManagerImplTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java/org/apache/harmony/xnet/tests/provider/jsse/KeyManagerFactoryImplTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java/org/apache/harmony/xnet/tests/provider/jsse/ProtocolVersionTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/impl/java/org/apache/harmony/xnet/tests/provider/jsse/TrustManagerFactoryImplTest.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/resources/key_store.bks (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/support/ harmony/enhanced/classlib/trunk/modules/x-net/src/test/support/common/ harmony/enhanced/classlib/trunk/modules/x-net/src/test/support/common/java/ harmony/enhanced/classlib/trunk/modules/x-net/src/test/support/common/java/org/ harmony/enhanced/classlib/trunk/modules/x-net/src/test/support/common/java/org/apache/ harmony/enhanced/classlib/trunk/modules/x-net/src/test/support/common/java/org/apache/harmony/ harmony/enhanced/classlib/trunk/modules/x-net/src/test/support/common/java/org/apache/harmony/xnet/ harmony/enhanced/classlib/trunk/modules/x-net/src/test/support/common/java/org/apache/harmony/xnet/provider/ harmony/enhanced/classlib/trunk/modules/x-net/src/test/support/common/java/org/apache/harmony/xnet/provider/jsse/ harmony/enhanced/classlib/trunk/modules/x-net/src/test/support/common/java/org/apache/harmony/xnet/provider/jsse/JSSETestData.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/support/common/java/org/apache/harmony/xnet/tests/ harmony/enhanced/classlib/trunk/modules/x-net/src/test/support/common/java/org/apache/harmony/xnet/tests/support/ harmony/enhanced/classlib/trunk/modules/x-net/src/test/support/common/java/org/apache/harmony/xnet/tests/support/MyKeyManagerFactorySpi.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/support/common/java/org/apache/harmony/xnet/tests/support/MySSLContextSpi.java (with props) harmony/enhanced/classlib/trunk/modules/x-net/src/test/support/common/java/org/apache/harmony/xnet/tests/support/MyTrustManagerFactorySpi.java (with props) Modified: harmony/enhanced/classlib/trunk/modules/x-net/.classpath harmony/enhanced/classlib/trunk/modules/x-net/META-INF/MANIFEST.MF harmony/enhanced/classlib/trunk/modules/x-net/build.xml harmony/enhanced/classlib/trunk/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLContextImpl.java harmony/enhanced/classlib/trunk/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLParameters.java Modified: harmony/enhanced/classlib/trunk/modules/x-net/.classpath URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/x-net/.classpath?view=diff&rev=503371&r1=503370&r2=503371 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/x-net/.classpath (original) +++ harmony/enhanced/classlib/trunk/modules/x-net/.classpath Sun Feb 4 00:10:09 2007 @@ -1,11 +1,13 @@ - - - - - + + + + + + + - + Modified: harmony/enhanced/classlib/trunk/modules/x-net/META-INF/MANIFEST.MF URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/x-net/META-INF/MANIFEST.MF?view=diff&rev=503371&r1=503370&r2=503371 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/x-net/META-INF/MANIFEST.MF (original) +++ harmony/enhanced/classlib/trunk/modules/x-net/META-INF/MANIFEST.MF Sun Feb 4 00:10:09 2007 @@ -30,7 +30,8 @@ org.apache.harmony.luni.util, org.apache.harmony.security.fortress, org.apache.harmony.security.tests.support;hy_usage=test;resolution:=optional, - org.apache.harmony.testframework.serialization;hy_usage=test;resolution:=optional + org.apache.harmony.testframework.serialization;hy_usage=test;resolution:=optional, + tests.support.resource;hy_usage=test;resolution:=optional Export-Package: javax.net, javax.net.ssl, org.apache.harmony.xnet.provider.jsse;hy_usage="suncompat";mandatory:="hy_usage" Modified: harmony/enhanced/classlib/trunk/modules/x-net/build.xml URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/x-net/build.xml?view=diff&rev=503371&r1=503370&r2=503371 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/x-net/build.xml (original) +++ harmony/enhanced/classlib/trunk/modules/x-net/build.xml Sun Feb 4 00:10:09 2007 @@ -125,7 +125,8 @@ - + + @@ -208,7 +209,7 @@ - + Modified: harmony/enhanced/classlib/trunk/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLContextImpl.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLContextImpl.java?view=diff&rev=503371&r1=503370&r2=503371 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLContextImpl.java (original) +++ harmony/enhanced/classlib/trunk/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLContextImpl.java Sun Feb 4 00:10:09 2007 @@ -43,6 +43,15 @@ */ public class SSLContextImpl extends SSLContextSpi { + // client session context contains the set of reusable + // client-side SSL sessions + private SSLSessionContextImpl clientSessionContext = + new SSLSessionContextImpl(); + // server session context contains the set of reusable + // server-side SSL sessions + private SSLSessionContextImpl serverSessionContext = + new SSLSessionContextImpl(); + protected SSLParameters sslParameters; public SSLContextImpl() { @@ -51,7 +60,8 @@ public void engineInit(KeyManager[] kms, TrustManager[] tms, SecureRandom sr) throws KeyManagementException { - sslParameters = new SSLParameters(kms, tms, sr); + sslParameters = new SSLParameters(kms, tms, sr, clientSessionContext, + serverSessionContext); } public SSLSocketFactory engineGetSocketFactory() { @@ -84,11 +94,11 @@ } public SSLSessionContext engineGetServerSessionContext() { - return sslParameters.getServerSessionContext(); + return serverSessionContext; } public SSLSessionContext engineGetClientSessionContext() { - return sslParameters.getClientSessionContext(); + return clientSessionContext; } } Modified: harmony/enhanced/classlib/trunk/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLParameters.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLParameters.java?view=diff&rev=503371&r1=503370&r2=503371 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLParameters.java (original) +++ harmony/enhanced/classlib/trunk/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLParameters.java Sun Feb 4 00:10:09 2007 @@ -58,12 +58,10 @@ // client session context contains the set of reusable // client-side SSL sessions - private SSLSessionContextImpl clientSessionContext = - new SSLSessionContextImpl(); + private SSLSessionContextImpl clientSessionContext; // server session context contains the set of reusable // server-side SSL sessions - private SSLSessionContextImpl serverSessionContext = - new SSLSessionContextImpl(); + private SSLSessionContextImpl serverSessionContext; // source of authentication keys private X509KeyManager keyManager; // source of authentication trust decisions @@ -104,8 +102,12 @@ * for more information */ protected SSLParameters(KeyManager[] kms, TrustManager[] tms, - SecureRandom sr) throws KeyManagementException { + SecureRandom sr, SSLSessionContextImpl clientSessionContext, + SSLSessionContextImpl serverSessionContext) + throws KeyManagementException { this(); + this.serverSessionContext = serverSessionContext; + this.clientSessionContext = clientSessionContext; try { // initialize key manager boolean initialize_default = false; @@ -188,7 +190,8 @@ protected static SSLParameters getDefault() throws KeyManagementException { if (defaultParameters == null) { - defaultParameters = new SSLParameters(null, null, null); + defaultParameters = new SSLParameters(null, null, null, + new SSLSessionContextImpl(), new SSLSessionContextImpl()); } return (SSLParameters) defaultParameters.clone(); } Added: harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/KeyManagerFactory1Test.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/KeyManagerFactory1Test.java?view=auto&rev=503371 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/KeyManagerFactory1Test.java (added) +++ harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/KeyManagerFactory1Test.java Sun Feb 4 00:10:09 2007 @@ -0,0 +1,447 @@ +/* + * 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.tests.javax.net.ssl; + +import java.security.InvalidAlgorithmParameterException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.Provider; +import java.security.Security; +import java.security.UnrecoverableKeyException; + +import javax.net.ssl.ManagerFactoryParameters; +import javax.net.ssl.KeyManager; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.KeyManagerFactorySpi; + +import org.apache.harmony.security.tests.support.SpiEngUtils; +import org.apache.harmony.xnet.tests.support.MyKeyManagerFactorySpi; +import junit.framework.TestCase; + +/** + * Tests for KeyManagerFactory class constructors and methods. + * + */ + +public class KeyManagerFactory1Test extends TestCase { + + private static final String srvKeyManagerFactory = "KeyManagerFactory"; + + private static String defaultAlgorithm = null; + + private static String defaultProviderName = null; + + private static Provider defaultProvider = null; + + private static boolean DEFSupported = false; + + private static final String NotSupportedMsg = "There is no suitable provider for KeyManagerFactory"; + + private static final String[] invalidValues = SpiEngUtils.invalidValues; + + private static String[] validValues = new String[3]; + static { + defaultAlgorithm = Security + .getProperty("ssl.KeyManagerFactory.algorithm"); + if (defaultAlgorithm != null) { + defaultProvider = SpiEngUtils.isSupport(defaultAlgorithm, + srvKeyManagerFactory); + DEFSupported = (defaultProvider != null); + defaultProviderName = (DEFSupported ? defaultProvider.getName() + : null); + validValues[0] = defaultAlgorithm; + validValues[1] = defaultAlgorithm.toUpperCase(); + validValues[2] = defaultAlgorithm.toLowerCase(); + } + } + + protected KeyManagerFactory[] createKMFac() { + if (!DEFSupported) { + fail(defaultAlgorithm + " algorithm is not supported"); + return null; + } + KeyManagerFactory[] kMF = new KeyManagerFactory[3]; + try { + kMF[0] = KeyManagerFactory.getInstance(defaultAlgorithm); + kMF[1] = KeyManagerFactory.getInstance(defaultAlgorithm, + defaultProvider); + kMF[2] = KeyManagerFactory.getInstance(defaultAlgorithm, + defaultProviderName); + return kMF; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * Test for getDefaultAlgorithm() method + * Assertion: returns value which is specifoed in security property + */ + public void testGetDefaultAlgorithm() { + if (!DEFSupported) { + fail(NotSupportedMsg); + return; + } + String def = KeyManagerFactory.getDefaultAlgorithm(); + if (defaultAlgorithm == null) { + assertNull("DefaultAlgorithm must be null", def); + } else { + assertEquals("Invalid default algorithm", def, defaultAlgorithm); + } + String defA = "Proba.keymanagerfactory.defaul.type"; + Security.setProperty("ssl.KeyManagerFactory.algorithm", defA); + assertEquals("Incorrect defaultAlgorithm", + KeyManagerFactory.getDefaultAlgorithm(), defA); + if (def == null) { + def = ""; + } + Security.setProperty("ssl.KeyManagerFactory.algorithm", def); + assertEquals("Incorrect defaultAlgorithm", + KeyManagerFactory.getDefaultAlgorithm(), def); + } + + /** + * Test for getInstance(String algorithm) method + * Assertions: + * returns security property "ssl.KeyManagerFactory.algorithm"; + * returns instance of KeyManagerFactory + */ + public void testKeyManagerFactory01() throws NoSuchAlgorithmException { + if (!DEFSupported) { + fail(NotSupportedMsg); + return; + } + KeyManagerFactory keyMF; + for (int i = 0; i < validValues.length; i++) { + keyMF = KeyManagerFactory.getInstance(validValues[i]); + assertTrue("Not KeyManagerFactory object", + keyMF instanceof KeyManagerFactory); + assertEquals("Invalid algorithm", keyMF.getAlgorithm(), + validValues[i]); + } + } + + /** + * Test for getInstance(String algorithm) method + * Assertion: + * throws NullPointerException when algorithm is null; + * throws NoSuchAlgorithmException when algorithm is not correct; + */ + public void testKeyManagerFactory02() { + try { + KeyManagerFactory.getInstance(null); + fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null"); + } catch (NoSuchAlgorithmException e) { + } catch (NullPointerException e) { + } + for (int i = 0; i < invalidValues.length; i++) { + try { + KeyManagerFactory.getInstance(invalidValues[i]); + fail("NoSuchAlgorithmException was not thrown as expected for algorithm: " + .concat(invalidValues[i])); + } catch (NoSuchAlgorithmException e) { + } + } + } + + /** + * Test for getInstance(String algorithm, String provider) + * method + * Assertion: throws IllegalArgumentException when provider is null or empty + */ + public void testKeyManagerFactory03() throws NoSuchProviderException, + NoSuchAlgorithmException { + if (!DEFSupported) { + fail(NotSupportedMsg); + return; + } + String provider = null; + for (int i = 0; i < validValues.length; i++) { + try { + KeyManagerFactory.getInstance(validValues[i], provider); + fail("Expected IllegalArgumentException was not thrown for null provider"); + } catch (IllegalArgumentException e) { + } + try { + KeyManagerFactory.getInstance(validValues[i], ""); + fail("Expected IllegalArgumentException was not thrown for empty provider"); + } catch (IllegalArgumentException e) { + } + } + } + + /** + * Test for getInstance(String algorithm, String provider) + * method + * Assertion: + * throws NullPointerException when algorithm is null; + * throws NoSuchAlgorithmException when algorithm is not correct; + */ + public void testKeyManagerFactory04() throws NoSuchProviderException { + if (!DEFSupported) { + fail(NotSupportedMsg); + return; + } + try { + KeyManagerFactory.getInstance(null, defaultProviderName); + fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null"); + } catch (NoSuchAlgorithmException e) { + } catch (NullPointerException e) { + } + for (int i = 0; i < invalidValues.length; i++) { + try { + KeyManagerFactory.getInstance(invalidValues[i], + defaultProviderName); + fail("NoSuchAlgorithmException must be thrown (algorithm: " + .concat(invalidValues[i]).concat(")")); + } catch (NoSuchAlgorithmException e) { + } + } + } + + /** + * Test for getInstance(String algorithm, String provider) + * method + * Assertion: throws NoSuchProviderException when provider has + * invalid value + */ + public void testKeyManagerFactory05() throws NoSuchAlgorithmException { + if (!DEFSupported) { + fail(NotSupportedMsg); + return; + } + for (int i = 0; i < validValues.length; i++) { + for (int j = 1; j < invalidValues.length; j++) { + try { + KeyManagerFactory.getInstance(validValues[i], + invalidValues[j]); + fail("NuSuchProviderException must be thrown (algorithm: " + + validValues[i] + " provider: " + invalidValues[j] + + ")"); + } catch (NoSuchProviderException e) { + } + } + } + } + + /** + * Test for getInstance(String algorithm, String provider) + * method Assertion: returns instance of KeyManagerFactory + */ + public void testKeyManagerFactory06() throws NoSuchProviderException, + NoSuchAlgorithmException { + if (!DEFSupported) { + fail(NotSupportedMsg); + return; + } + KeyManagerFactory kMF; + for (int i = 0; i < validValues.length; i++) { + kMF = KeyManagerFactory.getInstance(validValues[i], + defaultProviderName); + assertTrue("Not KeyManagerFactory object", + kMF instanceof KeyManagerFactory); + assertEquals("Incorrect algorithm", kMF.getAlgorithm(), + validValues[i]); + assertEquals("Incorrect provider", kMF.getProvider().getName(), + defaultProviderName); + } + } + + /** + * Test for getInstance(String algorithm, Provider provider) + * method + * Assertion: throws IllegalArgumentException when provider is null + */ + public void testKeyManagerFactory07() throws NoSuchAlgorithmException { + if (!DEFSupported) { + fail(NotSupportedMsg); + return; + } + Provider provider = null; + for (int i = 0; i < validValues.length; i++) { + try { + KeyManagerFactory.getInstance(validValues[i], provider); + fail("Expected IllegalArgumentException was not thrown when provider is null"); + } catch (IllegalArgumentException e) { + } + } + } + + /** + * Test for getInstance(String algorithm, Provider provider) + * method + * Assertion: + * throws NullPointerException when algorithm is null; + * throws NoSuchAlgorithmException when algorithm is not correct; + */ + public void testKeyManagerFactory08() { + if (!DEFSupported) { + fail(NotSupportedMsg); + return; + } + try { + KeyManagerFactory.getInstance(null, defaultProvider); + fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null"); + } catch (NoSuchAlgorithmException e) { + } catch (NullPointerException e) { + } + for (int i = 0; i < invalidValues.length; i++) { + try { + KeyManagerFactory + .getInstance(invalidValues[i], defaultProvider); + fail("Expected NuSuchAlgorithmException was not thrown"); + } catch (NoSuchAlgorithmException e) { + } + } + } + + /** + * Test for getInstance(String algorithm, Provider provider) + * method + * Assertion: returns instance of KeyManagerFactory + */ + public void testKeyManagerFactory09() throws NoSuchAlgorithmException, + IllegalArgumentException { + if (!DEFSupported) { + fail(NotSupportedMsg); + return; + } + KeyManagerFactory kMF; + for (int i = 0; i < validValues.length; i++) { + kMF = KeyManagerFactory + .getInstance(validValues[i], defaultProvider); + assertTrue(kMF instanceof KeyManagerFactory); + assertEquals(kMF.getAlgorithm(), validValues[i]); + assertEquals(kMF.getProvider(), defaultProvider); + } + } + + /** + * Test for KeyManagerFactory constructor + * Assertion: returns KeyManagerFactory object + */ + public void testKeyManagerFactory10() throws NoSuchAlgorithmException { + if (!DEFSupported) { + fail(NotSupportedMsg); + return; + } + KeyManagerFactorySpi spi = new MyKeyManagerFactorySpi(); + KeyManagerFactory keyMF = new myKeyManagerFactory(spi, defaultProvider, + defaultAlgorithm); + assertTrue("Not CertStore object", keyMF instanceof KeyManagerFactory); + assertEquals("Incorrect algorithm", keyMF.getAlgorithm(), + defaultAlgorithm); + assertEquals("Incorrect provider", keyMF.getProvider(), defaultProvider); + try { + keyMF.init(null, new char[1]); + fail("UnrecoverableKeyException must be thrown"); + } catch (UnrecoverableKeyException e) { + } catch (Exception e) { + fail("Unexpected: "+e.toString()+" was thrown"); + } + keyMF = new myKeyManagerFactory(null, null, null); + assertTrue("Not CertStore object", keyMF instanceof KeyManagerFactory); + assertNull("Aalgorithm must be null", keyMF.getAlgorithm()); + assertNull("Provider must be null", keyMF.getProvider()); + try { + keyMF.getKeyManagers(); + } catch (NullPointerException e) { + } + } + + /** + * Test for init(KeyStore keyStore, char[] password) and + * getKeyManagers() + * Assertion: returns not empty KeyManager array + */ + public void testKeyManagerFactory11() throws NoSuchAlgorithmException, + KeyStoreException, UnrecoverableKeyException { + if (!DEFSupported) { + fail(NotSupportedMsg); + return; + } + KeyManagerFactory[] keyMF = createKMFac(); + assertNotNull("KeyManagerFactory object were not created", keyMF); + KeyStore ksNull = null; + KeyManager[] km; + for (int i = 0; i < keyMF.length; i++) { + keyMF[i].init(ksNull, new char[10]); + km = keyMF[i].getKeyManagers(); + assertNotNull("Result should not be null", km); + assertTrue("Length of result KeyManager array should not be 0", + (km.length > 0)); + } + KeyStore ks; + try { + ks = KeyStore.getInstance(KeyStore.getDefaultType()); + ks.load(null, null); + } catch (KeyStoreException e) { + fail(e.toString() + "default KeyStore type is not supported"); + return; + } catch (Exception e) { + fail("Unexpected: " + e.toString()); + return; + } + for (int i = 0; i < keyMF.length; i++) { + try { + keyMF[i].init(ks, new char[10]); + } catch (KeyStoreException e) { + } + km = keyMF[i].getKeyManagers(); + assertNotNull("Result has not be null", km); + assertTrue("Length of result KeyManager array should not be 0", + (km.length > 0)); + } + } + + /** + * Test for init(ManagerFactoryParameters params) + * Assertion: + * throws InvalidAlgorithmParameterException when params is null + */ + public void testKeyManagerFactory12() throws NoSuchAlgorithmException { + if (!DEFSupported) { + fail(NotSupportedMsg); + return; + } + ManagerFactoryParameters par = null; + KeyManagerFactory[] keyMF = createKMFac(); + assertNotNull("KeyManagerFactory object were not created", keyMF); + for (int i = 0; i < keyMF.length; i++) { + try { + keyMF[i].init(par); + fail("InvalidAlgorithmParameterException must be thrown"); + } catch (InvalidAlgorithmParameterException e) { + } + } + } + +} + +/** + * Additional class for KeyManagerFactory constructor verification + */ +class myKeyManagerFactory extends KeyManagerFactory { + public myKeyManagerFactory(KeyManagerFactorySpi spi, Provider prov, + String alg) { + super(spi, prov, alg); + } +} Propchange: harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/KeyManagerFactory1Test.java ------------------------------------------------------------------------------ svn:eol-style = native Added: harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/KeyManagerFactory2Test.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/KeyManagerFactory2Test.java?view=auto&rev=503371 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/KeyManagerFactory2Test.java (added) +++ harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/KeyManagerFactory2Test.java Sun Feb 4 00:10:09 2007 @@ -0,0 +1,282 @@ +/* + * 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.tests.javax.net.ssl; + +import java.security.InvalidAlgorithmParameterException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.Provider; +import java.security.Security; +import java.security.UnrecoverableKeyException; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.ManagerFactoryParameters; + +import org.apache.harmony.security.tests.support.SpiEngUtils; +import org.apache.harmony.xnet.tests.support.MyKeyManagerFactorySpi; +import junit.framework.TestCase; + +/** + * Tests for KeyManagerFactory class constructors and methods + * + */ + +public class KeyManagerFactory2Test extends TestCase { + private static final String srvKeyManagerFactory = "KeyManagerFactory"; + + private static final String defaultAlg = "KeyMF"; + + private static final String KeyManagerFactoryProviderClass = "org.apache.harmony.xnet.tests.support.MyKeyManagerFactorySpi"; + + private static final String[] invalidValues = SpiEngUtils.invalidValues; + + private static final String[] validValues; + + static { + validValues = new String[4]; + validValues[0] = defaultAlg; + validValues[1] = defaultAlg.toLowerCase(); + validValues[2] = "Keymf"; + validValues[3] = "kEYMF"; + } + + Provider mProv; + + protected void setUp() throws Exception { + super.setUp(); + mProv = (new SpiEngUtils()).new MyProvider("MyKMFProvider", + "Provider for testing", srvKeyManagerFactory.concat(".") + .concat(defaultAlg), KeyManagerFactoryProviderClass); + Security.insertProviderAt(mProv, 2); + } + + /* + * @see TestCase#tearDown() + */ + protected void tearDown() throws Exception { + super.tearDown(); + Security.removeProvider(mProv.getName()); + } + + private void checkResult(KeyManagerFactory keyMF) + throws NoSuchAlgorithmException, UnrecoverableKeyException, + KeyStoreException { + KeyStore kStore = null; + ManagerFactoryParameters mfp = null; + + char[] pass = { 'a', 'b', 'c' }; + + try { + keyMF.init(kStore, null); + fail("KeyStoreException must be thrown"); + } catch (KeyStoreException e) { + } + try { + keyMF.init(kStore, pass); + fail("UnrecoverableKeyException must be thrown"); + } catch (UnrecoverableKeyException e) { + } + try { + keyMF.init(mfp); + fail("InvalidAlgorithmParameterException must be thrown"); + } catch (InvalidAlgorithmParameterException e) { + } + assertNull("getKeyManagers() should return null object", keyMF + .getKeyManagers()); + + try { + kStore = KeyStore.getInstance(KeyStore.getDefaultType()); + kStore.load(null, null); + } catch (KeyStoreException e) { + fail("default keystore is not supported"); + return; + } catch (Exception e) { + fail("Unexpected: "+e.toString()); + return; + } + try { + keyMF.init(kStore, pass); + } catch (KeyStoreException e) { + fail("Unexpected KeyStoreException was thrown"); + } + mfp = (ManagerFactoryParameters) new MyKeyManagerFactorySpi.Parameters(kStore, null); + try { + keyMF.init(mfp); + fail("InvalidAlgorithmParameterException must be thrown"); + } catch (InvalidAlgorithmParameterException e) { + } + mfp = (ManagerFactoryParameters) new MyKeyManagerFactorySpi.Parameters(kStore, pass); + try { + keyMF.init(mfp); + } catch (InvalidAlgorithmParameterException e) { + fail("Unexpected InvalidAlgorithmParameterException was thrown"); + } + } + /** + * Test for getInstance(String algorithm) method + * Assertions: + * throws NullPointerException when algorithm is null; + * throws NoSuchAlgorithmException when algorithm is not correct; + * returns KeyManagerFactory object + */ + public void testGetInstance01() throws NoSuchAlgorithmException, + KeyStoreException, UnrecoverableKeyException { + try { + KeyManagerFactory.getInstance(null); + fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null"); + } catch (NoSuchAlgorithmException e) { + } catch (NullPointerException e) { + } + for (int i = 0; i < invalidValues.length; i++) { + try { + KeyManagerFactory.getInstance(invalidValues[i]); + fail("NoSuchAlgorithmException must be thrown (algorithm: " + .concat(invalidValues[i]).concat(")")); + } catch (NoSuchAlgorithmException e) { + } + } + KeyManagerFactory keyMF; + for (int i = 0; i < validValues.length; i++) { + keyMF = KeyManagerFactory.getInstance(validValues[i]); + assertTrue("Not instanceof KeyManagerFactory object", + keyMF instanceof KeyManagerFactory); + assertEquals("Incorrect algorithm", keyMF.getAlgorithm(), + validValues[i]); + assertEquals("Incorrect provider", keyMF.getProvider(), mProv); + checkResult(keyMF); + } + } + + /** + * Test for getInstance(String algorithm, String provider) + * method + * Assertions: + * throws NullPointerException when algorithm is null; + * throws NoSuchAlgorithmException when algorithm is not correct; + * throws IllegalArgumentException when provider is null or empty; + * throws NoSuchProviderException when provider is available; + * returns KeyManagerFactory object + */ + public void testGetInstance02() throws NoSuchAlgorithmException, + NoSuchProviderException, IllegalArgumentException, + KeyStoreException, UnrecoverableKeyException { + try { + KeyManagerFactory.getInstance(null, mProv.getName()); + fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null"); + } catch (NoSuchAlgorithmException e) { + } catch (NullPointerException e) { + } + for (int i = 0; i < invalidValues.length; i++) { + try { + KeyManagerFactory + .getInstance(invalidValues[i], mProv.getName()); + fail("NoSuchAlgorithmException must be thrown (algorithm: " + .concat(invalidValues[i]).concat(")")); + } catch (NoSuchAlgorithmException e) { + } + } + String prov = null; + for (int i = 0; i < validValues.length; i++) { + try { + KeyManagerFactory.getInstance(validValues[i], prov); + fail("IllegalArgumentException must be thrown when provider is null (algorithm: " + .concat(invalidValues[i]).concat(")")); + } catch (IllegalArgumentException e) { + } + try { + KeyManagerFactory.getInstance(validValues[i], ""); + fail("IllegalArgumentException must be thrown when provider is empty (algorithm: " + .concat(invalidValues[i]).concat(")")); + } catch (IllegalArgumentException e) { + } + } + for (int i = 0; i < validValues.length; i++) { + for (int j = 1; j < invalidValues.length; j++) { + try { + KeyManagerFactory.getInstance(validValues[i], + invalidValues[j]); + fail("NoSuchProviderException must be thrown (algorithm: " + .concat(invalidValues[i]).concat(" provider: ") + .concat(invalidValues[j]).concat(")")); + } catch (NoSuchProviderException e) { + } + } + } + KeyManagerFactory keyMF; + for (int i = 0; i < validValues.length; i++) { + keyMF = KeyManagerFactory.getInstance(validValues[i], mProv + .getName()); + assertTrue("Not instanceof KeyManagerFactory object", + keyMF instanceof KeyManagerFactory); + assertEquals("Incorrect algorithm", keyMF.getAlgorithm(), + validValues[i]); + assertEquals("Incorrect provider", keyMF.getProvider().getName(), + mProv.getName()); + checkResult(keyMF); + } + } + + /** + * Test for getInstance(String algorithm, Provider provider) + * method + * Assertions: + * throws NullPointerException when algorithm is null; + * throws NoSuchAlgorithmException when algorithm is not correct; + * throws IllegalArgumentException when provider is null; + * returns KeyManagerFactory object + */ + public void testGetInstance03() throws NoSuchAlgorithmException, + IllegalArgumentException, KeyStoreException, + UnrecoverableKeyException { + try { + KeyManagerFactory.getInstance(null, mProv); + fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null"); + } catch (NoSuchAlgorithmException e) { + } catch (NullPointerException e) { + } + for (int i = 0; i < invalidValues.length; i++) { + try { + KeyManagerFactory.getInstance(invalidValues[i], mProv); + fail("NoSuchAlgorithmException must be thrown (algorithm: " + .concat(invalidValues[i]).concat(")")); + } catch (NoSuchAlgorithmException e) { + } + } + Provider prov = null; + for (int i = 0; i < validValues.length; i++) { + try { + KeyManagerFactory.getInstance(validValues[i], prov); + fail("IllegalArgumentException must be thrown when provider is null (algorithm: " + .concat(invalidValues[i]).concat(")")); + } catch (IllegalArgumentException e) { + } + } + KeyManagerFactory keyMF; + for (int i = 0; i < validValues.length; i++) { + keyMF = KeyManagerFactory.getInstance(validValues[i], mProv); + assertTrue("Not instanceof KeyManagerFactory object", + keyMF instanceof KeyManagerFactory); + assertEquals("Incorrect algorithm", keyMF.getAlgorithm(), + validValues[i]); + assertEquals("Incorrect provider", keyMF.getProvider(), mProv); + checkResult(keyMF); + } + } +} \ No newline at end of file Propchange: harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/KeyManagerFactory2Test.java ------------------------------------------------------------------------------ svn:eol-style = native Added: harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/SSLContext1Test.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/SSLContext1Test.java?view=auto&rev=503371 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/SSLContext1Test.java (added) +++ harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/SSLContext1Test.java Sun Feb 4 00:10:09 2007 @@ -0,0 +1,419 @@ +/* + * 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.tests.javax.net.ssl; + +import java.security.KeyManagementException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.Provider; +import java.security.SecureRandom; +import java.security.UnrecoverableKeyException; + +import javax.net.ssl.KeyManager; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLContextSpi; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLSessionContext; +import javax.net.ssl.SSLServerSocketFactory; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; + +import org.apache.harmony.security.tests.support.SpiEngUtils; +import org.apache.harmony.xnet.tests.support.MySSLContextSpi; +import junit.framework.TestCase; + +/** + * Tests for SSLContext class constructors and methods. + * + */ + +public class SSLContext1Test extends TestCase { + + private static String srvSSLContext = "SSLContext"; + + public static String defaultProtocol = "TLS"; + + private static final String NotSupportMsg = "Default protocol is not supported"; + + private static String defaultProviderName = null; + + private static Provider defaultProvider = null; + + private static final String[] invalidValues = SpiEngUtils.invalidValues; + + private static boolean DEFSupported = false; + + private static final String NotSupportedMsg = "There is no suitable provider for SSLContext"; + + private static String[] validValues = new String[3]; + static { + defaultProvider = SpiEngUtils.isSupport(defaultProtocol, srvSSLContext); + DEFSupported = (defaultProvider != null); + if (DEFSupported) { + defaultProviderName = (DEFSupported ? defaultProvider.getName() + : null); + validValues[0] = defaultProtocol; + validValues[1] = defaultProtocol.toUpperCase(); + validValues[2] = defaultProtocol.toLowerCase(); + } else { + defaultProtocol = null; + } + } + + protected SSLContext[] createSSLCon() { + if (!DEFSupported) { + fail(defaultProtocol + " protocol is not supported"); + return null; + } + SSLContext[] sslC = new SSLContext[3]; + try { + sslC[0] = SSLContext.getInstance(defaultProtocol); + sslC[1] = SSLContext.getInstance(defaultProtocol, defaultProvider); + sslC[2] = SSLContext.getInstance(defaultProtocol, + defaultProviderName); + return sslC; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * Test for getInstance(String protocol) method Assertion: + * returns SSLContext object + */ + public void testSSLContext01() throws NoSuchAlgorithmException { + if (!DEFSupported) { + fail(NotSupportMsg); + return; + } + SSLContext sslContext; + for (int i = 0; i < validValues.length; i++) { + sslContext = SSLContext.getInstance(validValues[i]); + assertTrue("Not SSLContext object", + sslContext instanceof SSLContext); + assertEquals("Invalid protocol", sslContext.getProtocol(), + validValues[i]); + } + } + + /** + * Test for getInstance(String protocol) method Assertion: + * throws NullPointerException when protocol is null; throws + * NoSuchAlgorithmException when protocol is not correct; + */ + public void testSSLContext02() { + try { + SSLContext.getInstance(null); + fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null"); + } catch (NoSuchAlgorithmException e) { + } catch (NullPointerException e) { + } + for (int i = 0; i < invalidValues.length; i++) { + try { + SSLContext.getInstance(invalidValues[i]); + fail("NoSuchAlgorithmException was not thrown as expected for provider: " + .concat(invalidValues[i])); + } catch (NoSuchAlgorithmException e) { + } + } + } + + /** + * Test for getInstance(String protocol, String provider) + * method Assertion: throws IllegalArgumentException when provider is null + * or empty + */ + public void testSSLContext03() throws NoSuchProviderException, + NoSuchAlgorithmException { + if (!DEFSupported) { + fail(NotSupportMsg); + return; + } + String provider = null; + for (int i = 0; i < validValues.length; i++) { + try { + SSLContext.getInstance(defaultProtocol, provider); + fail("IllegalArgumentException must be thrown when provider is null"); + } catch (IllegalArgumentException e) { + } + try { + SSLContext.getInstance(defaultProtocol, ""); + fail("IllegalArgumentException must be thrown when provider is empty"); + } catch (IllegalArgumentException e) { + } + } + } + + /** + * Test for getInstance(String protocol, String provider) + * method Assertion: throws NullPointerException when protocol is null; + * throws NoSuchAlgorithmException when protocol is not correct; + */ + public void testSSLContext04() throws NoSuchProviderException { + if (!DEFSupported) { + fail(NotSupportMsg); + return; + } + try { + SSLContext.getInstance(null, defaultProviderName); + fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null"); + } catch (NoSuchAlgorithmException e) { + } catch (NullPointerException e) { + } + for (int i = 0; i < invalidValues.length; i++) { + try { + SSLContext.getInstance(invalidValues[i], defaultProviderName); + fail("NoSuchAlgorithmException was not thrown as expected (protocol: " + .concat(invalidValues[i]).concat(")")); + } catch (NoSuchAlgorithmException e) { + } + } + } + + /** + * Test for getInstance(String protocol, String provider) + * method Assertion: throws NoSuchProviderException when provider has + * invalid value + */ + public void testSSLContext05() throws NoSuchAlgorithmException { + if (!DEFSupported) { + fail(NotSupportMsg); + return; + } + for (int i = 1; i < invalidValues.length; i++) { + for (int j = 0; j < validValues.length; j++) { + try { + SSLContext.getInstance(validValues[j], invalidValues[i]); + fail("NuSuchProviderException must be thrown (protocol: " + .concat(validValues[j]).concat(" provider: ") + .concat(invalidValues[i]).concat(")")); + } catch (NoSuchProviderException e) { + } + } + } + } + + /** + * Test for getInstance(String protocol, String provider) + * method Assertion: returns instance of SSLContext + */ + public void testSSLContext06() throws NoSuchAlgorithmException, + NoSuchProviderException { + if (!DEFSupported) { + fail(NotSupportMsg); + return; + } + SSLContext sslContext; + for (int i = 0; i < validValues.length; i++) { + sslContext = SSLContext.getInstance(validValues[i], + defaultProviderName); + assertTrue("Not SSLContext object", + sslContext instanceof SSLContext); + assertEquals("Invalid protocol", sslContext.getProtocol(), + validValues[i]); + assertEquals("Invalid provider", sslContext.getProvider(), + defaultProvider); + } + } + + /** + * Test for getInstance(String protocol, Provider provider) + * method Assertion: throws IllegalArgumentException when provider is null + */ + public void testSSLContext07() throws NoSuchAlgorithmException { + if (!DEFSupported) { + fail(NotSupportMsg); + return; + } + Provider provider = null; + for (int i = 0; i < validValues.length; i++) { + try { + SSLContext.getInstance(validValues[i], provider); + fail("IllegalArgumentException must be thrown when provider is null"); + } catch (IllegalArgumentException e) { + } + } + } + + /** + * Test for getInstance(String protocol, Provider provider) + * method Assertion: throws NullPointerException when protocol is null; + * throws NoSuchAlgorithmException when protocol is not correct; + */ + public void testSSLContext08() { + if (!DEFSupported) { + fail(NotSupportMsg); + return; + } + try { + SSLContext.getInstance(null, defaultProvider); + fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null"); + } catch (NoSuchAlgorithmException e) { + } catch (NullPointerException e) { + } + for (int i = 0; i < invalidValues.length; i++) { + try { + SSLContext.getInstance(invalidValues[i], defaultProvider); + fail("Expected NoSuchAlgorithmException was not thrown as expected"); + } catch (NoSuchAlgorithmException e) { + } + } + } + + /** + * Test for getInstance(String protocol, Provider provider) + * method Assertion: returns instance of SSLContext + */ + public void testSSLContext09() throws NoSuchAlgorithmException { + if (!DEFSupported) { + fail(NotSupportMsg); + return; + } + SSLContext sslContext; + for (int i = 0; i < validValues.length; i++) { + sslContext = SSLContext + .getInstance(validValues[i], defaultProvider); + assertTrue("Not SSLContext object", + sslContext instanceof SSLContext); + assertEquals("Invalid protocol", sslContext.getProtocol(), + validValues[i]); + assertEquals("Invalid provider", sslContext.getProvider(), + defaultProvider); + } + } + + /** + * Test for getClientSessionContext() + * getServiceSessionContext() + * methods Assertion: returns correspondent object + */ + public void testSSLContext10() throws NoSuchAlgorithmException { + if (!DEFSupported) { + fail(NotSupportMsg); + return; + } + SSLContext[] sslC = createSSLCon(); + assertNotNull("SSLContext objects were not created", sslC); + for (int i = 0; i < sslC.length; i++) { + assertTrue(sslC[i].getClientSessionContext() instanceof SSLSessionContext); + assertTrue(sslC[i].getServerSessionContext() instanceof SSLSessionContext); + } + } + + /** + * Test for getServerSocketFactory() + * getSocketFactory() + * init(KeyManager[] km, TrustManager[] tm, SecureRandom random) + * methods Assertion: returns correspondent object + * + */ + + public void testSSLContext11() throws NoSuchAlgorithmException, + KeyManagementException, KeyStoreException, + UnrecoverableKeyException { + if (!DEFSupported) { + fail(NotSupportMsg); + return; + } + SSLContext[] sslC = createSSLCon(); + assertNotNull("SSLContext objects were not created", sslC); + String tAlg = TrustManagerFactory.getDefaultAlgorithm(); + String kAlg = KeyManagerFactory.getDefaultAlgorithm(); + if (tAlg == null) { + fail("TrustManagerFactory default algorithm is not defined"); + return; + } + if (kAlg == null) { + fail("KeyManagerFactory default algorithm is not defined"); + return; + } + KeyManagerFactory kmf = KeyManagerFactory.getInstance(kAlg); + KeyStore ks = null; + kmf.init(ks, new char[10]); + KeyManager[] kms = kmf.getKeyManagers(); + TrustManagerFactory tmf = TrustManagerFactory.getInstance(tAlg); + tmf.init(ks); + TrustManager[] tms = tmf.getTrustManagers(); + for (int i = 0; i < sslC.length; i++) { + sslC[i].init(kms, tms, new SecureRandom()); + assertTrue(sslC[i].getServerSocketFactory() instanceof SSLServerSocketFactory); + assertTrue(sslC[i].getSocketFactory() instanceof SSLSocketFactory); + } + } + + /** + * Test for SSLContext constructor Assertion: returns + * SSLContext object + */ + public void testSSLContext12() throws NoSuchAlgorithmException, + KeyManagementException { + if (!DEFSupported) { + fail(NotSupportMsg); + return; + } + SSLContextSpi spi = new MySSLContextSpi(); + SSLContext sslContext = new mySSLContext(spi, defaultProvider, + defaultProtocol); + assertTrue("Not CertStore object", sslContext instanceof SSLContext); + assertEquals("Incorrect protocol", sslContext.getProtocol(), + defaultProtocol); + assertEquals("Incorrect provider", sslContext.getProvider(), + defaultProvider); + TrustManager[] tm = null; + KeyManager[] km = null; + sslContext.init(km, tm, new SecureRandom()); + assertTrue(sslContext.createSSLEngine() instanceof SSLEngine); + assertTrue(sslContext.createSSLEngine("host host", 8888) instanceof SSLEngine); + try { + sslContext.init(km, tm, null); + fail("KeyManagementException should be thrown for null SEcureRandom"); + } catch (KeyManagementException e) { + } + + sslContext = new mySSLContext(null, null, null); + assertTrue("Not CertStore object", sslContext instanceof SSLContext); + assertNull("Incorrect protocol", sslContext.getProtocol()); + assertNull("Incorrect provider", sslContext.getProvider()); + try { + sslContext.createSSLEngine(); + fail("NullPointerException should be thrown"); + } catch (NullPointerException e) { + } + try { + sslContext.getSocketFactory(); + fail("NullPointerException should be thrown"); + } catch (NullPointerException e) { + } + } + +} + +/** + * Addifional class to verify SSLContext constructor + */ + +class mySSLContext extends SSLContext { + public mySSLContext(SSLContextSpi spi, Provider prov, String alg) { + super(spi, prov, alg); + } +} \ No newline at end of file Propchange: harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/SSLContext1Test.java ------------------------------------------------------------------------------ svn:eol-style = native Added: harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/SSLContext2Test.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/SSLContext2Test.java?view=auto&rev=503371 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/SSLContext2Test.java (added) +++ harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/SSLContext2Test.java Sun Feb 4 00:10:09 2007 @@ -0,0 +1,299 @@ +/* + * 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.tests.javax.net.ssl; + +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.Provider; +import java.security.SecureRandom; +import java.security.Security; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.KeyManager; +import javax.net.ssl.TrustManager; + +import org.apache.harmony.security.tests.support.SpiEngUtils; +import org.apache.harmony.xnet.tests.support.MySSLContextSpi; +import junit.framework.TestCase; + +/** + * Tests for SSLContext class constructors and methods + * + */ + +public class SSLContext2Test extends TestCase { + + private static String srvSSLContext = "SSLContext"; + + private static final String defaultProtocol = "S+S+L"; + + public static final String SSLContextProviderClass = "org.apache.harmony.xnet.tests.support.MySSLContextSpi"; + + private static final String[] invalidValues = SpiEngUtils.invalidValues; + + private static final String[] validValues; + static { + validValues = new String[4]; + validValues[0] = defaultProtocol; + validValues[1] = defaultProtocol.toLowerCase(); + validValues[2] = "s+S+L"; + validValues[3] = "S+s+L"; + } + + Provider mProv; + + protected void setUp() throws Exception { + super.setUp(); + mProv = (new SpiEngUtils()).new MyProvider("MySSLContextProvider", "Provider for testing", + srvSSLContext.concat(".").concat(defaultProtocol), + SSLContextProviderClass); + Security.insertProviderAt(mProv, 1); + } + + /* + * @see TestCase#tearDown() + */ + protected void tearDown() throws Exception { + super.tearDown(); + Security.removeProvider(mProv.getName()); + } + + private void checkSSLContext(SSLContext sslC) + throws KeyManagementException { + + try { + sslC.getSocketFactory(); + fail("RuntimeException must be thrown"); + } catch (RuntimeException e) { + assertEquals("Incorrect message", e.getMessage(),"Not initialiazed"); + } + try { + sslC.getServerSocketFactory(); + fail("RuntimeException must be thrown"); + } catch (RuntimeException e) { + assertEquals("Incorrect message", e.getMessage(),"Not initialiazed"); + } + try { + sslC.getServerSessionContext(); + fail("RuntimeException must be thrown"); + } catch (RuntimeException e) { + assertEquals("Incorrect message", e.getMessage(),"Not initialiazed"); + } + try { + sslC.getClientSessionContext(); + fail("RuntimeException must be thrown"); + } catch (RuntimeException e) { + assertEquals("Incorrect message", e.getMessage(),"Not initialiazed"); + } + try { + sslC.createSSLEngine(); + fail("RuntimeException must be thrown"); + } catch (RuntimeException e) { + assertEquals("Incorrect message", e.getMessage(),"Not initialiazed"); + } + try { + sslC.createSSLEngine("host",1); + fail("RuntimeException must be thrown"); + } catch (RuntimeException e) { + assertEquals("Incorrect message", e.getMessage(),"Not initialiazed"); + } + TrustManager [] tm = new TManager[10]; + KeyManager [] km = new KManager[5]; + try { + sslC.init(km, tm, null); + fail("KeyManagementException must be thrown"); + } catch (KeyManagementException e) { + } + sslC.init(km, tm, new SecureRandom()); + + SSLEngine sslE = sslC.createSSLEngine(); + assertTrue("Not null result",sslE instanceof SSLEngine); + assertNull("Incorrect host", sslE.getPeerHost()); + assertEquals("Incorrect port", sslE.getPeerPort(), 0); + String host = "ZZZ"; + int port = 8080; + sslE = sslC.createSSLEngine(host, port); + assertTrue("Not null result",sslE instanceof SSLEngine); + assertEquals("Incorrect host", sslE.getPeerHost(), host); + assertEquals("Incorrect port", sslE.getPeerPort(), port); + try { + assertNull("Not null result", sslC.getServerSessionContext()); + } catch (NullPointerException e) { + } + try { + assertNull("Not null result", sslC.getClientSessionContext()); + } catch (NullPointerException e) { + } + } + + /** + * Test for getInstance(String protocol) method + * Assertions: + * throws NullPointerException when protocol is null; + * throws NoSuchAlgorithmException when protocol is not correct; + * returns SSLContext object + */ + public void testGetInstance01() throws NoSuchAlgorithmException, + KeyManagementException { + try { + SSLContext.getInstance(null); + fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null"); + } catch (NoSuchAlgorithmException e) { + } catch (NullPointerException e) { + } + for (int i = 0; i < invalidValues.length; i++) { + try { + SSLContext.getInstance(invalidValues[i]); + fail("NoSuchAlgorithmException must be thrown (protocol: " + .concat(invalidValues[i]).concat(")")); + } catch (NoSuchAlgorithmException e) { + } + } + SSLContext sslC; + for (int i = 0; i < validValues.length; i++) { + sslC = SSLContext.getInstance(validValues[i]); + assertTrue("Not instanceof SSLContext object", + sslC instanceof SSLContext); + assertEquals("Incorrect protocol", sslC.getProtocol(), + validValues[i]); + assertEquals("Incorrect provider", sslC.getProvider(), mProv); + checkSSLContext(sslC); + } + } + + /** + * Test for getInstance(String protocol, String provider) + * method + * Assertions: + * throws NullPointerException when protocol is null; + * throws NoSuchAlgorithmException when protocol is not correct; + * throws IllegalArgumentException when provider is null or empty; + * throws NoSuchProviderException when provider is available; + * returns SSLContext object + */ + public void testGetInstance02() throws NoSuchAlgorithmException, + NoSuchProviderException, IllegalArgumentException, + KeyManagementException { + try { + SSLContext.getInstance(null, mProv.getName()); + fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null"); + } catch (NoSuchAlgorithmException e) { + } catch (NullPointerException e) { + } + for (int i = 0; i < invalidValues.length; i++) { + try { + SSLContext.getInstance(invalidValues[i], mProv.getName()); + fail("NoSuchAlgorithmException must be thrown (protocol: " + .concat(invalidValues[i]).concat(")")); + } catch (NoSuchAlgorithmException e) { + } + } + String prov = null; + for (int i = 0; i < validValues.length; i++) { + try { + SSLContext.getInstance(validValues[i], prov); + fail("IllegalArgumentException must be thrown when provider is null (protocol: " + .concat(invalidValues[i]).concat(")")); + } catch (IllegalArgumentException e) { + } + try { + SSLContext.getInstance(validValues[i], ""); + fail("IllegalArgumentException must be thrown when provider is empty (protocol: " + .concat(invalidValues[i]).concat(")")); + } catch (IllegalArgumentException e) { + } + } + for (int i = 0; i < validValues.length; i++) { + for (int j = 1; j < invalidValues.length; j++) { + try { + SSLContext.getInstance(validValues[i], invalidValues[j]); + fail("NoSuchProviderException must be thrown (protocol: " + .concat(invalidValues[i]).concat(" provider: ") + .concat(invalidValues[j]).concat(")")); + } catch (NoSuchProviderException e) { + } + } + } + SSLContext sslC; + for (int i = 0; i < validValues.length; i++) { + sslC = SSLContext.getInstance(validValues[i], mProv.getName()); + assertTrue("Not instanceof SSLContext object", + sslC instanceof SSLContext); + assertEquals("Incorrect protocol", sslC.getProtocol(), + validValues[i]); + assertEquals("Incorrect provider", sslC.getProvider().getName(), + mProv.getName()); + checkSSLContext(sslC); + } + } + + /** + * Test for getInstance(String protocol, Provider provider) + * method + * Assertions: + * throws NullPointerException when protocol is null; + * throws NoSuchAlgorithmException when protocol is not correct; + * throws IllegalArgumentException when provider is null; + * returns SSLContext object + */ + public void testGetInstance03() throws NoSuchAlgorithmException, + IllegalArgumentException, KeyManagementException { + try { + SSLContext.getInstance(null, mProv); + fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null"); + } catch (NoSuchAlgorithmException e) { + } catch (NullPointerException e) { + } + for (int i = 0; i < invalidValues.length; i++) { + try { + SSLContext.getInstance(invalidValues[i], mProv); + fail("NoSuchAlgorithmException must be thrown (protocol: " + .concat(invalidValues[i]).concat(")")); + } catch (NoSuchAlgorithmException e) { + } + } + Provider prov = null; + for (int i = 0; i < validValues.length; i++) { + try { + SSLContext.getInstance(validValues[i], prov); + fail("IllegalArgumentException must be thrown when provider is null (protocol: " + .concat(invalidValues[i]).concat(")")); + } catch (IllegalArgumentException e) { + } + } + SSLContext sslC; + for (int i = 0; i < validValues.length; i++) { + sslC = SSLContext.getInstance(validValues[i], mProv); + assertTrue("Not instanceof SSLContext object", + sslC instanceof SSLContext); + assertEquals("Incorrect protocol", sslC.getProtocol(), + validValues[i]); + assertEquals("Incorrect provider", sslC.getProvider(), mProv); + checkSSLContext(sslC); + } + } + + class TManager implements TrustManager { + + } + class KManager implements KeyManager { + + } +} \ No newline at end of file Propchange: harmony/enhanced/classlib/trunk/modules/x-net/src/test/api/java/org/apache/harmony/xnet/tests/javax/net/ssl/SSLContext2Test.java ------------------------------------------------------------------------------ svn:eol-style = native