|
@@ -3,13 +3,7 @@
|
|
# All rights reserved
|
|
# All rights reserved
|
|
#
|
|
#
|
|
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
|
|
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
|
|
-# from app.rest.hash_util import(
|
|
|
|
-# DIGEST_SIZE_SIGNED_TO_UNSIGNED_BIT,
|
|
|
|
-# DIGEST_SIZE_BITMASK,
|
|
|
|
-# DIGEST_SIZE_BYTES,
|
|
|
|
-# DIGEST_SIZE_NIBBLES,
|
|
|
|
-# DIGEST_SIZE_SIGNED_TO_UNSIGNED_BITMASK,
|
|
|
|
-# )
|
|
|
|
|
|
+from pytest import fixture, mark
|
|
import app.rest.hash_util as hash_util
|
|
import app.rest.hash_util as hash_util
|
|
from app.rest.hash_util import(
|
|
from app.rest.hash_util import(
|
|
hash_to_hex,
|
|
hash_to_hex,
|
|
@@ -41,107 +35,66 @@ from app.rest.hash_util import(
|
|
hex_to_hash,
|
|
hex_to_hash,
|
|
)
|
|
)
|
|
|
|
|
|
-# PYTHONHASHSEED=0
|
|
|
|
-def test_four_byte_assertions():
|
|
|
|
- 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
|
|
|
|
- assert hash_to_hex(41545) == '0000a249', hash_to_hex(41545)
|
|
|
|
- assert hash_to_hex(-7583489610679606711) == 'cd93a249', hash_to_hex(-7583489610679606711)
|
|
|
|
- assert hash_to_hex(1421958803217889556) == normalize_hex('13bbcfff670ab914'), hash_to_hex(1421958803217889556)
|
|
|
|
- #assert hash('a').to_bytes(8,'big', signed=True) == b'\x96\xc2\x08`\xcd\x93\xa2I'
|
|
|
|
- assert normalize_bytes(b'\x96\xc2\x08`\xcd\x93\xa2I') == b'\xcd\x93\xa2I', normalize_bytes(b'\x96\xc2\x08`\xcd\x93\xa2I')
|
|
|
|
- assert normalize_bytes(b'\xa2I') == b'\x00\x00\xa2I'
|
|
|
|
- assert normalize_bytes(b'\x0a') == b'\x00\x00\x00\x0a'
|
|
|
|
-
|
|
|
|
- #assert hash('a') == -7583489610679606711
|
|
|
|
- assert normalize_hash(-7583489610679606711) == 3449004617, normalize_hash(-7583489610679606711)
|
|
|
|
- assert normalize_hash(41545) == 41545, normalize_hash(41545)
|
|
|
|
|
|
|
|
- #assert hash('a').to_bytes(8,'big', signed=True).hex() == '96c20860cd93a249'
|
|
|
|
- assert normalize_hex('96c20860cd93a249') == 'cd93a249', normalize_hex('96c20860cd93a249')
|
|
|
|
- assert normalize_hex('a249') == '0000a249', normalize_hex('a249')
|
|
|
|
-
|
|
|
|
- assert normalize_hex('a') == '0000000a', normalize_hex('a')
|
|
|
|
- assert normalize_hex('0a') == '0000000a', normalize_hex('0a')
|
|
|
|
- assert normalize_hex('00a') == '0000000a', normalize_hex('00a')
|
|
|
|
-
|
|
|
|
- assert hex_to_hash('a') == 10
|
|
|
|
- assert hex_to_hash('0a') == 10
|
|
|
|
- assert hex_to_hash('00a') == 10
|
|
|
|
- assert hex_to_hash('000a') == 10
|
|
|
|
- assert hex_to_hash('a249') == 41545
|
|
|
|
- assert hex_to_hash('96c20860cd93a249') == 3449004617, hex_to_hash('96c20860cd93a249')
|
|
|
|
-
|
|
|
|
- assert bytes_to_hash(b'\n') == 10
|
|
|
|
- assert bytes_to_hash(b'\x00\n') == 10
|
|
|
|
- assert bytes_to_hash(b'\xa2I') == 41545
|
|
|
|
- assert bytes_to_hash(b'\x96\xc2\x08`\xcd\x93\xa2I') == 3449004617, bytes_to_hash(b'\x96\xc2\x08`\xcd\x93\xa2I')
|
|
|
|
-
|
|
|
|
- assert hex_to_bytes('a') == b'\x00\x00\x00\n'
|
|
|
|
- assert hex_to_bytes('0a') == b'\x00\x00\x00\n'
|
|
|
|
- assert hex_to_bytes('00a') == b'\x00\x00\x00\n'
|
|
|
|
- assert hex_to_bytes('000a') == b'\x00\x00\x00\n'
|
|
|
|
- assert hex_to_bytes('a249') == b'\x00\x00\xa2I'
|
|
|
|
- assert hex_to_bytes('96c20860cd93a249') == b'\xcd\x93\xa2I'
|
|
|
|
-
|
|
|
|
- assert hash_to_hex(1421958803217889556) == normalize_hex('13bbcfff670ab914'), hash_to_hex(1421958803217889556)
|
|
|
|
- assert hash_to_hex(10) == '0000000a', hash_to_hex(10)
|
|
|
|
- assert hash_to_hex(41545) == '0000a249', hash_to_hex(41545)
|
|
|
|
- assert hash_to_hex(-7583489610679606711) == 'cd93a249', hash_to_hex(-7583489610679606711)
|
|
|
|
|
|
+def digest_size():
|
|
|
|
+ for g in ( two_bytes, three_bytes, four_bytes):
|
|
|
|
+ yield g
|
|
|
|
|
|
-def test_three_byte_assertions():
|
|
|
|
|
|
+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_BYTES = 3
|
|
hash_util.DIGEST_SIZE_NIBBLES = 6
|
|
hash_util.DIGEST_SIZE_NIBBLES = 6
|
|
hash_util.DIGEST_SIZE_BITMASK = 0xffffff
|
|
hash_util.DIGEST_SIZE_BITMASK = 0xffffff
|
|
hash_util.DIGEST_SIZE_SIGNED_TO_UNSIGNED_BIT = 0x1000000
|
|
hash_util.DIGEST_SIZE_SIGNED_TO_UNSIGNED_BIT = 0x1000000
|
|
hash_util.DIGEST_SIZE_SIGNED_TO_UNSIGNED_BITMASK = 0x1ffffff
|
|
hash_util.DIGEST_SIZE_SIGNED_TO_UNSIGNED_BITMASK = 0x1ffffff
|
|
- #assert hash_to_hex(10) == '000a', ":" + hash_to_hex(10)
|
|
|
|
- assert hash_to_hex(41545) == '00a249', hash_to_hex(41545)
|
|
|
|
- assert hash_to_hex(-7583489610679606711) == '93a249', hash_to_hex(-7583489610679606711)
|
|
|
|
- assert hash_to_hex(1421958803217889556) == normalize_hex('13bbcfff670ab914'), hash_to_hex(1421958803217889556)
|
|
|
|
- #assert hash('a').to_bytes(8,'big', signed=True) == b'\x96\xc2\x08`\xcd\x93\xa2I'
|
|
|
|
- assert normalize_bytes(b'\x96\xc2\x08`\xcd\x93\xa2I') == b'\x93\xa2I', normalize_bytes(b'\x96\xc2\x08`\xcd\x93\xa2I')
|
|
|
|
- assert normalize_bytes(b'\xa2I') == b'\x00\xa2I'
|
|
|
|
- assert normalize_bytes(b'\x0a') == b'\x00\x00\x0a'
|
|
|
|
-
|
|
|
|
- #assert hash('a') == -7583489610679606711
|
|
|
|
- assert normalize_hash(-7583489610679606711) == 9675337, normalize_hash(-7583489610679606711)
|
|
|
|
- assert normalize_hash(41545) == 41545, normalize_hash(41545)
|
|
|
|
|
|
+ return hash_util.DIGEST_SIZE_BYTES
|
|
|
|
|
|
- #assert hash('a').to_bytes(8,'big', signed=True).hex() == '96c20860cd93a249'
|
|
|
|
- assert normalize_hex('96c20860cd93a249') == '93a249', normalize_hex('96c20860cd93a249')
|
|
|
|
- assert normalize_hex('a249') == '00a249', normalize_hex('a249')
|
|
|
|
-
|
|
|
|
- assert normalize_hex('a') == '00000a', normalize_hex('a')
|
|
|
|
- assert normalize_hex('0a') == '00000a', normalize_hex('0a')
|
|
|
|
- assert normalize_hex('00a') == '00000a', normalize_hex('00a')
|
|
|
|
-
|
|
|
|
- assert hex_to_hash('a') == 10
|
|
|
|
- assert hex_to_hash('0a') == 10
|
|
|
|
- assert hex_to_hash('00a') == 10
|
|
|
|
- assert hex_to_hash('000a') == 10
|
|
|
|
- assert hex_to_hash('a249') == 41545
|
|
|
|
- assert hex_to_hash('96c20860cd93a249') == 9675337
|
|
|
|
-
|
|
|
|
- assert bytes_to_hash(b'\n') == 10
|
|
|
|
- assert bytes_to_hash(b'\x00\n') == 10
|
|
|
|
- assert bytes_to_hash(b'\xa2I') == 41545
|
|
|
|
- assert bytes_to_hash(b'\x96\xc2\x08`\xcd\x93\xa2I') == 9675337
|
|
|
|
-
|
|
|
|
- assert hex_to_bytes('a') == b'\x00\x00\n'
|
|
|
|
- assert hex_to_bytes('0a') == b'\x00\x00\n'
|
|
|
|
- assert hex_to_bytes('00a') == b'\x00\x00\n'
|
|
|
|
- assert hex_to_bytes('000a') == b'\x00\x00\n'
|
|
|
|
- assert hex_to_bytes('a249') == b'\x00\xa2I'
|
|
|
|
- assert hex_to_bytes('96c20860cd93a249') == b'\x93\xa2I'
|
|
|
|
-
|
|
|
|
- assert hash_to_hex(1421958803217889556) == normalize_hex('13bbcfff670ab914'), hash_to_hex(1421958803217889556)
|
|
|
|
- assert hash_to_hex(10) == '00000a', hash_to_hex(10)
|
|
|
|
- assert hash_to_hex(41545) == '00a249', hash_to_hex(41545)
|
|
|
|
- assert hash_to_hex(-7583489610679606711) == '93a249', hash_to_hex(-7583489610679606711)
|
|
|
|
|
|
+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
|
|
|
|
+
|
|
|
|
+# PYTHONHASHSEED=0
|
|
|
|
+
|
|
|
|
+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 hash_to_base64(-7583489610679606711) == 'k6JJ'
|
|
assert bytes_to_base64(b'\x96\xc2\x08`\xcd\x93\xa2I') == 'k6JJ'
|
|
assert bytes_to_base64(b'\x96\xc2\x08`\xcd\x93\xa2I') == 'k6JJ'
|
|
@@ -151,53 +104,133 @@ def test_three_byte_assertions():
|
|
assert base64_to_hex('aa') == '000069'
|
|
assert base64_to_hex('aa') == '000069'
|
|
assert base64_to_hash('aa') == 105
|
|
assert base64_to_hash('aa') == 105
|
|
|
|
|
|
-def test_two_byte_assertions():
|
|
|
|
- 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
|
|
|
|
- #assert hash_to_hex(10) == '000a', ":" + hash_to_hex(10)
|
|
|
|
- assert hash_to_hex(41545) == 'a249', hash_to_hex(41545)
|
|
|
|
- assert hash_to_hex(-7583489610679606711) == 'a249', hash_to_hex(-7583489610679606711)
|
|
|
|
- assert hash_to_hex(1421958803217889556) == normalize_hex('13bbcfff670ab914'), hash_to_hex(1421958803217889556)
|
|
|
|
- #assert hash('a').to_bytes(8,'big', signed=True) == b'\x96\xc2\x08`\xcd\x93\xa2I'
|
|
|
|
- assert normalize_bytes(b'\x96\xc2\x08`\xcd\x93\xa2I') == b'\xa2I', normalize_bytes(b'\x96\xc2\x08`\xcd\x93\xa2I')
|
|
|
|
- assert normalize_bytes(b'\xa2I') == b'\xa2I'
|
|
|
|
- assert normalize_bytes(b'\x0a') == b'\x00\x0a'
|
|
|
|
-
|
|
|
|
- #assert hash('a') == -7583489610679606711
|
|
|
|
- assert normalize_hash(-7583489610679606711) == 41545, normalize_hash(-7583489610679606711)
|
|
|
|
- assert normalize_hash(41545) == 41545, normalize_hash(41545)
|
|
|
|
-
|
|
|
|
- #assert hash('a').to_bytes(8,'big', signed=True).hex() == '96c20860cd93a249'
|
|
|
|
- assert normalize_hex('96c20860cd93a249') == 'a249', normalize_hex('96c20860cd93a249')
|
|
|
|
- assert normalize_hex('a249') == 'a249', normalize_hex('a249')
|
|
|
|
-
|
|
|
|
- assert normalize_hex('a') == '000a', normalize_hex('a')
|
|
|
|
- assert normalize_hex('0a') == '000a', normalize_hex('0a')
|
|
|
|
- assert normalize_hex('00a') == '000a', normalize_hex('00a')
|
|
|
|
-
|
|
|
|
- assert hex_to_hash('a') == 10
|
|
|
|
- assert hex_to_hash('0a') == 10
|
|
|
|
- assert hex_to_hash('00a') == 10
|
|
|
|
- assert hex_to_hash('000a') == 10
|
|
|
|
- assert hex_to_hash('a249') == 41545
|
|
|
|
- assert hex_to_hash('96c20860cd93a249') == 41545
|
|
|
|
-
|
|
|
|
- assert bytes_to_hash(b'\n') == 10
|
|
|
|
- assert bytes_to_hash(b'\x00\n') == 10
|
|
|
|
- assert bytes_to_hash(b'\xa2I') == 41545
|
|
|
|
- assert bytes_to_hash(b'\x96\xc2\x08`\xcd\x93\xa2I') == 41545
|
|
|
|
-
|
|
|
|
- assert hex_to_bytes('a') == b'\x00\n'
|
|
|
|
- assert hex_to_bytes('0a') == b'\x00\n'
|
|
|
|
- assert hex_to_bytes('00a') == b'\x00\n'
|
|
|
|
- assert hex_to_bytes('000a') == b'\x00\n'
|
|
|
|
- assert hex_to_bytes('a249') == b'\xa2I'
|
|
|
|
- assert hex_to_bytes('96c20860cd93a249') == b'\xa2I'
|
|
|
|
-
|
|
|
|
- assert hash_to_hex(1421958803217889556) == normalize_hex('13bbcfff670ab914'), hash_to_hex(1421958803217889556)
|
|
|
|
- assert hash_to_hex(10) == '000a', hash_to_hex(10)
|
|
|
|
- assert hash_to_hex(41545) == 'a249', hash_to_hex(41545)
|
|
|
|
- assert hash_to_hex(-7583489610679606711) == 'a249', hash_to_hex(-7583489610679606711)
|
|
|
|
|
|
+
|
|
|
|
+@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):
|
|
|
|
+ # test hex is zero padded
|
|
|
|
+ assert hash_to_hex(_hash & digest_size_bitmask(i*4)) == _hex[-i:].zfill(digest_size*2)
|
|
|
|
+
|
|
|
|
+@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):
|
|
|
|
+ # test non-zero padded hex
|
|
|
|
+ _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)
|
|
|
|
+
|
|
|
|
+ # test zero padded hex
|
|
|
|
+ _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):
|
|
|
|
+ # test non-zero padded hex
|
|
|
|
+ _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:]
|
|
|
|
+
|
|
|
|
+ # test zero padded hex
|
|
|
|
+ _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):
|
|
|
|
+ # test non-zero padded bytes
|
|
|
|
+ _input = _bytes[-i:]
|
|
|
|
+ assert digest_size == i or len(_input) < digest_size
|
|
|
|
+ assert bytes_to_hash(_input) == _hash & digest_size_bitmask(i*8)
|
|
|
|
+
|
|
|
|
+ # test zero padded bytes
|
|
|
|
+ _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('_hex', [
|
|
|
|
+ _hex for _, _hex, _bytes 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
|
|
|
|
+
|
|
|
|
+ # test commutivity
|
|
|
|
+ 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
|
|
|
|
+
|
|
|
|
+ # test commutivity
|
|
|
|
+ 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)
|
|
|
|
+
|
|
|
|
+ # test commutivity
|
|
|
|
+ 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)
|