Skip to content

Commit 3cac02a

Browse files
committed
Cleaning up tests
1 parent bfe61e3 commit 3cac02a

2 files changed

Lines changed: 48 additions & 10 deletions

File tree

js2pysecrets/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,12 @@ def share(secret, num_shares, threshold, pad_length=None):
567567
# Security: pad in multiples of 128 bits by default
568568
pad_length = pad_length or 128
569569

570-
if not isinstance(secret, str):
570+
if not isinstance(secret, str) or not all(
571+
c in "0123456789abcdefABCDEF" for c in secret
572+
):
571573
raise ValueError("Secret must be a hex string.")
572574

573-
if not isinstance(num_shares, int) or num_shares < 2:
575+
if not isinstance(num_shares, int) or num_shares <= 2:
574576
raise ValueError("Number of shares must be an integer >= 2.")
575577

576578
if num_shares > settings.maxShares:

tests/test_py_init.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@
6767
"sec_val, sec_error",
6868
[
6969
(None, "Secret must be a hex string."),
70+
(True, "Secret must be a hex string."),
71+
(False, "Secret must be a hex string."),
7072
(1234, "Secret must be a hex string."),
7173
(-256, "Secret must be a hex string."),
72-
("hello world", "Invalid hex character:"),
74+
("hello world", "Secret must be a hex string."),
7375
(None, None),
7476
],
7577
)
@@ -82,15 +84,38 @@
8284
(-22, "Number of shares must be an integer >= 2."),
8385
(
8486
"hello",
85-
"Threshold number of shares must be less than or equal to the total shares specified.",
87+
"Number of shares must be an integer >= 2.",
88+
),
89+
(
90+
lambda: settings.maxShares + 1,
91+
"Number of shares must be <=",
92+
),
93+
(None, None),
94+
],
95+
)
96+
@pytest.mark.parametrize(
97+
"th_val, th_error",
98+
[
99+
(False, "Number of shares must be an integer >= 2."),
100+
(2, "Threshold number of shares must be an integer >= 2."),
101+
(0, "Threshold number of shares must be an integer >= 2."),
102+
(-22, "Threshold number of shares must be an integer >= 2."),
103+
(
104+
"hello",
105+
"Threshold number of shares must be an integer >= 2.",
86106
),
87107
(
88108
lambda: settings.maxShares + 1,
89109
"Threshold number of shares must be <=",
90110
),
111+
(
112+
lambda: numShares + 1,
113+
"Threshold number of shares must be less than or equal to the",
114+
),
91115
(None, None),
92116
],
93117
)
118+
94119
@pytest.mark.skip(reason="WIP: started seeing odd failures")
95120
def test_py_init_with_errors(
96121
bits,
@@ -101,9 +126,11 @@ def test_py_init_with_errors(
101126
sec_error,
102127
num_val,
103128
num_error,
129+
th_val,
130+
th_error
104131
):
105132

106-
expected_error = check_error(bits_error, rand_error, sec_error, num_error)
133+
expected_error = check_error(bits_error, rand_error, sec_error, num_error, th_error)
107134

108135
if expected_error:
109136
with pytest.raises(ValueError, match=expected_error):
@@ -112,33 +139,42 @@ def test_py_init_with_errors(
112139
secret = secrets.random(rand_bits)
113140
if sec_error:
114141
secret = sec_val
115-
if not bits_error or rand_error or num_error:
142+
if not bits_error or rand_error or num_error or th_error:
116143
num_shares = base_value(num_val) or 6
117144
shares = secrets.share(secret, num_shares, 3)
118-
if not bits_error or rand_error:
145+
if not bits_error or rand_error or th_error:
119146
num_shares = base_value(num_val)
120147
shares = secrets.share(secret, num_shares, 3)
148+
if not bits_error or rand_error:
149+
150+
numShares = low_random(settings.maxShares, settings.bits)
151+
threshold = base_value(th_val)
152+
shares = secrets.share(secret, numShares, threshold)
153+
121154
assert len(caught_warnings) == 1
122155
assert issubclass(caught_warnings[0].category, Warning)
123156
assert expected_error in str(caught_warnings[0].message)
124157
else:
125158
secrets.init(bits)
126159
assert bits == settings.bits or settings.get_defaults().bits
127160

161+
numShares = low_random(settings.maxShares, settings.bits)
162+
threshold = low_random(numShares, settings.bits)
163+
128164
secret = secrets.random(rand_bits)
129-
shares = secrets.share(secret, 6, 3)
165+
shares = secrets.share(secret, numShares, threshold)
130166

131167

132168
# Used to catch the expected error
133-
def check_error(bits_error, rand_error, sec_error, num_error):
169+
def check_error(bits_error, rand_error, sec_error, num_error, th_error):
134170
expected_error = (
135171
bits_error
136172
if bits_error
137173
else (
138174
rand_error
139175
if rand_error
140176
else (
141-
sec_error if sec_error else (num_error if num_error else None)
177+
sec_error if sec_error else (num_error if num_error else (th_error if th_error else None))
142178
)
143179
)
144180
)

0 commit comments

Comments
 (0)