2020-05-08 22:10:06 +02:00
|
|
|
{.used.}
|
|
|
|
|
2023-05-18 10:24:17 +02:00
|
|
|
# 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.
|
|
|
|
|
2020-06-02 20:21:11 -06:00
|
|
|
import chronos, stew/byteutils
|
2020-06-19 11:29:43 -06:00
|
|
|
import
|
|
|
|
../libp2p/[
|
|
|
|
stream/connection,
|
2019-10-29 20:51:48 +02:00
|
|
|
transports/transport,
|
2019-10-03 13:30:22 -06:00
|
|
|
transports/tcptransport,
|
2021-03-18 09:20:36 -06:00
|
|
|
upgrademngrs/upgrade,
|
2019-10-29 20:51:48 +02:00
|
|
|
multiaddress,
|
2023-04-06 15:23:35 +02:00
|
|
|
multicodec,
|
2020-11-18 20:06:42 -06:00
|
|
|
errors,
|
2019-10-03 13:30:22 -06:00
|
|
|
wire,
|
|
|
|
]
|
2020-11-18 20:06:42 -06:00
|
|
|
|
2021-06-30 10:59:30 +02:00
|
|
|
import ./helpers, ./commontransport
|
2020-04-21 10:24:42 +09:00
|
|
|
|
2019-08-31 11:59:21 -06:00
|
|
|
suite "TCP transport":
|
2020-04-21 10:24:42 +09:00
|
|
|
teardown:
|
2020-09-21 19:48:19 +02:00
|
|
|
checkTrackers()
|
2020-04-21 10:24:42 +09:00
|
|
|
|
2020-11-12 21:44:02 -06:00
|
|
|
asyncTest "test listener: handle write":
|
2021-12-16 11:05:20 +01:00
|
|
|
let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]
|
2021-06-30 10:59:30 +02:00
|
|
|
let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade())
|
2021-06-14 17:21:44 -06:00
|
|
|
asyncSpawn transport.start(ma)
|
2020-04-21 10:24:42 +09:00
|
|
|
|
2023-12-05 08:05:32 +01:00
|
|
|
proc acceptHandler() {.async.} =
|
2020-11-18 20:06:42 -06:00
|
|
|
let conn = await transport.accept()
|
|
|
|
await conn.write("Hello!")
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
let handlerWait = acceptHandler()
|
2020-04-21 10:24:42 +09:00
|
|
|
|
2021-11-24 14:01:12 -06:00
|
|
|
let streamTransport = await connect(transport.addrs[0])
|
2019-08-21 16:53:16 -06:00
|
|
|
|
2020-11-12 21:44:02 -06:00
|
|
|
let msg = await streamTransport.read(6)
|
2020-04-21 10:24:42 +09:00
|
|
|
|
2021-06-14 17:21:44 -06:00
|
|
|
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
|
2020-11-12 21:44:02 -06:00
|
|
|
await streamTransport.closeWait()
|
2020-11-18 20:06:42 -06:00
|
|
|
await transport.stop()
|
2020-11-12 21:44:02 -06:00
|
|
|
check string.fromBytes(msg) == "Hello!"
|
|
|
|
|
|
|
|
asyncTest "test listener: handle read":
|
2021-12-16 11:05:20 +01:00
|
|
|
let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]
|
2020-11-18 20:06:42 -06:00
|
|
|
|
2021-06-30 10:59:30 +02:00
|
|
|
let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade())
|
2021-06-14 17:21:44 -06:00
|
|
|
asyncSpawn transport.start(ma)
|
2020-11-18 20:06:42 -06:00
|
|
|
|
2023-12-05 08:05:32 +01:00
|
|
|
proc acceptHandler() {.async.} =
|
2020-05-14 22:02:05 -06:00
|
|
|
var msg = newSeq[byte](6)
|
2020-11-18 20:06:42 -06:00
|
|
|
let conn = await transport.accept()
|
2020-05-14 22:02:05 -06:00
|
|
|
await conn.readExactly(addr msg[0], 6)
|
2020-11-12 21:44:02 -06:00
|
|
|
check string.fromBytes(msg) == "Hello!"
|
|
|
|
await conn.close()
|
|
|
|
|
2020-11-18 20:06:42 -06:00
|
|
|
let handlerWait = acceptHandler()
|
2021-11-24 14:01:12 -06:00
|
|
|
let streamTransport: StreamTransport = await connect(transport.addrs[0])
|
2020-11-18 20:06:42 -06:00
|
|
|
let sent = await streamTransport.write("Hello!")
|
2020-11-12 21:44:02 -06:00
|
|
|
|
2021-06-14 17:21:44 -06:00
|
|
|
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
|
2020-11-12 21:44:02 -06:00
|
|
|
await streamTransport.closeWait()
|
2020-11-18 20:06:42 -06:00
|
|
|
await transport.stop()
|
2020-11-12 21:44:02 -06:00
|
|
|
|
|
|
|
check sent == 6
|
|
|
|
|
|
|
|
asyncTest "test dialer: handle write":
|
|
|
|
let address = initTAddress("0.0.0.0:0")
|
|
|
|
let handlerWait = newFuture[void]()
|
|
|
|
proc serveClient(server: StreamServer, transp: StreamTransport) {.async.} =
|
|
|
|
var wstream = newAsyncStreamWriter(transp)
|
|
|
|
await wstream.write("Hello!")
|
|
|
|
await wstream.finish()
|
|
|
|
await wstream.closeWait()
|
|
|
|
await transp.closeWait()
|
|
|
|
server.stop()
|
|
|
|
server.close()
|
|
|
|
handlerWait.complete()
|
|
|
|
|
|
|
|
var server = createStreamServer(address, serveClient, {ReuseAddr})
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
let ma: MultiAddress = MultiAddress.init(server.sock.getLocalAddress()).tryGet()
|
2021-06-30 10:59:30 +02:00
|
|
|
let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade())
|
2020-11-12 21:44:02 -06:00
|
|
|
let conn = await transport.dial(ma)
|
|
|
|
var msg = newSeq[byte](6)
|
|
|
|
await conn.readExactly(addr msg[0], 6)
|
|
|
|
check string.fromBytes(msg) == "Hello!"
|
|
|
|
|
2021-06-14 17:21:44 -06:00
|
|
|
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
|
2020-11-12 21:44:02 -06:00
|
|
|
|
|
|
|
await conn.close()
|
2020-11-18 20:06:42 -06:00
|
|
|
await transport.stop()
|
2020-11-12 21:44:02 -06:00
|
|
|
|
|
|
|
server.stop()
|
|
|
|
server.close()
|
|
|
|
await server.join()
|
|
|
|
|
|
|
|
asyncTest "test dialer: handle write":
|
|
|
|
let address = initTAddress("0.0.0.0:0")
|
|
|
|
let handlerWait = newFuture[void]()
|
|
|
|
proc serveClient(server: StreamServer, transp: StreamTransport) {.async.} =
|
|
|
|
var rstream = newAsyncStreamReader(transp)
|
|
|
|
let msg = await rstream.read(6)
|
|
|
|
check string.fromBytes(msg) == "Hello!"
|
|
|
|
|
|
|
|
await rstream.closeWait()
|
|
|
|
await transp.closeWait()
|
|
|
|
server.stop()
|
|
|
|
server.close()
|
|
|
|
handlerWait.complete()
|
2020-04-21 10:24:42 +09:00
|
|
|
|
2020-11-12 21:44:02 -06:00
|
|
|
var server = createStreamServer(address, serveClient, {ReuseAddr})
|
|
|
|
server.start()
|
2020-04-21 10:24:42 +09:00
|
|
|
|
2020-11-12 21:44:02 -06:00
|
|
|
let ma: MultiAddress = MultiAddress.init(server.sock.getLocalAddress()).tryGet()
|
2021-06-30 10:59:30 +02:00
|
|
|
let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade())
|
2020-11-12 21:44:02 -06:00
|
|
|
let conn = await transport.dial(ma)
|
2020-11-18 20:06:42 -06:00
|
|
|
await conn.write("Hello!")
|
2020-11-12 21:44:02 -06:00
|
|
|
|
2021-06-14 17:21:44 -06:00
|
|
|
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
|
2020-11-18 20:06:42 -06:00
|
|
|
|
2020-11-12 21:44:02 -06:00
|
|
|
await conn.close()
|
2020-11-18 20:06:42 -06:00
|
|
|
await transport.stop()
|
2020-11-12 21:44:02 -06:00
|
|
|
|
|
|
|
server.stop()
|
|
|
|
server.close()
|
|
|
|
await server.join()
|
|
|
|
|
2023-04-06 15:23:35 +02:00
|
|
|
asyncTest "Starting with duplicate but zero ports addresses must NOT fail":
|
|
|
|
let ma =
|
|
|
|
@[
|
|
|
|
MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet(),
|
|
|
|
MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet(),
|
|
|
|
]
|
|
|
|
|
|
|
|
let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade())
|
|
|
|
|
|
|
|
await transport.start(ma)
|
|
|
|
await transport.stop()
|
|
|
|
|
|
|
|
asyncTest "Bind to listening port when not reachable":
|
|
|
|
let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]
|
|
|
|
let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade())
|
|
|
|
await transport.start(ma)
|
|
|
|
|
|
|
|
let ma2 = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]
|
|
|
|
let transport2: TcpTransport = TcpTransport.new(upgrade = Upgrade())
|
|
|
|
await transport2.start(ma2)
|
|
|
|
|
|
|
|
let ma3 = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]
|
|
|
|
let transport3: TcpTransport = TcpTransport.new(upgrade = Upgrade())
|
|
|
|
await transport3.start(ma3)
|
|
|
|
|
|
|
|
let listeningPort = transport.addrs[0][multiCodec("tcp")].get()
|
|
|
|
|
|
|
|
let conn = await transport.dial(transport2.addrs[0])
|
|
|
|
let acceptedConn = await transport2.accept()
|
|
|
|
let acceptedPort = acceptedConn.observedAddr.get()[multiCodec("tcp")].get()
|
|
|
|
check listeningPort != acceptedPort
|
|
|
|
|
|
|
|
transport.networkReachability = NetworkReachability.NotReachable
|
|
|
|
|
|
|
|
let conn2 = await transport.dial(transport3.addrs[0])
|
|
|
|
let acceptedConn2 = await transport3.accept()
|
|
|
|
let acceptedPort2 = acceptedConn2.observedAddr.get()[multiCodec("tcp")].get()
|
|
|
|
check listeningPort == acceptedPort2
|
|
|
|
|
|
|
|
await allFutures(transport.stop(), transport2.stop(), transport3.stop())
|
|
|
|
|
2022-11-04 19:20:23 +01:00
|
|
|
proc transProvider(): Transport =
|
|
|
|
TcpTransport.new(upgrade = Upgrade())
|
|
|
|
|
2023-06-07 14:27:32 +02:00
|
|
|
asyncTest "Custom timeout":
|
|
|
|
let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]
|
|
|
|
let transport: TcpTransport =
|
|
|
|
TcpTransport.new(upgrade = Upgrade(), connectionsTimeout = 1.milliseconds)
|
|
|
|
asyncSpawn transport.start(ma)
|
|
|
|
|
2023-12-05 08:05:32 +01:00
|
|
|
proc acceptHandler() {.async.} =
|
2023-06-07 14:27:32 +02:00
|
|
|
let conn = await transport.accept()
|
|
|
|
await conn.join()
|
|
|
|
|
|
|
|
let handlerWait = acceptHandler()
|
|
|
|
|
|
|
|
let streamTransport = await connect(transport.addrs[0])
|
|
|
|
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
|
|
|
|
await streamTransport.closeWait()
|
|
|
|
await transport.stop()
|
|
|
|
|
2021-08-03 15:48:03 +02:00
|
|
|
commonTransportTest(transProvider, "/ip4/0.0.0.0/tcp/0")
|