--- old/test/sun/security/pkcs11/PKCS11Test.java Wed May 27 22:52:23 2015 +++ new/test/sun/security/pkcs11/PKCS11Test.java Wed May 27 22:52:23 2015 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -26,7 +26,6 @@ import java.io.*; import java.util.*; -import java.lang.reflect.*; import java.security.*; import java.security.spec.ECGenParameterSpec; @@ -70,12 +69,40 @@ // for quick checking for generic testing than many if-else statements. static double softoken3_version = -1; static double nss3_version = -1; + static Provider sunpkcs11; + // Goes through ServiceLoader instead Provider.getInstance() since + // SunPKCS11 provider is NOT registered for non-Solaris platforms + static { + ServiceLoader sl = ServiceLoader.load(java.security.Provider.class); + Iterator iter = sl.iterator(); + Provider p = null; + while (iter.hasNext()) { + try { + p = iter.next(); + if (p.getName().startsWith("SunPKCS11")) { + break; + }; + } catch (Exception e) { + // ignore and move on to the next one + } + } + sunpkcs11 = p; + } + + // Return a SunPKCS11 provider with default config file (Solaris) + // or a dummy one which requires further configuration (non-Solaris) + static Provider getSunPKCS11() throws NoSuchProviderException { + if (sunpkcs11 == null) { + throw new NoSuchProviderException("No PKCS11 provider available"); + } + return sunpkcs11; + } + + + // Return a SunPKCS11 provider using the specified config file static Provider getSunPKCS11(String config) throws Exception { - Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11"); - Constructor cons = clazz.getConstructor(new Class[] {String.class}); - Object obj = cons.newInstance(new Object[] {config}); - return (Provider)obj; + return getSunPKCS11().configure(config); } public abstract void main(Provider p) throws Exception; @@ -85,7 +112,8 @@ System.out.println("Running test with provider " + p.getName() + "..."); main(p); long stop = System.currentTimeMillis(); - System.out.println("Completed test with provider " + p.getName() + " (" + (stop - start) + " ms)."); + System.out.println("Completed test with provider " + p.getName() + + " (" + (stop - start) + " ms)."); } public static void main(PKCS11Test test) throws Exception { @@ -113,9 +141,11 @@ } if (found) { for (Provider p: newProviders) { + System.out.println("PKCS11Test: remove new Provider: " + p.getName()); Security.removeProvider(p.getName()); } for (Provider p: oldProviders) { + System.out.println("PKCS11Test: add old Provider: " + p.getName()); Security.addProvider(p); } } @@ -576,8 +606,7 @@ if ((b == null) || (b.length == 0)) { return a; } - T[] r = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), a.length + b.length); - System.arraycopy(a, 0, r, 0, a.length); + T[] r = Arrays.copyOf(a, a.length + b.length); System.arraycopy(b, 0, r, a.length, b.length); return r; }