]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/test/test_utils.py
fa9792ef67792d496e6c6e94463d310132dd335f
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / test / test_utils.py
1
2 def foo(): pass # keep the line number constant
3
4 from twisted.trial import unittest
5
6 from allmydata.util import bencode, idlib, humanreadable
7
8
9 class IDLib(unittest.TestCase):
10     def test_b2a(self):
11         self.failUnlessEqual(idlib.b2a("\x12\x34"), "ci2a====")
12     def test_b2a_or_none(self):
13         self.failUnlessEqual(idlib.b2a_or_none(None), None)
14         self.failUnlessEqual(idlib.b2a_or_none("\x12\x34"), "ci2a====")
15     def test_a2b(self):
16         self.failUnlessEqual(idlib.a2b("ci2a===="), "\x12\x34")
17     def test_peerid(self):
18         # these are 160-bit numbers
19         peerid = "\x80" + "\x00" * 19
20         short = idlib.peerid_to_short_string(peerid)
21         self.failUnlessEqual(short, "qaaa")
22
23 class NoArgumentException(Exception):
24     def __init__(self):
25         pass
26
27 class HumanReadable(unittest.TestCase):
28     def test_repr(self):
29         hr = humanreadable.hr
30         self.failUnlessEqual(hr(foo), "<foo() at test_utils.py:2>")
31         self.failUnlessEqual(hr(self.test_repr),
32                              "<bound method HumanReadable.test_repr of <allmydata.test.test_utils.HumanReadable testMethod=test_repr>>")
33         self.failUnlessEqual(hr(1L), "1")
34         self.failUnlessEqual(hr(10**40),
35                              "100000000000000000...000000000000000000")
36         self.failUnlessEqual(hr(self), "<allmydata.test.test_utils.HumanReadable testMethod=test_repr>")
37         self.failUnlessEqual(hr([1,2]), "[1, 2]")
38         self.failUnlessEqual(hr({1:2}), "{1:2}")
39         try:
40             raise RuntimeError
41         except Exception, e:
42             self.failUnlessEqual(hr(e), "<RuntimeError: ()>")
43         try:
44             raise RuntimeError("oops")
45         except Exception, e:
46             self.failUnlessEqual(hr(e), "<RuntimeError: 'oops'>")
47         try:
48             raise NoArgumentException
49         except Exception, e:
50             self.failUnlessEqual(hr(e), "<NoArgumentException>")
51
52
53 class MyList(list):
54     pass
55
56 class Bencode(unittest.TestCase):
57     def test_bencode(self):
58         e = bencode.bencode
59         self.failUnlessEqual(e(4), "i4e")
60         self.failUnlessEqual(e([1,2]), "li1ei2ee")
61         self.failUnlessEqual(e(MyList([1,2])), "li1ei2ee")
62         self.failUnlessEqual(e({1:2}), "di1ei2ee")
63         self.failUnlessEqual(e(u"a"), "u1:a")
64         self.failUnlessEqual(e([True,False]), "lb1b0e")
65         self.failUnlessEqual(e(1.5), "f1.5e")
66         self.failUnlessEqual(e("foo"), "3:foo")
67         d = bencode.bdecode
68         self.failUnlessEqual(d("li1ei2ee"), [1,2])
69         self.failUnlessEqual(d("u1:a"), u"a")
70         self.failUnlessRaises(ValueError, d, "u10:short")
71         self.failUnlessEqual(d("lb1b0e"), [True,False])
72         self.failUnlessRaises(ValueError, d, "b2")
73         self.failUnlessEqual(d("f1.5e"), 1.5)
74         self.failUnlessEqual(d("3:foo"), "foo")
75         self.failUnlessRaises(ValueError, d,
76                               "38:When doing layout, always plan ah")
77         # ooh! fascinating! bdecode requires string keys! I think this ought
78         # to be changed
79         #self.failUnlessEqual(d("di1ei2ee"), {1:2})
80         self.failUnlessEqual(d("d1:ai2eu1:bi3ee"), {"a":2, u"b":3})
81         self.failUnlessRaises(ValueError, d, "di1ei2ee")
82         self.failUnlessRaises(ValueError, d, "d1:ai1e1:ai2ee")
83
84         self.failUnlessRaises(ValueError, d, "i1ei2e")
85
86         # now run all the module's builtin tests
87         bencode.test_decode_raw_string()
88         bencode.test_encode_and_decode_unicode_results_in_unicode_type()
89         bencode.test_encode_and_decode_unicode_at_least_preserves_the_content_even_if_it_flattens_the_type()
90         bencode.test_dict_forbids_non_string_key()
91         bencode.test_dict_forbids_key_repeat()
92         bencode.test_empty_dict()
93         bencode.test_dict_allows_unicode_keys()
94         bencode.test_ValueError_in_decode_unknown()
95         bencode.test_encode_and_decode_none()
96         bencode.test_encode_and_decode_long()
97         bencode.test_encode_and_decode_int()
98         bencode.test_encode_and_decode_float()
99         bencode.test_encode_and_decode_bool()
100         #bencode.test_decode_noncanonical_int()
101         bencode.test_encode_and_decode_dict()
102         bencode.test_encode_and_decode_list()
103         bencode.test_encode_and_decode_tuple()
104         bencode.test_encode_and_decode_empty_dict()
105         bencode.test_encode_and_decode_complex_object()
106         bencode.test_unfinished_list()
107         bencode.test_unfinished_dict()
108         bencode.test_unsupported_type()
109