source: trunk/src/allmydata/test/test_base62.py

Last change on this file was 1cfe843d, checked in by Alexandre Detiste <alexandre.detiste@…>, at 2024-02-22T23:40:25Z

more python2 removal

  • Property mode set to 100644
File size: 3.4 KB
Line 
1"""
2Tests for allmydata.util.base62.
3
4Ported to Python 3.
5"""
6
7from past.builtins import chr as byteschr
8
9import random, unittest
10
11from hypothesis import (
12    strategies as st,
13    given,
14)
15
16from allmydata.util import base62, mathutil
17
18def insecurerandstr(n):
19    return bytes(list(map(random.randrange, [0]*n, [256]*n)))
20
21class Base62(unittest.TestCase):
22    def _test_num_octets_that_encode_to_this_many_chars(self, chars, octets):
23        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)
24
25    def _test_roundtrip(self, bs):
26        encoded = base62.b2a(bs)
27        decoded = base62.a2b(encoded)
28        self.assertEqual(decoded, bs)
29        self.assertIsInstance(encoded, bytes)
30        self.assertIsInstance(bs, bytes)
31        self.assertIsInstance(decoded, bytes)
32        # Encoded string only uses values from the base62 allowed characters:
33        self.assertFalse(set(encoded) - set(base62.chars))
34
35    @given(input_bytes=st.binary(max_size=100))
36    def test_roundtrip(self, input_bytes):
37        self._test_roundtrip(input_bytes)
38
39    def test_known_values(self):
40        """Known values to ensure the algorithm hasn't changed."""
41
42        def check_expected(plaintext, encoded):
43            result1 = base62.b2a(plaintext)
44            self.assertEqual(encoded, result1)
45            result2 = base62.a2b(encoded)
46            self.assertEqual(plaintext, result2)
47
48        check_expected(b"hello", b'7tQLFHz')
49        check_expected(b"", b'0')
50        check_expected(b"zzz", b'0Xg7e')
51        check_expected(b"\x36\xffWAT", b'49pq4mq')
52        check_expected(b"1234 22323", b'1A0afZe9mxSZpz')
53        check_expected(b"______", b'0TmAuCHJX')
54
55    def test_num_octets_that_encode_to_this_many_chars(self):
56        return self._test_num_octets_that_encode_to_this_many_chars(2, 1)
57        return self._test_num_octets_that_encode_to_this_many_chars(3, 2)
58        return self._test_num_octets_that_encode_to_this_many_chars(5, 3)
59        return self._test_num_octets_that_encode_to_this_many_chars(6, 4)
60
61    def test_ende_0x00(self):
62        return self._test_roundtrip(b'\x00')
63
64    def test_ende_0x01(self):
65        return self._test_roundtrip(b'\x01')
66
67    def test_ende_0x0100(self):
68        return self._test_roundtrip(b'\x01\x00')
69
70    def test_ende_0x000000(self):
71        return self._test_roundtrip(b'\x00\x00\x00')
72
73    def test_ende_0x010000(self):
74        return self._test_roundtrip(b'\x01\x00\x00')
75
76    def test_ende_randstr(self):
77        return self._test_roundtrip(insecurerandstr(2**4))
78
79    def test_ende_longrandstr(self):
80        return self._test_roundtrip(insecurerandstr(random.randrange(0, 2**10)))
81
82    def test_odd_sizes(self):
83        for j in range(2**6):
84            lib = random.randrange(1, 2**8)
85            numos = mathutil.div_ceil(lib, 8)
86            bs = insecurerandstr(numos)
87            # zero-out unused least-sig bits
88            if lib%8:
89                b = ord(bs[-1:])
90                b = b >> (8 - (lib%8))
91                b = b << (8 - (lib%8))
92                bs = bs[:-1] + byteschr(b)
93            asl = base62.b2a_l(bs, lib)
94            assert len(asl) == base62.num_chars_that_this_many_octets_encode_to(numos) # the size of the base-62 encoding must be just right
95            bs2l = base62.a2b_l(asl, lib)
96            assert len(bs2l) == numos # the size of the result must be just right
97            assert bs == bs2l
Note: See TracBrowser for help on using the repository browser.