forked from rustyrussell/runes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_runes.py
318 lines (257 loc) · 12.5 KB
/
test_runes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import base64
import copy
import hashlib
import runes
import sha256 # type: ignore
import string
from typing import Sequence
# This is a simplified version of end_shastream
def end_shastream_simple(length: int) -> bytes:
stream = bytes([0x80])
while ((length + len(stream) + 8) % 64) != 0:
stream += bytes(1)
stream += int.to_bytes(length * 8, 8, 'big')
return stream
def check_auth_sha(secret: bytes, restrictions: Sequence[runes.Restriction]):
stream = secret
for r in restrictions:
stream += end_shastream_simple(len(stream))
stream += bytes(r.encode(), encoding='utf8')
return hashlib.sha256(stream).digest()
def test_end_shastream():
# Make sure it gives same as our naive approach
for length in range(0, 100):
assert runes.end_shastream(length) == end_shastream_simple(length)
# Make sure it agrees with actual SHA terminal.
for length in range(0, 100):
correct = hashlib.sha256(bytes(length)).digest()
manual = sha256.sha256()
manual.update(bytes(length))
manual.update(runes.end_shastream(length))
assert manual.state[0] == correct
def test_rune_auth():
# Rune with 16x0 secret.
secret = bytes(16)
mr = runes.MasterRune(secret)
assert check_auth_sha(secret, []) == mr.authcode()
assert mr.is_rune_authorized(runes.Rune(mr.authcode(), []))
restriction = runes.Restriction([runes.Alternative('f1', '=', 'v1')])
mr.add_restriction(restriction)
assert check_auth_sha(secret, [restriction]) == mr.authcode()
assert not mr.is_rune_authorized(runes.Rune(mr.authcode(), []))
assert mr.is_rune_authorized(runes.Rune(mr.authcode(), [restriction]))
long_restriction = runes.Restriction([runes.Alternative('f' * 32, '=', 'v1' * 64)])
mr.add_restriction(long_restriction)
assert check_auth_sha(secret, [restriction, long_restriction]) == mr.authcode()
assert not mr.is_rune_authorized(runes.Rune(mr.authcode(), [restriction]))
assert not mr.is_rune_authorized(runes.Rune(mr.authcode(), [long_restriction]))
assert not mr.is_rune_authorized(runes.Rune(mr.authcode(), [long_restriction, restriction]))
assert mr.is_rune_authorized(runes.Rune(mr.authcode(), [restriction, long_restriction]))
def test_rune_alternatives():
"""Test that we interpret alternatives as expected"""
alt = runes.Alternative('f1', '!', '')
assert alt.test({}) is None
assert alt.test({'f1': '1'}) == 'f1: is present'
assert alt.test({'f2': '1'}) is None
alt = runes.Alternative('f1', '=', '1')
assert alt.test({}) == 'f1: is missing'
assert alt.test({'f1': '1'}) is None
assert alt.test({'f1': '01'}) == 'f1: != 1'
assert alt.test({'f1': '10'}) == 'f1: != 1'
assert alt.test({'f1': '010'}) == 'f1: != 1'
assert alt.test({'f1': '10101'}) == 'f1: != 1'
alt = runes.Alternative('f1', '/', '1')
assert alt.test({}) == 'f1: is missing'
assert alt.test({'f1': '1'}) == 'f1: = 1'
assert alt.test({'f1': '01'}) is None
assert alt.test({'f1': '10'}) is None
assert alt.test({'f1': '010'}) is None
assert alt.test({'f1': '10101'}) is None
alt = runes.Alternative('f1', '$', '1')
assert alt.test({}) == 'f1: is missing'
assert alt.test({'f1': '1'}) is None
assert alt.test({'f1': '01'}) is None
assert alt.test({'f1': '10'}) == 'f1: does not end with 1'
assert alt.test({'f1': '010'}) == 'f1: does not end with 1'
assert alt.test({'f1': '10101'}) is None
alt = runes.Alternative('f1', '^', '1')
assert alt.test({}) == 'f1: is missing'
assert alt.test({'f1': '1'}) is None
assert alt.test({'f1': '01'}) == 'f1: does not start with 1'
assert alt.test({'f1': '10'}) is None
assert alt.test({'f1': '010'}) == 'f1: does not start with 1'
assert alt.test({'f1': '10101'}) is None
alt = runes.Alternative('f1', '~', '1')
assert alt.test({}) == 'f1: is missing'
assert alt.test({'f1': '1'}) is None
assert alt.test({'f1': '01'}) is None
assert alt.test({'f1': '10'}) is None
assert alt.test({'f1': '010'}) is None
assert alt.test({'f1': '10101'}) is None
assert alt.test({'f1': '020'}) == 'f1: does not contain 1'
alt = runes.Alternative('f1', '<', '1')
assert alt.test({}) == 'f1: is missing'
assert alt.test({'f1': '1'}) == 'f1: >= 1'
assert alt.test({'f1': '01'}) == 'f1: >= 1'
assert alt.test({'f1': '10'}) == 'f1: >= 1'
assert alt.test({'f1': '010'}) == 'f1: >= 1'
assert alt.test({'f1': '10101'}) == 'f1: >= 1'
assert alt.test({'f1': '020'}) == 'f1: >= 1'
assert alt.test({'f1': '0'}) is None
assert alt.test({'f1': 'x'}) == 'f1: not an integer field'
alt = runes.Alternative('f1', '<', 'x')
assert alt.test({}) == 'f1: is missing'
assert alt.test({'f1': '1'}) == 'f1: not a valid integer'
alt = runes.Alternative('f1', '>', '1')
assert alt.test({}) == 'f1: is missing'
assert alt.test({'f1': '1'}) == 'f1: <= 1'
assert alt.test({'f1': '01'}) == 'f1: <= 1'
assert alt.test({'f1': '10'}) is None
assert alt.test({'f1': '010'}) is None
assert alt.test({'f1': '10101'}) is None
assert alt.test({'f1': '020'}) is None
assert alt.test({'f1': '0'}) == 'f1: <= 1'
assert alt.test({'f1': 'x'}) == 'f1: not an integer field'
alt = runes.Alternative('f1', '>', 'x')
assert alt.test({}) == 'f1: is missing'
assert alt.test({'f1': '1'}) == 'f1: not a valid integer'
alt = runes.Alternative('f1', '{', '1')
assert alt.test({}) == 'f1: is missing'
assert alt.test({'f1': '1'}) == 'f1: is the same or ordered after 1'
assert alt.test({'f1': '01'}) is None
assert alt.test({'f1': '10'}) == 'f1: is the same or ordered after 1'
assert alt.test({'f1': '010'}) is None
assert alt.test({'f1': '10101'}) == 'f1: is the same or ordered after 1'
assert alt.test({'f1': '020'}) is None
assert alt.test({'f1': '0'}) is None
alt = runes.Alternative('f1', '}', '1')
assert alt.test({}) == 'f1: is missing'
assert alt.test({'f1': '1'}) == 'f1: is the same or ordered before 1'
assert alt.test({'f1': '01'}) == 'f1: is the same or ordered before 1'
assert alt.test({'f1': '10'}) is None
assert alt.test({'f1': '010'}) == 'f1: is the same or ordered before 1'
assert alt.test({'f1': '10101'}) is None
assert alt.test({'f1': '020'}) == 'f1: is the same or ordered before 1'
assert alt.test({'f1': '0'}) == 'f1: is the same or ordered before 1'
alt = runes.Alternative('f1', '#', '1')
assert alt.test({}) is None
assert alt.test({'f1': '1'}) is None
assert alt.test({'f1': '01'}) is None
assert alt.test({'f1': '10'}) is None
assert alt.test({'f1': '010'}) is None
assert alt.test({'f1': '10101'}) is None
assert alt.test({'f1': '020'}) is None
assert alt.test({'f1': '0'}) is None
def test_rune_restriction():
alt1 = runes.Alternative('f1', '!', '')
alt2 = runes.Alternative('f2', '=', '2')
# Either can be true
restr = runes.Restriction((alt1, alt2))
assert restr.test({}) is None
assert restr.test({'f1': '1', 'f2': 3}) == "f1: is present AND f2: != 2"
assert restr.test({'f2': '1'}) is None
assert restr.test({'f2': '2'}) is None
assert restr.test({'f2': 2}) is None
def test_rune_restrictions():
"""Either of these passes, the restriction passes"""
alt1 = runes.Alternative('f1', '!', '')
alt2 = runes.Alternative('f2', '=', '2')
rune = runes.Rune(bytes(32), [runes.Restriction((alt1, alt2))])
assert rune.are_restrictions_met({}) == (True, '')
assert (rune.are_restrictions_met({'f1': '1', 'f2': 3})
== (False, 'f1: is present AND f2: != 2'))
assert rune.are_restrictions_met({'f1': '1', 'f2': 2}) == (True, '')
assert rune.are_restrictions_met({'f2': '1'}) == (True, '')
assert rune.are_restrictions_met({'f2': '2'}) == (True, '')
alt3 = runes.Alternative('f3', '>', '2')
rune = runes.Rune(bytes(32), [runes.Restriction((alt1, alt2)),
runes.Restriction((alt3,))])
assert rune.are_restrictions_met({}) == (False, 'f3: is missing')
assert (rune.are_restrictions_met({'f1': '1', 'f2': 3})
== (False, 'f1: is present AND f2: != 2'))
assert (rune.are_restrictions_met({'f1': '1', 'f2': 2})
== (False, 'f3: is missing'))
assert rune.are_restrictions_met({'f2': '1'}) == (False, 'f3: is missing')
assert rune.are_restrictions_met({'f2': '2'}) == (False, 'f3: is missing')
assert rune.are_restrictions_met({'f3': '2'}) == (False, 'f3: <= 2')
assert rune.are_restrictions_met({'f3': '3'}) == (True, '')
assert (rune.are_restrictions_met({'f1': '1', 'f2': 'x', 'f3': 3})
== (False, 'f1: is present AND f2: != 2'))
assert (rune.are_restrictions_met({'f2': '1', 'f3': 2})
== (False, 'f3: <= 2'))
assert (rune.are_restrictions_met({'f2': '2', 'f3': 2})
== (False, 'f3: <= 2'))
assert rune.are_restrictions_met({'f2': '1', 'f3': 3}) == (True, '')
assert rune.are_restrictions_met({'f2': '2', 'f3': 4}) == (True, '')
def test_rune_fromstring_norestrictions():
rune = runes.Rune.from_base64('-YpZTBZ4Tb5SsUz3XIukxBx'
'R619iEthm9oNJnC0LxZM=')
assert rune.restrictions == []
def test_copy():
"""Make sure copies get their own sha state, as it corresponds to the restrictions list, which *is* copied"""
mr = runes.MasterRune(bytes(16))
mrstring = mr.to_base64()
# .copy() and copy module should work the same.
for mrune in (mr.copy(), copy.copy(mr), copy.deepcopy(mr)):
restriction = runes.Restriction([runes.Alternative('f1', '=', 'v1')])
mrune.add_restriction(restriction)
assert mrune.to_base64() != mrstring
assert mr.to_base64() == mrstring
# Should work on normal runes, as well.
orig = runes.Rune.from_base64(mr.to_base64())
for rune in (orig.copy(), copy.copy(orig), copy.deepcopy(orig)):
restriction = runes.Restriction([runes.Alternative('f1', '=', 'v1')])
rune.add_restriction(restriction)
assert rune.to_base64() != mrstring
assert orig.to_base64() == mrstring
def test_rune_tostring():
alt1 = runes.Alternative('f1', '!', '')
alt2 = runes.Alternative('f2', '=', '2')
alt3 = runes.Alternative('f3', '>', '2')
# Either can be true
restr1 = runes.Restriction((alt1, alt2))
restr2 = runes.Restriction((alt3,))
rune = runes.MasterRune(bytes([1] * 32), [restr1, restr2])
runestr = rune.to_base64()
rune2 = runes.Rune.from_base64(runestr)
assert rune == rune2
def test_check():
# Rune with 16x0 secret.
secret = bytes(16)
mr = runes.MasterRune(secret)
rune = mr.copy()
rune.add_restriction(runes.Restriction.from_str('foo=bar'))
runestr = rune.to_base64()
# MasterRune variants work
assert mr.check_with_reason(runestr, {'foo': 'bar'}) == (True, '')
assert mr.check_with_reason(runestr, {'foo': 'baz'}) == (False, 'foo: != bar')
assert runes.check_with_reason(secret, runestr, {'foo': 'bar'}) == (True, '')
assert (runes.check_with_reason(secret, runestr, {'foo': 'baz'})
== (False, 'foo: != bar'))
assert runes.check(secret, runestr, {'foo': 'bar'})
assert not runes.check(secret, runestr, {'foo': 'baz'})
def test_field_with_punctuation():
rune = runes.Rune(bytes(32), [runes.Restriction.from_str('foo=bar.baz')])
runestr = rune.to_base64()
rune2 = runes.Rune.from_base64(runestr)
assert rune == rune2
# You can add any punctuation this way...
alt = runes.Alternative.from_str("foo=" + string.punctuation)
alt2, remainder = runes.Alternative.decode(alt.encode())
assert remainder == ''
assert alt == alt2
restr = runes.Restriction([alt])
assert restr.encode() == "foo=" + string.punctuation.replace('\\', '\\\\').replace('&', '\\&').replace('|', '\\|')
restr2, remainder = runes.Restriction.decode(restr.encode())
assert remainder == ''
assert restr == restr2
rune.add_restriction(restr)
# | and & get escaped on demarshal.
decoded = base64.urlsafe_b64decode(rune.to_base64())[32:].decode('utf8')
assert decoded == 'foo=bar.baz' + '&' + 'foo=' + string.punctuation.replace('\\', '\\\\').replace('&', '\\&').replace('|', '\\|')
rune.add_restriction(runes.Restriction.from_str("foo=1"))
decoded = base64.urlsafe_b64decode(rune.to_base64())[32:].decode('utf8')
assert decoded == 'foo=bar.baz' + '&' + 'foo=' + string.punctuation.replace('\\', '\\\\').replace('&', '\\&').replace('|', '\\|') + '&' + 'foo=1'
runestr = rune.to_base64()
rune2 = runes.Rune.from_base64(runestr)
assert rune == rune2