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