123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- from pytest import fixture, mark
- import app.rest.hash_util as hash_util
- import base32_lib as b32
- from app.rest.hash_util import(
- hash_to_hex,
- hash_to_bytes,
- bytes_to_base32,
- bytes_to_base64,
- bytes_to_base85,
- bytes_to_hash,
- bytes_to_hex,
- normalize_bytes,
- normalize_hash,
- normalize_hex,
- base85_to_hash,
- base85_to_bytes,
- base85_to_hex,
- base32_to_bytes,
- base32_to_hash,
- base32_to_hex,
- base64_to_bytes,
- base64_to_hash,
- base64_to_hex,
- hash_to_base32,
- hash_to_base85,
- hash_to_base64,
- hex_to_base32,
- hex_to_base85,
- hex_to_base64,
- hex_to_bytes,
- hex_to_hash,
- )
- def to_unsigned(_hash: int) -> int:
- return _hash & hash_util.DIGEST_SIZE_BITMASK
- def digest_size():
- for g in ( two_bytes, three_bytes, four_bytes):
- yield g
- def two_bytes():
- hash_util.DIGEST_SIZE_BYTES = 2
- hash_util.DIGEST_SIZE_NIBBLES = 4
- hash_util.DIGEST_SIZE_BITMASK = 0xffff
- hash_util.DIGEST_SIZE_SIGNED_TO_UNSIGNED_BIT = 0x10000
- hash_util.DIGEST_SIZE_SIGNED_TO_UNSIGNED_BITMASK = 0x1ffff
- return hash_util.DIGEST_SIZE_BYTES
- def three_bytes():
- hash_util.DIGEST_SIZE_BYTES = 3
- hash_util.DIGEST_SIZE_NIBBLES = 6
- hash_util.DIGEST_SIZE_BITMASK = 0xffffff
- hash_util.DIGEST_SIZE_SIGNED_TO_UNSIGNED_BIT = 0x1000000
- hash_util.DIGEST_SIZE_SIGNED_TO_UNSIGNED_BITMASK = 0x1ffffff
- return hash_util.DIGEST_SIZE_BYTES
- def four_bytes():
- hash_util.DIGEST_SIZE_BYTES = 4
- hash_util.DIGEST_SIZE_NIBBLES = 8
- hash_util.DIGEST_SIZE_BITMASK = 0xffffffff
- hash_util.DIGEST_SIZE_SIGNED_TO_UNSIGNED_BIT = 0x100000000
- hash_util.DIGEST_SIZE_SIGNED_TO_UNSIGNED_BITMASK = 0x1ffffffff
- return hash_util.DIGEST_SIZE_BYTES
- @fixture
- def two_byte_bitmask():
- return 0xffff
- def hash_codes():
- for _hash in [
- -7583489610679606711,
- 1421958803217889556,
- 10,
- ]:
- _bytes = b'\00' * 8 + _hash.to_bytes(8, byteorder='big', signed=True)[-8:]
- _hex = _bytes.hex()
- print(_hash, _hex, _bytes)
- yield _hash, _hex, _bytes
- def digest_size_bitmask(digest_size_bits):
- mask = 0x0
- for _ in range(digest_size_bits):
- mask = mask << 1 | 0x1
- return mask
- def digest_size_force_unsigned_bitmask(digest_size):
- mask = 0x1
- for _ in range(8*digest_size):
- mask = mask << 1 | 0x1
- return mask
- def test_three_byte_assertions():
- digest_size = three_bytes()
- assert hash_to_base64(-7583489610679606711) == 'k6JJ'
- assert bytes_to_base64(b'\x96\xc2\x08`\xcd\x93\xa2I') == 'k6JJ'
- assert hex_to_base64('96c20860cd93a249') == 'k6JJ'
- assert base64_to_hex('k6JJ') == '93a249'
- assert base64_to_bytes('aa') == b'\x00\x00i'
- assert base64_to_hex('aa') == '000069'
- assert base64_to_hash('aa') == 105
- @mark.parametrize('_hash, _hex', [
- (_hash, _hex) for _hash, _hex, *_ in hash_codes()
- ])
- @mark.parametrize('digest_size_gen', digest_size())
- def test_hash_to_hex(_hash, _hex, digest_size_gen):
- digest_size = digest_size_gen()
- assert hash_to_hex(_hash) == _hex[-digest_size*2:]
- for i in range(1, digest_size*2+1):
-
- assert hash_to_hex(_hash & digest_size_bitmask(i*4)) == _hex[-i:].zfill(digest_size*2)
- @mark.parametrize('_hash, _bytes', [
- (_hash, _bytes) for _hash, _, _bytes, *_ in hash_codes()
- ])
- @mark.parametrize('digest_size_gen', digest_size())
- def test_hash_to_base32(_hash, _bytes, digest_size_gen):
- digest_size = digest_size_gen()
- norm = hash_to_base32(_hash)
- assert norm == b32.encode(
- to_unsigned(int.from_bytes(
- _bytes[-digest_size:], byteorder='big', signed=False)
- )
- ).upper().zfill(digest_size*8//5+1)
- assert len(norm) == digest_size*8//5 +1
- for i in range(1, digest_size+1):
- norm = hash_to_base32(_hash & digest_size_bitmask(i*8))
-
- assert norm == b32.encode(
- to_unsigned(int.from_bytes(
- (b'\x00'*digest_size + _bytes[-i:])[-digest_size:], byteorder='big', signed=False)
- )
- ).upper().zfill(digest_size*8//5+1)
- assert len(norm) == digest_size*8//5 +1
- @mark.parametrize('_hash, _hex', [
- (_hash, _hex) for _hash, _hex, *_ in hash_codes()
- ])
- @mark.parametrize('digest_size_gen', digest_size())
- def test_hex_to_hash(_hash, _hex, digest_size_gen):
- digest_size = digest_size_gen()
- assert hex_to_hash(_hex) == _hash & digest_size_bitmask(digest_size*8)
- for i in range(1, digest_size*2+1):
-
- _input = _hex[-i:]
- assert digest_size*2 == i or len(_input) < digest_size*2
- assert hex_to_hash(_input) == _hash & digest_size_bitmask(i*4)
-
- _input = _hex[-i:].zfill(digest_size*2)
- assert len(_input) == digest_size*2
- assert hex_to_hash(_input) == _hash & digest_size_bitmask(i*4)
- @mark.parametrize('_hex, _bytes', [
- (_hex, _bytes) for _, _hex, _bytes, *_ in hash_codes()
- ])
- @mark.parametrize('digest_size_gen', digest_size())
- def test_hex_to_bytes(_hex, _bytes, digest_size_gen):
- digest_size = digest_size_gen()
- assert hex_to_bytes(_hex) == _bytes[-digest_size:]
- for i in range(1, digest_size*2+1):
-
- _input = _hex[-i:]
- assert digest_size*2 == i or len(_input) < digest_size*2
- if i % 2:
- if i > 2:
- assert hex_to_bytes(_input) == (b'\x00'*digest_size + bytes([
- _bytes[-i//2] & 0x0f, *_bytes[-i//2+1:]
- ]))[-digest_size:]
- else:
- assert hex_to_bytes(_input) == (b'\x00'*digest_size + bytes([
- _bytes[-i//2] & 0x0f
- ]))[-digest_size:]
- else:
- assert hex_to_bytes(_input) == (b'\x00'*digest_size + _bytes[-i//2:])[-digest_size:]
-
- _input = _hex[-i:].zfill(digest_size*2)
- assert len(_input) == digest_size*2
- if i % 2:
- if i > 2:
- assert hex_to_bytes(_input) == (b'\x00'*digest_size + bytes([
- _bytes[-i//2] & 0x0f, *_bytes[-i//2+1:]
- ]))[-digest_size:]
- else:
- assert hex_to_bytes(_input) == (b'\x00'*digest_size + bytes([
- _bytes[-i//2] & 0x0f
- ]))[-digest_size:]
- else:
- assert hex_to_bytes(_input) == (b'\x00'*digest_size + _bytes[-i//2:])[-digest_size:]
- @mark.parametrize('_hash, _bytes', [
- (_hash, _bytes) for _hash, _, _bytes, *_ in hash_codes()
- ])
- @mark.parametrize('digest_size_gen', digest_size())
- def test_bytes_to_hash(_hash, _bytes, digest_size_gen):
- digest_size = digest_size_gen()
- assert bytes_to_hash(_bytes) == _hash & digest_size_bitmask(digest_size*8)
- for i in range(1, digest_size+1):
-
- _input = _bytes[-i:]
- assert digest_size == i or len(_input) < digest_size
- assert bytes_to_hash(_input) == _hash & digest_size_bitmask(i*8)
-
- _input = (b'\x00'*digest_size + _bytes[-i:])[-digest_size:]
- assert len(_input) == digest_size
- assert bytes_to_hash(_input) == _hash & digest_size_bitmask(i*8)
- @mark.parametrize('_hash, _bytes', [
- (_hash, _bytes) for _hash, _, _bytes, *_ in hash_codes()
- ])
- @mark.parametrize('digest_size_gen', digest_size())
- def test_base32_to_hash(_hash, _bytes, digest_size_gen):
- digest_size = digest_size_gen()
- _base32 = b32.encode(
- to_unsigned(int.from_bytes(
- _bytes[-digest_size:], byteorder='big', signed=False)
- )
- ).upper().zfill(digest_size*8//5+1)
- norm = base32_to_hash(_base32)
- assert norm == _hash & digest_size_bitmask(digest_size*8)
- for i in range(1, digest_size+1):
-
- _base32 = b32.encode(
- to_unsigned(int.from_bytes(
- (b'\x00'*digest_size + _bytes[-i:])[-digest_size:], byteorder='big', signed=False)
- )
- ).upper()
- norm = base32_to_hash(_base32)
- assert norm == _hash & digest_size_bitmask(i*8)
-
- _base32 = b32.encode(
- to_unsigned(int.from_bytes(
- (b'\x00'*digest_size + _bytes[-i:])[-digest_size:], byteorder='big', signed=False)
- )
- ).upper().zfill(digest_size*8//5+1)
- norm = base32_to_hash(_base32)
- assert norm == _hash & digest_size_bitmask(i*8)
- @mark.parametrize('_hex', [
- _hex for _, _hex, *_ in hash_codes()
- ])
- @mark.parametrize('digest_size_gen', digest_size())
- def test_normalize_hex(_hex, digest_size_gen):
- digest_size = digest_size_gen()
- assert normalize_hex(_hex) == _hex[-digest_size*2:]
- assert len(normalize_hex(_hex)) == digest_size*2
-
- for i in range(1, digest_size*2+1):
- assert i == digest_size*2 or _hex[-i:] != _hex[-i:].zfill(digest_size*2)
- assert normalize_hex(_hex[-i:]) == _hex[-i:].zfill(digest_size*2)
- assert len(normalize_hex(_hex[-i:])) == digest_size*2
- @mark.parametrize('_bytes', [
- _bytes for _, _, _bytes, *_ in hash_codes()
- ])
- @mark.parametrize('digest_size_gen', digest_size())
- def test_normalize_bytes(_bytes, digest_size_gen):
- digest_size = digest_size_gen()
- assert normalize_bytes(_bytes) == _bytes[-digest_size:]
- assert len(normalize_bytes(_bytes)) == digest_size
-
-
- for i in range(1, digest_size+1):
- assert i == digest_size or _bytes[-i:] != (b'\x00'*digest_size + _bytes[-i:])[-digest_size:]
- assert normalize_bytes(_bytes[-i:]) == (b'\x00'*digest_size + _bytes[-i:])[-digest_size:]
- assert len(normalize_bytes(_bytes[-i:])) == digest_size
- @mark.parametrize('_hash', [
- _hash for _hash, *_ in hash_codes()
- ])
- @mark.parametrize('digest_size_gen', digest_size())
- def test_normalize_hash(_hash, digest_size_gen):
- digest_size = digest_size_gen()
- norm = normalize_hash(_hash)
- assert norm == _hash & digest_size_bitmask(digest_size*8)
-
-
- for i in range(1, digest_size+1):
- norm = normalize_hash(_hash & digest_size_bitmask(i*8))
- assert norm == _hash & digest_size_bitmask(i*8)
- assert norm == norm & digest_size_bitmask(i*8)
|