~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/util/tests/test_bencode.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# bencode structured encoding
 
2
#
 
3
# Written by Petru Paler
 
4
#
 
5
# Permission is hereby granted, free of charge, to any person
 
6
# obtaining a copy of this software and associated documentation files
 
7
# (the "Software"), to deal in the Software without restriction,
 
8
# including without limitation the rights to use, copy, modify, merge,
 
9
# publish, distribute, sublicense, and/or sell copies of the Software,
 
10
# and to permit persons to whom the Software is furnished to do so,
 
11
# subject to the following conditions:
 
12
 
13
# The above copyright notice and this permission notice shall be
 
14
# included in all copies or substantial portions of the Software.
 
15
 
16
# The Software is provided "AS IS", without warranty of any kind,
 
17
# express or implied, including but not limited to the warranties of
 
18
# merchantability,  fitness for a particular purpose and
 
19
# noninfringement. In no event shall the  authors or copyright holders
 
20
# be liable for any claim, damages or other liability, whether in an
 
21
# action of contract, tort or otherwise, arising from, out of or in
 
22
# connection with the Software or the use or other dealings in the
 
23
# Software.
 
24
 
 
25
 
 
26
from bzrlib.util.bencode import bencode, bdecode, Bencached
 
27
from bzrlib.tests import TestCase
 
28
 
 
29
class TestBencode(TestCase):
 
30
    # tests moved from within the bencode module so they're not run on every
 
31
    # startup
 
32
 
 
33
    def test_bdecode(self):
 
34
        try:
 
35
            bdecode('0:0:')
 
36
            assert 0
 
37
        except ValueError:
 
38
            pass
 
39
        try:
 
40
            bdecode('ie')
 
41
            assert 0
 
42
        except ValueError:
 
43
            pass
 
44
        try:
 
45
            bdecode('i341foo382e')
 
46
            assert 0
 
47
        except ValueError:
 
48
            pass
 
49
        assert bdecode('i4e') == 4L
 
50
        assert bdecode('i0e') == 0L
 
51
        assert bdecode('i123456789e') == 123456789L
 
52
        assert bdecode('i-10e') == -10L
 
53
        try:
 
54
            bdecode('i-0e')
 
55
            assert 0
 
56
        except ValueError:
 
57
            pass
 
58
        try:
 
59
            bdecode('i123')
 
60
            assert 0
 
61
        except ValueError:
 
62
            pass
 
63
        try:
 
64
            bdecode('')
 
65
            assert 0
 
66
        except ValueError:
 
67
            pass
 
68
        try:
 
69
            bdecode('i6easd')
 
70
            assert 0
 
71
        except ValueError:
 
72
            pass
 
73
        try:
 
74
            bdecode('35208734823ljdahflajhdf')
 
75
            assert 0
 
76
        except ValueError:
 
77
            pass
 
78
        try:
 
79
            bdecode('2:abfdjslhfld')
 
80
            assert 0
 
81
        except ValueError:
 
82
            pass
 
83
        assert bdecode('0:') == ''
 
84
        assert bdecode('3:abc') == 'abc'
 
85
        assert bdecode('10:1234567890') == '1234567890'
 
86
        try:
 
87
            bdecode('02:xy')
 
88
            assert 0
 
89
        except ValueError:
 
90
            pass
 
91
        try:
 
92
            bdecode('l')
 
93
            assert 0
 
94
        except ValueError:
 
95
            pass
 
96
        assert bdecode('le') == []
 
97
        try:
 
98
            bdecode('leanfdldjfh')
 
99
            assert 0
 
100
        except ValueError:
 
101
            pass
 
102
        assert bdecode('l0:0:0:e') == ['', '', '']
 
103
        try:
 
104
            bdecode('relwjhrlewjh')
 
105
            assert 0
 
106
        except ValueError:
 
107
            pass
 
108
        assert bdecode('li1ei2ei3ee') == [1, 2, 3]
 
109
        assert bdecode('l3:asd2:xye') == ['asd', 'xy']
 
