~bzr-pqm/bzr/bzr.dev

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