diff --git a/Documentation_codeChange_task2.docx b/Documentation_codeChange_task2.docx new file mode 100644 index 0000000000000000000000000000000000000000..f65370753b457e8215a05a1835f1b830057c424f Binary files /dev/null and b/Documentation_codeChange_task2.docx differ diff --git a/internetworking_ws23.zip b/internetworking_ws23.zip deleted file mode 100644 index 01f158f5eb24e0e81a0330b0d7cd80a898c76c65..0000000000000000000000000000000000000000 Binary files a/internetworking_ws23.zip and /dev/null differ diff --git a/src/main/java/apps/CPClient.java b/src/main/java/apps/CPClient.java index 6a1d55c5482bda8a72e082123648b9e86db9160c..1b091b768d8f4c248eaff6ffe5606799a586f4db 100644 --- a/src/main/java/apps/CPClient.java +++ b/src/main/java/apps/CPClient.java @@ -45,7 +45,7 @@ public class CPClient { System.out.println("Command: "); sentence = inFromUser.readLine(); // Currently only these two commands are supported by the specification - if(!(sentence.equals("status") || sentence.startsWith("print"))) + if(!(sentence.equals("status") || sentence.equals("print"))) continue; cp.send(sentence.trim(), null); diff --git a/src/main/java/cp/CPCommandMessage.java b/src/main/java/cp/CPCommandMessage.java index df0b58935ffe05788769cfab77e2a463166a2e23..61f6d7a7aed5794b2231bf686881451fb3f24b01 100644 --- a/src/main/java/cp/CPCommandMessage.java +++ b/src/main/java/cp/CPCommandMessage.java @@ -49,10 +49,4 @@ public class CPCommandMessage extends CPMsg{ public static int getMaxMessageId() { return MAX_ID; } - - public static long calcCRC(String data) { - Checksum tmp = new CRC32(); - tmp.update(data.getBytes()); - return tmp.getValue(); - } } diff --git a/src/main/java/cp/CPMsg.java b/src/main/java/cp/CPMsg.java index 39f22905fc3ba2ef4c07a8290e4e5a0008012da2..3fe3e07479893764003417597cbe158e5acc8a6b 100644 --- a/src/main/java/cp/CPMsg.java +++ b/src/main/java/cp/CPMsg.java @@ -34,4 +34,10 @@ class CPMsg extends Msg { return parsedMsg; } + protected long calcCRC(String data) { + Checksum tmp = new CRC32(); + tmp.update(data.getBytes()); + return tmp.getValue(); + } + } diff --git a/src/test/java/phy/CPClientCookieRequestTest.java b/src/test/java/phy/CPClientCookieRequestTest.java index dbe591dd32299aa5cdb2384156e5ce0d72deb60e..8b215698b2a0020d682dbdbfdf9a95ebe59d071a 100644 --- a/src/test/java/phy/CPClientCookieRequestTest.java +++ b/src/test/java/phy/CPClientCookieRequestTest.java @@ -30,6 +30,7 @@ public class CPClientCookieRequestTest { @Mock PhyProtocol phyProtocolMock; PhyMsg testMsg; + PhyMsg corruptedMsg; CPProtocol cProtocol; @BeforeEach @@ -39,6 +40,7 @@ public class CPClientCookieRequestTest { try { phyConfig = new PhyConfiguration(InetAddress.getByName(serverName), serverPort, Protocol.proto_id.CP); testMsg = new PhyMsg(phyConfig); + corruptedMsg = new PhyMsg(phyConfig); } catch (UnknownHostException e) { fail(); } @@ -56,7 +58,7 @@ public class CPClientCookieRequestTest { when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg); // Run the test - assertDoesNotThrow(()->cProtocol.requestCookie(0)); + assertDoesNotThrow(()->cProtocol.requestCookie()); // verify a specified behavior verify(phyProtocolMock, times(1)).receive(2000); @@ -75,7 +77,7 @@ public class CPClientCookieRequestTest { // Run the test assertThrows(CookieRequestException.class, - ()->cProtocol.requestCookie(0)); + ()->cProtocol.requestCookie()); verify(phyProtocolMock, times(1)).receive(2000); } @@ -85,17 +87,18 @@ public class CPClientCookieRequestTest { // Fill the message object that is going to be returned to the object-under-test // with the message needed for this test case (also create corresponding configuration object) - PhyConfiguration phyConfig = new PhyConfiguration(InetAddress.getByName(serverName), serverPort, Protocol.proto_id.SLP); - testMsg = new PhyMsg(phyConfig); - testMsg = (PhyMsg) testMsg.parse("phy 5 cp cres ACK 12345"); + PhyConfiguration corruptedPhyConfig = new PhyConfiguration(InetAddress.getByName(serverName), serverPort, Protocol.proto_id.SLP); + PhyMsg corruptedPhyMsg = new PhyMsg(corruptedPhyConfig); + corruptedPhyMsg = (PhyMsg) corruptedPhyMsg.parse("phy 5 cp cres ACK 12345"); + testMsg = (PhyMsg) testMsg.parse("phy 7 cp cres ACK 12345"); // Implement behavior of the mocked object - when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg); + when(phyProtocolMock.receive(anyInt())).thenReturn(corruptedPhyMsg).thenReturn(testMsg); // Run the test - assertThrows(IllegalMsgException.class, - ()->cProtocol.requestCookie(0)); - verify(phyProtocolMock, times(1)).receive(2000); + assertDoesNotThrow(()->cProtocol.requestCookie()); + + verify(phyProtocolMock, times(2)).receive(2000); } @Test @@ -103,17 +106,16 @@ public class CPClientCookieRequestTest { // Fill the message object that is going to be returned to the object-under-test // with the message needed for this test case (also create corresponding configuration object) - PhyConfiguration phyConfig = new PhyConfiguration(InetAddress.getByName(serverName), serverPort, Protocol.proto_id.SLP); - testMsg = new PhyMsg(phyConfig); - testMsg = (PhyMsg) testMsg.parse("phy 7 cp cresACK 12345"); + corruptedMsg = (PhyMsg) corruptedMsg.parse("phy 7 cp cres ACK abc"); + testMsg = (PhyMsg) testMsg.parse("phy 7 cp cres ACK 12345"); // Implement behavior of the mocked object - when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg); + when(phyProtocolMock.receive(anyInt())).thenReturn(corruptedMsg).thenReturn(testMsg); // Run the test - assertThrows(IllegalMsgException.class, - ()->cProtocol.requestCookie(0)); - verify(phyProtocolMock, times(1)).receive(2000); + assertDoesNotThrow(()->cProtocol.requestCookie()); + + verify(phyProtocolMock, times(2)).receive(2000); } @Test @@ -121,17 +123,16 @@ public class CPClientCookieRequestTest { // Fill the message object that is going to be returned to the object-under-test // with the message needed for this test case (also create corresponding configuration object) - PhyConfiguration phyConfig = new PhyConfiguration(InetAddress.getByName(serverName), serverPort, Protocol.proto_id.SLP); - testMsg = new PhyMsg(phyConfig); - testMsg = (PhyMsg) testMsg.parse("phy 5 cp cres ACK"); + corruptedMsg = (PhyMsg) corruptedMsg.parse("phy 7 cp cres ACK"); + testMsg = (PhyMsg) testMsg.parse("phy 7 cp cres ACK 12345"); // Implement behavior of the mocked object - when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg); + when(phyProtocolMock.receive(anyInt())).thenReturn(corruptedMsg).thenReturn(testMsg); // Run the test - assertThrows(IllegalMsgException.class, - ()->cProtocol.requestCookie(0)); - verify(phyProtocolMock, times(1)).receive(2000); + assertDoesNotThrow(()->cProtocol.requestCookie()); + + verify(phyProtocolMock, times(2)).receive(anyInt()); } @Test @@ -142,7 +143,7 @@ public class CPClientCookieRequestTest { // Run the test assertThrows(CookieRequestException.class, - ()->cProtocol.requestCookie(0)); + ()->cProtocol.requestCookie()); verify(phyProtocolMock, times(3)).receive(2000); } diff --git a/~$cumentation_codeChange_task2.docx b/~$cumentation_codeChange_task2.docx new file mode 100644 index 0000000000000000000000000000000000000000..6ab6a4a4fce0e38572723182bd4dbc9343f00770 Binary files /dev/null and b/~$cumentation_codeChange_task2.docx differ