-
Notifications
You must be signed in to change notification settings - Fork 299
feat: add basic fuzz testing #1896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
exploreriii
merged 14 commits into
hiero-ledger:main
from
AntonioCeppellini:1872-implement-fuzzing
Mar 26, 2026
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
97e370c
feat: add basic fuzz testing implementation
AntonioCeppellini 6879f4c
feat: add basic fuzz testing implementation
AntonioCeppellini 5fbfede
feat: add basic fuzz testing implementation
AntonioCeppellini 47c71a2
feat: add basic fuzz testing implementation
AntonioCeppellini ae840aa
fix(hbar): return the stored integer directly in to_tinybars() to avo…
AntonioCeppellini 1362376
feat: add basic fuzz testing implementation
AntonioCeppellini 4f9018f
feat: add basic fuzz testing implementation
AntonioCeppellini df2be95
feat: add basic fuzz testing implementation
AntonioCeppellini 9f031cf
Merge branch 'main' into 1872-implement-fuzzing
AntonioCeppellini f8fe060
feat: add basic fuzz testing implementation
AntonioCeppellini 4ce6b30
feat: add basic fuzz testing implementation
AntonioCeppellini 638f3c6
Merge branch 'main' into 1872-implement-fuzzing
AntonioCeppellini cceaff2
feat: add basic fuzz testing implementation
AntonioCeppellini b8bd63b
feat: add basic fuzz testing implementation
AntonioCeppellini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # with <3 from Anto | ||
|
AntonioCeppellini marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """Shared Hypothesis setup, fixtures, and compatibility re-exports for fuzz tests.""" | ||
|
|
||
| from tests.fuzz.support.classes import ( | ||
| AccountIdAliasCase, | ||
| ContractValueCase, | ||
| EntityIdCase, | ||
| HbarConstructorCase, | ||
| HbarStringCase, | ||
| InvalidContractValueCase, | ||
| ) | ||
| from tests.fuzz.support.profiles import load_hypothesis_profile | ||
| from tests.fuzz.support.registry import ( | ||
| FUZZ_STRATEGIES, | ||
| fuzz_strategies_fixture, | ||
| get_strategy, | ||
| get_strategy_fixture, | ||
| ) | ||
|
|
||
| load_hypothesis_profile() | ||
|
|
||
| __all__ = [ | ||
| "AccountIdAliasCase", | ||
| "ContractValueCase", | ||
| "EntityIdCase", | ||
| "FUZZ_STRATEGIES", | ||
| "HbarConstructorCase", | ||
| "HbarStringCase", | ||
| "InvalidContractValueCase", | ||
| "fuzz_strategies_fixture", | ||
| "get_strategy", | ||
| "get_strategy_fixture", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| from tests.fuzz.support.classes import ( | ||
| AccountIdAliasCase, | ||
| ContractValueCase, | ||
| EntityIdCase, | ||
| HbarConstructorCase, | ||
| HbarStringCase, | ||
| InvalidContractValueCase, | ||
| ) | ||
| from tests.fuzz.support.registry import FUZZ_STRATEGIES, get_strategy | ||
|
|
||
| __all__ = [ | ||
| "AccountIdAliasCase", | ||
| "ContractValueCase", | ||
| "EntityIdCase", | ||
| "FUZZ_STRATEGIES", | ||
| "HbarConstructorCase", | ||
| "HbarStringCase", | ||
| "InvalidContractValueCase", | ||
| "get_strategy", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| from dataclasses import dataclass | ||
| from decimal import Decimal | ||
| from typing import Any | ||
|
|
||
| from hiero_sdk_python import HbarUnit | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class EntityIdCase: | ||
| """A parsed entity ID expectation for public string parsers.""" | ||
|
|
||
| text: str | ||
| shard: int | ||
| realm: int | ||
| value: int | ||
| checksum: str | None = None | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class AccountIdAliasCase: | ||
| """A valid account alias or EVM-address input.""" | ||
|
|
||
| text: str | ||
| shard: int | ||
| realm: int | ||
| alias_hex: str | None = None | ||
| evm_hex: str | None = None | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class HbarStringCase: | ||
| """A valid public Hbar string and its exact tinybar value.""" | ||
|
|
||
| text: str | ||
| tinybars: int | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class HbarConstructorCase: | ||
| """A valid Hbar constructor input and its exact tinybar value.""" | ||
|
|
||
| amount: int | float | Decimal | ||
| unit: HbarUnit | ||
| tinybars: int | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ContractValueCase: | ||
| """A valid contract parameter case routed to an explicit public add_* method.""" | ||
|
|
||
| method_name: str | ||
| value: Any | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class InvalidContractValueCase: | ||
| """An invalid contract parameter case with a precise expected exception.""" | ||
|
|
||
| method_name: str | ||
| value: Any | ||
| expected_exception: type[BaseException] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| from decimal import Decimal | ||
|
|
||
| from hypothesis import strategies as st | ||
| from hypothesis.strategies import SearchStrategy | ||
|
|
||
| from hiero_sdk_python import AccountId, HbarUnit, PrivateKey, TransactionId, TransferTransaction | ||
|
|
||
| from tests.fuzz.support.classes import HbarConstructorCase, HbarStringCase | ||
|
|
||
|
|
||
| def sized_hex(byte_length: int) -> SearchStrategy[str]: | ||
| """Return a fixed-length hex string strategy.""" | ||
| return st.binary(min_size=byte_length, max_size=byte_length).map(bytes.hex) | ||
|
|
||
|
|
||
| def with_optional_0x(hex_strategy: SearchStrategy[str]) -> SearchStrategy[str]: | ||
| """Allow a hex string with or without the `0x` prefix.""" | ||
| return st.one_of(hex_strategy, hex_strategy.map(lambda value: f"0x{value}")) | ||
|
|
||
|
|
||
| def decimal_string(value: Decimal) -> str: | ||
| """Format a Decimal without trailing zeros.""" | ||
| text = format(value, "f") | ||
| if "." in text: | ||
| text = text.rstrip("0").rstrip(".") | ||
| return text or "0" | ||
|
|
||
|
|
||
| def hbar_string_case(unit: HbarUnit, tinybars: int) -> HbarStringCase: | ||
| """Build a valid Hbar string case from an exact tinybar amount.""" | ||
| amount = Decimal(tinybars) / Decimal(unit.tinybar) | ||
| if unit == HbarUnit.HBAR: | ||
| return HbarStringCase(text=decimal_string(amount), tinybars=tinybars) | ||
| return HbarStringCase(text=f"{decimal_string(amount)} {unit.symbol}", tinybars=tinybars) | ||
|
|
||
|
|
||
| def hbar_constructor_case(unit: HbarUnit, tinybars: int) -> HbarConstructorCase: | ||
| """Build a valid Hbar constructor case from an exact tinybar amount.""" | ||
| if unit == HbarUnit.TINYBAR: | ||
| amount: int | float | Decimal = tinybars | ||
| else: | ||
| amount = Decimal(tinybars) / Decimal(unit.tinybar) | ||
| return HbarConstructorCase(amount=amount, unit=unit, tinybars=tinybars) | ||
|
|
||
|
|
||
| def build_valid_transaction_bytes() -> tuple[bytes, bytes]: | ||
| """Build one valid unsigned and one valid signed transaction payload.""" | ||
| operator_id = AccountId.from_string("0.0.1234") | ||
| node_id = AccountId.from_string("0.0.3") | ||
| receiver_id = AccountId.from_string("0.0.5678") | ||
|
|
||
| tx = ( | ||
| TransferTransaction() | ||
| .add_hbar_transfer(operator_id, -100_000_000) | ||
| .add_hbar_transfer(receiver_id, 100_000_000) | ||
| ) | ||
| tx.transaction_id = TransactionId.generate(operator_id) | ||
| tx.node_account_id = node_id | ||
| tx.freeze() | ||
| unsigned_bytes = tx.to_bytes() | ||
|
|
||
| signed_tx = TransferTransaction.from_bytes(unsigned_bytes) | ||
| signed_tx.sign(PrivateKey.from_string_ed25519("02" * 32)) | ||
| signed_bytes = signed_tx.to_bytes() | ||
| return unsigned_bytes, signed_bytes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import os | ||
|
|
||
| from hypothesis import HealthCheck, settings | ||
|
|
||
|
|
||
| def load_hypothesis_profile() -> None: | ||
| """Register and load the active Hypothesis profile.""" | ||
| settings.register_profile( | ||
| "ci", | ||
| settings( | ||
| derandomize=True, | ||
| max_examples=300, | ||
| deadline=750, | ||
| suppress_health_check=[HealthCheck.too_slow], | ||
| ), | ||
| ) | ||
| settings.register_profile( | ||
| "local", | ||
| settings( | ||
| derandomize=False, | ||
| max_examples=1000, | ||
| deadline=None, | ||
| ), | ||
| ) | ||
|
|
||
| requested = os.getenv("HYPOTHESIS_PROFILE") | ||
| if requested: | ||
| settings.load_profile(requested) | ||
| return | ||
|
|
||
| settings.load_profile("ci" if os.getenv("CI") else "local") |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.