30
33
_test_needs_features = [compiled_btreeparser_feature]
33
super(TestBtreeSerializer, self).setUp()
34
self.module = compiled_btreeparser_feature.module
37
return compiled_btreeparser_feature.module
37
40
class TestHexAndUnhex(TestBtreeSerializer):
45
48
mod_unhex = self.module._py_unhexlify(as_hex)
46
49
if ba_unhex != mod_unhex:
47
50
if mod_unhex is None:
50
53
mod_hex = binascii.hexlify(mod_unhex)
51
54
self.fail('_py_unhexlify returned a different answer'
52
' from binascii:\n %s\n != %s'
55
' from binascii:\n %r\n != %r'
53
56
% (binascii.hexlify(ba_unhex), mod_hex))
55
58
def assertFailUnhexlify(self, as_hex):
57
60
self.assertIs(None, self.module._py_unhexlify(as_hex))
59
62
def test_to_hex(self):
60
raw_bytes = ''.join(map(chr, range(256)))
63
raw_bytes = b''.join(map(int2byte, range(256)))
61
64
for i in range(0, 240, 20):
62
65
self.assertHexlify(raw_bytes[i:i+20])
63
66
self.assertHexlify(raw_bytes[240:]+raw_bytes[0:4])
65
68
def test_from_hex(self):
66
self.assertUnhexlify('0123456789abcdef0123456789abcdef01234567')
67
self.assertUnhexlify('123456789abcdef0123456789abcdef012345678')
68
self.assertUnhexlify('0123456789ABCDEF0123456789ABCDEF01234567')
69
self.assertUnhexlify('123456789ABCDEF0123456789ABCDEF012345678')
70
hex_chars = binascii.hexlify(''.join(map(chr, range(256))))
69
self.assertUnhexlify(b'0123456789abcdef0123456789abcdef01234567')
70
self.assertUnhexlify(b'123456789abcdef0123456789abcdef012345678')
71
self.assertUnhexlify(b'0123456789ABCDEF0123456789ABCDEF01234567')
72
self.assertUnhexlify(b'123456789ABCDEF0123456789ABCDEF012345678')
73
hex_chars = binascii.hexlify(b''.join(map(int2byte, range(256))))
71
74
for i in range(0, 480, 40):
72
75
self.assertUnhexlify(hex_chars[i:i+40])
73
76
self.assertUnhexlify(hex_chars[480:]+hex_chars[0:8])
75
78
def test_from_invalid_hex(self):
76
self.assertFailUnhexlify('123456789012345678901234567890123456789X')
77
self.assertFailUnhexlify('12345678901234567890123456789012345678X9')
80
_hex_form = '123456789012345678901234567890abcdefabcd'
79
self.assertFailUnhexlify(b'123456789012345678901234567890123456789X')
80
self.assertFailUnhexlify(b'12345678901234567890123456789012345678X9')
82
def test_bad_argument(self):
83
self.assertRaises(ValueError, self.module._py_unhexlify, u'1a')
84
self.assertRaises(ValueError, self.module._py_unhexlify, b'1b')
87
_hex_form = b'123456789012345678901234567890abcdefabcd'
82
89
class Test_KeyToSha1(TestBtreeSerializer):
95
102
% (actual_sha1, expected))
97
104
def test_simple(self):
98
self.assertKeyToSha1(_hex_form, ('sha1:' + _hex_form,))
105
self.assertKeyToSha1(_hex_form, (b'sha1:' + _hex_form,))
100
107
def test_invalid_not_tuple(self):
101
108
self.assertKeyToSha1(None, _hex_form)
102
self.assertKeyToSha1(None, 'sha1:' + _hex_form)
109
self.assertKeyToSha1(None, b'sha1:' + _hex_form)
104
111
def test_invalid_empty(self):
105
112
self.assertKeyToSha1(None, ())
111
118
def test_invalid_not_sha1(self):
112
119
self.assertKeyToSha1(None, (_hex_form,))
113
self.assertKeyToSha1(None, ('sha2:' + _hex_form,))
120
self.assertKeyToSha1(None, (b'sha2:' + _hex_form,))
115
122
def test_invalid_not_hex(self):
116
123
self.assertKeyToSha1(None,
117
('sha1:abcdefghijklmnopqrstuvwxyz12345678901234',))
124
(b'sha1:abcdefghijklmnopqrstuvwxyz12345678901234',))
120
127
class Test_Sha1ToKey(TestBtreeSerializer):
122
129
def assertSha1ToKey(self, hex_sha1):
123
130
bin_sha1 = binascii.unhexlify(hex_sha1)
124
131
key = self.module._py_sha1_to_key(bin_sha1)
125
self.assertEqual(('sha1:' + hex_sha1,), key)
132
self.assertEqual((b'sha1:' + hex_sha1,), key)
127
134
def test_simple(self):
128
135
self.assertSha1ToKey(_hex_form)
131
_one_key_content = """type=leaf
138
_one_key_content = b"""type=leaf
132
139
sha1:123456789012345678901234567890abcdefabcd\x00\x001 2 3 4
135
_large_offsets = """type=leaf
142
_large_offsets = b"""type=leaf
136
143
sha1:123456789012345678901234567890abcdefabcd\x00\x0012345678901 1234567890 0 1
137
144
sha1:abcd123456789012345678901234567890abcdef\x00\x002147483648 2147483647 0 1
138
145
sha1:abcdefabcd123456789012345678901234567890\x00\x004294967296 4294967295 4294967294 1
141
_multi_key_content = """type=leaf
148
_multi_key_content = b"""type=leaf
142
149
sha1:c80c881d4a26984ddce795f6f71817c9cf4480e7\x00\x000 0 0 0
143
150
sha1:c86f7e437faa5a7fce15d1ddcb9eaeaea377667b\x00\x001 1 1 1
144
151
sha1:c8e240de74fb1ed08fa08d38063f6a6a91462a81\x00\x002 2 2 2
149
156
sha1:cf7a9e24777ec23212c54d7a350bc5bea5477fdb\x00\x007 7 7 7
152
_multi_key_same_offset = """type=leaf
159
_multi_key_same_offset = b"""type=leaf
153
160
sha1:080c881d4a26984ddce795f6f71817c9cf4480e7\x00\x000 0 0 0
154
161
sha1:c86f7e437faa5a7fce15d1ddcb9eaeaea377667b\x00\x001 1 1 1
155
162
sha1:cd0c9035898dd52fc65c41454cec9c4d2611bfb3\x00\x002 2 2 2
160
167
sha1:ce93b4e3c464ffd51732fbd6ded717e9efda28aa\x00\x007 7 7 7
163
_common_32_bits = """type=leaf
170
_common_32_bits = b"""type=leaf
164
171
sha1:123456784a26984ddce795f6f71817c9cf4480e7\x00\x000 0 0 0
165
172
sha1:1234567874fb1ed08fa08d38063f6a6a91462a81\x00\x001 1 1 1
166
173
sha1:12345678777ec23212c54d7a350bc5bea5477fdb\x00\x002 2 2 2
175
182
class TestGCCKHSHA1LeafNode(TestBtreeSerializer):
177
def assertInvalid(self, bytes):
184
def assertInvalid(self, data):
178
185
"""Ensure that we get a proper error when trying to parse invalid bytes.
180
187
(mostly this is testing that bad input doesn't cause us to segfault)
182
self.assertRaises((ValueError, TypeError),
183
self.module._parse_into_chk, bytes, 1, 0)
190
(ValueError, TypeError), self.module._parse_into_chk, data, 1, 0)
185
def test_non_str(self):
192
def test_non_bytes(self):
186
193
self.assertInvalid(u'type=leaf\n')
188
195
def test_not_leaf(self):
189
self.assertInvalid('type=internal\n')
196
self.assertInvalid(b'type=internal\n')
191
198
def test_empty_leaf(self):
192
leaf = self.module._parse_into_chk('type=leaf\n', 1, 0)
199
leaf = self.module._parse_into_chk(b'type=leaf\n', 1, 0)
193
200
self.assertEqual(0, len(leaf))
194
201
self.assertEqual([], leaf.all_items())
195
202
self.assertEqual([], leaf.all_keys())
199
206
def test_one_key_leaf(self):
200
207
leaf = self.module._parse_into_chk(_one_key_content, 1, 0)
201
208
self.assertEqual(1, len(leaf))
202
sha_key = ('sha1:' + _hex_form,)
209
sha_key = (b'sha1:' + _hex_form,)
203
210
self.assertEqual([sha_key], leaf.all_keys())
204
self.assertEqual([(sha_key, ('1 2 3 4', ()))], leaf.all_items())
211
self.assertEqual([(sha_key, (b'1 2 3 4', ()))], leaf.all_items())
205
212
self.assertTrue(sha_key in leaf)
207
214
def test_large_offsets(self):
208
215
leaf = self.module._parse_into_chk(_large_offsets, 1, 0)
209
self.assertEqual(['12345678901 1234567890 0 1',
210
'2147483648 2147483647 0 1',
211
'4294967296 4294967295 4294967294 1',
216
self.assertEqual([b'12345678901 1234567890 0 1',
217
b'2147483648 2147483647 0 1',
218
b'4294967296 4294967295 4294967294 1',
212
219
], [x[1][0] for x in leaf.all_items()])
214
221
def test_many_key_leaf(self):
217
224
all_keys = leaf.all_keys()
218
225
self.assertEqual(8, len(leaf.all_keys()))
219
226
for idx, key in enumerate(all_keys):
220
self.assertEqual(str(idx), leaf[key][0].split()[0])
227
self.assertEqual(b'%d' % idx, leaf[key][0].split()[0])
222
229
def test_common_shift(self):
223
230
# The keys were deliberately chosen so that the first 5 bits all
235
242
for idx, val in enumerate(lst):
236
243
self.assertEqual(idx, offsets[val])
237
244
for idx, key in enumerate(leaf.all_keys()):
238
self.assertEqual(str(idx), leaf[key][0].split()[0])
245
self.assertEqual(b'%d' % idx, leaf[key][0].split()[0])
240
247
def test_multi_key_same_offset(self):
241
248
# there is no common prefix, though there are some common bits
250
257
self.assertEqual(lst.index(val), offsets[val])
251
258
for idx, key in enumerate(leaf.all_keys()):
252
self.assertEqual(str(idx), leaf[key][0].split()[0])
259
self.assertEqual(b'%d' % idx, leaf[key][0].split()[0])
254
261
def test_all_common_prefix(self):
255
262
# The first 32 bits of all hashes are the same. This is going to be
264
271
self.assertEqual(lst.index(val), offsets[val])
265
272
for idx, key in enumerate(leaf.all_keys()):
266
self.assertEqual(str(idx), leaf[key][0].split()[0])
273
self.assertEqual(b'%d' % idx, leaf[key][0].split()[0])
268
275
def test_many_entries(self):
269
276
# Again, this is almost impossible, but we should still work
270
277
# It would be hard to fit more that 120 entries in a 4k page, much less
271
278
# more than 256 of them. but hey, weird stuff happens sometimes
272
lines = ['type=leaf\n']
279
lines = [b'type=leaf\n']
273
280
for i in range(500):
274
key_str = 'sha1:%04x%s' % (i, _hex_form[:36])
281
key_str = b'sha1:%04x%s' % (i, _hex_form[:36])
276
lines.append('%s\0\0%d %d %d %d\n' % (key_str, i, i, i, i))
277
bytes = ''.join(lines)
278
leaf = self.module._parse_into_chk(bytes, 1, 0)
283
lines.append(b'%s\0\0%d %d %d %d\n' % (key_str, i, i, i, i))
284
data = b''.join(lines)
285
leaf = self.module._parse_into_chk(data, 1, 0)
279
286
self.assertEqual(24-7, leaf.common_shift)
280
287
offsets = leaf._get_offsets()
281
288
# This is the interesting bits for each entry
291
298
self.assertEqual(lst.index(val), offsets[val])
292
299
for idx, key in enumerate(leaf.all_keys()):
293
self.assertEqual(str(idx), leaf[key][0].split()[0])
300
self.assertEqual(b'%d' % idx, leaf[key][0].split()[0])
295
302
def test__sizeof__(self):
296
303
# We can't use the exact numbers because of platform variations, etc.
297
304
# But what we really care about is that it does get bigger with more
299
leaf0 = self.module._parse_into_chk('type=leaf\n', 1, 0)
306
leaf0 = self.module._parse_into_chk(b'type=leaf\n', 1, 0)
300
307
leaf1 = self.module._parse_into_chk(_one_key_content, 1, 0)
301
308
leafN = self.module._parse_into_chk(_multi_key_content, 1, 0)
302
309
sizeof_1 = leaf1.__sizeof__() - leaf0.__sizeof__()