示例#1
0
def test_get_value():
    ma = Multiaddr(
        "/ip4/127.0.0.1/utp/tcp/5555/udp/1234/utp/"
        "p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")

    assert_value_for_proto(ma, P_IP4, "127.0.0.1")
    assert_value_for_proto(ma, P_UTP, "")
    assert_value_for_proto(ma, P_TCP, "5555")
    assert_value_for_proto(ma, P_UDP, "1234")
    assert_value_for_proto(
        ma, P_P2P, "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")

    with pytest.raises(ProtocolNotFoundException):
        ma.value_for_protocol(P_IP6)

    a = Multiaddr("/ip4/0.0.0.0")  # only one addr
    assert_value_for_proto(a, P_IP4, "0.0.0.0")

    a = Multiaddr("/ip4/0.0.0.0/ip4/0.0.0.0/ip4/0.0.0.0")  # same sub-addr
    assert_value_for_proto(a, P_IP4, "0.0.0.0")

    a = Multiaddr("/ip4/0.0.0.0/udp/12345/utp")  # ending in a no-value one.
    assert_value_for_proto(a, P_IP4, "0.0.0.0")
    assert_value_for_proto(a, P_UDP, "12345")
    assert_value_for_proto(a, P_UTP, "")
示例#2
0
def test_value_for_protocol_argument_wrong_type():
    a = Multiaddr("/ip4/127.0.0.1/udp/1234")
    with pytest.raises(ProtocolNotFoundError):
        a.value_for_protocol('str123')

    with pytest.raises(TypeError):
        a.value_for_protocol(None)
示例#3
0
def test_get_value_too_many_fields_protocol(monkeypatch):
    """
    This test patches the Multiaddr's string representation to return
    an invalid string in order to test that value_for_protocol properly
    throws a ValueError.  This avoids some of the error checking in
    the constructor and is easier to patch, thus the actual values
    that the constructor specifies is ignored by the test.
    """
    monkeypatch.setattr("multiaddr.multiaddr.Multiaddr.__str__",
                        lambda ignore: '/udp/1234/5678')
    a = Multiaddr("/ip4/127.0.0.1/udp/1234")
    with pytest.raises(ValueError):
        a.value_for_protocol(P_UDP)
def test_get_value_too_many_fields_protocol(monkeypatch):
    """
    This test patches the Multiaddr's string representation to return
    an invalid string in order to test that value_for_protocol properly
    throws a ValueError.  This avoids some of the error checking in
    the constructor and is easier to patch, thus the actual values
    that the constructor specifies is ignored by the test.
    """
    monkeypatch.setattr("multiaddr.multiaddr.Multiaddr.__str__",
                        lambda ignore: '/udp/1234/5678')
    a = Multiaddr("/ip4/127.0.0.1/udp/1234")
    with pytest.raises(ValueError):
        a.value_for_protocol(P_UDP)
