/** Test reconstituting a message */ public void testReconstituteMessage() { String encoding1 = " 172.17.152.12253200 172.17.152.17 2017D 1TCP, the Queen of Protocols! 4587"; char[] encodingChars = encoding1.toCharArray(); byte[] encodingBytes = new byte[encodingChars.length]; for (int i = 0; i < encodingChars.length; i++) // convert i-th char to byte encodingBytes[i] = (byte) encodingChars[i]; assertEquals("encoding1 length", ReliableTransportMessage.BUFFER_LEN, encoding1.length()); ReliableTransportMessage msg1 = ReliableTransportMessage.reconstitute(encodingBytes); assertNotNull("reconstituted message 1 is NULL", msg1); assertEquals("Source IP", "/172.17.152.122", msg1.getSourceIP().toString()); assertEquals("Dest IP", "/172.17.152.17", msg1.getDestIP().toString()); assertEquals("Source port", 53200, msg1.getSrcPort()); assertEquals("Dest port", 2017, msg1.getDestPort()); assertEquals("Op code", ReliableTransportMessage.DATA, msg1.getOpCode()); assertEquals("seq no", 1, msg1.getSequenceNo()); assertEquals( "Payload length", ReliableTransportMessage.PAYLOAD_LEN, msg1.getPayload().length()); assertEquals("Payload", "TCP, the Queen of Protocols! ", msg1.getPayload()); assertEquals("Computed checksum", 4587, msg1.getComputedChecksum()); assertEquals("Stored checksum", 4587, msg1.getStoredChecksum()); // encoding with garbled contents String encoding2 = " 172.17.152.12253200 172.17.152.17 2017D 1Drink Coca-cola! 4568"; assertEquals("encoding2 length", ReliableTransportMessage.BUFFER_LEN, encoding2.length()); encodingChars = encoding2.toCharArray(); byte[] encodingBytes2 = new byte[encodingChars.length]; for (int i = 0; i < encodingChars.length; i++) // convert i-th char to byte encodingBytes2[i] = (byte) encodingChars[i]; ReliableTransportMessage msg2 = ReliableTransportMessage.reconstitute(encodingBytes2); assertNull("reconstituted message 2 is not NULL", msg2); }
/** * Test fields of the constructed object * * @throws UnknownHostException */ public void testConstructor() throws UnknownHostException { InetAddress srcIP = InetAddress.getByName("172.17.152.122"); InetAddress destIP = InetAddress.getByName("172.17.152.124"); ReliableTransportMessage msg = new ReliableTransportMessage( srcIP, destIP, 2200, 2015, ReliableTransportMessage.DATA, 1, "Hi mom!"); assertEquals("source port: ", 2200, msg.getSrcPort()); assertEquals("dest port: ", 2015, msg.getDestPort()); assertEquals("op code", 'D', msg.getOpCode()); assertEquals("sequence no", 1, msg.getSequenceNo()); assertEquals("payload", "Hi mom!", msg.getPayload()); }