1
# Copyright (C) 2007, 2009, 2010 Canonical Ltd
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.
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.
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
17
"""Tests for bencode structured encoding"""
21
from bzrlib import tests
23
def load_tests(standard_tests, module, loader):
24
suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
25
'bzrlib.util._bencode_py', 'bzrlib._bencode_pyx')
29
class TestBencodeDecode(tests.TestCase):
33
def _check(self, expected, source):
34
self.assertEquals(expected, self.module.bdecode(source))
36
def _run_check_error(self, exc, bad):
37
"""Check that bdecoding a string raises a particular exception."""
38
self.assertRaises(exc, self.module.bdecode, bad)
43
self._check(123456789, 'i123456789e')
44
self._check(-10, 'i-10e')
45
self._check(int('1' * 1000), 'i' + ('1' * 1000) + 'e')
48
self._check(12345678901234567890L, 'i12345678901234567890e')
49
self._check(-12345678901234567890L, 'i-12345678901234567890e')
51
def test_malformed_int(self):
52
self._run_check_error(ValueError, 'ie')
53
self._run_check_error(ValueError, 'i-e')
54
self._run_check_error(ValueError, 'i-010e')
55
self._run_check_error(ValueError, 'i-0e')
56
self._run_check_error(ValueError, 'i00e')
57
self._run_check_error(ValueError, 'i01e')
58
self._run_check_error(ValueError, 'i-03e')
59
self._run_check_error(ValueError, 'i')
60
self._run_check_error(ValueError, 'i123')
61
self._run_check_error(ValueError, 'i341foo382e')
63
def test_string(self):
65
self._check('abc', '3:abc')
66
self._check('1234567890', '10:1234567890')
68
def test_large_string(self):
69
self.assertRaises(ValueError, self.module.bdecode, "2147483639:foo")
71
def test_malformed_string(self):
72
self._run_check_error(ValueError, '10:x')
73
self._run_check_error(ValueError, '10:')
74
self._run_check_error(ValueError, '10')
75
self._run_check_error(ValueError, '01:x')
76
self._run_check_error(ValueError, '00:')
77
self._run_check_error(ValueError, '35208734823ljdahflajhdf')
78
self._run_check_error(ValueError, '432432432432432:foo')
79
self._run_check_error(ValueError, ' 1:x') # leading whitespace
80
self._run_check_error(ValueError, '-1:x') # negative
81
self._run_check_error(ValueError, '1 x') # space vs colon
82
self._run_check_error(ValueError, '1x') # missing colon
83
self._run_check_error(ValueError, ('1' * 1000) + ':')
87
self._check(['', '', ''], 'l0:0:0:e')
88
self._check([1, 2, 3], 'li1ei2ei3ee')
89
self._check(['asd', 'xy'], 'l3:asd2:xye')
90
self._check([['Alice', 'Bob'], [2, 3]], 'll5:Alice3:Bobeli2ei3eee')
92
def test_list_deepnested(self):
93
self._run_check_error(RuntimeError, ("l" * 10000) + ("e" * 10000))
95
def test_malformed_list(self):
96
self._run_check_error(ValueError, 'l')
97
self._run_check_error(ValueError, 'l01:ae')
98
self._run_check_error(ValueError, 'l0:')
99
self._run_check_error(ValueError, 'li1e')
100
self._run_check_error(ValueError, 'l-3:e')
103
self._check({}, 'de')
104
self._check({'':3}, 'd0:i3ee')
105
self._check({'age': 25, 'eyes': 'blue'}, 'd3:agei25e4:eyes4:bluee')
106
self._check({'spam.mp3': {'author': 'Alice', 'length': 100000}},
107
'd8:spam.mp3d6:author5:Alice6:lengthi100000eee')
109
def test_dict_deepnested(self):
110
# The recursion here provokes CPython into emitting a warning on
111
# stderr, "maximum recursion depth exceeded in __subclasscheck__", due
112
# to running out of stack space while evaluating "except (...):" in
113
# _bencode_py. This is harmless, so we temporarily override stderr to
114
# avoid distracting noise in the test output.
115
self.overrideAttr(sys, 'stderr', self._log_file)
116
self._run_check_error(
117
RuntimeError, ("d0:" * 10000) + 'i1e' + ("e" * 10000))
119
def test_malformed_dict(self):
120
self._run_check_error(ValueError, 'd')
121
self._run_check_error(ValueError, 'defoobar')
122
self._run_check_error(ValueError, 'd3:fooe')
123
self._run_check_error(ValueError, 'di1e0:e')
124
self._run_check_error(ValueError, 'd1:b0:1:a0:e')
125
self._run_check_error(ValueError, 'd1:a0:1:a0:e')
126
self._run_check_error(ValueError, 'd0:0:')
127
self._run_check_error(ValueError, 'd0:')
128
self._run_check_error(ValueError, 'd432432432432432432:e')
130
def test_empty_string(self):
131
self.assertRaises(ValueError, self.module.bdecode, '')
134
self._run_check_error(ValueError, 'i6easd')
135
self._run_check_error(ValueError, '2:abfdjslhfld')
136
self._run_check_error(ValueError, '0:0:')
137
self._run_check_error(ValueError, 'leanfdldjfh')
139
def test_unknown_object(self):
140
self.assertRaises(ValueError, self.module.bdecode, 'relwjhrlewjh')
142
def test_unsupported_type(self):
143
self._run_check_error(TypeError, float(1.5))
144
self._run_check_error(TypeError, None)
145
self._run_check_error(TypeError, lambda x: x)
146
self._run_check_error(TypeError, object)
147
self._run_check_error(TypeError, u"ie")
149
def test_decoder_type_error(self):
150
self.assertRaises(TypeError, self.module.bdecode, 1)
153
class TestBencodeEncode(tests.TestCase):
157
def _check(self, expected, source):
158
self.assertEquals(expected, self.module.bencode(source))
161
self._check('i4e', 4)
162
self._check('i0e', 0)
163
self._check('i-10e', -10)
166
self._check('i12345678901234567890e', 12345678901234567890L)
167
self._check('i-12345678901234567890e', -12345678901234567890L)
169
def test_string(self):
170
self._check('0:', '')
171
self._check('3:abc', 'abc')
172
self._check('10:1234567890', '1234567890')
175
self._check('le', [])
176
self._check('li1ei2ei3ee', [1, 2, 3])
177
self._check('ll5:Alice3:Bobeli2ei3eee', [['Alice', 'Bob'], [2, 3]])
179
def test_list_as_tuple(self):
180
self._check('le', ())
181
self._check('li1ei2ei3ee', (1, 2, 3))
182
self._check('ll5:Alice3:Bobeli2ei3eee', (('Alice', 'Bob'), (2, 3)))
184
def test_list_deep_nested(self):
187
for i in range(10000):
190
self.assertRaises(RuntimeError, self.module.bencode,
194
self._check('de', {})
195
self._check('d3:agei25e4:eyes4:bluee', {'age': 25, 'eyes': 'blue'})
196
self._check('d8:spam.mp3d6:author5:Alice6:lengthi100000eee',
197
{'spam.mp3': {'author': 'Alice',
200
def test_dict_deep_nested(self):
202
for i in range(10000):
205
self.assertRaises(RuntimeError, self.module.bencode,
208
def test_bencached(self):
209
self._check('i3e', self.module.Bencached(self.module.bencode(3)))
211
def test_invalid_dict(self):
212
self.assertRaises(TypeError, self.module.bencode, {1:"foo"})
215
self._check('i1e', True)
216
self._check('i0e', False)