/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2007, 2009, 2010, 2016 Canonical Ltd
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
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
5430.2.1 by Andrew Bennetts
Suppress 'maximum recursion depth exceeded in __subclasscheck__' warning triggered by test__bencode.
19
import sys
20
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
21
from .. import tests
2694.5.8 by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations.
22
6625.1.5 by Martin
Drop custom load_tests implementation and use unittest signature
23
def load_tests(loader, standard_tests, pattern):
4913.3.1 by John Arbash Meinel
Implement a permute_for_extension helper.
24
    suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
25
        'breezy.util._bencode_py', 'breezy._bencode_pyx')
2694.5.8 by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations.
26
    return suite
27
28
7059.1.3 by Martin
Fix recursion check in C bencode implementation
29
class RecursionLimit(object):
30
    """Context manager that lowers recursion limit for testing."""
31
32
    def __init__(self, limit=100):
33
        self._new_limit = limit
34
        self._old_limit = sys.getrecursionlimit()
35
36
    def __enter__(self):
37
        sys.setrecursionlimit(self._new_limit)
38
        return self
39
40
    def __exit__(self, *exc_info):
41
        sys.setrecursionlimit(self._old_limit)
42
43
2694.5.8 by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations.
44
class TestBencodeDecode(tests.TestCase):
45
4913.3.8 by John Arbash Meinel
Rename test_bencode to test__bencode, and use self.module
46
    module = None
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
47
48
    def _check(self, expected, source):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
49
        self.assertEqual(expected, self.module.bdecode(source))
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
50
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
51
    def _run_check_error(self, exc, bad):
52
        """Check that bdecoding a string raises a particular exception."""
