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
|
import unittest
import random
import math
import io
from . import tnetstring
import struct
MAXINT = 2 ** (struct.Struct('i').size * 8 - 1) - 1
FORMAT_EXAMPLES = {
b'0:}': {},
b'0:]': [],
b'51:5:hello,39:11:12345678901#4:this,4:true!0:~4:\x00\x00\x00\x00,]}':
{'hello': [12345678901, b'this', True, None, b'\x00\x00\x00\x00']},
b'5:12345#': 12345,
b'12:this is cool,': b'this is cool',
b'0:,': b'',
b'0:~': None,
b'4:true!': True,
b'5:false!': False,
b'10:\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00,': b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
b'24:5:12345#5:67890#5:xxxxx,]': [12345, 67890, b'xxxxx'],
b'18:3:0.1^3:0.2^3:0.3^]': [0.1, 0.2, 0.3],
b'243:238:233:228:223:218:213:208:203:198:193:188:183:178:173:168:163:158:153:148:143:138:133:128:123:118:113:108:103:99:95:91:87:83:79:75:71:67:63:59:55:51:47:43:39:35:31:27:23:19:15:11:hello-there,]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]': [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[b'hello-there']]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
}
def get_random_object(random=random, depth=0):
"""Generate a random serializable object."""
# The probability of generating a scalar value increases as the depth increase.
# This ensures that we bottom out eventually.
if random.randint(depth,10) <= 4:
what = random.randint(0,1)
if what == 0:
n = random.randint(0,10)
l = []
for _ in range(n):
l.append(get_random_object(random,depth+1))
return l
if what == 1:
n = random.randint(0,10)
d = {}
for _ in range(n):
n = random.randint(0,100)
k = str([random.randint(32,126) for _ in range(n)])
d[k] = get_random_object(random,depth+1)
return d
else:
what = random.randint(0,4)
if what == 0:
return None
if what == 1:
return True
if what == 2:
return False
if what == 3:
if random.randint(0,1) == 0:
return random.randint(0,MAXINT)
else:
return -1 * random.randint(0,MAXINT)
n = random.randint(0,100)
return bytes([random.randint(32,126) for _ in range(n)])
class Test_Format(unittest.TestCase):
def test_roundtrip_format_examples(self):
for data, expect in FORMAT_EXAMPLES.items():
self.assertEqual(expect,tnetstring.loads(data))
self.assertEqual(expect,tnetstring.loads(tnetstring.dumps(expect)))
self.assertEqual((expect,b''),tnetstring.pop(data))
def test_roundtrip_format_random(self):
for _ in range(500):
v = get_random_object()
self.assertEqual(v,tnetstring.loads(tnetstring.dumps(v)))
self.assertEqual((v,b""),tnetstring.pop(tnetstring.dumps(v)))
def test_unicode_handling(self):
with self.assertRaises(ValueError):
tnetstring.dumps("hello")
self.assertEqual(tnetstring.dumps("hello".encode()),b"5:hello,")
self.assertEqual(type(tnetstring.loads(b"5:hello,")),bytes)
def test_roundtrip_format_unicode(self):
for _ in range(500):
v = get_random_object()
self.assertEqual(v,tnetstring.loads(tnetstring.dumps(v)))
self.assertEqual((v,b''),tnetstring.pop(tnetstring.dumps(v)))
def test_roundtrip_big_integer(self):
i1 = math.factorial(30000)
s = tnetstring.dumps(i1)
i2 = tnetstring.loads(s)
self.assertEqual(i1, i2)
class Test_FileLoading(unittest.TestCase):
def test_roundtrip_file_examples(self):
for data, expect in FORMAT_EXAMPLES.items():
s = io.BytesIO()
s.write(data)
s.write(b'OK')
s.seek(0)
self.assertEqual(expect,tnetstring.load(s))
self.assertEqual(b'OK',s.read())
s = io.BytesIO()
tnetstring.dump(expect,s)
s.write(b'OK')
s.seek(0)
self.assertEqual(expect,tnetstring.load(s))
self.assertEqual(b'OK',s.read())
def test_roundtrip_file_random(self):
for _ in range(500):
v = get_random_object()
s = io.BytesIO()
tnetstring.dump(v,s)
s.write(b'OK')
s.seek(0)
self.assertEqual(v,tnetstring.load(s))
self.assertEqual(b'OK',s.read())
def test_error_on_absurd_lengths(self):
s = io.BytesIO()
s.write(b'1000000000:pwned!,')
s.seek(0)
with self.assertRaises(ValueError):
tnetstring.load(s)
self.assertEqual(s.read(1),b':')
def suite():
loader = unittest.TestLoader()
suite = unittest.TestSuite()
suite.addTest(loader.loadTestsFromTestCase(Test_Format))
suite.addTest(loader.loadTestsFromTestCase(Test_FileLoading))
return suite
|