2021-11-04 06:38:46 +00:00
|
|
|
import
|
|
|
|
chronos,
|
2022-01-04 08:52:38 +00:00
|
|
|
../../eth/utp/utp_socket,
|
|
|
|
../../eth/utp/packets,
|
2021-12-11 18:12:55 +00:00
|
|
|
../../eth/keys
|
2021-11-04 06:38:46 +00:00
|
|
|
|
2023-05-10 13:50:04 +00:00
|
|
|
type AssertionCallback = proc(): bool {.gcsafe, raises: [].}
|
2021-11-04 06:38:46 +00:00
|
|
|
|
2022-11-30 08:34:08 +00:00
|
|
|
let testBufferSize* = 1024'u32
|
2022-01-04 08:52:38 +00:00
|
|
|
let defaultRcvOutgoingId = 314'u16
|
|
|
|
|
2021-11-04 06:38:46 +00:00
|
|
|
proc waitUntil*(f: AssertionCallback): Future[void] {.async.} =
|
|
|
|
while true:
|
|
|
|
let res = f()
|
|
|
|
if res:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
await sleepAsync(milliseconds(50))
|
2022-01-04 08:52:38 +00:00
|
|
|
|
|
|
|
template connectOutGoingSocket*(
|
|
|
|
initialRemoteSeq: uint16,
|
|
|
|
q: AsyncQueue[Packet],
|
|
|
|
remoteReceiveBuffer: uint32 = testBufferSize,
|
|
|
|
cfg: SocketConfig = SocketConfig.init()): (UtpSocket[TransportAddress], Packet) =
|
|
|
|
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 responseAck =
|
|
|
|
ackPacket(
|
|
|
|
initialRemoteSeq,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
remoteReceiveBuffer,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
|
|
|
|
await sock1.processPacket(responseAck)
|
2022-02-24 17:22:44 +00:00
|
|
|
await waitUntil(proc (): bool = sock1.isConnected())
|
2022-01-04 08:52:38 +00:00
|
|
|
check:
|
|
|
|
sock1.isConnected()
|
|
|
|
|
|
|
|
(sock1, initialPacket)
|
|
|
|
|
|
|
|
template connectOutGoingSocketWithIncoming*(
|
|
|
|
initialRemoteSeq: uint16,
|
|
|
|
outgoingQueue: AsyncQueue[Packet],
|
|
|
|
incomingQueue: AsyncQueue[Packet],
|
|
|
|
remoteReceiveBuffer: uint32 = testBufferSize,
|
|
|
|
cfg: SocketConfig = SocketConfig.init()): (UtpSocket[TransportAddress], UtpSocket[TransportAddress]) =
|
|
|
|
let outgoingSocket = newOutgoingSocket[TransportAddress](testAddress, initTestSnd(outgoingQueue), cfg, defaultRcvOutgoingId, rng[])
|
|
|
|
asyncSpawn outgoingSocket.startOutgoingSocket()
|
|
|
|
let initialPacket = await outgoingQueue.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
initialPacket.header.pType == ST_SYN
|
|
|
|
|
|
|
|
let incomingSocket = newIncomingSocket[TransportAddress](
|
|
|
|
testAddress,
|
2022-06-17 20:45:37 +00:00
|
|
|
initTestSnd(incomingQueue),
|
2022-01-04 08:52:38 +00:00
|
|
|
cfg,
|
|
|
|
initialPacket.header.connectionId,
|
|
|
|
initialPacket.header.seqNr,
|
|
|
|
rng[]
|
|
|
|
)
|
|
|
|
|
2022-02-24 17:22:44 +00:00
|
|
|
incomingSocket.startIncomingSocket()
|
2022-01-04 08:52:38 +00:00
|
|
|
|
|
|
|
let responseAck = await incomingQueue.get()
|
|
|
|
|
|
|
|
await outgoingSocket.processPacket(responseAck)
|
2022-06-17 20:45:37 +00:00
|
|
|
|
2022-02-24 17:22:44 +00:00
|
|
|
await waitUntil(proc (): bool = outgoingSocket.isConnected())
|
2022-06-17 20:45:37 +00:00
|
|
|
|
2022-01-04 08:52:38 +00:00
|
|
|
check:
|
|
|
|
outgoingSocket.isConnected()
|
|
|
|
|
|
|
|
(outgoingSocket, incomingSocket)
|
|
|
|
|
|
|
|
|
|
|
|
proc generateDataPackets*(
|
|
|
|
numberOfPackets: uint16,
|
|
|
|
initialSeqNr: uint16,
|
|
|
|
connectionId: uint16,
|
|
|
|
ackNr: uint16,
|
2022-06-17 20:45:37 +00:00
|
|
|
rng: var HmacDrbgContext): seq[Packet] =
|
2022-01-04 08:52:38 +00:00
|
|
|
let packetSize = 100
|
|
|
|
var packets = newSeq[Packet]()
|
|
|
|
var i = 0'u16
|
|
|
|
while i < numberOfPackets:
|
|
|
|
let packet = dataPacket(
|
|
|
|
initialSeqNr + i,
|
|
|
|
connectionId,
|
|
|
|
ackNr,
|
|
|
|
testBufferSize,
|
2022-06-17 20:45:37 +00:00
|
|
|
rng.generateBytes(packetSize),
|
2022-01-04 08:52:38 +00:00
|
|
|
0
|
|
|
|
)
|
|
|
|
packets.add(packet)
|
|
|
|
|
|
|
|
inc i
|
|
|
|
|
|
|
|
packets
|
|
|
|
|
|
|
|
proc initTestSnd*(q: AsyncQueue[Packet]): SendCallback[TransportAddress]=
|
2024-01-23 17:41:38 +00:00
|
|
|
return (
|
|
|
|
proc (
|
|
|
|
to: TransportAddress, bytes: seq[byte]
|
|
|
|
) {.raises: [], gcsafe.} =
|
2022-01-04 08:52:38 +00:00
|
|
|
let p = decodePacket(bytes).get()
|
2024-01-23 17:41:38 +00:00
|
|
|
try:
|
|
|
|
q.addLastNoWait(p)
|
|
|
|
except AsyncQueueFullError:
|
|
|
|
raiseAssert "Should not occur as unlimited queue"
|
2022-01-04 08:52:38 +00:00
|
|
|
)
|