Browse Source

Merge branch 'merge_projects' of leon.boeck/ID2T-toolkit-BotnetTraffic into master

Carlos Garcia 6 years ago
parent
commit
a4c7158433
2 changed files with 33 additions and 8 deletions
  1. 15 3
      code/Test/TestUtil.py
  2. 18 5
      code/Test/test_determinism_mmcomm.py

+ 15 - 3
code/Test/TestUtil.py

@@ -1,5 +1,17 @@
 #!/usr/bin/python3
 
+import scapy.main
+
+# This import is needed, otherwise scapy throws warnings. When reading a pcap scapy will not
+# find the layer-type 1 (ethernet) because it has not been loaded at the time. To circumvent
+# this we explicitely load the ethernet-type here.
+# For the curious guys and gals, the exact error message is:
+# "RawPcapReader: unknown LL type [%i]/[%#x]. Using Raw packets" % the_missing_ll_number
+# If the same problems happens with other ll-types feel free to load ALL imaginable layers
+# with the following line.
+# import scapy.layers.all
+import scapy.layers.l2
+
 import scapy.packet
 import scapy.utils
 import shlex
@@ -15,13 +27,13 @@ class PcapComparator:
 
     def compare_captures(self, packetsA, packetsB):
         if len(packetsA) != len(packetsB):
-            self.fail("Both pcap's have to have the same amount of packets")
+            self.fail("Both pcaps have to have the same amount of packets")
 
         for i in range(len(packetsA)):
             p, p2 = packetsA[i], packetsB[i]
 
             if abs(p.time - p2.time) > (10 ** -7):
-                self.fail("Packets no %i in the pcap's don't appear at the same time" % (i + 1))
+                self.fail("Packets no %i in the pcaps don't appear at the same time" % (i + 1))
             self.compare_packets(p, p2, i + 1)
 
     def compare_packets(self, p: scapy.packet.BasePacket, p2: scapy.packet.BasePacket, packet_number: int):
@@ -114,7 +126,7 @@ class ID2TExecution:
         if file not in self.generated_files:
             raise ValueError("%s is not generated by id2t" % file)
         if file not in self.keep_files:
-            self.keep_files.append()
+            self.keep_files.append(file)
 
     def get_kept_files(self):
         self._require_run()

+ 18 - 5
code/Test/test_determinism_mmcomm.py

@@ -65,14 +65,18 @@ class PcapComparison(unittest.TestCase):
     def test_determinism(self):
         self.print_warning("Conducting test for determinism of Membership Management Communication Attack:\n")
         input_pcap = os.environ.get(self.PCAP_ENVIRONMENT_VALUE, self.DEFAULT_PCAP)
-        seed = os.environ.get(self.SEED_ENVIRONMENT_VALUE, self.DEFAULT_SEED)
+        seed = os.environ.get(self.SEED_ENVIRONMENT_VALUE, None)
 
         if self.id2t_params is None:
             self.id2t_params = self.random_id2t_params()
 
+        use_random_seeds = not bool(seed)
+
         for i, params in enumerate(self.id2t_params):
             self.print_warning("Test round %d:" % (i+1))
             self.print_warning("=================================")
+            if use_random_seeds:
+                seed = random.randint(0, 0x7FFFFFFF)
             self.do_test_round(input_pcap, seed, params)
             self.print_warning()
 
@@ -102,7 +106,14 @@ class PcapComparison(unittest.TestCase):
                         self.compare_pcaps(generated_pcap, pcap)
                     except AssertionError as e:
                         execution.keep_file(pcap)
-                        self.executions[-2].keep_file(generated_pcap)
+                        for ex in self.executions:
+                            try:
+                                ex.keep_file(generated_pcap)
+                            except ValueError:
+                                pass
+
+                        e.args += tuple(("Command was: %s" % execution.get_run_command(additional_params),))
+                        e.args += tuple(("Files are: %s, %s" % (generated_pcap, pcap),))
                         raise e
             else:
                 generated_pcap = pcap
@@ -120,9 +131,11 @@ class PcapComparison(unittest.TestCase):
 
         self.print_warning("Done")
 
-        kept = [file for file in id2t_run.get_kept_files() for id2t_run in self.executions]
-        if kept:
-            self.print_warning("The following files have been kept: " + ", ".join(kept))
+        if any(e.get_kept_files() for e in self.executions):
+            self.print_warning("The following files have been kept:")
+            for e in self.executions:
+                for file in e.get_kept_files():
+                    self.print_warning(file)
 
     def compare_pcaps(self, one: str, other: str):
         PcapComparator().compare_files(self.ID2T_PATH + "/" + one, self.ID2T_PATH + "/" + other)