ยปCore Development>Code coverage>Lib/test/test_secrets.py

Python code coverage for Lib/test/test_secrets.py

#countcontent
1n/a"""Test the secrets module.
2n/a
3n/aAs most of the functions in secrets are thin wrappers around functions
4n/adefined elsewhere, we don't need to test them exhaustively.
5n/a"""
6n/a
7n/a
8n/aimport secrets
9n/aimport unittest
10n/aimport string
11n/a
12n/a
13n/a# === Unit tests ===
14n/a
15n/aclass Compare_Digest_Tests(unittest.TestCase):
16n/a """Test secrets.compare_digest function."""
17n/a
18n/a def test_equal(self):
19n/a # Test compare_digest functionality with equal (byte/text) strings.
20n/a for s in ("a", "bcd", "xyz123"):
21n/a a = s*100
22n/a b = s*100
23n/a self.assertTrue(secrets.compare_digest(a, b))
24n/a self.assertTrue(secrets.compare_digest(a.encode('utf-8'), b.encode('utf-8')))
25n/a
26n/a def test_unequal(self):
27n/a # Test compare_digest functionality with unequal (byte/text) strings.
28n/a self.assertFalse(secrets.compare_digest("abc", "abcd"))
29n/a self.assertFalse(secrets.compare_digest(b"abc", b"abcd"))
30n/a for s in ("x", "mn", "a1b2c3"):
31n/a a = s*100 + "q"
32n/a b = s*100 + "k"
33n/a self.assertFalse(secrets.compare_digest(a, b))
34n/a self.assertFalse(secrets.compare_digest(a.encode('utf-8'), b.encode('utf-8')))
35n/a
37n/a # Test that compare_digest raises with mixed types.
38n/a a = 'abcde'
39n/a b = a.encode('utf-8')
40n/a assert isinstance(a, str)
41n/a assert isinstance(b, bytes)
42n/a self.assertRaises(TypeError, secrets.compare_digest, a, b)
43n/a self.assertRaises(TypeError, secrets.compare_digest, b, a)
44n/a
45n/a def test_bool(self):
46n/a # Test that compare_digest returns a bool.
47n/a self.assertIsInstance(secrets.compare_digest("abc", "abc"), bool)
48n/a self.assertIsInstance(secrets.compare_digest("abc", "xyz"), bool)
49n/a
50n/a
51n/aclass Random_Tests(unittest.TestCase):
52n/a """Test wrappers around SystemRandom methods."""
53n/a
54n/a def test_randbits(self):
55n/a # Test randbits.
56n/a errmsg = "randbits(%d) returned %d"
57n/a for numbits in (3, 12, 30):
58n/a for i in range(6):
59n/a n = secrets.randbits(numbits)
60n/a self.assertTrue(0 <= n < 2**numbits, errmsg % (numbits, n))
61n/a
62n/a def test_choice(self):
63n/a # Test choice.
64n/a items = [1, 2, 4, 8, 16, 32, 64]
65n/a for i in range(10):
66n/a self.assertTrue(secrets.choice(items) in items)
67n/a
68n/a def test_randbelow(self):
69n/a # Test randbelow.
70n/a for i in range(2, 10):
71n/a self.assertIn(secrets.randbelow(i), range(i))
72n/a self.assertRaises(ValueError, secrets.randbelow, 0)
73n/a self.assertRaises(ValueError, secrets.randbelow, -1)
74n/a
75n/a
76n/aclass Token_Tests(unittest.TestCase):
77n/a """Test token functions."""
78n/a
79n/a def test_token_defaults(self):
80n/a # Test that token_* functions handle default size correctly.
81n/a for func in (secrets.token_bytes, secrets.token_hex,
82n/a secrets.token_urlsafe):
83n/a with self.subTest(func=func):
84n/a name = func.__name__
85n/a try:
86n/a func()
87n/a except TypeError:
88n/a self.fail("%s cannot be called with no argument" % name)
89n/a try:
90n/a func(None)
91n/a except TypeError:
92n/a self.fail("%s cannot be called with None" % name)
93n/a size = secrets.DEFAULT_ENTROPY
94n/a self.assertEqual(len(secrets.token_bytes(None)), size)
95n/a self.assertEqual(len(secrets.token_hex(None)), 2*size)
96n/a
97n/a def test_token_bytes(self):
98n/a # Test token_bytes.
99n/a for n in (1, 8, 17, 100):
100n/a with self.subTest(n=n):
101n/a self.assertIsInstance(secrets.token_bytes(n), bytes)
102n/a self.assertEqual(len(secrets.token_bytes(n)), n)
103n/a
104n/a def test_token_hex(self):
105n/a # Test token_hex.
106n/a for n in (1, 12, 25, 90):
107n/a with self.subTest(n=n):
108n/a s = secrets.token_hex(n)
109n/a self.assertIsInstance(s, str)
110n/a self.assertEqual(len(s), 2*n)
111n/a self.assertTrue(all(c in string.hexdigits for c in s))
112n/a
113n/a def test_token_urlsafe(self):
114n/a # Test token_urlsafe.
115n/a legal = string.ascii_letters + string.digits + '-_'
116n/a for n in (1, 11, 28, 76):
117n/a with self.subTest(n=n):
118n/a s = secrets.token_urlsafe(n)
119n/a self.assertIsInstance(s, str)
120n/a self.assertTrue(all(c in legal for c in s))
121n/a
122n/a
123n/aif __name__ == '__main__':
124n/a unittest.main()