Gets rid of blob classes. Close #66

This commit is contained in:
Sergi Delgado Segura
2020-04-09 17:54:05 +02:00
parent 80e72f089d
commit 71507d4c32
17 changed files with 42 additions and 109 deletions

View File

@@ -3,12 +3,10 @@ import binascii
from pytest import fixture
from common.appointment import Appointment
from common.encrypted_blob import EncryptedBlob
from common.constants import LOCATOR_LEN_BYTES
from test.common.unit.conftest import get_random_value_hex
from common.constants import LOCATOR_LEN_BYTES
# Not much to test here, adding it for completeness
@fixture
@@ -46,7 +44,7 @@ def test_init_appointment(appointment_data):
and appointment_data["start_time"] == appointment.start_time
and appointment_data["end_time"] == appointment.end_time
and appointment_data["to_self_delay"] == appointment.to_self_delay
and EncryptedBlob(appointment_data["encrypted_blob"]) == appointment.encrypted_blob
and appointment_data["encrypted_blob"] == appointment.encrypted_blob
)
@@ -66,7 +64,7 @@ def test_to_dict(appointment_data):
and appointment_data["start_time"] == dict_appointment["start_time"]
and appointment_data["end_time"] == dict_appointment["end_time"]
and appointment_data["to_self_delay"] == dict_appointment["to_self_delay"]
and EncryptedBlob(appointment_data["encrypted_blob"]) == EncryptedBlob(dict_appointment["encrypted_blob"])
and appointment_data["encrypted_blob"] == dict_appointment["encrypted_blob"]
)
@@ -110,4 +108,4 @@ def test_serialize(appointment_data):
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
assert binascii.hexlify(encrypted_blob).decode() == appointment.encrypted_blob

View File

@@ -1,18 +0,0 @@
from binascii import unhexlify
from common.blob import Blob
from test.common.unit.conftest import get_random_value_hex
def test_init_blob():
data = get_random_value_hex(64)
blob = Blob(data)
assert isinstance(blob, Blob)
# Wrong data
try:
Blob(unhexlify(get_random_value_hex(64)))
assert False, "Able to create blob with wrong data"
except ValueError:
assert True

View File

@@ -6,10 +6,8 @@ from cryptography.hazmat.primitives import serialization
from coincurve import PrivateKey, PublicKey
import common.cryptographer
from common.blob import Blob
from common.logger import Logger
from common.cryptographer import Cryptographer
from common.encrypted_blob import EncryptedBlob
from test.common.unit.conftest import get_random_value_hex
common.cryptographer.logger = Logger(actor="Cryptographer", log_name_prefix="")
@@ -61,7 +59,7 @@ def test_check_data_key_format():
def test_encrypt_odd_length_data():
blob = Blob(get_random_value_hex(64)[-1])
blob = get_random_value_hex(64)[-1]
key = get_random_value_hex(32)
try:
@@ -73,7 +71,7 @@ def test_encrypt_odd_length_data():
def test_encrypt_wrong_key_size():
blob = Blob(get_random_value_hex(64))
blob = get_random_value_hex(64)
key = get_random_value_hex(31)
try:
@@ -85,15 +83,13 @@ def test_encrypt_wrong_key_size():
def test_encrypt():
blob = Blob(data)
assert Cryptographer.encrypt(blob, key) == encrypted_data
assert Cryptographer.encrypt(data, key) == encrypted_data
def test_decrypt_invalid_tag():
random_key = get_random_value_hex(32)
random_encrypted_data = get_random_value_hex(64)
random_encrypted_blob = EncryptedBlob(random_encrypted_data)
random_encrypted_blob = random_encrypted_data
# Trying to decrypt random data should result in an InvalidTag exception. Our decrypt function
# returns None
@@ -104,7 +100,7 @@ def test_decrypt_invalid_tag():
def test_decrypt_odd_length_data():
random_key = get_random_value_hex(32)
random_encrypted_data_odd = get_random_value_hex(64)[:-1]
random_encrypted_blob_odd = EncryptedBlob(random_encrypted_data_odd)
random_encrypted_blob_odd = random_encrypted_data_odd
try:
Cryptographer.decrypt(random_encrypted_blob_odd, random_key)
@@ -117,7 +113,7 @@ def test_decrypt_odd_length_data():
def test_decrypt_wrong_key_size():
random_key = get_random_value_hex(31)
random_encrypted_data_odd = get_random_value_hex(64)
random_encrypted_blob_odd = EncryptedBlob(random_encrypted_data_odd)
random_encrypted_blob_odd = random_encrypted_data_odd
try:
Cryptographer.decrypt(random_encrypted_blob_odd, random_key)
@@ -129,7 +125,7 @@ def test_decrypt_wrong_key_size():
def test_decrypt():
# Valid data should run with no InvalidTag and verify
assert Cryptographer.decrypt(EncryptedBlob(encrypted_data), key) == data
assert Cryptographer.decrypt(encrypted_data, key) == data
def test_load_key_file():

View File

@@ -1,16 +0,0 @@
from common.encrypted_blob import EncryptedBlob
from test.common.unit.conftest import get_random_value_hex
def test_init_encrypted_blob():
# No much to test here, basically that the object is properly created
data = get_random_value_hex(64)
assert EncryptedBlob(data).data == data
def test_equal():
data = get_random_value_hex(64)
e_blob1 = EncryptedBlob(data)
e_blob2 = EncryptedBlob(data)
assert e_blob1 == e_blob2 and id(e_blob1) != id(e_blob2)