1
# Copyright (C) 2007, 2009 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
suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
23
'bzrlib.util._bencode_py', 'bzrlib._bencode_pyx')
27
class TestBencodeDecode(tests.TestCase):
31
def _check(self, expected, source):
32
self.assertEquals(expected, self.module.bdecode(source))
34
def _run_check_error(self, exc, bad):
35
"""Check that bdecoding a string raises a particular exception."""
36
self.assertRaises(exc, self.module.bdecode, bad)
41
self._check(123456789, 'i123456789e')
42
self._check(-10, 'i-10e')
43
self._check(int('1' * 1000), 'i' + ('1' * 1000) + 'e')
46
self._check(12345678901234567890L, 'i12345678901234567890e')
47
self._check(-12345678901234567890L, 'i-12345678901234567890e')
49
def test_malformed_int(self):
50
self._run_check_error(ValueError, 'ie')
51
self._run_check_error(ValueError, 'i-e')
52
self._run_check_error(ValueError, 'i-010e')
53
self._run_check_error(ValueError, 'i-0e')
54
self._run_check_error(ValueError, 'i00e')
55
self._run_check_error(ValueError, 'i01e')
56
self._run_check_error(ValueError, 'i-03e')
57
self._run_check_error(ValueError, 'i')
58
self._run_check_error(ValueError, 'i123')
59
self._run_check_error(ValueError, 'i341foo382e')
61
def test_string(self):
63
self._check('abc', '3:abc')
64
self._check('1234567890', '10:1234567890')
66
def test_large_string(self):
67
self.assertRaises(ValueError, self.module.bdecode, "2147483639:foo")
69
def test_malformed_string(self):
70
self._run_check_error(ValueError, '10:x')
71
self._run_check_error(ValueError, '10:')
72
self._run_check_error(ValueError, '10')
73
self._run_check_error(ValueError, '01:x')
74
self._run_check_error(ValueError, '00:')
75
self._run_check_error(ValueError, '35208734823ljdahflajhdf')
76
self._run_check_error(ValueError, '432432432432432:foo')
77
self._run_check_error(ValueError, ' 1:x') # leading whitespace
78
self._run_check_error(ValueError, '-1:x') # negative
79
self._run_check_error(ValueError, '1 x') # space vs colon
80
self._run_check_error(ValueError, '1x') # missing colon
81
self._run_check_error(ValueError, ('1' * 1000) + ':')
85
self._check(['', '', ''], 'l0:0:0:e')
86
self._check([1, 2, 3], 'li1ei2ei3ee')
87
self._check(['asd', 'xy'], 'l3:asd2:xye')
88
self._check([['Alice', 'Bob'], [2, 3]], 'll5:Alice3:Bobeli2ei3eee')
90
def test_list_deepnested(self):
91
self._run_check_error(RuntimeError, ("l" * 10000) + ("e" * 10000))
93
def test_malformed_list(self):
94
self._run_check_error(ValueError, 'l')
95
self._run_check_error(ValueError, 'l01:ae')
96
self._run_check_error(ValueError, 'l0:')
97
self._run_check_error(ValueError, 'li1e')
98
self._run_check_error(ValueError, 'l-3:e')
101
self._check({}, 'de')
102
self._check({'':3}, 'd0:i3ee')
103
self._check({'age': 25, 'eyes': 'blue'}, 'd3:agei25e4:eyes4:bluee')
104
self._check({'spam.mp3': {'author': 'Alice', 'length': 100000}},
105
'd8:spam.mp3d6:author5:Alice6:lengthi100000eee')
107
def test_dict_deepnested(self):
108
self._run_check_error(RuntimeError, ("d0:" * 10000) + 'i1e' + ("e" * 10000))
110
def test_malformed_dict(self):
111
self._run_check_error(ValueError, 'd')
112
self._run_check_error(ValueError, 'defoobar')
113
self._run_check_error(ValueError, 'd3:fooe')
114
self._run_check_error(ValueError, 'di1e0:e')
115
self._run_check_error(ValueError, 'd1:b0:1:a0:e')
116
self._run_check_error(ValueError, 'd1:a0:1:a0:e')
117
self._run_check_error(ValueError, 'd0:0:')
118
self._run_check_error(ValueError, 'd0:')
119
self._run_check_error(ValueError, 'd432432432432432432:e')
121
def test_empty_string(self):
122
self.assertRaises(ValueError, self.module.bdecode, '')
125
self._run_check_error(ValueError, 'i6easd')
126
self._run_check_error(ValueError, '2:abfdjslhfld')
127
self._run_check_error(ValueError, '0:0:')
128
self._run_check_error(ValueError, 'leanfdldjfh')
130
def test_unknown_object(self):
131
self.assertRaises(ValueError, self.module.bdecode, 'relwjhrlewjh')
133
def test_unsupported_type(self):
134
self._run_check_error(TypeError, float(1.5))
135
self._run_check_error(TypeError, None)
136
self._run_check_error(TypeError, lambda x: x)
137
self._run_check_error(TypeError, object)
138
self._run_check_error(TypeError, u"ie")
140
def test_decoder_type_error(self):
141
self.assertRaises(TypeError, self.module.bdecode, 1)
144
class TestBencodeEncode(tests.TestCase):
148
def _check(self, expected, source):
149
self.assertEquals(expected, self.module.bencode(source))
152
self._check('i4e', 4)
153
self._check('i0e', 0)
154
self._check('i-10e', -10)
157
self._check('i12345678901234567890e', 12345678901234567890L)
158
self._check('i-12345678901234567890e', -12345678901234567890L)
160
def test_string(self):
161
self._check('0:', '')
162
self._check('3:abc', 'abc')
163
self._check('10:1234567890', '1234567890')
166
self._check('le', [])
167
self._check('li1ei2ei3ee', [1, 2, 3])
168
self._check('ll5:Alice3:Bobeli2ei3eee', [['Alice', 'Bob'], [2, 3]])
170
def test_list_as_tuple(self):
171
self._check('le', ())
172
self._check('li1ei2ei3ee', (1, 2, 3))
173
self._check('ll5:Alice3:Bobeli2ei3eee', (('Alice', 'Bob'), (2, 3)))
175
def test_list_deep_nested(self):
178
for i in range(10000):
181
self.assertRaises(RuntimeError, self.module.bencode,
185
self._check('de', {})
186
self._check('d3:agei25e4:eyes4:bluee', {'age': 25, 'eyes': 'blue'})
187
self._check('d8:spam.mp3d6:author5:Alice6:lengthi100000eee',
188
{'spam.mp3': {'author': 'Alice',
191
def test_dict_deep_nested(self):
193
for i in range(10000):
196
self.assertRaises(RuntimeError, self.module.bencode,
199
def test_bencached(self):
200
self._check('i3e', self.module.Bencached(self.module.bencode(3)))
202
def test_invalid_dict(self):
203
self.assertRaises(TypeError, self.module.bencode, {1:"foo"})
206
self._check('i1e', True)
207
self._check('i0e', False)