Skip to content
Snippets Groups Projects
Commit 292529a5 authored by Daniel Yang's avatar Daniel Yang
Browse files

Added default value for missing keys, by using 'int', dict returns 0 if the...

Added default value for missing keys, by using 'int', dict returns 0 if the key does not exist, and added more tests
parent 7afcf828
No related branches found
No related tags found
No related merge requests found
......@@ -98,7 +98,7 @@ def packet_handler(packet):
port_check(packet)
destination_check(packet)
malformed_check(packet)
syn_flood_detection(packet) # Should be under the 'if TCP in packet' clause?
syn_flood_detection(packet)
arp_spoofing(packet)
# Allows unit testing of the dict
......
......@@ -6,6 +6,8 @@ from scapy.layers.l2 import Ether
from src.packet_capturing import packet_handler, get_dict, dict_clear
EXPECTED_EMPTY_DICT = "Expected empty dict"
EXPECTED_VALUE_DICT = "Expected packet's IP in the dict"
class TestPacketCapturing(unittest.TestCase):
......@@ -23,7 +25,7 @@ class TestPacketCapturing(unittest.TestCase):
packet_handler(packet)
mock_print.assert_any_call("Packet captured: 100.84.6.141 -> 192.168.1.1")
self.assertEqual({'100.84.6.141' : 1}, get_dict() , "Expected packet's IP in the dict")
self.assertEqual({'100.84.6.141' : 1}, get_dict() , EXPECTED_VALUE_DICT)
def test_ip_spoofing(self):
# Starts with 169.254, it's a suspicious packet
......@@ -34,7 +36,7 @@ class TestPacketCapturing(unittest.TestCase):
mock_print.assert_any_call("Possible IP spoofing using private networks detected.")
# Has no TCP flags, therefore dict must be empty
self.assertEqual({}, get_dict(), "Expected empty dict")
self.assertEqual({}, get_dict(), EXPECTED_EMPTY_DICT)
def test_syn_fin(self):
# Has S and F flags, which is also known as SYN-FIN, which is a malicious packet signature
......@@ -45,7 +47,7 @@ class TestPacketCapturing(unittest.TestCase):
packet_handler(packet)
mock_print.assert_any_call("Malicious packet detected: SYN-FIN combination.")
self.assertEqual({}, get_dict(), "Expected empty dict")
self.assertEqual({}, get_dict(), EXPECTED_EMPTY_DICT)
def test_null_packet(self):
packet = IP(src = "100.84.6.141", dst = "192.168.1.1") / TCP(dport = 80, flags = 0) / Ether(src="00:11:22:33:44:55", dst="ff:ff:ff:ff:ff:ff")
......@@ -54,7 +56,7 @@ class TestPacketCapturing(unittest.TestCase):
packet_handler(packet)
mock_print.assert_any_call("Malicious null packet found.")
self.assertEqual(get_dict(), {}, "Expected empty dict")
self.assertEqual(get_dict(), {}, EXPECTED_EMPTY_DICT)
def test_port_dest_check(self):
packet = IP(src="100.84.6.141", dst="192.168.1.1") / TCP(dport = 0, sport = 200, flags="F") / Ether(src="00:11:22:33:44:55", dst="ff:ff:ff:ff:ff:ff")
......@@ -63,7 +65,7 @@ class TestPacketCapturing(unittest.TestCase):
packet_handler(packet)
mock_print.assert_any_call("Illegal packet with source or destination port 0.")
self.assertEqual(get_dict(), {}, "Expected empty dict")
self.assertEqual(get_dict(), {}, EXPECTED_EMPTY_DICT)
def test_port_src_check(self):
packet = IP(src="100.84.6.141", dst="192.168.1.1") / TCP(dport=80, sport=0, flags="F") / Ether(src="00:11:22:33:44:55", dst="ff:ff:ff:ff:ff:ff")
......@@ -72,7 +74,7 @@ class TestPacketCapturing(unittest.TestCase):
packet_handler(packet)
mock_print.assert_any_call("Illegal packet with source or destination port 0.")
self.assertEqual(get_dict(), {}, "Expected empty dict")
self.assertEqual(get_dict(), {}, EXPECTED_EMPTY_DICT)
if __name__ == '__main__':
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment