forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity_id_fuzzer.py
More file actions
60 lines (40 loc) · 1.48 KB
/
Copy pathentity_id_fuzzer.py
File metadata and controls
60 lines (40 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from __future__ import annotations
import sys
import warnings
import atheris
with atheris.instrument_imports():
from hiero_sdk_python import AccountId, TokenId
from hiero_sdk_python.contract.contract_id import ContractId
EXPECTED_EXCEPTIONS = (TypeError, UnicodeDecodeError, ValueError)
PARSERS = (AccountId.from_string, TokenId.from_string, ContractId.from_string)
def _try_parse(parser, candidate: str) -> None:
try:
parser(candidate)
except EXPECTED_EXCEPTIONS:
return
def _consume_text(provider: atheris.FuzzedDataProvider) -> str:
return provider.ConsumeUnicodeNoSurrogates(256)
def _candidate_inputs(provider: atheris.FuzzedDataProvider) -> list[str]:
text = _consume_text(provider)
shard = provider.ConsumeIntInRange(-10, 10)
realm = provider.ConsumeIntInRange(-10, 10)
number = provider.ConsumeIntInRange(-(2**31), 2**31 - 1)
hex_text = provider.ConsumeBytes(48).hex()
return [
text,
f"{shard}.{realm}.{number}",
f"{shard}.{realm}.{hex_text}",
f"0x{hex_text}",
]
def TestOneInput(data: bytes) -> None:
provider = atheris.FuzzedDataProvider(data)
with warnings.catch_warnings():
warnings.simplefilter("ignore", UserWarning)
for candidate in _candidate_inputs(provider):
for parser in PARSERS:
_try_parse(parser, candidate)
def main() -> None:
atheris.Setup(sys.argv, TestOneInput)
atheris.Fuzz()
if __name__ == "__main__":
main()