2020-05-08 20:10:06 +00:00
|
|
|
{.used.}
|
|
|
|
|
2023-05-18 08:24:17 +00: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-03 02:21:11 +00:00
|
|
|
import chronos, stew/byteutils
|
2020-06-19 17:29:43 +00:00
|
|
|
import ../libp2p/[stream/connection,
|
2019-10-29 18:51:48 +00:00
|
|
|
transports/transport,
|
2019-10-03 19:30:22 +00:00
|
|
|
transports/tcptransport,
|
2021-03-18 15:20:36 +00:00
|
|
|
upgrademngrs/upgrade,
|
2019-10-29 18:51:48 +00:00
|
|
|
multiaddress,
|
2023-04-06 13:23:35 +00:00
|
|
|
multicodec,
|
2020-11-19 02:06:42 +00:00
|
|
|
errors,
|
2019-10-03 19:30:22 +00:00
|
|
|
wire]
|
2020-11-19 02:06:42 +00:00
|
|
|
|
2021-06-30 08:59:30 +00:00
|
|
|
import ./helpers, ./commontransport
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-08-31 17:59:21 +00:00
|
|
|
suite "TCP transport":
|
2020-04-21 01:24:42 +00:00
|
|
|
teardown:
|
2020-09-21 17:48:19 +00:00
|
|
|
checkTrackers()
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
asyncTest "test listener: handle write":
|
2021-12-16 10:05:20 +00:00
|
|
|
let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]
|
2021-06-30 08:59:30 +00:00
|
|
|
let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade())
|
2021-06-14 23:21:44 +00:00
|
|
|
asyncSpawn transport.start(ma)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2023-12-05 07:05:32 +00:00
|
|
|
proc acceptHandler() {.async.} =
|
2020-11-19 02:06:42 +00:00
|
|
|
let conn = await transport.accept()
|
|
|
|
await conn.write("Hello!")
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
let handlerWait = acceptHandler()
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2021-11-24 20:01:12 +00:00
|
|
|
let streamTransport = await connect(transport.addrs[0])
|
2019-08-21 22:53:16 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
let msg = await streamTransport.read(6)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2021-06-14 23:21:44 +00:00
|
|
|
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
|
2020-11-13 03:44:02 +00:00
|
|
|
await streamTransport.closeWait()
|
2020-11-19 02:06:42 +00:00
|
|
|
await transport.stop()
|
2020-11-13 03:44:02 +00:00
|
|
|
check string.fromBytes(msg) == "Hello!"
|
|
|
|
|
|
|
|
asyncTest "test listener: handle read":
|
2021-12-16 10:05:20 +00:00
|
|
|
let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]
|
2020-11-19 02:06:42 +00:00
|
|
|
|
2021-06-30 08:59:30 +00:00
|
|
|
let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade())
|
2021-06-14 23:21:44 +00:00
|
|
|
asyncSpawn transport.start(ma)
|
2020-11-19 02:06:42 +00:00
|
|
|
|
2023-12-05 07:05:32 +00:00
|
|
|
proc acceptHandler() {.async.} =
|
2020-05-15 04:02:05 +00:00
|
|
|
var msg = newSeq[byte](6)
|
2020-11-19 02:06:42 +00:00
|
|
|
let conn = await transport.accept()
|
2020-05-15 04:02:05 +00:00
|
|
|
await conn.readExactly(addr msg[0], 6)
|
2020-11-13 03:44:02 +00:00
|
|
|
check string.fromBytes(msg) == "Hello!"
|
|
|
|
await conn.close()
|
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
let handlerWait = acceptHandler()
|
2021-11-24 20:01:12 +00:00
|
|
|
let streamTransport: StreamTransport = await connect(transport.addrs[0])
|
2020-11-19 02:06:42 +00:00
|
|
|
let sent = await streamTransport.write("Hello!")
|
2020-11-13 03:44:02 +00:00
|
|
|
|
2021-06-14 23:21:44 +00:00
|
|
|
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
|
2020-11-13 03:44:02 +00:00
|
|
|
await streamTransport.closeWait()
|
2020-11-19 02:06:42 +00:00
|
|
|
await transport.stop()
|
2020-11-13 03:44:02 +00: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,
|
2023-12-05 07:05:32 +00:00
|
|
|
transp: StreamTransport) {.async.} =
|
2020-11-13 03:44:02 +00:00
|
|
|
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 08:59:30 +00:00
|
|
|
let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade())
|
2020-11-13 03:44:02 +00: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 23:21:44 +00:00
|
|
|
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
|
2020-11-13 03:44:02 +00:00
|
|
|
|
|
|
|
await conn.close()
|
2020-11-19 02:06:42 +00:00
|
|
|
await transport.stop()
|
2020-11-13 03:44:02 +00: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,
|
2023-12-05 07:05:32 +00:00
|
|
|
transp: StreamTransport) {.async.} =
|
2020-11-13 03:44:02 +00:00
|
|
|
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 01:24:42 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
var server = createStreamServer(address, serveClient, {ReuseAddr})
|
|
|
|
server.start()
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
let ma: MultiAddress = MultiAddress.init(server.sock.getLocalAddress()).tryGet()
|
2021-06-30 08:59:30 +00:00
|
|
|
let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade())
|
2020-11-13 03:44:02 +00:00
|
|
|
let conn = await transport.dial(ma)
|
2020-11-19 02:06:42 +00:00
|
|
|
await conn.write("Hello!")
|
2020-11-13 03:44:02 +00:00
|
|
|
|
2021-06-14 23:21:44 +00:00
|
|
|
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
|
2020-11-19 02:06:42 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
await conn.close()
|
2020-11-19 02:06:42 +00:00
|
|
|
await transport.stop()
|
2020-11-13 03:44:02 +00:00
|
|
|
|
|
|
|
server.stop()
|
|
|
|
server.close()
|
|
|
|
await server.join()
|
|
|
|
|
2023-04-06 13:23:35 +00: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 18:20:23 +00:00
|
|
|
proc transProvider(): Transport = TcpTransport.new(upgrade = Upgrade())
|
|
|
|
|
2023-06-07 12:27:32 +00: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 07:05:32 +00:00
|
|
|
proc acceptHandler() {.async.} =
|
2023-06-07 12:27:32 +00: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 13:48:03 +00:00
|
|
|
commonTransportTest(
|
2022-11-04 18:20:23 +00:00
|
|
|
transProvider,
|
2021-08-03 13:48:03 +00:00
|
|
|
"/ip4/0.0.0.0/tcp/0")
|