diff --git a/CPClient_main.txt b/CPClient_main.txt new file mode 100644 index 0000000000000000000000000000000000000000..2cbf9d9db2b525ff51504d7d9e29b13d547fb15e --- /dev/null +++ b/CPClient_main.txt @@ -0,0 +1,103 @@ +#https://sequencediagram.org/ +title Internetworking WS 23/24 + +participant CPClient +participant PhyProtocol +participant CPProtocol +participant PhyConfiguration +participant Configuration +participant CPCookieRequestMsg +participant CPMsg +participant PhyMsg +participant CPCookieResponseMsg + +#Questions to Schöller +#Timeout Delay? (e.g at receive) +#Classes without Constructor? (e.g at line 60 in CPProtocol) +# + +[->CPClient:main +activate CPClient + +#Initializing PhyProtocol with everything in it +CPClient->PhyProtocol:<<create>> +activate PhyProtocol +PhyProtocol-->CPClient: +deactivate PhyProtocol +CPClient->CPProtocol:<<create>> +activate CPProtocol +CPProtocol->PhyConfiguration:<<create>> +activate PhyConfiguration +PhyConfiguration->Configuration:<<create>> +activate Configuration +Configuration-->PhyConfiguration: +deactivate Configuration +PhyConfiguration-->CPProtocol: +deactivate PhyConfiguration +CPProtocol-->CPClient: +deactivate CPProtocol + +#Sending message from User Input +CPClient->CPProtocol:send() +activate CPProtocol +CPProtocol->CPProtocol:requestCookie() +activate CPProtocol +CPProtocol->CPCookieRequestMsg:create() +activate CPCookieRequestMsg +CPCookieRequestMsg->CPMsg:create() +activate CPMsg +CPMsg-->CPCookieRequestMsg: +deactivate CPMsg +CPCookieRequestMsg-->CPProtocol: +deactivate CPCookieRequestMsg + +#calling send() in requestCookie() +CPProtocol->PhyProtocol:send() +activate PhyProtocol +PhyProtocol->PhyMsg:<<create>> +activate PhyMsg +PhyMsg-->PhyProtocol: +deactivate PhyMsg +PhyProtocol-->PhyMsg:create() +activate PhyMsg +PhyMsg-->PhyProtocol: +deactivate PhyMsg +PhyProtocol->PhyProtocol:send() +activate PhyProtocol +space +deactivate PhyProtocol +PhyProtocol-->CPProtocol: +deactivate PhyProtocol + +#Receive with Timeout +CPProtocol->PhyProtocol:receive +activate PhyProtocol +PhyProtocol->PhyProtocol:receive +activate PhyProtocol +space +deactivate PhyProtocol +PhyProtocol-->CPProtocol: +deactivate PhyProtocol + +#Creates Cookie Response Msg +CPProtocol->CPMsg: +activate CPMsg +CPMsg->CPCookieResponseMsg:<<create>> +activate CPCookieResponseMsg +CPCookieResponseMsg-->CPMsg: +deactivate CPCookieResponseMsg +CPMsg->CPMsg:parse +activate CPMsg +space +deactivate CPMsg +CPMsg-->CPProtocol: +deactivate CPMsg +space +deactivate CPProtocol +CPProtocol-->CPClient: + +deactivate CPProtocol +CPClient->CPProtocol:receive +activate CPProtocol +CPProtocol-->CPClient: +deactivate CPProtocol \ No newline at end of file diff --git a/sequence.jpg b/sequence.jpg new file mode 100644 index 0000000000000000000000000000000000000000..f8af87353d2ac0fbb00998f45d8b6e89382cfcd4 Binary files /dev/null and b/sequence.jpg differ diff --git a/src/test/java/phy/CPClientPrintTest.java b/src/test/java/phy/CPClientPrintTest.java new file mode 100644 index 0000000000000000000000000000000000000000..a8fb83ba44960c4e5b9eaca1044b4469934b7c52 --- /dev/null +++ b/src/test/java/phy/CPClientPrintTest.java @@ -0,0 +1,191 @@ +package phy; + +import core.Protocol; +import cp.CPProtocol; +import exceptions.CookieRequestException; +import exceptions.IWProtocolException; +import exceptions.IllegalMsgException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.zip.CRC32; +import java.util.zip.Checksum; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.verify; + +@ExtendWith(MockitoExtension.class) +public class CPClientPrintTest { + String serverName = "localhost"; + int serverPort = 3027; + + @Mock + PhyProtocol phyProtocolMock; + PhyConfiguration phyConfig; + PhyMsg testMsg; + PhyMsg corruptMsg; + CPProtocol cProtocol; + + @BeforeEach + void setup() throws UnknownHostException { + try { + phyConfig = new PhyConfiguration(InetAddress.getByName(serverName), serverPort, Protocol.proto_id.CP); + testMsg = new PhyMsg(phyConfig); + corruptMsg = new PhyMsg(phyConfig); + } catch (UnknownHostException e) { + fail(); + } + // Set up the object-under-test + cProtocol = new CPProtocol(InetAddress.getByName(serverName), serverPort, phyProtocolMock); + } + + @Test + void testCommandResponseOk() throws IWProtocolException, IOException { + // Fill the message object that is going to be returned to the object-under-test + // with the message needed for this test case + testMsg = (PhyMsg)testMsg.parse("phy 7 cp comres 1 ok 0 4003252835"); + + // Implement behavior of the mocked object + when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg); + + // Run the test + assertDoesNotThrow(()->cProtocol.receive()); + + // verify a specified behavior + verify(phyProtocolMock, times(1)).receive(2000); + } + + @Test + void testCommandResponseError() throws IWProtocolException, IOException { + // Fill the message object that is going to be returned to the object-under-test + // with the message needed for this test case + testMsg = (PhyMsg)testMsg.parse("phy 7 cp comres 1 error 0 3359664988"); + + // Implement behavior of the mocked object + when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg); + + // Run the test + assertDoesNotThrow(()->cProtocol.receive()); + + // verify a specified behavior + verify(phyProtocolMock, times(1)).receive(2000); + } + + @Test + void testCommandResponseErrorWithMessage() throws IWProtocolException, IOException { + // Fill the message object that is going to be returned to the object-under-test + // with the message needed for this test case + testMsg = (PhyMsg)testMsg.parse("phy 7 cp comres 1 error 16 Out of Resources 996594082"); + + // Implement behavior of the mocked object + when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg); + + // Run the test + assertDoesNotThrow(()->cProtocol.receive()); + + // verify a specified behavior + verify(phyProtocolMock, times(1)).receive(2000); + } + + @Test + void testCommandResponseOkWithMissingField() throws IWProtocolException, IOException { + // Fill the message object that is going to be returned to the object-under-test + // with the message needed for this test case + testMsg = (PhyMsg)testMsg.parse("phy 7 cp comres 1 ok 0 4003252835"); + corruptMsg = (PhyMsg)corruptMsg.parse("phy 7 cp comres 0 4003252835"); + + // Implement behavior of the mocked object + when(phyProtocolMock.receive(anyInt())).thenReturn(corruptMsg).thenReturn(testMsg); + + // Run the test + assertDoesNotThrow(()->cProtocol.receive()); + + // verify a specified behavior + verify(phyProtocolMock, times(2)).receive(2000); + } + + @Test + void testCommandResponseOkWithIllegalField() throws IWProtocolException, IOException { + // Fill the message object that is going to be returned to the object-under-test + // with the message needed for this test case + testMsg = (PhyMsg)testMsg.parse("phy 7 cp comres 1 ok 0 4003252835"); + corruptMsg = (PhyMsg)corruptMsg.parse("phy 7 cp comres 1 ok Null 4003252835"); + + // Implement behavior of the mocked object + when(phyProtocolMock.receive(anyInt())).thenReturn(corruptMsg).thenReturn(testMsg); + + // Run the test + assertDoesNotThrow(()->cProtocol.receive()); + + // verify a specified behavior + verify(phyProtocolMock, times(2)).receive(2000); + } + + @Test + void testCommandResponseOkWithIllegalChecksum() throws IWProtocolException, IOException { + // Fill the message object that is going to be returned to the object-under-test + // with the message needed for this test case + testMsg = (PhyMsg)testMsg.parse("phy 7 cp comres 1 ok 0 4003252835"); + corruptMsg = (PhyMsg)corruptMsg.parse("phy 7 cp comres 1 ok 0 400325283"); + + // Implement behavior of the mocked object + when(phyProtocolMock.receive(anyInt())).thenReturn(corruptMsg).thenReturn(testMsg); + + // Run the test + assertDoesNotThrow(()->cProtocol.receive()); + + // verify a specified behavior + verify(phyProtocolMock, times(2)).receive(2000); + } + + @Test + void testCommandResponseOkWithTooLongMessage() throws IWProtocolException, IOException { + // Fill the message object that is going to be returned to the object-under-test + // with the message needed for this test case + testMsg = (PhyMsg)testMsg.parse("phy 7 cp comres 1 ok 0 4003252835"); + corruptMsg = (PhyMsg)corruptMsg.parse("phy 7 cp comres 1 ok 0 Hello 4003252835"); + + // Implement behavior of the mocked object + when(phyProtocolMock.receive(anyInt())).thenReturn(corruptMsg).thenReturn(testMsg); + + // Run the test + assertDoesNotThrow(()->cProtocol.receive()); + + // verify a specified behavior + verify(phyProtocolMock, times(2)).receive(2000); + } + + @Test + void testCommandResponseOkWithTooShortMessage() throws IWProtocolException, IOException { + // Fill the message object that is going to be returned to the object-under-test + // with the message needed for this test case + testMsg = (PhyMsg)testMsg.parse("phy 7 cp comres 1 ok 0 4003252835"); + corruptMsg = (PhyMsg)corruptMsg.parse("phy 7 cp comres 1 ok 10 Hello 4003252835"); + + // Implement behavior of the mocked object + when(phyProtocolMock.receive(anyInt())).thenReturn(corruptMsg).thenReturn(testMsg); + + // Run the test + assertDoesNotThrow(()->cProtocol.receive()); + + // verify a specified behavior + verify(phyProtocolMock, times(2)).receive(2000); + } + protected long calcCRC(String data) { + // calculate string checksum + byte[] datab = data.getBytes(); + Checksum checksum = new CRC32(); + checksum.update(datab,0,datab.length); + return checksum.getValue(); + } +}