changeset 8793:c92db09aed60

8006994: Cleanup PKCS12 tests to ensure streams get closed Reviewed-by: mullan
author vinnie
date Tue, 21 Nov 2017 07:37:02 +0000
parents b968a7cf967f
children ce5ccb4e7938
files test/sun/security/pkcs12/StorePasswordTest.java test/sun/security/pkcs12/StoreSecretKeyTest.java test/sun/security/pkcs12/StoreTrustedCertTest.java
diffstat 3 files changed, 88 insertions(+), 92 deletions(-) [+]
line wrap: on
line diff
--- a/test/sun/security/pkcs12/StorePasswordTest.java	Tue Nov 21 07:17:36 2017 +0000
+++ b/test/sun/security/pkcs12/StorePasswordTest.java	Tue Nov 21 07:37:02 2017 +0000
@@ -47,40 +47,40 @@
 
         new File(KEYSTORE).delete();
 
-        try {
+        KeyStore keystore = KeyStore.getInstance("PKCS12");
+        keystore.load(null, null);
 
-            KeyStore keystore = KeyStore.getInstance("PKCS12");
-            keystore.load(null, null);
+        // Set entry
+        keystore.setEntry(ALIAS,
+            new KeyStore.SecretKeyEntry(convertPassword(USER_PASSWORD)),
+                new KeyStore.PasswordProtection(PASSWORD));
 
-            // Set entry
-            keystore.setEntry(ALIAS,
-                new KeyStore.SecretKeyEntry(convertPassword(USER_PASSWORD)),
-                    new KeyStore.PasswordProtection(PASSWORD));
+        try (FileOutputStream outStream = new FileOutputStream(KEYSTORE)) {
+            System.out.println("Storing keystore to: " + KEYSTORE);
+            keystore.store(outStream, PASSWORD);
+        }
 
-            System.out.println("Storing keystore to: " + KEYSTORE);
-            keystore.store(new FileOutputStream(KEYSTORE), PASSWORD);
-
+        try (FileInputStream inStream = new FileInputStream(KEYSTORE)) {
             System.out.println("Loading keystore from: " + KEYSTORE);
-            keystore.load(new FileInputStream(KEYSTORE), PASSWORD);
+            keystore.load(inStream, PASSWORD);
             System.out.println("Loaded keystore with " + keystore.size() +
                 " entries");
-            KeyStore.Entry entry = keystore.getEntry(ALIAS,
-                new KeyStore.PasswordProtection(PASSWORD));
-            System.out.println("Retrieved entry: " + entry);
+        }
+
+        KeyStore.Entry entry = keystore.getEntry(ALIAS,
+            new KeyStore.PasswordProtection(PASSWORD));
+        System.out.println("Retrieved entry: " + entry);
 
-            SecretKey key = (SecretKey) keystore.getKey(ALIAS, PASSWORD);
-            SecretKeyFactory factory =
-                SecretKeyFactory.getInstance(key.getAlgorithm());
-            PBEKeySpec keySpec =
-                (PBEKeySpec) factory.getKeySpec(key, PBEKeySpec.class);
-            char[] pwd = keySpec.getPassword();
-            System.out.println("Recovered credential: " + new String(pwd));
+        SecretKey key = (SecretKey) keystore.getKey(ALIAS, PASSWORD);
+        SecretKeyFactory factory =
+            SecretKeyFactory.getInstance(key.getAlgorithm());
+        PBEKeySpec keySpec =
+            (PBEKeySpec) factory.getKeySpec(key, PBEKeySpec.class);
+        char[] pwd = keySpec.getPassword();
+        System.out.println("Recovered credential: " + new String(pwd));
 
-            if (!Arrays.equals(USER_PASSWORD.toCharArray(), pwd)) {
-                throw new Exception("Failed to recover the stored password");
-            }
-        } finally {
-            new File(KEYSTORE).delete();
+        if (!Arrays.equals(USER_PASSWORD.toCharArray(), pwd)) {
+            throw new Exception("Failed to recover the stored password");
         }
     }
 
--- a/test/sun/security/pkcs12/StoreSecretKeyTest.java	Tue Nov 21 07:17:36 2017 +0000
+++ b/test/sun/security/pkcs12/StoreSecretKeyTest.java	Tue Nov 21 07:37:02 2017 +0000
@@ -45,35 +45,34 @@
 
         new File(KEYSTORE).delete();
 
-        try {
+        KeyStore keystore = KeyStore.getInstance("PKCS12");
+        keystore.load(null, null);
 
-            KeyStore keystore = KeyStore.getInstance("PKCS12");
-            keystore.load(null, null);
+        // Set entry
+        keystore.setEntry(ALIAS,
+            new KeyStore.SecretKeyEntry(generateSecretKey("AES", 128)),
+                new KeyStore.PasswordProtection(PASSWORD));
 
-            // Set entry
-            keystore.setEntry(ALIAS,
-                new KeyStore.SecretKeyEntry(generateSecretKey("AES", 128)),
-                    new KeyStore.PasswordProtection(PASSWORD));
+        try (FileOutputStream outStream = new FileOutputStream(KEYSTORE)) {
+            System.out.println("Storing keystore to: " + KEYSTORE);
+            keystore.store(outStream, PASSWORD);
+        }
 
-            System.out.println("Storing keystore to: " + KEYSTORE);
-            keystore.store(new FileOutputStream(KEYSTORE), PASSWORD);
-
+        try (FileInputStream inStream = new FileInputStream(KEYSTORE)) {
             System.out.println("Loading keystore from: " + KEYSTORE);
-            keystore.load(new FileInputStream(KEYSTORE), PASSWORD);
+            keystore.load(inStream, PASSWORD);
             System.out.println("Loaded keystore with " + keystore.size() +
                 " entries");
-            KeyStore.Entry entry = keystore.getEntry(ALIAS,
-                new KeyStore.PasswordProtection(PASSWORD));
-            System.out.println("Retrieved entry: " + entry);
+        }
 
-            if (entry instanceof KeyStore.SecretKeyEntry) {
-                System.out.println("Retrieved secret key entry: " +
-                    entry);
-            } else {
-                throw new Exception("Not a secret key entry");
-            }
-        } finally {
-            new File(KEYSTORE).delete();
+        KeyStore.Entry entry = keystore.getEntry(ALIAS,
+            new KeyStore.PasswordProtection(PASSWORD));
+        System.out.println("Retrieved entry: " + entry);
+
+        if (entry instanceof KeyStore.SecretKeyEntry) {
+            System.out.println("Retrieved secret key entry: " + entry);
+        } else {
+            throw new Exception("Not a secret key entry");
         }
     }
 
--- a/test/sun/security/pkcs12/StoreTrustedCertTest.java	Tue Nov 21 07:17:36 2017 +0000
+++ b/test/sun/security/pkcs12/StoreTrustedCertTest.java	Tue Nov 21 07:37:02 2017 +0000
@@ -54,62 +54,59 @@
 
         new File(KEYSTORE).delete();
 
