1 package test.base.jdbs.cryptography.asymmetric;
2
3 import java.io.File;
4 import java.security.KeyStore;
5 import java.security.KeyStoreException;
6 import java.util.Enumeration;
7
8 import org.apache.log4j.Logger;
9 import org.apache.log4j.xml.DOMConfigurator;
10
11 import test.base.jdbs.AllTests;
12 import base.jdbs.cryptography.asymmetric.AsymmetricKeyRing;
13 import base.jdbs.cryptography.asymmetric.KeyPair;
14 import base.jdbs.cryptography.asymmetric.RSAKeyPairFactory;
15 import junit.framework.TestCase;
16
17 public class AsymmetricKeyRingTest extends TestCase {
18
19 private static final transient Logger logger = Logger.getLogger(AsymmetricKeyRingTest.class.getName());
20
21
22 private static final boolean RUN_SILENTLY = true;
23 private static final String KEY_STORE_FILE_NAME = "target/test/keyStore.ks";
24 private static final String KEY_STORE_PASSWORD = "password";
25
26 private AsymmetricKeyRing asymmetricKeyRing;
27 private KeyPair keyPair;
28
29
30
31
32 @Override
33 protected void setUp() throws Exception {
34 DOMConfigurator.configure("log4jConfiguration.xml");
35 asymmetricKeyRing = new AsymmetricKeyRing(AllTests.getUser());
36 keyPair = RSAKeyPairFactory.newKeyPair(asymmetricKeyRing,"Test Key Pair");
37 asymmetricKeyRing.addKeyPair(keyPair);
38 File keyStoreFile = new File(KEY_STORE_FILE_NAME);
39
40 keyStoreFile.getParentFile().mkdirs();
41 }
42
43
44
45
46 public void testStore() {
47 if(RUN_SILENTLY){
48 File outputFile = new File(KEY_STORE_FILE_NAME);
49
50
51 asymmetricKeyRing.store(outputFile,KEY_STORE_PASSWORD);
52 assertTrue(outputFile.exists());
53 assertTrue(outputFile.length() > 0);
54 }
55 else {
56 File outputFile = null;
57
58 javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
59 chooser.setDialogTitle("Select the file where the the keyring has to be stored...");
60 chooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
61 chooser.setApproveButtonText("Select");
62 if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION){
63 outputFile = new File(chooser.getSelectedFile().getAbsolutePath());
64 }
65 if(outputFile != null){
66 asymmetricKeyRing.store(outputFile,KEY_STORE_PASSWORD);
67 assertTrue(outputFile.exists());
68 assertTrue(outputFile.length() > 0);
69 }
70 }
71 }
72
73
74
75
76 public void testRetrieve() throws KeyStoreException {
77 if(RUN_SILENTLY){
78 File inputFile = new File(KEY_STORE_FILE_NAME);
79
80 KeyStore keyStore = asymmetricKeyRing.retrieve(inputFile,KEY_STORE_PASSWORD);
81 assertTrue(keyStore!= null);
82 }
83 else{
84 File inputFile = null;
85
86 javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
87 chooser.setDialogTitle("Select the keyring file to be loaded...");
88 chooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
89 chooser.setApproveButtonText("Select");
90 if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION){
91 inputFile = new File(chooser.getSelectedFile().getAbsolutePath());
92 }
93 if(inputFile != null){
94 KeyStore keyStore = asymmetricKeyRing.retrieve(inputFile,KEY_STORE_PASSWORD);
95 assertTrue(keyStore!= null);
96
97 Enumeration aliasEnumeration = keyStore.aliases();
98 while(aliasEnumeration.hasMoreElements()){
99 logger.debug(aliasEnumeration.nextElement());
100 }
101
102 }
103 }
104 }
105
106 }