KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > ENCRYPT14KeystoreTest


1 /*
2  * Created on 04-Jul-2004
3  *
4  * To change the template for this generated file go to
5  * Window - Preferences - Java - Code Generation - Code and Comments
6  */

7 package org.jgroups.protocols;
8
9 import junit.framework.TestCase;
10 import org.jgroups.Address;
11 import org.jgroups.Event;
12 import org.jgroups.Message;
13 import org.jgroups.stack.Protocol;
14
15 import javax.crypto.Cipher;
16 import java.io.*;
17 import java.security.MessageDigest JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Properties JavaDoc;
21 /**
22  * @author xenephon
23  *
24  * To change the template for this generated type comment go to
25  * Window - Preferences - Java - Code Generation - Code and Comments
26  */

27 public class ENCRYPT14KeystoreTest extends TestCase {
28
29     
30    
31
32
33     public void testInitWrongKeystoreProperties() {
34         Properties JavaDoc props = new Properties JavaDoc();
35         String JavaDoc defaultKeystore = "unkownKeystore.keystore";
36         props.put("key_store_name",defaultKeystore);
37         ENCRYPT encrypt = new ENCRYPT();
38         encrypt.setProperties(props);
39         try {
40             encrypt.init();
41         } catch (Exception JavaDoc e){
42             System.out.println("didn't find incorrect keystore (as expected): " + e.getMessage());
43             assertEquals("Unable to load keystore " + defaultKeystore + " ensure file is on classpath",e.getMessage());
44         }
45     }
46
47     public void testInitKeystoreProperties() throws Exception JavaDoc {
48         
49         Properties JavaDoc props = new Properties JavaDoc();
50         String JavaDoc defaultKeystore = "defaultStore.keystore";
51         props.put("key_store_name",defaultKeystore);
52
53         //javax.
54
ENCRYPT encrypt = new ENCRYPT();
55         encrypt.setProperties(props);
56         encrypt.init();
57         assertNotNull(encrypt.getSymDecodingCipher());
58         assertNotNull(encrypt.getSymEncodingCipher());
59
60     }
61
62     public void testMessageDownEncode() throws Exception JavaDoc {
63         // initialise the encryption
64
Properties JavaDoc props = new Properties JavaDoc();
65         String JavaDoc defaultKeystore = "defaultStore.keystore";
66         props.put("key_store_name",defaultKeystore);
67
68         //javax.
69
ENCRYPT encrypt = new ENCRYPT();
70         encrypt.setProperties(props);
71         encrypt.init();
72
73         // use a second instance so we know we are not accidentally using internal key
74
Properties JavaDoc props2 = new Properties JavaDoc();
75         props2.put("key_store_name",defaultKeystore);
76 // javax.
77
ENCRYPT encrypt2 = new ENCRYPT();
78         encrypt2.setProperties(props2);
79         encrypt2.init();
80
81         MockObserver observer = new MockObserver();
82         encrypt.setObserver(observer);
83         
84         encrypt.keyServer = true;
85         String JavaDoc messageText = "hello this is a test message";
86         Message msg = new Message(null,null,messageText.getBytes());
87         
88         Event event = new Event(Event.MSG,msg);
89         encrypt.down(event);
90         Message sentMsg = (Message)((Event)observer.getDownMessages().get("message0")).getArg();
91         String JavaDoc encText = new String JavaDoc(sentMsg.getBuffer());
92         assertNotSame(encText,messageText);
93         Cipher cipher = encrypt2.getSymDecodingCipher();
94         byte[] decodedBytes = cipher.doFinal(sentMsg.getBuffer());
95         String JavaDoc temp = new String JavaDoc(decodedBytes);
96         System.out.println("decoded text:" + temp);
97         assertEquals(temp,messageText);
98
99     }
100     
101     
102     public void testMessageUpDecode() throws Exception JavaDoc{
103         // initialise the encryption
104
Properties JavaDoc props = new Properties JavaDoc();
105         String JavaDoc defaultKeystore = "defaultStore.keystore";
106         props.put("key_store_name",defaultKeystore);
107
108         //javax.
109
ENCRYPT encrypt = new ENCRYPT();
110         encrypt.setProperties(props);
111         encrypt.init();
112
113         // use a second instance so we know we are not accidentally using internal key
114
Properties JavaDoc props2 = new Properties JavaDoc();
115         props2.put("key_store_name",defaultKeystore);
116 // javax.
117
ENCRYPT encrypt2 = new ENCRYPT();
118         encrypt2.setProperties(props2);
119         encrypt2.init();
120
121         MockObserver observer = new MockObserver();
122         encrypt.setObserver(observer);
123         
124         encrypt.keyServer = true;
125         String JavaDoc messageText = "hello this is a test message";
126         Cipher cipher = encrypt2.getSymEncodingCipher();
127         byte[] encodedBytes = cipher.doFinal(messageText.getBytes());
128         assertNotSame(new String JavaDoc(encodedBytes),messageText);
129         
130         MessageDigest JavaDoc digest = MessageDigest.getInstance("MD5");
131         digest.reset();
132         digest.update(encrypt.getDesKey().getEncoded());
133          
134         String JavaDoc symVersion = new String JavaDoc(digest.digest(), "UTF-8");
135         
136         Message msg = new Message(null,null,encodedBytes);
137         msg.putHeader(ENCRYPT.EncryptHeader.KEY, new ENCRYPT.EncryptHeader(ENCRYPT.EncryptHeader.ENCRYPT,symVersion));
138         Event event = new Event(Event.MSG,msg);
139         encrypt.up(event);
140         Message rcvdMsg = (Message)((Event)observer.getUpMessages().get("message0")).getArg();
141         String JavaDoc decText = new String JavaDoc(rcvdMsg.getBuffer());
142
143         assertEquals(decText,messageText);
144
145     }
146     
147     public void testMessageUpWrongKey() throws Exception JavaDoc{
148         // initialise the encryption
149
Properties JavaDoc props = new Properties JavaDoc();
150         String JavaDoc defaultKeystore = "defaultStore.keystore";
151         String JavaDoc defaultKeystore2 = "defaultStore2.keystore";
152         props.put("key_store_name",defaultKeystore);
153
154         //javax.
155
ENCRYPT encrypt = new ENCRYPT();
156         encrypt.setProperties(props);
157         encrypt.init();
158         // use a second instance so we know we are not accidentally using internal key
159

160         Properties JavaDoc props2 = new Properties JavaDoc();
161         ENCRYPT encrypt2 = new ENCRYPT();
162         props2.setProperty("key_store_name",defaultKeystore2);
163         
164         encrypt2.setProperties(props2);
165         encrypt2.init();
166
167         MockObserver observer = new MockObserver();
168         encrypt.setObserver(observer);
169         
170         encrypt.keyServer = true;
171         String JavaDoc messageText = "hello this is a test message";
172         Cipher cipher = encrypt2.getSymEncodingCipher();
173         byte[] encodedBytes = cipher.doFinal(messageText.getBytes());
174         assertNotSame(new String JavaDoc(encodedBytes),messageText);
175         
176         MessageDigest JavaDoc digest = MessageDigest.getInstance("MD5");
177         digest.reset();
178         digest.update(encrypt2.getDesKey().getEncoded());
179          
180         String JavaDoc symVersion = new String JavaDoc(digest.digest());
181         
182         Message msg = new Message(null,null,encodedBytes);
183         msg.putHeader(ENCRYPT.EncryptHeader.KEY, new ENCRYPT.EncryptHeader(ENCRYPT.EncryptHeader.ENCRYPT,symVersion));
184         Event event = new Event(Event.MSG,msg);
185         encrypt.up(event);
186         assertEquals(0, observer.getUpMessages().size());
187
188     }
189     
190     public void testMessageUpNoEncryptHeader() throws Exception JavaDoc{
191         // initialise the encryption
192
Properties JavaDoc props = new Properties JavaDoc();
193         String JavaDoc defaultKeystore = "defaultStore.keystore";
194         props.put("key_store_name",defaultKeystore);
195
196         //javax.
197
ENCRYPT encrypt = new ENCRYPT();
198         encrypt.setProperties(props);
199         encrypt.init();
200
201         // use a second instance so we know we are not accidentally using internal key
202
Properties JavaDoc props2 = new Properties JavaDoc();
203         props2.put("key_store_name",defaultKeystore);
204 // javax.
205
ENCRYPT encrypt2 = new ENCRYPT();
206         encrypt2.setProperties(props2);
207         encrypt2.init();
208
209         MockObserver observer = new MockObserver();
210         encrypt.setObserver(observer);
211         
212         encrypt.keyServer = true;
213         String JavaDoc messageText = "hello this is a test message";
214         Cipher cipher = encrypt2.getSymEncodingCipher();
215         byte[] encodedBytes = cipher.doFinal(messageText.getBytes());
216         assertNotSame(new String JavaDoc(encodedBytes),messageText);
217         
218         Message msg = new Message(null,null,encodedBytes);
219         Event event = new Event(Event.MSG,msg);
220         encrypt.up(event);
221         assertEquals(0, observer.getUpMessages().size());
222     
223
224
225     }
226     
227     public void testEventUpNoMessage() throws Exception JavaDoc{
228         // initialise the encryption
229
Properties JavaDoc props = new Properties JavaDoc();
230         String JavaDoc defaultKeystore = "defaultStore.keystore";
231         props.put("key_store_name",defaultKeystore);
232
233         //javax.
234
ENCRYPT encrypt = new ENCRYPT();
235         encrypt.setProperties(props);
236         encrypt.init();
237
238         MockObserver observer = new MockObserver();
239         encrypt.setObserver(observer);
240         encrypt.keyServer = true;
241     
242         Event event = new Event(Event.MSG,null);
243         encrypt.up(event);
244         assertEquals(1, observer.getUpMessages().size());
245     
246
247
248     }
249     
250     public void testMessageUpNoBuffer() throws Exception JavaDoc{
251         // initialise the encryption
252
Properties JavaDoc props = new Properties JavaDoc();
253         String JavaDoc defaultKeystore = "defaultStore.keystore";
254         props.put("key_store_name",defaultKeystore);
255
256         //javax.
257
ENCRYPT encrypt = new ENCRYPT();
258         encrypt.setProperties(props);
259         encrypt.init();
260
261         MockObserver observer = new MockObserver();
262         encrypt.setObserver(observer);
263         encrypt.keyServer = true;
264         Message msg = new Message(null,null,null);
265         Event event = new Event(Event.MSG,msg);
266         encrypt.up(event);
267         assertEquals(1, observer.getUpMessages().size());
268     }
269     
270     static class MockObserver implements ENCRYPT.Observer {
271
272         private Map JavaDoc upMessages = new HashMap JavaDoc();
273         private Map JavaDoc downMessages = new HashMap JavaDoc();
274         private int counter =0;
275         /* (non-Javadoc)
276          * @see org.jgroups.UpHandler#up(org.jgroups.Event)
277          */

278         
279         private void storeUp(Event evt){
280             upMessages.put("message"+counter++,evt);
281         }
282         
283         private void storeDown(Event evt){
284             downMessages.put("message"+counter++,evt);
285         }
286         public void up(Event evt)
287         {
288             storeUp(evt);
289             System.out.println("Up:"+evt.toString());
290         }
291
292         /* (non-Javadoc)
293          * @see org.jgroups.stack.ProtocolObserver#setProtocol(org.jgroups.stack.Protocol)
294          */

295         public void setProtocol(Protocol prot)
296         {
297         }
298
299
300
301         /* (non-Javadoc)
302          * @see org.jgroups.stack.ProtocolObserver#up_prot.up(org.jgroups.Event)
303          */

304         public void passUp(Event evt)
305         {
306             storeUp(evt);
307             System.out.println("PassUp:"+evt.toString());
308         }
309
310         /* (non-Javadoc)
311          * @see org.jgroups.stack.ProtocolObserver#down(org.jgroups.Event, int)
312          */

313         public void down(Event evt)
314         {
315             System.out.println("down:"+evt.toString());
316         }
317
318         /* (non-Javadoc)
319          * @see org.jgroups.stack.ProtocolObserver#down_prot.down(org.jgroups.Event)
320          */

321         public void passDown(Event evt)
322         {
323             storeDown(evt);
324             System.out.println("passdown:"+evt.toString());
325         }
326         
327         /**
328          * @return Returns the upMessages.
329          */

330         protected Map JavaDoc getUpMessages()
331         {
332             return upMessages;
333         }
334         /**
335          * @param upMessages The upMessages to set.
336          */

337         protected void setUpMessages(Map JavaDoc upMessages)
338         {
339             this.upMessages = upMessages;
340         }
341         /**
342          * @return Returns the downMessages.
343          */

344         protected Map JavaDoc getDownMessages()
345         {
346             return downMessages;
347         }
348         /**
349          * @param downMessages The downMessages to set.
350          */

351         protected void setDownMessages(Map JavaDoc downMessages)
352         {
353             this.downMessages = downMessages;
354         }
355     }
356     
357     static class MockAddress implements Address{
358
359         private static final long serialVersionUID = -2044466632514705356L;
360
361         /* (non-Javadoc)
362          * @see org.jgroups.Address#isMulticastAddress()
363          */

364         public boolean isMulticastAddress()
365         {
366             return false;
367         }
368
369         public int size() {
370             return 0;
371         }
372
373         /* (non-Javadoc)
374          * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
375          */

376         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc
377         {
378
379         }
380
381         /* (non-Javadoc)
382          * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
383          */

384         public void writeExternal(ObjectOutput out) throws IOException
385         {
386         }
387
388         public void writeTo(DataOutputStream out) throws IOException {
389             ;
390         }
391
392         public void readFrom(DataInputStream in) throws IOException, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
393             ;
394         }
395
396         /* (non-Javadoc)
397          * @see java.lang.Comparable#compareTo(java.lang.Object)
398          */

399         public int compareTo(Object JavaDoc o)
400         {
401             return -1;
402         }
403
404
405     }
406 }
407
Popular Tags