/* * Copyright 2003 Federal Chancellery Austria * MOA-ID has been developed in a cooperation between BRZ, the Federal * Chancellery Austria - ICT staff unit, and Graz University of Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. */ package test.at.gv.egovernment.moa.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigInteger; import java.security.KeyStore; import java.security.Security; import java.security.cert.X509Certificate; import java.util.Enumeration; import at.gv.egiz.eaaf.core.impl.utils.KeyStoreUtils; import junit.framework.TestCase; /** * @author Paul Ivancsics * @version $Id$ */ public class KeyStoreUtilsTest extends TestCase { private String tmpDir = "tmp/KeyStoreUtilsTest"; private String tmpDirURL = "file:" + tmpDir; public KeyStoreUtilsTest(String arg0) { super(arg0); } protected void setUp() throws Exception { //Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); new File(tmpDir).mkdirs(); } protected void tearDown() throws Exception { new File(tmpDir).delete(); } public void testCreateKeyStoreJKS() throws Exception { String[] certFilenames = new String[] { "data/test/security/server-certs/baltimore.cer" }; KeyStore ks = KeyStoreUtils.createKeyStore("jks", certFilenames); assertEquals(1, ks.size()); X509Certificate cert = (X509Certificate)ks.getCertificate("0"); assertEquals(3424, cert.getSerialNumber().intValue()); } public void testCreateKeyStorePKCS12() throws Exception { String[] certFilenames = new String[] { "data/test/security/server-certs/baltimore.cer" }; KeyStore ks = KeyStoreUtils.createKeyStore("pkcs12", certFilenames); assertEquals(1, ks.size()); X509Certificate cert = (X509Certificate)ks.getCertificate("0"); assertEquals(3424, cert.getSerialNumber().intValue()); } // public void testCreateKeyStoreFromCertificateDirectory() throws Exception { // // copy certificate files to a temporary directory, // // omitting the "CVS" directory in the source directory // copyCertificates("data/test/security/server-certs", tmpDir); // KeyStore ks = KeyStoreUtils.createKeyStoreFromCertificateDirectory("jks", tmpDirURL); // assertEquals(2, ks.size()); // X509Certificate cert0 = (X509Certificate)ks.getCertificate("0"); // X509Certificate cert1 = (X509Certificate)ks.getCertificate("1"); // assertTrue(3424 == cert0.getSerialNumber().intValue() || 3424 == cert1.getSerialNumber().intValue()); // } private void copyCertificates(String from, String to) throws IOException { String[] fromList = new File(from).list(); for (int i = 0; i < fromList.length; i++) { File fromFile = new File(from + File.separator + fromList[i]); if (fromFile.isFile()) { String toFile = to + "/" + fromList[i]; FileInputStream in = new FileInputStream(fromFile); FileOutputStream out = new FileOutputStream(toFile); for (int ch = in.read(); ch >= 0; ch = in.read()) out.write(ch); out.close(); in.close(); } } } public void testLoadKeyStore() throws Exception { String keyStoreURL = "file:data/test/security/client-certs/sicher-demo(buergerkarte).p12"; KeyStore ks = KeyStoreUtils.loadKeyStore("pkcs12", keyStoreURL, "buergerkarte"); assertEquals(1, ks.size()); Enumeration aliases = ks.aliases(); String alias = (String)aliases.nextElement(); X509Certificate cert = (X509Certificate)ks.getCertificate(alias); assertEquals(new BigInteger("1044289238331").intValue(), cert.getSerialNumber().intValue()); } }