8206258: [Test Error] sun/security/pkcs11 tests fail if NSS libs not found
authorjjiang
Thu, 26 Jul 2018 08:46:27 +0800
changeset 51245 f095e3bc2d41
parent 51244 d31dcfaa96f3
child 51246 73f3487f271d
8206258: [Test Error] sun/security/pkcs11 tests fail if NSS libs not found Summary: Improve the logics on skipping test Reviewed-by: valeriep
test/jdk/sun/security/pkcs11/PKCS11Test.java
test/jdk/sun/security/pkcs11/Secmod/TestNssDbSqlite.java
test/jdk/sun/security/pkcs11/Signature/TestDSAKeyLength.java
test/jdk/sun/security/pkcs11/ec/TestCurves.java
test/jdk/sun/security/pkcs11/ec/TestECDH.java
test/jdk/sun/security/pkcs11/ec/TestECDH2.java
test/jdk/sun/security/pkcs11/ec/TestECDSA.java
test/jdk/sun/security/pkcs11/ec/TestECDSA2.java
test/jdk/sun/security/pkcs11/ec/TestECGenSpec.java
--- a/test/jdk/sun/security/pkcs11/PKCS11Test.java	Wed Jul 25 17:21:04 2018 -0700
+++ b/test/jdk/sun/security/pkcs11/PKCS11Test.java	Thu Jul 26 08:46:27 2018 +0800
@@ -154,7 +154,15 @@
 
     public abstract void main(Provider p) throws Exception;
 
