Skip to content

Commit 405a6bb

Browse files
committed
Fix an issue in to_node_num and add a bunch of tests to it as well
1 parent db746a0 commit 405a6bb

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

meshtastic/tests/test_util.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
stripnl,
3838
support_info,
3939
message_to_json,
40+
to_node_num,
4041
Acknowledgment
4142
)
4243

@@ -715,3 +716,69 @@ def test_generate_channel_hash_fuzz_aes256(channel_name, key_bytes):
715716
"Test generate_channel_hash with fuzzed channel names and 256-bit keys, ensuring it produces single-byte values"
716717
hashed = generate_channel_hash(channel_name, key_bytes)
717718
assert 0 <= hashed <= 0xFF
719+
720+
721+
@pytest.mark.unit
722+
@pytest.mark.parametrize("input_val,expected", [
723+
# int passthrough
724+
(0, 0),
725+
(1, 1),
726+
(6, 6),
727+
(502009325, 502009325),
728+
(2198819370, 2198819370),
729+
(0xFFFFFFFF, 0xFFFFFFFF),
730+
# !hex format (always treated as hex)
731+
("!00000000", 0x00000000),
732+
("!00000001", 0x00000001),
733+
("!00000010", 0x00000010),
734+
("!000000ff", 0x000000FF),
735+
("!830f522a", 0x830F522A),
736+
("!1dec0ded", 0x1DEC0DED),
737+
("!ffffffff", 0xFFFFFFFF),
738+
("!FFFFFFFF", 0xFFFFFFFF),
739+
# 0xhex format
740+
("0x00000000", 0x00000000),
741+
("0x00000010", 0x00000010),
742+
("0x830f522a", 0x830F522A),
743+
("0x1dec0ded", 0x1DEC0DED),
744+
("0xFFFFFFFF", 0xFFFFFFFF),
745+
# Unprefixed hex string (falls back to hex when decimal fails)
746+
("830f522a", 0x830F522A),
747+
("1dec0ded", 0x1DEC0DED),
748+
# Decimal string
749+
("42", 42),
750+
("12345678", 12345678),
751+
("0", 0),
752+
("1", 1),
753+
# With whitespace
754+
(" !830f522a ", 2198819370),
755+
(" !00000010 ", 16),
756+
(" 0x830f522a ", 2198819370),
757+
])
758+
def test_to_node_num(input_val, expected):
759+
"""Test to_node_num with various valid inputs"""
760+
assert to_node_num(input_val) == expected
761+
762+
763+
@pytest.mark.unit
764+
@pytest.mark.parametrize("input_val", [
765+
"",
766+
"!",
767+
"!!",
768+
"!0x10",
769+
"!xyz",
770+
])
771+
def test_to_node_num_invalid(input_val):
772+
"""Test to_node_num raises ValueError for invalid inputs"""
773+
with pytest.raises(ValueError):
774+
to_node_num(input_val)
775+
776+
777+
@pytest.mark.unit
778+
@given(st.integers(min_value=0, max_value=2**32 - 1))
779+
def test_to_node_num_hypothesis_roundtrip(n):
780+
"""Property: all supported input formats roundtrip for any valid node number"""
781+
assert to_node_num(n) == n
782+
assert to_node_num(f"!{n:08x}") == n
783+
assert to_node_num(f"0x{n:x}") == n
784+
assert to_node_num(str(n)) == n

meshtastic/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ def to_node_num(node_id: Union[int, str]) -> int:
728728
return node_id
729729
s = str(node_id).strip()
730730
if s.startswith("!"):
731-
s = s[1:]
731+
s = "0x" + s[1:]
732732
if s.lower().startswith("0x"):
733733
return int(s, 16)
734734
try:

0 commit comments

Comments
 (0)