nim-libp2p/tests/testpeerinfo.nim

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

86 lines
2.4 KiB
Nim
Raw Permalink Normal View History

{.used.}
# Nim-Libp2p
# Copyright (c) 2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
import options
import chronos
import
../libp2p/crypto/crypto,
../libp2p/multicodec,
../libp2p/peerinfo,
../libp2p/peerid,
../libp2p/routing_record
import ./helpers
suite "PeerInfo":
test "Should init with private key":
let seckey = PrivateKey.random(ECDSA, rng[]).get()
var peerInfo = PeerInfo.new(seckey)
2021-12-16 10:05:20 +00:00
var peerId = PeerId.init(seckey).get()
check peerId == peerInfo.peerId
check seckey.getPublicKey().get() == peerInfo.publicKey
2022-10-20 10:22:28 +00:00
test "Signed peer record":
const
ExpectedDomain = $multiCodec("libp2p-peer-record")
ExpectedPayloadType = @[(byte) 0x03, (byte) 0x01]
2022-10-20 10:22:28 +00:00
let
seckey = PrivateKey.random(rng[]).tryGet()
peerId = PeerId.init(seckey).get()
multiAddresses =
@[
MultiAddress.init("/ip4/0.0.0.0/tcp/24").tryGet(),
MultiAddress.init("/ip4/0.0.0.0/tcp/25").tryGet(),
]
peerInfo = PeerInfo.new(seckey, multiAddresses)
2022-10-20 10:22:28 +00:00
waitFor(peerInfo.update())
let
env = peerInfo.signedPeerRecord.envelope
rec = PeerRecord.decode(env.payload()).tryGet()
2022-10-20 10:22:28 +00:00
# Check envelope fields
check:
env.publicKey == peerInfo.publicKey
env.domain == ExpectedDomain
env.payloadType == ExpectedPayloadType
# Check payload (routing record)
check:
rec.peerId == peerId
rec.seqNo > 0
rec.addresses.len == 2
rec.addresses[0].address == multiAddresses[0]
rec.addresses[1].address == multiAddresses[1]
2022-10-20 10:22:28 +00:00
test "Public address mapping":
let
seckey = PrivateKey.random(ECDSA, rng[]).get()
multiAddresses =
@[
MultiAddress.init("/ip4/0.0.0.0/tcp/24").tryGet(),
MultiAddress.init("/ip4/0.0.0.0/tcp/25").tryGet(),
]
multiAddresses2 = @[MultiAddress.init("/ip4/8.8.8.8/tcp/33").tryGet()]
proc addressMapper(input: seq[MultiAddress]): Future[seq[MultiAddress]] {.async.} =
check input == multiAddresses
await sleepAsync(0.seconds)
return multiAddresses2
2022-10-20 10:22:28 +00:00
var peerInfo =
PeerInfo.new(seckey, multiAddresses, addressMappers = @[addressMapper])
waitFor peerInfo.update()
check peerInfo.addrs == multiAddresses2