Path: blob/master/test/jdk/sun/security/pkcs12/ProbeLargeKeystore.java
41149 views
/*1* Copyright (c) 2017, 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 818197826* @summary Test automatic keystore type detection for a large PKCS12 keystore27*/2829import java.io.*;30import java.security.*;31import java.security.cert.*;32import java.security.cert.Certificate;3334public class ProbeLargeKeystore {3536private static final String DIR = System.getProperty("test.src", ".");37private static final String CERT = DIR + "/trusted.pem";38private static final String ALIAS = "test-entry-";39private static final int COUNT = 100;40private static final String KEYSTORE = "test-keystore.p12";41private static final char[] PASSWORD = "passphrase".toCharArray();4243public static final void main(String[] args) throws Exception {4445// Create a large PKCS12 keystore4647new File(KEYSTORE).delete();48KeyStore keystore = KeyStore.getInstance("PKCS12");49keystore.load(null, null);50Certificate cert = loadCertificate(CERT);5152for (int i = 0; i < COUNT; i++) {53keystore.setCertificateEntry(ALIAS + i, cert);54}5556try (FileOutputStream out = new FileOutputStream(KEYSTORE)) {57keystore.store(out, PASSWORD);58}5960// Test the automatic keystore type detection mechanism for PKCS126162KeyStore largeKeystore =63KeyStore.getInstance(new File(KEYSTORE), PASSWORD);6465if (largeKeystore.size() != COUNT) {66throw new Exception("Error detecting a large PKCS12 keystore");67}6869new File(KEYSTORE).delete();70System.out.println("OK");71}7273private static final Certificate loadCertificate(String certFile)74throws Exception {75try (FileInputStream certStream = new FileInputStream(certFile)) {76CertificateFactory factory =77CertificateFactory.getInstance("X.509");78return factory.generateCertificate(certStream);79}80}81}828384