2019-01-13 00:31:08 +08:00
|
|
|
import random
|
2019-07-08 17:50:32 +08:00
|
|
|
|
2019-01-13 00:31:08 +08:00
|
|
|
import pytest
|
2019-07-08 17:50:32 +08:00
|
|
|
|
|
|
|
import multiaddr
|
|
|
|
|
2019-01-13 00:31:08 +08:00
|
|
|
from libp2p.peer.peerinfo import PeerInfo, info_from_p2p_addr, InvalidAddrError
|
|
|
|
from libp2p.peer.peerdata import PeerData
|
|
|
|
from libp2p.peer.id import ID
|
2019-01-10 02:38:56 +08:00
|
|
|
|
2019-07-08 17:50:32 +08:00
|
|
|
|
2019-01-13 00:31:08 +08:00
|
|
|
ALPHABETS = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
2018-11-29 23:06:40 +08:00
|
|
|
|
2019-07-08 17:50:32 +08:00
|
|
|
|
2019-01-13 00:31:08 +08:00
|
|
|
def test_init_():
|
|
|
|
peer_data = PeerData()
|
|
|
|
random_addrs = [random.randint(0, 255) for r in range(4)]
|
|
|
|
peer_data.add_addrs(random_addrs)
|
|
|
|
random_id_string = ''
|
|
|
|
for _ in range(10):
|
|
|
|
random_id_string += random.SystemRandom().choice(ALPHABETS)
|
|
|
|
peer_id = ID(random_id_string)
|
|
|
|
peer_info = PeerInfo(peer_id, peer_data)
|
|
|
|
|
|
|
|
assert peer_info.peer_id == peer_id
|
|
|
|
assert peer_info.addrs == random_addrs
|
|
|
|
|
2019-07-08 17:50:32 +08:00
|
|
|
|
2019-01-13 00:31:08 +08:00
|
|
|
def test_init_no_value():
|
|
|
|
with pytest.raises(Exception) as _:
|
2019-07-08 17:13:08 +08:00
|
|
|
# pylint: disable=no-value-for-parameter
|
2019-01-13 00:31:08 +08:00
|
|
|
PeerInfo()
|
|
|
|
|
|
|
|
|
2019-07-08 17:50:32 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'addr',
|
|
|
|
(
|
|
|
|
None,
|
|
|
|
random.randint(0, 255),
|
|
|
|
multiaddr.Multiaddr('/'),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
def test_info_from_p2p_addr_invalid(addr):
|
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)
|
|
|
|
|
2018-11-29 23:06:40 +08:00
|
|
|
|
2019-07-08 17:50:32 +08:00
|
|
|
def test_info_from_p2p_addr_valid():
|
2018-11-29 23:06:40 +08:00
|
|
|
# pylint: disable=line-too-long
|
2018-12-28 14:56:00 +08:00
|
|
|
m_addr = multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8000/p2p/3YgLAeMKSAPcGqZkAt8mREqhQXmJT8SN8VCMN4T6ih4GNX9wvK8mWJnWZ1qA2mLdCQ')
|
2018-11-29 23:06:40 +08:00
|
|
|
info = info_from_p2p_addr(m_addr)
|
|
|
|
assert info.peer_id.pretty() == '3YgLAeMKSAPcGqZkAt8mREqhQXmJT8SN8VCMN4T6ih4GNX9wvK8mWJnWZ1qA2mLdCQ'
|
|
|
|
assert len(info.addrs) == 1
|
|
|
|
assert str(info.addrs[0]) == '/ip4/127.0.0.1/tcp/8000'
|