Browse Source

add GenericTest Class

add param passthrough
add current coverage comment
add test_default
update todos
Jens Keim 6 years ago
parent
commit
5700b0663b
2 changed files with 38 additions and 27 deletions
  1. 18 0
      code/Test/GenericTest.py
  2. 20 27
      code/Test/test_SMBLoris.py

+ 18 - 0
code/Test/GenericTest.py

@@ -0,0 +1,18 @@
+import random
+import unittest
+
+from ID2TLib.Controller import Controller
+from Test.Lib import test_pcap, get_sha256, clean_up
+
+
+class GenericTest(unittest.TestCase):
+
+    def generic_test(self, attack_args, sha_checksum, seed=5, cleanup=True, pcap=test_pcap, flag_write_file=False,
+                     flag_recalculate_stats=False, flag_print_statistics=False):
+        random.seed(seed)
+        controller = Controller(pcap_file_path=pcap, do_extra_tests=False)
+        controller.load_pcap_statistics(flag_write_file, flag_recalculate_stats, flag_print_statistics)
+        controller.process_attacks(attack_args)
+        self.assertEqual(get_sha256(controller.pcap_dest_path), sha_checksum)
+        if cleanup:
+            clean_up(controller)

+ 20 - 27
code/Test/test_SMBLoris.py

@@ -1,47 +1,40 @@
-import random
 import unittest
 import unittest.mock as mock
 
-from ID2TLib.Controller import *
-from Test.Lib import test_pcap, test_pcap_ips, get_sha256, clean_up
+from Test.GenericTest import GenericTest
+from Test.Lib import test_pcap_ips
 
-# FIXME: create new hashes
+# FIXME: create new hashes if new test.pcap is used
+sha_default = 'cd02bcc6376f4701d13384cbec6e210ea99308dca5d08f58edd3ab190cd50bf2'
 sha_one_attacker = '887226f047456608c5c8746c91d387ffa35777650f083564e0104e381155c58e'
 sha_one_hundred_attackers = '3cab29e73bc048ea7cbffde51f5637fe00256d896a6788db27fbec3804f19cc9'
 sha_ips_in_pcap = '4a072c57bf97c8543305964c4df4de5f010df7da22fc8f414ccbbf88b962ae86'
 
+"""
+CURRENT COVERAGE
+Name                             Stmts   Miss  Cover   Missing (lines)
+---------------------------------------------------------------------------
+Attack/SMBLorisAttack.py           128      5    96%   60, 73, 78, 155, 188
+"""
+# TODO: get 100% coverage
 
-class UnitTestSMBLoris(unittest.TestCase):
+
+class UnitTestSMBLoris(GenericTest):
+
+    def test_default(self):
+        # FIXME: maybe use another seed
+        self.generic_test([['SMBLorisAttack']], sha_default)
 
     def test_one_attacker(self):
-        random.seed(5)
-        controller = Controller(pcap_file_path=test_pcap, do_extra_tests=False)
-        controller.load_pcap_statistics(False, False, False)
-        controller.process_attacks([['SMBLorisAttack', 'ip.src=192.168.1.240', 'ip.dst=192.168.1.210']])
-        self.assertEqual(get_sha256(controller.pcap_dest_path), sha_one_attacker)
-        clean_up(controller)
+        self.generic_test([['SMBLorisAttack', 'ip.src=192.168.1.240', 'ip.dst=192.168.1.210']], sha_one_attacker)
 
     def test_ips_in_pcap(self):
-        # TODO: implement test
         ip_src = 'ip.src='+test_pcap_ips[0]
         ip_dst = 'ip.dst='+test_pcap_ips[1]
-        random.seed(5)
-        controller = Controller(pcap_file_path=test_pcap, do_extra_tests=False)
-        controller.load_pcap_statistics(False, False, False)
-        controller.process_attacks([['SMBLorisAttack', ip_src, ip_dst]])
-        self.assertEqual(get_sha256(controller.pcap_dest_path), sha_ips_in_pcap)
-        clean_up(controller)
-
-    #def five_minutes(self):
-        # TODO: implement test
+        self.generic_test([['SMBLorisAttack', ip_src, ip_dst]], sha_ips_in_pcap)
 
     def test_one_hundred_attackers(self):
-        random.seed(5)
-        controller = Controller(pcap_file_path=test_pcap, do_extra_tests=False)
-        controller.load_pcap_statistics(False, False, False)
-        controller.process_attacks([['SMBLorisAttack', 'ip.dst=192.168.1.210', 'attackers.count=100']])
-        self.assertEqual(get_sha256(controller.pcap_dest_path), sha_one_hundred_attackers)
-        clean_up(controller)
+        self.generic_test([['SMBLorisAttack', 'ip.dst=192.168.1.210', 'attackers.count=100']], sha_one_hundred_attackers)
 
 
 if __name__ == '__main__':