-        try {
-            KeyStore keystore = KeyStore.getInstance("PKCS12");
-            keystore.load(null, null);
+        KeyStore keystore = KeyStore.getInstance("PKCS12");
+        keystore.load(null, null);
 
-            Certificate cert = loadCertificate(CERT);
-            Set<PKCS12Attribute> attributes = new HashSet<>();
-            attributes.add(new PKCS12Attribute("1.3.5.7.9", "that's odd"));
-            attributes.add(new PKCS12Attribute("2.4.6.8.10", "that's even"));
+        Certificate cert = loadCertificate(CERT);
+        Set<PKCS12Attribute> attributes = new HashSet<>();
+        attributes.add(new PKCS12Attribute("1.3.5.7.9", "that's odd"));
+        attributes.add(new PKCS12Attribute("2.4.6.8.10", "that's even"));
 
-            // Set trusted certificate entry
-            keystore.setEntry(ALIAS,
-                new KeyStore.TrustedCertificateEntry(cert), null);
+        // Set trusted certificate entry
+        keystore.setEntry(ALIAS,
+            new KeyStore.TrustedCertificateEntry(cert), null);
 
-            // Set trusted certificate entry with attributes
-            JavaSecurityKeyStoreAccess jsksa =
-                SharedSecrets.getJavaSecurityKeyStoreAccess();
-            keystore.setEntry(ALIAS2,
-                jsksa.constructTrustedCertificateEntry(cert, attributes),
-                null);
+        // Set trusted certificate entry with attributes
+        JavaSecurityKeyStoreAccess jsksa =
+            SharedSecrets.getJavaSecurityKeyStoreAccess();
+        keystore.setEntry(ALIAS2,
+                          jsksa.constructTrustedCertificateEntry(cert, attributes),
+                          null);
+        try (FileOutputStream outStream = new FileOutputStream(KEYSTORE)) {
+            System.out.println("Storing keystore to: " + KEYSTORE);
+            keystore.store(outStream, PASSWORD);
+        }
 
-            System.out.println("Storing keystore to: " + KEYSTORE);
-            keystore.store(new FileOutputStream(KEYSTORE), PASSWORD);
-
+        try (FileInputStream inStream = new FileInputStream(KEYSTORE)) {
             System.out.println("Loading keystore from: " + KEYSTORE);
-            keystore.load(new FileInputStream(KEYSTORE), PASSWORD);
+            keystore.load(inStream, PASSWORD);
             System.out.println("Loaded keystore with " + keystore.size() +
                 " entries");
+        }
 
-            KeyStore.Entry entry = keystore.getEntry(ALIAS, null);
-            if (entry instanceof KeyStore.TrustedCertificateEntry) {
-                System.out.println("Retrieved trusted certificate entry: " +
-                    entry);
-            } else {
-                throw new Exception("Not a trusted certificate entry");
-            }
-            System.out.println();
+        KeyStore.Entry entry = keystore.getEntry(ALIAS, null);
+        if (entry instanceof KeyStore.TrustedCertificateEntry) {
+            System.out.println("Retrieved trusted certificate entry: " + entry);
+        } else {
+            throw new Exception("Not a trusted certificate entry");
+        }
+        System.out.println();
 
-            entry = keystore.getEntry(ALIAS2, null);
-            if (entry instanceof KeyStore.TrustedCertificateEntry) {
-                KeyStore.TrustedCertificateEntry trustedEntry =
-                    (KeyStore.TrustedCertificateEntry) entry;
-                Set<PKCS12Attribute> entryAttributes =
-                    jsksa.getTrustedCertificateEntryAttributes(trustedEntry);
+        entry = keystore.getEntry(ALIAS2, null);
+        if (entry instanceof KeyStore.TrustedCertificateEntry) {
+            KeyStore.TrustedCertificateEntry trustedEntry =
+                (KeyStore.TrustedCertificateEntry) entry;
+            Set<PKCS12Attribute> entryAttributes =
+                jsksa.getTrustedCertificateEntryAttributes(trustedEntry);
 
-                if (entryAttributes.containsAll(attributes)) {
-                    System.out.println("Retrieved trusted certificate entry " +
-                        "with attributes: " + entry);
-                } else {
-                    throw new Exception("Failed to retrieve entry attributes");
-                }
+            if (entryAttributes.containsAll(attributes)) {
+                System.out.println("Retrieved trusted certificate entry " +
+                    "with attributes: " + entry);
             } else {
-                throw new Exception("Not a trusted certificate entry");
+                throw new Exception("Failed to retrieve entry attributes");
             }
-
-        } finally {
-            new File(KEYSTORE).delete();
+        } else {
+            throw new Exception("Not a trusted certificate entry");
         }
     }