/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2006-2010, 2012, 2013, 2016 Canonical Ltd
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
16
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
17
"""Tests from HTTP response parsing.
18
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
19
The handle_response method read the response body of a GET request an returns
20
the corresponding RangeFile.
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
21
22
There are four different kinds of RangeFile:
23
- a whole file whose size is unknown, seen as a simple byte stream,
24
- a whole file whose size is known, we can't read past its end,
25
- a single range file, a part of a file with a start and a size,
26
- a multiple range file, several consecutive parts with known start offset
27
  and size.
28
29
Some properties are common to all kinds:
30
- seek can only be forward (its really a socket underneath),
31
- read can't cross ranges,
32
- successive ranges are taken into account transparently,
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
33
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
34
- the expected pattern of use is either seek(offset)+read(size) or a single
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
35
  read with no size specified. For multiple range files, multiple read() will
36
  return the corresponding ranges, trying to read further will raise
37
  InvalidHttpResponse.
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
38
"""
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
39
6791.2.3 by Jelmer Vernooij
Fix more imports.
40
try:
41
    import http.client as http_client
42
except ImportError:  # python < 3
43
    import httplib as http_client
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
44
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
45
from .. import (
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
46
    errors,
47
    tests,
48
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
49
from ..sixish import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
50
    BytesIO,
51
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
52
from ..transport.http import (
3104.3.4 by Vincent Ladeuil
Add test.
53
    response,
54
    _urllib2_wrappers,
55
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
56
from .file_utils import (
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
57
    FakeReadFile,
58
    )
3104.3.4 by Vincent Ladeuil
Add test.
59
60
61
class ReadSocket(object):
62
    """A socket-like object that can be given a predefined content."""
63
64
    def __init__(self, data):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
65
        self.readfile = BytesIO(data)
3104.3.4 by Vincent Ladeuil
Add test.
66
67
    def makefile(self, mode='r', bufsize=None):
68
        return self.readfile
69
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
70
3104.3.4 by Vincent Ladeuil
Add test.
71
class FakeHTTPConnection(_urllib2_wrappers.HTTPConnection):
72
73
    def __init__(self, sock):
74
        _urllib2_wrappers.HTTPConnection.__init__(self, 'localhost')
75
        # Set the socket to bypass the connection
76
        self.sock = sock
77
78
    def send(self, str):
79
        """Ignores the writes on the socket."""
80
        pass
81
82
6575.1.2 by Vincent Ladeuil
TDD backwards, works here ;)
83
class TestResponseFileIter(tests.TestCase):
84
85
    def test_iter_empty(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
86
        f = response.ResponseFile('empty', BytesIO())
6575.1.3 by Vincent Ladeuil
Simpler.
87
        self.assertEqual([], list(f))
6575.1.2 by Vincent Ladeuil
TDD backwards, works here ;)
88
89
    def test_iter_many(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
90
        f = response.ResponseFile('many', BytesIO(b'0\n1\nboo!\n'))
6575.1.3 by Vincent Ladeuil
Simpler.
91
        self.assertEqual(['0\n', '1\n', 'boo!\n'], list(f))
6575.1.2 by Vincent Ladeuil
TDD backwards, works here ;)
92
93
3104.3.4 by Vincent Ladeuil
Add test.
94
class TestHTTPConnection(tests.TestCase):
95
96
    def test_cleanup_pipe(self):
97
        sock = ReadSocket("""HTTP/1.1 200 OK\r
98
Content-Type: text/plain; charset=UTF-8\r
99
Content-Length: 18
100
\r
101
0123456789
102
garbage""")
103
        conn = FakeHTTPConnection(sock)
104
        # Simulate the request sending so that the connection will be able to
105
        # read the response.
106
        conn.putrequest('GET', 'http://localhost/fictious')
107
        conn.endheaders()
108
        # Now, get the response
109
        resp = conn.getresponse()
110
        # Read part of the response
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
111
        self.assertEqual('0123456789\n', resp.read(11))
3104.3.4 by Vincent Ladeuil
Add test.
112
        # Override the thresold to force the warning emission
113
        conn._range_warning_thresold = 6 # There are 7 bytes pending
114
        conn.cleanup_pipe()
4794.1.15 by Robert Collins
Review feedback.
115
        self.assertContainsRe(self.get_log(), 'Got a 200 response when asking')
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
116
117
118
class TestRangeFileMixin(object):
119
    """Tests for accessing the first range in a RangeFile."""
120
121
    # A simple string used to represent a file part (also called a range), in
122
    # which offsets are easy to calculate for test writers. It's used as a
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
123
    # building block with slight variations but basically 'a' is the first char
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
124
    # of the range and 'z' is the last.
125
    alpha = 'abcdefghijklmnopqrstuvwxyz'
126
127
    def test_can_read_at_first_access(self):
128
        """Test that the just created file can be read."""
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
129
        self.assertEqual(self.alpha, self._file.read())
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
130
131
    def test_seek_read(self):
132
        """Test seek/read inside the range."""
133
        f = self._file
134
        start = self.first_range_start
135
        # Before any use, tell() should be at the range start
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
136
        self.assertEqual(start, f.tell())
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
137
        cur = start # For an overall offset assertion
138
        f.seek(start + 3)
139
        cur += 3
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
140
        self.assertEqual('def', f.read(3))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
141
        cur += len('def')
142
        f.seek(4, 1)
143
        cur += 4
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
144
        self.assertEqual('klmn', f.read(4))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
145
        cur += len('klmn')
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
146
        # read(0) in the middle of a range
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
147
        self.assertEqual('', f.read(0))
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
148
        # seek in place
149
        here = f.tell()
150
        f.seek(0, 1)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
151
        self.assertEqual(here, f.tell())
152
        self.assertEqual(cur, f.tell())
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
153
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
154
    def test_read_zero(self):
155
        f = self._file
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
156
        self.assertEqual('', f.read(0))
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
157
        f.seek(10, 1)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
158
        self.assertEqual('', f.read(0))
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
159
160
    def test_seek_at_range_end(self):
161
        f = self._file
162
        f.seek(26, 1)
163
164
    def test_read_at_range_end(self):
165
        """Test read behaviour at range end."""
166
        f = self._file
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
167
        self.assertEqual(self.alpha, f.read())
168
        self.assertEqual('', f.read(0))
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
169
        self.assertRaises(errors.InvalidRange, f.read, 1)
170
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
171
    def test_unbounded_read_after_seek(self):
172
        f = self._file
173
        f.seek(24, 1)
174
        # Should not cross ranges
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
175
        self.assertEqual('yz', f.read())
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
176
177
    def test_seek_backwards(self):
178
        f = self._file
179
        start = self.first_range_start
180
        f.seek(start)
181
        f.read(12)
182
        self.assertRaises(errors.InvalidRange, f.seek, start + 5)
183
184
    def test_seek_outside_single_range(self):
185
        f = self._file
186
        if f._size == -1 or f._boundary is not None:
187
            raise tests.TestNotApplicable('Needs a fully defined range')
188
        # Will seek past the range and then errors out
189
        self.assertRaises(errors.InvalidRange,
190
                          f.seek, self.first_range_start + 27)
191
192
    def test_read_past_end_of_range(self):
193
        f = self._file
194
        if f._size == -1:
195
            raise tests.TestNotApplicable("Can't check an unknown size")
196
        start = self.first_range_start
197
        f.seek(start + 20)
198
        self.assertRaises(errors.InvalidRange, f.read, 10)
199
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
200
    def test_seek_from_end(self):
201
       """Test seeking from the end of the file.
202
203
       The semantic is unclear in case of multiple ranges. Seeking from end
204
       exists only for the http transports, cannot be used if the file size is
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
205
       unknown and is not used in breezy itself. This test must be (and is)
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
206
       overridden by daughter classes.
207
208
       Reading from end makes sense only when a range has been requested from
209
       the end of the file (see HttpTransportBase._get() when using the
210
       'tail_amount' parameter). The HTTP response can only be a whole file or
211
       a single range.
212
       """
213
       f = self._file
214
       f.seek(-2, 2)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
215
       self.assertEqual('yz', f.read())
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
216
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
217
218
class TestRangeFileSizeUnknown(tests.TestCase, TestRangeFileMixin):
219
    """Test a RangeFile for a whole file whose size is not known."""
220
221
    def setUp(self):
222
        super(TestRangeFileSizeUnknown, self).setUp()
223
        self._file = response.RangeFile('Whole_file_size_known',
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
224
                                        BytesIO(self.alpha))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
225
        # We define no range, relying on RangeFile to provide default values
226
        self.first_range_start = 0 # It's the whole file
227
228
    def test_seek_from_end(self):
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
229
        """See TestRangeFileMixin.test_seek_from_end.
230
231
        The end of the file can't be determined since the size is unknown.
232
        """
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
233
        self.assertRaises(errors.InvalidRange, self._file.seek, -1, 2)
234
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
235
    def test_read_at_range_end(self):
236
        """Test read behaviour at range end."""
237
        f = self._file
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
238
        self.assertEqual(self.alpha, f.read())
239
        self.assertEqual('', f.read(0))
240
        self.assertEqual('', f.read(1))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
241
3537.1.1 by Vincent Ladeuil
Fix some more PEP8isms and delete useless import
242
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
243
class TestRangeFileSizeKnown(tests.TestCase, TestRangeFileMixin):
244
    """Test a RangeFile for a whole file whose size is known."""
245
246
    def setUp(self):
247
        super(TestRangeFileSizeKnown, self).setUp()
248
        self._file = response.RangeFile('Whole_file_size_known',
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
249
                                        BytesIO(self.alpha))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
250
        self._file.set_range(0, len(self.alpha))
251
        self.first_range_start = 0 # It's the whole file
252
253
254
class TestRangeFileSingleRange(tests.TestCase, TestRangeFileMixin):
255
    """Test a RangeFile for a single range."""
256
257
    def setUp(self):
258
        super(TestRangeFileSingleRange, self).setUp()
259
        self._file = response.RangeFile('Single_range_file',
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
260
                                        BytesIO(self.alpha))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
261
        self.first_range_start = 15
262
        self._file.set_range(self.first_range_start, len(self.alpha))
263
264
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
265
    def test_read_before_range(self):
266
        # This can't occur under normal circumstances, we have to force it
267
        f = self._file
268
        f._pos = 0 # Force an invalid pos
269
        self.assertRaises(errors.InvalidRange, f.read, 2)
270
3537.1.1 by Vincent Ladeuil
Fix some more PEP8isms and delete useless import
271
3146.3.2 by Vincent Ladeuil
Fix #179368 by keeping the current range hint on ShortReadvErrors.
272
class TestRangeFileMultipleRanges(tests.TestCase, TestRangeFileMixin):
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
273
    """Test a RangeFile for multiple ranges.
274
275
    The RangeFile used for the tests contains three ranges:
276
277
    - at offset 25: alpha
278
    - at offset 100: alpha
279
    - at offset 126: alpha.upper()
280
281
    The two last ranges are contiguous. This only rarely occurs (should not in
282
    fact) in real uses but may lead to hard to track bugs.
283
    """
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
284
285
    # The following is used to represent the boundary paramter defined
286
    # in HTTP response headers and the boundary lines that separate
287
    # multipart content.
288
289
    boundary = "separation"
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
290
291
    def setUp(self):
3146.3.2 by Vincent Ladeuil
Fix #179368 by keeping the current range hint on ShortReadvErrors.
292
        super(TestRangeFileMultipleRanges, self).setUp()
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
293
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
294
        boundary = self.boundary
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
295
296
        content = ''
297
        self.first_range_start = 25
298
        file_size = 200 # big enough to encompass all ranges
299
        for (start, part) in [(self.first_range_start, self.alpha),
300
                              # Two contiguous ranges
301
                              (100, self.alpha),
302
                              (126, self.alpha.upper())]:
303
            content += self._multipart_byterange(part, start, boundary,
304
                                                 file_size)
305
        # Final boundary
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
306
        content += self._boundary_line()
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
307
308
        self._file = response.RangeFile('Multiple_ranges_file',
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
309
                                        BytesIO(content))
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
310
        self.set_file_boundary()
311
312
    def _boundary_line(self):
313
        """Helper to build the formatted boundary line."""
314
        return '--' + self.boundary + '\r\n'
315
316
    def set_file_boundary(self):
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
317
        # Ranges are set by decoding the range headers, the RangeFile user is
318
        # supposed to call the following before using seek or read since it
319
        # requires knowing the *response* headers (in that case the boundary
320
        # which is part of the Content-Type header).
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
321
        self._file.set_boundary(self.boundary)
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
322
323
    def _multipart_byterange(self, data, offset, boundary, file_size='*'):
324
        """Encode a part of a file as a multipart/byterange MIME type.
325
326
        When a range request is issued, the HTTP response body can be
327
        decomposed in parts, each one representing a range (start, size) in a
328
        file.
329
330
        :param data: The payload.
331
        :param offset: where data starts in the file
332
        :param boundary: used to separate the parts
333
        :param file_size: the size of the file containing the range (default to
334
            '*' meaning unknown)
335
336
        :return: a string containing the data encoded as it will appear in the
337
            HTTP response body.
338
        """
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
339
        bline = self._boundary_line()
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
340
        # Each range begins with a boundary line
341
        range = bline
342
        # A range is described by a set of headers, but only 'Content-Range' is
343
        # required for our implementation (TestHandleResponse below will
344
        # exercise ranges with multiple or missing headers')
345
        range += 'Content-Range: bytes %d-%d/%d\r\n' % (offset,
346
                                                        offset+len(data)-1,
347
                                                        file_size)
348
        range += '\r\n'
349
        # Finally the raw bytes
350
        range += data
351
        return range
352
353
    def test_read_all_ranges(self):
354
        f = self._file
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
355
        self.assertEqual(self.alpha, f.read()) # Read first range
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
356
        f.seek(100) # Trigger the second range recognition
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
357
        self.assertEqual(self.alpha, f.read()) # Read second range
358
        self.assertEqual(126, f.tell())
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
359
        f.seek(126) # Start of third range which is also the current pos !
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
360
        self.assertEqual('A', f.read(1))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
361
        f.seek(10, 1)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
362
        self.assertEqual('LMN', f.read(3))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
363
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
364
    def test_seek_from_end(self):
365
        """See TestRangeFileMixin.test_seek_from_end."""
366
        # The actual implementation will seek from end for the first range only
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
367
        # and then fail. Since seeking from end is intended to be used for a
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
368
        # single range only anyway, this test just document the actual
369
        # behaviour.
370
        f = self._file
371
        f.seek(-2, 2)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
372
        self.assertEqual('yz', f.read())
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
373
        self.assertRaises(errors.InvalidRange, f.seek, -2, 2)
374
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
375
    def test_seek_into_void(self):
376
        f = self._file
377
        start = self.first_range_start
378
        f.seek(start)
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
379
        # Seeking to a point between two ranges is possible (only once) but
380
        # reading there is forbidden
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
381
        f.seek(start + 40)
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
382
        # We crossed a range boundary, so now the file is positioned at the
383
        # start of the new range (i.e. trying to seek below 100 will error out)
384
        f.seek(100)
3059.2.7 by Vincent Ladeuil
Allow pycurl users to watch the blinkenlights and fix a bug when ranges are contiguous.
385
        f.seek(125)
386
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
387
    def test_seek_across_ranges(self):
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
388
        f = self._file
389
        f.seek(126) # skip the two first ranges
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
390
        self.assertEqual('AB', f.read(2))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
391
3146.3.2 by Vincent Ladeuil
Fix #179368 by keeping the current range hint on ShortReadvErrors.
392
    def test_checked_read_dont_overflow_buffers(self):
393
        f = self._file
394
        # We force a very low value to exercise all code paths in _checked_read
395
        f._discarded_buf_size = 8
396
        f.seek(126) # skip the two first ranges
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
397
        self.assertEqual('AB', f.read(2))
3146.3.2 by Vincent Ladeuil
Fix #179368 by keeping the current range hint on ShortReadvErrors.
398
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
399
    def test_seek_twice_between_ranges(self):
400
        f = self._file
401
        start = self.first_range_start
402
        f.seek(start + 40) # Past the first range but before the second
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
403
        # Now the file is positioned at the second range start (100)
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
404
        self.assertRaises(errors.InvalidRange, f.seek, start + 41)
405
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
406
    def test_seek_at_range_end(self):
407
        """Test seek behavior at range end."""
408
        f = self._file
409
        f.seek(25 + 25)
410
        f.seek(100 + 25)
411
        f.seek(126 + 25)
412
413
    def test_read_at_range_end(self):
414
        f = self._file
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
415
        self.assertEqual(self.alpha, f.read())
416
        self.assertEqual(self.alpha, f.read())
417
        self.assertEqual(self.alpha.upper(), f.read())
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
418
        self.assertRaises(errors.InvalidHttpResponse, f.read, 1)
419
3537.1.1 by Vincent Ladeuil
Fix some more PEP8isms and delete useless import
420
3535.1.1 by Adrian Wilkins
Made the behaviour of the existing multi-range test more like the real thing by
421
class TestRangeFileMultipleRangesQuotedBoundaries(TestRangeFileMultipleRanges):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
422
    """Perform the same tests as TestRangeFileMultipleRanges, but uses
3535.1.1 by Adrian Wilkins
Made the behaviour of the existing multi-range test more like the real thing by
423
    an angle-bracket quoted boundary string like IIS 6.0 and 7.0
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
424
    (but not IIS 5, which breaks the RFC in a different way
425
    by using square brackets, not angle brackets)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
426
427
    This reveals a bug caused by
428
429
    - The bad implementation of RFC 822 unquoting in Python (angles are not
430
      quotes), coupled with
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
431
432
    - The bad implementation of RFC 2046 in IIS (angles are not permitted chars
433
      in boundary lines).
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
434
3535.1.1 by Adrian Wilkins
Made the behaviour of the existing multi-range test more like the real thing by
435
    """
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
436
    # The boundary as it appears in boundary lines
437
    # IIS 6 and 7 use this value
438
    _boundary_trimmed = "q1w2e3r4t5y6u7i8o9p0zaxscdvfbgnhmjklkl"
439
    boundary = '<' + _boundary_trimmed + '>'
440
441
    def set_file_boundary(self):
442
        # Emulate broken rfc822.unquote() here by removing angles
443
        self._file.set_boundary(self._boundary_trimmed)
3537.1.1 by Vincent Ladeuil
Fix some more PEP8isms and delete useless import
444
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
445
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
446
class TestRangeFileVarious(tests.TestCase):
447
    """Tests RangeFile aspects not covered elsewhere."""
448
449
    def test_seek_whence(self):
450
        """Test the seek whence parameter values."""
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
451
        f = response.RangeFile('foo', BytesIO(b'abc'))
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
452
        f.set_range(0, 3)
453
        f.seek(0)
454
        f.seek(1, 1)
455
        f.seek(-1, 2)
456
        self.assertRaises(ValueError, f.seek, 0, 14)
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
457
458
    def test_range_syntax(self):
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
459
        """Test the Content-Range scanning."""
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
460
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
461
        f = response.RangeFile('foo', BytesIO())
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
462
463
        def ok(expected, header_value):
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
464
            f.set_range_from_header(header_value)
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
465
            # Slightly peek under the covers to get the size
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
466
            self.assertEqual(expected, (f.tell(), f._size))
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
467
468
        ok((1, 10), 'bytes 1-10/11')
469
        ok((1, 10), 'bytes 1-10/*')
470
        ok((12, 2), '\tbytes 12-13/*')
471
        ok((28, 1), '  bytes 28-28/*')
472
        ok((2123, 2120), 'bytes  2123-4242/12310')
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
473
        ok((1, 10), 'bytes 1-10/ttt') # We don't check total (ttt)
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
474
475
        def nok(header_value):
476
            self.assertRaises(errors.InvalidHttpRange,
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
477
                              f.set_range_from_header, header_value)
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
478
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
479
        nok('bytes 10-2/3')
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
480
        nok('chars 1-2/3')
481
        nok('bytes xx-yyy/zzz')
482
        nok('bytes xx-12/zzz')
483
        nok('bytes 11-yy/zzz')
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
484
        nok('bytes10-2/3')
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
485
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
486
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
487
# Taken from real request responses
1786.1.26 by John Arbash Meinel
Update and test handle_response.
488
_full_text_response = (200, """HTTP/1.1 200 OK\r
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
489
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
490
Server: Apache/2.0.54 (Fedora)\r
491
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
492
ETag: "56691-23-38e9ae00"\r
493
Accept-Ranges: bytes\r
494
Content-Length: 35\r
495
Connection: close\r
496
Content-Type: text/plain; charset=UTF-8\r
497
\r
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
498
""", """Bazaar-NG meta directory, format 1
499
""")
500
501
1786.1.26 by John Arbash Meinel
Update and test handle_response.
502
_single_range_response = (206, """HTTP/1.1 206 Partial Content\r
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
503
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
504
Server: Apache/2.0.54 (Fedora)\r
505
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
506
ETag: "238a3c-16ec2-805c5540"\r
507
Accept-Ranges: bytes\r
508
Content-Length: 100\r
1786.1.26 by John Arbash Meinel
Update and test handle_response.
509
Content-Range: bytes 100-199/93890\r
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
510
Connection: close\r
511
Content-Type: text/plain; charset=UTF-8\r
512
\r
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
513
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06
1786.1.26 by John Arbash Meinel
Update and test handle_response.
514
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
515
516
2070.1.1 by John Arbash Meinel
Fix bug #62473 by not requiring content-type in range responses
517
_single_range_no_content_type = (206, """HTTP/1.1 206 Partial Content\r
518
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
519
Server: Apache/2.0.54 (Fedora)\r
520
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
521
ETag: "238a3c-16ec2-805c5540"\r
522
Accept-Ranges: bytes\r
523
Content-Length: 100\r
524
Content-Range: bytes 100-199/93890\r
525
Connection: close\r
526
\r
527
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06
528
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
529
530
1786.1.26 by John Arbash Meinel
Update and test handle_response.
531
_multipart_range_response = (206, """HTTP/1.1 206 Partial Content\r
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
532
Date: Tue, 11 Jul 2006 04:49:48 GMT\r
533
Server: Apache/2.0.54 (Fedora)\r
534
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
535
ETag: "238a3c-16ec2-805c5540"\r
536
Accept-Ranges: bytes\r
537
Content-Length: 1534\r
538
Connection: close\r
539
Content-Type: multipart/byteranges; boundary=418470f848b63279b\r
540
\r
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
541
\r""", """--418470f848b63279b\r
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
542
Content-type: text/plain; charset=UTF-8\r
543
Content-range: bytes 0-254/93890\r
544
\r
545
mbp@sourcefrog.net-20050309040815-13242001617e4a06
546
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e7627
547
mbp@sourcefrog.net-20050309040957-6cad07f466bb0bb8
548
mbp@sourcefrog.net-20050309041501-c840e09071de3b67
549
mbp@sourcefrog.net-20050309044615-c24a3250be83220a
550
\r
551
--418470f848b63279b\r
552
Content-type: text/plain; charset=UTF-8\r
553
Content-range: bytes 1000-2049/93890\r
554
\r
555
40-fd4ec249b6b139ab
556
mbp@sourcefrog.net-20050311063625-07858525021f270b
557
mbp@sourcefrog.net-20050311231934-aa3776aff5200bb9
558
mbp@sourcefrog.net-20050311231953-73aeb3a131c3699a
559
mbp@sourcefrog.net-20050311232353-f5e33da490872c6a
560
mbp@sourcefrog.net-20050312071639-0a8f59a34a024ff0
561
mbp@sourcefrog.net-20050312073432-b2c16a55e0d6e9fb
562
mbp@sourcefrog.net-20050312073831-a47c3335ece1920f
563
mbp@sourcefrog.net-20050312085412-13373aa129ccbad3
564
mbp@sourcefrog.net-20050313052251-2bf004cb96b39933
565
mbp@sourcefrog.net-20050313052856-3edd84094687cb11
566
mbp@sourcefrog.net-20050313053233-e30a4f28aef48f9d
567
mbp@sourcefrog.net-20050313053853-7c64085594ff3072
568
mbp@sourcefrog.net-20050313054757-a86c3f5871069e22
569
mbp@sourcefrog.net-20050313061422-418f1f73b94879b9
570
mbp@sourcefrog.net-20050313120651-497bd231b19df600
571
mbp@sourcefrog.net-20050314024931-eae0170ef25a5d1a
572
mbp@sourcefrog.net-20050314025438-d52099f915fe65fc
573
mbp@sourcefrog.net-20050314025539-637a636692c055cf
574
mbp@sourcefrog.net-20050314025737-55eb441f430ab4ba
575
mbp@sourcefrog.net-20050314025901-d74aa93bb7ee8f62
576
mbp@source\r
1979.1.1 by John Arbash Meinel
Fix bug #57723, parse boundary="" correctly, since Squid uses it
577
--418470f848b63279b--\r
578
""")
579
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
580
1979.1.1 by John Arbash Meinel
Fix bug #57723, parse boundary="" correctly, since Squid uses it
581
_multipart_squid_range_response = (206, """HTTP/1.0 206 Partial Content\r
582
Date: Thu, 31 Aug 2006 21:16:22 GMT\r
583
Server: Apache/2.2.2 (Unix) DAV/2\r
584
Last-Modified: Thu, 31 Aug 2006 17:57:06 GMT\r
585
Accept-Ranges: bytes\r
586
Content-Type: multipart/byteranges; boundary="squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196"\r
587
Content-Length: 598\r
588
X-Cache: MISS from localhost.localdomain\r
589
X-Cache-Lookup: HIT from localhost.localdomain:3128\r
590
Proxy-Connection: keep-alive\r
591
\r
592
""",
593
"""\r
594
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196\r
595
Content-Type: text/plain\r
596
Content-Range: bytes 0-99/18672\r
597
\r
598
# bzr knit index 8
599
600
scott@netsplit.com-20050708230047-47c7868f276b939f fulltext 0 863  :
601
scott@netsp\r
602
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196\r
603
Content-Type: text/plain\r
604
Content-Range: bytes 300-499/18672\r
605
\r
606
com-20050708231537-2b124b835395399a :
607
scott@netsplit.com-20050820234126-551311dbb7435b51 line-delta 1803 479 .scott@netsplit.com-20050820232911-dc4322a084eadf7e :
608
scott@netsplit.com-20050821213706-c86\r
609
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196--\r
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
610
""")
611
612
1786.1.26 by John Arbash Meinel
Update and test handle_response.
613
# This is made up
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
614
_full_text_response_no_content_type = (200, """HTTP/1.1 200 OK\r
615
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
616
Server: Apache/2.0.54 (Fedora)\r
617
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
618
ETag: "56691-23-38e9ae00"\r
619
Accept-Ranges: bytes\r
620
Content-Length: 35\r
621
Connection: close\r
622
\r
623
""", """Bazaar-NG meta directory, format 1
624
""")
625
626
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
627
_full_text_response_no_content_length = (200, """HTTP/1.1 200 OK\r
628
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
629
Server: Apache/2.0.54 (Fedora)\r
630
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
631
ETag: "56691-23-38e9ae00"\r
632
Accept-Ranges: bytes\r
633
Connection: close\r
634
Content-Type: text/plain; charset=UTF-8\r
635
\r
636
""", """Bazaar-NG meta directory, format 1
637
""")
638
639
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
640
_single_range_no_content_range = (206, """HTTP/1.1 206 Partial Content\r
641
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
642
Server: Apache/2.0.54 (Fedora)\r
643
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
644
ETag: "238a3c-16ec2-805c5540"\r
645
Accept-Ranges: bytes\r
646
Content-Length: 100\r
647
Connection: close\r
648
\r
649
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06
650
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
651
652
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
653
_single_range_response_truncated = (206, """HTTP/1.1 206 Partial Content\r
654
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
655
Server: Apache/2.0.54 (Fedora)\r
656
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
657
ETag: "238a3c-16ec2-805c5540"\r
658
Accept-Ranges: bytes\r
659
Content-Length: 100\r
660
Content-Range: bytes 100-199/93890\r
661
Connection: close\r
662
Content-Type: text/plain; charset=UTF-8\r
663
\r
664
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06""")
665
666
1786.1.26 by John Arbash Meinel
Update and test handle_response.
667
_invalid_response = (444, """HTTP/1.1 444 Bad Response\r
668
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
669
Connection: close\r
670
Content-Type: text/html; charset=iso-8859-1\r
671
\r
672
""", """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
673
<html><head>
674
<title>404 Not Found</title>
675
</head><body>
676
<h1>Not Found</h1>
677
<p>I don't know what I'm doing</p>
678
<hr>
679
</body></html>
680
""")
681
682
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
683
_multipart_no_content_range = (206, """HTTP/1.0 206 Partial Content\r
684
Content-Type: multipart/byteranges; boundary=THIS_SEPARATES\r
685
Content-Length: 598\r
686
\r
687
""",
688
"""\r
689
--THIS_SEPARATES\r
690
Content-Type: text/plain\r
691
\r
692
# bzr knit index 8
693
--THIS_SEPARATES\r
694
""")
695
696
697
_multipart_no_boundary = (206, """HTTP/1.0 206 Partial Content\r
698
Content-Type: multipart/byteranges; boundary=THIS_SEPARATES\r
699
Content-Length: 598\r
700
\r
701
""",
702
"""\r
703
--THIS_SEPARATES\r
704
Content-Type: text/plain\r
705
Content-Range: bytes 0-18/18672\r
706
\r
707
# bzr knit index 8
708
709
The range ended at the line above, this text is garbage instead of a boundary
710
line
711
""")
712
713
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
714
class TestHandleResponse(tests.TestCase):
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
715
716
    def _build_HTTPMessage(self, raw_headers):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
717
        status_and_headers = BytesIO(raw_headers)
3059.2.11 by Vincent Ladeuil
Fix typos mentioned by spiv.
718
        # Get rid of the status line
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
719
        status_and_headers.readline()
6791.2.3 by Jelmer Vernooij
Fix more imports.
720
        msg = http_client.HTTPMessage(status_and_headers)
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
721
        return msg
722
1786.1.26 by John Arbash Meinel
Update and test handle_response.
723
    def get_response(self, a_response):
724
        """Process a supplied response, and return the result."""
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
725
        code, raw_headers, body = a_response
726
        msg = self._build_HTTPMessage(raw_headers)
727
        return response.handle_response('http://foo', code, msg,
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
728
                                        BytesIO(a_response[2]))
1786.1.26 by John Arbash Meinel
Update and test handle_response.
729
730
    def test_full_text(self):
731
        out = self.get_response(_full_text_response)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
732
        # It is a BytesIO from the original data
1786.1.26 by John Arbash Meinel
Update and test handle_response.
733
        self.assertEqual(_full_text_response[2], out.read())
734
735
    def test_single_range(self):
736
        out = self.get_response(_single_range_response)
737
738
        out.seek(100)
739
        self.assertEqual(_single_range_response[2], out.read(100))
740
2070.1.1 by John Arbash Meinel
Fix bug #62473 by not requiring content-type in range responses
741
    def test_single_range_no_content(self):
742
        out = self.get_response(_single_range_no_content_type)
743
744
        out.seek(100)
745
        self.assertEqual(_single_range_no_content_type[2], out.read(100))
746
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
747
    def test_single_range_truncated(self):
748
        out = self.get_response(_single_range_response_truncated)
749
        # Content-Range declares 100 but only 51 present
750
        self.assertRaises(errors.ShortReadvError, out.seek, out.tell() + 51)
751
1786.1.26 by John Arbash Meinel
Update and test handle_response.
752
    def test_multi_range(self):
753
        out = self.get_response(_multipart_range_response)
754
755
        # Just make sure we can read the right contents
756
        out.seek(0)
757
        out.read(255)
758
759
        out.seek(1000)
760
        out.read(1050)
761
1979.1.1 by John Arbash Meinel
Fix bug #57723, parse boundary="" correctly, since Squid uses it
762
    def test_multi_squid_range(self):
763
        out = self.get_response(_multipart_squid_range_response)
764
765
        # Just make sure we can read the right contents
766
        out.seek(0)
767
        out.read(100)
768
769
        out.seek(300)
770
        out.read(200)
771
1786.1.26 by John Arbash Meinel
Update and test handle_response.
772
    def test_invalid_response(self):
773
        self.assertRaises(errors.InvalidHttpResponse,
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
774
                          self.get_response, _invalid_response)
1786.1.26 by John Arbash Meinel
Update and test handle_response.
775
776
    def test_full_text_no_content_type(self):
777
        # We should not require Content-Type for a full response
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
778
        code, raw_headers, body = _full_text_response_no_content_type
779
        msg = self._build_HTTPMessage(raw_headers)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
780
        out = response.handle_response('http://foo', code, msg, BytesIO(body))
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
781
        self.assertEqual(body, out.read())
1786.1.26 by John Arbash Meinel
Update and test handle_response.
782
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
783
    def test_full_text_no_content_length(self):
784
        code, raw_headers, body = _full_text_response_no_content_length
785
        msg = self._build_HTTPMessage(raw_headers)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
786
        out = response.handle_response('http://foo', code, msg, BytesIO(body))
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
787
        self.assertEqual(body, out.read())
788
1786.1.26 by John Arbash Meinel
Update and test handle_response.
789
    def test_missing_content_range(self):
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
790
        code, raw_headers, body = _single_range_no_content_range
791
        msg = self._build_HTTPMessage(raw_headers)
1786.1.26 by John Arbash Meinel
Update and test handle_response.
792
        self.assertRaises(errors.InvalidHttpResponse,
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
793
                          response.handle_response,
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
794
                          'http://bogus', code, msg, BytesIO(body))
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
795
796
    def test_multipart_no_content_range(self):
797
        code, raw_headers, body = _multipart_no_content_range
798
        msg = self._build_HTTPMessage(raw_headers)
799
        self.assertRaises(errors.InvalidHttpResponse,
800
                          response.handle_response,
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
801
                          'http://bogus', code, msg, BytesIO(body))
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
802
803
    def test_multipart_no_boundary(self):
804
        out = self.get_response(_multipart_no_boundary)
805
        out.read()  # Read the whole range
806
        # Fail to find the boundary line
807
        self.assertRaises(errors.InvalidHttpResponse, out.seek, 1, 1)
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
808
809
810
class TestRangeFileSizeReadLimited(tests.TestCase):
811
    """Test RangeFile _max_read_size functionality which limits the size of
812
    read blocks to prevent MemoryError messages in socket.recv.
813
    """
814
815
    def setUp(self):
6552.1.3 by Vincent Ladeuil
Use super() instead of calling <base>.setup(self), as the original fix illustrated a too-easy-to-fall-into trap.
816
        super(TestRangeFileSizeReadLimited, self).setUp()
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
817
        # create a test datablock larger than _max_read_size.
818
        chunk_size = response.RangeFile._max_read_size
819
        test_pattern = '0123456789ABCDEF'
820
        self.test_data =  test_pattern * (3 * chunk_size / len(test_pattern))
821
        self.test_data_len = len(self.test_data)
822
823
    def test_max_read_size(self):
824
        """Read data in blocks and verify that the reads are not larger than
825
           the maximum read size.
826
        """
827
        # retrieve data in large blocks from response.RangeFile object
828
        mock_read_file = FakeReadFile(self.test_data)
829
        range_file = response.RangeFile('test_max_read_size', mock_read_file)
830
        response_data = range_file.read(self.test_data_len)
831
832
        # verify read size was equal to the maximum read size
833
        self.assertTrue(mock_read_file.get_max_read_size() > 0)
834
        self.assertEqual(mock_read_file.get_max_read_size(),
835
                         response.RangeFile._max_read_size)
836
        self.assertEqual(mock_read_file.get_read_count(), 3)
837
838
        # report error if the data wasn't equal (we only report the size due
839
        # to the length of the data)
840
        if response_data != self.test_data:
841
            message = "Data not equal.  Expected %d bytes, received %d."
842
            self.fail(message % (len(response_data), self.test_data_len))
843