2022-07-21 08:58:17 +00:00
|
|
|
# Copyright (c) 2020-2022 Status Research & Development GmbH
|
2021-11-04 06:38:46 +00:00
|
|
|
# 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
|
2022-01-04 08:52:38 +00:00
|
|
|
std/[algorithm, random, sequtils, options],
|
2022-07-21 08:58:17 +00:00
|
|
|
chronos,
|
2021-11-04 06:38:46 +00:00
|
|
|
testutils/unittests,
|
|
|
|
./test_utils,
|
|
|
|
../../eth/utp/utp_router,
|
2021-11-09 14:29:59 +00:00
|
|
|
../../eth/utp/utp_socket,
|
2021-11-04 06:38:46 +00:00
|
|
|
../../eth/utp/packets,
|
2022-11-10 08:01:58 +00:00
|
|
|
../../eth/keys,
|
|
|
|
../stubloglevel
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
procSuite "Utp socket unit test":
|
|
|
|
let rng = newRng()
|
|
|
|
let testAddress = initTAddress("127.0.0.1", 9079)
|
|
|
|
let testBufferSize = 1024'u32
|
2021-11-18 09:05:56 +00:00
|
|
|
let defaultRcvOutgoingId = 314'u16
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
proc packetsToBytes(packets: seq[Packet]): seq[byte] =
|
|
|
|
var resultBytes = newSeq[byte]()
|
|
|
|
for p in packets:
|
|
|
|
resultBytes.add(p.payload)
|
|
|
|
return resultBytes
|
|
|
|
|
|
|
|
asyncTest "Starting outgoing socket should send Syn packet":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
2021-11-12 09:58:49 +00:00
|
|
|
let defaultConfig = SocketConfig.init()
|
2021-11-19 10:36:46 +00:00
|
|
|
let sock1 = newOutgoingSocket[TransportAddress](
|
2021-11-18 09:05:56 +00:00
|
|
|
testAddress,
|
|
|
|
initTestSnd(q),
|
|
|
|
defaultConfig,
|
|
|
|
defaultRcvOutgoingId,
|
|
|
|
rng[]
|
|
|
|
)
|
|
|
|
let fut1 = sock1.startOutgoingSocket()
|
2021-11-04 06:38:46 +00:00
|
|
|
let initialPacket = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
initialPacket.header.pType == ST_SYN
|
2021-11-12 09:58:49 +00:00
|
|
|
initialPacket.header.wndSize == defaultConfig.optRcvBuffer
|
2021-11-04 06:38:46 +00:00
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await sock1.destroyWait()
|
2021-11-18 09:05:56 +00:00
|
|
|
fut1.cancel()
|
|
|
|
|
2021-11-04 06:38:46 +00:00
|
|
|
asyncTest "Outgoing socket should re-send syn packet 2 times before declaring failure":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
2021-11-19 10:36:46 +00:00
|
|
|
let sock1 = newOutgoingSocket[TransportAddress](
|
2021-11-18 09:05:56 +00:00
|
|
|
testAddress,
|
|
|
|
initTestSnd(q),
|
|
|
|
SocketConfig.init(milliseconds(100)),
|
|
|
|
defaultRcvOutgoingId,
|
|
|
|
rng[]
|
|
|
|
)
|
|
|
|
let fut1 = sock1.startOutgoingSocket()
|
2021-11-04 06:38:46 +00:00
|
|
|
let initialPacket = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
initialPacket.header.pType == ST_SYN
|
|
|
|
|
|
|
|
let resentSynPacket = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
resentSynPacket.header.pType == ST_SYN
|
|
|
|
|
|
|
|
let resentSynPacket1 = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
resentSynPacket1.header.pType == ST_SYN
|
|
|
|
|
|
|
|
# next timeout will should disconnect socket
|
|
|
|
await waitUntil(proc (): bool = sock1.isConnected() == false)
|
|
|
|
|
|
|
|
check:
|
|
|
|
not sock1.isConnected()
|
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await sock1.destroyWait()
|
2021-11-18 09:05:56 +00:00
|
|
|
fut1.cancel()
|
|
|
|
|
2021-11-04 06:38:46 +00:00
|
|
|
asyncTest "Processing in order ack should make socket connected":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
let (sock1, packet) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
|
|
|
check:
|
|
|
|
sock1.isConnected()
|
|
|
|
|
|
|
|
await sock1.destroyWait()
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
asyncTest "Processing in order data packet should upload it to buffer and ack packet":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
2022-11-16 16:44:00 +00:00
|
|
|
let initialRemoteSeqNr = 10'u16
|
2021-11-04 06:38:46 +00:00
|
|
|
let data = @[1'u8, 2'u8, 3'u8]
|
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeqNr, q)
|
2021-11-04 06:38:46 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
let dataP1 =
|
2021-12-09 09:52:21 +00:00
|
|
|
dataPacket(
|
2022-11-16 16:44:00 +00:00
|
|
|
initialRemoteSeqNr,
|
2021-12-09 09:52:21 +00:00
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
|
|
|
data,
|
|
|
|
0
|
|
|
|
)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-04 06:38:46 +00:00
|
|
|
await outgoingSocket.processPacket(dataP1)
|
|
|
|
let ack1 = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
ack1.header.pType == ST_STATE
|
2022-11-16 16:44:00 +00:00
|
|
|
ack1.header.ackNr == initialRemoteSeqNr
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
let receivedBytes = await outgoingSocket.read(len(data))
|
|
|
|
|
|
|
|
check:
|
|
|
|
receivedBytes == data
|
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
2022-01-20 08:22:53 +00:00
|
|
|
asyncTest "Processing duplicated fresh data packet should ack it and stop processing":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
2022-11-16 16:44:00 +00:00
|
|
|
let initialRemoteSeqNr = 10'u16
|
2022-01-20 08:22:53 +00:00
|
|
|
let data = @[1'u8, 2'u8, 3'u8]
|
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeqNr, q)
|
2022-01-20 08:22:53 +00:00
|
|
|
|
|
|
|
let dataP1 =
|
|
|
|
dataPacket(
|
2022-11-16 16:44:00 +00:00
|
|
|
initialRemoteSeqNr,
|
2022-01-20 08:22:53 +00:00
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
|
|
|
data,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
|
|
|
|
await outgoingSocket.processPacket(dataP1)
|
|
|
|
|
|
|
|
let ack1 = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
ack1.header.pType == ST_STATE
|
2022-11-16 16:44:00 +00:00
|
|
|
ack1.header.ackNr == initialRemoteSeqNr
|
2022-01-20 08:22:53 +00:00
|
|
|
|
|
|
|
let receivedBytes = await outgoingSocket.read(len(data))
|
|
|
|
|
|
|
|
check:
|
|
|
|
receivedBytes == data
|
|
|
|
|
|
|
|
# remote re-send data packet, most probably due to lost ack
|
|
|
|
await outgoingSocket.processPacket(dataP1)
|
|
|
|
|
|
|
|
let ack2 = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
ack2.header.pType == ST_STATE
|
2022-11-16 16:44:00 +00:00
|
|
|
ack2.header.ackNr == initialRemoteSeqNr
|
2022-01-20 08:22:53 +00:00
|
|
|
# we do not upload data one more time
|
|
|
|
outgoingSocket.numOfBytesInIncomingBuffer() == 0'u32
|
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
2021-11-04 06:38:46 +00:00
|
|
|
asyncTest "Processing out of order data packet should buffer it until receiving in order one":
|
|
|
|
# TODO test is valid until implementing selective acks
|
|
|
|
let q = newAsyncQueue[Packet]()
|
2022-11-16 16:44:00 +00:00
|
|
|
let initialRemoteSeqNr = 10'u16
|
2022-01-04 08:52:38 +00:00
|
|
|
let numOfPackets = 10'u16
|
2021-11-04 06:38:46 +00:00
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeqNr, q)
|
2021-11-04 06:38:46 +00:00
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
var packets = generateDataPackets(numOfPackets, initialRemoteSeqNr, initialPacket.header.connectionId, initialPacket.header.seqNr, rng[])
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
let data = packetsToBytes(packets)
|
|
|
|
|
|
|
|
# start feeding packets from the last one
|
|
|
|
packets.reverse()
|
|
|
|
|
|
|
|
for p in packets:
|
|
|
|
await outgoingSocket.processPacket(p)
|
|
|
|
|
2022-01-04 08:52:38 +00:00
|
|
|
var sentAcks: seq[Packet] = @[]
|
2022-06-17 20:45:37 +00:00
|
|
|
|
2022-01-04 08:52:38 +00:00
|
|
|
for i in 0'u16..<numOfPackets:
|
|
|
|
let ack = await q.get()
|
|
|
|
sentAcks.add(ack)
|
|
|
|
|
|
|
|
# all packets except last one should be selective acks, without bumped ackNr
|
|
|
|
for i in 0'u16..<numOfPackets - 1:
|
|
|
|
check:
|
2022-11-16 16:44:00 +00:00
|
|
|
sentAcks[i].header.ackNr == initialRemoteSeqNr - 1
|
2022-01-04 08:52:38 +00:00
|
|
|
sentAcks[i].eack.isSome()
|
|
|
|
|
|
|
|
# last ack should be normal ack packet (not selective one), and it should ack
|
|
|
|
# all remaining packets
|
|
|
|
let lastAck = sentAcks[numOfPackets - 1]
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
check:
|
2022-01-04 08:52:38 +00:00
|
|
|
lastAck.header.pType == ST_STATE
|
2021-11-04 06:38:46 +00:00
|
|
|
# we are acking in one shot whole 10 packets
|
2022-11-16 16:44:00 +00:00
|
|
|
lastAck.header.ackNr == initialRemoteSeqNr + uint16(len(packets) - 1)
|
2022-01-04 08:52:38 +00:00
|
|
|
|
|
|
|
lastAck.eack.isNone()
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
let receivedData = await outgoingSocket.read(len(data))
|
|
|
|
|
|
|
|
check:
|
|
|
|
receivedData == data
|
2021-11-19 10:36:46 +00:00
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-04 06:38:46 +00:00
|
|
|
asyncTest "Processing out of order data packet should ignore duplicated not ordered packets":
|
|
|
|
# TODO test is valid until implementing selective acks
|
|
|
|
let q = newAsyncQueue[Packet]()
|
2022-11-16 16:44:00 +00:00
|
|
|
let initialRemoteSeqNr = 10'u16
|
2022-01-04 08:52:38 +00:00
|
|
|
let numOfPackets = 3'u16
|
2021-11-04 06:38:46 +00:00
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeqNr, q)
|
2021-11-04 06:38:46 +00:00
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
var packets = generateDataPackets(numOfPackets, initialRemoteSeqNr, initialPacket.header.connectionId, initialPacket.header.seqNr, rng[])
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
let data = packetsToBytes(packets)
|
|
|
|
|
|
|
|
# start feeding packets from the last one
|
|
|
|
packets.reverse()
|
|
|
|
|
|
|
|
# Process last packet additional two times, it should be ignored by processing logic
|
|
|
|
await outgoingSocket.processPacket(packets[0])
|
|
|
|
await outgoingSocket.processPacket(packets[0])
|
|
|
|
|
|
|
|
for p in packets:
|
|
|
|
await outgoingSocket.processPacket(p)
|
|
|
|
|
2022-01-04 08:52:38 +00:00
|
|
|
var sentAcks: seq[Packet] = @[]
|
2022-06-17 20:45:37 +00:00
|
|
|
|
2022-01-04 08:52:38 +00:00
|
|
|
for i in 0'u16..<numOfPackets:
|
|
|
|
let ack = await q.get()
|
|
|
|
sentAcks.add(ack)
|
|
|
|
|
|
|
|
# all packets except last one should be selective acks, without bumped ackNr
|
|
|
|
for i in 0'u16..<numOfPackets - 1:
|
|
|
|
check:
|
2022-11-16 16:44:00 +00:00
|
|
|
sentAcks[i].header.ackNr == initialRemoteSeqNr - 1
|
2022-01-04 08:52:38 +00:00
|
|
|
sentAcks[i].eack.isSome()
|
|
|
|
|
|
|
|
# last ack should be normal ack packet (not selective one), and it should ack
|
|
|
|
# all remaining packets
|
|
|
|
let lastAck = sentAcks[numOfPackets - 1]
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
check:
|
2022-01-04 08:52:38 +00:00
|
|
|
lastAck.header.pType == ST_STATE
|
2021-11-04 06:38:46 +00:00
|
|
|
# we are acking in one shot whole 10 packets
|
2022-11-16 16:44:00 +00:00
|
|
|
lastAck.header.ackNr == initialRemoteSeqNr + uint16(len(packets) - 1)
|
2022-01-04 08:52:38 +00:00
|
|
|
|
|
|
|
lastAck.eack.isNone()
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
let receivedData = await outgoingSocket.read(len(data))
|
|
|
|
|
|
|
|
check:
|
|
|
|
receivedData == data
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-04 06:38:46 +00:00
|
|
|
asyncTest "Processing packets in random order":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
2022-11-16 16:44:00 +00:00
|
|
|
let initialRemoteSeqNr = 10'u16
|
2021-11-04 06:38:46 +00:00
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeqNr, q)
|
2021-11-04 06:38:46 +00:00
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
var packets = generateDataPackets(30, initialRemoteSeqNr, initialPacket.header.connectionId, initialPacket.header.seqNr, rng[])
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
let data = packetsToBytes(packets)
|
|
|
|
|
|
|
|
# start feeding packets from the last one
|
|
|
|
randomize()
|
|
|
|
packets.shuffle()
|
|
|
|
|
|
|
|
for p in packets:
|
|
|
|
await outgoingSocket.processPacket(p)
|
|
|
|
|
|
|
|
let receivedData = await outgoingSocket.read(len(data))
|
|
|
|
|
|
|
|
check:
|
2022-11-16 16:44:00 +00:00
|
|
|
# with packets totally out of order we cannot assert on acks
|
2021-11-04 06:38:46 +00:00
|
|
|
# as they can be fired at any point. What matters is that data is passed
|
|
|
|
# in same order as received.
|
|
|
|
receivedData == data
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
asyncTest "Ignoring totally out of order packet":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
2022-11-16 16:44:00 +00:00
|
|
|
let initialRemoteSeqNr = 10'u16
|
2021-11-04 06:38:46 +00:00
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeqNr, q)
|
2021-11-04 06:38:46 +00:00
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
var packets = generateDataPackets(1025, initialRemoteSeqNr, initialPacket.header.connectionId, initialPacket.header.seqNr, rng[])
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
await outgoingSocket.processPacket(packets[1024])
|
|
|
|
|
|
|
|
await outgoingSocket.processPacket(packets[1023])
|
|
|
|
|
2022-02-24 17:22:44 +00:00
|
|
|
# give some time to process those packets
|
|
|
|
await sleepAsync(milliseconds(500))
|
|
|
|
|
2021-11-04 06:38:46 +00:00
|
|
|
check:
|
2022-11-16 16:44:00 +00:00
|
|
|
outgoingSocket.numPacketsInReorderedBuffer() == 1
|
2021-11-04 06:38:46 +00:00
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
2021-11-04 06:38:46 +00:00
|
|
|
asyncTest "Writing small enough data should produce 1 data packet":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
let dataToWrite = @[1'u8]
|
|
|
|
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
|
|
|
let bytesWritten = await outgoingSocket.write(dataToWrite)
|
|
|
|
|
|
|
|
check:
|
2021-11-09 14:29:59 +00:00
|
|
|
bytesWritten.get() == len(dataToWrite)
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
let sentPacket = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
outgoingSocket.numPacketsInOutGoingBuffer() == 1
|
|
|
|
sentPacket.header.pType == ST_DATA
|
|
|
|
sentPacket.header.seqNr == initialPacket.header.seqNr + 1
|
|
|
|
sentPacket.payload == dataToWrite
|
|
|
|
|
|
|
|
# ackNr in state packet, is set to sentPacket.header.seqNr which means remote
|
|
|
|
# side processed out packet
|
2021-12-09 09:52:21 +00:00
|
|
|
let responseAck =
|
|
|
|
ackPacket(
|
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
sentPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
|
|
|
0
|
|
|
|
)
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
await outgoingSocket.processPacket(responseAck)
|
|
|
|
|
2022-02-24 17:22:44 +00:00
|
|
|
await waitUntil(proc (): bool = outgoingSocket.numPacketsInOutGoingBuffer() == 0)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
check:
|
2021-11-04 06:38:46 +00:00
|
|
|
outgoingSocket.numPacketsInOutGoingBuffer() == 0
|
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
2022-01-21 13:48:01 +00:00
|
|
|
proc ackAllPacket(
|
|
|
|
socket: UtpSocket[TransportAddress],
|
|
|
|
queue: AsyncQueue[Packet],
|
|
|
|
initialRemoteSeq: uint16): Future[void] {.async.} =
|
|
|
|
try:
|
|
|
|
while true:
|
|
|
|
let sentPacket = await queue.get()
|
|
|
|
let ack = ackPacket(
|
|
|
|
initialRemoteSeq,
|
|
|
|
sentPacket.header.connectionId,
|
|
|
|
sentPacket.header.seqNr,
|
|
|
|
1024 * 1024,
|
|
|
|
1000'u32
|
|
|
|
)
|
|
|
|
await socket.processPacket(ack)
|
|
|
|
except CancelledError:
|
2022-01-27 10:07:40 +00:00
|
|
|
discard
|
2022-01-21 13:48:01 +00:00
|
|
|
|
|
|
|
asyncTest "Hitting RTO timeout with packets in flight should not decay window":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
# lot of data which will generate at least 5 packets
|
2022-06-17 20:45:37 +00:00
|
|
|
let bigDataTowWrite = rng[].generateBytes(10000)
|
2022-01-21 13:48:01 +00:00
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
|
|
|
let acker = outgoingSocket.ackAllPacket(q, initialRemoteSeq)
|
|
|
|
let bytesWritten = await outgoingSocket.write(bigDataTowWrite)
|
|
|
|
|
|
|
|
check:
|
|
|
|
bytesWritten.get() == len(bigDataTowWrite)
|
|
|
|
|
|
|
|
await waitUntil(proc (): bool = outgoingSocket.numPacketsInOutGoingBuffer() == 0)
|
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
let maxWindowAfterSuccessfulSends = outgoingSocket.currentMaxWindowSize()
|
2022-01-21 13:48:01 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
# after processing a lot of data, our window size should be a lot bigger than our packet size
|
2022-11-16 16:44:00 +00:00
|
|
|
maxWindowAfterSuccessfulSends > uint32(outgoingSocket.getPacketSize())
|
2022-01-21 13:48:01 +00:00
|
|
|
|
|
|
|
# cancel acking process, next writes will for sure timeout
|
|
|
|
await acker.cancelAndWait()
|
2022-06-17 20:45:37 +00:00
|
|
|
|
2022-01-21 13:48:01 +00:00
|
|
|
# data which fits one packet and will timeout
|
2022-06-17 20:45:37 +00:00
|
|
|
let smallerData = rng[].generateBytes(100)
|
2022-01-21 13:48:01 +00:00
|
|
|
|
|
|
|
let bytesWritten1 = await outgoingSocket.write(smallerData)
|
|
|
|
|
|
|
|
# ignore standard sent packet
|
|
|
|
discard await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
bytesWritten1.get() == len(smallerData)
|
|
|
|
|
|
|
|
# ignore also first re-send
|
|
|
|
discard await q.get()
|
2022-06-17 20:45:37 +00:00
|
|
|
|
2022-01-21 13:48:01 +00:00
|
|
|
let maxWindowAfterTimeout = outgoingSocket.currentMaxWindowSize()
|
|
|
|
|
|
|
|
check:
|
|
|
|
# After standard timeout window should not decay and must be bigger than packet size
|
|
|
|
maxWindowAfterTimeout > uint32(outgoingSocket.getPacketSize())
|
2022-11-16 16:44:00 +00:00
|
|
|
maxWindowAfterTimeout == maxWindowAfterSuccessfulSends
|
2022-01-21 13:48:01 +00:00
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
2021-12-02 14:46:18 +00:00
|
|
|
asyncTest "Blocked writing futures should be properly finished when socket is closed":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
let dataToWrite1 = @[0'u8]
|
|
|
|
let dataToWrite2 = @[1'u8]
|
|
|
|
|
2022-02-24 17:22:44 +00:00
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q, cfg = SocketConfig.init(optSndBuffer = 0))
|
2021-12-02 14:46:18 +00:00
|
|
|
|
|
|
|
let writeFut1 = outgoingSocket.write(dataToWrite1)
|
|
|
|
let writeFut2 = outgoingSocket.write(dataToWrite2)
|
|
|
|
|
|
|
|
# wait a little to show that futures are not progressing
|
|
|
|
await sleepAsync(seconds(1))
|
|
|
|
|
|
|
|
check:
|
|
|
|
not writeFut1.finished()
|
|
|
|
not writeFut2.finished()
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
outgoingSocket.destroy()
|
2021-12-02 14:46:18 +00:00
|
|
|
|
|
|
|
yield writeFut1
|
|
|
|
yield writeFut2
|
|
|
|
|
|
|
|
check:
|
|
|
|
writeFut1.completed()
|
|
|
|
writeFut2.completed()
|
|
|
|
writeFut1.read().isErr()
|
|
|
|
writeFut2.read().isErr()
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
2021-12-02 14:46:18 +00:00
|
|
|
|
|
|
|
asyncTest "Cancelled write futures should not be processed if cancelled before processing":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
let dataToWrite1 = @[0'u8]
|
|
|
|
let dataToWrite2 = @[1'u8]
|
|
|
|
let dataToWrite3 = @[2'u8]
|
|
|
|
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q, 0)
|
|
|
|
|
|
|
|
# only writeFut1 will progress as to processing stage, writeFut2 and writeFut3
|
|
|
|
# will be blocked in queue
|
|
|
|
let writeFut1 = outgoingSocket.write(dataToWrite1)
|
|
|
|
let writeFut2 = outgoingSocket.write(dataToWrite2)
|
|
|
|
let writeFut3 = outgoingSocket.write(dataToWrite3)
|
|
|
|
|
|
|
|
# user decided to cancel second write
|
|
|
|
await writeFut2.cancelAndWait()
|
|
|
|
# remote increased wnd size enough for all writes
|
2021-12-09 09:52:21 +00:00
|
|
|
let someAckFromRemote =
|
|
|
|
ackPacket(
|
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
10,
|
|
|
|
0
|
|
|
|
)
|
2021-12-02 14:46:18 +00:00
|
|
|
|
|
|
|
await outgoingSocket.processPacket(someAckFromRemote)
|
|
|
|
|
|
|
|
yield writeFut1
|
|
|
|
yield writeFut2
|
|
|
|
yield writeFut3
|
|
|
|
|
|
|
|
check:
|
|
|
|
writeFut1.completed()
|
|
|
|
writeFut2.cancelled()
|
|
|
|
writeFut3.completed()
|
|
|
|
|
|
|
|
let p1 = await q.get()
|
|
|
|
let p2 = await q.get
|
|
|
|
|
|
|
|
check:
|
|
|
|
# we produce only two data packets as write with dataToWrite2 was cancelled
|
|
|
|
p1.payload == dataToWrite1
|
|
|
|
p2.payload == dataToWrite3
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
2021-12-02 14:46:18 +00:00
|
|
|
|
2021-11-04 06:38:46 +00:00
|
|
|
asyncTest "Socket should re-send data packet configurable number of times before declaring failure":
|
2021-12-20 12:14:50 +00:00
|
|
|
let q = newAsyncQueue[Packet]()
|
2022-11-16 16:44:00 +00:00
|
|
|
let initialRemoteSeqNr = 10'u16
|
2021-11-04 06:38:46 +00:00
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
let outgoingSocket = newOutgoingSocket[TransportAddress](
|
2021-12-20 12:14:50 +00:00
|
|
|
testAddress,
|
2021-11-18 09:05:56 +00:00
|
|
|
initTestSnd(q),
|
|
|
|
SocketConfig.init(milliseconds(3000), 2),
|
|
|
|
defaultRcvOutgoingId,
|
|
|
|
rng[]
|
|
|
|
)
|
|
|
|
|
|
|
|
let fut1 = outgoingSocket.startOutgoingSocket()
|
|
|
|
|
2021-11-04 06:38:46 +00:00
|
|
|
let initialPacket = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
initialPacket.header.pType == ST_SYN
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
let responseAck =
|
2021-12-09 09:52:21 +00:00
|
|
|
ackPacket(
|
2022-11-16 16:44:00 +00:00
|
|
|
initialRemoteSeqNr,
|
2021-12-09 09:52:21 +00:00
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
|
|
|
0
|
|
|
|
)
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
await outgoingSocket.processPacket(responseAck)
|
|
|
|
|
2022-02-24 17:22:44 +00:00
|
|
|
await waitUntil(proc (): bool = outgoingSocket.isConnected())
|
|
|
|
|
2021-11-04 06:38:46 +00:00
|
|
|
check:
|
|
|
|
outgoingSocket.isConnected()
|
|
|
|
|
|
|
|
let dataToWrite = @[1'u8]
|
|
|
|
|
|
|
|
let bytesWritten = await outgoingSocket.write(dataToWrite)
|
|
|
|
|
|
|
|
check:
|
2021-11-09 14:29:59 +00:00
|
|
|
bytesWritten.get() == len(dataToWrite)
|
2021-11-04 06:38:46 +00:00
|
|
|
|
|
|
|
let sentPacket = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
outgoingSocket.numPacketsInOutGoingBuffer() == 1
|
|
|
|
sentPacket.header.pType == ST_DATA
|
|
|
|
sentPacket.header.seqNr == initialPacket.header.seqNr + 1
|
|
|
|
sentPacket.payload == dataToWrite
|
|
|
|
|
|
|
|
let reSend1 = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
outgoingSocket.numPacketsInOutGoingBuffer() == 1
|
|
|
|
reSend1.header.pType == ST_DATA
|
|
|
|
reSend1.header.seqNr == initialPacket.header.seqNr + 1
|
|
|
|
reSend1.payload == dataToWrite
|
|
|
|
|
|
|
|
let reSend2 = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
outgoingSocket.numPacketsInOutGoingBuffer() == 1
|
|
|
|
reSend2.header.pType == ST_DATA
|
|
|
|
reSend2.header.seqNr == initialPacket.header.seqNr + 1
|
|
|
|
reSend2.payload == dataToWrite
|
|
|
|
|
|
|
|
# next timeout will should disconnect socket
|
|
|
|
await waitUntil(proc (): bool = outgoingSocket.isConnected() == false)
|
|
|
|
|
|
|
|
check:
|
|
|
|
not outgoingSocket.isConnected()
|
|
|
|
len(q) == 0
|
2021-11-09 14:29:59 +00:00
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
asyncTest "Processing in order fin should make socket reach eof and ack this packet":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
2021-12-09 09:52:21 +00:00
|
|
|
let finP =
|
|
|
|
finPacket(
|
2021-12-20 12:14:50 +00:00
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
2021-12-09 09:52:21 +00:00
|
|
|
testBufferSize,
|
|
|
|
0
|
|
|
|
)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
await outgoingSocket.processPacket(finP)
|
|
|
|
let ack1 = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
ack1.header.pType == ST_STATE
|
|
|
|
outgoingSocket.atEof()
|
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
asyncTest "Processing out of order fin should buffer it until receiving all remaining packets":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
let data = @[1'u8, 2'u8, 3'u8]
|
|
|
|
let data1 = @[4'u8, 5'u8, 6'u8]
|
|
|
|
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
|
|
|
let readF = outgoingSocket.read()
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
let dataP =
|
2021-12-09 09:52:21 +00:00
|
|
|
dataPacket(
|
|
|
|
initialRemoteSeq,
|
2021-12-20 12:14:50 +00:00
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
|
|
|
data,
|
2021-12-09 09:52:21 +00:00
|
|
|
0
|
|
|
|
)
|
2021-11-09 14:29:59 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
let dataP1 =
|
2021-12-09 09:52:21 +00:00
|
|
|
dataPacket(
|
2021-12-20 12:14:50 +00:00
|
|
|
initialRemoteSeq + 1,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
|
|
|
data1,
|
2021-12-09 09:52:21 +00:00
|
|
|
0
|
|
|
|
)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
let finP =
|
2021-12-09 09:52:21 +00:00
|
|
|
finPacket(
|
2021-12-20 12:14:50 +00:00
|
|
|
initialRemoteSeq + 2,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
2021-12-09 09:52:21 +00:00
|
|
|
0
|
|
|
|
)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
await outgoingSocket.processPacket(finP)
|
|
|
|
|
|
|
|
check:
|
|
|
|
not readF.finished()
|
|
|
|
not outgoingSocket.atEof()
|
|
|
|
|
|
|
|
await outgoingSocket.processPacket(dataP1)
|
|
|
|
|
|
|
|
check:
|
|
|
|
not readF.finished()
|
|
|
|
not outgoingSocket.atEof()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
await outgoingSocket.processPacket(dataP)
|
|
|
|
|
|
|
|
let bytes = await readF
|
|
|
|
|
|
|
|
check:
|
|
|
|
readF.finished()
|
|
|
|
outgoingSocket.atEof()
|
|
|
|
bytes == concat(data, data1)
|
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
asyncTest "Socket should ignore data past eof packet":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
let data = @[1'u8, 2'u8, 3'u8]
|
|
|
|
let data1 = @[4'u8, 5'u8, 6'u8]
|
|
|
|
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
|
|
|
let readF = outgoingSocket.read()
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
let dataP =
|
2021-12-09 09:52:21 +00:00
|
|
|
dataPacket(
|
2021-12-20 12:14:50 +00:00
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
|
|
|
data,
|
2021-12-09 09:52:21 +00:00
|
|
|
0
|
|
|
|
)
|
2021-11-09 14:29:59 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
let finP =
|
2021-12-09 09:52:21 +00:00
|
|
|
finPacket(
|
2021-12-20 12:14:50 +00:00
|
|
|
initialRemoteSeq + 1,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
2021-12-09 09:52:21 +00:00
|
|
|
0
|
|
|
|
)
|
2021-11-09 14:29:59 +00:00
|
|
|
|
|
|
|
# dataP1 has seqNr larger than fin, there fore it should be considered past eof and never passed
|
|
|
|
# to user of library
|
2021-12-20 12:14:50 +00:00
|
|
|
let dataP1 =
|
2021-12-09 09:52:21 +00:00
|
|
|
dataPacket(
|
2021-12-20 12:14:50 +00:00
|
|
|
initialRemoteSeq + 2,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
|
|
|
data1,
|
2021-12-09 09:52:21 +00:00
|
|
|
0
|
|
|
|
)
|
2021-11-09 14:29:59 +00:00
|
|
|
|
|
|
|
await outgoingSocket.processPacket(finP)
|
|
|
|
|
|
|
|
check:
|
|
|
|
not readF.finished()
|
|
|
|
not outgoingSocket.atEof()
|
|
|
|
|
|
|
|
# it is out of order dataP1 (as we still not processed dataP packet)
|
|
|
|
await outgoingSocket.processPacket(dataP1)
|
|
|
|
|
|
|
|
check:
|
|
|
|
not readF.finished()
|
|
|
|
not outgoingSocket.atEof()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
await outgoingSocket.processPacket(dataP)
|
|
|
|
|
|
|
|
# it is in order dataP1, as we have now processed dataP + fin which came before
|
|
|
|
# but it is past eof so it should be ignored
|
|
|
|
await outgoingSocket.processPacket(dataP1)
|
|
|
|
|
|
|
|
let bytes = await readF
|
|
|
|
|
|
|
|
check:
|
|
|
|
readF.finished()
|
|
|
|
outgoingSocket.atEof()
|
|
|
|
bytes == concat(data)
|
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
asyncTest "Calling close should send fin packet":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
2021-12-02 14:46:18 +00:00
|
|
|
outgoingSocket.close()
|
2021-11-09 14:29:59 +00:00
|
|
|
|
|
|
|
let sendFin = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
sendFin.header.pType == ST_FIN
|
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
asyncTest "Receiving ack for fin packet should destroy socket":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
2021-12-02 14:46:18 +00:00
|
|
|
outgoingSocket.close()
|
2021-11-09 14:29:59 +00:00
|
|
|
|
|
|
|
let sendFin = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
sendFin.header.pType == ST_FIN
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
let responseAck =
|
2021-12-09 09:52:21 +00:00
|
|
|
ackPacket(
|
2021-12-20 12:14:50 +00:00
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
sendFin.header.seqNr,
|
2021-12-09 09:52:21 +00:00
|
|
|
testBufferSize,
|
|
|
|
0
|
|
|
|
)
|
2021-11-09 14:29:59 +00:00
|
|
|
|
|
|
|
await outgoingSocket.processPacket(responseAck)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2022-02-24 17:22:44 +00:00
|
|
|
await waitUntil(proc (): bool = not outgoingSocket.isConnected())
|
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
check:
|
|
|
|
not outgoingSocket.isConnected()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
2021-11-09 14:29:59 +00:00
|
|
|
|
|
|
|
asyncTest "Trying to write data onto closed socket should return error":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
|
|
|
let writeResult = await outgoingSocket.write(@[1'u8])
|
|
|
|
|
|
|
|
check:
|
|
|
|
writeResult.isErr()
|
|
|
|
|
|
|
|
let error = writeResult.error()
|
|
|
|
|
|
|
|
check:
|
|
|
|
error.kind == SocketNotWriteable
|
|
|
|
error.currentState == Destroy
|
|
|
|
|
|
|
|
asyncTest "Trying to write data onto closed socket which sent fin":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
2021-12-02 14:46:18 +00:00
|
|
|
outgoingSocket.close()
|
2021-11-09 14:29:59 +00:00
|
|
|
|
|
|
|
let writeResult = await outgoingSocket.write(@[1'u8])
|
|
|
|
|
|
|
|
check:
|
|
|
|
writeResult.isErr()
|
|
|
|
|
|
|
|
let error = writeResult.error()
|
|
|
|
|
|
|
|
check:
|
|
|
|
error.kind == FinSent
|
2021-11-12 09:58:49 +00:00
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
2021-11-12 09:58:49 +00:00
|
|
|
asyncTest "Processing data packet should update window size accordingly and use it in all send packets":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeqNr = 10'u16
|
|
|
|
let initialRcvBufferSize = 10'u32
|
|
|
|
let data = @[1'u8, 2'u8, 3'u8]
|
|
|
|
let sCfg = SocketConfig.init(optRcvBuffer = initialRcvBufferSize)
|
2021-12-02 14:46:18 +00:00
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeqNr, q, testBufferSize, sCfg)
|
2021-11-12 09:58:49 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
let dataP1 =
|
2021-12-09 09:52:21 +00:00
|
|
|
dataPacket(
|
2021-12-20 12:14:50 +00:00
|
|
|
initialRemoteSeqNr,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
2021-12-09 09:52:21 +00:00
|
|
|
data,
|
|
|
|
0
|
|
|
|
)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-12 09:58:49 +00:00
|
|
|
await outgoingSocket.processPacket(dataP1)
|
|
|
|
|
|
|
|
let ack1 = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
ack1.header.pType == ST_STATE
|
|
|
|
ack1.header.ackNr == initialRemoteSeqNr
|
|
|
|
ack1.header.wndSize == initialRcvBufferSize - uint32(len(data))
|
|
|
|
|
|
|
|
let written = await outgoingSocket.write(data)
|
|
|
|
|
|
|
|
let sentData = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
sentData.header.pType == ST_DATA
|
|
|
|
sentData.header.wndSize == initialRcvBufferSize - uint32(len(data))
|
|
|
|
|
2021-12-02 14:46:18 +00:00
|
|
|
outgoingSocket.close()
|
2021-11-12 09:58:49 +00:00
|
|
|
|
|
|
|
let sentFin = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
sentFin.header.pType == ST_FIN
|
|
|
|
sentFin.header.wndSize == initialRcvBufferSize - uint32(len(data))
|
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
asyncTest "Reading data from the buffer should increase receive window":
|
2021-11-12 09:58:49 +00:00
|
|
|
let q = newAsyncQueue[Packet]()
|
2022-11-16 16:44:00 +00:00
|
|
|
let initialRemoteSeqNr = 10'u16
|
2021-11-12 09:58:49 +00:00
|
|
|
let initialRcvBufferSize = 10'u32
|
|
|
|
let data = @[1'u8, 2'u8, 3'u8]
|
|
|
|
let sCfg = SocketConfig.init(optRcvBuffer = initialRcvBufferSize)
|
2022-11-16 16:44:00 +00:00
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeqNr, q, testBufferSize, sCfg)
|
2021-11-12 09:58:49 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
let dataP1 =
|
2021-12-09 09:52:21 +00:00
|
|
|
dataPacket(
|
2022-11-16 16:44:00 +00:00
|
|
|
initialRemoteSeqNr,
|
2021-12-20 12:14:50 +00:00
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
|
|
|
data,
|
2021-12-09 09:52:21 +00:00
|
|
|
0
|
|
|
|
)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-12 09:58:49 +00:00
|
|
|
await outgoingSocket.processPacket(dataP1)
|
|
|
|
|
|
|
|
let ack1 = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
ack1.header.pType == ST_STATE
|
2022-11-16 16:44:00 +00:00
|
|
|
ack1.header.ackNr == initialRemoteSeqNr
|
2021-11-12 09:58:49 +00:00
|
|
|
ack1.header.wndSize == initialRcvBufferSize - uint32(len(data))
|
|
|
|
|
|
|
|
let readData = await outgoingSocket.read(data.len())
|
|
|
|
|
|
|
|
check:
|
|
|
|
readData == data
|
|
|
|
|
|
|
|
discard await outgoingSocket.write(data)
|
|
|
|
|
|
|
|
let sentData = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
sentData.header.pType == ST_DATA
|
|
|
|
# we have read all data from rcv buffer, advertised window should go back to
|
|
|
|
# initial size
|
|
|
|
sentData.header.wndSize == initialRcvBufferSize
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
2021-11-15 10:32:00 +00:00
|
|
|
|
|
|
|
asyncTest "Socket should ignore packets with bad ack number":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
let data1 = @[1'u8, 2'u8, 3'u8]
|
|
|
|
let data2 = @[4'u8, 5'u8, 6'u8]
|
|
|
|
let data3 = @[7'u8, 7'u8, 9'u8]
|
|
|
|
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
|
|
|
# data packet with ack nr set above our seq nr i.e packet from the future
|
2021-12-20 12:14:50 +00:00
|
|
|
let dataFuture =
|
2021-12-09 09:52:21 +00:00
|
|
|
dataPacket(
|
2021-12-20 12:14:50 +00:00
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr + 1,
|
|
|
|
testBufferSize,
|
|
|
|
data1,
|
2021-12-09 09:52:21 +00:00
|
|
|
0
|
|
|
|
)
|
2021-11-15 10:32:00 +00:00
|
|
|
# data packet wth ack number set below out ack window i.e packet too old
|
2021-12-20 12:14:50 +00:00
|
|
|
let dataTooOld =
|
2021-12-09 09:52:21 +00:00
|
|
|
dataPacket(
|
2021-12-20 12:14:50 +00:00
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr - allowedAckWindow - 1,
|
2021-12-09 09:52:21 +00:00
|
|
|
testBufferSize,
|
2021-12-20 12:14:50 +00:00
|
|
|
data2,
|
2021-12-09 09:52:21 +00:00
|
|
|
0
|
|
|
|
)
|
2021-11-15 10:32:00 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
let dataOk =
|
2021-12-09 09:52:21 +00:00
|
|
|
dataPacket(
|
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
2021-12-20 12:14:50 +00:00
|
|
|
testBufferSize,
|
2021-12-09 09:52:21 +00:00
|
|
|
data3,
|
|
|
|
0
|
|
|
|
)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-15 10:32:00 +00:00
|
|
|
await outgoingSocket.processPacket(dataFuture)
|
|
|
|
await outgoingSocket.processPacket(dataTooOld)
|
|
|
|
await outgoingSocket.processPacket(dataOk)
|
|
|
|
|
|
|
|
let receivedBytes = await outgoingSocket.read(data3.len)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# data1 and data2 were sent in bad packets we should only receive data3
|
|
|
|
receivedBytes == data3
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await outgoingSocket.destroyWait()
|
2021-11-24 16:49:13 +00:00
|
|
|
|
|
|
|
asyncTest "Writing data should increase current bytes window":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
let dataToWrite = @[1'u8, 2, 3, 4, 5]
|
|
|
|
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
|
|
|
discard await outgoingSocket.write(dataToWrite)
|
|
|
|
|
|
|
|
check:
|
|
|
|
int(outgoingSocket.numOfBytesInFlight) == len(dataToWrite)
|
|
|
|
|
|
|
|
discard await outgoingSocket.write(dataToWrite)
|
|
|
|
|
|
|
|
check:
|
|
|
|
int(outgoingSocket.numOfBytesInFlight) == len(dataToWrite) + len(dataToWrite)
|
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
|
|
|
asyncTest "Acking data packet should decrease current bytes window":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
let dataToWrite = @[1'u8, 2, 3, 4, 5]
|
|
|
|
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
|
|
|
discard await outgoingSocket.write(dataToWrite)
|
|
|
|
|
|
|
|
let sentPacket = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
int(outgoingSocket.numOfBytesInFlight) == len(dataToWrite)
|
|
|
|
|
|
|
|
|
|
|
|
discard await outgoingSocket.write(dataToWrite)
|
|
|
|
|
|
|
|
check:
|
|
|
|
int(outgoingSocket.numOfBytesInFlight) == len(dataToWrite) + len(dataToWrite)
|
|
|
|
|
2021-12-09 09:52:21 +00:00
|
|
|
let responseAck =
|
|
|
|
ackPacket(
|
|
|
|
initialRemoteSeq,
|
2021-12-20 12:14:50 +00:00
|
|
|
initialPacket.header.connectionId,
|
|
|
|
sentPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
2021-12-09 09:52:21 +00:00
|
|
|
0
|
|
|
|
)
|
2021-11-24 16:49:13 +00:00
|
|
|
|
|
|
|
await outgoingSocket.processPacket(responseAck)
|
|
|
|
|
2022-02-24 17:22:44 +00:00
|
|
|
await waitUntil(proc (): bool = int(outgoingSocket.numOfBytesInFlight) == len(dataToWrite))
|
|
|
|
|
2021-11-24 16:49:13 +00:00
|
|
|
check:
|
|
|
|
# only first packet has been acked so there should still by 5 bytes left
|
|
|
|
int(outgoingSocket.numOfBytesInFlight) == len(dataToWrite)
|
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
|
|
|
asyncTest "Timeout packets should decrease bytes window":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
let dataToWrite = @[1'u8, 2, 3]
|
|
|
|
let dataToWrite1 = @[6'u8, 7, 8, 9, 10]
|
|
|
|
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
|
|
|
discard await outgoingSocket.write(dataToWrite)
|
|
|
|
|
|
|
|
let sentPacket = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
int(outgoingSocket.numOfBytesInFlight) == len(dataToWrite)
|
|
|
|
|
|
|
|
|
|
|
|
discard await outgoingSocket.write(dataToWrite1)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-24 16:49:13 +00:00
|
|
|
let sentPacket1 = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
int(outgoingSocket.numOfBytesInFlight) == len(dataToWrite) + len(dataToWrite1)
|
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
# after timeout oldest packet will be immediately re-sent
|
2021-11-24 16:49:13 +00:00
|
|
|
let reSentFirstPacket = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
reSentFirstPacket.payload == sentPacket.payload
|
|
|
|
|
|
|
|
# first packet has been re-sent so its payload still counts to bytes in flight
|
|
|
|
# second packet has been marked as missing, therefore its bytes are not counting
|
|
|
|
# to bytes in flight
|
|
|
|
int(outgoingSocket.numOfBytesInFlight) == len(dataToWrite)
|
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|
2021-12-02 14:46:18 +00:00
|
|
|
|
|
|
|
asyncTest "Writing data should asynchronously block until there is enough space in snd buffer":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
2022-06-17 20:45:37 +00:00
|
|
|
let dataToWrite = rng[].generateBytes(1001)
|
2021-12-02 14:46:18 +00:00
|
|
|
|
|
|
|
# remote is initialized with buffer to small to handle whole payload
|
2022-02-24 17:22:44 +00:00
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q, cfg = SocketConfig.init(optSndBuffer = 1000))
|
2021-12-02 14:46:18 +00:00
|
|
|
|
|
|
|
let writeFut = outgoingSocket.write(dataToWrite)
|
|
|
|
|
|
|
|
# wait some time to check future is not finished
|
|
|
|
await sleepAsync(seconds(2))
|
|
|
|
|
2022-02-24 17:22:44 +00:00
|
|
|
# write is not finished as future is blocked from progressing due to to full
|
|
|
|
# send buffer
|
2021-12-02 14:46:18 +00:00
|
|
|
check:
|
|
|
|
not writeFut.finished()
|
|
|
|
|
2021-12-09 09:52:21 +00:00
|
|
|
let someAckFromRemote =
|
|
|
|
ackPacket(
|
2021-12-20 12:14:50 +00:00
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
2022-02-24 17:22:44 +00:00
|
|
|
initialPacket.header.seqNr + 1,
|
|
|
|
testBufferSize,
|
2021-12-09 09:52:21 +00:00
|
|
|
0
|
|
|
|
)
|
2021-12-02 14:46:18 +00:00
|
|
|
|
|
|
|
await outgoingSocket.processPacket(someAckFromRemote)
|
|
|
|
|
2022-02-24 17:22:44 +00:00
|
|
|
# only after processing ack write will progress
|
|
|
|
let writeResult = await writeFut
|
2021-12-02 14:46:18 +00:00
|
|
|
|
|
|
|
check:
|
2022-07-21 08:58:17 +00:00
|
|
|
writeResult.isOk()
|
2021-12-02 14:46:18 +00:00
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
|
|
|
asyncTest "Writing data should not progress in case of timeouting packets and small snd window":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
let dataToWrite = 1160
|
2021-12-02 14:46:18 +00:00
|
|
|
# remote is initialized with buffer to small to handle whole payload
|
2022-02-24 17:22:44 +00:00
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q, cfg = SocketConfig.init(optSndBuffer = 1160))
|
2021-12-02 14:46:18 +00:00
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
let twoPacketData = rng[].generateBytes(int(dataToWrite))
|
2021-12-02 14:46:18 +00:00
|
|
|
|
2022-02-24 17:22:44 +00:00
|
|
|
let writeResult = await outgoingSocket.write(twoPacketData)
|
|
|
|
|
|
|
|
check:
|
|
|
|
writeResult.isOk()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2022-02-24 17:22:44 +00:00
|
|
|
# this write will not progress as snd buffer is full
|
2021-12-02 14:46:18 +00:00
|
|
|
let writeFut = outgoingSocket.write(twoPacketData)
|
|
|
|
|
2022-02-24 17:22:44 +00:00
|
|
|
# we wait for packets to timeout
|
2021-12-02 14:46:18 +00:00
|
|
|
await sleepAsync(seconds(2))
|
|
|
|
|
|
|
|
check:
|
|
|
|
not writeFut.finished()
|
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
|
|
|
asyncTest "Writing data should respect remote rcv window size":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
let dataToWrite = @[1'u8, 2, 3, 4, 5]
|
|
|
|
|
|
|
|
# remote is initialized with buffer to small to handle whole payload
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
let remoteRcvWindowSize = uint32(outgoingSocket.getPacketSize())
|
2021-12-20 12:14:50 +00:00
|
|
|
let someAckFromRemote =
|
2021-12-09 09:52:21 +00:00
|
|
|
ackPacket(
|
2021-12-20 12:14:50 +00:00
|
|
|
initialRemoteSeq,
|
2021-12-09 09:52:21 +00:00
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
remoteRcvWindowSize,
|
|
|
|
0
|
|
|
|
)
|
2021-12-02 14:46:18 +00:00
|
|
|
|
|
|
|
# we are using ack from remote to setup our snd window size to one packet size on one packet
|
|
|
|
await outgoingSocket.processPacket(someAckFromRemote)
|
|
|
|
|
2022-06-17 20:45:37 +00:00
|
|
|
let twoPacketData = rng[].generateBytes(int(2 * remoteRcvWindowSize))
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-02 14:46:18 +00:00
|
|
|
let writeFut = outgoingSocket.write(twoPacketData)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
let firstAckFromRemote =
|
2021-12-09 09:52:21 +00:00
|
|
|
ackPacket(
|
|
|
|
initialRemoteSeq,
|
2021-12-20 12:14:50 +00:00
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr + 1,
|
|
|
|
remoteRcvWindowSize,
|
2021-12-09 09:52:21 +00:00
|
|
|
0
|
|
|
|
)
|
2021-12-02 14:46:18 +00:00
|
|
|
|
|
|
|
let packet = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
packet.header.pType == ST_DATA
|
|
|
|
uint32(len(packet.payload)) == remoteRcvWindowSize
|
2022-02-24 17:22:44 +00:00
|
|
|
|
|
|
|
let packet1Fut = q.get()
|
|
|
|
|
|
|
|
await sleepAsync(milliseconds(500))
|
|
|
|
|
|
|
|
check:
|
|
|
|
not packet1Fut.finished()
|
2021-12-02 14:46:18 +00:00
|
|
|
|
|
|
|
await outgoingSocket.processPacket(firstAckFromRemote)
|
|
|
|
|
2022-02-24 17:22:44 +00:00
|
|
|
# packet is sent only after first packet is acked
|
|
|
|
let packet1 = await packet1Fut
|
2021-12-02 14:46:18 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
packet1.header.pType == ST_DATA
|
2022-02-24 17:22:44 +00:00
|
|
|
packet1.header.seqNr == packet.header.seqNr + 1
|
2021-12-02 14:46:18 +00:00
|
|
|
writeFut.finished
|
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
asyncTest "Remote window should be reset to minimal value after configured amount of time":
|
2021-12-02 14:46:18 +00:00
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
let someData = @[1'u8]
|
2021-12-20 12:14:50 +00:00
|
|
|
let (outgoingSocket, packet) =
|
2021-12-02 14:46:18 +00:00
|
|
|
connectOutGoingSocket(
|
|
|
|
initialRemoteSeq,
|
|
|
|
q,
|
2021-12-20 12:14:50 +00:00
|
|
|
remoteReceiveBuffer = 0,
|
2021-12-02 14:46:18 +00:00
|
|
|
cfg = SocketConfig.init(
|
|
|
|
remoteWindowResetTimeout = seconds(3)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
check:
|
|
|
|
outgoingSocket.isConnected()
|
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
# write result will be successful as send buffer has space
|
2022-02-24 17:22:44 +00:00
|
|
|
let writeResult = await outgoingSocket.write(someData)
|
2022-06-17 20:45:37 +00:00
|
|
|
|
2022-02-24 17:22:44 +00:00
|
|
|
# this will finish in seconds(3) as only after this time window will be set to min value
|
2021-12-02 14:46:18 +00:00
|
|
|
let p = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
writeResult.isOk()
|
|
|
|
p.payload == someData
|
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|
2021-12-02 15:51:44 +00:00
|
|
|
|
|
|
|
asyncTest "Writing data should respect max snd buffer option":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
let someData1 = @[1'u8]
|
2021-12-20 12:14:50 +00:00
|
|
|
let someData2 = @[2'u8]
|
|
|
|
let (outgoingSocket, initialPacket) =
|
2021-12-02 15:51:44 +00:00
|
|
|
connectOutGoingSocket(
|
|
|
|
initialRemoteSeq,
|
|
|
|
q,
|
|
|
|
cfg = SocketConfig.init(
|
|
|
|
optSndBuffer = 1
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
check:
|
|
|
|
outgoingSocket.isConnected()
|
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
# snd buffer got 1 byte of space so this future should finish
|
2021-12-02 15:51:44 +00:00
|
|
|
let write1 = await outgoingSocket.write(someData1)
|
|
|
|
|
|
|
|
let writeFut2 = outgoingSocket.write(someData2)
|
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
# wait until 2 re-sends to check we do not accidentally free buffer during re-sends
|
2021-12-02 15:51:44 +00:00
|
|
|
discard await q.get()
|
|
|
|
discard await q.get()
|
|
|
|
let firstPacket = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
# this write still cannot progress as 1st write is not acked
|
|
|
|
not writeFut2.finished()
|
|
|
|
|
2021-12-09 09:52:21 +00:00
|
|
|
let someAckFromRemote =
|
|
|
|
ackPacket(
|
2021-12-20 12:14:50 +00:00
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr + 1,
|
|
|
|
10,
|
2021-12-09 09:52:21 +00:00
|
|
|
0
|
|
|
|
)
|
2021-12-02 15:51:44 +00:00
|
|
|
|
|
|
|
# acks first write, so there is space in buffer for new data and second
|
|
|
|
# write should progress
|
|
|
|
await outgoingSocket.processPacket(someAckFromRemote)
|
|
|
|
|
|
|
|
yield writeFut2
|
|
|
|
|
|
|
|
let secondPacket = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
writeFut2.finished()
|
|
|
|
firstPacket.payload == someData1
|
2021-12-20 12:14:50 +00:00
|
|
|
secondPacket.payload == someData2
|
2021-12-02 15:51:44 +00:00
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|
2021-12-09 09:52:21 +00:00
|
|
|
|
|
|
|
asyncTest "Socket should inform remote about its delay":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
let dataP1 =
|
2021-12-09 09:52:21 +00:00
|
|
|
dataPacket(
|
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
|
|
|
@[1'u8],
|
|
|
|
0
|
|
|
|
)
|
|
|
|
|
|
|
|
check:
|
|
|
|
outgoingSocket.isConnected()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-09 09:52:21 +00:00
|
|
|
# necessary to avoid timestampDiff near 0 and flaky tests
|
|
|
|
await sleepAsync(milliseconds(50))
|
|
|
|
await outgoingSocket.processPacket(dataP1)
|
|
|
|
|
|
|
|
let socketAck = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
socketAck.header.timestampDiff > 0
|
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|
2021-12-10 09:18:00 +00:00
|
|
|
|
|
|
|
asyncTest "Re-sent packet should have updated timestamps and ack numbers":
|
2021-12-20 12:14:50 +00:00
|
|
|
let q = newAsyncQueue[Packet]()
|
2022-11-16 16:44:00 +00:00
|
|
|
let initialRemoteSeqNr = 10'u16
|
2021-12-10 09:18:00 +00:00
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeqNr, q)
|
2021-12-10 09:18:00 +00:00
|
|
|
|
|
|
|
let writeResult = await outgoingSocket.write(@[1'u8])
|
|
|
|
|
|
|
|
check:
|
|
|
|
writeResult.isOk()
|
|
|
|
|
|
|
|
let firstSend = await q.get()
|
|
|
|
|
|
|
|
let secondSend = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
# there was sometime between resend but no packet from remote
|
|
|
|
# so timestamp should be updated but not ackNr
|
|
|
|
secondSend.header.timestamp > firstSend.header.timestamp
|
|
|
|
firstSend.header.ackNr == secondSend.header.ackNr
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
let dataP1 =
|
2021-12-10 09:18:00 +00:00
|
|
|
dataPacket(
|
2022-11-16 16:44:00 +00:00
|
|
|
initialRemoteSeqNr,
|
2021-12-10 09:18:00 +00:00
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
|
|
|
@[1'u8],
|
|
|
|
0
|
|
|
|
)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-10 09:18:00 +00:00
|
|
|
await outgoingSocket.processPacket(dataP1)
|
|
|
|
|
2022-01-26 08:49:34 +00:00
|
|
|
let fastResend = await q.get()
|
|
|
|
|
2021-12-10 09:18:00 +00:00
|
|
|
let ack = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
ack.header.pType == ST_STATE
|
|
|
|
|
|
|
|
let thirdSend = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
# as there was some incoming data between resend, both timestamp and ackNr
|
|
|
|
# should be updated
|
|
|
|
thirdSend.header.timestamp > secondSend.header.timestamp
|
|
|
|
thirdSend.header.ackNr > secondSend.header.ackNr
|
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|
2022-01-26 08:49:34 +00:00
|
|
|
|
|
|
|
asyncTest "Should support fast timeout ":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
# small writes to make sure it will be 3 different packets
|
|
|
|
let dataToWrite1 = @[1'u8]
|
|
|
|
let dataToWrite2 = @[1'u8]
|
|
|
|
let dataToWrite3 = @[1'u8]
|
|
|
|
|
|
|
|
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
|
|
|
let writeRes1 = await outgoingSocket.write(dataToWrite1)
|
|
|
|
let writeRes2 = await outgoingSocket.write(dataToWrite2)
|
2022-06-17 20:45:37 +00:00
|
|
|
let writeRes3 = await outgoingSocket.write(dataToWrite3)
|
2022-01-26 08:49:34 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
writeRes1.isOk()
|
|
|
|
writeRes2.isOk()
|
|
|
|
writeRes3.isOk()
|
|
|
|
|
|
|
|
# drain queue of all sent packets
|
|
|
|
let sent1 = await q.get()
|
|
|
|
let sent2 = await q.get()
|
|
|
|
let sent3 = await q.get()
|
|
|
|
|
|
|
|
# wait for first timeout. Socket will enter fast timeout mode
|
|
|
|
let reSent1 = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
# check that re-sent packet is the oldest one
|
|
|
|
reSent1.payload == sent1.payload
|
|
|
|
reSent1.header.seqNr == sent1.header.seqNr
|
|
|
|
|
|
|
|
# ack which will ack our re-sent packet
|
|
|
|
let responseAck =
|
|
|
|
ackPacket(
|
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
reSent1.header.seqNr,
|
|
|
|
testBufferSize,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
|
|
|
|
await outgoingSocket.processPacket(responseAck)
|
|
|
|
|
|
|
|
let fastResentPacket = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
# second packet is now oldest unacked packet so it should be the one which
|
|
|
|
# is send during fast resend
|
|
|
|
fastResentPacket.payload == sent2.payload
|
|
|
|
fastResentPacket.header.seqNr == sent2.header.seqNr
|
|
|
|
|
|
|
|
# duplicate ack, processing it should not fast-resend any packet
|
|
|
|
await outgoingSocket.processPacket(responseAck)
|
|
|
|
|
|
|
|
let resent3 = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
# in next timeout cycle packet nr3 is the only one waiting for re-send
|
|
|
|
resent3.payload == sent3.payload
|
|
|
|
resent3.header.seqNr == sent3.header.seqNr
|
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|
2022-03-18 07:13:17 +00:00
|
|
|
|
|
|
|
asyncTest "Socket should accept data only in connected state":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
let cfg = SocketConfig.init()
|
2022-11-16 16:44:00 +00:00
|
|
|
let remoteReceiveBuffer = 1024'u32
|
2022-03-18 07:13:17 +00:00
|
|
|
|
|
|
|
let dataDropped = @[1'u8]
|
2022-11-16 16:44:00 +00:00
|
|
|
let dataReceived = @[2'u8]
|
2022-03-18 07:13:17 +00:00
|
|
|
|
|
|
|
let sock1 = newOutgoingSocket[TransportAddress](testAddress, initTestSnd(q), cfg, defaultRcvOutgoingId, rng[])
|
|
|
|
|
|
|
|
asyncSpawn sock1.startOutgoingSocket()
|
|
|
|
|
|
|
|
let initialPacket = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
initialPacket.header.pType == ST_SYN
|
|
|
|
|
|
|
|
let dpDropped = dataPacket(
|
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
|
|
|
dataDropped,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
|
|
|
|
let dpReceived = dataPacket(
|
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
testBufferSize,
|
2022-11-16 16:44:00 +00:00
|
|
|
dataReceived,
|
2022-03-18 07:13:17 +00:00
|
|
|
0
|
|
|
|
)
|
|
|
|
|
|
|
|
let responseAck =
|
|
|
|
ackPacket(
|
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
2022-11-16 16:44:00 +00:00
|
|
|
remoteReceiveBuffer,
|
2022-03-18 07:13:17 +00:00
|
|
|
0
|
|
|
|
)
|
|
|
|
|
|
|
|
# even though @[1'u8] is received first, it should be dropped as socket is not
|
|
|
|
# yet in connected state
|
|
|
|
await sock1.processPacket(dpDropped)
|
|
|
|
await sock1.processPacket(responseAck)
|
|
|
|
await sock1.processPacket(dpReceived)
|
|
|
|
|
|
|
|
let receivedData = await sock1.read(1)
|
|
|
|
|
|
|
|
check:
|
2022-11-16 16:44:00 +00:00
|
|
|
receivedData == dataReceived
|
2022-03-18 07:13:17 +00:00
|
|
|
|
|
|
|
await sock1.destroyWait()
|
2022-03-28 10:35:08 +00:00
|
|
|
|
|
|
|
asyncTest "Clean up all resources when closing due to timeout failure":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
let dataToWrite = @[1'u8]
|
|
|
|
let customCfg = SocketConfig.init(dataResendsBeforeFailure = 2, optSndBuffer = 1)
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q, cfg = customCfg)
|
|
|
|
|
|
|
|
let bytesWritten = await outgoingSocket.write(dataToWrite)
|
|
|
|
# this future will never finish as there is not place in write buffer
|
2022-11-16 16:44:00 +00:00
|
|
|
# although it should get properly cleared up when socket is closed
|
2022-03-28 10:35:08 +00:00
|
|
|
let writeFut = outgoingSocket.write(dataToWrite)
|
|
|
|
|
|
|
|
check:
|
|
|
|
bytesWritten.get() == len(dataToWrite)
|
|
|
|
|
|
|
|
let sentPacket = await q.get()
|
|
|
|
# wait for failure and cleanup of all resources
|
|
|
|
await waitUntil(proc (): bool = outgoingSocket.isClosedAndCleanedUpAllResources())
|
|
|
|
check:
|
|
|
|
# main event loop handler should fire and clean up dangling future
|
|
|
|
writeFut.finished()
|
|
|
|
await outgoingSocket.destroyWait()
|
|
|
|
|
|
|
|
asyncTest "Receiving ack for fin packet should destroy socket and clean up all resources":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
|
|
|
let initialRemoteSeq = 10'u16
|
|
|
|
|
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeq, q)
|
|
|
|
|
|
|
|
outgoingSocket.close()
|
|
|
|
|
|
|
|
let sendFin = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
sendFin.header.pType == ST_FIN
|
|
|
|
|
|
|
|
let responseAck =
|
|
|
|
ackPacket(
|
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
sendFin.header.seqNr,
|
|
|
|
testBufferSize,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
|
|
|
|
await outgoingSocket.processPacket(responseAck)
|
|
|
|
|
|
|
|
await waitUntil(proc (): bool = not outgoingSocket.isConnected())
|
|
|
|
|
|
|
|
check:
|
|
|
|
not outgoingSocket.isConnected()
|
|
|
|
|
|
|
|
await waitUntil(proc (): bool = outgoingSocket.isClosedAndCleanedUpAllResources())
|
|
|
|
|
|
|
|
check:
|
|
|
|
outgoingSocket.isClosedAndCleanedUpAllResources()
|
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|
2022-04-04 11:44:32 +00:00
|
|
|
|
|
|
|
asyncTest "Maximum payload size should be configurable":
|
|
|
|
let q = newAsyncQueue[Packet]()
|
2022-11-16 16:44:00 +00:00
|
|
|
let initialRemoteSeqNr = 10'u16
|
2022-06-17 20:45:37 +00:00
|
|
|
let d = rng[].generateBytes(5000)
|
2022-04-04 11:44:32 +00:00
|
|
|
let maxPayloadSize = 800'u32
|
|
|
|
let config = SocketConfig.init(payloadSize = maxPayloadSize)
|
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
let (outgoingSocket, initialPacket) = connectOutGoingSocket(initialRemoteSeqNr, q, cfg = config)
|
2022-04-04 11:44:32 +00:00
|
|
|
|
|
|
|
let wr = await outgoingSocket.write(d)
|
|
|
|
|
|
|
|
check:
|
|
|
|
wr.isOk()
|
|
|
|
|
|
|
|
# Initial max window should allow for at least 2 packets to go through
|
|
|
|
let dp1 = await q.get()
|
|
|
|
let dp2 = await q.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
dp1.header.pType == ST_DATA
|
|
|
|
len(dp1.payload) == int(maxPayloadSize)
|
|
|
|
dp2.header.pType == ST_DATA
|
|
|
|
len(dp2.payload) == int(maxPayloadSize)
|
|
|
|
|
|
|
|
await outgoingSocket.destroyWait()
|