~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bencode.py

  • Committer: Martin Pool
  • Date: 2009-07-10 06:46:10 UTC
  • mto: (4525.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4526.
  • Revision ID: mbp@sourcefrog.net-20090710064610-sqviksbqp5i34sw2
Rename to per_interrepository

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2007 Canonical Ltd
2
2
#
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
16
16
 
17
17
"""Tests for bencode structured encoding"""
18
18
 
19
 
import sys
20
 
 
21
19
from bzrlib import tests
22
20
 
23
21
def load_tests(standard_tests, module, loader):
24
 
    suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
25
 
        'bzrlib.util._bencode_py', 'bzrlib._bencode_pyx')
 
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}))
 
29
    else:
 
30
        # the compiled module isn't available, so we add a failing test
 
31
        class FailWithoutFeature(tests.TestCase):
 
32
            def test_fail(self):
 
33
                self.requireFeature(CompiledBencodeFeature)
 
34
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
 
35
    tests.multiply_tests(standard_tests, scenarios, suite)
26
36
    return suite
27
37
 
28
38
 
 
39
class _CompiledBencodeFeature(tests.Feature):
 
40
 
 
41
    def _probe(self):
 
42
        try:
 
43
            import bzrlib._bencode_pyx
 
44
        except ImportError:
 
45
            return False
 
46
        return True
 
47
 
 
48
    def feature_name(self):
 
49
        return 'bzrlib._bencode_pyx'
 
50
 
 
51
CompiledBencodeFeature = _CompiledBencodeFeature()
 
52
 
 
53
 
29
54
class TestBencodeDecode(tests.TestCase):
30
55
 
31
 
    module = None
 
56
    bencode = None
32
57
 
33
58
    def _check(self, expected, source):
34
 
        self.assertEquals(expected, self.module.bdecode(source))
 
59
        self.assertEquals(expected, self.bencode.bdecode(source))
35
60
 
36
61
    def _run_check_error(self, exc, bad):
37
62
        """Check that bdecoding a string raises a particular exception."""
38
 
        self.assertRaises(exc, self.module.bdecode, bad)
 
63
        self.assertRaises(exc, self.bencode.bdecode, bad)
39
64
 
40
65
    def test_int(self):
41
66
        self._check(0, 'i0e')
66
91
        self._check('1234567890', '10:1234567890')
67
92
 
68
93
    def test_large_string(self):
69
 
        self.assertRaises(ValueError, self.module.bdecode, "2147483639:foo")
 
94
        self.assertRaises(ValueError, self.bencode.bdecode, "2147483639:foo")
70
95
 
71
96
    def test_malformed_string(self):
72
97
        self._run_check_error(ValueError, '10:x')
107
132
                            'd8:spam.mp3d6:author5:Alice6:lengthi100000eee')
108
133
 
109
134
    def test_dict_deepnested(self):
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))
 
135
        self._run_check_error(RuntimeError, ("d0:" * 10000) + 'i1e' + ("e" * 10000))
118
136
 
119
137
    def test_malformed_dict(self):
120
138
        self._run_check_error(ValueError, 'd')
128
146
        self._run_check_error(ValueError, 'd432432432432432432:e')
129
147
 
130
148
    def test_empty_string(self):
131
 
        self.assertRaises(ValueError, self.module.bdecode, '')
 
149
        self.assertRaises(ValueError, self.bencode.bdecode, '')
132
150
 
133
151
    def test_junk(self):
134
152
        self._run_check_error(ValueError, 'i6easd')
137
155
        self._run_check_error(ValueError, 'leanfdldjfh')
138
156
 
139
157
    def test_unknown_object(self):
140
 
        self.assertRaises(ValueError, self.module.bdecode, 'relwjhrlewjh')
 
158
        self.assertRaises(ValueError, self.bencode.bdecode, 'relwjhrlewjh')
141
159
 
142
160
    def test_unsupported_type(self):
143
161
        self._run_check_error(TypeError, float(1.5))
147
165
        self._run_check_error(TypeError, u"ie")
148
166
 
149
167
    def test_decoder_type_error(self):
150
 
        self.assertRaises(TypeError, self.module.bdecode, 1)
 
168
        self.assertRaises(TypeError, self.bencode.bdecode, 1)
151
169
 
152
170
 
153
171
class TestBencodeEncode(tests.TestCase):
154
172
 
155
 
    module = None
 
173
    bencode = None
156
174
 
157
175
    def _check(self, expected, source):
158
 
        self.assertEquals(expected, self.module.bencode(source))
 
176
        self.assertEquals(expected, self.bencode.bencode(source))
159
177
 
160
178
    def test_int(self):
161
179
        self._check('i4e', 4)
187
205
        for i in range(10000):
188
206
            l.append([])
189
207
            l = l[0]
190
 
        self.assertRaises(RuntimeError, self.module.bencode, 
 
208
        self.assertRaises(RuntimeError, self.bencode.bencode, 
191
209
            top)
192
210
 
193
211
    def test_dict(self):
202
220
        for i in range(10000):
203
221
            d[''] = {}
204
222
            d = d['']
205
 
        self.assertRaises(RuntimeError, self.module.bencode, 
 
223
        self.assertRaises(RuntimeError, self.bencode.bencode, 
206
224
            top)
207
225
 
208
226
    def test_bencached(self):
209
 
        self._check('i3e', self.module.Bencached(self.module.bencode(3)))
 
227
        self._check('i3e', self.bencode.Bencached(self.bencode.bencode(3)))
210
228
 
211
229
    def test_invalid_dict(self):
212
 
        self.assertRaises(TypeError, self.module.bencode, {1:"foo"})
 
230
        self.assertRaises(TypeError, self.bencode.bencode, {1:"foo"})
213
231
 
214
232
    def test_bool(self):
215
233
        self._check('i1e', True)