81f902f6a37fc585684b6fc375ffcb5f2ae964c6
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 3 months ago

1) # SPDX-FileCopyrightText: 2024 Marco Ricci <m@the13thletter.info>
2) #
3) # SPDX-License-Identifier: MIT
4) 
5) """Test passphrase generation via derivepassphrase.Vault."""
6) 
Marco Ricci Expose some functionality f...

Marco Ricci authored 2 months ago

7) from __future__ import annotations
8) 
9) import math
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 3 months ago

10) 
11) import derivepassphrase
12) import sequin
Marco Ricci Expose some functionality f...

Marco Ricci authored 2 months ago

13) import pytest
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 3 months ago

14) 
15) Vault = derivepassphrase.Vault
16) phrase = b'She cells C shells bye the sea shoars'
Marco Ricci Expose some functionality f...

Marco Ricci authored 2 months ago

17) google_phrase = rb': 4TVH#5:aZl8LueOT\{'
18) twitter_phrase = rb"[ (HN_N:lI&<ro=)3'g9"
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 3 months ago

19) 
Marco Ricci Expose some functionality f...

Marco Ricci authored 2 months ago

20) @pytest.mark.parametrize(['service', 'expected'], [
21)     (b'google', google_phrase),
22)     ('twitter', twitter_phrase),
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 3 months ago

23) ])
24) def test_200_basic_configuration(service, expected):
25)     assert Vault(phrase=phrase).generate(service) == expected
26) 
27) def test_201_phrase_dependence():
28)     assert (
29)         Vault(phrase=(phrase + b'X')).generate('google') ==
30)         b'n+oIz6sL>K*lTEWYRO%7'
31)     )
32) 
33) def test_202_reproducibility_and_bytes_service_name():
34)     assert (
35)         Vault(phrase=phrase).generate(b'google') ==
36)         Vault(phrase=phrase).generate('google')
37)     )
38) 
Marco Ricci Add test for service name b...

Marco Ricci authored 3 months ago

39) def test_203_reproducibility_and_bytearray_service_name():
40)     assert (
41)         Vault(phrase=phrase).generate(b'google') ==
42)         Vault(phrase=phrase).generate(bytearray(b'google'))
43)     )
44) 
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 3 months ago

45) def test_210_nonstandard_length():
46)     assert Vault(phrase=phrase, length=4).generate('google') == b'xDFu'
47) 
48) def test_211_repetition_limit():
49)     assert (
50)         Vault(phrase=b'', length=24, symbol=0, number=0,
51)               repeat=1).generate('asd') ==
52)         b'IVTDzACftqopUXqDHPkuCIhV'
53)     )
54) 
55) def test_212_without_symbols():
56)     assert (
57)         Vault(phrase=phrase, symbol=0).generate('google') ==
58)         b'XZ4wRe0bZCazbljCaMqR'
59)     )
60) 
61) def test_213_too_many_symbols():
62)     with pytest.raises(ValueError,
63)                        match='requested passphrase length too short'):
64)         Vault(phrase=phrase, symbol=100)
65) 
66) def test_214_no_numbers():
67)     assert (
68)         Vault(phrase=phrase, number=0).generate('google') ==
69)         b'_*$TVH.%^aZl(LUeOT?>'
70)     )
71) 
72) def test_214_no_lowercase_letters():
73)     assert (
74)         Vault(phrase=phrase, lower=0).generate('google') ==
75)         b':{?)+7~@OA:L]!0E$)(+'
76)     )
77) 
78) def test_215_at_least_5_digits():
79)     assert (
80)         Vault(phrase=phrase, length=8, number=5).generate('songkick') ==
81)         b'i0908.7['
82)     )
83) 
84) def test_216_lots_of_spaces():
85)     assert (
86)         Vault(phrase=phrase, space=12).generate('songkick') ==
87)         b' c   6 Bq  % 5fR    '
88)     )
89) 
90) def test_217_no_viable_characters():
91)     with pytest.raises(ValueError,
92)                        match='no allowed characters left'):
93)         Vault(phrase=phrase, lower=0, upper=0, number=0,
94)               space=0, dash=0, symbol=0)
95) 
96) def test_218_all_character_classes():
97)     assert (
98)         Vault(phrase=phrase, lower=2, upper=2, number=1,
99)               space=3, dash=2, symbol=1).generate('google') ==
100)         b': : fv_wqt>a-4w1S  R'
101)     )
Marco Ricci Fix repeated character dete...

Marco Ricci authored 3 months ago

102) 
103) def test_219_only_numbers_and_very_high_repetition_limit():
104)     generated = Vault(phrase=b'', length=40, lower=0, upper=0, space=0,
105)                       dash=0, symbol=0, repeat=4).generate('abcdef')
106)     assert b'0000' not in generated
107)     assert b'1111' not in generated
108)     assert b'2222' not in generated
109)     assert b'3333' not in generated
110)     assert b'4444' not in generated
111)     assert b'5555' not in generated
112)     assert b'6666' not in generated
113)     assert b'7777' not in generated
114)     assert b'8888' not in generated
115)     assert b'9999' not in generated
116) 
117) def test_220_very_limited_character_set():
118)     generated = Vault(phrase=b'', length=24, lower=0, upper=0,
119)                       space=0, symbol=0).generate('testing')
120)     assert b'763252593304946694588866' == generated
Marco Ricci Fix character set subtracti...

Marco Ricci authored 3 months ago

121) 
122) def test_300_character_set_subtraction():
123)     assert Vault._subtract(b'be', b'abcdef') == bytearray(b'acdf')
124) 
125) def test_301_character_set_subtraction_duplicate():
126)     with pytest.raises(ValueError, match='duplicate characters'):
127)         Vault._subtract(b'abcdef', b'aabbccddeeff')
128)     with pytest.raises(ValueError, match='duplicate characters'):
129)         Vault._subtract(b'aabbccddeeff', b'abcdef')