/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
1
# Copyright (C) 2007 Canonical Ltd
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
2694.5.14 by Jelmer Vernooij
Fix copyright headers, add _bencode_py.py to the list of files that do not have to have canonical copyright.
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
16
17
"""Tests for bencode structured encoding"""
18
2694.5.8 by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations.
19
from bzrlib import tests
20
21
def load_tests(standard_tests, module, loader):
22
    # parameterize all tests in this module
23
    suite = loader.suiteClass()
4398.5.4 by John Arbash Meinel
A bit of code restructuring.
24
    import bzrlib.util._bencode_py as py_module
2694.5.8 by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations.
25
    scenarios = [('python', {'bencode': py_module})]
4913.2.20 by John Arbash Meinel
Change all of the compiled_foo to compiled_foo_feature
26
    if compiled_bencode_feature.available():
27
        scenarios.append(('C', {'bencode': compiled_bencode_feature.module}))
2694.5.8 by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations.
28
    else:
29
        # the compiled module isn't available, so we add a failing test
30
        class FailWithoutFeature(tests.TestCase):
31
            def test_fail(self):
4913.2.20 by John Arbash Meinel
Change all of the compiled_foo to compiled_foo_feature
32
                self.requireFeature(compiled_bencode_feature)
2694.5.8 by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations.
33
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
34
    tests.multiply_tests(standard_tests, scenarios, suite)
35
    return suite
36
37
4913.2.20 by John Arbash Meinel
Change all of the compiled_foo to compiled_foo_feature
38
compiled_bencode_feature = tests.ModuleAvailableFeature('bzrlib._bencode_pyx')
2694.5.8 by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations.
39
40
41
class TestBencodeDecode(tests.TestCase):
42
43
    bencode = None
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
44
45
    def _check(self, expected, source):
46
        self.assertEquals(expected, self.bencode.bdecode(source))
47
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
48
    def _run_check_error(self, exc, bad):
49
        """Check that bdecoding a string raises a particular exception."""
