2020-05-06 16:31:47 +00:00
|
|
|
{.used.}
|
2019-12-07 16:36:39 +00:00
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
import options, bearssl
|
2020-02-11 17:53:39 +00:00
|
|
|
import chronos
|
2019-12-07 16:36:39 +00:00
|
|
|
import ../libp2p/crypto/crypto,
|
|
|
|
../libp2p/peerinfo,
|
2020-07-01 06:25:09 +00:00
|
|
|
../libp2p/peerid
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
import ./helpers
|
|
|
|
|
2019-12-07 16:36:39 +00:00
|
|
|
suite "PeerInfo":
|
|
|
|
test "Should init with private key":
|
2020-07-07 11:14:11 +00:00
|
|
|
let seckey = PrivateKey.random(ECDSA, rng[]).get()
|
2019-12-07 16:36:39 +00:00
|
|
|
var peerInfo = PeerInfo.init(seckey)
|
2020-07-01 06:25:09 +00:00
|
|
|
var peerId = PeerID.init(seckey).get()
|
2019-12-07 16:36:39 +00:00
|
|
|
|
|
|
|
check peerId == peerInfo.peerId
|
|
|
|
check seckey == peerInfo.privateKey
|
2021-09-02 10:03:40 +00:00
|
|
|
check seckey.getPublicKey().get() == peerInfo.publicKey.get()
|
2019-12-07 16:36:39 +00:00
|
|
|
|
|
|
|
test "Should init with public key":
|
2020-07-07 11:14:11 +00:00
|
|
|
let seckey = PrivateKey.random(ECDSA, rng[]).get()
|
2021-09-02 10:03:40 +00:00
|
|
|
var peerInfo = PeerInfo.init(seckey.getPublicKey().get())
|
|
|
|
var peerId = PeerID.init(seckey.getPublicKey().get()).get()
|
2019-12-07 16:36:39 +00:00
|
|
|
|
|
|
|
check peerId == peerInfo.peerId
|
2021-09-02 10:03:40 +00:00
|
|
|
check seckey.getPublicKey.get() == peerInfo.publicKey.get()
|
2019-12-07 16:36:39 +00:00
|
|
|
|
|
|
|
test "Should init from PeerId with public key":
|
2020-07-07 11:14:11 +00:00
|
|
|
let seckey = PrivateKey.random(Ed25519, rng[]).get()
|
2021-09-02 10:03:40 +00:00
|
|
|
var peerInfo = PeerInfo.init(PeerID.init(seckey.getPublicKey.get()).get())
|
|
|
|
var peerId = PeerID.init(seckey.getPublicKey.get()).get()
|
2019-12-07 16:36:39 +00:00
|
|
|
|
|
|
|
check peerId == peerInfo.peerId
|
2021-09-02 10:03:40 +00:00
|
|
|
check seckey.getPublicKey.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":
|
2021-05-21 16:27:01 +00:00
|
|
|
var peerInfo: PeerInfo
|
|
|
|
try:
|
|
|
|
peerInfo = PeerInfo.init("QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N")
|
|
|
|
except CatchableError:
|
|
|
|
check false
|
2019-12-07 16:36:39 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
check:
|
2020-07-01 06:25:09 +00:00
|
|
|
PeerID.init("QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N").get() == peerInfo.peerId
|
2019-12-07 16:36:39 +00:00
|
|
|
|
2020-05-23 17:14:22 +00:00
|
|
|
# TODO: CIDv1 handling is missing from PeerID
|
2019-12-10 20:50:35 +00:00
|
|
|
# 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-07-07 11:14:11 +00:00
|
|
|
let peerInfo = PeerInfo.init(PeerID.init(PrivateKey.random(ECDSA, rng[]).get()).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-07-07 11:14:11 +00:00
|
|
|
let peerInfo = PeerInfo.init(PeerID.init(PrivateKey.random(Ed25519, rng[]).get()).get())
|
2019-12-10 20:50:35 +00:00
|
|
|
check peerInfo.publicKey.isSome
|