X-Git-Url: http://average.org/gitweb/?a=blobdiff_plain;f=test%2Ftest_fuzz.py;h=0456fda0352ddede2b315fb351bf431b500929ba;hb=f075dbd6eea0e7326d68fd15a103d668baf67c10;hp=6dea449af255ae5c8ad8eabbe5d657880543ab34;hpb=b1dd80b69cf259d4ad3a26c9cf8f9440e3ace0fd;p=loctrkd.git diff --git a/test/test_fuzz.py b/test/test_fuzz.py index 6dea449..0456fda 100644 --- a/test/test_fuzz.py +++ b/test/test_fuzz.py @@ -1,18 +1,22 @@ """ 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 +from time import sleep +from typing import Optional +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, ): @@ -21,10 +25,32 @@ class Fuzz(TestCase): self.sock.connect(skadr) def tearDown(self) -> None: + sleep(1) # give collector some time + self._send_and_drain(None) self.sock.close() + print("finished fuzzing") + super().tearDown() - def test_fuzz(self) -> None: + def _send_and_drain(self, buf: Optional[bytes]) -> None: + if buf is not None: + self.sock.send(buf) + try: + self.sock.recv(4096, MSG_DONTWAIT) + except BlockingIOError: + pass + + def test_stream(self) -> None: for _ in range(REPEAT): size = self.rnd.randint(1, 5000) buf = self.rnd.randbytes(size) - self.sock.send(buf) + self._send_and_drain(buf) + + def test_msgs(self) -> None: + for _ in range(REPEAT): + size = self.rnd.randint(0, 300) + buf = b"xx" + self.rnd.randbytes(size) + b"\r\n" + self._send_and_drain(buf) + + +if __name__ == "__main__": + unittest.main()