mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-17 22:24:23 +01:00
Update unit tests to use serialize
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import json
|
||||
import struct
|
||||
import binascii
|
||||
from pytest import fixture
|
||||
|
||||
from pisa import c_logger
|
||||
@@ -110,3 +112,27 @@ def test_from_dict(appointment_data):
|
||||
except ValueError:
|
||||
appointment_data[key] = prev_val
|
||||
assert True
|
||||
|
||||
|
||||
def test_serialize(appointment_data):
|
||||
# From the tower end, appointments are only created if they pass the inspector tests, so not covering weird formats.
|
||||
# Serialize may fail if, from the user end, the user tries to do it with an weird appointment. Not critical.
|
||||
|
||||
appointment = Appointment.from_dict(appointment_data)
|
||||
serialized_appointment = appointment.serialize()
|
||||
|
||||
# Size must be 16 + 4 + 4 + 4 + len(encrypted_blob)
|
||||
assert len(serialized_appointment) >= 28
|
||||
assert isinstance(serialized_appointment, bytes)
|
||||
|
||||
locator = serialized_appointment[:16]
|
||||
start_time = serialized_appointment[16:20]
|
||||
end_time = serialized_appointment[20:24]
|
||||
to_self_delay = serialized_appointment[24:28]
|
||||
encrypted_blob = serialized_appointment[28:]
|
||||
|
||||
assert binascii.hexlify(locator).decode() == appointment.locator
|
||||
assert struct.unpack(">I", start_time)[0] == appointment.start_time
|
||||
assert struct.unpack(">I", end_time)[0] == appointment.end_time
|
||||
assert struct.unpack(">I", to_self_delay)[0] == appointment.to_self_delay
|
||||
assert binascii.hexlify(encrypted_blob).decode() == appointment.encrypted_blob.data
|
||||
|
||||
@@ -211,7 +211,7 @@ def test_sign_wrong_rtype():
|
||||
# Calling sign with an rtype different than 'str' or 'bytes' should fail
|
||||
for wtype in WRONG_TYPES:
|
||||
try:
|
||||
Cryptographer.sign("", "", rtype=wtype)
|
||||
Cryptographer.sign(b"", "", rtype=wtype)
|
||||
assert False
|
||||
|
||||
except ValueError:
|
||||
@@ -221,20 +221,19 @@ def test_sign_wrong_rtype():
|
||||
def test_sign_wrong_sk():
|
||||
# If a sk is not passed, sign will return None
|
||||
for wtype in WRONG_TYPES:
|
||||
assert Cryptographer.sign("", wtype) is None
|
||||
assert Cryptographer.sign(b"", wtype) is None
|
||||
|
||||
|
||||
# FIXME: signature_format is not covered, so we are not covering cases where the message is not in the proper format
|
||||
# at the moment (related to #68, happens in multiple tests from here on)
|
||||
def test_sign():
|
||||
# Otherwise we should get a signature
|
||||
sk, _ = generate_keypair()
|
||||
message = b""
|
||||
|
||||
assert Cryptographer.sign(Cryptographer.signature_format(""), sk) is not None
|
||||
assert Cryptographer.sign(message, sk) is not None
|
||||
|
||||
# Check that the returns work
|
||||
assert isinstance(Cryptographer.sign(Cryptographer.signature_format(""), sk, rtype="str"), str)
|
||||
assert isinstance(Cryptographer.sign(Cryptographer.signature_format(""), sk, rtype="bytes"), bytes)
|
||||
assert isinstance(Cryptographer.sign(message, sk, rtype="str"), str)
|
||||
assert isinstance(Cryptographer.sign(message, sk, rtype="bytes"), bytes)
|
||||
|
||||
|
||||
def test_verify_wrong_pk():
|
||||
@@ -247,10 +246,10 @@ def test_verify_random_values():
|
||||
# Random values shouldn't verify
|
||||
sk, pk = generate_keypair()
|
||||
|
||||
message = get_random_value_hex(32)
|
||||
message = binascii.unhexlify(get_random_value_hex(32))
|
||||
signature = get_random_value_hex(32)
|
||||
|
||||
assert Cryptographer.verify(Cryptographer.signature_format(message), signature, pk) is False
|
||||
assert Cryptographer.verify(message, signature, pk) is False
|
||||
|
||||
|
||||
def test_verify_wrong_pair():
|
||||
@@ -258,16 +257,28 @@ def test_verify_wrong_pair():
|
||||
sk, _ = generate_keypair()
|
||||
_, pk = generate_keypair()
|
||||
|
||||
message = get_random_value_hex(32)
|
||||
message = binascii.unhexlify(get_random_value_hex(32))
|
||||
signature = get_random_value_hex(32)
|
||||
|
||||
assert Cryptographer.verify(Cryptographer.signature_format(message), signature, pk) is False
|
||||
assert Cryptographer.verify(message, signature, pk) is False
|
||||
|
||||
|
||||
def test_verify_wrong_message():
|
||||
# Verifying with a wrong keypair must fail
|
||||
sk, pk = generate_keypair()
|
||||
|
||||
message = binascii.unhexlify(get_random_value_hex(32))
|
||||
signature = Cryptographer.sign(message, sk)
|
||||
|
||||
wrong_message = binascii.unhexlify(get_random_value_hex(32))
|
||||
|
||||
assert Cryptographer.verify(wrong_message, signature, pk) is False
|
||||
|
||||
|
||||
def test_verify():
|
||||
# A properly generated signature should verify
|
||||
sk, pk = generate_keypair()
|
||||
message = get_random_value_hex(32)
|
||||
signature = Cryptographer.sign(Cryptographer.signature_format(message), sk)
|
||||
message = binascii.unhexlify(get_random_value_hex(32))
|
||||
signature = Cryptographer.sign(message, sk)
|
||||
|
||||
assert Cryptographer.verify(Cryptographer.signature_format(message), signature, pk) is True
|
||||
assert Cryptographer.verify(message, signature, pk) is True
|
||||
|
||||
Reference in New Issue
Block a user