]> www.average.org Git - loctrkd.git/blob - test/test_fuzz.py
test: complete fuzzer unittest
[loctrkd.git] / test / test_fuzz.py
1 """ Send junk to the collector """
2
3 from random import Random
4 from socket import getaddrinfo, socket, AF_INET6, MSG_DONTWAIT, SOCK_STREAM
5 import unittest
6 from .common import TestWithServers
7
8 REPEAT: int = 1000000
9
10
11 class Fuzz(TestWithServers):
12     def setUp(self, *args: str) -> None:
13         super().setUp("collector")
14         self.rnd = Random()
15         for fam, typ, pro, cnm, skadr in getaddrinfo(
16             "::1",
17             self.conf.getint("collector", "port"),
18             family=AF_INET6,
19             type=SOCK_STREAM,
20         ):
21             break  # Just take the first element
22         self.sock = socket(AF_INET6, SOCK_STREAM)
23         self.sock.connect(skadr)
24
25     def tearDown(self) -> None:
26         self.sock.close()
27         print("finished fuzzing")
28         super().tearDown()
29
30     def test_fuzz(self) -> None:
31         for _ in range(REPEAT):
32             size = self.rnd.randint(1, 5000)
33             buf = self.rnd.randbytes(size)
34             self.sock.send(buf)
35             try:
36                 self.sock.recv(4096, MSG_DONTWAIT)
37             except BlockingIOError:
38                 pass
39
40
41 if __name__ == "__main__":
42     unittest.main()