nim-libp2p-experimental/tests/testautonat.nim

61 lines
1.7 KiB
Nim
Raw Normal View History

2022-08-23 15:49:07 +00:00
import std/options
import chronos
import
../libp2p/[
builders,
protocols/connectivity/autonat
2022-08-23 15:49:07 +00:00
],
./helpers
proc createAutonatSwitch(): Switch =
result = SwitchBuilder.new()
.withRng(newRng())
.withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ])
.withTcpTransport()
.withMplex()
.withAutonat()
.withNoise()
.build()
proc makeAutonatServicePrivate(): Switch =
var autonatProtocol = new LPProtocol
autonatProtocol.handler = proc (conn: Connection, proto: string) {.async, gcsafe.} =
discard await conn.readLp(1024)
await conn.writeLp(AutonatDialResponse(
status: DialError,
text: some("dial failed"),
ma: none(MultiAddress)).encode().buffer)
await conn.close()
autonatProtocol.codec = AutonatCodec
result = newStandardSwitch()
result.mount(autonatProtocol)
suite "Autonat":
teardown:
checkTrackers()
2022-12-22 16:33:59 +00:00
asyncTest "dialMe returns public address":
2022-08-23 15:49:07 +00:00
let
src = newStandardSwitch()
dst = createAutonatSwitch()
await src.start()
await dst.start()
await src.connect(dst.peerInfo.peerId, dst.peerInfo.addrs)
let ma = await Autonat.new(src).dialMe(dst.peerInfo.peerId, dst.peerInfo.addrs)
2022-12-22 16:33:59 +00:00
check ma in src.peerInfo.addrs
2022-08-23 15:49:07 +00:00
await allFutures(src.stop(), dst.stop())
2022-12-22 16:33:59 +00:00
asyncTest "dialMe handles dial error msg":
2022-08-23 15:49:07 +00:00
let
src = newStandardSwitch()
dst = makeAutonatServicePrivate()
await src.start()
await dst.start()
await src.connect(dst.peerInfo.peerId, dst.peerInfo.addrs)
2022-12-22 16:33:59 +00:00
expect AutonatUnreachableError:
2022-08-23 15:49:07 +00:00
discard await Autonat.new(src).dialMe(dst.peerInfo.peerId, dst.peerInfo.addrs)
await allFutures(src.stop(), dst.stop())