+    protected boolean skipTest(Provider p) {
+        return false;
+    }
+
     private void premain(Provider p) throws Exception {
+        if (skipTest(p)) {
+            return;
+        }
+
         // set a security manager and policy before a test case runs,
         // and disable them after the test case finished
         try {
@@ -327,9 +335,10 @@
     }
 
     static boolean isBadNSSVersion(Provider p) {
-        if (isNSS(p) && badNSSVersion) {
+        double nssVersion = getNSSVersion();
+        if (isNSS(p) && nssVersion >= 3.11 && nssVersion < 3.12) {
             System.out.println("NSS 3.11 has a DER issue that recent " +
-                    "version do not.");
+                    "version do not, skipping");
             return true;
         }
         return false;
@@ -667,9 +676,6 @@
 
     private final static char[] hexDigits = "0123456789abcdef".toCharArray();
 
-    static final boolean badNSSVersion =
-            getNSSVersion() >= 3.11 && getNSSVersion() < 3.12;
-
     private static final String distro = distro();
 
     static final boolean badSolarisSparc =
--- a/test/jdk/sun/security/pkcs11/Secmod/TestNssDbSqlite.java	Wed Jul 25 17:21:04 2018 -0700
+++ b/test/jdk/sun/security/pkcs11/Secmod/TestNssDbSqlite.java	Thu Jul 26 08:46:27 2018 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, Red Hat, Inc. and/or its affiliates.
+ * Copyright (c) 2017, 2018, Red Hat, Inc. and/or its affiliates.
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
@@ -66,7 +66,9 @@
 
     public static void main(String[] args) throws Exception {
 
-        initialize();
+        if (!initialize()) {
+            return;
+        }
 
         if (enableDebug) {
             System.out.println("SunPKCS11 provider: " +
@@ -108,14 +110,15 @@
         }
     }
 
-    private static void initialize() throws Exception {
-        initializeProvider();
+    private static boolean initialize() throws Exception {
+        return initializeProvider();
     }
 
-    private static void initializeProvider () throws Exception {
+    private static boolean initializeProvider() throws Exception {
         useSqlite(true);
         if (!initSecmod()) {
-            return;
+            System.out.println("Cannot init security module database, skipping");
+            return false;
         }
 
         sunPKCS11NSSProvider = getSunPKCS11(BASE + SEP + "nss-sqlite.cfg");
@@ -132,5 +135,7 @@
         gen.generate(2048);
         privateKey = gen.getPrivateKey();
         certificate = gen.getSelfCertificate(new X500Name("CN=Me"), 365);
+
+        return true;
     }
 }
--- a/test/jdk/sun/security/pkcs11/Signature/TestDSAKeyLength.java	Wed Jul 25 17:21:04 2018 -0700
+++ b/test/jdk/sun/security/pkcs11/Signature/TestDSAKeyLength.java	Thu Jul 26 08:46:27 2018 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2018, 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
@@ -47,12 +47,17 @@
     }
 
     @Override
-    public void main(Provider provider) throws Exception {
+    protected boolean skipTest(Provider provider) {
         if (isNSS(provider) && getNSSVersion() >= 3.14) {
             System.out.println("Skip testing NSS " + getNSSVersion());
-            return;
+            return true;
         }
 
+        return false;
+    }
+
+    @Override
+    public void main(Provider provider) throws Exception {
         /*
          * Use Solaris SPARC 11.2 or later to avoid an intermittent failure
          * when running SunPKCS11-Solaris (8044554)
--- a/test/jdk/sun/security/pkcs11/ec/TestCurves.java	Wed Jul 25 17:21:04 2018 -0700
+++ b/test/jdk/sun/security/pkcs11/ec/TestCurves.java	Thu Jul 26 08:46:27 2018 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2018, 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
@@ -51,20 +51,21 @@
     }
 
     @Override
-    public void main(Provider p) throws Exception {
+    protected boolean skipTest(Provider p) {
         if (p.getService("KeyAgreement", "ECDH") == null) {
             System.out.println("Not supported by provider, skipping");
-            return;
+            return true;
         }
 
-        if (isBadNSSVersion(p)) {
-            return;
+        if (isBadNSSVersion(p) || isBadSolarisSparc(p)) {
+            return true;
         }
 
-        if (isBadSolarisSparc(p)) {
-            return;
-        }
+        return false;
+    }
 
+    @Override
+    public void main(Provider p) throws Exception {
         // Check if this is sparc for later failure avoidance.
         boolean sparc = false;
         if (props.getProperty("os.arch").equals("sparcv9")) {
--- a/test/jdk/sun/security/pkcs11/ec/TestECDH.java	Wed Jul 25 17:21:04 2018 -0700
+++ b/test/jdk/sun/security/pkcs11/ec/TestECDH.java	Thu Jul 26 08:46:27 2018 +0800
@@ -91,17 +91,22 @@
     private final static String privBrainpoolP512r1b = "3062020100301406072a8648ce3d020106092b240303020801010d044730450201010440230e18e1bcc88a362fa54e4ea3902009292f7f8033624fd471b5d8ace49d12cfabbc19963dab8e2f1eba00bffb29e4d72d13f2224562f405cb80503666b25429";
     private final static String secretBrainpoolP512r1 = "a7927098655f1f9976fa50a9d566865dc530331846381c87256baf3226244b76d36403c024d7bbf0aa0803eaff405d3d24f11a9b5c0bef679fe1454b21c4cd1f";
 
-    @Override public void main(Provider p) throws Exception {
+    @Override
+    protected boolean skipTest(Provider p) {
         if (p.getService("KeyAgreement", "ECDH") == null) {
             System.out.println("Provider does not support ECDH, skipping");
-            return;
+            return true;
         }
 
         if (isNSS(p) && getNSSECC() == ECCState.Basic) {
-            System.out.println("NSS only supports Basic ECC.  Skipping..");
-            return;
+            System.out.println("NSS only supports Basic ECC, skipping");
+            return true;
         }
 
+        return false;
+    }
+
+    @Override public void main(Provider p) throws Exception {
         /*
          * PKCS11Test.main will remove this provider if needed
          */
--- a/test/jdk/sun/security/pkcs11/ec/TestECDH2.java	Wed Jul 25 17:21:04 2018 -0700
+++ b/test/jdk/sun/security/pkcs11/ec/TestECDH2.java	Thu Jul 26 08:46:27 2018 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2018, 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
@@ -111,16 +111,21 @@
     }
 
     @Override
-    public void main(Provider provider) throws Exception {
+    protected boolean skipTest(Provider provider) {
         if (provider.getService("KeyAgreement", "ECDH") == null) {
             System.out.println("ECDH not supported, skipping");
-            return;
+            return true;
         }
 
         if (isBadNSSVersion(provider)) {
-            return;
+            return true;
         }
 
+        return false;
+    }
+
+    @Override
+    public void main(Provider provider) throws Exception {
         kf = KeyFactory.getInstance("EC", provider);
         kpg = KeyPairGenerator.getInstance("EC", provider);
 
--- a/test/jdk/sun/security/pkcs11/ec/TestECDSA.java	Wed Jul 25 17:21:04 2018 -0700
+++ b/test/jdk/sun/security/pkcs11/ec/TestECDSA.java	Thu Jul 26 08:46:27 2018 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2018, 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
@@ -120,21 +120,22 @@
     }
 
     @Override
-    public void main(Provider provider) throws Exception {
-        long start = System.currentTimeMillis();
-
+    protected boolean skipTest(Provider provider) {
         if (provider.getService("Signature", "SHA1withECDSA") == null) {
             System.out.println("ECDSA not supported, skipping");
-            return;
+            return true;
         }
 
-        if (isBadNSSVersion(provider)) {
-            return;
+        if (isBadNSSVersion(provider) || isBadSolarisSparc(provider)) {
+            return true;
         }
 
-        if (isBadSolarisSparc(provider)) {
-            return;
-        }
+        return false;
+    }
+
+    @Override
+    public void main(Provider provider) throws Exception {
+        long start = System.currentTimeMillis();
 
         /*
          * PKCS11Test.main will remove this provider if needed
--- a/test/jdk/sun/security/pkcs11/ec/TestECDSA2.java	Wed Jul 25 17:21:04 2018 -0700
+++ b/test/jdk/sun/security/pkcs11/ec/TestECDSA2.java	Thu Jul 26 08:46:27 2018 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2018, 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
@@ -102,6 +102,26 @@
     }
 
     @Override
+    protected boolean skipTest(Provider provider) {
+        boolean testP256 =
+                provider.getService("Signature", "SHA256withECDSA") != null;
+
+        boolean testP384 =
+                provider.getService("Signature", "SHA384withECDSA") != null;
+
+        if (!testP256 && !testP384) {
+            System.out.println("ECDSA not supported, skipping");
+            return true;
+        }
+
+        if (isBadNSSVersion(provider)) {
+            return true;
+        }
+
+        return false;
+    }
+
+    @Override
     public void main(Provider provider) throws Exception {
         boolean testP256 =
             (provider.getService("Signature", "SHA256withECDSA") != null);
@@ -109,15 +129,6 @@
         boolean testP384 =
             (provider.getService("Signature", "SHA384withECDSA") != null);
 
-        if (!testP256 && !testP384) {
-            System.out.println("ECDSA not supported, skipping");
-            return;
-        }
-
-        if (isBadNSSVersion(provider)) {
-            return;
-        }
-
         kf = KeyFactory.getInstance("EC", provider);
 
         long start = System.currentTimeMillis();
--- a/test/jdk/sun/security/pkcs11/ec/TestECGenSpec.java	Wed Jul 25 17:21:04 2018 -0700
+++ b/test/jdk/sun/security/pkcs11/ec/TestECGenSpec.java	Thu Jul 26 08:46:27 2018 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2018, 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
@@ -47,16 +47,21 @@
     }
 
     @Override
-    public void main(Provider p) throws Exception {
+    protected boolean skipTest(Provider p) {
         if (p.getService("Signature", "SHA1withECDSA") == null) {
             System.out.println("Provider does not support ECDSA, skipping...");
-            return;
+            return true;
         }
 
         if (isBadNSSVersion(p)) {
-            return;
+            return true;
         }
 
+        return false;
+    }
+
+    @Override
+    public void main(Provider p) throws Exception {
         String[] names = { "secp256r1", "NIST P-192", "sect163k1", "1.3.132.0.26",
             "X9.62 c2tnb239v1"};
         int curves = 1;