1/*
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.
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 * @test
25 * @bug 6535697
26 * @summary keytool can be more flexible on format of PEM-encoded
27 *  X.509 certificates
28 */
29
30import java.io.*;
31import java.nio.file.Files;
32import java.nio.file.Paths;
33import java.util.Arrays;
34import java.security.cert.CertificateFactory;
35
36public class OpenSSLCert {
37    static final String OUTFILE = "6535697.test";
38
39    public static void main(String[] args) throws Exception {
40        test("open");
41        test("pem");
42        test("open", "open");
43        test("open", "pem");
44        test("pem", "pem");
45        test("pem", "open");
46        test("open", "pem", "open");
47        test("pem", "open", "pem");
48    }
49
50    static void test(String... files) throws Exception {
51        try (FileOutputStream fout = new FileOutputStream(OUTFILE)) {
52            String here = System.getProperty("test.src", "");
53            for (String file: files) {
54                Files.copy(Paths.get(here, file), fout);
55            }
56        }
57        try (FileInputStream fin = new FileInputStream(OUTFILE)) {
58            System.out.println("Testing " + Arrays.toString(files) + "...");
59            if (CertificateFactory.getInstance("X509")
60                    .generateCertificates(fin)
61                    .size() != files.length) {
62                throw new Exception("Not same number");
63            }
64        }
65        Files.delete(Paths.get(OUTFILE));
66    }
67}
68