py-libp2p/tests/peer/test_peerinfo.py
Tran Ly Vu ebd5ddc21f 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-12 11:31:08 -05:00

45 lines
1.6 KiB
Python

import random
import multiaddr
import pytest
from libp2p.peer.peerinfo import PeerInfo, info_from_p2p_addr, InvalidAddrError
from libp2p.peer.peerdata import PeerData
from libp2p.peer.id import ID
ALPHABETS = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
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
def test_init_no_value():
with pytest.raises(Exception) as _:
#pylint: disable=no-value-for-parameter
PeerInfo()
def test_invalid_addr_1():
with pytest.raises(InvalidAddrError):
info_from_p2p_addr(None)
def test_invalid_addr_2(monkeypatch):
random_addrs = [random.randint(0, 255) for r in range(4)]
monkeypatch.setattr("multiaddr.util.split", lambda x: None)
with pytest.raises(InvalidAddrError):
info_from_p2p_addr(random_addrs)
def test_info_from_p2p_addr():
# pylint: disable=line-too-long
m_addr = multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8000/p2p/3YgLAeMKSAPcGqZkAt8mREqhQXmJT8SN8VCMN4T6ih4GNX9wvK8mWJnWZ1qA2mLdCQ')
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'