Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/mscapi/KeyStoreCompatibilityMode.java
38840 views
/*1* Copyright (c) 2005, 2015, 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* @see KeyStoreCompatibilityMode.sh25*/2627import java.io.*;28import java.security.Provider;29import java.security.*;3031public class KeyStoreCompatibilityMode {3233private static final String KEYSTORE_COMPATIBILITY_MODE_PROP =34"sun.security.mscapi.keyStoreCompatibilityMode";3536private static boolean mode;3738public static void main(String[] args) throws Exception {3940if (args.length > 0 && "-disable".equals(args[0])) {41mode = false;42} else {43mode = true;44}4546Provider p = Security.getProvider("SunMSCAPI");4748System.out.println("SunMSCAPI provider classname is " +49p.getClass().getName());50System.out.println(KEYSTORE_COMPATIBILITY_MODE_PROP + " = " +51System.getProperty(KEYSTORE_COMPATIBILITY_MODE_PROP));5253KeyStore myKeyStore = KeyStore.getInstance("Windows-MY", p);54KeyStore myKeyStore2 = KeyStore.getInstance("Windows-MY", p);55KeyStore rootKeyStore = KeyStore.getInstance("Windows-ROOT", p);56KeyStore rootKeyStore2 = KeyStore.getInstance("Windows-ROOT", p);5758InputStream inStream = new ByteArrayInputStream(new byte[1]);59OutputStream outStream = new ByteArrayOutputStream();60char[] password = new char[1];6162// Checking keystore load operations63testLoadStore(myKeyStore, null, null, true);64testLoadStore(myKeyStore2, null, password, true);65testLoadStore(rootKeyStore, inStream, null, true);66testLoadStore(rootKeyStore2, inStream, password, true);6768// Checking keystore store operations69testLoadStore(myKeyStore, null, null, false);70testLoadStore(myKeyStore2, null, password, false);71testLoadStore(rootKeyStore, outStream, null, false);72testLoadStore(rootKeyStore2, outStream, password, false);73}7475private static void testLoadStore(KeyStore keyStore, Object stream,76char[] password, boolean doLoad) throws Exception {7778String streamValue = stream == null ? "null" : "non-null";79String passwordValue = password == null ? "null" : "non-null";8081System.out.println("Checking " + (doLoad ? "load" : "store") +82"(stream=" + streamValue + ", password=" + passwordValue + ")...");8384try {8586if (doLoad) {87keyStore.load((InputStream) stream, password);88} else {89keyStore.store((OutputStream) stream, password);90}9192if (!mode && keyStore != null && password != null) {93throw new Exception(94"Expected an IOException to be thrown by KeyStore.load");95}9697} catch (IOException ioe) {98// When mode=false the exception is expected.99if (mode) {100throw ioe;101} else {102System.out.println("caught the expected exception: " + ioe);103}104105} catch (KeyStoreException kse) {106// store will fail if load has previously failed107if (doLoad) {108throw kse;109} else {110System.out.println("caught the expected exception: " + kse);111}112}113}114}115116117