1
# Copyright (C) 2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Direct tests of the btree serializer extension"""
23
from bzrlib import tests
25
from bzrlib.tests.test_btree_index import compiled_btreeparser_feature
28
class TestBtreeSerializer(tests.TestCase):
30
_test_needs_features = [compiled_btreeparser_feature]
33
super(TestBtreeSerializer, self).setUp()
34
self.module = compiled_btreeparser_feature.module
37
class TestHexAndUnhex(TestBtreeSerializer):
39
def assertHexlify(self, as_binary):
40
self.assertEqual(binascii.hexlify(as_binary),
41
self.module._test_hexlify(as_binary))
43
def assertUnhexlify(self, as_hex):
44
ba_unhex = binascii.unhexlify(as_hex)
45
mod_unhex = self.module._test_unhexlify(as_hex)
46
if ba_unhex != mod_unhex:
50
mod_hex = binascii.hexlify(mod_unhex)
51
self.fail('_test_unhexlify returned a different answer'
52
' from binascii:\n %s\n != %s'
53
% (binascii.hexlify(ba_unhex), mod_hex))
55
def assertFailUnhexlify(self, as_hex):
57
self.assertIs(None, self.module._test_unhexlify(as_hex))
59
def test_to_hex(self):
60
raw_bytes = ''.join(map(chr, range(256)))
61
for i in range(0, 240, 20):
62
self.assertHexlify(raw_bytes[i:i+20])
63
self.assertHexlify(raw_bytes[240:]+raw_bytes[0:4])
65
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))))
71
for i in range(0, 480, 40):
72
self.assertUnhexlify(hex_chars[i:i+40])
73
self.assertUnhexlify(hex_chars[480:]+hex_chars[0:8])
75
def test_from_invalid_hex(self):
76
self.assertFailUnhexlify('123456789012345678901234567890123456789X')
77
self.assertFailUnhexlify('12345678901234567890123456789012345678X9')
80
_hex_form = '123456789012345678901234567890abcdefabcd'
82
class Test_KeyToSha1(TestBtreeSerializer):
84
def assertKeyToSha1(self, expected, key):
88
expected_bin = binascii.unhexlify(expected)
89
actual_sha1 = self.module._test_key_to_sha1(key)
90
if expected_bin != actual_sha1:
91
actual_hex_sha1 = None
92
if actual_sha1 is not None:
93
actual_hex_sha1 = binascii.hexlify(actual_sha1)
94
self.fail('_key_to_sha1 returned:\n %s\n != %s'
95
% (actual_sha1, expected))
97
def test_simple(self):
98
self.assertKeyToSha1(_hex_form, ('sha1:' + _hex_form,))
100
def test_invalid_not_tuple(self):
101
self.assertKeyToSha1(None, _hex_form)
102
self.assertKeyToSha1(None, 'sha1:' + _hex_form)
104
def test_invalid_empty(self):
105
self.assertKeyToSha1(None, ())
107
def test_invalid_not_string(self):
108
self.assertKeyToSha1(None, (None,))
109
self.assertKeyToSha1(None, (list(_hex_form),))
111
def test_invalid_not_sha1(self):
112
self.assertKeyToSha1(None, (_hex_form,))
113
self.assertKeyToSha1(None, ('sha2:' + _hex_form,))
115
def test_invalid_not_hex(self):
116
self.assertKeyToSha1(None,
117
('sha1:abcdefghijklmnopqrstuvwxyz12345678901234',))
120
class Test_Sha1ToKey(TestBtreeSerializer):
122
def assertSha1ToKey(self, hex_sha1):
123
bin_sha1 = binascii.unhexlify(hex_sha1)
124
key = self.module._test_sha1_to_key(bin_sha1)
125
self.assertEqual(('sha1:' + hex_sha1,), key)
127
def test_simple(self):
128
self.assertSha1ToKey(_hex_form)
131
_one_key_content = """type=leaf
132
sha1:123456789012345678901234567890abcdefabcd\x00\x001 2 3 4
135
_large_offsets = """type=leaf
136
sha1:123456789012345678901234567890abcdefabcd\x00\x0012345678901 1234567890 0 1
137
sha1:abcd123456789012345678901234567890abcdef\x00\x002147483648 2147483647 0 1
138
sha1:abcdefabcd123456789012345678901234567890\x00\x004294967296 4294967295 4294967294 1
141
_multi_key_content = """type=leaf
142
sha1:c80c881d4a26984ddce795f6f71817c9cf4480e7\x00\x000 0 0 0
143
sha1:c86f7e437faa5a7fce15d1ddcb9eaeaea377667b\x00\x001 1 1 1
144
sha1:c8e240de74fb1ed08fa08d38063f6a6a91462a81\x00\x002 2 2 2
145
sha1:cda39a3ee5e6b4b0d3255bfef95601890afd8070\x00\x003 3 3 3
146
sha1:cdf51e37c269aa94d38f93e537bf6e2020b21406\x00\x004 4 4 4
147
sha1:ce0c9035898dd52fc65c41454cec9c4d2611bfb3\x00\x005 5 5 5
148
sha1:ce93b4e3c464ffd51732fbd6ded717e9efda28aa\x00\x006 6 6 6
149
sha1:cf7a9e24777ec23212c54d7a350bc5bea5477fdb\x00\x007 7 7 7
152
class TestGCCKHSHA1LeafNode(TestBtreeSerializer):
154
def assertInvalid(self, bytes):
155
"""Ensure that we get a proper error when trying to parse invalid bytes.
157
(mostly this is testing that bad input doesn't cause us to segfault)
159
self.assertRaises((ValueError, TypeError),
160
self.module._parse_into_chk, bytes, 1, 0)
162
def test_non_str(self):
163
self.assertInvalid(u'type=leaf\n')
165
def test_not_leaf(self):
166
self.assertInvalid('type=internal\n')
168
def test_empty_leaf(self):
169
leaf = self.module._parse_into_chk('type=leaf\n', 1, 0)
170
self.assertEqual(0, len(leaf))
171
self.assertEqual([], leaf.all_items())
172
self.assertEqual([], leaf.all_keys())
173
# It should allow any key to be queried
174
self.assertFalse(('key',) in leaf)
176
def test_one_key_leaf(self):
177
leaf = self.module._parse_into_chk(_one_key_content, 1, 0)
178
self.assertEqual(1, len(leaf))
179
sha_key = ('sha1:' + _hex_form,)
180
self.assertEqual([sha_key], leaf.all_keys())
181
self.assertEqual([(sha_key, ('1 2 3 4', ()))], leaf.all_items())
182
self.assertTrue(sha_key in leaf)
184
def test_large_offsets(self):
185
leaf = self.module._parse_into_chk(_large_offsets, 1, 0)
186
self.assertEqual(['12345678901 1234567890 0 1',
187
'2147483648 2147483647 0 1',
188
'4294967296 4294967295 4294967294 1',
189
], [x[1][0] for x in leaf.all_items()])
191
def test_many_key_leaf(self):
192
leaf = self.module._parse_into_chk(_multi_key_content, 1, 0)
193
self.assertEqual(8, len(leaf))
194
all_keys = leaf.all_keys()
195
self.assertEqual(8, len(leaf.all_keys()))
196
for idx, key in enumerate(all_keys):
197
self.assertEqual(str(idx), leaf[key][0].split()[0])
199
def test_common_mask(self):
200
# The keys were deliberately chosen so that the first 5 bits all
201
# overlapped, it also happens that a later bit overlaps
202
# Note that by 'overlap' we mean that given bit is either on in all
203
# keys, or off in all keys
204
leaf = self.module._parse_into_chk(_multi_key_content, 1, 0)
205
self.assertEqual(hex(0xF8000100), hex(leaf.common_mask))
206
self.assertEqual(5, leaf.common_shift)
207
self.assertEqual(0xc8000000, leaf.common_bits)
208
# The interesting byte for each key is
209
# (defined as the 8-bits that come after the common prefix)
210
# [1, 13, 28, 180, 190, 193, 210, 239]
211
lst = [1, 13, 28, 180, 190, 193, 210, 239]
212
offsets = leaf._test_offsets
213
self.assertEqual([bisect.bisect_left(lst, x) for x in range(0, 257)],
215
for idx, val in enumerate(lst):
216
self.assertEqual(idx, offsets[val])