jdk/test/sun/security/pkcs11/PKCS11Test.java
changeset 31270 e6470b24700d
parent 28403 02b91a87c4dd
child 32138 23830562d3d1
equal deleted inserted replaced
31269:14968253ce7e 31270:e6470b24700d
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2015, 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.
    24 
    24 
    25 // common infrastructure for SunPKCS11 tests
    25 // common infrastructure for SunPKCS11 tests
    26 
    26 
    27 import java.io.*;
    27 import java.io.*;
    28 import java.util.*;
    28 import java.util.*;
    29 import java.lang.reflect.*;
       
    30 
    29 
    31 import java.security.*;
    30 import java.security.*;
    32 import java.security.spec.ECGenParameterSpec;
    31 import java.security.spec.ECGenParameterSpec;
    33 import java.security.spec.ECParameterSpec;
    32 import java.security.spec.ECParameterSpec;
    34 
    33 
    68 
    67 
    69     // NSS versions of each library.  It is simplier to keep nss_version
    68     // NSS versions of each library.  It is simplier to keep nss_version
    70     // for quick checking for generic testing than many if-else statements.
    69     // for quick checking for generic testing than many if-else statements.
    71     static double softoken3_version = -1;
    70     static double softoken3_version = -1;
    72     static double nss3_version = -1;
    71     static double nss3_version = -1;
    73 
    72     static Provider pkcs11;
       
    73 
       
    74     // Goes through ServiceLoader instead of Provider.getInstance() since it
       
    75     // works on all platforms
       
    76     static {
       
    77         ServiceLoader sl = ServiceLoader.load(java.security.Provider.class);
       
    78         Iterator<Provider> iter = sl.iterator();
       
    79         Provider p = null;
       
    80         boolean found = false;
       
    81         while (iter.hasNext()) {
       
    82             try {
       
    83                 p = iter.next();
       
    84                 if (p.getName().equals("SunPKCS11")) {
       
    85                     found = true;
       
    86                     break;
       
    87                 };
       
    88             } catch (Exception e) {
       
    89                 // ignore and move on to the next one
       
    90             }
       
    91         }
       
    92         // Nothing found through ServiceLoader; fall back to reflection
       
    93         if (!found) {
       
    94             try {
       
    95                 Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11");
       
    96                 p = (Provider) clazz.newInstance();
       
    97             } catch (Exception ex) {
       
    98                 ex.printStackTrace();
       
    99             }
       
   100         }
       
   101         pkcs11 = p;
       
   102     }
       
   103 
       
   104     // Return a SunPKCS11 provider configured with the specified config file
    74     static Provider getSunPKCS11(String config) throws Exception {
   105     static Provider getSunPKCS11(String config) throws Exception {
    75         Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11");
   106         if (pkcs11 == null) {
    76         Constructor cons = clazz.getConstructor(new Class[] {String.class});
   107             throw new NoSuchProviderException("No PKCS11 provider available");
    77         Object obj = cons.newInstance(new Object[] {config});
   108         }
    78         return (Provider)obj;
   109         return pkcs11.configure(config);
    79     }
   110     }
    80 
   111 
    81     public abstract void main(Provider p) throws Exception;
   112     public abstract void main(Provider p) throws Exception;
    82 
   113 
    83     private void premain(Provider p) throws Exception {
   114     private void premain(Provider p) throws Exception {
    84         long start = System.currentTimeMillis();
   115         long start = System.currentTimeMillis();
    85         System.out.println("Running test with provider " + p.getName() + "...");
   116         System.out.println("Running test with provider " + p.getName() + "...");
    86         main(p);
   117         main(p);
    87         long stop = System.currentTimeMillis();
   118         long stop = System.currentTimeMillis();
    88         System.out.println("Completed test with provider " + p.getName() + " (" + (stop - start) + " ms).");
   119         System.out.println("Completed test with provider " + p.getName() +
       
   120             " (" + (stop - start) + " ms).");
    89     }
   121     }
    90 
   122 
    91     public static void main(PKCS11Test test) throws Exception {
   123     public static void main(PKCS11Test test) throws Exception {
    92         Provider[] oldProviders = Security.getProviders();
   124         Provider[] oldProviders = Security.getProviders();
    93         try {
   125         try {
   574 
   606 
   575     <T> T[] concat(T[] a, T[] b) {
   607     <T> T[] concat(T[] a, T[] b) {
   576         if ((b == null) || (b.length == 0)) {
   608         if ((b == null) || (b.length == 0)) {
   577             return a;
   609             return a;
   578         }
   610         }
   579         T[] r = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
   611         T[] r = Arrays.copyOf(a, a.length + b.length);
   580         System.arraycopy(a, 0, r, 0, a.length);
       
   581         System.arraycopy(b, 0, r, a.length, b.length);
   612         System.arraycopy(b, 0, r, a.length, b.length);
   582         return r;
   613         return r;
   583     }
   614     }
   584 
   615 
   585     /**
   616     /**