Clean up peer ID tests
This commit is contained in:
parent
6506079a36
commit
329bd4eb0f
|
@ -1,32 +1,26 @@
|
|||
import random
|
||||
|
||||
from Crypto.PublicKey import RSA
|
||||
import base58
|
||||
import multihash
|
||||
import pytest
|
||||
|
||||
from libp2p.crypto.rsa import create_new_key_pair
|
||||
from libp2p.peer.id import ID
|
||||
|
||||
ALPHABETS = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
||||
|
||||
|
||||
def test_init():
|
||||
def test_eq_impl_for_bytes():
|
||||
random_id_string = ""
|
||||
for _ in range(10):
|
||||
random_id_string += random.SystemRandom().choice(ALPHABETS)
|
||||
random_id_string += random.choice(ALPHABETS)
|
||||
peer_id = ID(random_id_string.encode())
|
||||
assert peer_id == random_id_string.encode()
|
||||
|
||||
|
||||
def test_no_init_value():
|
||||
with pytest.raises(Exception):
|
||||
ID()
|
||||
|
||||
|
||||
def test_pretty():
|
||||
random_id_string = ""
|
||||
for _ in range(10):
|
||||
random_id_string += random.SystemRandom().choice(ALPHABETS)
|
||||
random_id_string += random.choice(ALPHABETS)
|
||||
peer_id = ID(random_id_string.encode())
|
||||
actual = peer_id.pretty()
|
||||
expected = base58.b58encode(random_id_string).decode()
|
||||
|
@ -37,7 +31,7 @@ def test_pretty():
|
|||
def test_str_less_than_10():
|
||||
random_id_string = ""
|
||||
for _ in range(5):
|
||||
random_id_string += random.SystemRandom().choice(ALPHABETS)
|
||||
random_id_string += random.choice(ALPHABETS)
|
||||
peer_id = base58.b58encode(random_id_string).decode()
|
||||
expected = peer_id
|
||||
actual = ID(random_id_string.encode()).__str__()
|
||||
|
@ -48,7 +42,7 @@ def test_str_less_than_10():
|
|||
def test_str_more_than_10():
|
||||
random_id_string = ""
|
||||
for _ in range(10):
|
||||
random_id_string += random.SystemRandom().choice(ALPHABETS)
|
||||
random_id_string += random.choice(ALPHABETS)
|
||||
peer_id = base58.b58encode(random_id_string).decode()
|
||||
expected = peer_id
|
||||
actual = ID(random_id_string.encode()).__str__()
|
||||
|
@ -59,7 +53,7 @@ def test_str_more_than_10():
|
|||
def test_eq_true():
|
||||
random_id_string = ""
|
||||
for _ in range(10):
|
||||
random_id_string += random.SystemRandom().choice(ALPHABETS)
|
||||
random_id_string += random.choice(ALPHABETS)
|
||||
peer_id = ID(random_id_string.encode())
|
||||
|
||||
assert peer_id == base58.b58encode(random_id_string).decode()
|
||||
|
@ -74,21 +68,10 @@ def test_eq_false():
|
|||
assert peer_id != other
|
||||
|
||||
|
||||
def test_hash():
|
||||
random_id_string = ""
|
||||
for _ in range(10):
|
||||
random_id_string += random.SystemRandom().choice(ALPHABETS)
|
||||
|
||||
expected = hash(random_id_string.encode())
|
||||
actual = ID(random_id_string.encode()).__hash__()
|
||||
|
||||
assert actual == expected
|
||||
|
||||
|
||||
def test_id_to_base58():
|
||||
random_id_string = ""
|
||||
for _ in range(10):
|
||||
random_id_string += random.SystemRandom().choice(ALPHABETS)
|
||||
random_id_string += random.choice(ALPHABETS)
|
||||
expected = base58.b58encode(random_id_string).decode()
|
||||
actual = ID(random_id_string.encode()).to_base58()
|
||||
|
||||
|
@ -98,7 +81,7 @@ def test_id_to_base58():
|
|||
def test_id_from_base58():
|
||||
random_id_string = ""
|
||||
for _ in range(10):
|
||||
random_id_string += random.SystemRandom().choice(ALPHABETS)
|
||||
random_id_string += random.choice(ALPHABETS)
|
||||
expected = ID(base58.b58decode(random_id_string))
|
||||
actual = ID.from_base58(random_id_string.encode())
|
||||
|
||||
|
@ -106,12 +89,13 @@ def test_id_from_base58():
|
|||
|
||||
|
||||
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")
|
||||
_, public_key = create_new_key_pair()
|
||||
|
||||
key_bin = public_key.serialize_to_protobuf().SerializeToString()
|
||||
algo = multihash.Func.sha2_256
|
||||
mh_digest = multihash.digest(key_bin, algo)
|
||||
expected = ID(mh_digest.encode())
|
||||
actual = ID.from_pubkey(key_bin)
|
||||
|
||||
actual = ID.from_pubkey(public_key)
|
||||
|
||||
assert actual == expected
|
||||
|
|
Loading…
Reference in New Issue
Block a user