-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_protocol.py
490 lines (408 loc) · 13.2 KB
/
test_protocol.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
from tor_protocol import *
from nacl.utils import random
from nacl.hash import blake2b
def PrintPass(test_name: str):
print('[ \033[92mOK\033[0m ] {}'.format(test_name))
def TestCellHeader():
# Serialization for all known types should work
for type in range(0, 17):
cell_header = CellHeader(THOR_VERSION, type, bytes(16), 0)
assert cell_header.version == 1
assert cell_header.type == type
assert cell_header.circ_id == bytes(16)
assert cell_header.body_len == 0
data = cell_header.serialize()
correct_data = b"\x01\x00" + \
type.to_bytes(2, byteorder='little') + \
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
assert len(data) == 24
assert data == correct_data
# Try for a type out of range
triggered = False
try:
cell_header = CellHeader(THOR_VERSION, 17, bytes(16), 0)
except ValueError:
triggered = True
assert triggered
# Try for an invalid circuit ID length
triggered = False
try:
cell_header = CellHeader(THOR_VERSION, 0, bytes(15), 0)
except ValueError:
triggered = True
assert triggered
# Deserialization for all known types should work
for type in range(0, 17):
data = b"\x01\x00" + type.to_bytes(2, byteorder='little') + \
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
cell_header = CellHeader.deserialize(data)
assert cell_header.version == THOR_VERSION
assert cell_header.type == type
assert cell_header.circ_id == bytes(16)
assert cell_header.body_len == 0
# Try for a wrong length
data = b"\x00\x00"
triggered = False
try:
cell_header = CellHeader.deserialize(data)
except AssertionError:
triggered = True
assert triggered
# Try for a wrong version
data = b"\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
triggered = False
try:
cell_header = CellHeader.deserialize(data)
except ValueError:
triggered = True
assert triggered
def TestCreate():
# Serialize from valid key
pk = random(32)
create_cell = CreateCellBody(pk)
assert create_cell.pk == pk
data = create_cell.serialize()
assert len(data) == 32
assert data == pk
# Serialize from invalid key
pk = random(31)
triggered = False
try:
create_cell = CreateCellBody(pk)
except ValueError:
triggered = True
assert triggered
# Deserialize from valid key
data = random(32)
create_cell = CreateCellBody.deserialize(data)
assert create_cell.pk == data
# Deserialize from invalid key
data = random(33)
triggered = False
try:
create_cell = CreateCellBody.deserialize(data)
except AssertionError:
triggered = True
assert triggered
def TestCreated():
# Serialize from valid key, hash, and signature
pk = random(32)
sharedhash = random(32)
signature = random(64)
created_cell = CreatedCellBody(pk, sharedhash, signature)
assert created_cell.pk == pk
assert created_cell.hash == sharedhash
assert created_cell.signature == signature
data = created_cell.serialize()
assert len(data) == 128
assert data == pk + sharedhash + signature
# Serialize from invalid public key
pk = random(31)
sharedhash = random(32)
signature = random(64)
triggered = False
try:
created_cell = CreatedCellBody(pk, sharedhash, signature)
except ValueError:
triggered = True
assert triggered
# Serialize from invalid shared hash
pk = random(32)
sharedhash = random(31)
signature = random(64)
triggered = False
try:
created_cell = CreatedCellBody(pk, sharedhash, signature)
except ValueError:
triggered = True
assert triggered
# Serialize from invalid signature
pk = random(32)
sharedhash = random(32)
signature = random(68)
triggered = False
try:
created_cell = CreatedCellBody(pk, sharedhash, signature)
except ValueError:
triggered = True
assert triggered
# Deserialize from valid data
data = random(128)
created_cell = CreatedCellBody.deserialize(data)
assert created_cell.pk == data[0:32]
assert created_cell.hash == data[32:64]
assert created_cell.signature == data[64:128]
# Deserialize from invalid data
data = random(127)
triggered = False
try:
created_cell = CreatedCellBody.deserialize(data)
except AssertionError:
triggered = True
assert triggered
def TestDestroy():
# Serialize
destroy_cell = DestroyCellBody().serialize()
assert len(destroy_cell) == 0
# Deserialize from valid data
data = random(0)
destroy_cell = DestroyCellBody.deserialize(data)
# Deserialize from invalid data
data = random(1)
triggered = False
try:
destroy_cell = DestroyCellBody.deserialize(data)
except AssertionError:
triggered = True
assert triggered
def TestComputeDigest():
for i in range(128):
data = random(i)
digest = compute_digest(data)
assert digest == blake2b(data, digest_size=32,
person=b"THOR", encoder=RawEncoder)
def TestVerifyDigest():
data = blake2b(b"", digest_size=32, person=b"THOR", encoder=RawEncoder)
assert verify_digest(data)
data = bytearray(random(64))
data[32:64] = blake2b(bytes(data[:32]), digest_size=32,
person=b"THOR", encoder=RawEncoder)
assert verify_digest(bytes(data))
def TestRelayData():
data = random(128)
digest = compute_digest(data)
cell_body = RelayDataCellBody(data)
assert cell_body.data == data
assert cell_body.digest == digest
serial = cell_body.serialize()
assert serial == data + digest
data = random(31)
triggered = False
try:
cell_body = RelayDataCellBody.deserialize(data)
except AssertionError:
triggered = True
assert triggered
data = random(64)
cell_body = RelayDataCellBody.deserialize(data)
assert cell_body.data == data[0:32]
assert cell_body.digest == compute_digest(data[0:32])
def TestRelayBegin():
port = 50051
hostname = "hostname"
cell_body = RelayBeginCellBody(port, hostname)
assert cell_body.port == port
assert cell_body.hostname == hostname
assert cell_body.digest == compute_digest(port.to_bytes(
2, byteorder='little') + bytes(hostname, 'utf-8'))
port = 65536
triggered = False
try:
cell_body = RelayBeginCellBody(port, hostname)
except ValueError:
triggered = True
assert triggered
serial_data = b"\x83\xc3\x68\x6f\x73\x74\x6e\x61\x6d\x65" + \
compute_digest(b"\x83\xc3\x68\x6f\x73\x74\x6e\x61\x6d\x65")
cell_body = RelayBeginCellBody.deserialize(serial_data)
assert cell_body.port == 50051
assert cell_body.hostname == "hostname"
def TestRelayConnected():
status = 0
cell_body = RelayConnectedCellBody(status)
assert cell_body.status == status
assert cell_body.digest == compute_digest(status.to_bytes(
2, byteorder='little'))
status = 65536
triggered = False
try:
cell_body = RelayConnectedCellBody(status)
except ValueError:
triggered = True
assert triggered
serial_data = b"\x00\x00" + compute_digest(b"\x00\x00")
cell_body = RelayConnectedCellBody.deserialize(serial_data)
assert cell_body.status == 0
def TestRelayExtend():
next_or_ip = random(4)
pk = random(32)
cell_body = RelayExtendCellBody(next_or_ip, pk)
assert cell_body.next_or_ip == next_or_ip
assert cell_body.pk == pk
assert cell_body.digest == compute_digest(next_or_ip + pk)
next_or_ip = random(5)
pk = random(32)
triggered = False
try:
cell_body = RelayExtendCellBody(next_or_ip, pk)
except ValueError:
triggered = True
assert triggered
next_or_ip = random(4)
pk = random(64)
triggered = False
try:
cell_body = RelayExtendCellBody(next_or_ip, pk)
except ValueError:
triggered = True
assert triggered
pk = random(32)
serial_data = b"\x01\x02\x03\x04" + pk + \
compute_digest(b"\x01\x02\x03\x04" + pk)
cell_body = RelayExtendCellBody.deserialize(serial_data)
assert cell_body.next_or_ip == b"\x01\x02\x03\x04"
assert cell_body.pk == pk
def TestRelayExtended():
pk = random(32)
hash = random(32)
signature = random(64)
cell_body = RelayExtendedCellBody(pk, hash, signature)
assert cell_body.pk == pk
assert cell_body.hash == hash
assert cell_body.signature == signature
assert cell_body.digest == compute_digest(pk + hash + signature)
pk = random(33)
hash = random(32)
signature = random(64)
triggered = False
try:
cell_body = RelayExtendedCellBody(pk, hash, signature)
except ValueError:
triggered = True
assert triggered
pk = random(32)
hash = random(53)
signature = random(64)
triggered = False
try:
cell_body = RelayExtendedCellBody(pk, hash, signature)
except ValueError:
triggered = True
assert triggered
pk = random(32)
hash = random(32)
signature = random(1)
triggered = False
try:
cell_body = RelayExtendedCellBody(pk, hash, signature)
except ValueError:
triggered = True
assert triggered
serial_data = random(160)
cell_body = RelayExtendedCellBody.deserialize(serial_data)
assert cell_body.pk == serial_data[0:32]
assert cell_body.hash == serial_data[32:64]
assert cell_body.signature == serial_data[64:128]
def TestDirectoryChallengeInit():
pk = random(32)
nonce = random(32)
cell_body = DirectoryChallengeInitCellBody(pk, nonce)
assert cell_body.pk == pk
assert cell_body.nonce == nonce
pk = random(33)
nonce = random(32)
triggered = False
try:
cell_body = DirectoryChallengeInitCellBody(pk, nonce)
except ValueError:
triggered = True
assert triggered
pk = random(32)
nonce = random(256)
triggered = False
try:
cell_body = DirectoryChallengeInitCellBody(pk, nonce)
except ValueError:
triggered = True
assert triggered
serial_data = random(64)
cell_body = DirectoryChallengeInitCellBody.deserialize(serial_data)
assert cell_body.pk == serial_data[0:32]
assert cell_body.nonce == serial_data[32:64]
def TestDirectoryChallengeRequest():
nonce = random(32)
signature = random(64)
cell_body = DirectoryChallengeRequestCellBody(nonce, signature)
assert cell_body.nonce == nonce
assert cell_body.signature == signature
nonce = random(93)
signature = random(64)
triggered = False
try:
cell_body = DirectoryChallengeRequestCellBody(nonce, signature)
except ValueError:
triggered = True
assert triggered
nonce = random(32)
signature = random(72)
triggered = False
try:
cell_body = DirectoryChallengeRequestCellBody(nonce, signature)
except ValueError:
triggered = True
assert triggered
serial_data = random(96)
cell_body = DirectoryChallengeRequestCellBody.deserialize(serial_data)
assert cell_body.nonce == serial_data[0:32]
assert cell_body.signature == serial_data[32:96]
def TestDirectoryChallengeResponse():
signature = random(64)
cell_body = DirectoryChallengeResponseCellBody(signature)
assert cell_body.signature == signature
signature = random(52)
triggered = False
try:
cell_body = DirectoryChallengeResponseCellBody(signature)
except ValueError:
triggered = True
assert triggered
serial_data = random(64)
cell_body = DirectoryChallengeResponseCellBody.deserialize(serial_data)
assert cell_body.signature == serial_data
def TestDirectoryChallengeAck():
status = 0
cell_body = DirectoryChallengeAckCellBody(status)
assert cell_body.status == status
status = 65536
triggered = False
try:
cell_body = DirectoryChallengeAckCellBody(status)
except ValueError:
triggered = True
assert triggered
serial_data = b"\x04\x03"
cell_body = DirectoryChallengeAckCellBody.deserialize(serial_data)
assert cell_body.status == 772
def main():
TestCellHeader()
PrintPass("CellHeader")
TestCreate()
PrintPass("CreateCellBody")
TestCreated()
PrintPass("CreatedCellBody")
TestDestroy()
PrintPass("DestroyCellBody")
TestComputeDigest()
PrintPass("ComputeDigest")
TestVerifyDigest()
PrintPass("VerifyDigest")
TestRelayData()
PrintPass("RelayDataCellBody")
TestRelayBegin()
PrintPass("RelayBeginCellBody")
TestRelayConnected()
PrintPass("RelayConnectedCellBody")
TestRelayExtend()
PrintPass("RelayExtendCellBody")
TestRelayExtended()
PrintPass("RelayExtendedCellBody")
TestDirectoryChallengeInit()
PrintPass("DirectoryChallengeInit")
TestDirectoryChallengeRequest()
PrintPass("DirectoryChallengeRequest")
TestDirectoryChallengeResponse()
PrintPass("DirectoryChallengeResponse")
TestDirectoryChallengeAck()
PrintPass("DirectoryChallengeAck")
if __name__ == "__main__":
main()