Path: blob/master/test/jdk/sun/security/provider/KeyStore/WrongPassword.java
41152 views
/*1* Copyright (c) 2005, 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 623653326* @summary verify that JKS throws the correct exception if an incorrect password is specified27* @author Andreas Sterbenz28*/2930import java.io.*;31import java.util.*;3233import java.security.*;34import java.security.KeyStore.*;3536public class WrongPassword {3738private final static String BASE = System.getProperty("test.src", ".");3940public static void main(String[] args) throws Exception {41File ksFile = new File(BASE, "pw.jks");42char[] pw = "test12".toCharArray();43char[] wrongPW = "foobar".toCharArray();44String alias = "mykey";45String otherAlias = "foo";4647KeyStore ks;48InputStream in;49Entry entry;5051ks = KeyStore.getInstance("JKS");52in = new FileInputStream(ksFile);53ks.load(in, null);54in.close();55System.out.println(Collections.list(ks.aliases()));5657ks = KeyStore.getInstance("JKS");58in = new FileInputStream(ksFile);59try {60ks.load(in, wrongPW);61} catch (IOException e) {62e.printStackTrace();63Throwable cause = e.getCause();64if (cause instanceof UnrecoverableKeyException == false) {65throw new Exception("not an UnrecoverableKeyException: " + cause);66}67}68in.close();6970ks = KeyStore.getInstance("JKS");71in = new FileInputStream(ksFile);72ks.load(in, pw);73in.close();74System.out.println(Collections.list(ks.aliases()));7576try {77entry = ks.getEntry(alias, null);78throw new Exception("no exception");79} catch (UnrecoverableKeyException e) {80System.out.println(e);81}8283try {84entry = ks.getEntry(alias, new PasswordProtection(wrongPW));85throw new Exception("no exception");86} catch (UnrecoverableKeyException e) {87System.out.println(e);88}8990try {91entry = ks.getEntry(alias, new PasswordProtection(new char[0]));92throw new Exception("no exception");93} catch (UnrecoverableKeyException e) {94System.out.println(e);95}9697entry = ks.getEntry(alias, new PasswordProtection(pw));98System.out.println(entry.toString().split("\n")[0]);99100try {101ks.getKey(alias, null);102throw new Exception("no exception");103} catch (UnrecoverableKeyException e) {104System.out.println(e);105}106107try {108ks.getKey(alias, wrongPW);109throw new Exception("no exception");110} catch (UnrecoverableKeyException e) {111System.out.println(e);112}113114try {115ks.getKey(alias, new char[0]);116throw new Exception("no exception");117} catch (UnrecoverableKeyException e) {118System.out.println(e);119}120121Key k = ks.getKey(alias, pw);122System.out.println(k.toString().split("\n")[0]);123124System.out.println(ks.getEntry(otherAlias, null));125System.out.println(ks.getEntry(otherAlias, new PasswordProtection(wrongPW)));126System.out.println(ks.getKey(otherAlias, null));127System.out.println(ks.getKey(otherAlias, wrongPW));128129System.out.println("OK");130}131}132133134