1
# Copyright (C) 2007 Canonical Ltd
1
# Copyright (C) 2007, 2009, 2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
"""Tests for bencode structured encoding"""
19
21
from bzrlib import tests
21
23
def load_tests(standard_tests, module, loader):
22
# parameterize all tests in this module
23
suite = loader.suiteClass()
24
import bzrlib.util._bencode_py as py_module
25
scenarios = [('python', {'bencode': py_module})]
26
if CompiledBencodeFeature.available():
27
import bzrlib._bencode_pyx as c_module
28
scenarios.append(('C', {'bencode': c_module}))
30
# the compiled module isn't available, so we add a failing test
31
class FailWithoutFeature(tests.TestCase):
33
self.requireFeature(CompiledBencodeFeature)
34
suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
35
tests.multiply_tests(standard_tests, scenarios, suite)
24
suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
25
'bzrlib.util._bencode_py', 'bzrlib._bencode_pyx')
39
class _CompiledBencodeFeature(tests.Feature):
43
import bzrlib._bencode_pyx
48
def feature_name(self):
49
return 'bzrlib._bencode_pyx'
51
CompiledBencodeFeature = _CompiledBencodeFeature()
54
29
class TestBencodeDecode(tests.TestCase):
58
33
def _check(self, expected, source):
59
self.assertEquals(expected, self.bencode.bdecode(source))
34
self.assertEquals(expected, self.module.bdecode(source))
61
36
def _run_check_error(self, exc, bad):
62
37
"""Check that bdecoding a string raises a particular exception."""
63
self.assertRaises(exc, self.bencode.bdecode, bad)
38
self.assertRaises(exc, self.module.bdecode, bad)
65
40
def test_int(self):
66
41
self._check(0, 'i0e')
91
66
self._check('1234567890', '10:1234567890')
93
68
def test_large_string(self):
94
self.assertRaises(ValueError, self.bencode.bdecode, "2147483639:foo")
69
self.assertRaises(ValueError, self.module.bdecode, "2147483639:foo")
96
71
def test_malformed_string(self):
97
72
self._run_check_error(ValueError, '10:x')
132
107
'd8:spam.mp3d6:author5:Alice6:lengthi100000eee')
134
109
def test_dict_deepnested(self):
135
self._run_check_error(RuntimeError, ("d0:" * 10000) + 'i1e' + ("e" * 10000))
110
# The recursion here provokes CPython into emitting a warning on
111
# stderr, "maximum recursion depth exceeded in __subclasscheck__", due
112
# to running out of stack space while evaluating "except (...):" in
113
# _bencode_py. This is harmless, so we temporarily override stderr to
114
# avoid distracting noise in the test output.
115
self.overrideAttr(sys, 'stderr', self._log_file)
116
self._run_check_error(
117
RuntimeError, ("d0:" * 10000) + 'i1e' + ("e" * 10000))
137
119
def test_malformed_dict(self):
138
120
self._run_check_error(ValueError, 'd')
146
128
self._run_check_error(ValueError, 'd432432432432432432:e')
148
130
def test_empty_string(self):
149
self.assertRaises(ValueError, self.bencode.bdecode, '')
131
self.assertRaises(ValueError, self.module.bdecode, '')
151
133
def test_junk(self):
152
134
self._run_check_error(ValueError, 'i6easd')
155
137
self._run_check_error(ValueError, 'leanfdldjfh')
157
139
def test_unknown_object(self):
158
self.assertRaises(ValueError, self.bencode.bdecode, 'relwjhrlewjh')
140
self.assertRaises(ValueError, self.module.bdecode, 'relwjhrlewjh')
160
142
def test_unsupported_type(self):
161
143
self._run_check_error(TypeError, float(1.5))
165
147
self._run_check_error(TypeError, u"ie")
167
149
def test_decoder_type_error(self):
168
self.assertRaises(TypeError, self.bencode.bdecode, 1)
150
self.assertRaises(TypeError, self.module.bdecode, 1)
171
153
class TestBencodeEncode(tests.TestCase):
175
157
def _check(self, expected, source):
176
self.assertEquals(expected, self.bencode.bencode(source))
158
self.assertEquals(expected, self.module.bencode(source))
178
160
def test_int(self):
179
161
self._check('i4e', 4)
220
202
for i in range(10000):
223
self.assertRaises(RuntimeError, self.bencode.bencode,
205
self.assertRaises(RuntimeError, self.module.bencode,
226
208
def test_bencached(self):
227
self._check('i3e', self.bencode.Bencached(self.bencode.bencode(3)))
209
self._check('i3e', self.module.Bencached(self.module.bencode(3)))
229
211
def test_invalid_dict(self):
230
self.assertRaises(TypeError, self.bencode.bencode, {1:"foo"})
212
self.assertRaises(TypeError, self.module.bencode, {1:"foo"})
232
214
def test_bool(self):
233
215
self._check('i1e', True)