From 369f95e520bd944cbb78fc208f9a3fe4aafeb9bd Mon Sep 17 00:00:00 2001 From: weijun Date: Tue, 1 Mar 2011 16:22:22 +0800 Subject: [PATCH] 7020531: test: java/security/cert/CertificateFactory/openssl/OpenSSLCert.java file not closed after run Reviewed-by: alanb, smarks --- test/ProblemList.txt | 3 -- .../openssl/OpenSSLCert.java | 32 +++++++++---------- test/sun/security/tools/keytool/NewSize7.java | 10 ++++-- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/test/ProblemList.txt b/test/ProblemList.txt index 40fc9f9a9..62c19030b 100644 --- a/test/ProblemList.txt +++ b/test/ProblemList.txt @@ -625,9 +625,6 @@ sun/security/tools/keytool/emptysubject.sh generic-all # Timeout on solaris-sparcv9 or exception thrown com/sun/crypto/provider/Cipher/RSA/TestOAEP_KAT.java solaris-all -# File 6535697.test input stream left open? windows samevm -java/security/cert/CertificateFactory/openssl/OpenSSLCert.java generic-all - # Leaving file open: SerialVersion.current, windows samevm java/security/BasicPermission/SerialVersion.java generic-all diff --git a/test/java/security/cert/CertificateFactory/openssl/OpenSSLCert.java b/test/java/security/cert/CertificateFactory/openssl/OpenSSLCert.java index 92fbef6b9..1f19292da 100644 --- a/test/java/security/cert/CertificateFactory/openssl/OpenSSLCert.java +++ b/test/java/security/cert/CertificateFactory/openssl/OpenSSLCert.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2011, 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 @@ -28,6 +28,8 @@ */ import java.io.*; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Arrays; import java.security.cert.CertificateFactory; @@ -46,24 +48,20 @@ public class OpenSSLCert { } static void test(String... files) throws Exception { - FileOutputStream fout = new FileOutputStream(OUTFILE); - for (String file: files) { - FileInputStream fin = new FileInputStream( - new File(System.getProperty("test.src", "."), file)); - byte[] buffer = new byte[4096]; - while (true) { - int len = fin.read(buffer); - if (len < 0) break; - fout.write(buffer, 0, len); + try (FileOutputStream fout = new FileOutputStream(OUTFILE)) { + String here = System.getProperty("test.src", ""); + for (String file: files) { + Files.copy(Paths.get(here, file), fout); } - fin.close(); } - fout.close(); - System.out.println("Testing " + Arrays.toString(files) + "..."); - if (CertificateFactory.getInstance("X509") - .generateCertificates(new FileInputStream(OUTFILE)) - .size() != files.length) { - throw new Exception("Not same number"); + try (FileInputStream fin = new FileInputStream(OUTFILE)) { + System.out.println("Testing " + Arrays.toString(files) + "..."); + if (CertificateFactory.getInstance("X509") + .generateCertificates(fin) + .size() != files.length) { + throw new Exception("Not same number"); + } } + Files.delete(Paths.get(OUTFILE)); } } diff --git a/test/sun/security/tools/keytool/NewSize7.java b/test/sun/security/tools/keytool/NewSize7.java index d9ff1f554..ab8ef5cb5 100644 --- a/test/sun/security/tools/keytool/NewSize7.java +++ b/test/sun/security/tools/keytool/NewSize7.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2011, 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 @@ -29,6 +29,8 @@ import java.io.File; import java.io.FileInputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.security.KeyStore; import java.security.cert.X509Certificate; import java.security.interfaces.RSAPublicKey; @@ -42,8 +44,10 @@ public class NewSize7 { " -alias a -dname cn=c -storepass changeit" + " -keypass changeit -keyalg rsa").split(" ")); KeyStore ks = KeyStore.getInstance("JKS"); - ks.load(new FileInputStream(FILE), null); - new File(FILE).delete(); + try (FileInputStream fin = new FileInputStream(FILE)) { + ks.load(fin, null); + } + Files.delete(Paths.get(FILE)); RSAPublicKey r = (RSAPublicKey)ks.getCertificate("a").getPublicKey(); if (r.getModulus().bitLength() != 2048) { throw new Exception("Bad keysize"); -- GitLab