]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
improve entropy coverage
authorBob Halley <halley@dnspython.org>
Thu, 18 Jun 2020 16:07:04 +0000 (09:07 -0700)
committerBob Halley <halley@dnspython.org>
Thu, 18 Jun 2020 16:07:04 +0000 (09:07 -0700)
dns/entropy.py
tests/test_entropy.py

index cd7908362b8aa6e87d1b69c81b5fa22f838ec307..fd8484189b3c523c394b70b335fc6010bec2f3a7 100644 (file)
@@ -21,7 +21,7 @@ import random
 import time
 try:
     import threading as _threading
-except ImportError:
+except ImportError:  # pragma: no cover
     import dummy_threading as _threading    # type: ignore
 
 
@@ -64,7 +64,7 @@ class EntropyPool:
         if not self.seeded or self.seed_pid != os.getpid():
             try:
                 seed = os.urandom(16)
-            except Exception:
+            except Exception:  # pragma: no cover
                 try:
                     with open('/dev/urandom', 'rb', 0) as r:
                         seed = r.read(16)
index 601b733e1cb817e1fa0a4209400557ee00d1b724..061f0770e7cc68426aea36ebe445df51b5004d69 100644 (file)
@@ -20,6 +20,18 @@ class EntropyTestCase(unittest.TestCase):
         # Make sure that the results are at least somewhat random.
         self.assertGreater(len(values), 8)
 
+    def test_pool_random_between(self):
+        pool = dns.entropy.EntropyPool()
+        def bad():
+            pool.random_between(0, 4294967296)
+        self.assertRaises(ValueError, bad)
+        v = pool.random_between(50, 50 + 100000)
+        self.assertTrue(v >= 50 and v <= 50 + 100000)
+        v = pool.random_between(50, 50 + 10000)
+        self.assertTrue(v >= 50 and v <= 50 + 10000)
+        v = pool.random_between(50, 50 + 100)
+        self.assertTrue(v >= 50 and v <= 50 + 100)
+
     def test_functions(self):
         v = dns.entropy.random_16()
         self.assertTrue(0 <= v <= 65535)