Path: blob/master/test/jdk/sun/security/ssl/X509KeyManager/SelectOneKeyOutOfMany.java
41152 views
/*1* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4387949 430219726* @summary Need to add Sockets and key arrays to the27* X509KeyManager.choose*Alias() methods & There's no mechanism28* to select one key out of many in a keystore29*30* chooseServerAlias method is reverted back to accept a single31* keytype as a parameter, please see RFE: 450101432* The part of the test on the server-side is changed to test33* passing in a single keytype parameter to chooseServerAlias method.34*35* @author Brad Wetmore36*/3738import java.io.*;39import java.net.*;40import java.security.*;41import javax.net.ssl.*;4243public class SelectOneKeyOutOfMany {4445/*46* =============================================================47* Set the various variables needed for the tests, then48* specify what tests to run on each side.49*/5051/*52* Where do we find the keystores?53*/54static String pathToStores = "../../../../javax/net/ssl/etc";55static String keyStoreFile = "keystore";56static String passwd = "passphrase";5758public static void main(String[] args) throws Exception {59KeyStore ks;60KeyManagerFactory kmf;61X509KeyManager km;6263char[] passphrase = passwd.toCharArray();6465String keyFilename =66System.getProperty("test.src", ".") + "/" + pathToStores +67"/" + keyStoreFile;68/*69* Setup the tests.70*/71kmf = KeyManagerFactory.getInstance("SunX509");72ks = KeyStore.getInstance("JKS");73ks.load(new FileInputStream(keyFilename), passphrase);74kmf.init(ks, passphrase);75km = (X509KeyManager) kmf.getKeyManagers()[0];7677/*78* There should be one of each key type here.79*/80String [] nothing = new String [] { "nothing" };81String [] rsa = new String [] { "RSA" };82String [] dsa = new String [] { "DSA" };83String [] rsaDsa = new String [] { "RSA", "DSA" };84String [] dsaRsa = new String [] { "DSA", "RSA" };8586String resultsRsaDsa, resultsDsaRsa;87String resultsRsa, resultsDsa;88String resultsNone;8990String [] resultArrayRSA;91String [] resultArrayDSA;9293/*94* Check get*Aliases for null returns95*/96if (km.getClientAliases("nothing", null) != null)97throw new Exception("km.getClientAliases(nothing) != null");98System.out.println("km.getClientAlias(nothing) returning nulls");99100if (km.getServerAliases("nothing", null) != null)101throw new Exception("km.getServerAliases(nothing) != null");102System.out.println("km.getServerAlias(nothing) returning nulls");103System.out.println("=====");104105System.out.println("Dumping Certs...");106if ((resultArrayRSA = km.getServerAliases("RSA", null)) == null)107throw new Exception("km.getServerAliases(RSA) == null");108for (int i = 0; i < resultArrayRSA.length; i++) {109System.out.println(" resultArrayRSA#" + i + ": " +110resultArrayRSA[i]);111}112113if ((resultArrayDSA = km.getServerAliases("DSA", null)) == null)114throw new Exception("km.getServerAliases(DSA) == null");115for (int i = 0; i < resultArrayDSA.length; i++) {116System.out.println(" resultArrayDSA#" + i + ": " +117resultArrayDSA[i]);118}119System.out.println("=====");120121/*122* Check chooseClientAliases for null returns123*/124resultsNone = km.chooseClientAlias(nothing, null, null);125if (resultsNone != null) {126throw new Exception("km.chooseClientAlias(nothing) != null");127}128System.out.println("km.ChooseClientAlias(nothing) passed");129130/*131* Check chooseClientAlias for RSA keys.132*/133resultsRsa = km.chooseClientAlias(rsa, null, null);134if (resultsRsa == null) {135throw new Exception(136"km.chooseClientAlias(rsa, null, null) != null");137}138System.out.println("km.chooseClientAlias(rsa) passed");139140/*141* Check chooseClientAlias for DSA keys.142*/143resultsDsa = km.chooseClientAlias(dsa, null, null);144if (resultsDsa == null) {145throw new Exception(146"km.chooseClientAlias(dsa, null, null) != null");147}148System.out.println("km.chooseClientAlias(dsa) passed");149150/*151* There should be both an rsa and a dsa entry in the152* keystore.153*154* Check chooseClientAlias for RSA/DSA keys and be sure155* the ordering is correct.156*/157resultsRsaDsa = km.chooseClientAlias(rsaDsa, null, null);158if ((resultsRsaDsa == null) || (resultsRsaDsa != resultsRsa)) {159throw new Exception("km.chooseClientAlias(rsaDsa) failed");160}161System.out.println("km.chooseClientAlias(rsaDsa) passed");162163resultsDsaRsa = km.chooseClientAlias(dsaRsa, null, null);164if ((resultsDsaRsa == null) || (resultsDsaRsa != resultsDsa)) {165throw new Exception("km.chooseClientAlias(DsaRsa) failed");166}167System.out.println("km.chooseClientAlias(DsaRsa) passed");168169System.out.println("=====");170171/*172* Check chooseServerAliases for null returns173*/174resultsNone = km.chooseServerAlias("nothing", null, null);175if (resultsNone != null) {176throw new Exception("km.chooseServerAlias(\"nothing\") != null");177}178System.out.println("km.ChooseServerAlias(\"nothing\") passed");179180/*181* Check chooseServerAlias for RSA keys.182*/183resultsRsa = km.chooseServerAlias("RSA", null, null);184if (resultsRsa == null) {185throw new Exception(186"km.chooseServerAlias(\"RSA\", null, null) != null");187}188System.out.println("km.chooseServerAlias(\"RSA\") passed");189190/*191* Check chooseServerAlias for DSA keys.192*/193resultsDsa = km.chooseServerAlias("DSA", null, null);194if (resultsDsa == null) {195throw new Exception(196"km.chooseServerAlias(\"DSA\", null, null) != null");197}198System.out.println("km.chooseServerAlias(\"DSA\") passed");199200}201}202203204