test/jdk/java/security/KeyStore/ProbeKeystores.java
changeset 48700 953eca1167b6
parent 47216 71c04702a3d5
child 51131 9502e3b9d415
equal deleted inserted replaced
48699:f4e628259d1b 48700:953eca1167b6
     1 /*
     1 /*
     2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 8044445
    26  * @bug 8044445 8194307
    27  * @summary test new methods from JEP-229: Create PKCS12 Keystores by Default
    27  * @summary test new methods from JEP-229: Create PKCS12 Keystores by Default
    28  */
    28  */
    29 
    29 
    30 import java.io.*;
    30 import java.io.*;
    31 import java.security.*;
    31 import java.security.*;
    35 import javax.security.auth.callback.*;
    35 import javax.security.auth.callback.*;
    36 
    36 
    37 public class ProbeKeystores {
    37 public class ProbeKeystores {
    38     private static final char[] PASSWORD = "changeit".toCharArray();
    38     private static final char[] PASSWORD = "changeit".toCharArray();
    39     private static final char[] BAD_PASSWORD = "badpasword".toCharArray();
    39     private static final char[] BAD_PASSWORD = "badpasword".toCharArray();
       
    40     private static final LoadStoreParameter LOAD_STORE_PARAM =
       
    41         new MyLoadStoreParameter(new PasswordProtection(PASSWORD));
       
    42     private static final LoadStoreParameter BAD_LOAD_STORE_PARAM =
       
    43         new MyLoadStoreParameter(new PasswordProtection(BAD_PASSWORD));
    40     private static final String DIR = System.getProperty("test.src", ".");
    44     private static final String DIR = System.getProperty("test.src", ".");
    41     private static final String CERT_FILE = "trusted.pem";
    45     private static final String CERT_FILE = "trusted.pem";
       
    46 
       
    47     private static class MyLoadStoreParameter implements LoadStoreParameter {
       
    48 
       
    49         private ProtectionParameter protection;
       
    50 
       
    51         MyLoadStoreParameter(ProtectionParameter protection) {
       
    52             this.protection = protection;
       
    53         }
       
    54 
       
    55         public ProtectionParameter getProtectionParameter() {
       
    56             return protection;
       
    57         }
       
    58     }
    42 
    59 
    43     public static final void main(String[] args) throws Exception {
    60     public static final void main(String[] args) throws Exception {
    44 
    61 
    45         // Testing empty keystores
    62         // Testing empty keystores
    46 
    63 
   167         }
   184         }
   168 
   185 
   169         // Next try with an incorrect password
   186         // Next try with an incorrect password
   170         try {
   187         try {
   171             ks = KeyStore.getInstance(new File(file), BAD_PASSWORD);
   188             ks = KeyStore.getInstance(new File(file), BAD_PASSWORD);
       
   189             throw new Exception("ERROR: expected an exception but got success");
       
   190         } catch (IOException e) {
       
   191             System.out.println("Failed to load a " + type + " keystore named '" + file + "' (as expected)");
       
   192         }
       
   193 
       
   194         // Now try with the correct password within a LoadStoreParameter
       
   195         ks = KeyStore.getInstance(new File(file), LOAD_STORE_PARAM);
       
   196         if (!type.equalsIgnoreCase(ks.getType())) {
       
   197             throw new Exception("ERROR: expected a " + type + " keystore, " +
       
   198                 "got a " + ks.getType() + " keystore instead");
       
   199         } else {
       
   200             System.out.println("Probed a " + type + " keystore named '" + file + "'");
       
   201         }
       
   202 
       
   203         // Next try with an incorrect password within a LoadStoreParameter
       
   204         try {
       
   205             ks = KeyStore.getInstance(new File(file), BAD_LOAD_STORE_PARAM);
   172             throw new Exception("ERROR: expected an exception but got success");
   206             throw new Exception("ERROR: expected an exception but got success");
   173         } catch (IOException e) {
   207         } catch (IOException e) {
   174             System.out.println("Failed to load a " + type + " keystore named '" + file + "' (as expected)");
   208             System.out.println("Failed to load a " + type + " keystore named '" + file + "' (as expected)");
   175         }
   209         }
   176     }
   210     }