From: Bob Halley Date: Thu, 18 Jun 2020 16:07:04 +0000 (-0700) Subject: improve entropy coverage X-Git-Tag: v2.0.0rc1~37 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c125472d13dd3b9edce6c69155bae65515759b9f;p=thirdparty%2Fdnspython.git improve entropy coverage --- diff --git a/dns/entropy.py b/dns/entropy.py index cd790836..fd848418 100644 --- a/dns/entropy.py +++ b/dns/entropy.py @@ -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) diff --git a/tests/test_entropy.py b/tests/test_entropy.py index 601b733e..061f0770 100644 --- a/tests/test_entropy.py +++ b/tests/test_entropy.py @@ -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)