assertTrue(((EncryptHeader)((Message)sent.getArg()).getHeader(ENCRYPT.EncryptHeader.KEY)).getType() == ENCRYPT.EncryptHeader.KEY_REQUEST);
assertEquals(((EncryptHeader)((Message)sent.getArg()).getHeader(EncryptHeader.KEY)).getType(), EncryptHeader.KEY_REQUEST);
	public void testViewChangeNewKeyServerNewKey() throws Exception{		// create peer and server		ENCRYPT peer = new ENCRYPT();		try {			peer.init();		} catch (Exception e){			fail(e.getMessage());		}				ENCRYPT server = new ENCRYPT();		try {			server.init();		} catch (Exception e){			fail(e.getMessage());		}						ENCRYPT peer2 = new ENCRYPT();		try {			peer2.init();		} catch (Exception e){			fail(e.getMessage());		}				// set up server 		server.keyServer = true;		MockObserver serverObserver = new MockObserver();		server.setObserver(serverObserver);				//set the local address and view change to simulate a started instance		Address serverAddress = new MockAddress("server");		server.setLocal_addr(serverAddress);				//	set the server up as keyserver		Vector serverVector = new Vector();		serverVector.add(serverAddress);		View tempView = new View(new ViewId(serverAddress,1),serverVector);		Event serverEvent = new Event(Event.VIEW_CHANGE,tempView);		server.up(serverEvent);						// set up peer as if it has started but not recieved view change		Address peerAddress = new MockAddress("peer");		peer.setLocal_addr(peerAddress);		MockObserver peerObserver = new MockObserver();		peer.setObserver(peerObserver);		peer.keyServer = false;				// set up peer2 with server as key server		Address peer2Address = new MockAddress("peer2");		peer2.setLocal_addr(peer2Address);		MockObserver peer2Observer = new MockObserver();		peer2.setObserver(peer2Observer);		peer2.keyServer = false;		peer2.setKeyServerAddr(serverAddress);				// send an encrypted message from the server		Message msg = new Message();		msg.setBuffer("hello".getBytes());						Event evt = new Event(Event.MSG,msg);		server.down(evt);				// message0 is in response to view change		Event encEvt = (Event)serverObserver.getDownMessages().get("message1");		// sent to peer encrypted - should be queued in encyption layer as we do not have a keyserver set		peer.up(encEvt);				//assert that message is queued as we have no key from server		assertTrue(peerObserver.getUpMessages().isEmpty());				// send a view change to peer where peer2 is  controller		Vector peerVector = new Vector();		peerVector.add(peer2Address);		View tempPeerView = new View(new ViewId(peer2Address,1),peerVector);		Event event = new Event(Event.VIEW_CHANGE,tempPeerView);				// send to peer - should set peer2 as keyserver		peer.up(event);				// assert that peer\ keyserver address is now set		assertTrue(peer2Address == peer.getKeyServerAddr());				// get the resulting message from the peer - should be a key request to peer2				Event sent = (Event)peerObserver.getDownMessages().get("message0");				// ensure type and that request contains peers pub key		assertTrue(((EncryptHeader)((Message)sent.getArg()).getHeader(ENCRYPT.EncryptHeader.KEY)).getType() == ENCRYPT.EncryptHeader.KEY_REQUEST);		assertEquals(new String(((Message)sent.getArg()).getBuffer()),new String(peer.getKpair().getPublic().getEncoded()));				//assume that server is no longer available and peer2 is new server		// but did not get the key from server before assuming role		// send this event to peer2//		 send a view change to trigger the become key server		// we use the fact that our address is now the controller one		// send a view change where we are not the controller		Vector peer2Vector = new Vector();		peer2Vector.add(peer2Address);		View tempPeer2View = new View(new ViewId(peer2Address,1),peer2Vector);		Event event2 = new Event(Event.VIEW_CHANGE,tempPeer2View);		// this should have changed us to the key server		peer2.up(event2);				peer2.up(sent);				Event reply = (Event)peer2Observer.getDownMessages().get("message1");						//assert that reply is the session key encrypted with peer's public key		assertTrue(((EncryptHeader)((Message)reply.getArg()).getHeader(ENCRYPT.EncryptHeader.KEY)).getType() == ENCRYPT.EncryptHeader.SECRETKEY);			assertNotSame(peer.getDesKey(),peer2.getDesKey());		assertNotSame(server.getDesKey(),peer2.getDesKey());				// now send back to peer		peer.up(reply);				// assert that both now have same key		assertEquals(peer.getDesKey(),peer2.getDesKey());		assertNotSame(server.getDesKey(),peer.getDesKey());						// send another encrypted message to peer to test queue		Message msg2 = new Message();		msg2.setBuffer("hello2".getBytes());				Event evt2 = new Event(Event.MSG,msg2);		peer2.down(evt2);				Event Evt2 = (Event)peer2Observer.getDownMessages().get("message2");		peer.up(Evt2);		// make sure we have the events now in the up layers		assertEquals(2,peerObserver.getUpMessages().size());				Event tempEvt = (Event)peerObserver.getUpMessages().get("message2");						assertEquals(new String(((Message)tempEvt.getArg()).getBuffer()),"hello2");							}