1
# Copyright (C) 2007 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"""
19
from bzrlib import tests
21
def load_tests(standard_tests, module, loader):
22
# parameterize all tests in this module
23
suite = loader.suiteClass()
24
import bzrlib.util._bencode_py as py_module
25
scenarios = [('python', {'bencode': py_module})]
26
if CompiledBencodeFeature.available():
27
import bzrlib._bencode_pyx as c_module
28
scenarios.append(('C', {'bencode': c_module}))
30
# the compiled module isn't available, so we add a failing test
31
class FailWithoutFeature(tests.TestCase):
33
self.requireFeature(CompiledBencodeFeature)
34
suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
35
tests.multiply_tests(standard_tests, scenarios, suite)
39
class _CompiledBencodeFeature(tests.Feature):
43
import bzrlib._bencode_pyx
48
def feature_name(self):
49
return 'bzrlib._bencode_pyx'
51
CompiledBencodeFeature = _CompiledBencodeFeature()
54
class TestBencodeDecode(tests.TestCase):
58
def _check(self, expected, source):
59
self.assertEquals(expected, self.bencode.bdecode(source))
61
def _run_check_error(self, exc, bad):
62
"""Check that bdecoding a string raises a particular exception."""
63
self.assertRaises(exc, self.bencode.bdecode, bad)
68
self._check(123456789, 'i123456789e')
69
self._check(-10, 'i-10e')
70
self._check(int('1' * 1000), 'i' + ('1' * 1000) + 'e')
73
self._check(12345678901234567890L, 'i12345678901234567890e')
74
self._check(-12345678901234567890L, 'i-12345678901234567890e')
76
def test_malformed_int(self):
77
self._run_check_error(ValueError, 'ie')
78
self._run_check_error(ValueError, 'i-e')
79
self._run_check_error(ValueError, 'i-010e')
80
self._run_check_error(ValueError, 'i-0e')
81
self._run_check_error(ValueError, 'i00e')
82
self._run_check_error(ValueError, 'i01e')
83
self._run_check_error(ValueError, 'i-03e')
84
self._run_check_error(ValueError, 'i')
85
self._run_check_error(ValueError, 'i123')
86
self._run_check_error(ValueError, 'i341foo382e')
88
def test_string(self):
90
self._check('abc', '3:abc')
91
self._check('1234567890', '10:1234567890')
93
def test_large_string(self):
94
self.assertRaises(ValueError, self.bencode.bdecode, "2147483639:foo")
96
def test_malformed_string(self):
97
self._run_check_error(ValueError, '10:x')
98
self._run_check_error(ValueError, '10:')
99
self._run_check_error(ValueError, '10')
100
self._run_check_error(ValueError, '01:x')
101
self._run_check_error(ValueError, '00:')
102
self._run_check_error(ValueError, '35208734823ljdahflajhdf')
103
self._run_check_error(ValueError, '432432432432432:foo')
104
self._run_check_error(ValueError, ' 1:x') # leading whitespace
105
self._run_check_error(ValueError, '-1:x') # negative
106
self._run_check_error(ValueError, '1 x') # space vs colon
107
self._run_check_error(ValueError, '1x') # missing colon
108
self._run_check_error(ValueError, ('1' * 1000) + ':')
111
self._check([], 'le')
112
self._check(['', '', ''], 'l0:0:0:e')
113
self._check([1, 2, 3], 'li1ei2ei3ee')
114
self._check(['asd', 'xy'], 'l3:asd2:xye')
115
self._check([['Alice', 'Bob'], [2, 3]], 'll5:Alice3:Bobeli2ei3eee')
117
def test_list_deepnested(self):
118
self._run_check_error(RuntimeError, ("l" * 10000) + ("e" * 10000))
120
def test_malformed_list(self):
121
self._run_check_error(ValueError, 'l')
122
self._run_check_error(ValueError, 'l01:ae')
123
self._run_check_error(ValueError, 'l0:')
124
self._run_check_error(ValueError, 'li1e')
125
self._run_check_error(ValueError, 'l-3:e')
128
self._check({}, 'de')
129
self._check({'':3}, 'd0:i3ee')
130
self._check({'age': 25, 'eyes': 'blue'}, 'd3:agei25e4:eyes4:bluee')
131
self._check({'spam.mp3': {'author': 'Alice', 'length': 100000}},
132
'd8:spam.mp3d6:author5:Alice6:lengthi100000eee')
134
def test_dict_deepnested(self):
135
self._run_check_error(RuntimeError, ("d0:" * 10000) + 'i1e' + ("e" * 10000))
137
def test_malformed_dict(self):
138
self._run_check_error(ValueError, 'd')
139
self._run_check_error(ValueError, 'defoobar')
140
self._run_check_error(ValueError, 'd3:fooe')
141
self._run_check_error(ValueError, 'di1e0:e')
142
self._run_check_error(ValueError, 'd1:b0:1:a0:e')
143
self._run_check_error(ValueError, 'd1:a0:1:a0:e')
144
self._run_check_error(ValueError, 'd0:0:')
145
self._run_check_error(ValueError, 'd0:')
146
self._run_check_error(ValueError, 'd432432432432432432:e')
148
def test_empty_string(self):
149
self.assertRaises(ValueError, self.bencode.bdecode, '')
152
self._run_check_error(ValueError, 'i6easd')
153
self._run_check_error(ValueError, '2:abfdjslhfld')
154
self._run_check_error(ValueError, '0:0:')
155
self._run_check_error(ValueError, 'leanfdldjfh')
157
def test_unknown_object(self):
158
self.assertRaises(ValueError, self.bencode.bdecode, 'relwjhrlewjh')
160
def test_unsupported_type(self):
161
self._run_check_error(TypeError, float(1.5))
162
self._run_check_error(TypeError, None)
163
self._run_check_error(TypeError, lambda x: x)
164
self._run_check_error(TypeError, object)
165
self._run_check_error(TypeError, u"ie")
167
def test_decoder_type_error(self):
168
self.assertRaises(TypeError, self.bencode.bdecode, 1)
171
class TestBencodeEncode(tests.TestCase):
175
def _check(self, expected, source):
176
self.assertEquals(expected, self.bencode.bencode(source))
179
self._check('i4e', 4)
180
self._check('i0e', 0)
181
self._check('i-10e', -10)
184
self._check('i12345678901234567890e', 12345678901234567890L)
185
self._check('i-12345678901234567890e', -12345678901234567890L)
187
def test_string(self):
188
self._check('0:', '')
189
self._check('3:abc', 'abc')
190
self._check('10:1234567890', '1234567890')
193
self._check('le', [])
194
self._check('li1ei2ei3ee', [1, 2, 3])
195
self._check('ll5:Alice3:Bobeli2ei3eee', [['Alice', 'Bob'], [2, 3]])
197
def test_list_as_tuple(self):
198
self._check('le', ())
199
self._check('li1ei2ei3ee', (1, 2, 3))
200
self._check('ll5:Alice3:Bobeli2ei3eee', (('Alice', 'Bob'), (2, 3)))
202
def test_list_deep_nested(self):
205
for i in range(10000):
208
self.assertRaises(RuntimeError, self.bencode.bencode,
212
self._check('de', {})
213
self._check('d3:agei25e4:eyes4:bluee', {'age': 25, 'eyes': 'blue'})
214
self._check('d8:spam.mp3d6:author5:Alice6:lengthi100000eee',
215
{'spam.mp3': {'author': 'Alice',
218
def test_dict_deep_nested(self):
220
for i in range(10000):
223
self.assertRaises(RuntimeError, self.bencode.bencode,
226
def test_bencached(self):
227
self._check('i3e', self.bencode.Bencached(self.bencode.bencode(3)))
229
def test_invalid_dict(self):
230
self.assertRaises(TypeError, self.bencode.bencode, {1:"foo"})
233
self._check('i1e', True)
234
self._check('i0e', False)