Exemplo n.º 1
0
def test_int_from_integer():
    i = secrets.randbits(256 - 8)
    assert i == int_from_integer(i)
    assert i == int_from_integer(hex(i).upper())
    assert i == int_from_integer(bin(i).upper())
    assert i == int_from_integer(hex_string(i))
    assert i == int_from_integer(i.to_bytes(32, "big"))
Exemplo n.º 2
0
def test_single_byte_serialization() -> None:

    for i in range(256):
        hex_str = hex_string(i)  # e.g., "1A"
        serialized_byte = serialize([hex_str])
        assert len(serialized_byte) == 2
        assert serialized_byte[0] == 1
        assert [hex_str] == parse(serialized_byte)
Exemplo n.º 3
0
def test_integer_serialization() -> None:

    assert ["OP_0"] == parse(b"\x00")

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        assert serialize([0]) != b"\x00"
        for i in range(1, 17):
            serialized_int = serialize([i])
            assert [hex_string(i)] == parse(serialized_int)

    for i in range(17, 128):
        serialized_int = serialize([i])  # e.g., i = 26
        assert [hex_string(i)] == parse(serialized_int)

    for i in range(128, 256):
        serialized_int = serialize([i])
Exemplo n.º 4
0
def test_int_from_integer() -> None:
    for i in (
        secrets.randbits(256 - 8),
        0x0B6CA75B7D3076C561958CCED813797F6D2275C7F42F3856D007D587769A90,
    ):
        assert i == int_from_integer(i)
        assert i == int_from_integer(" " + hex(i).upper())
        assert -i == int_from_integer(hex(-i).upper() + " ")
        assert i == int_from_integer(hex_string(i))
        assert i == int_from_integer(i.to_bytes(32, "big"))
Exemplo n.º 5
0
def test_hex_string():
    a = 34492435054806958080
    assert hex_string(a) == "01 DEADBEEF 00000000"
    assert hex_string(hex(a).lower()) == "01 DEADBEEF 00000000"
    assert hex_string(bin(a).lower()) == "01 DEADBEEF 00000000"

    a = "01de adbeef00000000"
    assert hex_string(a) == "01 DEADBEEF 00000000"
    a = bytes.fromhex(a)
    assert hex_string(a) == "01 DEADBEEF 00000000"

    # invalid hex-string: odd number of hex digits
    a = "1deadbeef00000000"
    with pytest.raises(ValueError,
                       match="non-hexadecimal number found in fromhex"):
        hex_string(a)

    a = -1
    with pytest.raises(ValueError, match="negative integer: "):
        hex_string(a)
Exemplo n.º 6
0
def test_hex_string() -> None:
    int_ = 34492435054806958080
    assert hex_string(int_) == "01 DEADBEEF 00000000"
    assert hex_string(hex(int_).lower()) == "01 DEADBEEF 00000000"

    a_str = "01de adbeef00000000"
    assert hex_string(a_str) == "01 DEADBEEF 00000000"
    a_bytes = bytes.fromhex(a_str)
    assert hex_string(a_bytes) == "01 DEADBEEF 00000000"

    # invalid hex-string: odd number of hex digits
    a_str = "1deadbeef00000000"
    with pytest.raises(ValueError,
                       match="non-hexadecimal number found in fromhex"):
        hex_string(a_str)

    int_ = -1
    with pytest.raises(BTClibValueError, match="negative integer: "):
        hex_string(int_)