/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__bencode.py

  • Committer: Vincent Ladeuil
  • Date: 2012-01-18 14:09:19 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120118140919-rlvdrhpc0nq1lbwi
Change set/remove to require a lock for the branch config files.

This means that tests (or any plugin for that matter) do not requires an
explicit lock on the branch anymore to change a single option. This also
means the optimisation becomes "opt-in" and as such won't be as
spectacular as it may be and/or harder to get right (nothing fails
anymore).

This reduces the diff by ~300 lines.

Code/tests that were updating more than one config option is still taking
a lock to at least avoid some IOs and demonstrate the benefits through
the decreased number of hpss calls.

The duplication between BranchStack and BranchOnlyStack will be removed
once the same sharing is in place for local config files, at which point
the Stack class itself may be able to host the changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007, 2009, 2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Tests for bencode structured encoding"""
 
18
 
 
19
import sys
 
20
 
 
21
from bzrlib import tests
 
22
 
 
23
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')
 
26
    return suite
 
27
 
 
28
 
 
29
class TestBencodeDecode(tests.TestCase):
 
30
 
 
31
    module = None
 
32
 
 
33
    def _check(self, expected, source):
 
34
        self.assertEquals(expected, self.module.bdecode(source))
 
35
 
 
36
    def _run_check_error(self, exc, bad):
 
37
        """Check that bdecoding a string raises a particular exception."""
 
38
        self.assertRaises(exc, self.module.bdecode, bad)
 
39
 
 
40
    def test_int(self):
 
41
        self._check(0, 'i0e')
 
42
        self._check(4, 'i4e')
 
43
        self._check(123456789, 'i123456789e')
 
44
        self._check(-10, 'i-10e')
 
45
        self._check(int('1' * 1000), 'i' + ('1' * 1000) + 'e')
 
46
 
 
47
    def test_long(self):
 
48
        self._check(12345678901234567890L, 'i12345678901234567890e')
 
49
        self._check(-12345678901234567890L, 'i-12345678901234567890e')
 
50
 
 
51
    def test_malformed_int(self):
 
52
        self._run_check_error(ValueError, 'ie')
 
53
        self._run_check_error(ValueError, 'i-e')
 
54
        self._run_check_error(ValueError, 'i-010e')
 
55
        self._run_check_error(ValueError, 'i-0e')
 
56
        self._run_check_error(ValueError, 'i00e')
 
57
        self._run_check_error(ValueError, 'i01e')
 
58
        self._run_check_error(ValueError, 'i-03e')
 
59
        self._run_check_error(ValueError, 'i')
 
60
        self._run_check_error(ValueError, 'i123')
 
61
        self._run_check_error(ValueError, 'i341foo382e')
 
62
 
 
63
    def test_string(self):
 
64
        self._check('', '0:')
 
65
        self._check('abc', '3:abc')
 
66
        self._check('1234567890', '10:1234567890')
 
67
 
 
68
    def test_large_string(self):
 
69
        self.assertRaises(ValueError, self.module.bdecode, "2147483639:foo")
 
70
 
 
71
    def test_malformed_string(self):
 
72
        self._run_check_error(ValueError, '10:x')
 
73
        self._run_check_error(ValueError, '10:')
 
74
        self._run_check_error(ValueError, '10')
 
75
        self._run_check_error(ValueError, '01:x')
 
76
        self._run_check_error(ValueError, '00:')
 
77
        self._run_check_error(ValueError, '35208734823ljdahflajhdf')
 
78
        self._run_check_error(ValueError, '432432432432432:foo')
 
79
        self._run_check_error(ValueError, ' 1:x') # leading whitespace
 
80
        self._run_check_error(ValueError, '-1:x') # negative
 
81
        self._run_check_error(ValueError, '1 x') # space vs colon
 
82
        self._run_check_error(ValueError, '1x') # missing colon
 
83
        self._run_check_error(ValueError, ('1' * 1000) + ':')
 
84
 
 
85
    def test_list(self):
 
86
        self._check([], 'le')
 
87
        self._check(['', '', ''], 'l0:0:0:e')
 
88
        self._check([1, 2, 3], 'li1ei2ei3ee')
 
89
        self._check(['asd', 'xy'], 'l3:asd2:xye')
 
90
        self._check([['Alice', 'Bob'], [2, 3]], 'll5:Alice3:Bobeli2ei3eee')
 
91
 
 
92
    def test_list_deepnested(self):
 
93
        self._run_check_error(RuntimeError, ("l" * 10000) + ("e" * 10000))
 
94
 
 
95
    def test_malformed_list(self):
 
96
        self._run_check_error(ValueError, 'l')
 
97
        self._run_check_error(ValueError, 'l01:ae')
 
98
        self._run_check_error(ValueError, 'l0:')
 
99
        self._run_check_error(ValueError, 'li1e')
 
100
        self._run_check_error(ValueError, 'l-3:e')
 
101
 
 
102
    def test_dict(self):
 
103
        self._check({}, 'de')
 
104
        self._check({'':3}, 'd0:i3ee')
 
105
        self._check({'age': 25, 'eyes': 'blue'}, 'd3:agei25e4:eyes4:bluee')
 
106
        self._check({'spam.mp3': {'author': 'Alice', 'length': 100000}},
 
107
                            'd8:spam.mp3d6:author5:Alice6:lengthi100000eee')
 
108
 
 
109
    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))
 
118
 
 
119
    def test_malformed_dict(self):
 
120
        self._run_check_error(ValueError, 'd')
 
121
        self._run_check_error(ValueError, 'defoobar')
 
122
        self._run_check_error(ValueError, 'd3:fooe')
 
123
        self._run_check_error(ValueError, 'di1e0:e')
 
124
        self._run_check_error(ValueError, 'd1:b0:1:a0:e')
 
125
        self._run_check_error(ValueError, 'd1:a0:1:a0:e')
 
126
        self._run_check_error(ValueError, 'd0:0:')
 
127
        self._run_check_error(ValueError, 'd0:')
 
128
        self._run_check_error(ValueError, 'd432432432432432432:e')
 
129
 
 
130
    def test_empty_string(self):
 
131
        self.assertRaises(ValueError, self.module.bdecode, '')
 
132
 
 
133
    def test_junk(self):
 
134
        self._run_check_error(ValueError, 'i6easd')
 
135
        self._run_check_error(ValueError, '2:abfdjslhfld')
 
136
        self._run_check_error(ValueError, '0:0:')
 
137
        self._run_check_error(ValueError, 'leanfdldjfh')
 
138
 
 
139
    def test_unknown_object(self):
 
140
        self.assertRaises(ValueError, self.module.bdecode, 'relwjhrlewjh')
 
141
 
 
142
    def test_unsupported_type(self):
 
143
        self._run_check_error(TypeError, float(1.5))
 
144
        self._run_check_error(TypeError, None)
 
145
        self._run_check_error(TypeError, lambda x: x)
 
146
        self._run_check_error(TypeError, object)
 
147
        self._run_check_error(TypeError, u"ie")
 
148
 
 
149
    def test_decoder_type_error(self):
 
150
        self.assertRaises(TypeError, self.module.bdecode, 1)
 
151
 
 
152
 
 
153
class TestBencodeEncode(tests.TestCase):
 
154
 
 
155
    module = None
 
156
 
 
157
    def _check(self, expected, source):
 
158
        self.assertEquals(expected, self.module.bencode(source))
 
159
 
 
160
    def test_int(self):
 
161
        self._check('i4e', 4)
 
162
        self._check('i0e', 0)
 
163
        self._check('i-10e', -10)
 
164
 
 
165
    def test_long(self):
 
166
        self._check('i12345678901234567890e', 12345678901234567890L)
 
167
        self._check('i-12345678901234567890e', -12345678901234567890L)
 
168
 
 
169
    def test_string(self):
 
170
        self._check('0:', '')
 
171
        self._check('3:abc', 'abc')
 
172
        self._check('10:1234567890', '1234567890')
 
173
 
 
174
    def test_list(self):
 
175
        self._check('le', [])
 
176
        self._check('li1ei2ei3ee', [1, 2, 3])
 
177
        self._check('ll5:Alice3:Bobeli2ei3eee', [['Alice', 'Bob'], [2, 3]])
 
178
 
 
179
    def test_list_as_tuple(self):
 
180
        self._check('le', ())
 
181
        self._check('li1ei2ei3ee', (1, 2, 3))
 
182
        self._check('ll5:Alice3:Bobeli2ei3eee', (('Alice', 'Bob'), (2, 3)))
 
183
 
 
184
    def test_list_deep_nested(self):
 
185
        top = []
 
186
        l = top
 
187
        for i in range(10000):
 
188
            l.append([])
 
189
            l = l[0]
 
190
        self.assertRaises(RuntimeError, self.module.bencode, 
 
191
            top)
 
192
 
 
193
    def test_dict(self):
 
194
        self._check('de', {})
 
195
        self._check('d3:agei25e4:eyes4:bluee', {'age': 25, 'eyes': 'blue'})
 
196
        self._check('d8:spam.mp3d6:author5:Alice6:lengthi100000eee',
 
197
                            {'spam.mp3': {'author': 'Alice',
 
198
                                          'length': 100000}})
 
199
 
 
200
    def test_dict_deep_nested(self):
 
201
        d = top = {}
 
202
        for i in range(10000):
 
203
            d[''] = {}
 
204
            d = d['']
 
205
        self.assertRaises(RuntimeError, self.module.bencode, 
 
206
            top)
 
207
 
 
208
    def test_bencached(self):
 
209
        self._check('i3e', self.module.Bencached(self.module.bencode(3)))
 
210
 
 
211
    def test_invalid_dict(self):
 
212
        self.assertRaises(TypeError, self.module.bencode, {1:"foo"})
 
213
 
 
214
    def test_bool(self):
 
215
        self._check('i1e', True)
 
216
        self._check('i0e', False)
 
217