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