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.egovernment.moa.util.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()); } }