4
Tests for the bencode module
7
__author__ = "Tom Lazar (tom@tomster.org)"
8
__version__ = "$Revision: 0.1 $"
9
__date__ = "$Date: 2007/07/29 $"
10
__copyright__ = "Copyright (c) 2007 Tom Lazar"
11
__license__ = "BitTorrent Open Source License"
13
# Python 3 port, UTF-8 support, PEP-8 compliance, implemented commented-
14
# out test cases, formatting by Christopher Foo :)
16
from .bencode import bencode
17
from .bencode import bdecode
18
from .bencode import BTFailure
23
class KnownValues(unittest.TestCase):
25
* example values partially taken from
26
http://en.wikipedia.org/wiki/Bencode
27
* test case inspired by Mark Pilgrim's examples:
28
http://diveintopython.org/unit_testing/romantest.html
30
knownValues = ((0, b'i0e'),
38
(b'parrot sketch', b'13:parrot sketch'),
39
([b'parrot sketch', 42], b'l13:parrot sketchi42ee'),
41
# Now thats UTF-8 testing!—Christopher Foo
42
('ðÞở'.encode(), b'9:\xc3\xb0\xc3\x9e\xe1\xbb\x8f\xcc\x9b'),
47
}, b'd3:bar4:spam3:fooi42ee'),
50
def testBencodeKnownValues(self):
51
"""bencode should give known result with known input"""
52
for plain, encoded in self.knownValues:
53
result = bencode(plain)
54
self.assertEqual(encoded, result)
56
def testBdecodeKnownValues(self):
57
"""bdecode should give known result with known input"""
58
for plain, encoded in self.knownValues:
60
result = bdecode(encoded)
61
self.assertEqual(plain, result)
63
def testRoundtripEncoded(self):
64
"""consecutive calls to bdecode and bencode should deliver the original
67
for plain, encoded in self.knownValues:
68
result = bdecode(encoded)
69
self.assertEqual(encoded, bencode(result))
71
def testRoundtripDecoded(self):
72
"""consecutive calls to bencode and bdecode should deliver the original
75
for plain, encoded in self.knownValues:
76
result = bencode(plain)
77
self.assertEqual(plain, bdecode(result))
80
class IllegaleValues(unittest.TestCase):
81
""" handling of illegal values"""
83
def testFloatRaisesIllegalForEncode(self):
84
""" floats cannot be encoded. """
86
self.assertRaises(BTFailure, bencode, 1.0)
88
def testNonStringsRaiseIllegalInputForDecode(self):
89
""" non-strings should raise an exception. """
91
self.assertRaises(BTFailure, bdecode, 0)
92
self.assertRaises(BTFailure, bdecode, None)
93
self.assertRaises(BTFailure, bdecode, 1.0)
94
self.assertRaises(BTFailure, bdecode, [1, 2])
95
self.assertRaises(BTFailure, bdecode, {'foo': 'bar'})
97
def testRaiseIllegalInputForDecode(self):
98
"""illegally formatted strings should raise an exception when decoded.
100
self.assertRaises(BTFailure, bdecode, b"foo")
101
self.assertRaises(BTFailure, bdecode, b"x:foo")
102
self.assertRaises(BTFailure, bdecode, b"x42e")
105
class Dictionaries(unittest.TestCase):
106
""" handling of dictionaries """
108
def testSortedKeysForDicts(self):
109
""" the keys of a dictionary must be sorted before encoded. """
111
dict_ = {b'zoo': 42, b'bar': b'spam'}
112
encoded_dict = bencode(dict_)
114
encoded_dict.index(b'zoo') > encoded_dict.index(b'bar'))
116
def testNestedDictionary(self):
117
""" tests for handling of nested dicts"""
118
dict_ = {b'foo': 42, b'bar': {b'sketch': b'parrot', b'foobar': 23}}
120
encoded_dict = bencode(dict_)
121
self.assertEqual(encoded_dict,
122
b"d3:bard6:foobari23e6:sketch6:parrote3:fooi42ee")
125
if __name__ == "__main__":