001: /*
002: * Created on 04-Jul-2004
003: *
004: * To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package org.jgroups.protocols;
008:
009: import junit.framework.TestCase;
010: import org.jgroups.Address;
011: import org.jgroups.Event;
012: import org.jgroups.Message;
013: import org.jgroups.stack.Protocol;
014: import org.jgroups.stack.ProtocolObserver;
015:
016: import javax.crypto.Cipher;
017: import java.io.*;
018: import java.security.MessageDigest;
019: import java.util.HashMap;
020: import java.util.Map;
021: import java.util.Properties;
022:
023: /**
024: * @author xenephon
025: *
026: * To change the template for this generated type comment go to
027: * Window - Preferences - Java - Code Generation - Code and Comments
028: */
029: public class ENCRYPT14KeystoreTest extends TestCase {
030:
031: public void testInitWrongKeystoreProperties() {
032:
033: Properties props = new Properties();
034: String defaultKeystore = "unkownKeystore.keystore";
035: props.put("key_store_name", defaultKeystore);
036:
037: //javax.
038: ENCRYPT encrypt = new ENCRYPT();
039: encrypt.setProperties(props);
040: try {
041: encrypt.init();
042: } catch (Exception e) {
043: System.out.println(e.getMessage());
044: assertEquals("Unable to load keystore " + defaultKeystore
045: + " ensure file is on classpath", e.getMessage());
046: }
047:
048: }
049:
050: public void testInitKeystoreProperties() {
051:
052: Properties props = new Properties();
053: String defaultKeystore = "defaultStore.keystore";
054: props.put("key_store_name", defaultKeystore);
055:
056: //javax.
057: ENCRYPT encrypt = new ENCRYPT();
058: encrypt.setProperties(props);
059: try {
060: encrypt.init();
061: } catch (Exception e) {
062: System.out.println(e.getMessage());
063: }
064: assertNotNull(encrypt.getSymDecodingCipher());
065: assertNotNull(encrypt.getSymEncodingCipher());
066:
067: }
068:
069: public void testMessageDownEncode() throws Exception {
070: // initialise the encryption
071: Properties props = new Properties();
072: String defaultKeystore = "defaultStore.keystore";
073: props.put("key_store_name", defaultKeystore);
074:
075: //javax.
076: ENCRYPT encrypt = new ENCRYPT();
077: encrypt.setProperties(props);
078: try {
079: encrypt.init();
080: } catch (Exception e) {
081: fail(e.getMessage());
082: }
083: // use a second instance so we know we are not accidentally using internal key
084: Properties props2 = new Properties();
085: props2.put("key_store_name", defaultKeystore);
086: // javax.
087: ENCRYPT encrypt2 = new ENCRYPT();
088: encrypt2.setProperties(props2);
089: try {
090: encrypt2.init();
091: } catch (Exception e) {
092: fail(e.getMessage());
093: }
094: MockObserver observer = new MockObserver();
095: encrypt.setObserver(observer);
096:
097: encrypt.keyServer = true;
098: String messageText = "hello this is a test message";
099: Message msg = new Message(null, null, messageText.getBytes());
100:
101: Event event = new Event(Event.MSG, msg);
102: encrypt.down(event);
103: Message sentMsg = (Message) ((Event) observer.getDownMessages()
104: .get("message0")).getArg();
105: String encText = new String(sentMsg.getBuffer());
106: assertNotSame(encText, messageText);
107: Cipher cipher = encrypt2.getSymDecodingCipher();
108: byte[] decodedBytes = cipher.doFinal(sentMsg.getBuffer());
109: String temp = new String(decodedBytes);
110: System.out.println("decoded text:" + temp);
111: assertEquals(temp, messageText);
112:
113: }
114:
115: public void testMessageUpDecode() throws Exception {
116: // initialise the encryption
117: Properties props = new Properties();
118: String defaultKeystore = "defaultStore.keystore";
119: props.put("key_store_name", defaultKeystore);
120:
121: //javax.
122: ENCRYPT encrypt = new ENCRYPT();
123: encrypt.setProperties(props);
124: try {
125: encrypt.init();
126: } catch (Exception e) {
127: fail(e.getMessage());
128: }
129: // use a second instance so we know we are not accidentally using internal key
130: Properties props2 = new Properties();
131: props2.put("key_store_name", defaultKeystore);
132: // javax.
133: ENCRYPT encrypt2 = new ENCRYPT();
134: encrypt2.setProperties(props2);
135: try {
136: encrypt2.init();
137: } catch (Exception e) {
138: fail(e.getMessage());
139: }
140: MockObserver observer = new MockObserver();
141: encrypt.setObserver(observer);
142:
143: encrypt.keyServer = true;
144: String messageText = "hello this is a test message";
145: Cipher cipher = encrypt2.getSymEncodingCipher();
146: byte[] encodedBytes = cipher.doFinal(messageText.getBytes());
147: assertNotSame(new String(encodedBytes), messageText);
148:
149: MessageDigest digest = MessageDigest.getInstance("MD5");
150: digest.reset();
151: digest.update(encrypt.getDesKey().getEncoded());
152:
153: String symVersion = new String(digest.digest(), "UTF-8");
154:
155: Message msg = new Message(null, null, encodedBytes);
156: msg.putHeader(ENCRYPT.EncryptHeader.KEY,
157: new ENCRYPT.EncryptHeader(
158: ENCRYPT.EncryptHeader.ENCRYPT, symVersion));
159: Event event = new Event(Event.MSG, msg);
160: encrypt.up(event);
161: Message rcvdMsg = (Message) ((Event) observer.getUpMessages()
162: .get("message0")).getArg();
163: String decText = new String(rcvdMsg.getBuffer());
164:
165: assertEquals(decText, messageText);
166:
167: }
168:
169: public void testMessageUpWrongKey() throws Exception {
170: // initialise the encryption
171: Properties props = new Properties();
172: String defaultKeystore = "defaultStore.keystore";
173: String defaultKeystore2 = "defaultStore2.keystore";
174: props.put("key_store_name", defaultKeystore);
175:
176: //javax.
177: ENCRYPT encrypt = new ENCRYPT();
178: encrypt.setProperties(props);
179: try {
180: encrypt.init();
181: } catch (Exception e) {
182: fail(e.getMessage());
183: }
184: // use a second instance so we know we are not accidentally using internal key
185:
186: // javax.
187: Properties props2 = new Properties();
188: ENCRYPT encrypt2 = new ENCRYPT();
189: props2.setProperty("key_store_name", defaultKeystore2);
190:
191: encrypt2.setProperties(props2);
192: try {
193: encrypt2.init();
194: } catch (Exception e) {
195: fail(e.getMessage());
196: }
197: MockObserver observer = new MockObserver();
198: encrypt.setObserver(observer);
199:
200: encrypt.keyServer = true;
201: String messageText = "hello this is a test message";
202: Cipher cipher = encrypt2.getSymEncodingCipher();
203: byte[] encodedBytes = cipher.doFinal(messageText.getBytes());
204: assertNotSame(new String(encodedBytes), messageText);
205:
206: MessageDigest digest = MessageDigest.getInstance("MD5");
207: digest.reset();
208: digest.update(encrypt2.getDesKey().getEncoded());
209:
210: String symVersion = new String(digest.digest());
211:
212: Message msg = new Message(null, null, encodedBytes);
213: msg.putHeader(ENCRYPT.EncryptHeader.KEY,
214: new ENCRYPT.EncryptHeader(
215: ENCRYPT.EncryptHeader.ENCRYPT, symVersion));
216: Event event = new Event(Event.MSG, msg);
217: encrypt.up(event);
218: assertEquals(observer.getUpMessages().size(), 0);
219:
220: }
221:
222: public void testMessageUpNoEncryptHeader() throws Exception {
223: // initialise the encryption
224: Properties props = new Properties();
225: String defaultKeystore = "defaultStore.keystore";
226: props.put("key_store_name", defaultKeystore);
227:
228: //javax.
229: ENCRYPT encrypt = new ENCRYPT();
230: encrypt.setProperties(props);
231: try {
232: encrypt.init();
233: } catch (Exception e) {
234: fail(e.getMessage());
235: }
236: // use a second instance so we know we are not accidentally using internal key
237: Properties props2 = new Properties();
238: props2.put("key_store_name", defaultKeystore);
239: // javax.
240: ENCRYPT encrypt2 = new ENCRYPT();
241: encrypt2.setProperties(props2);
242: try {
243: encrypt2.init();
244: } catch (Exception e) {
245: fail(e.getMessage());
246: }
247: MockObserver observer = new MockObserver();
248: encrypt.setObserver(observer);
249:
250: encrypt.keyServer = true;
251: String messageText = "hello this is a test message";
252: Cipher cipher = encrypt2.getSymEncodingCipher();
253: byte[] encodedBytes = cipher.doFinal(messageText.getBytes());
254: assertNotSame(new String(encodedBytes), messageText);
255:
256: Message msg = new Message(null, null, encodedBytes);
257: Event event = new Event(Event.MSG, msg);
258: encrypt.up(event);
259: assertEquals(observer.getUpMessages().size(), 0);
260:
261: }
262:
263: public void testEventUpNoMessage() throws Exception {
264: // initialise the encryption
265: Properties props = new Properties();
266: String defaultKeystore = "defaultStore.keystore";
267: props.put("key_store_name", defaultKeystore);
268:
269: //javax.
270: ENCRYPT encrypt = new ENCRYPT();
271: encrypt.setProperties(props);
272: try {
273: encrypt.init();
274: } catch (Exception e) {
275: fail(e.getMessage());
276: }
277:
278: MockObserver observer = new MockObserver();
279: encrypt.setObserver(observer);
280:
281: encrypt.keyServer = true;
282:
283: Event event = new Event(Event.MSG, null);
284: encrypt.up(event);
285: assertEquals(observer.getUpMessages().size(), 1);
286:
287: }
288:
289: public void testMessageUpNoBuffer() throws Exception {
290: // initialise the encryption
291: Properties props = new Properties();
292: String defaultKeystore = "defaultStore.keystore";
293: props.put("key_store_name", defaultKeystore);
294:
295: //javax.
296: ENCRYPT encrypt = new ENCRYPT();
297: encrypt.setProperties(props);
298: try {
299: encrypt.init();
300: } catch (Exception e) {
301: fail(e.getMessage());
302: }
303:
304: MockObserver observer = new MockObserver();
305: encrypt.setObserver(observer);
306:
307: encrypt.keyServer = true;
308: Message msg = new Message(null, null, null);
309: Event event = new Event(Event.MSG, msg);
310: encrypt.up(event);
311: assertEquals(observer.getUpMessages().size(), 1);
312:
313: }
314:
315: class MockObserver implements ProtocolObserver {
316:
317: private Map upMessages = new HashMap();
318: private Map downMessages = new HashMap();
319: private int counter = 0;
320:
321: /* (non-Javadoc)
322: * @see org.jgroups.UpHandler#up(org.jgroups.Event)
323: */
324:
325: private void storeUp(Event evt) {
326: upMessages.put("message" + counter++, evt);
327: }
328:
329: private void storeDown(Event evt) {
330: downMessages.put("message" + counter++, evt);
331: }
332:
333: public void up(Event evt) {
334: storeUp(evt);
335: System.out.println("Up:" + evt.toString());
336:
337: }
338:
339: /* (non-Javadoc)
340: * @see org.jgroups.stack.ProtocolObserver#setProtocol(org.jgroups.stack.Protocol)
341: */
342: public void setProtocol(Protocol prot) {
343: }
344:
345: /* (non-Javadoc)
346: * @see org.jgroups.stack.ProtocolObserver#up(org.jgroups.Event, int)
347: */
348: public boolean up(Event evt, int num_evts) {
349: System.out.println("Up:" + evt.toString());
350:
351: return false;
352: }
353:
354: /* (non-Javadoc)
355: * @see org.jgroups.stack.ProtocolObserver#passUp(org.jgroups.Event)
356: */
357: public boolean passUp(Event evt) {
358: storeUp(evt);
359: System.out.println("PassUp:" + evt.toString());
360:
361: return false;
362: }
363:
364: /* (non-Javadoc)
365: * @see org.jgroups.stack.ProtocolObserver#down(org.jgroups.Event, int)
366: */
367: public boolean down(Event evt, int num_evts) {
368: System.out.println("down:" + evt.toString());
369:
370: return false;
371: }
372:
373: /* (non-Javadoc)
374: * @see org.jgroups.stack.ProtocolObserver#passDown(org.jgroups.Event)
375: */
376: public boolean passDown(Event evt) {
377: storeDown(evt);
378: System.out.println("passdown:" + evt.toString());
379:
380: return false;
381: }
382:
383: /**
384: * @return Returns the upMessages.
385: */
386: protected Map getUpMessages() {
387: return upMessages;
388: }
389:
390: /**
391: * @param upMessages The upMessages to set.
392: */
393: protected void setUpMessages(Map upMessages) {
394: this .upMessages = upMessages;
395: }
396:
397: /**
398: * @return Returns the downMessages.
399: */
400: protected Map getDownMessages() {
401: return downMessages;
402: }
403:
404: /**
405: * @param downMessages The downMessages to set.
406: */
407: protected void setDownMessages(Map downMessages) {
408: this .downMessages = downMessages;
409: }
410: }
411:
412: class MockAddress implements Address {
413:
414: /* (non-Javadoc)
415: * @see org.jgroups.Address#isMulticastAddress()
416: */
417: public boolean isMulticastAddress() {
418: return false;
419: }
420:
421: public int size() {
422: return 0;
423: }
424:
425: /* (non-Javadoc)
426: * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
427: */
428: public void readExternal(ObjectInput in) throws IOException,
429: ClassNotFoundException {
430:
431: }
432:
433: /* (non-Javadoc)
434: * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
435: */
436: public void writeExternal(ObjectOutput out) throws IOException {
437: }
438:
439: public void writeTo(DataOutputStream out) throws IOException {
440: ;
441: }
442:
443: public void readFrom(DataInputStream in) throws IOException,
444: IllegalAccessException, InstantiationException {
445: ;
446: }
447:
448: /* (non-Javadoc)
449: * @see java.lang.Comparable#compareTo(java.lang.Object)
450: */
451: public int compareTo(Object o) {
452: return -1;
453: }
454:
455: }
456: }
|