/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
1
# Copyright (C) 2007 Canonical Ltd
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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):
22
    # parameterize all tests in this module
23
    suite = loader.suiteClass()
24
    import bzrlib._bencode_py as py_module
25
    scenarios = [('python', {'bencode': py_module})]
26
    if CompiledBencodeFeature.available():
27
        import bzrlib._bencode_c 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)
36
    return suite
37
38
39
class _CompiledBencodeFeature(tests.Feature):
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
40
41
    def _probe(self):
2694.5.8 by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations.
42
        try:
43
            import bzrlib._bencode_c
44
        except ImportError:
45
            return False
46
        return True
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
47
48
    def feature_name(self):
49
        return 'bzrlib._bencode_c'
50
2694.5.8 by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations.
51
CompiledBencodeFeature = _CompiledBencodeFeature()
52
53
54
class TestBencodeDecode(tests.TestCase):
55
56
    bencode = None
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
57
58
    def _check(self, expected, source):
59
        self.assertEquals(expected, self.bencode.bdecode(source))
60
61
    def _run_check(self, pairs):
62
        """Run _check for each (expected, source) in pairs list"""
63
        for expected, source in pairs:
64
            self._check(expected, source)
65
66
    def _check_error(self, x):
67
        self.assertRaises(ValueError, self.bencode.bdecode, x)
68
69
    def _run_check_error(self, bads):
70
        """Run _check_error for each x in bads list"""
71
        for x in bads:
72
            self._check_error(x)
73
74
    def test_int(self):
75
        self._run_check([(0, 'i0e'),
76
                         (4, 'i4e'),
77
                         (123456789, 'i123456789e'),
78
                         (-10, 'i-10e')])
79
80
    def test_long(self):
81
        self._run_check([(12345678901234567890L, 'i12345678901234567890e'),
82
                         (-12345678901234567890L, 'i-12345678901234567890e')])
83
84
    def test_malformed_int(self):
85
        self._run_check_error(['ie', 'i-e',
86
                               'i-0e', 'i00e', 'i01e', 'i-03e',
87
                               'i', 'i123',
88
                               'i341foo382e'])
89
90
    def test_string(self):
91
        self._run_check([('', '0:'),
92
                         ('abc', '3:abc'),
93
                         ('1234567890', '10:1234567890')])
94
95
    def test_malformed_string(self):
96
        self._run_check_error(['10:x', '10:', '10',
97
                               '01:x', '00:',
98
                               '35208734823ljdahflajhdf'])
99
100
    def test_list(self):
101
        self._run_check([
102
                         ([], 'le'),
103
                         (['', '', ''], 'l0:0:0:e'),
104
                         ([1, 2, 3], 'li1ei2ei3ee'),
105
                         (['asd', 'xy'], 'l3:asd2:xye'),
106
                         ([['Alice', 'Bob'], [2, 3]],
107
                              'll5:Alice3:Bobeli2ei3eee'),
108
                        ])
109
110
    def test_malformed_list(self):
111
        self._run_check_error(['l', 'l01:ae', 'l0:', 'li1e', 'l-3:e'])
112
113
    def test_dict(self):
114
        self._run_check([({}, 'de'),
115
                         ({'':3}, 'd0:i3ee'),
116
                         ({'age': 25, 'eyes': 'blue'},
117
                            'd3:agei25e4:eyes4:bluee'),
118
                         ({'spam.mp3': {'author': 'Alice', 'length': 100000}},
119
                            'd8:spam.mp3d6:author5:Alice6:lengthi100000eee')])
120
121
    def test_malformed_dict(self):
122
        self._run_check_error(['d', 'defoobar',
123
                               'd3:fooe', 'di1e0:e',
124
                               'd1:b0:1:a0:e',
125
                               'd1:a0:1:a0:e',
126
                               'd0:0:', 'd0:'])
127
128
    def test_empty_string(self):
2694.5.9 by Jelmer Vernooij
Fix tests.
129
        self.assertRaises(ValueError, self.bencode.bdecode, '')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
130
131
    def test_junk(self):
132
        self._run_check_error(['i6easd', '2:abfdjslhfld',
133
                               '0:0:', 'leanfdldjfh'])
134
135
    def test_unknown_object(self):
2694.5.9 by Jelmer Vernooij
Fix tests.
136
        self.assertRaises(ValueError, self.bencode.bdecode, 'relwjhrlewjh')
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
137
2694.5.8 by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations.
138
    def test_unsupported_type(self):
2694.5.9 by Jelmer Vernooij
Fix tests.
139
        self.assertRaises(TypeError, self.bencode.bdecode, float(1.5))
140
        self.assertRaises(TypeError, self.bencode.bdecode, None)
141
        self.assertRaises(TypeError, self.bencode.bdecode, lambda x: x)
142
        self.assertRaises(TypeError, self.bencode.bdecode, object)
143
        self.assertRaises(TypeError, self.bencode.bdecode, u"ie")
144
145
    def test_decoder_type_error(self):
146
        self.assertRaises(TypeError, self.bencode.bdecode, 1)
2694.5.8 by Jelmer Vernooij
Use standard infrastructure for testing python and pyrex bencode implementations.
147
148
149
class TestBencodeEncode(tests.TestCase):
150
151
    bencode = None
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
152
153
    def _check(self, expected, source):
154
        self.assertEquals(expected, self.bencode.bencode(source))
155
156
    def _run_check(self, pairs):
157
        for expected, source in pairs:
158
            self._check(expected, source)
159
160
    def _check_error(self, x):
161
        self.assertRaises(TypeError, self.bencode.bencode, x)
162
163
    def test_int(self):
164
        self._run_check([('i4e', 4),
165
                         ('i0e', 0),
166
                         ('i-10e', -10)])
167
168
    def test_long(self):
169
        self._run_check([('i12345678901234567890e', 12345678901234567890L),
170
                         ('i-12345678901234567890e', -12345678901234567890L)])
171
172
    def test_string(self):
173
        self._run_check([('0:', ''),
174
                         ('3:abc', 'abc'),
175
                         ('10:1234567890', '1234567890')])
176
177
    def test_list(self):
178
        self._run_check([('le', []),
179
                         ('li1ei2ei3ee', [1, 2, 3]),
180
                         ('ll5:Alice3:Bobeli2ei3eee',
181
                            [['Alice', 'Bob'], [2, 3]])
182
                        ])
183
2694.5.5 by Jelmer Vernooij
Support bdecode_as_tuple.
184
    def test_list_as_tuple(self):
185
        self._run_check([('le', ()),
186
                         ('li1ei2ei3ee', (1, 2, 3)),
187
                         ('ll5:Alice3:Bobeli2ei3eee',
188
                            (('Alice', 'Bob'), (2, 3)))
189
                        ])
190
2694.5.1 by Alexander Belchenko
pyrex bencode (without benchmarks)
191
    def test_dict(self):
192
        self._run_check([('de', {}),
193
                         ('d3:agei25e4:eyes4:bluee',
194
                            {'age': 25, 'eyes': 'blue'}),
195
                         ('d8:spam.mp3d6:author5:Alice6:lengthi100000eee',
196
                            {'spam.mp3': {'author': 'Alice',
197
                                          'length': 100000}})
198
                         ])
199
200
    def test_bencached(self):
201
        self._check('i3e', self.bencode.Bencached(self.bencode.bencode(3)))
202
203
    def test_invalid_dict(self):
204
        self._check_error({1: 'foo'})
205
206
    def test_bool(self):
207
        self._run_check([('i1e', True),
208
                         ('i0e', False)])