4913.3.8 by John Arbash Meinel
Rename test_bencode to test__bencode, and use self.module
53
        self.assertRaises(exc, self.module.bdecode, bad)
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
54
55
    def test_int(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
56
        self._check(0, b'i0e')
57
        self._check(4, b'i4e')
58
        self._check(123456789, b'i123456789e')
59
        self._check(-10, b'i-10e')
60
        self._check(int('1' * 1000), b'i' + (b'1' * 1000) + b'e')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
61
62
    def test_long(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
63
        self._check(12345678901234567890, b'i12345678901234567890e')
64
        self._check(-12345678901234567890, b'i-12345678901234567890e')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
65
66
    def test_malformed_int(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
67
        self._run_check_error(ValueError, b'ie')
68
        self._run_check_error(ValueError, b'i-e')
69
        self._run_check_error(ValueError, b'i-010e')
70
        self._run_check_error(ValueError, b'i-0e')
71
        self._run_check_error(ValueError, b'i00e')
72
        self._run_check_error(ValueError, b'i01e')
73
        self._run_check_error(ValueError, b'i-03e')
74
        self._run_check_error(ValueError, b'i')
75
        self._run_check_error(ValueError, b'i123')
76
        self._run_check_error(ValueError, b'i341foo382e')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
77
78
    def test_string(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
79
        self._check(b'', b'0:')
80
        self._check(b'abc', b'3:abc')
81
        self._check(b'1234567890', b'10:1234567890')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
82
2694.5.10 by Jelmer Vernooij
Add some more tests.
83
    def test_large_string(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
84
        self.assertRaises(ValueError, self.module.bdecode, b"2147483639:foo")
2694.5.10 by Jelmer Vernooij
Add some more tests.
85
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
86
    def test_malformed_string(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
87
        self._run_check_error(ValueError, b'10:x')
88
        self._run_check_error(ValueError, b'10:')
89
        self._run_check_error(ValueError, b'10')
90
        self._run_check_error(ValueError, b'01:x')
91
        self._run_check_error(ValueError, b'00:')
92
        self._run_check_error(ValueError, b'35208734823ljdahflajhdf')
93
        self._run_check_error(ValueError, b'432432432432432:foo')
94
        self._run_check_error(ValueError, b' 1:x') # leading whitespace
95
        self._run_check_error(ValueError, b'-1:x') # negative
96
        self._run_check_error(ValueError, b'1 x') # space vs colon
97
        self._run_check_error(ValueError, b'1x') # missing colon
98
        self._run_check_error(ValueError, (b'1' * 1000) + b':')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
99
100
    def test_list(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
101
        self._check([], b'le')
102
        self._check([b'', b'', b''], b'l0:0:0:e')
103
        self._check([1, 2, 3], b'li1ei2ei3ee')
104
        self._check([b'asd', b'xy'], b'l3:asd2:xye')
105
        self._check([[b'Alice', b'Bob'], [2, 3]], b'll5:Alice3:Bobeli2ei3eee')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
106
2694.5.20 by Jelmer Vernooij
add handling of deep nesting.
107
    def test_list_deepnested(self):
7059.1.3 by Martin
Fix recursion check in C bencode implementation
108
        with RecursionLimit():
109
            self._run_check_error(RuntimeError, (b"l" * 100) + (b"e" * 100))
2694.5.20 by Jelmer Vernooij
add handling of deep nesting.
110
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
111
    def test_malformed_list(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
112
        self._run_check_error(ValueError, b'l')
113
        self._run_check_error(ValueError, b'l01:ae')
114
        self._run_check_error(ValueError, b'l0:')
115
        self._run_check_error(ValueError, b'li1e')
116
        self._run_check_error(ValueError, b'l-3:e')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
117
118
    def test_dict(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
119
        self._check({}, b'de')
120
        self._check({b'':3}, b'd0:i3ee')
121
        self._check({b'age': 25, b'eyes': b'blue'}, b'd3:agei25e4:eyes4:bluee')
122
        self._check({b'spam.mp3': {b'author': b'Alice', b'length': 100000}},
123
            b'd8:spam.mp3d6:author5:Alice6:lengthi100000eee')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
124
2694.5.20 by Jelmer Vernooij
add handling of deep nesting.
125
    def test_dict_deepnested(self):
7059.1.3 by Martin
Fix recursion check in C bencode implementation
126
        with RecursionLimit():
127
            self._run_check_error(
128
                RuntimeError, (b"d0:" * 1000) + b'i1e' + (b"e" * 1000))
2694.5.20 by Jelmer Vernooij
add handling of deep nesting.
129
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
130
    def test_malformed_dict(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
131
        self._run_check_error(ValueError, b'd')
132
        self._run_check_error(ValueError, b'defoobar')
133
        self._run_check_error(ValueError, b'd3:fooe')
134
        self._run_check_error(ValueError, b'di1e0:e')
135
        self._run_check_error(ValueError, b'd1:b0:1:a0:e')
136
        self._run_check_error(ValueError, b'd1:a0:1:a0:e')
137
        self._run_check_error(ValueError, b'd0:0:')
138
        self._run_check_error(ValueError, b'd0:')
139
        self._run_check_error(ValueError, b'd432432432432432432:e')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
140
141
    def test_empty_string(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
142
        self.assertRaises(ValueError, self.module.bdecode, b'')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
143
144
    def test_junk(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
145
        self._run_check_error(ValueError, b'i6easd')
146
        self._run_check_error(ValueError, b'2:abfdjslhfld')
147
        self._run_check_error(ValueError, b'0:0:')
148
        self._run_check_error(ValueError, b'leanfdldjfh')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
149
150
    def test_unknown_object(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
151
        self.assertRaises(ValueError, self.module.bdecode, b'relwjhrlewjh')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
152
2694.5.8 by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations.
153
    def test_unsupported_type(self):
2694.5.16 by Jelmer Vernooij
Simplify the code a bit more.
154
        self._run_check_error(TypeError, float(1.5))
155
        self._run_check_error(TypeError, None)
156
        self._run_check_error(TypeError, lambda x: x)
157
        self._run_check_error(TypeError, object)
158
        self._run_check_error(TypeError, u"ie")
2694.5.9 by Jelmer Vernooij
Fix tests.
159
160
    def test_decoder_type_error(self):
4913.3.8 by John Arbash Meinel
Rename test_bencode to test__bencode, and use self.module
161
        self.assertRaises(TypeError, self.module.bdecode, 1)
2694.5.8 by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations.
162
163
164
class TestBencodeEncode(tests.TestCase):
165
4913.3.8 by John Arbash Meinel
Rename test_bencode to test__bencode, and use self.module
166
    module = None
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
167
168
    def _check(self, expected, source):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
169
        self.assertEqual(expected, self.module.bencode(source))
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
170
171
    def test_int(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
172
        self._check(b'i4e', 4)
173
        self._check(b'i0e', 0)
174
        self._check(b'i-10e', -10)
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
175
176
    def test_long(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
177
        self._check(b'i12345678901234567890e', 12345678901234567890)
178
        self._check(b'i-12345678901234567890e', -12345678901234567890)
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
179
180
    def test_string(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
181
        self._check(b'0:', b'')
182
        self._check(b'3:abc', b'abc')
183
        self._check(b'10:1234567890', b'1234567890')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
184
185
    def test_list(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
186
        self._check(b'le', [])
187
        self._check(b'li1ei2ei3ee', [1, 2, 3])
188
        self._check(b'll5:Alice3:Bobeli2ei3eee', [[b'Alice', b'Bob'], [2, 3]])
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
189
2694.5.5 by Jelmer Vernooij
Support bdecode_as_tuple.
190
    def test_list_as_tuple(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
191
        self._check(b'le', ())
192
        self._check(b'li1ei2ei3ee', (1, 2, 3))
193
        self._check(b'll5:Alice3:Bobeli2ei3eee', ((b'Alice', b'Bob'), (2, 3)))
2694.5.5 by Jelmer Vernooij
Support bdecode_as_tuple.
194
2694.5.20 by Jelmer Vernooij
add handling of deep nesting.
195
    def test_list_deep_nested(self):
196
        top = []
197
        l = top
7059.1.3 by Martin
Fix recursion check in C bencode implementation
198
        for i in range(1000):
2694.5.20 by Jelmer Vernooij
add handling of deep nesting.
199
            l.append([])
200
            l = l[0]
7059.1.3 by Martin
Fix recursion check in C bencode implementation
201
        with RecursionLimit():
202
            self.assertRaises(RuntimeError, self.module.bencode, top)
2694.5.20 by Jelmer Vernooij
add handling of deep nesting.
203
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
204
    def test_dict(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
205
        self._check(b'de', {})
206
        self._check(b'd3:agei25e4:eyes4:bluee', {b'age': 25, b'eyes': b'blue'})
207
        self._check(b'd8:spam.mp3d6:author5:Alice6:lengthi100000eee',
208
            {b'spam.mp3': {b'author': b'Alice', b'length': 100000}})
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
209
2694.5.20 by Jelmer Vernooij
add handling of deep nesting.
210
    def test_dict_deep_nested(self):
211
        d = top = {}
7059.1.3 by Martin
Fix recursion check in C bencode implementation
212
        for i in range(1000):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
213
            d[b''] = {}
214
            d = d[b'']
7059.1.3 by Martin
Fix recursion check in C bencode implementation
215
        with RecursionLimit():
216
            self.assertRaises(RuntimeError, self.module.bencode, top)
2694.5.20 by Jelmer Vernooij
add handling of deep nesting.
217
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
218
    def test_bencached(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
219
        self._check(b'i3e', self.module.Bencached(self.module.bencode(3)))
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
220
221
    def test_invalid_dict(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
222
        self.assertRaises(TypeError, self.module.bencode, {1: b"foo"})
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
223
224
    def test_bool(self):
6681.1.1 by Martin
Switch bencode to bytes types for Python 3
225
        self._check(b'i1e', True)
226
        self._check(b'i0e', False)
2694.5.10 by Jelmer Vernooij
Add some more tests.
227