42
45
self.assertEqual(expected, actual, 'actual: %r' % (actual,))
44
47
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',))
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',))
50
53
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
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'))
54
57
# The standard mapping for these would include '\n', so it should be
56
self.assertSearchKey255('\xfdm\x93_\x00P_\x1bL', stuple('<', 'V'))
59
self.assertSearchKey255(b'\xfdm\x93_\x00P_\x1bL', stuple(b'<', b'V'))
58
61
def test_255_does_not_include_newline(self):
59
62
# When mapping via _search_key_255, we should never have the '\n'
60
63
# character, but all other 255 values should be present
62
65
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
search_key = self.module._search_key_255(stuple(int2byte(char_in),))
67
chars_used.update(map(int2byte, bytearray(search_key)))
68
all_chars = {int2byte(x) for x in range(256)}
66
69
unused_chars = all_chars.symmetric_difference(chars_used)
67
self.assertEqual(set('\n'), unused_chars)
70
self.assertEqual({b'\n'}, unused_chars)
70
73
class TestDeserialiseLeafNode(tests.TestCase):
74
77
def assertDeserialiseErrors(self, text):
75
78
self.assertRaises((ValueError, IndexError),
76
self.module._deserialise_leaf_node, text, 'not-a-real-sha')
79
self.module._deserialise_leaf_node, text, b'not-a-real-sha')
78
81
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')
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')
89
92
def test_deserialise_empty(self):
90
93
node = self.module._deserialise_leaf_node(
91
"chkleaf:\n10\n1\n0\n\n", stuple("sha1:1234",))
94
b"chkleaf:\n10\n1\n0\n\n", stuple(b"sha1:1234",))
92
95
self.assertEqual(0, len(node))
93
96
self.assertEqual(10, node.maximum_size)
94
self.assertEqual(("sha1:1234",), node.key())
97
self.assertEqual((b"sha1:1234",), node.key())
95
98
self.assertIsInstance(node.key(), StaticTuple)
96
99
self.assertIs(None, node._search_prefix)
97
100
self.assertIs(None, node._common_serialised_prefix)
99
102
def test_deserialise_items(self):
100
103
node = self.module._deserialise_leaf_node(
101
"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
104
b"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
103
106
self.assertEqual(2, len(node))
104
self.assertEqual([(("foo bar",), "baz"), (("quux",), "blarh")],
107
self.assertEqual([((b"foo bar",), b"baz"), ((b"quux",), b"blarh")],
105
108
sorted(node.iteritems(None)))
107
110
def test_deserialise_item_with_null_width_1(self):
108
111
node = self.module._deserialise_leaf_node(
109
"chkleaf:\n0\n1\n2\n\nfoo\x001\nbar\x00baz\nquux\x001\nblarh\n",
112
b"chkleaf:\n0\n1\n2\n\nfoo\x001\nbar\x00baz\nquux\x001\nblarh\n",
111
114
self.assertEqual(2, len(node))
112
self.assertEqual([(("foo",), "bar\x00baz"), (("quux",), "blarh")],
115
self.assertEqual([((b"foo",), b"bar\x00baz"), ((b"quux",), b"blarh")],
113
116
sorted(node.iteritems(None)))
115
118
def test_deserialise_item_with_null_width_2(self):
116
119
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
b"chkleaf:\n0\n2\n2\n\nfoo\x001\x001\nbar\x00baz\n"
121
b"quux\x00\x001\nblarh\n",
120
123
self.assertEqual(2, len(node))
121
self.assertEqual([(("foo", "1"), "bar\x00baz"), (("quux", ""), "blarh")],
124
self.assertEqual([((b"foo", b"1"), b"bar\x00baz"), ((b"quux", b""), b"blarh")],
122
125
sorted(node.iteritems(None)))
124
127
def test_iteritems_selected_one_of_two_items(self):
125
128
node = self.module._deserialise_leaf_node(
126
"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
129
b"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
128
131
self.assertEqual(2, len(node))
129
self.assertEqual([(("quux",), "blarh")],
130
sorted(node.iteritems(None, [("quux",), ("qaz",)])))
132
self.assertEqual([((b"quux",), b"blarh")],
133
sorted(node.iteritems(None, [(b"quux",), (b"qaz",)])))
132
135
def test_deserialise_item_with_common_prefix(self):
133
136
node = self.module._deserialise_leaf_node(
134
"chkleaf:\n0\n2\n2\nfoo\x00\n1\x001\nbar\x00baz\n2\x001\nblarh\n",
137
b"chkleaf:\n0\n2\n2\nfoo\x00\n1\x001\nbar\x00baz\n2\x001\nblarh\n",
136
139
self.assertEqual(2, len(node))
137
self.assertEqual([(("foo", "1"), "bar\x00baz"), (("foo", "2"), "blarh")],
140
self.assertEqual([((b"foo", b"1"), b"bar\x00baz"), ((b"foo", b"2"), b"blarh")],
138
141
sorted(node.iteritems(None)))
139
142
self.assertIs(chk_map._unknown, node._search_prefix)
140
self.assertEqual('foo\x00', node._common_serialised_prefix)
143
self.assertEqual(b'foo\x00', node._common_serialised_prefix)
142
145
def test_deserialise_multi_line(self):
143
146
node = self.module._deserialise_leaf_node(
144
"chkleaf:\n0\n2\n2\nfoo\x00\n1\x002\nbar\nbaz\n2\x002\nblarh\n\n",
147
b"chkleaf:\n0\n2\n2\nfoo\x00\n1\x002\nbar\nbaz\n2\x002\nblarh\n\n",
146
149
self.assertEqual(2, len(node))
147
self.assertEqual([(("foo", "1"), "bar\nbaz"),
148
(("foo", "2"), "blarh\n"),
150
self.assertEqual([((b"foo", b"1"), b"bar\nbaz"),
151
((b"foo", b"2"), b"blarh\n"),
149
152
], sorted(node.iteritems(None)))
150
153
self.assertIs(chk_map._unknown, node._search_prefix)
151
self.assertEqual('foo\x00', node._common_serialised_prefix)
154
self.assertEqual(b'foo\x00', node._common_serialised_prefix)
153
156
def test_key_after_map(self):
154
157
node = self.module._deserialise_leaf_node(
155
"chkleaf:\n10\n1\n0\n\n", ("sha1:1234",))
156
node.map(None, ("foo bar",), "baz quux")
158
b"chkleaf:\n10\n1\n0\n\n", (b"sha1:1234",))
159
node.map(None, (b"foo bar",), b"baz quux")
157
160
self.assertEqual(None, node.key())
159
162
def test_key_after_unmap(self):
160
163
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
b"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
166
node.unmap(None, (b"foo bar",))
164
167
self.assertEqual(None, node.key())
171
174
def assertDeserialiseErrors(self, text):
172
175
self.assertRaises((ValueError, IndexError),
173
176
self.module._deserialise_internal_node, text,
174
stuple('not-a-real-sha',))
177
stuple(b'not-a-real-sha',))
176
179
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')
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')
186
189
# no trailing newline
187
self.assertDeserialiseErrors('chknode:\n10\n256\n0\n1\nfo')
190
self.assertDeserialiseErrors(b'chknode:\n10\n256\n0\n1\nfo')
189
192
def test_deserialise_one(self):
190
193
node = self.module._deserialise_internal_node(
191
"chknode:\n10\n1\n1\n\na\x00sha1:abcd\n", stuple('sha1:1234',))
194
b"chknode:\n10\n1\n1\n\na\x00sha1:abcd\n", stuple(b'sha1:1234',))
192
195
self.assertIsInstance(node, chk_map.InternalNode)
193
196
self.assertEqual(1, len(node))
194
197
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)
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)
199
202
def test_deserialise_with_prefix(self):
200
203
node = self.module._deserialise_internal_node(
201
"chknode:\n10\n1\n1\npref\na\x00sha1:abcd\n", stuple('sha1:1234',))
204
b"chknode:\n10\n1\n1\npref\na\x00sha1:abcd\n",
205
stuple(b'sha1:1234',))
202
206
self.assertIsInstance(node, chk_map.InternalNode)
203
207
self.assertEqual(1, len(node))
204
208
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
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)
209
213
node = self.module._deserialise_internal_node(
210
"chknode:\n10\n1\n1\npref\n\x00sha1:abcd\n", stuple('sha1:1234',))
214
b"chknode:\n10\n1\n1\npref\n\x00sha1:abcd\n",
215
stuple(b'sha1:1234',))
211
216
self.assertIsInstance(node, chk_map.InternalNode)
212
217
self.assertEqual(1, len(node))
213
218
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)
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)
218
223
def test_deserialise_pref_with_null(self):
219
224
node = self.module._deserialise_internal_node(
220
"chknode:\n10\n1\n1\npref\x00fo\n\x00sha1:abcd\n",
221
stuple('sha1:1234',))
225
b"chknode:\n10\n1\n1\npref\x00fo\n\x00sha1:abcd\n",
226
stuple(b'sha1:1234',))
222
227
self.assertIsInstance(node, chk_map.InternalNode)
223
228
self.assertEqual(1, len(node))
224
229
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)
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)
229
234
def test_deserialise_with_null_pref(self):
230
235
node = self.module._deserialise_internal_node(
231
"chknode:\n10\n1\n1\npref\x00fo\n\x00\x00sha1:abcd\n",
232
stuple('sha1:1234',))
236
b"chknode:\n10\n1\n1\npref\x00fo\n\x00\x00sha1:abcd\n",
237
stuple(b'sha1:1234',))
233
238
self.assertIsInstance(node, chk_map.InternalNode)
234
239
self.assertEqual(1, len(node))
235
240
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
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)
246
class Test_BytesToTextKey(tests.TestCase):
248
def assertBytesToTextKey(self, key, bytes):
249
self.assertEqual(key,
250
self.module._bytes_to_text_key(bytes))
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)
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')
263
def test_invalid_no_kind(self):
264
self.assertBytesToTextKeyRaises(
265
b'file file-id\nparent-id\nname\nrevision-id\n'
266
b'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
268
def test_invalid_no_space(self):
269
self.assertBytesToTextKeyRaises(
270
b'file:file-id\nparent-id\nname\nrevision-id\n'
271
b'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
273
def test_invalid_too_short_file_id(self):
274
self.assertBytesToTextKeyRaises(b'file:file-id')
276
def test_invalid_too_short_parent_id(self):
277
self.assertBytesToTextKeyRaises(b'file:file-id\nparent-id')
279
def test_invalid_too_short_name(self):
280
self.assertBytesToTextKeyRaises(b'file:file-id\nparent-id\nname')
283
self.assertBytesToTextKey((b'dir-id', b'revision-id'),
284
b'dir: dir-id\nparent-id\nname\nrevision-id')