From ebd5ddc21f48e90cbb9d75a9616fa543e429f9e0 Mon Sep 17 00:00:00 2001 From: Tran Ly Vu Date: Sun, 13 Jan 2019 00:31:08 +0800 Subject: [PATCH] Update unit tests from peer package (#103) * Update more test for peerid.py Signed-off-by: Tran Ly Vu * Update more tests for peerid.py Signed-off-by: Tran Ly Vu * Update more tests for peerid.py Signed-off-by: Tran Ly Vu * Update more unit tests Signed-off-by: Tran Ly Vu * Update more unit tests Signed-off-by: Tran Ly Vu * FIx travis failaure Signed-off-by: Tran Ly Vu * Fix travis failaure Signed-off-by: Tran Ly Vu * Update indent-string to tab Signed-off-by: Tran Ly Vu * Fix lines that too long Signed-off-by: Tran Ly Vu * Convert indent back to space Signed-off-by: Tran Ly Vu * Fix lines that too long Signed-off-by: Tran Ly Vu * Fix lines that too long Signed-off-by: Tran Ly Vu * Fix lines that too long Signed-off-by: Tran Ly Vu * Fix lines that too long Signed-off-by: Tran Ly Vu * Disable W0212 and E1120 Signed-off-by: Tran Ly Vu * Fix lines that too long Signed-off-by: Tran Ly Vu * Fix failure from travis Signed-off-by: Tran Ly Vu * Fix failure from travis Signed-off-by: Tran Ly Vu * Remove W0212 and E1120 Signed-off-by: Tran Ly Vu * Fix packages import Signed-off-by: Tran Ly Vu * Add pylint dsiable Signed-off-by: Tran Ly Vu --- .pylintrc | 4 +- tests/peer/test_peerid.py | 111 +++++++++++++++++++++++++++++++++++- tests/peer/test_peerinfo.py | 34 ++++++++++- 3 files changed, 145 insertions(+), 4 deletions(-) diff --git a/.pylintrc b/.pylintrc index f062243..6bf6c9f 100644 --- a/.pylintrc +++ b/.pylintrc @@ -127,7 +127,7 @@ disable=print-statement, dict-items-not-iterating, dict-keys-not-iterating, dict-values-not-iterating, - missing-docstring + missing-docstring, # Enable the message, report, category or checker with the given id(s). You can # either give multiple identifier separated by comma (,) or put this option @@ -292,7 +292,7 @@ indent-after-paren=4 # String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 # tab). -indent-string=' ' +indent-string=" " # Maximum number of characters on a single line. max-line-length=100 diff --git a/tests/peer/test_peerid.py b/tests/peer/test_peerid.py index 7e702c0..f7c82cb 100644 --- a/tests/peer/test_peerid.py +++ b/tests/peer/test_peerid.py @@ -1,10 +1,119 @@ +import random +import multihash +import pytest +import base58 from Crypto.PublicKey import RSA +from libp2p.peer.id import ID, id_b58_encode, id_b58_decode, id_from_public_key, id_from_private_key -from libp2p.peer.id import id_from_private_key, id_from_public_key +ALPHABETS = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' + +def test_init_(): + random_id_string = '' + for _ in range(10): + random_id_string += random.SystemRandom().choice(ALPHABETS) + peer_id = ID(random_id_string) + #pylint: disable=protected-access + assert peer_id._id_str == random_id_string + +def test_no_init_value(): + with pytest.raises(Exception) as _: + #pylint: disable=no-value-for-parameter + ID() + +def test_pretty(): + random_id_string = '' + for _ in range(10): + random_id_string += random.SystemRandom().choice(ALPHABETS) + peer_id = ID(random_id_string) + actual = peer_id.pretty() + expected = base58.b58encode(random_id_string).decode() + + assert actual == expected + +def test_str_less_than_10(): + random_id_string = '' + for _ in range(5): + random_id_string += random.SystemRandom().choice(ALPHABETS) + pid = base58.b58encode(random_id_string).decode() + expected = "" + actual = ID(random_id_string).__str__() + + assert actual == expected + +def test_str_more_than_10(): + random_id_string = '' + for _ in range(10): + random_id_string += random.SystemRandom().choice(ALPHABETS) + pid = base58.b58encode(random_id_string).decode() + part_1, part_2 = pid[:2], pid[len(pid)-6:] + + expected = "" + actual = ID(random_id_string).__str__() + + assert actual == expected + +def test_eq_true(): + random_id_string = '' + for _ in range(10): + random_id_string += random.SystemRandom().choice(ALPHABETS) + other = ID(random_id_string) + + expected = True + actual = ID(random_id_string).__eq__(other) + + assert actual == expected + +def test_eq_false(): + other = ID("efgh") + + expected = False + actual = ID("abcd").__eq__(other) + + assert actual == expected + +def test_hash(): + random_id_string = '' + for _ in range(10): + random_id_string += random.SystemRandom().choice(ALPHABETS) + + expected = hash(random_id_string) + actual = ID(random_id_string).__hash__() + + assert actual == expected + +def test_id_b58_encode(): + random_id_string = '' + for _ in range(10): + random_id_string += random.SystemRandom().choice(ALPHABETS) + expected = base58.b58encode(random_id_string).decode() + actual = id_b58_encode(ID(random_id_string)) + + assert actual == expected + +def test_id_b58_decode(): + random_id_string = '' + for _ in range(10): + random_id_string += random.SystemRandom().choice(ALPHABETS) + expected = ID(base58.b58decode(random_id_string)) + actual = id_b58_decode(random_id_string) + + assert actual == expected + +def test_id_from_public_key(): + bits_list = [1024, 1280, 1536, 1536, 2048] + key = RSA.generate(random.choice(bits_list)) + key_bin = key.exportKey("DER") + algo = multihash.Func.sha2_256 + mh_digest = multihash.digest(key_bin, algo) + expected = ID(mh_digest.encode()) + actual = id_from_public_key(key) + + assert actual == expected def test_id_from_private_key(): key = RSA.generate(2048, e=65537) id_from_pub = id_from_public_key(key.publickey()) id_from_priv = id_from_private_key(key) assert id_from_pub == id_from_priv + \ No newline at end of file diff --git a/tests/peer/test_peerinfo.py b/tests/peer/test_peerinfo.py index d0d12a2..30f5e50 100644 --- a/tests/peer/test_peerinfo.py +++ b/tests/peer/test_peerinfo.py @@ -1,7 +1,39 @@ +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 -from libp2p.peer.peerinfo import info_from_p2p_addr +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