@Test /** Test entire encoding */ public void testEncoding() throws UnknownHostException { String expectedHeader = " 172.17.152.12253200 172.17.152.17 2016D 1"; String payload = "Hi Mom!"; String padding = " "; String checksumStr = " 3352"; assertEquals("Header length", ReliableTransportMessage.HEADER_LEN, expectedHeader.length()); assertEquals("padding length", 23, padding.length()); InetAddress srcIP = InetAddress.getByName("172.17.152.122"); InetAddress destIP = InetAddress.getByName("172.17.152.17"); ReliableTransportMessage msg = new ReliableTransportMessage( srcIP, destIP, 53200, 2016, ReliableTransportMessage.DATA, 1, payload); assertEquals("source IP", "/172.17.152.122", msg.getSourceIP().toString()); assertEquals("dest IP", "/172.17.152.17", msg.getDestIP().toString()); msg.encode(); String expectedEncoding = expectedHeader + payload + padding + checksumStr; assertEquals("Expected Encoding length", 78, expectedEncoding.length()); assertEquals( "Checksum input length", 73, ReliableTransportMessage.HEADER_LEN + ReliableTransportMessage.PAYLOAD_LEN); for (int i = 0; i < 73; i++) // check i-th term of checksum assertEquals("Checksum term " + i, (int) expectedEncoding.charAt(i), msg.getChecksumTerm(i)); String encodingStr = new String(msg.getBuffer()); assertEquals("encoded length", 78, encodingStr.length()); assertEquals("computed checksum", 3352, msg.getComputedChecksum()); assertEquals("encoding", expectedEncoding, encodingStr); }
/** 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); }