From 13c506a17748b62636ccfc07b6d0c49171a6831d Mon Sep 17 00:00:00 2001 From: jeboog Date: Tue, 23 Mar 2021 11:35:38 -0400 Subject: [PATCH] Script for Testing Purposes --- test.py | 648 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 648 insertions(+) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 0000000..6197ba1 --- /dev/null +++ b/test.py @@ -0,0 +1,648 @@ +import Python.connectionObject as connectionObject +import Python.client as client +import time +import Python.packet as packet +import random +import Python.nat as nat +import Python.dddController as dddController +import Python.dddPortMapping as dddPortMapping +import Python.attacker as attacker +import Python.server as server +import Python.router as router + + +def wait(): + for i in range(10): + time.sleep(0.1) + print(".", end=' ') + + +start_time = time.time() + + +# Testing for the Client Class +def test_clientObject(): + print("") + print("----------------------Testing Client class-------------------------") + + def testNormalTraffic(): + testConnection2 = connectionObject.connectionObject(2000, 4, + None, "2.0.0.0", None, + "10.0.0.0", None, "C-T") + simClient = client.client(testConnection2, "1.0.0.0", + "4.0.0.0", "3.0.0.0", "5.0.0.0", 2, + None, 10, 20, + 20, 10000, "C-") + testConnection2.device1 = simClient + simClient.updateTime() + simClient.updateTime() + simClient.updateTime() + simClient.updateTime() + if(testConnection2.packetsInTransit[0][0].dstIP == "4.0.0.0" + and testConnection2.packetsInTransit[1][0].dstIP == "3.0.0.0"): + print("Normal Traffic Generation is Successful") + return 1 + else: + print("Normal Traffic Generation is not Successful") + return 0 + + def testCreateReponse(): + testConnection2 = connectionObject.connectionObject(2000, 4, + None, "2.0.0.0", None, + "10.0.0.0", None, "C-T") + simClient = client.client(testConnection2, "1.0.0.0", + "4.0.0.0", "3.0.0.0", "5.0.0.0", 2, + None, 10, 20, + 100, 10000, "C-") + testConnection2.device1 = simClient + testPacket = packet.packet("1.0.0.0", 1, "2.0.0.0", 2, + "SYN-ACK", random.randint(0, 10000), 0, + "A-1", 10, "1.0.0.0") + synPacket = packet.packet("1.0.0.0", 1, "2.0.0.0", 2, + "SYN-ACK", random.randint(0, 10000), 24, + "A-2", 10, "1.0.0.0") + simClient.expectedSYNACKs[("1.0.0.0", 24)] = 0 + simClient.receivePacket(testPacket) + simClient.receivePacket(synPacket) + simClient.updateTime() + simClient.updateTime() + simClient.updateTime() + simClient.updateTime() + if((testConnection2.packetsInTransit[1][0].packetType == "RST" and testConnection2.packetsInTransit[1][0].packetID == "C-2") + and (testConnection2.packetsInTransit[2][0].packetType == "ACK" and testConnection2.packetsInTransit[2][0].packetID == "C-3")): + print("Creating Responses is Successful") + return 1 + else: + print("Creating Responses is not Successful") + return 0 + + returnVal = testNormalTraffic() + testCreateReponse() + if(returnVal == 2): + print("Test was Successful for Client Class") + else: + print("Test was Not Successful for Client Class") + + +# Testing for the Connection Object Class +def test_connectionObject(): + print("") + print("----------------Testing ConnectionObject class---------------------") + testPacket = packet.packet("1.0.0.0", 1, "2.0.0.0", 2, + "SYN", random.randint(0, 10000), 0, + "A-1", 10, "1.0.0.0") + simClient = client.client(None, "2.0.0.0", + "4.0.0.0", "3.0.0.0", "5.0.0.0", 2, + None, 20, 40, + 20, None, "C-") + testConnection = connectionObject.connectionObject(2000, 2, + None, "1.0.0.0", simClient, + "2.0.0.0", None, "C-T") + + testConnection.transferPacketOut(testPacket) + testConnection.updateTime() + testConnection.updateTime() + if(simClient.receiveBuffer[0].packetID == "A-1"): + print("Test Successful for connectionObject") + else: + print("Test Was Not Successful") + print(simClient.receiveBuffer[0]) + + +# Testing for the NAT Class +def test_natObject(): + print("") + print("-----------------------Testing nat class---------------------------") + + def testClientToNat(): + testConnection2 = connectionObject.connectionObject(2000, 2, + None, "10.0.0.0", None, + "2.0.0.0", None, "C-T") + simNat = nat.nat(testConnection2, None, + 1000, None, 10, 10000, 10000) + + testConnection2.device2 = simNat + testPacket = packet.packet("2.0.0.0", 1, "4.0.0.0", 2, + "SYN", random.randint(0, 10000), 0, + "C-1", 10, "1.0.0.0") + + simNat.receivePacket(testPacket) + simNat.updateTime() + simNat.updateTime() + if(testConnection2.packetsInTransit[0][0].packetID == "C-1" + and (testConnection2.packetsInTransit[0][0].srcPortNumber is not 1 + and testConnection2.packetsInTransit[0][0].srcIP == "10.0.0.0")): + print("Client to Nat Functionality is Successful") + return 1 + else: + print("Client to Nat Functionality is not Successful") + return 0 + + def testRouterToNat(): + testConnection = connectionObject.connectionObject(2000, 2, + None, "10.0.0.0", None, + "2.0.0.0", None, "C-T") + simNat = nat.nat(None, testConnection, + 1000, None, 10, 10000, 10000) + simNat.openConnections[("10.0.0.0", 4)] = ("2.0.0.0", 20) + testConnection.device1 = simNat + testPacket = packet.packet("4.0.0.0", 1, "10.0.0.0", 4, + "SYN-ACK", random.randint(0, 10000), 0, + "R-1", 10, "4.0.0.0") + testPacket2 = packet.packet("4.0.0.0", 1, "10.0.0.0", 5, + "SYN-ACK", random.randint(0, 10000), 0, + "R-2", 10, "4.0.0.0") + simNat.receivePacket(testPacket) + simNat.receivePacket(testPacket2) + simNat.updateTime() + simNat.updateTime() + if(testConnection.packetsInTransit[0][0].packetID == "R-1" + and (testConnection.packetsInTransit[0][0].dstPortNumber == 20 + and testConnection.packetsInTransit[0][0].dstIP == "2.0.0.0") + and len(testConnection.packetsInTransit) == 1): + print("Router to Nat Functionality is Successful") + return 1 + else: + print("Rotuer to Nat Functionality is not Successful") + return 0 + + val = testRouterToNat() + testClientToNat() + if(val != 2): + print("Test was unsuccessful") + else: + print("Test was Successful") + + +# Testing for the DDD Controller Class +def test_DDDController(): + print("") + print("------------------Testing DDD Controller class----------------------") + + def testGeneratePorts(): + simDddController = dddController.dddController(5, + 2, 5, + None) + oldPorts = simDddController.currentUsablePorts + newPorts = simDddController.generatePorts(5, oldPorts) + for i in newPorts: + for j in oldPorts: + if(i == j): + print("Generating Ports Failed") + return 0 + print("Generating Ports Succeeded") + return 1 + + def testChangePorts(): + successCounter = 0 + simDddController = dddController.dddController(5, + 2, 5, + None) + oldPorts = simDddController.currentUsablePorts + simDddController.updateTime() + simDddController.updateTime() + simDddController.updateTime() + simDddController.updateTime() + simDddController.updateTime() + if((oldPorts == simDddController.oldPorts and oldPorts not in simDddController.currentUsablePorts) + and (simDddController.timeToFullChange == 7 and simDddController.toNextChange == 10)): + print("Ports were Changed Successfully") + successCounter += 1 + else: + print("Ports were not Changed Successfully") + simDddController.updateTime() + simDddController.updateTime() + if(simDddController.timeToFullChange == 0 and len(simDddController.oldPorts) == 0): + print("Old Ports Successfully Deleted After Overlap Time") + successCounter += 1 + else: + print("Old Ports Weren't Successfully Deleted After Overlap Time") + if(successCounter == 2): + return 1 + else: + return 0 + + returnVal = testGeneratePorts() + testChangePorts() + if(returnVal == 2): + print("Test was Successful") + else: + print("Test was not Successful") + + +# Testing for the DDD Mapper Class +def test_DDDMapper(): + print("") + print("-------------------Testing DDD Mapping class-----------------------") + + def testIntialization(): + simDddController = dddController.dddController(5, + 2, 5, + None) + for i in simDddController.dddPortMapper.portMappings: + if(i[0] != 0): + print("Intialization of Mapped Ports is not Successful") + return 0 + print("Intialization of Mapped Ports is Successful") + return 1 + + def testAvailablePort(): + simDddController = dddController.dddController(5, + 2, 5, + None) + openPort = simDddController.dddPortMapper.getAvailableOpenPort + for i in simDddController.dddPortMapper.portMappings: + if(i[1] == openPort and i[1] != 0): + print("Getting Available Open Port Failed") + return 0 + print("Getting Available Open Port is Successful") + return 1 + + def testAddandDeletePorts(): + success = 0 + simDDDPortMapper = dddPortMapping.dddPortMapping([2, 5]) + simDDDPortMapper.addNewPorts([6]) + if([0, 6] in simDDDPortMapper.portMappings): + print("New Port Added Successfully") + success += 1 + else: + print("Adding New Port Failed") + + simDDDPortMapper.deletePorts([6]) + if([0, 6] not in simDDDPortMapper.portMappings): + print("Deletion of Ports is Successful") + success += 1 + else: + print("Deletion of Ports Failed") + + if(success == 2): + return 1 + else: + return 0 + + returnVal = testIntialization() + testAvailablePort() + testAddandDeletePorts() + if(returnVal == 3): + print("Test was Successful") + else: + print("Test was not Successful") + + +# Testing for the Attacker Class +def test_attackerObject(): + print("") + print("-------------------Testing Attacker class-----------------------") + + def testReceivePacket(): + success = 0 + testConnectionAR = connectionObject.connectionObject(2000, 2, + None, "8.0.0.0", None, + "4.0.0.0", None, "A-R") + testConnectionAS = connectionObject.connectionObject(2000, 2, + None, "8.0.0.0", None, + "5.0.0.0", None, "A-R") + simAttackerAmpNonSmart = attacker.attacker(testConnectionAS, + testConnectionAR, + "10.0.0.0", "5.0.0.0", None, + 10, 10000, + "8.0.0.0", "A-", 0, 1) + testPacket = packet.packet("4.0.0.0", 1, "10.0.0.0", 4, + "PONG", random.randint(0, 10000), 0, + "R-1", 10, "4.0.0.0") + simAttackerAmpNonSmart.receivePacket(testPacket) + if(simAttackerAmpNonSmart.openPort == testPacket.srcPortNumber): + success += 1 + testPacket2 = packet.packet("4.0.0.0", 1, "10.0.0.0", 4, + "PONG-RST", random.randint(0, 10000), 0, + "R-1", 10, "4.0.0.0") + simAttackerAmpNonSmart.receivePacket(testPacket2) + if(simAttackerAmpNonSmart.openPort is None): + success += 1 + if(success == 2): + print("Attacker Class Receives Packets Successfully") + return 1 + else: + print("Attacker Class Doesn't Receive Packets Successfully") + return 0 + + def testGenerateSynAmpNonSmart(): + testConnectionAR = connectionObject.connectionObject(2000, 2, + None, "8.0.0.0", None, + "4.0.0.0", None, "A-R") + testConnectionAS = connectionObject.connectionObject(2000, 2, + None, "8.0.0.0", None, + "5.0.0.0", None, "A-R") + simAttackerAmpNonSmart = attacker.attacker(testConnectionAS, + testConnectionAR, + "10.0.0.0", "5.0.0.0", None, + 10, 10000, + "8.0.0.0", "A-", 0, 1) + testConnectionAR.device1 = simAttackerAmpNonSmart + testConnectionAS.device1 = simAttackerAmpNonSmart + simAttackerAmpNonSmart.updateTime() + + if(testConnectionAS.packetsInTransit[0][0].dstIP == "5.0.0.0" and testConnectionAS.packetsInTransit[0][0].packetType == "SYN"): + print("Generating a SYN Packt for Non-Smart Attacker with Amplification is a Success") + return 1 + else: + print("Generating a SYN Packt for Non-Smart Attacker with Amplification is not a Success") + return 0 + + def testGenerateSynAmpSmart(): + testConnectionAR = connectionObject.connectionObject(2000, 2, + None, "8.0.0.0", None, + "4.0.0.0", None, "A-R") + testConnectionAS = connectionObject.connectionObject(2000, 2, + None, "8.0.0.0", None, + "5.0.0.0", None, "A-R") + simAttackerAmpSmart = attacker.attacker(testConnectionAS, + testConnectionAR, + "10.0.0.0", "5.0.0.0", None, + 10, 10000, + "8.0.0.0", "A-", 1, 1) + testConnectionAR.device1 = simAttackerAmpSmart + testConnectionAS.device1 = simAttackerAmpSmart + simAttackerAmpSmart.updateTime() + testPacket = packet.packet("4.0.0.0", 1, "10.0.0.0", 4, + "PONG", random.randint(0, 10000), 0, + "R-1", 10, "4.0.0.0") + simAttackerAmpSmart.receivePacket(testPacket) + simAttackerAmpSmart.updateTime() + if((testConnectionAR.packetsInTransit[0][0].packetType == "PING" and testConnectionAR.packetsInTransit[0][0].dstIP == "2.0.0.0") + and (testConnectionAS.packetsInTransit[0][0].packetType == "SYN" and testConnectionAS.packetsInTransit[0][0].dstIP == "5.0.0.0")): + print("Generating Packets for Smart Attacker with Amplification is a Success") + return 1 + else: + print("Generating Packets for Smart Attacker with Amplification is not a Success") + return 0 + + def testGenerateSynNonAmpNonSmart(): + testConnectionAR = connectionObject.connectionObject(2000, 2, + None, "8.0.0.0", None, + "4.0.0.0", None, "A-R") + testConnectionAS = connectionObject.connectionObject(2000, 2, + None, "8.0.0.0", None, + "5.0.0.0", None, "A-R") + simAttackerNonAmpNonSmart = attacker.attacker(testConnectionAS, + testConnectionAR, + "10.0.0.0", "5.0.0.0", None, + 10, 10000, + "8.0.0.0", "A-", 0, 0) + testConnectionAR.device1 = simAttackerNonAmpNonSmart + testConnectionAS.device1 = simAttackerNonAmpNonSmart + simAttackerNonAmpNonSmart.updateTime() + if(testConnectionAR.packetsInTransit[0][0].srcIP == "8.0.0.0" and testConnectionAR.packetsInTransit[0][0].dstIP == "10.0.0.0" and testConnectionAR.packetsInTransit[0][0].packetType == "SYN"): + print("Generating Packets for Non-Smart Attacker with Non-Amplification is a Success") + return 1 + else: + print("Generating Packets for Non-Smart Attacker with Non-Amplification is not a Success") + return 0 + + def testGenerateSynNonAmpSmart(): + testConnectionAR = connectionObject.connectionObject(2000, 2, + None, "8.0.0.0", None, + "4.0.0.0", None, "A-R") + testConnectionAS = connectionObject.connectionObject(2000, 2, + None, "8.0.0.0", None, + "5.0.0.0", None, "A-R") + simAttackerNonAmpSmart = attacker.attacker(testConnectionAS, + testConnectionAR, + "10.0.0.0", "5.0.0.0", None, + 10, 10000, + "8.0.0.0", "A-", 1, 0) + testConnectionAR.device1 = simAttackerNonAmpSmart + testConnectionAS.device1 = simAttackerNonAmpSmart + simAttackerNonAmpSmart.updateTime() + testPacket = packet.packet("4.0.0.0", 1, "10.0.0.0", 4, + "PONG", random.randint(0, 10000), 0, + "R-1", 10, "4.0.0.0") + simAttackerNonAmpSmart.receivePacket(testPacket) + simAttackerNonAmpSmart.updateTime() + + if((testConnectionAR.packetsInTransit[0][0].packetType == "PING" and testConnectionAR.packetsInTransit[0][0].dstIP == "2.0.0.0") + and (testConnectionAR.packetsInTransit[1][0].packetType == "SYN" and testConnectionAR.packetsInTransit[1][0].dstIP == "5.0.0.0")): + print("Generating Packets for Smart Attacker with Non-Amplification is a Success") + return 1 + else: + print("Generating Packets for Smart Attacker with Non-Amplification is not a Success") + return 0 + + returnVal = testReceivePacket() + testGenerateSynAmpNonSmart() + testGenerateSynAmpSmart() + testGenerateSynNonAmpNonSmart() + testGenerateSynNonAmpSmart() + if(returnVal == 4): + print("Test was Successful") + else: + print("Test was not Successful") + + +# Testing for the Server Class +def test_serverObject(): + print("") + print("----------------------Testing Server class--------------------------") + + def testCurateResponse(): + testConnection = connectionObject.connectionObject(2000, 2, + None, "4.0.0.0", None, + "8.0.0.0", None, "A-R") + simNServer = server.Server(testConnection, "4.0.0.0", + 20, 2, + 2, None, + 10, 20, 10000, + "N-",) + + testConnection.device1 = simNServer + testPacket = packet.packet("6.0.0.0", 1, "4.0.0.0", 4, + "SYN", random.randint(0, 10000), 0, + "A-1", 10, "6.0.0.0") + simNServer.receivePacket(testPacket) + simNServer.updateTime() + simNServer.updateTime() + if(testConnection.packetsInTransit[0][0].dstIP == "6.0.0.0" and testConnection.packetsInTransit[0][0].packetType == "SYN-ACK" and testConnection.packetsInTransit[0][0].packetID == "N-1"): + print("Response Packet was Generated Successfully") + return 1 + else: + print("Response Packet was not Generated Successfully") + return 0 + + def testResendSynACK(): + testConnection = connectionObject.connectionObject(2000, 2, + None, "4.0.0.0", None, + "8.0.0.0", None, "A-R") + simNServer = server.Server(testConnection, "4.0.0.0", + 20, 2, + 2, None, + 10, 20, 10000, + "N-",) + + testConnection.device1 = simNServer + testPacket = packet.packet("6.0.0.0", 1, "4.0.0.0", 4, + "SYN", random.randint(0, 10000), 0, + "A-1", 10, "6.0.0.0") + simNServer.receivePacket(testPacket) + simNServer.updateTime() + simNServer.updateTime() + simNServer.updateTime() + if((testConnection.packetsInTransit[0][0].sequenceNumber + 1) == testConnection.packetsInTransit[1][0].sequenceNumber and testConnection.packetsInTransit[1][0].packetID == "N-3"): + print("Resend Syn-Ack's were Generated Successfully") + return 1 + else: + print("Resend Syn-Ack's were not Generated Successfully") + return 0 + + returnVal = testCurateResponse() + testResendSynACK() + if(returnVal == 2): + print("Test was Successful") + else: + print("Test was not Successful") + + +# Testing for the Router Class +def test_routerObject(): + print("") + print("----------------------Testing Router class--------------------------") + + def testNoDDD(): + testConnection = connectionObject.connectionObject(2000, 2, + None, "2.0.0.0", None, + "4.0.0.0", None, "R-T") + + simRouter = router.router(1000, False, "2.0.0.0", 0, + 1, testConnection, + None, + None, + None, None, + None, None, + 20, 10000, 10000, + 10000, 10, "R-") + testConnection.device1 = simRouter + testPacket = packet.packet("6.0.0.0", 1, "10.0.0.0", 4, + "SYN-ACK", random.randint(0, 10000), 0, + "R-1", 10, "6.0.0.0") + simRouter.receivePacket(testPacket) + simRouter.updateTime() + simRouter.updateTime() + if(testConnection.packetsInTransit[0][0].packetID == "R-1" and testConnection.packetsInTransit[0][0].dstIP == "10.0.0.0"): + print("Router Functions Successfully with No DDD") + return 1 + else: + print("Router doesn't Function Successfully with No DDD") + return 0 + + def testDDDtoClient(): + success = 0 + + def testDrop(): + testConnection = connectionObject.connectionObject(2000, 2, + None, "2.0.0.0", None, + "4.0.0.0", None, "R-T") + simDddController = dddController.dddController(20, + 2, 5, + None) + simRouter = router.router(1000, True, "2.0.0.0", 0, + 1, testConnection, + None, + None, + None, simDddController, + simDddController.dddPortMapper, None, + 20, 10000, 10000, + 10000, 10, "R-") + testConnection.device1 = simRouter + testPacket = packet.packet("6.0.0.0", 1, "10.0.0.0", 4, + "SYN-ACK", random.randint(0, 10000), 0, + "R-1", 10, "6.0.0.0") + simRouter.receivePacket(testPacket) + simRouter.updateTime() + simRouter.updateTime() + simRouter.updateTime() + if(len(testConnection.packetsInTransit) == 0): + return 1 + else: + return 0 + + def testThrough(): + testConnection = connectionObject.connectionObject(2000, 2, + None, "2.0.0.0", None, + "4.0.0.0", None, "R-T") + simDddController = dddController.dddController(20, + 2, 5, + None) + simRouter = router.router(1000, True, "2.0.0.0", 0, + 1, testConnection, + None, + None, + None, simDddController, + simDddController.dddPortMapper, None, + 20, 10000, 10000, + 10000, 10, "R-") + testConnection.device1 = simRouter + simDddController.dddPortMapper.portMappings.append([2, 4]) + testPacket = packet.packet("6.0.0.0", 1, "10.0.0.0", 4, + "SYN-ACK", random.randint(0, 10000), 0, + "R-1", 10, "6.0.0.0") + simRouter.receivePacket(testPacket) + simRouter.updateTime() + simRouter.updateTime() + simRouter.updateTime() + if(len(testConnection.packetsInTransit) != 0): + return 1 + else: + return 0 + success = testDrop() + testThrough() + if(success == 2): + print("Packets can be Successfully Dropped and Sent from the Router to the Client With the DDD On") + return 1 + else: + print("Packets can not beSuccessfully Dropped and Sent from the Router to the Client With the DDD On") + return 0 + + def testDDDtoInternet(): + testConnection = connectionObject.connectionObject(2000, 2, + None, "2.0.0.0", None, + "3.0.0.0", None, "R-T") + simDddController = dddController.dddController(20, + 2, 5, + None) + simRouter = router.router(1000, True, "2.0.0.0", 0, + 1, None, + testConnection, + None, + None, simDddController, + simDddController.dddPortMapper, None, + 20, 10000, 10000, + 10000, 10, "R-") + testConnection.device1 = simRouter + testPacket = packet.packet("10.0.0.0", 1, "3.0.0.0", 4, + "SYN", random.randint(0, 10000), 0, + "C-1", 10, "10.0.0.0") + simRouter.receivePacket(testPacket) + simRouter.updateTime() + simRouter.updateTime() + simRouter.updateTime() + if(len(testConnection.packetsInTransit) > 0): + print("Packets can be Successfully Dropped and Sent from the Router to the Servers With the DDD On") + return 1 + else: + print("Packets can not be Successfully Dropped and Sent from the Router to the Servers With the DDD On") + return 0 + + returnVal = testNoDDD() + testDDDtoClient() + testDDDtoInternet() + if(returnVal == 3): + print("Test was Successful") + else: + print("Test was not Successful") + + +test_clientObject() +wait() +test_connectionObject() +wait() +test_natObject() +wait() +test_DDDController() +wait() +test_DDDMapper() +wait() +test_attackerObject() +wait() +test_serverObject() +wait() +test_routerObject() +end_time = time.time() +print("") +print("") +print("Testing completed after " + str(end_time - start_time) + " seconds")