]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/test/test_base62.py
2882b5b3ab478235ea5368fcb8db6ad703d3b433
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / test / test_base62.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2002-2008 Bryce "Zooko" Wilcox-O'Hearn
4 # mailto:zooko@zooko.com
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this work to deal in this work without restriction (including the rights
7 # to use, modify, distribute, sublicense, and/or sell copies).
8
9 import random, unittest
10
11 from allmydata.util import base62, mathutil
12
13 def insecurerandstr(n):
14     return ''.join(map(chr, map(random.randrange, [0]*n, [256]*n)))
15
16 class T(unittest.TestCase):
17     def _test_num_octets_that_encode_to_this_many_chars(self, chars, octets):
18         assert base62.num_octets_that_encode_to_this_many_chars(chars) == octets, "%s != %s <- %s" % (octets, base62.num_octets_that_encode_to_this_many_chars(chars), chars)
19
20     def _test_ende(self, bs):
21         ascii=base62.b2a(bs)
22         bs2=base62.a2b(ascii)
23         assert bs2 == bs, "bs2: %s:%s, bs: %s:%s, ascii: %s:%s" % (len(bs2), `bs2`, len(bs), `bs`, len(ascii), `ascii`)
24
25     def test_num_octets_that_encode_to_this_many_chars(self):
26         return self._test_num_octets_that_encode_to_this_many_chars(2, 1)
27         return self._test_num_octets_that_encode_to_this_many_chars(3, 2)
28         return self._test_num_octets_that_encode_to_this_many_chars(5, 3)
29         return self._test_num_octets_that_encode_to_this_many_chars(6, 4)
30
31     def test_ende_0x00(self):
32         return self._test_ende('\x00')
33
34     def test_ende_0x01(self):
35         return self._test_ende('\x01')
36
37     def test_ende_0x0100(self):
38         return self._test_ende('\x01\x00')
39
40     def test_ende_0x000000(self):
41         return self._test_ende('\x00\x00\x00')
42
43     def test_ende_0x010000(self):
44         return self._test_ende('\x01\x00\x00')
45
46     def test_ende_randstr(self):
47         return self._test_ende(insecurerandstr(2**4))
48
49     def test_ende_longrandstr(self):
50         return self._test_ende(insecurerandstr(random.randrange(0, 2**10)))
51
52     def test_odd_sizes(self):
53         for j in range(2**6):
54             lib = random.randrange(1, 2**8)
55             numos = mathutil.div_ceil(lib, 8)
56             bs = insecurerandstr(numos)
57             # zero-out unused least-sig bits
58             if lib%8:
59                 b=ord(bs[-1])
60                 b = b >> (8 - (lib%8))
61                 b = b << (8 - (lib%8))
62                 bs = bs[:-1] + chr(b)
63             asl = base62.b2a_l(bs, lib)
64             assert len(asl) == base62.num_chars_that_this_many_octets_encode_to(numos) # the size of the base-62 encoding must be just right
65             bs2l = base62.a2b_l(asl, lib)
66             assert len(bs2l) == numos # the size of the result must be just right
67             assert bs == bs2l
68
69 def suite():
70     suite = unittest.makeSuite(T, 'test')
71     return suite
72
73 if __name__ == "__main__":
74     unittest.main()