nim-libp2p-experimental/tests/testpeerinfo.nim
Jacek Sieka ccd019b328
use stream directly in chronosstream (#163)
* use stream directly in chronosstream

for now, chronos.AsyncStream is not used to provide any features on top
of chronos.Stream, so in order to simplify the code, chronosstream can
be used directly.

In particular, the exception handling is broken in the current
chronosstream - opening and closing the stream is simplified this way as
well.

A future implementation that actually takes advantage of the AsyncStream
features would wrap AsyncStream instead as a separate lpstream
implementation, leaving this one as-is.

* work around chronos exception type issue
2020-05-08 22:10:06 +02:00

74 lines
2.5 KiB
Nim

{.used.}
import unittest, options
import chronos
import ../libp2p/crypto/crypto,
../libp2p/peerinfo,
../libp2p/peer
import ./helpers
suite "PeerInfo":
teardown:
for tracker in testTrackers():
check tracker.isLeaked() == false
test "Should init with private key":
let seckey = PrivateKey.random(RSA)
var peerInfo = PeerInfo.init(seckey)
var peerId = PeerID.init(seckey)
check peerId == peerInfo.peerId
check seckey == peerInfo.privateKey
check seckey.getKey == peerInfo.publicKey.get()
test "Should init with public key":
let seckey = PrivateKey.random(RSA)
var peerInfo = PeerInfo.init(seckey.getKey())
var peerId = PeerID.init(seckey.getKey())
check peerId == peerInfo.peerId
check seckey.getKey == peerInfo.publicKey.get()
test "Should init from PeerId with public key":
let seckey = PrivateKey.random(Ed25519)
var peerInfo = PeerInfo.init(PeerID.init(seckey.getKey()))
var peerId = PeerID.init(seckey.getKey())
check peerId == peerInfo.peerId
check seckey.getKey == peerInfo.publicKey.get()
test "Should init from CIDv0 string":
var peerInfo = PeerInfo.init("QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N")
check:
PeerID.init("QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N") == peerInfo.peerId
# 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")
# check:
# PeerID.init("bafzbeie5745rpv2m6tjyuugywy4d5ewrqgqqhfnf445he3omzpjbx5xqxe") == peerInfo.peerId
test "Should return none if pubkey is missing from id":
let peerInfo = PeerInfo.init(PeerID.init(PrivateKey.random(RSA)))
check peerInfo.publicKey.isNone
test "Should return some if pubkey is present in id":
let peerInfo = PeerInfo.init(PeerID.init(PrivateKey.random(Ed25519)))
check peerInfo.publicKey.isSome
test "join() and isClosed() test":
proc testJoin(): Future[bool] {.async, gcsafe.} =
let peerInfo = PeerInfo.init(PeerID.init(PrivateKey.random(Ed25519)))
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