1
# bencode structured encoding
3
# Written by Petru Paler
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:
13
# The above copyright notice and this permission notice shall be
14
# included in all copies or substantial portions of the Software.
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
26
from bzrlib.util.bencode import bencode, bdecode, Bencached
27
from bzrlib.tests import TestCase
29
class TestBencode(TestCase):
30
# tests moved from within the bencode module so they're not run on every
33
def test_bdecode(self):
45
bdecode('i341foo382e')
49
assert bdecode('i4e') == 4L
50
assert bdecode('i0e') == 0L
51
assert bdecode('i123456789e') == 123456789L
52
assert bdecode('i-10e') == -10L
74
bdecode('35208734823ljdahflajhdf')
79
bdecode('2:abfdjslhfld')
83
assert bdecode('0:') == ''
84
assert bdecode('3:abc') == 'abc'
85
assert bdecode('10:1234567890') == '1234567890'
96
assert bdecode('le') == []
98
bdecode('leanfdldjfh')
102
assert bdecode('l0:0:0:e') == ['', '', '']
104
bdecode('relwjhrlewjh')
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]]
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}}
135
bdecode('d1:b0:1:a0:e')
140
bdecode('d1:a0:1:a0:e')
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'