Contrato identico al verificador de Node: contador = floor(unix_utc/45) codigo = HMAC-SHA256(SEMILLA, 'q1|'+contador)[0..15] base64url sin padding token = q1.<base36(contador)>.<codigo> Vector canonico verificado: q1.md56o.Jo2Q_p4tq_sTY9A9YGj-7A Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
"""
|
|
Test del vector canonico. Debe dar EXACTAMENTE el mismo token que el verificador de Node.
|
|
Ejecutar: python3 test_contrato.py
|
|
"""
|
|
|
|
import qr_contract as qc
|
|
|
|
SEMILLA_HEX = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
|
|
CONTADOR = 37564800
|
|
TOKEN_ESPERADO = "q1.md56o.Jo2Q_p4tq_sTY9A9YGj-7A"
|
|
|
|
|
|
def test_vector_canonico():
|
|
semilla = bytes.fromhex(SEMILLA_HEX)
|
|
token = qc.calcular_token(semilla, CONTADOR)
|
|
assert token == TOKEN_ESPERADO, "\n esperado: %s\n obtenido: %s" % (TOKEN_ESPERADO, token)
|
|
|
|
|
|
def test_base36():
|
|
assert qc.base36(0) == "0"
|
|
assert qc.base36(37564800) == "md56o"
|
|
assert qc.base36(35) == "z"
|
|
assert qc.base36(36) == "10"
|
|
|
|
|
|
def test_contador_utc():
|
|
# 37564800 * 45 = 1690416000 (epoch UTC exacto al inicio del periodo)
|
|
assert qc.contador_actual(1690416000) == 37564800
|
|
assert qc.contador_actual(1690416044) == 37564800 # aun mismo periodo
|
|
assert qc.contador_actual(1690416045) == 37564801 # siguiente periodo
|
|
|
|
|
|
if __name__ == "__main__":
|
|
for nombre, fn in sorted(globals().items()):
|
|
if nombre.startswith("test_") and callable(fn):
|
|
fn()
|
|
print("OK %s" % nombre)
|
|
print("\nToken canonico:", qc.calcular_token(bytes.fromhex(SEMILLA_HEX), CONTADOR))
|
|
print("Todo verde: el .exe dara el codigo identico a Node.")
|