comparison test/test_hypothesis.py @ 7827:604da0650797

test: add basic tests using hypothesis It is segregated to its own file. I am skipping the entire file using importorskip(). Mixing hypothesis tests with non-hypothesis tests is tricky. Hypothesis uses decorators before test commands: @given(text()) @settings(max_examples=_max_examples) Pytest runs the decorators and the arguments as part of scanning the file for tests. This means the decorator (given, settings ...) and the strategies inside the decorators (e.g. text()) have to be defined using a lambda or something. Only aborting at the top of the file using importorskip prevents having to define all the symbols that would be imported from hypothesis.
author John Rouillard <rouilj@ieee.org>
date Sun, 24 Mar 2024 13:49:52 -0400
parents
children 6aea9dad6d4a
comparison
equal deleted inserted replaced
7826:897c23876e9f 7827:604da0650797
1 import unittest
2
3 import pytest
4
5 pytest.importorskip("hypothesis")
6
7 # ruff: noqa: E402
8 from hypothesis import example, given, settings
9 from hypothesis.strategies import binary, none, one_of, sampled_from, text
10
11 from roundup.anypy.strings import b2s, s2b, s2u, u2s
12 # ruff: noqa: I001 - yes I know I am using \ to continue the line...
13 from roundup.password import PasswordValueError, encodePassword, \
14 h64decode, h64encode
15
16
17 def Identity(x):
18 return x
19
20
21 _max_examples = 1000
22
23
24 class HypoTestStrings(unittest.TestCase):
25
26 @given(text())
27 @settings(max_examples=_max_examples)
28 def test_b2s(self, utf8_bytes):
29 self.assertEqual(b2s(utf8_bytes.encode("utf-8")), utf8_bytes)
30
31 @given(text())
32 @settings(max_examples=_max_examples)
33 def test_s2b(self, s):
34 self.assertTrue(isinstance(s2b(s), bytes))
35
36 @given(text())
37 @settings(max_examples=_max_examples)
38 @example("\U0001F600 hi there") # smiley face emoji
39 def test_s2u_u2s_invertable(self, s):
40 self.assertEqual(u2s(s2u(s)), s)
41
42
43 class HypoTestPassword(unittest.TestCase):
44
45 @given(binary())
46 @example(b"")
47 @settings(max_examples=_max_examples)
48 def test_h64encode_h64decode(self, s):
49
50 self.assertEqual(h64decode(h64encode(s)), s)
51
52 @given(one_of(none(), text()),
53 sampled_from(("PBKDF2S5", "PBKDF2", "SSHA",
54 "SHA", "MD5", "crypt", "plaintext",
55 "zot")))
56 @example("asd\x00df", "crypt")
57 @settings(max_examples=100 * _max_examples)
58 def test_encodePassword(self, password, scheme):
59
60 if scheme == "crypt" and password and "\x00" in password:
61 with self.assertRaises(ValueError) as e:
62 encodePassword(password, scheme)
63 self.assertEqual(e.exception.args[0],
64 "embedded null character")
65 elif scheme == "plaintext":
66 if password is not None:
67 self.assertEqual(encodePassword(password, scheme), password)
68 else:
69 self.assertEqual(encodePassword(password, scheme), "")
70 elif scheme == "zot":
71 with self.assertRaises(PasswordValueError) as e:
72 encodePassword(password, scheme)
73 self.assertEqual(e.exception.args[0],
74 "Unknown encryption scheme 'zot'")
75 else:
76 # it shouldn't throw anything.
77 pw = encodePassword(password, scheme)
78
79 # verify format
80 if scheme in ["PBKDF2S5", "PBKDF2"]:
81 # 1000$XbSsijELEQbZZb1LlD7CFuotF/8$DdtssSlm.e
82 self.assertRegex(pw, r"^\d{4,8}\$.{27}\$.*")
83 elif scheme == "SSHA":
84 # vqDbjvs8rhrS1AJxHYEGGXQW3x7STAPgo7uCtnw4GYgU7FN5VYbZxccQYCC0eXOxSipLbtgBudH1vDRMNlG0uw==
85 self.assertRegex(pw, r"^[^=]*={0,3}$")
86 elif scheme == "SHA":
87 # da39a3ee5e6b4b0d3255bfef95601890afd80709'
88 self.assertRegex(pw, r"^[a-z0-9]{40}$")
89 elif scheme == "MD5":
90 # d41d8cd98f00b204e9800998ecf8427e'
91 self.assertRegex(pw, r"^[a-z0-9]{32}$")
92 elif scheme == "crypt":
93 # WqzFDzhi8MmoU
94 self.assertRegex(pw, r"^[A-z0-9./]{13}$")
95 else:
96 self.assertFalse("Unknown scheme: %s, val: %s" % (scheme, pw))

Roundup Issue Tracker: http://roundup-tracker.org/