1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
# Copyright (C) 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Tests for bzrlib.pack."""
from cStringIO import StringIO
from bzrlib import pack, errors, tests
class TestContainerWriter(tests.TestCase):
def test_construct(self):
"""Test constructing a ContainerWriter.
This uses None as the output stream to show that the constructor doesn't
try to use the output stream.
"""
writer = pack.ContainerWriter(None)
def test_begin(self):
"""The begin() method writes the container format marker line."""
output = StringIO()
writer = pack.ContainerWriter(output.write)
writer.begin()
self.assertEqual('Bazaar pack format 1\n', output.getvalue())
def test_end(self):
"""The end() method writes an End Marker record."""
output = StringIO()
writer = pack.ContainerWriter(output.write)
writer.begin()
writer.end()
self.assertEqual('Bazaar pack format 1\nE', output.getvalue())
def test_add_bytes_record_no_name(self):
"""Add a bytes record with no name."""
output = StringIO()
writer = pack.ContainerWriter(output.write)
writer.begin()
writer.add_bytes_record('abc', names=[])
self.assertEqual('Bazaar pack format 1\nB3\n\nabc', output.getvalue())
def test_add_bytes_record_one_name(self):
"""Add a bytes record with one name."""
output = StringIO()
writer = pack.ContainerWriter(output.write)
writer.begin()
writer.add_bytes_record('abc', names=['name1'])
self.assertEqual('Bazaar pack format 1\nB3\nname1\n\nabc',
output.getvalue())
def test_add_bytes_record_two_names(self):
"""Add a bytes record with two names."""
output = StringIO()
writer = pack.ContainerWriter(output.write)
writer.begin()
writer.add_bytes_record('abc', names=['name1', 'name2'])
self.assertEqual('Bazaar pack format 1\nB3\nname1\nname2\n\nabc',
output.getvalue())
def test_add_bytes_record_invalid_name(self):
"""Adding a Bytes record with a name with whitespace in it raises
InvalidRecordError.
"""
output = StringIO()
writer = pack.ContainerWriter(output.write)
writer.begin()
self.assertRaises(
errors.InvalidRecordError,
writer.add_bytes_record, 'abc', names=['bad name'])
class TestContainerReader(tests.TestCase):
def get_reader_for(self, bytes):
stream = StringIO(bytes)
reader = pack.ContainerReader(stream.read)
return reader
def test_construct(self):
"""Test constructing a ContainerReader.
This uses None as the output stream to show that the constructor doesn't
try to use the input stream.
"""
reader = pack.ContainerReader(None)
def test_empty_container(self):
"""Read an empty container."""
reader = self.get_reader_for("Bazaar pack format 1\nE")
self.assertEqual([], list(reader.iter_records()))
def test_unknown_format(self):
"""Unrecognised container formats raise UnknownContainerFormatError."""
reader = self.get_reader_for("unknown format\n")
self.assertRaises(
errors.UnknownContainerFormatError, reader.iter_records)
def test_unexpected_end_of_container(self):
"""Containers that don't end with an End Marker record should cause
UnexpectedEndOfContainerError to be raised.
"""
reader = self.get_reader_for("Bazaar pack format 1\n")
iterator = reader.iter_records()
self.assertRaises(
errors.UnexpectedEndOfContainerError, iterator.next)
def test_unknown_record_type(self):
"""Unknown record types cause UnknownRecordTypeError to be raised."""
reader = self.get_reader_for("Bazaar pack format 1\nX")
iterator = reader.iter_records()
self.assertRaises(
errors.UnknownRecordTypeError, iterator.next)
def test_container_with_one_unnamed_record(self):
"""Read a container with one Bytes record.
Parsing Bytes records is more thoroughly exercised by
TestBytesRecordReader. This test is here to ensure that
ContainerReader's integration with BytesRecordReader is working.
"""
reader = self.get_reader_for("Bazaar pack format 1\nB5\n\naaaaaE")
expected_records = [([], 'aaaaa')]
self.assertEqual(
expected_records,
[(names, read_bytes(None))
for (names, read_bytes) in reader.iter_records()])
def test_validate_empty_container(self):
"""validate does not raise an error for a container with no records."""
reader = self.get_reader_for("Bazaar pack format 1\nE")
# No exception raised
reader.validate()
def test_validate_non_empty_valid_container(self):
"""validate does not raise an error for a container with a valid record.
"""
reader = self.get_reader_for("Bazaar pack format 1\nB3\nname\n\nabcE")
# No exception raised
reader.validate()
def test_validate_bad_format(self):
"""validate raises an error for unrecognised format strings.
It may raise either UnexpectedEndOfContainerError or
UnknownContainerFormatError, depending on exactly what the string is.
"""
inputs = ["", "x", "Bazaar pack format 1", "bad\n"]
for input in inputs:
reader = self.get_reader_for(input)
self.assertRaises(
(errors.UnexpectedEndOfContainerError,
errors.UnknownContainerFormatError),
reader.validate)
def test_validate_bad_record_marker(self):
"""validate raises UnknownRecordTypeError for unrecognised record
types.
"""
reader = self.get_reader_for("Bazaar pack format 1\nX")
self.assertRaises(errors.UnknownRecordTypeError, reader.validate)
def test_validate_data_after_end_marker(self):
"""validate raises ContainerHasExcessDataError if there are any bytes
after the end of the container.
"""
reader = self.get_reader_for("Bazaar pack format 1\nEcrud")
self.assertRaises(
errors.ContainerHasExcessDataError, reader.validate)
def test_validate_no_end_marker(self):
"""validate raises UnexpectedEndOfContainerError if there's no end of
container marker, even if the container up to this point has been valid.
"""
reader = self.get_reader_for("Bazaar pack format 1\n")
self.assertRaises(
errors.UnexpectedEndOfContainerError, reader.validate)
def test_validate_duplicate_name(self):
"""validate raises DuplicateRecordNameError if the same name occurs
multiple times in the container.
"""
reader = self.get_reader_for(
"Bazaar pack format 1\n"
"B0\nname\n\n"
"B0\nname\n\n"
"E")
self.assertRaises(errors.DuplicateRecordNameError, reader.validate)
def test_validate_undecodeable_name(self):
"""Names that aren't valid UTF-8 cause validate to fail."""
reader = self.get_reader_for("Bazaar pack format 1\nB0\n\xcc\n\nE")
self.assertRaises(errors.InvalidRecordError, reader.validate)
class TestBytesRecordReader(tests.TestCase):
"""Tests for reading and validating Bytes records with BytesRecordReader."""
def get_reader_for(self, bytes):
stream = StringIO(bytes)
reader = pack.BytesRecordReader(stream.read)
return reader
def test_record_with_no_name(self):
"""Reading a Bytes record with no name returns an empty list of
names.
"""
reader = self.get_reader_for("5\n\naaaaa")
names, get_bytes = reader.read()
self.assertEqual([], names)
self.assertEqual('aaaaa', get_bytes(None))
def test_record_with_one_name(self):
"""Reading a Bytes record with one name returns a list of just that
name.
"""
reader = self.get_reader_for("5\nname1\n\naaaaa")
names, get_bytes = reader.read()
self.assertEqual(['name1'], names)
self.assertEqual('aaaaa', get_bytes(None))
def test_record_with_two_names(self):
"""Reading a Bytes record with two names returns a list of both names.
"""
reader = self.get_reader_for("5\nname1\nname2\n\naaaaa")
names, get_bytes = reader.read()
self.assertEqual(['name1', 'name2'], names)
self.assertEqual('aaaaa', get_bytes(None))
def test_invalid_length(self):
"""If the length-prefix is not a number, parsing raises
InvalidRecordError.
"""
reader = self.get_reader_for("not a number\n")
self.assertRaises(errors.InvalidRecordError, reader.read)
def test_early_eof(self):
"""Tests for premature EOF occuring during parsing Bytes records with
BytesRecordReader.
A incomplete container might be interrupted at any point. The
BytesRecordReader needs to cope with the input stream running out no
matter where it is in the parsing process.
In all cases, UnexpectedEndOfContainerError should be raised.
"""
complete_record = "6\nname\n\nabcdef"
for count in range(0, len(complete_record)):
incomplete_record = complete_record[:count]
reader = self.get_reader_for(incomplete_record)
# We don't use assertRaises to make diagnosing failures easier
# (assertRaises doesn't allow a custom failure message).
try:
names, read_bytes = reader.read()
read_bytes(None)
except errors.UnexpectedEndOfContainerError:
pass
else:
self.fail(
"UnexpectedEndOfContainerError not raised when parsing %r"
% (incomplete_record,))
def test_initial_eof(self):
"""EOF before any bytes read at all."""
reader = self.get_reader_for("")
self.assertRaises(errors.UnexpectedEndOfContainerError, reader.read)
def test_eof_after_length(self):
"""EOF after reading the length and before reading name(s)."""
reader = self.get_reader_for("123\n")
self.assertRaises(errors.UnexpectedEndOfContainerError, reader.read)
def test_eof_during_name(self):
"""EOF during reading a name."""
reader = self.get_reader_for("123\nname")
self.assertRaises(errors.UnexpectedEndOfContainerError, reader.read)
def test_read_invalid_name_whitespace(self):
"""Names must have no whitespace."""
# A name with a space.
reader = self.get_reader_for("0\nbad name\n\n")
self.assertRaises(errors.InvalidRecordError, reader.read)
# A name with a tab.
reader = self.get_reader_for("0\nbad\tname\n\n")
self.assertRaises(errors.InvalidRecordError, reader.read)
# A name with a vertical tab.
reader = self.get_reader_for("0\nbad\vname\n\n")
self.assertRaises(errors.InvalidRecordError, reader.read)
def test_validate_whitespace_in_name(self):
"""Names must have no whitespace."""
reader = self.get_reader_for("0\nbad name\n\n")
self.assertRaises(errors.InvalidRecordError, reader.validate)
def test_validate_interrupted_prelude(self):
"""EOF during reading a record's prelude causes validate to fail."""
reader = self.get_reader_for("")
self.assertRaises(
errors.UnexpectedEndOfContainerError, reader.validate)
def test_validate_interrupted_body(self):
"""EOF during reading a record's body causes validate to fail."""
reader = self.get_reader_for("1\n\n")
self.assertRaises(
errors.UnexpectedEndOfContainerError, reader.validate)
def test_validate_unparseable_length(self):
"""An unparseable record length causes validate to fail."""
reader = self.get_reader_for("\n\n")
self.assertRaises(
errors.InvalidRecordError, reader.validate)
def test_validate_undecodeable_name(self):
"""Names that aren't valid UTF-8 cause validate to fail."""
reader = self.get_reader_for("0\n\xcc\n\n")
self.assertRaises(errors.InvalidRecordError, reader.validate)
def test_read_max_length(self):
"""If the max_length passed to the callable returned by read is not
None, then no more than that many bytes will be read.
"""
reader = self.get_reader_for("6\n\nabcdef")
names, get_bytes = reader.read()
self.assertEqual('abc', get_bytes(3))
def test_read_no_max_length(self):
"""If the max_length passed to the callable returned by read is None,
then all the bytes in the record will be read.
"""
reader = self.get_reader_for("6\n\nabcdef")
names, get_bytes = reader.read()
self.assertEqual('abcdef', get_bytes(None))
def test_repeated_read_calls(self):
"""Repeated calls to the callable returned from BytesRecordReader.read
will not read beyond the end of the record.
"""
reader = self.get_reader_for("6\n\nabcdefB3\nnext-record\nXXX")
names, get_bytes = reader.read()
self.assertEqual('abcdef', get_bytes(None))
self.assertEqual('', get_bytes(None))
self.assertEqual('', get_bytes(99))
|