Skip to content

Commit b00613d

Browse files
committed
refactor(tests): fix encoding test, add fuzzing
1 parent b419e12 commit b00613d

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"pytest>=6.0", # Core testing package
88
"pytest-xdist", # Multi-process runner
99
"pytest-cov", # Coverage analyzer plugin
10-
# "hypothesis>=6.2.0,<7.0", # Strategy-based fuzzer
10+
"hypothesis", # Strategy-based fuzzer
11+
"hypothesis-jsonschema", # Generate strategies for pydantic models
1112
],
1213
"lint": [
1314
"black>=24", # Auto-formatter and linter

tests/test_cluster.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
import uuid
1+
from eth_utils import to_checksum_address
2+
from hypothesis import given
3+
from hypothesis import strategies as st
4+
from hypothesis_jsonschema import from_schema
25

36
from silverback.cluster.types import ClusterConfiguration
47

8+
CONFIG_SCHEMA = ClusterConfiguration.model_json_schema()
59

6-
def test_hmac_signature():
7-
config = ClusterConfiguration()
8-
cluster_id = uuid.uuid4()
9-
owner = "0x4838B106FCe9647Bdf1E7877BF73cE8B0BAD5f97"
10+
11+
@given( # type: ignore[call-overload]
12+
cluster_id=st.uuids(version=4),
13+
owner=st.binary(min_size=20, max_size=20).map(to_checksum_address),
14+
config_dict=from_schema(CONFIG_SCHEMA),
15+
)
16+
def test_hmac_signature(cluster_id, owner, config_dict):
17+
# NOTE: Ignore `version` fuzzed value
18+
config_dict["version"] = 1
19+
config = ClusterConfiguration(**config_dict)
1020
product_code = config.get_product_code(owner, cluster_id)
21+
# NOTE: There is a gap of empty bytes between 8-16
22+
encoded_config, sig = product_code[:8], product_code[16:]
1123
# NOTE: Ensure we can properly decode the encoded product code into a configuration
12-
assert config == ClusterConfiguration.decode(product_code[:16])
13-
assert config.validate_product_code(owner, product_code[16:], cluster_id)
24+
assert config == ClusterConfiguration.decode(encoded_config)
25+
assert config.validate_product_code(owner, sig, cluster_id)

0 commit comments

Comments
 (0)