changeset 14823:02ed0906dd82

8205507: jdk/javax/xml/crypto/dsig/GenerationTests.java timed out Reviewed-by: mullan
author weijun
date Tue, 11 Sep 2018 08:48:26 +0800
parents aefa283b6740
children f1405f818b74
files test/javax/xml/crypto/dsig/GenerationTests.java
diffstat 1 files changed, 68 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/test/javax/xml/crypto/dsig/GenerationTests.java	Thu Apr 04 12:29:43 2019 -0700
+++ b/test/javax/xml/crypto/dsig/GenerationTests.java	Tue Sep 11 08:48:26 2018 +0800
@@ -24,7 +24,7 @@
 /**
  * @test
  * @bug 4635230 6283345 6303830 6824440 6867348 7094155 8038184
- * 8038349 8046724 8074784 8079693 8177334 8210736 8217878
+ * 8038349 8046724 8074784 8079693 8177334 8205507 8210736 8217878
  * @summary Basic unit tests for generating XML Signatures with JSR 105
  * @compile -XDignore.symbol.file KeySelectors.java SignatureValidator.java
  *     X509KeySelector.java GenerationTests.java
@@ -268,6 +268,26 @@
         KeyValue, x509data, KeyName
     }
 
+    // cached keys (for performance) used by test_create_detached_signature().
+    private static HashMap<String,Key[]> cachedKeys = new HashMap<>();
+
+    // Load cachedKeys persisted in a file to reproduce a failure.
+    // The keys are always saved to "cached-keys" but you can rename
+    // it to a different file name and load it here. Note: The keys will
+    // always be persisted so renaming is a good idea although the
+    // content might not change.
+    static {
+        String cacheFile = System.getProperty("use.cached.keys");
+        if (cacheFile != null) {
+            try (FileInputStream fis = new FileInputStream(cacheFile);
+                 ObjectInputStream ois = new ObjectInputStream(fis)) {
+                cachedKeys = (HashMap<String,Key[]>) ois.readObject();
+            } catch (Exception e) {
+                throw new AssertionError("Cannot read " + cacheFile, e);
+            }
+        }
+    }
+
     private static boolean result = true;
 
     public static void main(String args[]) throws Exception {
@@ -432,6 +452,12 @@
                     XMLSignatureException.class);
         }
 
+        // persist cached keys to a file.
+        try (FileOutputStream fos = new FileOutputStream("cached-keys", true);
+             ObjectOutputStream oos = new ObjectOutputStream(fos)) {
+            oos.writeObject(cachedKeys);
+        }
+
         if (!result) {
             throw new RuntimeException("At least one test case failed");
         }
@@ -1726,37 +1752,9 @@
 
         SignatureMethod sm = fac.newSignatureMethod(signatureMethod, null);
 
-        Key signingKey;
-        Key validationKey;
-        if (signatureMethod.contains("#hmac-")) {
-            // http://...#hmac-sha1 -> hmac-sha1 -> hmacsha1
-            String algName = signatureMethod
-                    .substring(signatureMethod.indexOf('#') + 1)
-                    .replace("-", "");
-            KeyGenerator kg = KeyGenerator.getInstance(algName);
-            signingKey = kg.generateKey();
-            validationKey = signingKey;
-        } else {
-            KeyPairGenerator kpg;
-            SecureRandom random = new SecureRandom();
-            if (signatureMethod.contains("#rsa-")
-                    || signatureMethod.contains("-rsa-MGF1")) {
-                kpg = KeyPairGenerator.getInstance("RSA");
-                kpg.initialize(signatureMethod.contains("#sha512-rsa-MGF1")
-                        ? 2048 : 1024, random);
-            } else if (signatureMethod.contains("#dsa-")) {
-                kpg = KeyPairGenerator.getInstance("DSA");
-                kpg.initialize(1024, random);
-            } else if (signatureMethod.contains("#ecdsa-")) {
-                kpg = KeyPairGenerator.getInstance("EC");
-                kpg.initialize(256, random);
-            } else {
-                throw new RuntimeException("Unsupported signature algorithm");
-            }
-            KeyPair kp = kpg.generateKeyPair();
-            validationKey = kp.getPublic();
-            signingKey = kp.getPrivate();
-        }
+        Key[] pair = getCachedKeys(signatureMethod);
+        Key signingKey = pair[0];
+        Key validationKey = pair[1];
 
         SignedInfo si = fac.newSignedInfo(cm, sm, refs, null);
 
@@ -1852,6 +1850,44 @@
         setup();
     }
 
+    private static Key[] getCachedKeys(String signatureMethod) {
+        return cachedKeys.computeIfAbsent(signatureMethod, sm -> {
+            try {
+                System.out.print("<create keys for " + sm + ">");
+                System.out.flush();
+                if (sm.contains("#hmac-")) {
+                    // http://...#hmac-sha1 -> hmac-sha1 -> hmacsha1
+                    String algName = sm
+                            .substring(sm.indexOf('#') + 1)
+                            .replace("-", "");
+                    KeyGenerator kg = KeyGenerator.getInstance(algName);
+                    Key signingKey = kg.generateKey();
+                    return new Key[] { signingKey, signingKey};
+                } else {
+                    KeyPairGenerator kpg;
+                    if (sm.contains("#rsa-")
+                            || sm.contains("-rsa-MGF1")) {
+                        kpg = KeyPairGenerator.getInstance("RSA");
+                        kpg.initialize(
+                                sm.contains("#sha512-rsa-MGF1") ? 2048 : 1024);
+                    } else if (sm.contains("#dsa-")) {
+                        kpg = KeyPairGenerator.getInstance("DSA");
+                        kpg.initialize(1024);
+                    } else if (sm.contains("#ecdsa-")) {
+                        kpg = KeyPairGenerator.getInstance("EC");
+                        kpg.initialize(256);
+                    } else {
+                        throw new RuntimeException("Unsupported signature algorithm");
+                    }
+                    KeyPair kp = kpg.generateKeyPair();
+                    return new Key[] { kp.getPrivate(), kp.getPublic()};
+                }
+            } catch (NoSuchAlgorithmException e) {
+                throw new AssertionError("Should not happen", e);
+            }
+        });
+    }
+
     private static final String DSA_Y =
         "070662842167565771936588335128634396171789331656318483584455493822" +
         "400811200853331373030669235424928346190274044631949560438023934623" +