2024-06-18 16:09:27 +00:00
|
|
|
# Copyright (c) 2020-2024 Status Research & Development GmbH
|
2022-01-07 09:38:36 +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-07-09 08:55:15 +00:00
|
|
|
stew/byteutils,
|
2022-01-07 09:38:36 +00:00
|
|
|
unittest2,
|
|
|
|
../../eth/utp/packets,
|
|
|
|
../../eth/keys
|
|
|
|
|
2024-01-18 11:07:03 +00:00
|
|
|
suite "uTP packets test vectors":
|
2022-01-07 09:38:36 +00:00
|
|
|
test "SYN packet":
|
|
|
|
let synPacket = Packet(
|
|
|
|
header: PacketHeaderV1(
|
|
|
|
pType: ST_SYN,
|
|
|
|
version: 1,
|
|
|
|
extension: 0,
|
|
|
|
connectionId: 10049,
|
|
|
|
timestamp: 3384187322'u32,
|
|
|
|
timestampDiff: 0,
|
|
|
|
wndSize: 1048576,
|
|
|
|
seqNr: 11884,
|
|
|
|
ackNr: 0
|
|
|
|
),
|
2024-06-18 16:09:27 +00:00
|
|
|
eack: Opt.none(SelectiveAckExtension),
|
2022-01-07 09:38:36 +00:00
|
|
|
payload: @[]
|
|
|
|
)
|
|
|
|
|
|
|
|
let encodedSyn = encodePacket(synPacket)
|
2022-07-09 08:55:15 +00:00
|
|
|
|
2022-01-07 09:38:36 +00:00
|
|
|
let synBytes = hexToSeqByte("0x41002741c9b699ba00000000001000002e6c0000")
|
|
|
|
|
|
|
|
check:
|
|
|
|
encodedSyn == synBytes
|
|
|
|
|
|
|
|
test "ACK packet (No selective acks)":
|
|
|
|
let ackPacket = Packet(
|
|
|
|
header: PacketHeaderV1(
|
|
|
|
pType: ST_STATE,
|
|
|
|
version: 1,
|
|
|
|
extension: 0,
|
|
|
|
connectionId: 10049,
|
|
|
|
timestamp: 6195294,
|
|
|
|
timestampDiff: 916973699,
|
|
|
|
wndSize: 1048576,
|
|
|
|
seqNr: 16807,
|
|
|
|
ackNr: 11885
|
|
|
|
),
|
2024-06-18 16:09:27 +00:00
|
|
|
eack: Opt.none(SelectiveAckExtension),
|
2022-01-07 09:38:36 +00:00
|
|
|
payload: @[]
|
|
|
|
)
|
|
|
|
|
|
|
|
let encodedAck = encodePacket(ackPacket)
|
|
|
|
|
|
|
|
let ackBytes = hexToSeqByte("0x21002741005e885e36a7e8830010000041a72e6d")
|
|
|
|
|
|
|
|
check:
|
|
|
|
encodedAck == ackBytes
|
|
|
|
|
|
|
|
test "ACK packet (with selective acks)":
|
|
|
|
# bit mask with the first and last bit set
|
|
|
|
var bitMask = [1'u8, 0, 0, 128]
|
|
|
|
|
|
|
|
let ackPacket = Packet(
|
|
|
|
header: PacketHeaderV1(
|
|
|
|
pType: ST_STATE,
|
|
|
|
version: 1,
|
|
|
|
extension: 1,
|
|
|
|
connectionId: 10049,
|
|
|
|
timestamp: 6195294,
|
|
|
|
timestampDiff: 916973699,
|
|
|
|
wndSize: 1048576,
|
|
|
|
seqNr: 16807,
|
|
|
|
ackNr: 11885
|
|
|
|
),
|
2024-06-18 16:09:27 +00:00
|
|
|
eack: Opt.some(SelectiveAckExtension(
|
2022-01-07 09:38:36 +00:00
|
|
|
acks: bitMask
|
|
|
|
)),
|
|
|
|
payload: @[]
|
|
|
|
)
|
|
|
|
|
|
|
|
let encodedAck = encodePacket(ackPacket)
|
|
|
|
|
|
|
|
let ackBytes = hexToSeqByte("0x21012741005e885e36a7e8830010000041a72e6d000401000080")
|
|
|
|
|
|
|
|
check:
|
|
|
|
encodedAck == ackBytes
|
|
|
|
|
|
|
|
test "DATA packet":
|
|
|
|
let dataPacket = Packet(
|
|
|
|
header: PacketHeaderV1(
|
|
|
|
pType: ST_DATA,
|
|
|
|
version: 1,
|
|
|
|
extension: 0,
|
|
|
|
connectionId: 26237,
|
|
|
|
timestamp: 252492495'u32,
|
|
|
|
timestampDiff: 242289855,
|
|
|
|
wndSize: 1048576,
|
|
|
|
seqNr: 8334,
|
|
|
|
ackNr: 16806
|
|
|
|
),
|
2024-06-18 16:09:27 +00:00
|
|
|
eack: Opt.none(SelectiveAckExtension),
|
2022-01-07 09:38:36 +00:00
|
|
|
payload: @[0'u8, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
)
|
|
|
|
|
|
|
|
let encodedData = encodePacket(dataPacket)
|
2022-07-09 08:55:15 +00:00
|
|
|
|
2022-01-07 09:38:36 +00:00
|
|
|
let dataBytes = hexToSeqByte("0x0100667d0f0cbacf0e710cbf00100000208e41a600010203040506070809")
|
|
|
|
|
|
|
|
check:
|
|
|
|
encodedData == dataBytes
|
|
|
|
|
|
|
|
test "FIN packet":
|
|
|
|
let finPacket = Packet(
|
|
|
|
header: PacketHeaderV1(
|
|
|
|
pType: ST_FIN,
|
|
|
|
version: 1,
|
|
|
|
extension: 0,
|
|
|
|
connectionId: 19003,
|
|
|
|
timestamp: 515227279,
|
|
|
|
timestampDiff: 511481041,
|
|
|
|
wndSize: 1048576,
|
|
|
|
seqNr: 41050,
|
|
|
|
ackNr: 16806
|
|
|
|
),
|
2024-06-18 16:09:27 +00:00
|
|
|
eack: Opt.none(SelectiveAckExtension),
|
2022-01-07 09:38:36 +00:00
|
|
|
payload: @[]
|
|
|
|
)
|
|
|
|
|
|
|
|
let encodedFIn = encodePacket(finPacket)
|
|
|
|
|
|
|
|
let finBytes = hexToSeqByte("0x11004a3b1eb5be8f1e7c94d100100000a05a41a6")
|
|
|
|
|
|
|
|
check:
|
|
|
|
encodedFIn == finBytes
|
|
|
|
|
|
|
|
test "RESET packet":
|
|
|
|
let resetPacket = Packet(
|
|
|
|
header: PacketHeaderV1(
|
|
|
|
pType: ST_RESET,
|
|
|
|
version: 1,
|
|
|
|
extension: 0,
|
|
|
|
connectionId: 62285,
|
|
|
|
timestamp: 751226811,
|
|
|
|
timestampDiff: 0,
|
|
|
|
wndSize: 0,
|
|
|
|
seqNr: 55413,
|
|
|
|
ackNr: 16807
|
|
|
|
),
|
2024-06-18 16:09:27 +00:00
|
|
|
eack: Opt.none(SelectiveAckExtension),
|
2022-01-07 09:38:36 +00:00
|
|
|
payload: @[]
|
|
|
|
)
|
|
|
|
|
|
|
|
let encodedReset = encodePacket(resetPacket)
|
|
|
|
|
|
|
|
let resetBytes = hexToSeqByte("0x3100f34d2cc6cfbb0000000000000000d87541a7")
|
|
|
|
|
|
|
|
check:
|
|
|
|
encodedReset == resetBytes
|