/brz/remove-bazaar

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