Skip to content

Commit a7d13eb

Browse files
committed
the fuzz
1 parent 3916009 commit a7d13eb

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

bin/inject_nanopb_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
def parse_value(s: str) -> Any:
5151
"""Convert an option value string to an appropriate Python type."""
52-
if s.lstrip("-").isdigit():
52+
if re.fullmatch(r"-?[0-9]+", s):
5353
return int(s)
5454
if s.lower() == "true":
5555
return True

meshtastic/tests/test_inject_nanopb_options.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from unittest.mock import patch
1616

1717
import pytest
18+
from hypothesis import given, strategies as st
1819

1920
from meshtastic.protobuf import (
2021
atak_pb2,
@@ -90,6 +91,28 @@ def test_parse_value_string():
9091
assert parse_value("IS_8") == "IS_8"
9192

9293

94+
@pytest.mark.unit
95+
@given(st.integers())
96+
def test_parse_value_any_integer_returns_int(n):
97+
"""parse_value always returns int for any decimal integer string."""
98+
assert parse_value(str(n)) == n
99+
100+
101+
@pytest.mark.unit
102+
@given(st.text())
103+
def test_parse_value_never_crashes(s):
104+
"""parse_value never raises on arbitrary input."""
105+
result = parse_value(s)
106+
assert isinstance(result, (int, bool, str))
107+
108+
109+
@pytest.mark.unit
110+
@given(st.text().filter(lambda s: not s.lstrip("-").isdigit() and s.lower() not in ("true", "false")))
111+
def test_parse_value_non_numeric_non_bool_returns_str(s):
112+
"""parse_value returns the original string when it is neither an integer nor a boolean."""
113+
assert parse_value(s) == s
114+
115+
93116
# ---------------------------------------------------------------------------
94117
# parse_options_file
95118
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)