50
        self.assertRaises(exc, self.bencode.bdecode, bad)
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
51
52
    def test_int(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
53
        self._check(0, 'i0e')
54
        self._check(4, 'i4e')
55
        self._check(123456789, 'i123456789e')
56
        self._check(-10, 'i-10e')
2694.5.19 by Jelmer Vernooij
Support longs in the pyrex extensions, avoid memcpy.
57
        self._check(int('1' * 1000), 'i' + ('1' * 1000) + 'e')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
58
59
    def test_long(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
60
        self._check(12345678901234567890L, 'i12345678901234567890e')
61
        self._check(-12345678901234567890L, 'i-12345678901234567890e')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
62
63
    def test_malformed_int(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
64
        self._run_check_error(ValueError, 'ie')
65
        self._run_check_error(ValueError, 'i-e')
66
        self._run_check_error(ValueError, 'i-010e')
67
        self._run_check_error(ValueError, 'i-0e')
68
        self._run_check_error(ValueError, 'i00e')
69
        self._run_check_error(ValueError, 'i01e')
70
        self._run_check_error(ValueError, 'i-03e')
71
        self._run_check_error(ValueError, 'i')
72
        self._run_check_error(ValueError, 'i123')
73
        self._run_check_error(ValueError, 'i341foo382e')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
74
75
    def test_string(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
76
        self._check('', '0:')
77
        self._check('abc', '3:abc')
78
        self._check('1234567890', '10:1234567890')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
79
2694.5.10 by Jelmer Vernooij
Add some more tests.
80
    def test_large_string(self):
81
        self.assertRaises(ValueError, self.bencode.bdecode, "2147483639:foo")
82
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
83
    def test_malformed_string(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
84
        self._run_check_error(ValueError, '10:x')
85
        self._run_check_error(ValueError, '10:')
86
        self._run_check_error(ValueError, '10')
87
        self._run_check_error(ValueError, '01:x')
88
        self._run_check_error(ValueError, '00:')
89
        self._run_check_error(ValueError, '35208734823ljdahflajhdf')
90
        self._run_check_error(ValueError, '432432432432432:foo')
4398.5.14 by John Arbash Meinel
Some small tweaks to decoding strings (avoid passing over the length 2x)
91
        self._run_check_error(ValueError, ' 1:x') # leading whitespace
92
        self._run_check_error(ValueError, '-1:x') # negative
93
        self._run_check_error(ValueError, '1 x') # space vs colon
94
        self._run_check_error(ValueError, '1x') # missing colon
95
        self._run_check_error(ValueError, ('1' * 1000) + ':')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
96
97
    def test_list(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
98
        self._check([], 'le')
99
        self._check(['', '', ''], 'l0:0:0:e')
100
        self._check([1, 2, 3], 'li1ei2ei3ee')
101
        self._check(['asd', 'xy'], 'l3:asd2:xye')
102
        self._check([['Alice', 'Bob'], [2, 3]], 'll5:Alice3:Bobeli2ei3eee')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
103
2694.5.20 by Jelmer Vernooij
add handling of deep nesting.
104
    def test_list_deepnested(self):
105
        self._run_check_error(RuntimeError, ("l" * 10000) + ("e" * 10000))
106
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
107
    def test_malformed_list(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
108
        self._run_check_error(ValueError, 'l')
109
        self._run_check_error(ValueError, 'l01:ae')
110
        self._run_check_error(ValueError, 'l0:')
111
        self._run_check_error(ValueError, 'li1e')
112
        self._run_check_error(ValueError, 'l-3:e')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
113
114
    def test_dict(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
115
        self._check({}, 'de')
116
        self._check({'':3}, 'd0:i3ee')
117
        self._check({'age': 25, 'eyes': 'blue'}, 'd3:agei25e4:eyes4:bluee')
118
        self._check({'spam.mp3': {'author': 'Alice', 'length': 100000}},
119
                            'd8:spam.mp3d6:author5:Alice6:lengthi100000eee')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
120
2694.5.20 by Jelmer Vernooij
add handling of deep nesting.
121
    def test_dict_deepnested(self):
122
        self._run_check_error(RuntimeError, ("d0:" * 10000) + 'i1e' + ("e" * 10000))
123
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
124
    def test_malformed_dict(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
125
        self._run_check_error(ValueError, 'd')
126
        self._run_check_error(ValueError, 'defoobar')
127
        self._run_check_error(ValueError, 'd3:fooe')
128
        self._run_check_error(ValueError, 'di1e0:e')
129
        self._run_check_error(ValueError, 'd1:b0:1:a0:e')
130
        self._run_check_error(ValueError, 'd1:a0:1:a0:e')
131
        self._run_check_error(ValueError, 'd0:0:')
132
        self._run_check_error(ValueError, 'd0:')
133
        self._run_check_error(ValueError, 'd432432432432432432:e')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
134
135
    def test_empty_string(self):
2694.5.9 by Jelmer Vernooij
Fix tests.
136
        self.assertRaises(ValueError, self.bencode.bdecode, '')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
137
138
    def test_junk(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
139
        self._run_check_error(ValueError, 'i6easd')
140
        self._run_check_error(ValueError, '2:abfdjslhfld')
141
        self._run_check_error(ValueError, '0:0:')
142
        self._run_check_error(ValueError, 'leanfdldjfh')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
143
144
    def test_unknown_object(self):
2694.5.9 by Jelmer Vernooij
Fix tests.
145
        self.assertRaises(ValueError, self.bencode.bdecode, 'relwjhrlewjh')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
146
2694.5.8 by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations.
147
    def test_unsupported_type(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
148
        self._run_check_error(TypeError, float(1.5))
149
        self._run_check_error(TypeError, None)
150
        self._run_check_error(TypeError, lambda x: x)
151
        self._run_check_error(TypeError, object)
152
        self._run_check_error(TypeError, u"ie")
2694.5.9 by Jelmer Vernooij
Fix tests.
153
154
    def test_decoder_type_error(self):
155
        self.assertRaises(TypeError, self.bencode.bdecode, 1)
2694.5.8 by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations.
156
157
158
class TestBencodeEncode(tests.TestCase):
159
160
    bencode = None
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
161
162
    def _check(self, expected, source):
163
        self.assertEquals(expected, self.bencode.bencode(source))
164
165
    def test_int(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
166
        self._check('i4e', 4)
167
        self._check('i0e', 0)
168
        self._check('i-10e', -10)
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
169
170
    def test_long(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
171
        self._check('i12345678901234567890e', 12345678901234567890L)
172
        self._check('i-12345678901234567890e', -12345678901234567890L)
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
173
174
    def test_string(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
175
        self._check('0:', '')
176
        self._check('3:abc', 'abc')
177
        self._check('10:1234567890', '1234567890')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
178
179
    def test_list(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
180
        self._check('le', [])
181
        self._check('li1ei2ei3ee', [1, 2, 3])
182
        self._check('ll5:Alice3:Bobeli2ei3eee', [['Alice', 'Bob'], [2, 3]])
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
183
2694.5.5 by Jelmer Vernooij
Support bdecode_as_tuple.
184
    def test_list_as_tuple(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
185
        self._check('le', ())
186
        self._check('li1ei2ei3ee', (1, 2, 3))
187
        self._check('ll5:Alice3:Bobeli2ei3eee', (('Alice', 'Bob'), (2, 3)))
2694.5.5 by Jelmer Vernooij
Support bdecode_as_tuple.
188
2694.5.20 by Jelmer Vernooij
add handling of deep nesting.
189
    def test_list_deep_nested(self):
190
        top = []
191
        l = top
192
        for i in range(10000):
193
            l.append([])
194
            l = l[0]
195
        self.assertRaises(RuntimeError, self.bencode.bencode, 
196
            top)
197
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
198
    def test_dict(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
199
        self._check('de', {})
200
        self._check('d3:agei25e4:eyes4:bluee', {'age': 25, 'eyes': 'blue'})
201
        self._check('d8:spam.mp3d6:author5:Alice6:lengthi100000eee',
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
202
                            {'spam.mp3': {'author': 'Alice',
203
                                          'length': 100000}})
204
2694.5.20 by Jelmer Vernooij
add handling of deep nesting.
205
    def test_dict_deep_nested(self):
206
        d = top = {}
207
        for i in range(10000):
208
            d[''] = {}
209
            d = d['']
210
        self.assertRaises(RuntimeError, self.bencode.bencode, 
211
            top)
212
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
213
    def test_bencached(self):
214
        self._check('i3e', self.bencode.Bencached(self.bencode.bencode(3)))
215
216
    def test_invalid_dict(self):
2694.5.10 by Jelmer Vernooij
Add some more tests.
217
        self.assertRaises(TypeError, self.bencode.bencode, {1:"foo"})
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
218
219
    def test_bool(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
220
        self._check('i1e', True)
221
        self._check('i0e', False)
2694.5.10 by Jelmer Vernooij
Add some more tests.
222