Mercurial > p > roundup > code
annotate 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 |
| rev | line source |
|---|---|
|
7827
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
1 import unittest |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
2 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
3 import pytest |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
4 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
5 pytest.importorskip("hypothesis") |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
6 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
7 # ruff: noqa: E402 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
8 from hypothesis import example, given, settings |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
9 from hypothesis.strategies import binary, none, one_of, sampled_from, text |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
10 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
11 from roundup.anypy.strings import b2s, s2b, s2u, u2s |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
12 # ruff: noqa: I001 - yes I know I am using \ to continue the line... |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
13 from roundup.password import PasswordValueError, encodePassword, \ |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
14 h64decode, h64encode |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
15 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
16 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
17 def Identity(x): |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
18 return x |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
19 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
20 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
21 _max_examples = 1000 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
22 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
23 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
24 class HypoTestStrings(unittest.TestCase): |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
25 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
26 @given(text()) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
27 @settings(max_examples=_max_examples) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
28 def test_b2s(self, utf8_bytes): |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
29 self.assertEqual(b2s(utf8_bytes.encode("utf-8")), utf8_bytes) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
30 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
31 @given(text()) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
32 @settings(max_examples=_max_examples) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
33 def test_s2b(self, s): |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
34 self.assertTrue(isinstance(s2b(s), bytes)) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
35 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
36 @given(text()) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
37 @settings(max_examples=_max_examples) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
38 @example("\U0001F600 hi there") # smiley face emoji |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
39 def test_s2u_u2s_invertable(self, s): |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
40 self.assertEqual(u2s(s2u(s)), s) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
41 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
42 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
43 class HypoTestPassword(unittest.TestCase): |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
44 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
45 @given(binary()) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
46 @example(b"") |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
47 @settings(max_examples=_max_examples) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
48 def test_h64encode_h64decode(self, s): |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
49 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
50 self.assertEqual(h64decode(h64encode(s)), s) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
51 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
52 @given(one_of(none(), text()), |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
53 sampled_from(("PBKDF2S5", "PBKDF2", "SSHA", |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
54 "SHA", "MD5", "crypt", "plaintext", |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
55 "zot"))) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
56 @example("asd\x00df", "crypt") |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
57 @settings(max_examples=100 * _max_examples) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
58 def test_encodePassword(self, password, scheme): |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
59 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
60 if scheme == "crypt" and password and "\x00" in password: |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
61 with self.assertRaises(ValueError) as e: |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
62 encodePassword(password, scheme) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
63 self.assertEqual(e.exception.args[0], |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
64 "embedded null character") |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
65 elif scheme == "plaintext": |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
66 if password is not None: |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
67 self.assertEqual(encodePassword(password, scheme), password) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
68 else: |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
69 self.assertEqual(encodePassword(password, scheme), "") |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
70 elif scheme == "zot": |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
71 with self.assertRaises(PasswordValueError) as e: |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
72 encodePassword(password, scheme) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
73 self.assertEqual(e.exception.args[0], |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
74 "Unknown encryption scheme 'zot'") |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
75 else: |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
76 # it shouldn't throw anything. |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
77 pw = encodePassword(password, scheme) |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
78 |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
79 # verify format |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
80 if scheme in ["PBKDF2S5", "PBKDF2"]: |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
81 # 1000$XbSsijELEQbZZb1LlD7CFuotF/8$DdtssSlm.e |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
82 self.assertRegex(pw, r"^\d{4,8}\$.{27}\$.*") |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
83 elif scheme == "SSHA": |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
84 # vqDbjvs8rhrS1AJxHYEGGXQW3x7STAPgo7uCtnw4GYgU7FN5VYbZxccQYCC0eXOxSipLbtgBudH1vDRMNlG0uw== |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
85 self.assertRegex(pw, r"^[^=]*={0,3}$") |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
86 elif scheme == "SHA": |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
87 # da39a3ee5e6b4b0d3255bfef95601890afd80709' |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
88 self.assertRegex(pw, r"^[a-z0-9]{40}$") |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
89 elif scheme == "MD5": |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
90 # d41d8cd98f00b204e9800998ecf8427e' |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
91 self.assertRegex(pw, r"^[a-z0-9]{32}$") |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
92 elif scheme == "crypt": |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
93 # WqzFDzhi8MmoU |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
94 self.assertRegex(pw, r"^[A-z0-9./]{13}$") |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
95 else: |
|
604da0650797
test: add basic tests using hypothesis
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
96 self.assertFalse("Unknown scheme: %s, val: %s" % (scheme, pw)) |
