~chris.foo/bytestag/trunk

« back to all changes in this revision

Viewing changes to src/py3/bytestag/lib/bencode_test.py

  • Committer: Christopher Foo
  • Date: 2012-09-09 15:38:30 UTC
  • mfrom: (24.1.28 trunk)
  • Revision ID: chris.foo@gmail.com-20120909153830-575fqdbsju8dmg0k
Merges from trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# encoding: utf-8
 
3
"""
 
4
    Tests for the bencode module
 
5
"""
 
6
 
 
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"
 
12
 
 
13
# Python 3 port, UTF-8 support, PEP-8 compliance, implemented commented-
 
14
# out test cases, formatting by Christopher Foo :)
 
15
 
 
16
from .bencode import bencode
 
17
from .bencode import bdecode
 
18
from .bencode import BTFailure
 
19
 
 
20
import unittest
 
21
 
 
22
 
 
23
class KnownValues(unittest.TestCase):
 
24
    """
 
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
 
29
    """
 
30
    knownValues = ((0, b'i0e'),
 
31
                    (1, b'i1e'),
 
32
                    (10, b'i10e'),
 
33
                    (42, b'i42e'),
 
34
                    (-42, b'i-42e'),
 
35
                    (True, b'i1e'),
 
36
                    (False, b'i0e'),
 
37
                    (b'spam', b'4:spam'),
 
38
                    (b'parrot sketch', b'13:parrot sketch'),
 
39
                    ([b'parrot sketch', 42], b'l13:parrot sketchi42ee'),
 
40
 
 
41
                    # Now thats UTF-8 testing!—Christopher Foo
 
42
                    ('ðÞở'.encode(), b'9:\xc3\xb0\xc3\x9e\xe1\xbb\x8f\xcc\x9b'),
 
43
 
 
44
                    ({
 
45
                        b'foo': 42,
 
46
                        b'bar': b'spam'
 
47
                    }, b'd3:bar4:spam3:fooi42ee'),
 
48
                  )
 
49
 
 
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)
 
55
 
 
56
    def testBdecodeKnownValues(self):
 
57
        """bdecode should give known result with known input"""
 
58
        for plain, encoded in self.knownValues:
 
59
            print(plain, encoded)
 
60
            result = bdecode(encoded)
 
61
            self.assertEqual(plain, result)
 
62
 
 
63
    def testRoundtripEncoded(self):
 
64
        """consecutive calls to bdecode and bencode should deliver the original
 
65
        data again
 
66
        """
 
67
        for plain, encoded in self.knownValues:
 
68
            result = bdecode(encoded)
 
69
            self.assertEqual(encoded, bencode(result))
 
70
 
 
71
    def testRoundtripDecoded(self):
 
72
        """consecutive calls to bencode and bdecode should deliver the original
 
73
        data again
 
74
        """
 
75
        for plain, encoded in self.knownValues:
 
76
            result = bencode(plain)
 
77
            self.assertEqual(plain, bdecode(result))
 
78
 
 
79
 
 
80
class IllegaleValues(unittest.TestCase):
 
81
    """ handling of illegal values"""
 
82
 
 
83
    def testFloatRaisesIllegalForEncode(self):
 
84
        """ floats cannot be encoded. """
 
85
 
 
86
        self.assertRaises(BTFailure, bencode, 1.0)
 
87
 
 
88
    def testNonStringsRaiseIllegalInputForDecode(self):
 
89
        """ non-strings should raise an exception. """
 
90
 
 
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'})
 
96
 
 
97
    def testRaiseIllegalInputForDecode(self):
 
98
        """illegally formatted strings should raise an exception when decoded.
 
99
        """
 
100
        self.assertRaises(BTFailure, bdecode, b"foo")
 
101
        self.assertRaises(BTFailure, bdecode, b"x:foo")
 
102
        self.assertRaises(BTFailure, bdecode, b"x42e")
 
103
 
 
104
 
 
105
class Dictionaries(unittest.TestCase):
 
106
    """ handling of dictionaries """
 
107
 
 
108
    def testSortedKeysForDicts(self):
 
109
        """ the keys of a dictionary must be sorted before encoded. """
 
110
 
 
111
        dict_ = {b'zoo': 42, b'bar': b'spam'}
 
112
        encoded_dict = bencode(dict_)
 
113
        self.failUnless(
 
114
            encoded_dict.index(b'zoo') > encoded_dict.index(b'bar'))
 
115
 
 
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}}
 
119
 
 
120
        encoded_dict = bencode(dict_)
 
121
        self.assertEqual(encoded_dict,
 
122
            b"d3:bard6:foobari23e6:sketch6:parrote3:fooi42ee")
 
123
 
 
124
 
 
125
if __name__ == "__main__":
 
126
    unittest.main()