2021-10-11 12:16:06 +00:00
|
|
|
# Copyright (c) 2020-2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2021-10-19 11:36:57 +00:00
|
|
|
sequtils,
|
|
|
|
chronos, bearssl,
|
2021-10-11 12:16:06 +00:00
|
|
|
testutils/unittests,
|
2021-11-04 06:38:46 +00:00
|
|
|
./test_utils,
|
2021-10-28 09:41:43 +00:00
|
|
|
../../eth/utp/utp_router,
|
2021-10-11 12:16:06 +00:00
|
|
|
../../eth/utp/utp_protocol,
|
|
|
|
../../eth/keys
|
2021-10-19 11:36:57 +00:00
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
proc setAcceptedCallback(event: AsyncEvent): AcceptConnectionCallback[TransportAddress] =
|
|
|
|
return (
|
|
|
|
proc(server: UtpRouter[TransportAddress], client: UtpSocket[TransportAddress]): Future[void] =
|
|
|
|
let fut = newFuture[void]()
|
|
|
|
event.fire()
|
|
|
|
fut.complete()
|
|
|
|
fut
|
|
|
|
)
|
|
|
|
|
|
|
|
proc registerIncomingSocketCallback(serverSockets: AsyncQueue): AcceptConnectionCallback[TransportAddress] =
|
|
|
|
return (
|
|
|
|
proc(server: UtpRouter[TransportAddress], client: UtpSocket[TransportAddress]): Future[void] =
|
|
|
|
serverSockets.addLast(client)
|
|
|
|
)
|
|
|
|
|
2021-11-18 09:05:56 +00:00
|
|
|
proc allowOneIdCallback(allowedId: uint16): AllowConnectionCallback[TransportAddress] =
|
|
|
|
return (
|
|
|
|
proc(r: UtpRouter[TransportAddress], remoteAddress: TransportAddress, connectionId: uint16): bool =
|
|
|
|
connectionId == allowedId
|
|
|
|
)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc transferData(sender: UtpSocket[TransportAddress], receiver: UtpSocket[TransportAddress], data: seq[byte]): Future[seq[byte]] {.async.} =
|
2021-10-19 11:36:57 +00:00
|
|
|
let bytesWritten = await sender.write(data)
|
2021-11-09 14:29:59 +00:00
|
|
|
doAssert bytesWritten.get() == len(data)
|
2021-10-19 11:36:57 +00:00
|
|
|
let received = await receiver.read(len(data))
|
|
|
|
return received
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
type
|
2021-10-28 09:41:43 +00:00
|
|
|
ClientServerScenario = object
|
|
|
|
utp1: UtpProtocol
|
|
|
|
utp2: UtpProtocol
|
|
|
|
clientSocket: UtpSocket[TransportAddress]
|
|
|
|
serverSocket: UtpSocket[TransportAddress]
|
|
|
|
|
|
|
|
TwoClientsServerScenario = object
|
|
|
|
utp1: UtpProtocol
|
|
|
|
utp2: UtpProtocol
|
|
|
|
utp3: UtpProtocol
|
|
|
|
clientSocket1: UtpSocket[TransportAddress]
|
|
|
|
clientSocket2: UtpSocket[TransportAddress]
|
|
|
|
serverSocket1: UtpSocket[TransportAddress]
|
|
|
|
serverSocket2: UtpSocket[TransportAddress]
|
|
|
|
|
|
|
|
proc initClientServerScenario(): Future[ClientServerScenario] {.async.} =
|
|
|
|
let q = newAsyncQueue[UtpSocket[TransportAddress]]()
|
|
|
|
var server1Called = newAsyncEvent()
|
|
|
|
let address = initTAddress("127.0.0.1", 9079)
|
|
|
|
let utpProt1 = UtpProtocol.new(setAcceptedCallback(server1Called), address)
|
|
|
|
|
|
|
|
let address1 = initTAddress("127.0.0.1", 9080)
|
|
|
|
let utpProt2 = UtpProtocol.new(registerIncomingSocketCallback(q), address1)
|
|
|
|
let clientSocket = await utpProt1.connectTo(address1)
|
|
|
|
# this future will be completed when we called accepted connection callback
|
|
|
|
let serverSocket = await q.popFirst()
|
|
|
|
|
|
|
|
return ClientServerScenario(
|
|
|
|
utp1: utpProt1,
|
|
|
|
utp2: utpProt2,
|
2021-11-18 09:05:56 +00:00
|
|
|
clientSocket: clientSocket.get(),
|
2021-10-28 09:41:43 +00:00
|
|
|
serverSocket: serverSocket
|
|
|
|
)
|
|
|
|
|
|
|
|
proc close(s: ClientServerScenario) {.async.} =
|
2021-11-09 14:29:59 +00:00
|
|
|
await s.clientSocket.destroyWait()
|
|
|
|
await s.serverSocket.destroyWait()
|
|
|
|
await s.utp1.shutdownWait()
|
|
|
|
await s.utp2.shutdownWait()
|
2021-10-28 09:41:43 +00:00
|
|
|
|
|
|
|
proc init2ClientsServerScenario(): Future[TwoClientsServerScenario] {.async.} =
|
|
|
|
var serverSockets = newAsyncQueue[UtpSocket[TransportAddress]]()
|
|
|
|
var server1Called = newAsyncEvent()
|
|
|
|
let address1 = initTAddress("127.0.0.1", 9079)
|
|
|
|
let utpProt1 = UtpProtocol.new(setAcceptedCallback(server1Called), address1)
|
|
|
|
|
|
|
|
let address2 = initTAddress("127.0.0.1", 9080)
|
|
|
|
let utpProt2 = UtpProtocol.new(registerIncomingSocketCallback(serverSockets), address2)
|
|
|
|
|
|
|
|
let address3 = initTAddress("127.0.0.1", 9081)
|
|
|
|
let utpProt3 = UtpProtocol.new(registerIncomingSocketCallback(serverSockets), address3)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
let clientSocket1 = await utpProt1.connectTo(address2)
|
|
|
|
let clientSocket2 = await utpProt1.connectTo(address3)
|
|
|
|
|
|
|
|
await waitUntil(proc (): bool = len(serverSockets) == 2)
|
|
|
|
|
|
|
|
# this future will be completed when we called accepted connection callback
|
|
|
|
let serverSocket1 = serverSockets[0]
|
|
|
|
let serverSocket2 = serverSockets[1]
|
|
|
|
|
|
|
|
return TwoClientsServerScenario(
|
|
|
|
utp1: utpProt1,
|
|
|
|
utp2: utpProt2,
|
|
|
|
utp3: utpProt3,
|
2021-11-18 09:05:56 +00:00
|
|
|
clientSocket1: clientSocket1.get(),
|
|
|
|
clientSocket2: clientSocket2.get(),
|
2021-10-28 09:41:43 +00:00
|
|
|
serverSocket1: serverSocket1,
|
|
|
|
serverSocket2: serverSocket2
|
|
|
|
)
|
|
|
|
|
|
|
|
proc close(s: TwoClientsServerScenario) {.async.} =
|
2021-11-09 14:29:59 +00:00
|
|
|
await s.utp1.shutdownWait()
|
|
|
|
await s.utp2.shutdownWait()
|
|
|
|
await s.utp3.shutdownWait()
|
2021-10-28 09:41:43 +00:00
|
|
|
|
|
|
|
procSuite "Utp protocol over udp tests":
|
2021-10-11 12:16:06 +00:00
|
|
|
let rng = newRng()
|
|
|
|
|
|
|
|
asyncTest "Success connect to remote host":
|
2021-10-19 11:36:57 +00:00
|
|
|
let server1Called = newAsyncEvent()
|
2021-10-11 12:16:06 +00:00
|
|
|
let address = initTAddress("127.0.0.1", 9079)
|
2021-10-19 11:36:57 +00:00
|
|
|
let utpProt1 = UtpProtocol.new(setAcceptedCallback(server1Called), address)
|
2021-10-11 12:16:06 +00:00
|
|
|
|
2021-10-19 11:36:57 +00:00
|
|
|
var server2Called = newAsyncEvent()
|
2021-10-11 12:16:06 +00:00
|
|
|
let address1 = initTAddress("127.0.0.1", 9080)
|
2021-10-19 11:36:57 +00:00
|
|
|
let utpProt2 = UtpProtocol.new(setAcceptedCallback(server2Called), address1)
|
2021-10-11 12:16:06 +00:00
|
|
|
|
2021-11-18 09:05:56 +00:00
|
|
|
let sockResult = await utpProt1.connectTo(address1)
|
|
|
|
let sock = sockResult.get()
|
2021-10-19 11:36:57 +00:00
|
|
|
# this future will be completed when we called accepted connection callback
|
|
|
|
await server2Called.wait()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-10-11 12:16:06 +00:00
|
|
|
check:
|
|
|
|
sock.isConnected()
|
2021-10-15 11:38:51 +00:00
|
|
|
# after successful connection outgoing buffer should be empty as syn packet
|
|
|
|
# should be correctly acked
|
|
|
|
sock.numPacketsInOutGoingBuffer() == 0
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-10-19 11:36:57 +00:00
|
|
|
server2Called.isSet()
|
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
await utpProt1.shutdownWait()
|
|
|
|
await utpProt2.shutdownWait()
|
2021-10-19 11:36:57 +00:00
|
|
|
|
2021-10-25 07:58:13 +00:00
|
|
|
asyncTest "Fail to connect to offline remote host":
|
|
|
|
let server1Called = newAsyncEvent()
|
|
|
|
let address = initTAddress("127.0.0.1", 9079)
|
|
|
|
let utpProt1 = UtpProtocol.new(setAcceptedCallback(server1Called), address , SocketConfig.init(milliseconds(200)))
|
|
|
|
|
|
|
|
let address1 = initTAddress("127.0.0.1", 9080)
|
|
|
|
|
2021-11-18 09:05:56 +00:00
|
|
|
let connectionResult = await utpProt1.connectTo(address1)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-10-25 07:58:13 +00:00
|
|
|
check:
|
2021-11-18 09:05:56 +00:00
|
|
|
connectionResult.isErr()
|
|
|
|
|
|
|
|
let connectionError = connectionResult.error()
|
|
|
|
|
|
|
|
check:
|
|
|
|
connectionError.kind == ConnectionTimedOut
|
|
|
|
|
2021-10-25 07:58:13 +00:00
|
|
|
await waitUntil(proc (): bool = utpProt1.openSockets() == 0)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-10-25 07:58:13 +00:00
|
|
|
check:
|
|
|
|
utpProt1.openSockets() == 0
|
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
await utpProt1.shutdownWait()
|
2021-10-25 07:58:13 +00:00
|
|
|
|
|
|
|
asyncTest "Success connect to remote host which initialy was offline":
|
|
|
|
let server1Called = newAsyncEvent()
|
|
|
|
let address = initTAddress("127.0.0.1", 9079)
|
|
|
|
let utpProt1 = UtpProtocol.new(setAcceptedCallback(server1Called), address, SocketConfig.init(milliseconds(500)))
|
|
|
|
|
|
|
|
let address1 = initTAddress("127.0.0.1", 9080)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-10-25 07:58:13 +00:00
|
|
|
let futSock = utpProt1.connectTo(address1)
|
|
|
|
|
|
|
|
# waiting 400 milisecond will trigger at least one re-send
|
|
|
|
await sleepAsync(milliseconds(400))
|
|
|
|
|
|
|
|
var server2Called = newAsyncEvent()
|
|
|
|
let utpProt2 = UtpProtocol.new(setAcceptedCallback(server2Called), address1)
|
|
|
|
|
|
|
|
# this future will be completed when we called accepted connection callback
|
|
|
|
await server2Called.wait()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-10-25 07:58:13 +00:00
|
|
|
yield futSock
|
|
|
|
|
|
|
|
check:
|
2021-12-20 12:14:50 +00:00
|
|
|
futSock.finished() and (not futSock.failed()) and (not futSock.cancelled())
|
2021-10-25 07:58:13 +00:00
|
|
|
server2Called.isSet()
|
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
await utpProt1.shutdownWait()
|
|
|
|
await utpProt2.shutdownWait()
|
2021-10-25 07:58:13 +00:00
|
|
|
|
2021-10-19 11:36:57 +00:00
|
|
|
asyncTest "Success data transfer when data fits into one packet":
|
2021-10-28 09:41:43 +00:00
|
|
|
let s = await initClientServerScenario()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-10-19 11:36:57 +00:00
|
|
|
check:
|
2021-10-28 09:41:43 +00:00
|
|
|
s.clientSocket.isConnected()
|
2021-10-19 11:36:57 +00:00
|
|
|
# after successful connection outgoing buffer should be empty as syn packet
|
|
|
|
# should be correctly acked
|
2021-10-28 09:41:43 +00:00
|
|
|
s.clientSocket.numPacketsInOutGoingBuffer() == 0
|
2021-10-19 11:36:57 +00:00
|
|
|
|
|
|
|
# Server socket is not in connected state, until first data transfer
|
2021-10-28 09:41:43 +00:00
|
|
|
(not s.serverSocket.isConnected())
|
2021-10-19 11:36:57 +00:00
|
|
|
|
|
|
|
let bytesToTransfer = generateByteArray(rng[], 100)
|
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
let bytesReceivedFromClient = await transferData(s.clientSocket, s.serverSocket, bytesToTransfer)
|
2021-10-19 11:36:57 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
bytesToTransfer == bytesReceivedFromClient
|
2021-10-28 09:41:43 +00:00
|
|
|
s.serverSocket.isConnected()
|
2021-10-19 11:36:57 +00:00
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
let bytesReceivedFromServer = await transferData(s.serverSocket, s.clientSocket, bytesToTransfer)
|
2021-10-19 11:36:57 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
bytesToTransfer == bytesReceivedFromServer
|
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
await s.close()
|
2021-10-19 11:36:57 +00:00
|
|
|
|
|
|
|
asyncTest "Success data transfer when data need to be sliced into multiple packets":
|
2021-10-28 09:41:43 +00:00
|
|
|
let s = await initClientServerScenario()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-10-19 11:36:57 +00:00
|
|
|
check:
|
2021-10-28 09:41:43 +00:00
|
|
|
s.clientSocket.isConnected()
|
2021-10-19 11:36:57 +00:00
|
|
|
# after successful connection outgoing buffer should be empty as syn packet
|
|
|
|
# should be correctly acked
|
2021-10-28 09:41:43 +00:00
|
|
|
s.clientSocket.numPacketsInOutGoingBuffer() == 0
|
2021-10-19 11:36:57 +00:00
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
(not s.serverSocket.isConnected())
|
2021-10-19 11:36:57 +00:00
|
|
|
|
|
|
|
# 5000 bytes is over maximal packet size
|
|
|
|
let bytesToTransfer = generateByteArray(rng[], 5000)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
let bytesReceivedFromClient = await transferData(s.clientSocket, s.serverSocket, bytesToTransfer)
|
|
|
|
let bytesReceivedFromServer = await transferData(s.serverSocket, s.clientSocket, bytesToTransfer)
|
2021-10-19 11:36:57 +00:00
|
|
|
|
|
|
|
# ultimatly all send packets will acked, and outgoing buffer will be empty
|
2021-10-28 09:41:43 +00:00
|
|
|
await waitUntil(proc (): bool = s.clientSocket.numPacketsInOutGoingBuffer() == 0)
|
|
|
|
await waitUntil(proc (): bool = s.serverSocket.numPacketsInOutGoingBuffer() == 0)
|
2021-10-19 11:36:57 +00:00
|
|
|
|
|
|
|
check:
|
2021-10-28 09:41:43 +00:00
|
|
|
s.serverSocket.isConnected()
|
|
|
|
s.clientSocket.numPacketsInOutGoingBuffer() == 0
|
|
|
|
s.serverSocket.numPacketsInOutGoingBuffer() == 0
|
2021-10-19 11:36:57 +00:00
|
|
|
bytesReceivedFromClient == bytesToTransfer
|
|
|
|
bytesReceivedFromServer == bytesToTransfer
|
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
await s.close()
|
2021-10-19 11:36:57 +00:00
|
|
|
|
|
|
|
asyncTest "Success multiple data transfers when data need to be sliced into multiple packets":
|
2021-10-28 09:41:43 +00:00
|
|
|
let s = await initClientServerScenario()
|
2021-10-19 11:36:57 +00:00
|
|
|
|
|
|
|
check:
|
2021-10-28 09:41:43 +00:00
|
|
|
s.clientSocket.isConnected()
|
2021-10-19 11:36:57 +00:00
|
|
|
# after successful connection outgoing buffer should be empty as syn packet
|
|
|
|
# should be correctly acked
|
2021-10-28 09:41:43 +00:00
|
|
|
s.clientSocket.numPacketsInOutGoingBuffer() == 0
|
2021-10-19 11:36:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
# 5000 bytes is over maximal packet size
|
|
|
|
let bytesToTransfer = generateByteArray(rng[], 5000)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
let written = await s.clientSocket.write(bytesToTransfer)
|
2021-10-19 11:36:57 +00:00
|
|
|
|
|
|
|
check:
|
2021-11-09 14:29:59 +00:00
|
|
|
written.get() == len(bytesToTransfer)
|
2021-10-19 11:36:57 +00:00
|
|
|
|
|
|
|
let bytesToTransfer1 = generateByteArray(rng[], 5000)
|
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
let written1 = await s.clientSocket.write(bytesToTransfer1)
|
2021-10-19 11:36:57 +00:00
|
|
|
|
|
|
|
check:
|
2021-11-09 14:29:59 +00:00
|
|
|
written1.get() == len(bytesToTransfer)
|
2021-10-19 11:36:57 +00:00
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
let bytesReceived = await s.serverSocket.read(len(bytesToTransfer) + len(bytesToTransfer1))
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-10-19 11:36:57 +00:00
|
|
|
# ultimatly all send packets will acked, and outgoing buffer will be empty
|
2021-10-28 09:41:43 +00:00
|
|
|
await waitUntil(proc (): bool = s.clientSocket.numPacketsInOutGoingBuffer() == 0)
|
2021-10-19 11:36:57 +00:00
|
|
|
|
|
|
|
check:
|
2021-10-28 09:41:43 +00:00
|
|
|
s.clientSocket.numPacketsInOutGoingBuffer() == 0
|
2021-10-19 11:36:57 +00:00
|
|
|
bytesToTransfer.concat(bytesToTransfer1) == bytesReceived
|
2021-10-28 09:41:43 +00:00
|
|
|
|
|
|
|
await s.close()
|
|
|
|
|
|
|
|
asyncTest "Success data transfers from multiple clients":
|
|
|
|
let s = await init2ClientsServerScenario()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
check:
|
|
|
|
s.clientSocket1.isConnected()
|
|
|
|
s.clientSocket2.isConnected()
|
|
|
|
s.clientSocket1.numPacketsInOutGoingBuffer() == 0
|
|
|
|
s.clientSocket2.numPacketsInOutGoingBuffer() == 0
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
let numBytesToTransfer = 5000
|
|
|
|
let client1Data = generateByteArray(rng[], numBytesToTransfer)
|
|
|
|
let client2Data = generateByteArray(rng[], numBytesToTransfer)
|
|
|
|
|
|
|
|
discard s.clientSocket1.write(client1Data)
|
|
|
|
discard s.clientSocket2.write(client2Data)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
let server1ReadBytes = await s.serverSocket1.read(numBytesToTransfer)
|
|
|
|
let server2ReadBytes = await s.serverSocket2.read(numBytesToTransfer)
|
|
|
|
|
|
|
|
check:
|
|
|
|
client1Data == server1ReadBytes
|
|
|
|
client2Data == server2ReadBytes
|
|
|
|
|
|
|
|
await s.close()
|
2021-11-09 14:29:59 +00:00
|
|
|
|
|
|
|
asyncTest "Gracefull stop of the socket":
|
|
|
|
let s = await initClientServerScenario()
|
|
|
|
check:
|
|
|
|
s.clientSocket.isConnected()
|
|
|
|
# after successful connection outgoing buffer should be empty as syn packet
|
|
|
|
# should be correctly acked
|
|
|
|
s.clientSocket.numPacketsInOutGoingBuffer() == 0
|
|
|
|
|
|
|
|
# Server socket is not in connected state, until first data transfer
|
|
|
|
(not s.serverSocket.isConnected())
|
|
|
|
|
|
|
|
let bytesToTransfer = generateByteArray(rng[], 100)
|
|
|
|
|
|
|
|
let bytesReceivedFromClient = await transferData(s.clientSocket, s.serverSocket, bytesToTransfer)
|
|
|
|
|
|
|
|
check:
|
|
|
|
bytesToTransfer == bytesReceivedFromClient
|
|
|
|
s.serverSocket.isConnected()
|
|
|
|
|
|
|
|
await s.clientSocket.closeWait()
|
|
|
|
|
|
|
|
check:
|
|
|
|
not s.clientSocket.isConnected()
|
|
|
|
s.serverSocket.atEof()
|
|
|
|
s.utp1.openSockets() == 0
|
|
|
|
s.utp2.openSockets() == 1
|
|
|
|
|
|
|
|
await s.serverSocket.destroyWait()
|
|
|
|
|
|
|
|
check:
|
|
|
|
not s.serverSocket.isConnected()
|
|
|
|
s.utp2.openSockets() == 0
|
|
|
|
|
|
|
|
await s.close()
|
|
|
|
|
|
|
|
asyncTest "Reading data until eof":
|
|
|
|
let s = await initClientServerScenario()
|
|
|
|
check:
|
|
|
|
s.clientSocket.isConnected()
|
|
|
|
# after successful connection outgoing buffer should be empty as syn packet
|
|
|
|
# should be correctly acked
|
|
|
|
s.clientSocket.numPacketsInOutGoingBuffer() == 0
|
|
|
|
|
|
|
|
# Server socket is not in connected state, until first data transfer
|
|
|
|
(not s.serverSocket.isConnected())
|
|
|
|
|
|
|
|
let bytesToTransfer1 = generateByteArray(rng[], 1000)
|
|
|
|
let bytesToTransfer2 = generateByteArray(rng[], 1000)
|
|
|
|
let bytesToTransfer3 = generateByteArray(rng[], 1000)
|
|
|
|
|
|
|
|
let w1 = await s.clientSocket.write(bytesToTransfer1)
|
|
|
|
let w2 = await s.clientSocket.write(bytesToTransfer2)
|
|
|
|
let w3 = await s.clientSocket.write(bytesToTransfer3)
|
|
|
|
await s.clientSocket.closeWait()
|
|
|
|
|
|
|
|
let readData = await s.serverSocket.read()
|
|
|
|
|
|
|
|
check:
|
|
|
|
readData == concat(bytesToTransfer1, bytesToTransfer2, bytesToTransfer3)
|
|
|
|
s.serverSocket.atEof()
|
|
|
|
s.utp1.openSockets() == 0
|
|
|
|
|
|
|
|
await s.close()
|
2021-11-18 09:05:56 +00:00
|
|
|
|
|
|
|
asyncTest "Accept connection only from allowed peers":
|
|
|
|
let allowedId: uint16 = 10
|
|
|
|
let lowSynTimeout = milliseconds(500)
|
|
|
|
var serverSockets = newAsyncQueue[UtpSocket[TransportAddress]]()
|
|
|
|
var server1Called = newAsyncEvent()
|
|
|
|
let address1 = initTAddress("127.0.0.1", 9079)
|
2021-12-20 12:14:50 +00:00
|
|
|
let utpProt1 =
|
2021-11-18 09:05:56 +00:00
|
|
|
UtpProtocol.new(setAcceptedCallback(server1Called), address1, SocketConfig.init(lowSynTimeout))
|
|
|
|
|
|
|
|
let address2 = initTAddress("127.0.0.1", 9080)
|
|
|
|
let utpProt2 =
|
|
|
|
UtpProtocol.new(registerIncomingSocketCallback(serverSockets), address2, SocketConfig.init(lowSynTimeout))
|
|
|
|
|
|
|
|
let address3 = initTAddress("127.0.0.1", 9081)
|
|
|
|
let utpProt3 =
|
|
|
|
UtpProtocol.new(
|
|
|
|
registerIncomingSocketCallback(serverSockets),
|
|
|
|
address3,
|
|
|
|
SocketConfig.init(),
|
|
|
|
allowOneIdCallback(allowedId)
|
|
|
|
)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-18 09:05:56 +00:00
|
|
|
let allowedSocketRes = await utpProt1.connectTo(address3, allowedId)
|
|
|
|
let notAllowedSocketRes = await utpProt2.connectTo(address3, allowedId + 1)
|
|
|
|
|
|
|
|
check:
|
|
|
|
allowedSocketRes.isOk()
|
|
|
|
notAllowedSocketRes.isErr()
|
|
|
|
# remote did not allow this connection, and utlimatly it did time out
|
|
|
|
notAllowedSocketRes.error().kind == ConnectionTimedOut
|
|
|
|
|
|
|
|
let clientSocket = allowedSocketRes.get()
|
|
|
|
let serverSocket = await serverSockets.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
clientSocket.connectionId() == allowedId
|
|
|
|
serverSocket.connectionId() == allowedId
|
|
|
|
|
|
|
|
await utpProt1.shutdownWait()
|
|
|
|
await utpProt2.shutdownWait()
|
|
|
|
await utpProt3.shutdownWait()
|
2021-12-09 09:52:21 +00:00
|
|
|
|
|
|
|
asyncTest "Success data transfer of a lot of data should increase available window on sender side":
|
|
|
|
let s = await initClientServerScenario()
|
2022-04-04 11:44:32 +00:00
|
|
|
let startMaxWindow = 2 * s.clientSocket.getSocketConfig().payloadSize
|
2021-12-09 09:52:21 +00:00
|
|
|
check:
|
|
|
|
s.clientSocket.isConnected()
|
|
|
|
# initially window has value equal to some pre configured constant
|
|
|
|
s.clientSocket.currentMaxWindowSize == startMaxWindow
|
|
|
|
# after successful connection outgoing buffer should be empty as syn packet
|
|
|
|
# should be correctly acked
|
|
|
|
s.clientSocket.numPacketsInOutGoingBuffer() == 0
|
|
|
|
|
|
|
|
(not s.serverSocket.isConnected())
|
|
|
|
|
|
|
|
# big transfer of 50kb
|
|
|
|
let bytesToTransfer = generateByteArray(rng[], 50000)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-09 09:52:21 +00:00
|
|
|
let bytesReceivedFromClient = await transferData(s.clientSocket, s.serverSocket, bytesToTransfer)
|
|
|
|
|
|
|
|
# ultimatly all send packets will acked, and outgoing buffer will be empty
|
|
|
|
await waitUntil(proc (): bool = s.clientSocket.numPacketsInOutGoingBuffer() == 0)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# we can only assert that window has grown, becouse specific values depends on
|
|
|
|
# particual timings
|
|
|
|
s.clientSocket.currentMaxWindowSize > startMaxWindow
|
|
|
|
s.serverSocket.isConnected()
|
|
|
|
s.clientSocket.numPacketsInOutGoingBuffer() == 0
|
|
|
|
bytesReceivedFromClient == bytesToTransfer
|
|
|
|
|
|
|
|
await s.close()
|
|
|
|
|
|
|
|
asyncTest "Not used socket should decay its max send window":
|
|
|
|
let s = await initClientServerScenario()
|
2022-04-04 11:44:32 +00:00
|
|
|
let startMaxWindow = 2 * s.clientSocket.getSocketConfig().payloadSize
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-09 09:52:21 +00:00
|
|
|
check:
|
|
|
|
s.clientSocket.isConnected()
|
|
|
|
# initially window has value equal to some pre configured constant
|
|
|
|
s.clientSocket.currentMaxWindowSize == startMaxWindow
|
|
|
|
# after successful connection outgoing buffer should be empty as syn packet
|
|
|
|
# should be correctly acked
|
|
|
|
s.clientSocket.numPacketsInOutGoingBuffer() == 0
|
|
|
|
|
|
|
|
(not s.serverSocket.isConnected())
|
|
|
|
|
|
|
|
# big transfer of 50kb
|
|
|
|
let bytesToTransfer = generateByteArray(rng[], 50000)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-09 09:52:21 +00:00
|
|
|
let bytesReceivedFromClient = await transferData(s.clientSocket, s.serverSocket, bytesToTransfer)
|
|
|
|
|
|
|
|
# ultimatly all send packets will acked, and outgoing buffer will be empty
|
|
|
|
await waitUntil(proc (): bool = s.clientSocket.numPacketsInOutGoingBuffer() == 0)
|
|
|
|
|
|
|
|
let maximumMaxWindow = s.clientSocket.currentMaxWindowSize
|
|
|
|
|
|
|
|
check:
|
|
|
|
# we can only assert that window has grown, becouse specific values depends on
|
|
|
|
# particual timings
|
|
|
|
maximumMaxWindow > startMaxWindow
|
|
|
|
s.serverSocket.isConnected()
|
|
|
|
s.clientSocket.numPacketsInOutGoingBuffer() == 0
|
|
|
|
bytesReceivedFromClient == bytesToTransfer
|
|
|
|
|
|
|
|
# wait long enough to trigger timeout
|
|
|
|
await sleepAsync(seconds(5))
|
|
|
|
|
|
|
|
check:
|
|
|
|
# window should decay when idle
|
|
|
|
s.clientSocket.currentMaxWindowSize < maximumMaxWindow
|
|
|
|
|
|
|
|
await s.close()
|