示例#5
0
def test_get_value():
    ma = Multiaddr(
        "/ip4/127.0.0.1/utp/tcp/5555/udp/1234/utp/"
        "p2p/bafzbeigalb34xlqdtvyklzqa5ibmn6pssqsdskc4ty2e4jxy2kamquh22y")

    assert_value_for_proto(ma, P_IP4, "127.0.0.1")
    assert_value_for_proto(ma, P_UTP, None)
    assert_value_for_proto(ma, P_TCP, "5555")
    assert_value_for_proto(ma, P_UDP, "1234")
    assert_value_for_proto(ma, P_P2P,
                           "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
    assert_value_for_proto(ma, "ip4", "127.0.0.1")
    assert_value_for_proto(ma, "utp", None)
    assert_value_for_proto(ma, "tcp", "5555")
    assert_value_for_proto(ma, "udp", "1234")
    assert_value_for_proto(ma, protocol_with_name("ip4"), "127.0.0.1")
    assert_value_for_proto(ma, protocol_with_name("utp"), None)
    assert_value_for_proto(ma, protocol_with_name("tcp"), "5555")
    assert_value_for_proto(ma, protocol_with_name("udp"), "1234")

    with pytest.raises(ProtocolLookupError):
        ma.value_for_protocol(P_IP6)
    with pytest.raises(ProtocolLookupError):
        ma.value_for_protocol("ip6")
    with pytest.raises(ProtocolLookupError):
        ma.value_for_protocol(protocol_with_name("ip6"))

    a = Multiaddr(b"\x35\x03a:b")  # invalid protocol value
    with pytest.raises(BinaryParseError):
        a.value_for_protocol(P_DNS)

    a = Multiaddr("/ip4/0.0.0.0")  # only one addr
    assert_value_for_proto(a, P_IP4, "0.0.0.0")

    a = Multiaddr("/ip4/0.0.0.0/ip4/0.0.0.0/ip4/0.0.0.0")  # same sub-addr
    assert_value_for_proto(a, P_IP4, "0.0.0.0")

    a = Multiaddr("/ip4/0.0.0.0/udp/12345/utp")  # ending in a no-value one.
    assert_value_for_proto(a, P_IP4, "0.0.0.0")
    assert_value_for_proto(a, P_UDP, "12345")
    assert_value_for_proto(a, P_UTP, None)

    a = Multiaddr("/ip4/0.0.0.0/unix/a/b/c/d")  # ending in a path one.
    assert_value_for_proto(a, P_IP4, "0.0.0.0")
    assert_value_for_proto(a, P_UNIX, "/a/b/c/d")

    a = Multiaddr("/unix/studio")
    assert_value_for_proto(a, P_UNIX, "/studio")  # only a path.
示例#6
0
def test_get_value():
    ma = Multiaddr(
        "/ip4/127.0.0.1/utp/tcp/5555/udp/1234/utp/"
        "p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")

    assert_value_for_proto(ma, P_IP4, "127.0.0.1")
    assert_value_for_proto(ma, P_UTP, "")
    assert_value_for_proto(ma, P_TCP, "5555")
    assert_value_for_proto(ma, P_UDP, "1234")
    assert_value_for_proto(
        ma, P_P2P, "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
    assert_value_for_proto(ma, "ip4", "127.0.0.1")
    assert_value_for_proto(ma, "utp", "")
    assert_value_for_proto(ma, "tcp", "5555")
    assert_value_for_proto(ma, "udp", "1234")
    assert_value_for_proto(ma, protocol_with_name("ip4"), "127.0.0.1")
    assert_value_for_proto(ma, protocol_with_name("utp"), "")
    assert_value_for_proto(ma, protocol_with_name("tcp"), "5555")
    assert_value_for_proto(ma, protocol_with_name("udp"), "1234")

    with pytest.raises(ProtocolLookupError):
        ma.value_for_protocol(P_IP6)
    with pytest.raises(ProtocolLookupError):
        ma.value_for_protocol("ip6")
    with pytest.raises(ProtocolLookupError):
        ma.value_for_protocol(protocol_with_name("ip6"))

    a = Multiaddr(b"\x35\x03a:b")  # invalid protocol value
    with pytest.raises(BinaryParseError):
        a.value_for_protocol(P_DNS)

    a = Multiaddr("/ip4/0.0.0.0")  # only one addr
    assert_value_for_proto(a, P_IP4, "0.0.0.0")

    a = Multiaddr("/ip4/0.0.0.0/ip4/0.0.0.0/ip4/0.0.0.0")  # same sub-addr
    assert_value_for_proto(a, P_IP4, "0.0.0.0")

    a = Multiaddr("/ip4/0.0.0.0/udp/12345/utp")  # ending in a no-value one.
    assert_value_for_proto(a, P_IP4, "0.0.0.0")
    assert_value_for_proto(a, P_UDP, "12345")
    assert_value_for_proto(a, P_UTP, "")

    a = Multiaddr("/ip4/0.0.0.0/unix/a/b/c/d")  # ending in a path one.
    assert_value_for_proto(a, P_IP4, "0.0.0.0")
    assert_value_for_proto(a, P_UNIX, "/a/b/c/d")

    a = Multiaddr("/unix/studio")
    assert_value_for_proto(a, P_UNIX, "/studio")  # only a path.
示例#7
0
def test_value_for_protocol_argument_wrong_type():
    a = Multiaddr("/ip4/127.0.0.1/udp/1234")
    with pytest.raises(TypeError):
        a.value_for_protocol(None)
示例#8
0
def test_value_for_protocol_argument_wrong_type():
    a = Multiaddr("/ip4/127.0.0.1/udp/1234")
    with pytest.raises(ProtocolNotFoundError):
        a.value_for_protocol('str123')