Fixes gatekeeper and common/tools tests according to 33966e59e1

This commit is contained in:
Sergi Delgado Segura
2020-03-30 13:08:14 +02:00
parent c0ada5f818
commit 709a40dc64
2 changed files with 44 additions and 21 deletions

View File

@@ -3,6 +3,7 @@ import logging
from common.constants import LOCATOR_LEN_BYTES
from common.tools import (
check_compressed_pk_format,
check_sha256_hex_format,
check_locator_format,
compute_locator,
@@ -12,6 +13,34 @@ from common.tools import (
from test.common.unit.conftest import get_random_value_hex
def test_check_compressed_pk_format():
wrong_values = [
None,
3,
15.23,
"",
{},
(),
object,
str,
get_random_value_hex(32),
get_random_value_hex(34),
"06" + get_random_value_hex(32),
]
# check_user_pk must only accept values that is not a 33-byte hex string
for i in range(100):
if i % 2:
prefix = "02"
else:
prefix = "03"
assert check_compressed_pk_format(prefix + get_random_value_hex(32))
# check_user_pk must only accept values that is not a 33-byte hex string
for value in wrong_values:
assert not check_compressed_pk_format(value)
def test_check_sha256_hex_format():
# Only 32-byte hex encoded strings should pass the test
wrong_inputs = [None, str(), 213, 46.67, dict(), "A" * 63, "C" * 65, bytes(), get_random_value_hex(31)]

View File

@@ -15,23 +15,9 @@ def test_init():
assert isinstance(gatekeeper.registered_users, dict) and len(gatekeeper.registered_users) == 0
def test_check_user_pk():
# check_user_pk must only accept values that is not a 33-byte hex string
for _ in range(100):
assert gatekeeper.check_user_pk(get_random_value_hex(33))
def test_check_wrong_user_pk():
wrong_values = [None, 3, 15.23, "", {}, (), object, str, get_random_value_hex(32), get_random_value_hex(34)]
# check_user_pk must only accept values that is not a 33-byte hex string
for value in wrong_values:
assert not gatekeeper.check_user_pk(value)
def test_add_update_user():
# add_update_user adds DEFAULT_SLOTS to a given user as long as the identifier is a 33-byte hex str
user_pk = get_random_value_hex(33)
# add_update_user adds DEFAULT_SLOTS to a given user as long as the identifier is {02, 03}| 32-byte hex str
user_pk = "02" + get_random_value_hex(32)
for _ in range(10):
current_slots = gatekeeper.registered_users.get(user_pk)
@@ -44,7 +30,7 @@ def test_add_update_user():
# The same can be checked for multiple users
for _ in range(10):
# The user identifier is changed every call
user_pk = get_random_value_hex(33)
user_pk = "03" + get_random_value_hex(32)
gatekeeper.add_update_user(user_pk)
assert gatekeeper.registered_users.get(user_pk) == DEFAULT_SLOTS
@@ -58,6 +44,14 @@ def test_add_update_user_wrong_pk():
gatekeeper.add_update_user(wrong_pk)
def test_add_update_user_wrong_pk_prefix():
# Prefixes must be 02 or 03, anything else should fail
wrong_pk = "04" + get_random_value_hex(32)
with pytest.raises(ValueError):
gatekeeper.add_update_user(wrong_pk)
def test_identify_user():
# Identify user should return a user_pk for registered users. It raises
# IdentificationFailure for invalid parameters or non-registered users.
@@ -115,7 +109,7 @@ def test_identify_user_wrong():
def test_fill_slots():
# Free slots will decrease the slot count of a user as long as he has enough slots, otherwise raise NotEnoughSlots
user_pk = get_random_value_hex(33)
user_pk = "02" + get_random_value_hex(32)
gatekeeper.add_update_user(user_pk)
gatekeeper.fill_slots(user_pk, DEFAULT_SLOTS - 1)
@@ -131,11 +125,11 @@ def test_fill_slots():
def test_free_slots():
# Free slots simply adds slots to the user as long as it exists.
user_pk = get_random_value_hex(33)
user_pk = "03" + get_random_value_hex(32)
gatekeeper.add_update_user(user_pk)
gatekeeper.free_slots(user_pk, 33)
gatekeeper.free_slots(user_pk, 42)
assert gatekeeper.registered_users.get(user_pk) == DEFAULT_SLOTS + 33
assert gatekeeper.registered_users.get(user_pk) == DEFAULT_SLOTS + 42
# Just making sure it does not crash for non-registered user
assert gatekeeper.free_slots(get_random_value_hex(33), 10) is None