Path: blob/master/test/jdk/sun/security/mscapi/KeyStoreCompatibilityMode.java
41152 views
/*1* Copyright (c) 2005, 2018, 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 6324294 6931562 818057026* @requires os.family == "windows"27* @run main KeyStoreCompatibilityMode28* @run main/othervm -Dsun.security.mscapi.keyStoreCompatibilityMode=true KeyStoreCompatibilityMode29* @run main/othervm -Dsun.security.mscapi.keyStoreCompatibilityMode=false KeyStoreCompatibilityMode -disable30* @summary Confirm that a null stream or password is not permitted when31* compatibility mode is enabled (and vice versa).32*/3334import java.io.*;35import java.security.Provider;36import java.security.*;3738public class KeyStoreCompatibilityMode {3940private static final String KEYSTORE_COMPATIBILITY_MODE_PROP =41"sun.security.mscapi.keyStoreCompatibilityMode";4243private static boolean mode;4445public static void main(String[] args) throws Exception {4647if (args.length > 0 && "-disable".equals(args[0])) {48mode = false;49} else {50mode = true;51}5253Provider p = Security.getProvider("SunMSCAPI");5455System.out.println("SunMSCAPI provider classname is " +56p.getClass().getName());57System.out.println(KEYSTORE_COMPATIBILITY_MODE_PROP + " = " +58System.getProperty(KEYSTORE_COMPATIBILITY_MODE_PROP));5960KeyStore myKeyStore = KeyStore.getInstance("Windows-MY", p);61KeyStore myKeyStore2 = KeyStore.getInstance("Windows-MY", p);62KeyStore rootKeyStore = KeyStore.getInstance("Windows-ROOT", p);63KeyStore rootKeyStore2 = KeyStore.getInstance("Windows-ROOT", p);6465InputStream inStream = new ByteArrayInputStream(new byte[1]);66OutputStream outStream = new ByteArrayOutputStream();67char[] password = new char[1];6869// Checking keystore load operations70testLoadStore(myKeyStore, null, null, true);71testLoadStore(myKeyStore2, null, password, true);72testLoadStore(rootKeyStore, inStream, null, true);73testLoadStore(rootKeyStore2, inStream, password, true);7475// Checking keystore store operations76testLoadStore(myKeyStore, null, null, false);77testLoadStore(myKeyStore2, null, password, false);78testLoadStore(rootKeyStore, outStream, null, false);79testLoadStore(rootKeyStore2, outStream, password, false);80}8182private static void testLoadStore(KeyStore keyStore, Object stream,83char[] password, boolean doLoad) throws Exception {8485String streamValue = stream == null ? "null" : "non-null";86String passwordValue = password == null ? "null" : "non-null";8788System.out.println("Checking " + (doLoad ? "load" : "store") +89"(stream=" + streamValue + ", password=" + passwordValue + ")...");9091try {9293if (doLoad) {94keyStore.load((InputStream) stream, password);95} else {96keyStore.store((OutputStream) stream, password);97}9899if (!mode && keyStore != null && password != null) {100throw new Exception(101"Expected an IOException to be thrown by KeyStore.load");102}103104} catch (IOException ioe) {105// When mode=false the exception is expected.106if (mode) {107throw ioe;108} else {109System.out.println("caught the expected exception: " + ioe);110}111112} catch (KeyStoreException kse) {113// store will fail if load has previously failed114if (doLoad) {115throw kse;116} else {117System.out.println("caught the expected exception: " + kse);118}119}120}121}122123124