110
        assert bdecode('ll5:Alice3:Bobeli2ei3eee') == [['Alice', 'Bob'], [2, 3]]
 
111
        try:
 
112
            bdecode('d')
 
113
            assert 0
 
114
        except ValueError:
 
115
            pass
 
116
        try:
 
117
            bdecode('defoobar')
 
118
            assert 0
 
119
        except ValueError:
 
120
            pass
 
121
        assert bdecode('de') == {}
 
122
        assert bdecode('d3:agei25e4:eyes4:bluee') == {'age': 25, 'eyes': 'blue'}
 
123
        assert bdecode('d8:spam.mp3d6:author5:Alice6:lengthi100000eee') == {'spam.mp3': {'author': 'Alice', 'length': 100000}}
 
124
        try:
 
125
            bdecode('d3:fooe')
 
126
            assert 0
 
127
        except ValueError:
 
128
            pass
 
129
        try:
 
130
            bdecode('di1e0:e')
 
131
            assert 0
 
132
        except ValueError:
 
133
            pass
 
134
        try:
 
135
            bdecode('d1:b0:1:a0:e')
 
136
            assert 0
 
137
        except ValueError:
 
138
            pass
 
139
        try:
 
140
            bdecode('d1:a0:1:a0:e')
 
141
            assert 0
 
142
        except ValueError:
 
143
            pass
 
144
        try:
 
145
            bdecode('i03e')
 
146
            assert 0
 
147
        except ValueError:
 
148
            pass
 
149
        try:
 
150
            bdecode('l01:ae')
 
151
            assert 0
 
152
        except ValueError:
 
153
            pass
 
154
        try:
 
155
            bdecode('9999:x')
 
156
            assert 0
 
157
        except ValueError:
 
158
            pass
 
159
        try:
 
160
            bdecode('l0:')
 
161
            assert 0
 
162
        except ValueError:
 
163
            pass
 
164
        try:
 
165
            bdecode('d0:0:')
 
166
            assert 0
 
167
        except ValueError:
 
168
            pass
 
169
        try:
 
170
            bdecode('d0:')
 
171
            assert 0
 
172
        except ValueError:
 
173
            pass
 
174
        try:
 
175
            bdecode('00:')
 
176
            assert 0
 
177
        except ValueError:
 
178
            pass
 
179
        try:
 
180
            bdecode('l-3:e')
 
181
            assert 0
 
182
        except ValueError:
 
183
            pass
 
184
        try:
 
185
            bdecode('i-03e')
 
186
            assert 0
 
187
        except ValueError:
 
188
            pass
 
189
        bdecode('d0:i3ee')
 
190
 
 
191
 
 
192
    def test_bencode(self):
 
193
        assert bencode(4) == 'i4e'
 
194
        assert bencode(0) == 'i0e'
 
195
        assert bencode(-10) == 'i-10e'
 
196
        assert bencode(12345678901234567890L) == 'i12345678901234567890e'
 
197
        assert bencode('') == '0:'
 
198
        assert bencode('abc') == '3:abc'
 
199
        assert bencode('1234567890') == '10:1234567890'
 
200
        assert bencode([]) == 'le'
 
201
        assert bencode([1, 2, 3]) == 'li1ei2ei3ee'
 
202
        assert bencode([['Alice', 'Bob'], [2, 3]]) == 'll5:Alice3:Bobeli2ei3eee'
 
203
        assert bencode({}) == 'de'
 
204
        assert bencode({'age': 25, 'eyes': 'blue'}) == 'd3:agei25e4:eyes4:bluee'
 
205
        assert bencode({'spam.mp3': {'author': 'Alice', 'length': 100000}}) == 'd8:spam.mp3d6:author5:Alice6:lengthi100000eee'
 
206
        assert bencode(Bencached(bencode(3))) == 'i3e'
 
207
        try:
 
208
            bencode({1: 'foo'})
 
209
        except TypeError:
 
210
            return
 
211
        assert 0
 
212