Explorar o código

add tests for hash_util and more hashes

Daniel Sheffield hai 1 ano
pai
achega
6da1dcff68
Modificáronse 3 ficheiros con 369 adicións e 56 borrados
  1. 165 56
      app/rest/hash_util.py
  2. 1 0
      app/rest/requirements.txt
  3. 203 0
      test/rest/test_hash_util.py

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 165 - 56
app/rest/hash_util.py


+ 1 - 0
app/rest/requirements.txt

@@ -3,3 +3,4 @@ psycopg[binary]
 bottle
 wsgigzip
 cherrypy
+base32-lib

+ 203 - 0
test/rest/test_hash_util.py

@@ -0,0 +1,203 @@
+#
+# Copyright (c) Daniel Sheffield 2023
+# All rights reserved
+#
+# 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,
+# )
+import app.rest.hash_util as hash_util
+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,
+)
+
+# 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 test_three_byte_assertions():
+    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
+    #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)
+
+    #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)
+
+    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
+
+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)

Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio