2020-05-06 16:31:47 +00:00
|
|
|
{.used.}
|
2019-12-07 16:36:39 +00:00
|
|
|
|
|
|
|
import unittest, options
|
2020-02-11 17:53:39 +00:00
|
|
|
import chronos
|
2019-12-07 16:36:39 +00:00
|
|
|
import ../libp2p/crypto/crypto,
|
|
|
|
../libp2p/peerinfo,
|
|
|
|
../libp2p/peer
|
2020-05-08 20:10:06 +00:00
|
|
|
import ./helpers
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-12-07 16:36:39 +00:00
|
|
|
suite "PeerInfo":
|
2020-04-21 01:24:42 +00:00
|
|
|
teardown:
|
2020-05-08 20:10:06 +00:00
|
|
|
for tracker in testTrackers():
|
|
|
|
check tracker.isLeaked() == false
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-12-07 16:36:39 +00:00
|
|
|
test "Should init with private key":
|
2020-05-18 05:25:55 +00:00
|
|
|
let seckey = PrivateKey.random(RSA).get()
|
2019-12-07 16:36:39 +00:00
|
|
|
var peerInfo = PeerInfo.init(seckey)
|
|
|
|
var peerId = PeerID.init(seckey)
|
|
|
|
|
|
|
|
check peerId == peerInfo.peerId
|
|
|
|
check seckey == peerInfo.privateKey
|
2020-05-18 05:25:55 +00:00
|
|
|
check seckey.getKey().get() == peerInfo.publicKey.get()
|
2019-12-07 16:36:39 +00:00
|
|
|
|
|
|
|
test "Should init with public key":
|
2020-05-18 05:25:55 +00:00
|
|
|
let seckey = PrivateKey.random(RSA).get()
|
|
|
|
var peerInfo = PeerInfo.init(seckey.getKey().get())
|
|
|
|
var peerId = PeerID.init(seckey.getKey().get())
|
2019-12-07 16:36:39 +00:00
|
|
|
|
|
|
|
check peerId == peerInfo.peerId
|
2020-05-18 05:25:55 +00:00
|
|
|
check seckey.getKey.get() == peerInfo.publicKey.get()
|
2019-12-07 16:36:39 +00:00
|
|
|
|
|
|
|
test "Should init from PeerId with public key":
|
2020-05-18 05:25:55 +00:00
|
|
|
let seckey = PrivateKey.random(Ed25519).get()
|
|
|
|
var peerInfo = PeerInfo.init(PeerID.init(seckey.getKey.get()))
|
|
|
|
var peerId = PeerID.init(seckey.getKey.get())
|
2019-12-07 16:36:39 +00:00
|
|
|
|
|
|
|
check peerId == peerInfo.peerId
|
2020-05-18 05:25:55 +00:00
|
|
|
check seckey.getKey.get() == peerInfo.publicKey.get()
|
2019-12-07 16:36:39 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
test "Should init from CIDv0 string":
|
|
|
|
var peerInfo = PeerInfo.init("QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N")
|
2019-12-07 16:36:39 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
check:
|
|
|
|
PeerID.init("QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N") == peerInfo.peerId
|
2019-12-07 16:36:39 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
# TODO: CIDv1 is handling is missing from PeerID
|
|
|
|
# https://github.com/status-im/nim-libp2p/issues/53
|
|
|
|
# test "Should init from CIDv1 string":
|
|
|
|
# var peerInfo = PeerInfo.init("bafzbeie5745rpv2m6tjyuugywy4d5ewrqgqqhfnf445he3omzpjbx5xqxe")
|
2019-12-07 16:36:39 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
# check:
|
|
|
|
# PeerID.init("bafzbeie5745rpv2m6tjyuugywy4d5ewrqgqqhfnf445he3omzpjbx5xqxe") == peerInfo.peerId
|
2019-12-07 16:36:39 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
test "Should return none if pubkey is missing from id":
|
2020-05-18 05:25:55 +00:00
|
|
|
let peerInfo = PeerInfo.init(PeerID.init(PrivateKey.random(RSA).get()))
|
2019-12-10 20:50:35 +00:00
|
|
|
check peerInfo.publicKey.isNone
|
2019-12-07 16:36:39 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
test "Should return some if pubkey is present in id":
|
2020-05-18 05:25:55 +00:00
|
|
|
let peerInfo = PeerInfo.init(PeerID.init(PrivateKey.random(Ed25519).get()))
|
2019-12-10 20:50:35 +00:00
|
|
|
check peerInfo.publicKey.isSome
|
2020-02-11 17:53:39 +00:00
|
|
|
|
|
|
|
test "join() and isClosed() test":
|
|
|
|
proc testJoin(): Future[bool] {.async, gcsafe.} =
|
2020-05-18 05:25:55 +00:00
|
|
|
let peerInfo = PeerInfo.init(PeerID.init(PrivateKey.random(Ed25519).get()))
|
2020-02-11 17:53:39 +00:00
|
|
|
check peerInfo.isClosed() == false
|
|
|
|
var joinFut = peerInfo.join()
|
|
|
|
check joinFut.finished() == false
|
|
|
|
peerInfo.close()
|
|
|
|
await wait(joinFut, 100.milliseconds)
|
|
|
|
check peerInfo.isClosed() == true
|
|
|
|
check (joinFut.finished() == true) and (joinFut.cancelled() == false)
|
|
|
|
result = true
|
|
|
|
check waitFor(testJoin()) == true
|