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._bencode_py as py_module
25
scenarios = [('python', {'bencode': py_module})]
26
if CompiledBencodeFeature.available():
27
import bzrlib._bencode_c 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_c
48
def feature_name(self):
49
return 'bzrlib._bencode_c'
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, 'i' + ('1' * 1000) + ':')
107
self._check([], 'le')
108
self._check(['', '', ''], 'l0:0:0:e')
109
self._check([1, 2, 3], 'li1ei2ei3ee')
110
self._check(['asd', 'xy'], 'l3:asd2:xye')
111
self._check([['Alice', 'Bob'], [2, 3]], 'll5:Alice3:Bobeli2ei3eee')
113
def test_list_deepnested(self):
114
self._run_check_error(RuntimeError, ("l" * 10000) + ("e" * 10000))
116
def test_malformed_list(self):
117
self._run_check_error(ValueError, 'l')
118
self._run_check_error(ValueError, 'l01:ae')
119
self._run_check_error(ValueError, 'l0:')
120
self._run_check_error(ValueError, 'li1e')
121
self._run_check_error(ValueError, 'l-3:e')
124
self._check({}, 'de')
125
self._check({'':3}, 'd0:i3ee')
126
self._check({'age': 25, 'eyes': 'blue'}, 'd3:agei25e4:eyes4:bluee')
127
self._check({'spam.mp3': {'author': 'Alice', 'length': 100000}},
128
'd8:spam.mp3d6:author5:Alice6:lengthi100000eee')
130
def test_dict_deepnested(self):
131
self._run_check_error(RuntimeError, ("d0:" * 10000) + 'i1e' + ("e" * 10000))
133
def test_malformed_dict(self):
134
self._run_check_error(ValueError, 'd')
135
self._run_check_error(ValueError, 'defoobar')
136
self._run_check_error(ValueError, 'd3:fooe')
137
self._run_check_error(ValueError, 'di1e0:e')
138
self._run_check_error(ValueError, 'd1:b0:1:a0:e')
139
self._run_check_error(ValueError, 'd1:a0:1:a0:e')
140
self._run_check_error(ValueError, 'd0:0:')
141
self._run_check_error(ValueError, 'd0:')
142
self._run_check_error(ValueError, 'd432432432432432432:e')
144
def test_empty_string(self):
145
self.assertRaises(ValueError, self.bencode.bdecode, '')
148
self._run_check_error(ValueError, 'i6easd')
149
self._run_check_error(ValueError, '2:abfdjslhfld')
150
self._run_check_error(ValueError, '0:0:')
151
self._run_check_error(ValueError, 'leanfdldjfh')
153
def test_unknown_object(self):
154
self.assertRaises(ValueError, self.bencode.bdecode, 'relwjhrlewjh')
156
def test_unsupported_type(self):
157
self._run_check_error(TypeError, float(1.5))
158
self._run_check_error(TypeError, None)
159
self._run_check_error(TypeError, lambda x: x)
160
self._run_check_error(TypeError, object)
161
self._run_check_error(TypeError, u"ie")
163
def test_decoder_type_error(self):
164
self.assertRaises(TypeError, self.bencode.bdecode, 1)
167
class TestBencodeEncode(tests.TestCase):
171
def _check(self, expected, source):
172
self.assertEquals(expected, self.bencode.bencode(source))
175
self._check('i4e', 4)
176
self._check('i0e', 0)
177
self._check('i-10e', -10)
180
self._check('i12345678901234567890e', 12345678901234567890L)
181
self._check('i-12345678901234567890e', -12345678901234567890L)
183
def test_string(self):
184
self._check('0:', '')
185
self._check('3:abc', 'abc')
186
self._check('10:1234567890', '1234567890')
189
self._check('le', [])
190
self._check('li1ei2ei3ee', [1, 2, 3])
191
self._check('ll5:Alice3:Bobeli2ei3eee', [['Alice', 'Bob'], [2, 3]])
193
def test_list_as_tuple(self):
194
self._check('le', ())
195
self._check('li1ei2ei3ee', (1, 2, 3))
196
self._check('ll5:Alice3:Bobeli2ei3eee', (('Alice', 'Bob'), (2, 3)))
198
def test_list_deep_nested(self):
201
for i in range(10000):
204
self.assertRaises(RuntimeError, self.bencode.bencode,
208
self._check('de', {})
209
self._check('d3:agei25e4:eyes4:bluee', {'age': 25, 'eyes': 'blue'})
210
self._check('d8:spam.mp3d6:author5:Alice6:lengthi100000eee',
211
{'spam.mp3': {'author': 'Alice',
214
def test_dict_deep_nested(self):
216
for i in range(10000):
219
self.assertRaises(RuntimeError, self.bencode.bencode,
222
def test_bencached(self):
223
self._check('i3e', self.bencode.Bencached(self.bencode.bencode(3)))
225
def test_invalid_dict(self):
226
self.assertRaises(TypeError, self.bencode.bencode, {1:"foo"})
229
self._check('i1e', True)
230
self._check('i0e', False)