KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > PublicKeyStoreTest


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security;
21
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.assertEquals;
24
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29
30 import com.sslexplorer.boot.Util;
31 import com.sslexplorer.testcontainer.AbstractTest;
32
33 /**
34  */

35 public class PublicKeyStoreTest extends AbstractTest {
36     
37     static PublicKeyStore publicKeyStore;
38     
39     final static String JavaDoc USERNAME = "test1";
40     final static String JavaDoc TEXT_TO_ENCRYPT = "{}-_=a:,123whydocatshavealltheflaps098!\"$5";
41     final static String JavaDoc PASSPHRASE = "asecret";
42     final static String JavaDoc NEW_PASSPHRASE = "aNotherSecret";
43     
44
45     @BeforeClass
46     public static void oneTimeSetUp() throws Exception JavaDoc {
47         setUp("");
48         publicKeyStore = PublicKeyStore.getInstance();
49     }
50
51     @Before
52     @After
53     public void intialize() throws Exception JavaDoc {
54         publicKeyStore.removeKeys(USERNAME);
55     }
56
57     @Test
58     public void newKey() throws Exception JavaDoc {
59         publicKeyStore.verifyPrivateKey(USERNAME, PASSPHRASE.toCharArray());
60         assertTrue("Key creation", publicKeyStore.hasLoadedKey(USERNAME));
61     }
62
63     @Test
64     public void encryptStuff() throws Exception JavaDoc {
65         publicKeyStore.verifyPrivateKey(USERNAME, PASSPHRASE.toCharArray());
66         assertTrue("Key creation", publicKeyStore.hasLoadedKey(USERNAME));
67         String JavaDoc encrypted = publicKeyStore.encryptText(TEXT_TO_ENCRYPT, USERNAME);
68         String JavaDoc decrypted = publicKeyStore.decryptText(encrypted, USERNAME);
69         assertEquals("Encrypt and decrypted", TEXT_TO_ENCRYPT, decrypted);
70     }
71
72     @Test
73     public void changePassphrase() throws Exception JavaDoc {
74         publicKeyStore.verifyPrivateKey(USERNAME, PASSPHRASE.toCharArray());
75         assertTrue("Key creation", publicKeyStore.hasLoadedKey(USERNAME));
76         publicKeyStore.changePrivateKeyPassphrase(USERNAME, PASSPHRASE, NEW_PASSPHRASE);
77         String JavaDoc encrypted = publicKeyStore.encryptText(TEXT_TO_ENCRYPT, USERNAME);
78         String JavaDoc decrypted = publicKeyStore.decryptText(encrypted, USERNAME);
79         assertEquals("Encrypt and decrypted", TEXT_TO_ENCRYPT, decrypted);
80     }
81
82     @Test
83     public void passwordChangeRequired() throws Exception JavaDoc {
84         publicKeyStore.verifyPrivateKey(USERNAME, PASSPHRASE.toCharArray());
85         assertTrue("Key creation", publicKeyStore.hasLoadedKey(USERNAME));
86         publicKeyStore.changePrivateKeyPassphrase(USERNAME, PASSPHRASE, NEW_PASSPHRASE);
87         publicKeyStore.removeCachedKeys(USERNAME);
88         // Try and use the old password
89
try {
90             publicKeyStore.verifyPrivateKey(USERNAME, PASSPHRASE.toCharArray());
91             assertTrue("Verification should have thrown a UpdatePrivateKeyPassphraseException but didn't", false);
92         }
93         catch(UpdatePrivateKeyPassphraseException upkp) {
94             // All ok
95
publicKeyStore.verifyPrivateKey(USERNAME, NEW_PASSPHRASE.toCharArray());
96             String JavaDoc encrypted = publicKeyStore.encryptText(TEXT_TO_ENCRYPT, USERNAME);
97             String JavaDoc decrypted = publicKeyStore.decryptText(encrypted, USERNAME);
98             assertEquals("Encrypt and decrypted", TEXT_TO_ENCRYPT, decrypted);
99         }
100     }
101
102     @Test
103     public void passwordPrompt() throws Exception JavaDoc {
104         publicKeyStore.verifyPrivateKey(USERNAME, PASSPHRASE.toCharArray());
105         assertTrue("Key creation", publicKeyStore.hasLoadedKey(USERNAME));
106         publicKeyStore.changePrivateKeyPassphrase(USERNAME, PASSPHRASE, NEW_PASSPHRASE);
107         publicKeyStore.removeCachedKeys(USERNAME);
108         
109         // Try and prompt for password
110
try {
111             publicKeyStore.verifyPrivateKey(USERNAME, null);
112             assertTrue("Verification should have thrown a PromptForPasswordException but didn't", false);
113         }
114         catch(PromptForPasswordException pfpe) {
115             publicKeyStore.verifyPrivateKey(USERNAME, NEW_PASSPHRASE.toCharArray());
116             // All ok
117
String JavaDoc encrypted = publicKeyStore.encryptText(TEXT_TO_ENCRYPT, USERNAME);
118             String JavaDoc decrypted = publicKeyStore.decryptText(encrypted, USERNAME);
119             assertEquals("Encrypt and decrypted", TEXT_TO_ENCRYPT, decrypted);
120         }
121     }
122
123     @Test
124     public void failEncrypt() throws Exception JavaDoc {
125         try {
126             publicKeyStore.encryptText(TEXT_TO_ENCRYPT, USERNAME);
127             assertTrue("Verification should have thrown a FatalKeyException but didn't", false);
128         }
129         catch(FatalKeyException fke) {
130             // All ok
131
}
132     }
133 }
134
Popular Tags