Path: blob/master/test/jdk/sun/security/tools/keytool/CloseFile.java
41152 views
/*1* Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 648972126* @summary keytool has not closed several file streams27* @author weijun.wang28* @modules java.base/sun.security.tools.keytool29* @compile -XDignore.symbol.file CloseFile.java30* @run main CloseFile31*32* This test is only useful on Windows, which fails before the fix and succeeds33* after it. On other platforms, it always passes.34*/3536import java.io.*;3738public class CloseFile {39public static void main(String[] args) throws Exception {40remove("f0", false);41remove("f1", false);42run("-keystore f0 -storepass changeit -keypass changeit -genkeypair -dname CN=Haha");43run("-keystore f0 -storepass changeit -keypass changeit -certreq -file f2");44remove("f2", true);45run("-keystore f0 -storepass changeit -keypass changeit -exportcert -file f2");46remove("f2", true);47run("-keystore f0 -storepass changeit -keypass changeit -exportcert -file f2");48run("-keystore f0 -storepass changeit -keypass changeit -delete -alias mykey");49run("-keystore f0 -storepass changeit -keypass changeit -importcert -file f2 -noprompt");50remove("f2", true);51run("-keystore f0 -storepass changeit -keypass changeit -exportcert -file f2");52run("-printcert -file f2");53remove("f2", true);54remove("f0", true);55run("-keystore f0 -storepass changeit -keypass changeit -genkeypair -dname CN=Haha");56run("-importkeystore -srckeystore f0 -destkeystore f1 -srcstorepass changeit -deststorepass changeit");57remove("f0", true);58remove("f1", true);59}6061static void run(String s) throws Exception {62sun.security.tools.keytool.Main.main((s+" -debug -keyalg rsa").split(" "));63}64static void remove(String filename, boolean check) {65new File(filename).delete();66if (check && new File(filename).exists()) {67throw new RuntimeException("Error deleting " + filename);68}69}70}717273