Skip to content
Snippets Groups Projects
Commit d97c4332 authored by Martin Hustoles's avatar Martin Hustoles
Browse files

moved crc to cpMsg

parent 40b8947a
No related branches found
No related tags found
No related merge requests found
File added
File deleted
...@@ -45,7 +45,7 @@ public class CPClient { ...@@ -45,7 +45,7 @@ public class CPClient {
System.out.println("Command: "); System.out.println("Command: ");
sentence = inFromUser.readLine(); sentence = inFromUser.readLine();
// Currently only these two commands are supported by the specification // 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; continue;
cp.send(sentence.trim(), null); cp.send(sentence.trim(), null);
......
...@@ -49,10 +49,4 @@ public class CPCommandMessage extends CPMsg{ ...@@ -49,10 +49,4 @@ public class CPCommandMessage extends CPMsg{
public static int getMaxMessageId() { public static int getMaxMessageId() {
return MAX_ID; return MAX_ID;
} }
public static long calcCRC(String data) {
Checksum tmp = new CRC32();
tmp.update(data.getBytes());
return tmp.getValue();
}
} }
...@@ -34,4 +34,10 @@ class CPMsg extends Msg { ...@@ -34,4 +34,10 @@ class CPMsg extends Msg {
return parsedMsg; return parsedMsg;
} }
protected long calcCRC(String data) {
Checksum tmp = new CRC32();
tmp.update(data.getBytes());
return tmp.getValue();
}
} }
...@@ -30,6 +30,7 @@ public class CPClientCookieRequestTest { ...@@ -30,6 +30,7 @@ public class CPClientCookieRequestTest {
@Mock @Mock
PhyProtocol phyProtocolMock; PhyProtocol phyProtocolMock;
PhyMsg testMsg; PhyMsg testMsg;
PhyMsg corruptedMsg;
CPProtocol cProtocol; CPProtocol cProtocol;
@BeforeEach @BeforeEach
...@@ -39,6 +40,7 @@ public class CPClientCookieRequestTest { ...@@ -39,6 +40,7 @@ public class CPClientCookieRequestTest {
try { try {
phyConfig = new PhyConfiguration(InetAddress.getByName(serverName), serverPort, Protocol.proto_id.CP); phyConfig = new PhyConfiguration(InetAddress.getByName(serverName), serverPort, Protocol.proto_id.CP);
testMsg = new PhyMsg(phyConfig); testMsg = new PhyMsg(phyConfig);
corruptedMsg = new PhyMsg(phyConfig);
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
fail(); fail();
} }
...@@ -56,7 +58,7 @@ public class CPClientCookieRequestTest { ...@@ -56,7 +58,7 @@ public class CPClientCookieRequestTest {
when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg); when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg);
// Run the test // Run the test
assertDoesNotThrow(()->cProtocol.requestCookie(0)); assertDoesNotThrow(()->cProtocol.requestCookie());
// verify a specified behavior // verify a specified behavior
verify(phyProtocolMock, times(1)).receive(2000); verify(phyProtocolMock, times(1)).receive(2000);
...@@ -75,7 +77,7 @@ public class CPClientCookieRequestTest { ...@@ -75,7 +77,7 @@ public class CPClientCookieRequestTest {
// Run the test // Run the test
assertThrows(CookieRequestException.class, assertThrows(CookieRequestException.class,
()->cProtocol.requestCookie(0)); ()->cProtocol.requestCookie());
verify(phyProtocolMock, times(1)).receive(2000); verify(phyProtocolMock, times(1)).receive(2000);
} }
...@@ -85,17 +87,18 @@ public class CPClientCookieRequestTest { ...@@ -85,17 +87,18 @@ public class CPClientCookieRequestTest {
// Fill the message object that is going to be returned to the object-under-test // 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) // 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); PhyConfiguration corruptedPhyConfig = new PhyConfiguration(InetAddress.getByName(serverName), serverPort, Protocol.proto_id.SLP);
testMsg = new PhyMsg(phyConfig); PhyMsg corruptedPhyMsg = new PhyMsg(corruptedPhyConfig);
testMsg = (PhyMsg) testMsg.parse("phy 5 cp cres ACK 12345"); 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 // Implement behavior of the mocked object
when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg); when(phyProtocolMock.receive(anyInt())).thenReturn(corruptedPhyMsg).thenReturn(testMsg);
// Run the test // Run the test
assertThrows(IllegalMsgException.class, assertDoesNotThrow(()->cProtocol.requestCookie());
()->cProtocol.requestCookie(0));
verify(phyProtocolMock, times(1)).receive(2000); verify(phyProtocolMock, times(2)).receive(2000);
} }
@Test @Test
...@@ -103,17 +106,16 @@ public class CPClientCookieRequestTest { ...@@ -103,17 +106,16 @@ public class CPClientCookieRequestTest {
// Fill the message object that is going to be returned to the object-under-test // 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) // 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); corruptedMsg = (PhyMsg) corruptedMsg.parse("phy 7 cp cres ACK abc");
testMsg = new PhyMsg(phyConfig);
testMsg = (PhyMsg) testMsg.parse("phy 7 cp cres ACK 12345"); testMsg = (PhyMsg) testMsg.parse("phy 7 cp cres ACK 12345");
// Implement behavior of the mocked object // Implement behavior of the mocked object
when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg); when(phyProtocolMock.receive(anyInt())).thenReturn(corruptedMsg).thenReturn(testMsg);
// Run the test // Run the test
assertThrows(IllegalMsgException.class, assertDoesNotThrow(()->cProtocol.requestCookie());
()->cProtocol.requestCookie(0));
verify(phyProtocolMock, times(1)).receive(2000); verify(phyProtocolMock, times(2)).receive(2000);
} }
@Test @Test
...@@ -121,17 +123,16 @@ public class CPClientCookieRequestTest { ...@@ -121,17 +123,16 @@ public class CPClientCookieRequestTest {
// Fill the message object that is going to be returned to the object-under-test // 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) // 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); corruptedMsg = (PhyMsg) corruptedMsg.parse("phy 7 cp cres ACK");
testMsg = new PhyMsg(phyConfig); testMsg = (PhyMsg) testMsg.parse("phy 7 cp cres ACK 12345");
testMsg = (PhyMsg) testMsg.parse("phy 5 cp cres ACK");
// Implement behavior of the mocked object // Implement behavior of the mocked object
when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg); when(phyProtocolMock.receive(anyInt())).thenReturn(corruptedMsg).thenReturn(testMsg);
// Run the test // Run the test
assertThrows(IllegalMsgException.class, assertDoesNotThrow(()->cProtocol.requestCookie());
()->cProtocol.requestCookie(0));
verify(phyProtocolMock, times(1)).receive(2000); verify(phyProtocolMock, times(2)).receive(anyInt());
} }
@Test @Test
...@@ -142,7 +143,7 @@ public class CPClientCookieRequestTest { ...@@ -142,7 +143,7 @@ public class CPClientCookieRequestTest {
// Run the test // Run the test
assertThrows(CookieRequestException.class, assertThrows(CookieRequestException.class,
()->cProtocol.requestCookie(0)); ()->cProtocol.requestCookie());
verify(phyProtocolMock, times(3)).receive(2000); verify(phyProtocolMock, times(3)).receive(2000);
} }
......
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment