1 /* |
1 /* |
2 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
26 * @summary keytool can be more flexible on format of PEM-encoded |
26 * @summary keytool can be more flexible on format of PEM-encoded |
27 * X.509 certificates |
27 * X.509 certificates |
28 */ |
28 */ |
29 |
29 |
30 import java.io.*; |
30 import java.io.*; |
|
31 import java.nio.file.Files; |
|
32 import java.nio.file.Paths; |
31 import java.util.Arrays; |
33 import java.util.Arrays; |
32 import java.security.cert.CertificateFactory; |
34 import java.security.cert.CertificateFactory; |
33 |
35 |
34 public class OpenSSLCert { |
36 public class OpenSSLCert { |
35 static final String OUTFILE = "6535697.test"; |
37 static final String OUTFILE = "6535697.test"; |
44 test("open", "pem", "open"); |
46 test("open", "pem", "open"); |
45 test("pem", "open", "pem"); |
47 test("pem", "open", "pem"); |
46 } |
48 } |
47 |
49 |
48 static void test(String... files) throws Exception { |
50 static void test(String... files) throws Exception { |
49 FileOutputStream fout = new FileOutputStream(OUTFILE); |
51 try (FileOutputStream fout = new FileOutputStream(OUTFILE)) { |
50 for (String file: files) { |
52 String here = System.getProperty("test.src", ""); |
51 FileInputStream fin = new FileInputStream( |
53 for (String file: files) { |
52 new File(System.getProperty("test.src", "."), file)); |
54 Files.copy(Paths.get(here, file), fout); |
53 byte[] buffer = new byte[4096]; |
|
54 while (true) { |
|
55 int len = fin.read(buffer); |
|
56 if (len < 0) break; |
|
57 fout.write(buffer, 0, len); |
|
58 } |
55 } |
59 fin.close(); |
|
60 } |
56 } |
61 fout.close(); |
57 try (FileInputStream fin = new FileInputStream(OUTFILE)) { |
62 System.out.println("Testing " + Arrays.toString(files) + "..."); |
58 System.out.println("Testing " + Arrays.toString(files) + "..."); |
63 if (CertificateFactory.getInstance("X509") |
59 if (CertificateFactory.getInstance("X509") |
64 .generateCertificates(new FileInputStream(OUTFILE)) |
60 .generateCertificates(fin) |
65 .size() != files.length) { |
61 .size() != files.length) { |
66 throw new Exception("Not same number"); |
62 throw new Exception("Not same number"); |
|
63 } |
67 } |
64 } |
|
65 Files.delete(Paths.get(OUTFILE)); |
68 } |
66 } |
69 } |
67 } |