2020-07-14 14:34:53 +00:00
|
|
|
"""
|
|
|
|
Tests for allmydata.util.base62.
|
|
|
|
|
|
|
|
Ported to Python 3.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from future.utils import PY2
|
|
|
|
if PY2:
|
|
|
|
from builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, int, list, object, range, str, max, min # noqa: F401
|
2008-02-13 03:48:37 +00:00
|
|
|
|
2020-07-13 20:21:15 +00:00
|
|
|
from past.builtins import chr as byteschr
|
|
|
|
|
2020-07-14 14:34:53 +00:00
|
|
|
import random, unittest
|
|
|
|
|
2020-07-13 20:19:48 +00:00
|
|
|
from hypothesis import (
|
|
|
|
strategies as st,
|
|
|
|
given,
|
|
|
|
)
|
|
|
|
|
2008-02-13 03:48:37 +00:00
|
|
|
from allmydata.util import base62, mathutil
|
|
|
|
|
|
|
|
def insecurerandstr(n):
|
2020-07-14 14:51:31 +00:00
|
|
|
return bytes(list(map(random.randrange, [0]*n, [256]*n)))
|
2008-02-13 03:48:37 +00:00
|
|
|
|
|
|
|
class T(unittest.TestCase):
|
|
|
|
def _test_num_octets_that_encode_to_this_many_chars(self, chars, octets):
|
|
|
|
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)
|
|
|
|
|
2020-07-14 17:44:00 +00:00
|
|
|
def _test_roundtrip(self, bs):
|
2020-07-14 17:44:56 +00:00
|
|
|
encoded = base62.b2a(bs)
|
|
|
|
decoded = base62.a2b(encoded)
|
|
|
|
self.assertEqual(decoded, bs)
|
|
|
|
self.assertIsInstance(encoded, bytes)
|
2020-07-14 14:28:22 +00:00
|
|
|
self.assertIsInstance(bs, bytes)
|
2020-07-14 17:44:56 +00:00
|
|
|
self.assertIsInstance(decoded, bytes)
|
2008-02-13 03:48:37 +00:00
|
|
|
|
2020-07-13 20:19:48 +00:00
|
|
|
@given(input_bytes=st.binary(max_size=100))
|
|
|
|
def test_roundtrip(self, input_bytes):
|
2020-07-14 17:44:00 +00:00
|
|
|
self._test_roundtrip(input_bytes)
|
2020-07-13 20:19:48 +00:00
|
|
|
|
2008-02-13 03:48:37 +00:00
|
|
|
def test_num_octets_that_encode_to_this_many_chars(self):
|
|
|
|
return self._test_num_octets_that_encode_to_this_many_chars(2, 1)
|
|
|
|
return self._test_num_octets_that_encode_to_this_many_chars(3, 2)
|
|
|
|
return self._test_num_octets_that_encode_to_this_many_chars(5, 3)
|
|
|
|
return self._test_num_octets_that_encode_to_this_many_chars(6, 4)
|
|
|
|
|
|
|
|
def test_ende_0x00(self):
|
2020-07-14 17:44:00 +00:00
|
|
|
return self._test_roundtrip(b'\x00')
|
2008-02-13 03:48:37 +00:00
|
|
|
|
|
|
|
def test_ende_0x01(self):
|
2020-07-14 17:44:00 +00:00
|
|
|
return self._test_roundtrip(b'\x01')
|
2008-02-13 03:48:37 +00:00
|
|
|
|
|
|
|
def test_ende_0x0100(self):
|
2020-07-14 17:44:00 +00:00
|
|
|
return self._test_roundtrip(b'\x01\x00')
|
2008-02-13 03:48:37 +00:00
|
|
|
|
|
|
|
def test_ende_0x000000(self):
|
2020-07-14 17:44:00 +00:00
|
|
|
return self._test_roundtrip(b'\x00\x00\x00')
|
2008-02-13 03:48:37 +00:00
|
|
|
|
|
|
|
def test_ende_0x010000(self):
|
2020-07-14 17:44:00 +00:00
|
|
|
return self._test_roundtrip(b'\x01\x00\x00')
|
2008-02-13 03:48:37 +00:00
|
|
|
|
|
|
|
def test_ende_randstr(self):
|
2020-07-14 17:44:00 +00:00
|
|
|
return self._test_roundtrip(insecurerandstr(2**4))
|
2008-02-13 03:48:37 +00:00
|
|
|
|
|
|
|
def test_ende_longrandstr(self):
|
2020-07-14 17:44:00 +00:00
|
|
|
return self._test_roundtrip(insecurerandstr(random.randrange(0, 2**10)))
|
2008-02-13 03:48:37 +00:00
|
|
|
|
|
|
|
def test_odd_sizes(self):
|
|
|
|
for j in range(2**6):
|
|
|
|
lib = random.randrange(1, 2**8)
|
|
|
|
numos = mathutil.div_ceil(lib, 8)
|
|
|
|
bs = insecurerandstr(numos)
|
|
|
|
# zero-out unused least-sig bits
|
|
|
|
if lib%8:
|
2020-07-14 14:51:31 +00:00
|
|
|
b = ord(bs[-1:])
|
2008-02-13 03:48:37 +00:00
|
|
|
b = b >> (8 - (lib%8))
|
|
|
|
b = b << (8 - (lib%8))
|
2020-07-14 14:34:53 +00:00
|
|
|
bs = bs[:-1] + byteschr(b)
|
2008-02-13 03:48:37 +00:00
|
|
|
asl = base62.b2a_l(bs, lib)
|
|
|
|
assert len(asl) == base62.num_chars_that_this_many_octets_encode_to(numos) # the size of the base-62 encoding must be just right
|
|
|
|
bs2l = base62.a2b_l(asl, lib)
|
|
|
|
assert len(bs2l) == numos # the size of the result must be just right
|
|
|
|
assert bs == bs2l
|
|
|
|
|
|
|
|
def suite():
|
2008-02-13 14:38:08 +00:00
|
|
|
suite = unittest.makeSuite(T, 'test')
|
2008-02-13 03:48:37 +00:00
|
|
|
return suite
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|