4763.2.4
by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry. |
1 |
# Copyright (C) 2007, 2009, 2010 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 |
||
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): |
|
4913.3.1
by John Arbash Meinel
Implement a permute_for_extension helper. |
22 |
suite, _ = tests.permute_tests_for_extension(standard_tests, loader, |
23 |
'bzrlib.util._bencode_py', 'bzrlib._bencode_pyx') |
|
2694.5.8
by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations. |
24 |
return suite |
25 |
||
26 |
||
27 |
class TestBencodeDecode(tests.TestCase): |
|
28 |
||
4913.3.8
by John Arbash Meinel
Rename test_bencode to test__bencode, and use self.module |
29 |
module = None |
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
30 |
|
31 |
def _check(self, expected, source): |
|
4913.3.8
by John Arbash Meinel
Rename test_bencode to test__bencode, and use self.module |
32 |
self.assertEquals(expected, self.module.bdecode(source)) |
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
33 |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
34 |
def _run_check_error(self, exc, bad): |
35 |
"""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 |
36 |
self.assertRaises(exc, self.module.bdecode, bad) |
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
37 |
|
38 |
def test_int(self): |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
39 |
self._check(0, 'i0e') |
40 |
self._check(4, 'i4e') |
|
41 |
self._check(123456789, 'i123456789e') |
|
42 |
self._check(-10, 'i-10e') |
|
2694.5.19
by Jelmer Vernooij
Support longs in the pyrex extensions, avoid memcpy. |
43 |
self._check(int('1' * 1000), 'i' + ('1' * 1000) + 'e') |
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
44 |
|
45 |
def test_long(self): |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
46 |
self._check(12345678901234567890L, 'i12345678901234567890e') |
47 |
self._check(-12345678901234567890L, 'i-12345678901234567890e') |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
48 |
|
49 |
def test_malformed_int(self): |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
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') |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
60 |
|
61 |
def test_string(self): |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
62 |
self._check('', '0:') |
63 |
self._check('abc', '3:abc') |
|
64 |
self._check('1234567890', '10:1234567890') |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
65 |
|
2694.5.10
by Jelmer Vernooij
Add some more tests. |
66 |
def test_large_string(self): |
4913.3.8
by John Arbash Meinel
Rename test_bencode to test__bencode, and use self.module |
67 |
self.assertRaises(ValueError, self.module.bdecode, "2147483639:foo") |
2694.5.10
by Jelmer Vernooij
Add some more tests. |
68 |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
69 |
def test_malformed_string(self): |
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
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') |
|
4398.5.14
by John Arbash Meinel
Some small tweaks to decoding strings (avoid passing over the length 2x) |
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) + ':') |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
82 |
|
83 |
def test_list(self): |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
84 |
self._check([], 'le') |
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') |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
89 |
|
2694.5.20
by Jelmer Vernooij
add handling of deep nesting. |
90 |
def test_list_deepnested(self): |
91 |
self._run_check_error(RuntimeError, ("l" * 10000) + ("e" * 10000)) |
|
92 |
||
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
93 |
def test_malformed_list(self): |
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
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') |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
99 |
|
100 |
def test_dict(self): |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
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') |
|
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_dict_deepnested(self): |
108 |
self._run_check_error(RuntimeError, ("d0:" * 10000) + 'i1e' + ("e" * 10000)) |
|
109 |
||
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
110 |
def test_malformed_dict(self): |
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
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') |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
120 |
|
121 |
def test_empty_string(self): |
|
4913.3.8
by John Arbash Meinel
Rename test_bencode to test__bencode, and use self.module |
122 |
self.assertRaises(ValueError, self.module.bdecode, '') |
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
123 |
|
124 |
def test_junk(self): |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
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') |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
129 |
|
130 |
def test_unknown_object(self): |
|
4913.3.8
by John Arbash Meinel
Rename test_bencode to test__bencode, and use self.module |
131 |
self.assertRaises(ValueError, self.module.bdecode, 'relwjhrlewjh') |
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
132 |
|
2694.5.8
by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations. |
133 |
def test_unsupported_type(self): |
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
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") |
|
2694.5.9
by Jelmer Vernooij
Fix tests. |
139 |
|
140 |
def test_decoder_type_error(self): |
|
4913.3.8
by John Arbash Meinel
Rename test_bencode to test__bencode, and use self.module |
141 |
self.assertRaises(TypeError, self.module.bdecode, 1) |
2694.5.8
by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations. |
142 |
|
143 |
||
144 |
class TestBencodeEncode(tests.TestCase): |
|
145 |
||
4913.3.8
by John Arbash Meinel
Rename test_bencode to test__bencode, and use self.module |
146 |
module = None |
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
147 |
|
148 |
def _check(self, expected, source): |
|
4913.3.8
by John Arbash Meinel
Rename test_bencode to test__bencode, and use self.module |
149 |
self.assertEquals(expected, self.module.bencode(source)) |
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
150 |
|
151 |
def test_int(self): |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
152 |
self._check('i4e', 4) |
153 |
self._check('i0e', 0) |
|
154 |
self._check('i-10e', -10) |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
155 |
|
156 |
def test_long(self): |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
157 |
self._check('i12345678901234567890e', 12345678901234567890L) |
158 |
self._check('i-12345678901234567890e', -12345678901234567890L) |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
159 |
|
160 |
def test_string(self): |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
161 |
self._check('0:', '') |
162 |
self._check('3:abc', 'abc') |
|
163 |
self._check('10:1234567890', '1234567890') |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
164 |
|
165 |
def test_list(self): |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
166 |
self._check('le', []) |
167 |
self._check('li1ei2ei3ee', [1, 2, 3]) |
|
168 |
self._check('ll5:Alice3:Bobeli2ei3eee', [['Alice', 'Bob'], [2, 3]]) |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
169 |
|
2694.5.5
by Jelmer Vernooij
Support bdecode_as_tuple. |
170 |
def test_list_as_tuple(self): |
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
171 |
self._check('le', ()) |
172 |
self._check('li1ei2ei3ee', (1, 2, 3)) |
|
173 |
self._check('ll5:Alice3:Bobeli2ei3eee', (('Alice', 'Bob'), (2, 3))) |
|
2694.5.5
by Jelmer Vernooij
Support bdecode_as_tuple. |
174 |
|
2694.5.20
by Jelmer Vernooij
add handling of deep nesting. |
175 |
def test_list_deep_nested(self): |
176 |
top = [] |
|
177 |
l = top |
|
178 |
for i in range(10000): |
|
179 |
l.append([]) |
|
180 |
l = l[0] |
|
4913.3.8
by John Arbash Meinel
Rename test_bencode to test__bencode, and use self.module |
181 |
self.assertRaises(RuntimeError, self.module.bencode, |
2694.5.20
by Jelmer Vernooij
add handling of deep nesting. |
182 |
top) |
183 |
||
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
184 |
def test_dict(self): |
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
185 |
self._check('de', {}) |
186 |
self._check('d3:agei25e4:eyes4:bluee', {'age': 25, 'eyes': 'blue'}) |
|
187 |
self._check('d8:spam.mp3d6:author5:Alice6:lengthi100000eee', |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
188 |
{'spam.mp3': {'author': 'Alice', |
189 |
'length': 100000}}) |
|
190 |
||
2694.5.20
by Jelmer Vernooij
add handling of deep nesting. |
191 |
def test_dict_deep_nested(self): |
192 |
d = top = {} |
|
193 |
for i in range(10000): |
|
194 |
d[''] = {} |
|
195 |
d = d[''] |
|
4913.3.8
by John Arbash Meinel
Rename test_bencode to test__bencode, and use self.module |
196 |
self.assertRaises(RuntimeError, self.module.bencode, |
2694.5.20
by Jelmer Vernooij
add handling of deep nesting. |
197 |
top) |
198 |
||
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
199 |
def test_bencached(self): |
4913.3.8
by John Arbash Meinel
Rename test_bencode to test__bencode, and use self.module |
200 |
self._check('i3e', self.module.Bencached(self.module.bencode(3))) |
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
201 |
|
202 |
def test_invalid_dict(self): |
|
4913.3.8
by John Arbash Meinel
Rename test_bencode to test__bencode, and use self.module |
203 |
self.assertRaises(TypeError, self.module.bencode, {1:"foo"}) |
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
204 |
|
205 |
def test_bool(self): |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
206 |
self._check('i1e', True) |
207 |
self._check('i0e', False) |
|
2694.5.10
by Jelmer Vernooij
Add some more tests. |
208 |