]> www.average.org Git - loctrkd.git/commitdiff
test: complete fuzzer unittest
authorEugene Crosser <crosser@average.org>
Wed, 15 Jun 2022 21:41:56 +0000 (23:41 +0200)
committerEugene Crosser <crosser@average.org>
Wed, 15 Jun 2022 21:41:56 +0000 (23:41 +0200)
test/common.py [new file with mode: 0644]
test/test_fuzz.py

diff --git a/test/common.py b/test/common.py
new file mode 100644 (file)
index 0000000..dccecfc
--- /dev/null
@@ -0,0 +1,48 @@
+""" Common housekeeping for tests that rely on daemons """
+
+from configparser import ConfigParser, SectionProxy
+from contextlib import closing
+from importlib import import_module
+from multiprocessing import Process
+from os import kill, unlink
+from signal import SIGINT
+from socket import AF_INET6, SOCK_DGRAM, SOL_SOCKET, SO_REUSEADDR, socket
+from tempfile import mkstemp
+from time import sleep
+from unittest import TestCase
+
+
+class TestWithServers(TestCase):
+    def setUp(self, *args: str) -> None:
+        with closing(socket(AF_INET6, SOCK_DGRAM)) as sock:
+            sock.bind(("", 0))
+            sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
+            freeport = sock.getsockname()[1]
+        _, self.tmpfilebase = mkstemp()
+        self.conf = ConfigParser()
+        self.conf["collector"] = {
+            "port": str(freeport),
+            "publishurl": "ipc://" + self.tmpfilebase + ".pub",
+            "listenurl": "ipc://" + self.tmpfilebase + ".pul",
+        }
+        self.children = []
+        for srvname in args:
+            if srvname == "collector":
+                kwargs = {"handle_hibernate": False}
+            else:
+                kwargs = {}
+            cls = import_module("gps303." + srvname, package=".")
+            p = Process(target=cls.runserver, args=(self.conf,), kwargs=kwargs)
+            p.start()
+            self.children.append((srvname, p))
+        sleep(1)
+
+    def tearDown(self) -> None:
+        sleep(1)
+        for srvname, p in self.children:
+            if p.pid is not None:
+                kill(p.pid, SIGINT)
+            p.join()
+            print(srvname, "terminated with return code", p.exitcode)
+        for sfx in (".pub", ".pul"):
+            unlink(self.tmpfilebase + sfx)
index 6dea449af255ae5c8ad8eabbe5d657880543ab34..d937c95a3dd216a064e2718ce38dbdf68b391b23 100644 (file)
@@ -1,18 +1,20 @@
 """ Send junk to the collector """
 
 from random import Random
-from socket import getaddrinfo, socket, AF_INET6, SOCK_STREAM
-from unittest import TestCase
+from socket import getaddrinfo, socket, AF_INET6, MSG_DONTWAIT, SOCK_STREAM
+import unittest
+from .common import TestWithServers
 
 REPEAT: int = 1000000
 
 
-class Fuzz(TestCase):
-    def setUp(self) -> None:
+class Fuzz(TestWithServers):
+    def setUp(self, *args: str) -> None:
+        super().setUp("collector")
         self.rnd = Random()
         for fam, typ, pro, cnm, skadr in getaddrinfo(
             "::1",
-            4303,
+            self.conf.getint("collector", "port"),
             family=AF_INET6,
             type=SOCK_STREAM,
         ):
@@ -22,9 +24,19 @@ class Fuzz(TestCase):
 
     def tearDown(self) -> None:
         self.sock.close()
+        print("finished fuzzing")
+        super().tearDown()
 
     def test_fuzz(self) -> None:
         for _ in range(REPEAT):
             size = self.rnd.randint(1, 5000)
             buf = self.rnd.randbytes(size)
             self.sock.send(buf)
+            try:
+                self.sock.recv(4096, MSG_DONTWAIT)
+            except BlockingIOError:
+                pass
+
+
+if __name__ == "__main__":
+    unittest.main()