1 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 ; 18 import java.util.HashMap ; 19 import java.util.Map ; 20 import java.util.Properties ; 21 27 public class ENCRYPT14KeystoreTest extends TestCase { 28 29 30 31 32 33 public void testInitWrongKeystoreProperties() { 34 Properties props = new Properties (); 35 String 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 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 { 48 49 Properties props = new Properties (); 50 String defaultKeystore = "defaultStore.keystore"; 51 props.put("key_store_name",defaultKeystore); 52 53 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 { 63 Properties props = new Properties (); 65 String defaultKeystore = "defaultStore.keystore"; 66 props.put("key_store_name",defaultKeystore); 67 68 ENCRYPT encrypt = new ENCRYPT(); 70 encrypt.setProperties(props); 71 encrypt.init(); 72 73 Properties props2 = new Properties (); 75 props2.put("key_store_name",defaultKeystore); 76 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 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 encText = new String (sentMsg.getBuffer()); 92 assertNotSame(encText,messageText); 93 Cipher cipher = encrypt2.getSymDecodingCipher(); 94 byte[] decodedBytes = cipher.doFinal(sentMsg.getBuffer()); 95 String temp = new String (decodedBytes); 96 System.out.println("decoded text:" + temp); 97 assertEquals(temp,messageText); 98 99 } 100 101 102 public void testMessageUpDecode() throws Exception { 103 Properties props = new Properties (); 105 String defaultKeystore = "defaultStore.keystore"; 106 props.put("key_store_name",defaultKeystore); 107 108 ENCRYPT encrypt = new ENCRYPT(); 110 encrypt.setProperties(props); 111 encrypt.init(); 112 113 Properties props2 = new Properties (); 115 props2.put("key_store_name",defaultKeystore); 116 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 messageText = "hello this is a test message"; 126 Cipher cipher = encrypt2.getSymEncodingCipher(); 127 byte[] encodedBytes = cipher.doFinal(messageText.getBytes()); 128 assertNotSame(new String (encodedBytes),messageText); 129 130 MessageDigest digest = MessageDigest.getInstance("MD5"); 131 digest.reset(); 132 digest.update(encrypt.getDesKey().getEncoded()); 133 134 String symVersion = new String (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 decText = new String (rcvdMsg.getBuffer()); 142 143 assertEquals(decText,messageText); 144 145 } 146 147 public void testMessageUpWrongKey() throws Exception { 148 Properties props = new Properties (); 150 String defaultKeystore = "defaultStore.keystore"; 151 String defaultKeystore2 = "defaultStore2.keystore"; 152 props.put("key_store_name",defaultKeystore); 153 154 ENCRYPT encrypt = new ENCRYPT(); 156 encrypt.setProperties(props); 157 encrypt.init(); 158 160 Properties props2 = new Properties (); 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 messageText = "hello this is a test message"; 172 Cipher cipher = encrypt2.getSymEncodingCipher(); 173 byte[] encodedBytes = cipher.doFinal(messageText.getBytes()); 174 assertNotSame(new String (encodedBytes),messageText); 175 176 MessageDigest digest = MessageDigest.getInstance("MD5"); 177 digest.reset(); 178 digest.update(encrypt2.getDesKey().getEncoded()); 179 180 String symVersion = new String (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 { 191 Properties props = new Properties (); 193 String defaultKeystore = "defaultStore.keystore"; 194 props.put("key_store_name",defaultKeystore); 195 196 ENCRYPT encrypt = new ENCRYPT(); 198 encrypt.setProperties(props); 199 encrypt.init(); 200 201 Properties props2 = new Properties (); 203 props2.put("key_store_name",defaultKeystore); 204 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 messageText = "hello this is a test message"; 214 Cipher cipher = encrypt2.getSymEncodingCipher(); 215 byte[] encodedBytes = cipher.doFinal(messageText.getBytes()); 216 assertNotSame(new String (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 { 228 Properties props = new Properties (); 230 String defaultKeystore = "defaultStore.keystore"; 231 props.put("key_store_name",defaultKeystore); 232 233 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 { 251 Properties props = new Properties (); 253 String defaultKeystore = "defaultStore.keystore"; 254 props.put("key_store_name",defaultKeystore); 255 256 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 upMessages = new HashMap (); 273 private Map downMessages = new HashMap (); 274 private int counter =0; 275 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 295 public void setProtocol(Protocol prot) 296 { 297 } 298 299 300 301 304 public void passUp(Event evt) 305 { 306 storeUp(evt); 307 System.out.println("PassUp:"+evt.toString()); 308 } 309 310 313 public void down(Event evt) 314 { 315 System.out.println("down:"+evt.toString()); 316 } 317 318 321 public void passDown(Event evt) 322 { 323 storeDown(evt); 324 System.out.println("passdown:"+evt.toString()); 325 } 326 327 330 protected Map getUpMessages() 331 { 332 return upMessages; 333 } 334 337 protected void setUpMessages(Map upMessages) 338 { 339 this.upMessages = upMessages; 340 } 341 344 protected Map getDownMessages() 345 { 346 return downMessages; 347 } 348 351 protected void setDownMessages(Map 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 364 public boolean isMulticastAddress() 365 { 366 return false; 367 } 368 369 public int size() { 370 return 0; 371 } 372 373 376 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException 377 { 378 379 } 380 381 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 , InstantiationException { 393 ; 394 } 395 396 399 public int compareTo(Object o) 400 { 401 return -1; 402 } 403 404 405 } 406 } 407 | Popular Tags |