|
37 | 37 | stripnl, |
38 | 38 | support_info, |
39 | 39 | message_to_json, |
| 40 | + to_node_num, |
40 | 41 | Acknowledgment |
41 | 42 | ) |
42 | 43 |
|
@@ -715,3 +716,69 @@ def test_generate_channel_hash_fuzz_aes256(channel_name, key_bytes): |
715 | 716 | "Test generate_channel_hash with fuzzed channel names and 256-bit keys, ensuring it produces single-byte values" |
716 | 717 | hashed = generate_channel_hash(channel_name, key_bytes) |
717 | 718 | 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 |
0 commit comments