1
# Copyright (C) 2009, 2010, 2011 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
17
"""Tests for _chk_map_*."""
23
from bzrlib.static_tuple import StaticTuple
27
def load_tests(standard_tests, module, loader):
28
suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
29
'bzrlib._chk_map_py', 'bzrlib._chk_map_pyx')
33
class TestSearchKeys(tests.TestCase):
35
module = None # Filled in by test parameterization
37
def assertSearchKey16(self, expected, key):
38
self.assertEqual(expected, self.module._search_key_16(key))
40
def assertSearchKey255(self, expected, key):
41
actual = self.module._search_key_255(key)
42
self.assertEqual(expected, actual, 'actual: %r' % (actual,))
44
def test_simple_16(self):
45
self.assertSearchKey16('8C736521', stuple('foo',))
46
self.assertSearchKey16('8C736521\x008C736521', stuple('foo', 'foo'))
47
self.assertSearchKey16('8C736521\x0076FF8CAA', stuple('foo', 'bar'))
48
self.assertSearchKey16('ED82CD11', stuple('abcd',))
50
def test_simple_255(self):
51
self.assertSearchKey255('\x8cse!', stuple('foo',))
52
self.assertSearchKey255('\x8cse!\x00\x8cse!', stuple('foo', 'foo'))
53
self.assertSearchKey255('\x8cse!\x00v\xff\x8c\xaa', stuple('foo', 'bar'))
54
# The standard mapping for these would include '\n', so it should be
56
self.assertSearchKey255('\xfdm\x93_\x00P_\x1bL', stuple('<', 'V'))
58
def test_255_does_not_include_newline(self):
59
# When mapping via _search_key_255, we should never have the '\n'
60
# character, but all other 255 values should be present
62
for char_in in range(256):
63
search_key = self.module._search_key_255(stuple(chr(char_in),))
64
chars_used.update(search_key)
65
all_chars = set([chr(x) for x in range(256)])
66
unused_chars = all_chars.symmetric_difference(chars_used)
67
self.assertEqual(set('\n'), unused_chars)
70
class TestDeserialiseLeafNode(tests.TestCase):
74
def assertDeserialiseErrors(self, text):
75
self.assertRaises((ValueError, IndexError),
76
self.module._deserialise_leaf_node, text, 'not-a-real-sha')
78
def test_raises_on_non_leaf(self):
79
self.assertDeserialiseErrors('')
80
self.assertDeserialiseErrors('short\n')
81
self.assertDeserialiseErrors('chknotleaf:\n')
82
self.assertDeserialiseErrors('chkleaf:x\n')
83
self.assertDeserialiseErrors('chkleaf:\n')
84
self.assertDeserialiseErrors('chkleaf:\nnotint\n')
85
self.assertDeserialiseErrors('chkleaf:\n10\n')
86
self.assertDeserialiseErrors('chkleaf:\n10\n256\n')
87
self.assertDeserialiseErrors('chkleaf:\n10\n256\n10\n')
89
def test_deserialise_empty(self):
90
node = self.module._deserialise_leaf_node(
91
"chkleaf:\n10\n1\n0\n\n", stuple("sha1:1234",))
92
self.assertEqual(0, len(node))
93
self.assertEqual(10, node.maximum_size)
94
self.assertEqual(("sha1:1234",), node.key())
95
self.assertIsInstance(node.key(), StaticTuple)
96
self.assertIs(None, node._search_prefix)
97
self.assertIs(None, node._common_serialised_prefix)
99
def test_deserialise_items(self):
100
node = self.module._deserialise_leaf_node(
101
"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
103
self.assertEqual(2, len(node))
104
self.assertEqual([(("foo bar",), "baz"), (("quux",), "blarh")],
105
sorted(node.iteritems(None)))
107
def test_deserialise_item_with_null_width_1(self):
108
node = self.module._deserialise_leaf_node(
109
"chkleaf:\n0\n1\n2\n\nfoo\x001\nbar\x00baz\nquux\x001\nblarh\n",
111
self.assertEqual(2, len(node))
112
self.assertEqual([(("foo",), "bar\x00baz"), (("quux",), "blarh")],
113
sorted(node.iteritems(None)))
115
def test_deserialise_item_with_null_width_2(self):
116
node = self.module._deserialise_leaf_node(
117
"chkleaf:\n0\n2\n2\n\nfoo\x001\x001\nbar\x00baz\n"
118
"quux\x00\x001\nblarh\n",
120
self.assertEqual(2, len(node))
121
self.assertEqual([(("foo", "1"), "bar\x00baz"), (("quux", ""), "blarh")],
122
sorted(node.iteritems(None)))
124
def test_iteritems_selected_one_of_two_items(self):
125
node = self.module._deserialise_leaf_node(
126
"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
128
self.assertEqual(2, len(node))
129
self.assertEqual([(("quux",), "blarh")],
130
sorted(node.iteritems(None, [("quux",), ("qaz",)])))
132
def test_deserialise_item_with_common_prefix(self):
133
node = self.module._deserialise_leaf_node(
134
"chkleaf:\n0\n2\n2\nfoo\x00\n1\x001\nbar\x00baz\n2\x001\nblarh\n",
136
self.assertEqual(2, len(node))
137
self.assertEqual([(("foo", "1"), "bar\x00baz"), (("foo", "2"), "blarh")],
138
sorted(node.iteritems(None)))
139
self.assertIs(chk_map._unknown, node._search_prefix)
140
self.assertEqual('foo\x00', node._common_serialised_prefix)
142
def test_deserialise_multi_line(self):
143
node = self.module._deserialise_leaf_node(
144
"chkleaf:\n0\n2\n2\nfoo\x00\n1\x002\nbar\nbaz\n2\x002\nblarh\n\n",
146
self.assertEqual(2, len(node))
147
self.assertEqual([(("foo", "1"), "bar\nbaz"),
148
(("foo", "2"), "blarh\n"),
149
], sorted(node.iteritems(None)))
150
self.assertIs(chk_map._unknown, node._search_prefix)
151
self.assertEqual('foo\x00', node._common_serialised_prefix)
153
def test_key_after_map(self):
154
node = self.module._deserialise_leaf_node(
155
"chkleaf:\n10\n1\n0\n\n", ("sha1:1234",))
156
node.map(None, ("foo bar",), "baz quux")
157
self.assertEqual(None, node.key())
159
def test_key_after_unmap(self):
160
node = self.module._deserialise_leaf_node(
161
"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
163
node.unmap(None, ("foo bar",))
164
self.assertEqual(None, node.key())
167
class TestDeserialiseInternalNode(tests.TestCase):
171
def assertDeserialiseErrors(self, text):
172
self.assertRaises((ValueError, IndexError),
173
self.module._deserialise_internal_node, text,
174
stuple('not-a-real-sha',))
176
def test_raises_on_non_internal(self):
177
self.assertDeserialiseErrors('')
178
self.assertDeserialiseErrors('short\n')
179
self.assertDeserialiseErrors('chknotnode:\n')
180
self.assertDeserialiseErrors('chknode:x\n')
181
self.assertDeserialiseErrors('chknode:\n')
182
self.assertDeserialiseErrors('chknode:\nnotint\n')
183
self.assertDeserialiseErrors('chknode:\n10\n')
184
self.assertDeserialiseErrors('chknode:\n10\n256\n')
185
self.assertDeserialiseErrors('chknode:\n10\n256\n10\n')
186
# no trailing newline
187
self.assertDeserialiseErrors('chknode:\n10\n256\n0\n1\nfo')
189
def test_deserialise_one(self):
190
node = self.module._deserialise_internal_node(
191
"chknode:\n10\n1\n1\n\na\x00sha1:abcd\n", stuple('sha1:1234',))
192
self.assertIsInstance(node, chk_map.InternalNode)
193
self.assertEqual(1, len(node))
194
self.assertEqual(10, node.maximum_size)
195
self.assertEqual(("sha1:1234",), node.key())
196
self.assertEqual('', node._search_prefix)
197
self.assertEqual({'a': ('sha1:abcd',)}, node._items)
199
def test_deserialise_with_prefix(self):
200
node = self.module._deserialise_internal_node(
201
"chknode:\n10\n1\n1\npref\na\x00sha1:abcd\n", stuple('sha1:1234',))
202
self.assertIsInstance(node, chk_map.InternalNode)
203
self.assertEqual(1, len(node))
204
self.assertEqual(10, node.maximum_size)
205
self.assertEqual(("sha1:1234",), node.key())
206
self.assertEqual('pref', node._search_prefix)
207
self.assertEqual({'prefa': ('sha1:abcd',)}, node._items)
209
node = self.module._deserialise_internal_node(
210
"chknode:\n10\n1\n1\npref\n\x00sha1:abcd\n", stuple('sha1:1234',))
211
self.assertIsInstance(node, chk_map.InternalNode)
212
self.assertEqual(1, len(node))
213
self.assertEqual(10, node.maximum_size)
214
self.assertEqual(("sha1:1234",), node.key())
215
self.assertEqual('pref', node._search_prefix)
216
self.assertEqual({'pref': ('sha1:abcd',)}, node._items)
218
def test_deserialise_pref_with_null(self):
219
node = self.module._deserialise_internal_node(
220
"chknode:\n10\n1\n1\npref\x00fo\n\x00sha1:abcd\n",
221
stuple('sha1:1234',))
222
self.assertIsInstance(node, chk_map.InternalNode)
223
self.assertEqual(1, len(node))
224
self.assertEqual(10, node.maximum_size)
225
self.assertEqual(("sha1:1234",), node.key())
226
self.assertEqual('pref\x00fo', node._search_prefix)
227
self.assertEqual({'pref\x00fo': ('sha1:abcd',)}, node._items)
229
def test_deserialise_with_null_pref(self):
230
node = self.module._deserialise_internal_node(
231
"chknode:\n10\n1\n1\npref\x00fo\n\x00\x00sha1:abcd\n",
232
stuple('sha1:1234',))
233
self.assertIsInstance(node, chk_map.InternalNode)
234
self.assertEqual(1, len(node))
235
self.assertEqual(10, node.maximum_size)
236
self.assertEqual(("sha1:1234",), node.key())
237
self.assertEqual('pref\x00fo', node._search_prefix)
238
self.assertEqual({'pref\x00fo\x00': ('sha1:abcd',)}, node._items)
241
class Test_BytesToTextKey(tests.TestCase):
243
def assertBytesToTextKey(self, key, bytes):
244
self.assertEqual(key,
245
self.module._bytes_to_text_key(bytes))
247
def assertBytesToTextKeyRaises(self, bytes):
248
# These are invalid bytes, and we want to make sure the code under test
249
# raises an exception rather than segfaults, etc. We don't particularly
250
# care what exception.
251
self.assertRaises(Exception, self.module._bytes_to_text_key, bytes)
254
self.assertBytesToTextKey(('file-id', 'revision-id'),
255
'file: file-id\nparent-id\nname\nrevision-id\n'
256
'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
258
def test_invalid_no_kind(self):
259
self.assertBytesToTextKeyRaises(
260
'file file-id\nparent-id\nname\nrevision-id\n'
261
'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
263
def test_invalid_no_space(self):
264
self.assertBytesToTextKeyRaises(
265
'file:file-id\nparent-id\nname\nrevision-id\n'
266
'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
268
def test_invalid_too_short_file_id(self):
269
self.assertBytesToTextKeyRaises('file:file-id')
271
def test_invalid_too_short_parent_id(self):
272
self.assertBytesToTextKeyRaises('file:file-id\nparent-id')
274
def test_invalid_too_short_name(self):
275
self.assertBytesToTextKeyRaises('file:file-id\nparent-id\nname')
278
self.assertBytesToTextKey(('dir-id', 'revision-id'),
279
'dir: dir-id\nparent-id\nname\nrevision-id')