digest.update(server.getDesKey().getEncoded());
String symVersion=new String(digest.digest(), "UTF-8");
// encrypt and send an initial message to peer
Cipher cipher=server.getSymEncodingCipher();
Message msg=new Message();
msg.setBuffer(cipher.doFinal("hello".getBytes()));
msg.putHeader(ENCRYPT_ID, new EncryptHeader(EncryptHeader.ENCRYPT, symVersion));
Event evt=new Event(Event.MSG, msg);
peer.up(evt);
//assert that message is queued as we have no key from server
Util.assertTrue(peerObserver.getUpMessages().isEmpty());
// send a view change where we are not the controller
// send to peer - which should have peer2 as its key server
peer.up(serverEvent);
// assert that peer\ keyserver address is now set
Util.assertEquals(serverAddress, peer.getKeyServerAddr());
// get the resulting message from the peer - should be a key request
Event sent=(Event)peerObserver.getDownMessages().get("message0");
Util.assertEquals(((EncryptHeader)((Message)sent.getArg()).getHeader(ENCRYPT_ID)).getType(), EncryptHeader.KEY_REQUEST);
Util.assertEquals(new String(((Message)sent.getArg()).getBuffer()), new String(peer.getKpair().getPublic().getEncoded()));
// send this event to server
server.up(sent);
Event reply=(Event)serverObserver.getDownMessages().get("message1");
//assert that reply is the session key encrypted with peer's public key
Util.assertEquals(((EncryptHeader)((Message)reply.getArg()).getHeader(ENCRYPT_ID)).getType(), EncryptHeader.SECRETKEY);
assert !peer.getDesKey().equals(server.getDesKey());
// now send back to peer
peer.up(reply);
// assert that both now have same key
Util.assertEquals(peer.getDesKey(), server.getDesKey());
// send another encrypted message to peer to test queue
Message msg2=new Message();
msg2.setBuffer(cipher.doFinal("hello2".getBytes()));
msg2.putHeader(ENCRYPT_ID, new EncryptHeader(EncryptHeader.ENCRYPT, symVersion));
Event evt2=new Event(Event.MSG, msg2);
peer.up(evt2);