py-libp2p/tests/peer/test_peerinfo.py

49 lines
1.4 KiB
Python
Raw Normal View History

Update unit tests from peer package (#103) * Update more test for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more tests for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more tests for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more unit tests Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more unit tests Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * FIx travis failaure Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix travis failaure Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update indent-string to tab Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Convert indent back to space Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Disable W0212 and E1120 Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix failure from travis Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix failure from travis Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Remove W0212 and E1120 Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com> * Fix packages import Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com> * Add pylint dsiable Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com>
2019-01-13 00:31:08 +08:00
import random
2019-07-08 17:50:32 +08:00
import multiaddr
2019-08-03 13:36:19 +08:00
import pytest
2019-07-08 17:50:32 +08:00
Update unit tests from peer package (#103) * Update more test for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more tests for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more tests for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more unit tests Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more unit tests Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * FIx travis failaure Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix travis failaure Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update indent-string to tab Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Convert indent back to space Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Disable W0212 and E1120 Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix failure from travis Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix failure from travis Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Remove W0212 and E1120 Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com> * Fix packages import Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com> * Add pylint dsiable Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com>
2019-01-13 00:31:08 +08:00
from libp2p.peer.id import ID
2019-08-03 13:36:19 +08:00
from libp2p.peer.peerinfo import InvalidAddrError, PeerInfo, info_from_p2p_addr
2019-07-08 17:50:32 +08:00
2019-08-01 06:00:12 +08:00
ALPHABETS = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
VALID_MULTI_ADDR_STR = "/ip4/127.0.0.1/tcp/8000/p2p/3YgLAeMKSAPcGqZkAt8mREqhQXmJT8SN8VCMN4T6ih4GNX9wvK8mWJnWZ1qA2mLdCQ" # noqa: E501
2019-07-08 17:50:32 +08:00
Update unit tests from peer package (#103) * Update more test for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more tests for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more tests for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more unit tests Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more unit tests Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * FIx travis failaure Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix travis failaure Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update indent-string to tab Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Convert indent back to space Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Disable W0212 and E1120 Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix failure from travis Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix failure from travis Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Remove W0212 and E1120 Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com> * Fix packages import Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com> * Add pylint dsiable Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com>
2019-01-13 00:31:08 +08:00
def test_init_():
random_addrs = [random.randint(0, 255) for r in range(4)]
2019-08-01 06:00:12 +08:00
random_id_string = ""
Update unit tests from peer package (#103) * Update more test for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more tests for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more tests for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more unit tests Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more unit tests Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * FIx travis failaure Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix travis failaure Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update indent-string to tab Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Convert indent back to space Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Disable W0212 and E1120 Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix failure from travis Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix failure from travis Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Remove W0212 and E1120 Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com> * Fix packages import Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com> * Add pylint dsiable Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com>
2019-01-13 00:31:08 +08:00
for _ in range(10):
random_id_string += random.SystemRandom().choice(ALPHABETS)
2019-07-31 19:26:13 +08:00
peer_id = ID(random_id_string.encode())
peer_info = PeerInfo(peer_id, random_addrs)
Update unit tests from peer package (#103) * Update more test for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more tests for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more tests for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more unit tests Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more unit tests Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * FIx travis failaure Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix travis failaure Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update indent-string to tab Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Convert indent back to space Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Disable W0212 and E1120 Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix failure from travis Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix failure from travis Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Remove W0212 and E1120 Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com> * Fix packages import Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com> * Add pylint dsiable Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com>
2019-01-13 00:31:08 +08:00
assert peer_info.peer_id == peer_id
assert peer_info.addrs == random_addrs
2019-07-08 17:50:32 +08:00
@pytest.mark.parametrize(
2019-08-01 06:00:12 +08:00
"addr",
2019-07-08 17:50:32 +08:00
(
2019-08-01 06:00:12 +08:00
pytest.param(multiaddr.Multiaddr("/"), id="empty multiaddr"),
2019-07-08 17:55:25 +08:00
pytest.param(
multiaddr.Multiaddr("/ip4/127.0.0.1"),
id="multiaddr without peer_id(p2p protocol)",
2019-07-08 17:55:25 +08:00
),
2019-08-01 06:00:12 +08:00
),
2019-07-08 17:50:32 +08:00
)
def test_info_from_p2p_addr_invalid(addr):
Update unit tests from peer package (#103) * Update more test for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more tests for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more tests for peerid.py Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more unit tests Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update more unit tests Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * FIx travis failaure Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix travis failaure Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Update indent-string to tab Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Convert indent back to space Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Disable W0212 and E1120 Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix lines that too long Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix failure from travis Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Fix failure from travis Signed-off-by: Tran Ly Vu <tranly.vu@accellion.com> * Remove W0212 and E1120 Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com> * Fix packages import Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com> * Add pylint dsiable Signed-off-by: Tran Ly Vu <vutransingapore@gmail.com>
2019-01-13 00:31:08 +08:00
with pytest.raises(InvalidAddrError):
2019-07-08 17:50:32 +08:00
info_from_p2p_addr(addr)
2019-07-08 17:50:32 +08:00
def test_info_from_p2p_addr_valid():
m_addr = multiaddr.Multiaddr(VALID_MULTI_ADDR_STR)
info = info_from_p2p_addr(m_addr)
2019-08-01 06:00:12 +08:00
assert (
info.peer_id.pretty()
== "3YgLAeMKSAPcGqZkAt8mREqhQXmJT8SN8VCMN4T6ih4GNX9wvK8mWJnWZ1qA2mLdCQ"
)
assert len(info.addrs) == 1
2019-08-01 06:00:12 +08:00
assert str(info.addrs[0]) == "/ip4/127.0.0.1/tcp/8000"