2023-03-14 16:17:39 +00:00
|
|
|
# Copyright (c) 2020-2023 Status Research & Development GmbH
|
2021-09-13 12:54:06 +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
|
2021-12-15 12:35:17 +00:00
|
|
|
std/options,
|
2021-12-11 18:12:55 +00:00
|
|
|
unittest2,
|
2021-10-15 11:38:51 +00:00
|
|
|
../../eth/utp/packets,
|
|
|
|
../../eth/keys
|
2021-09-13 12:54:06 +00:00
|
|
|
|
2024-01-18 11:07:03 +00:00
|
|
|
suite "uTP packet encoding":
|
2022-11-10 08:01:58 +00:00
|
|
|
test "Encode/decode SYN packet":
|
2023-03-14 16:17:39 +00:00
|
|
|
let
|
|
|
|
synPacket = synPacket(5, 10, 20)
|
|
|
|
encoded = encodePacket(synPacket)
|
|
|
|
decoded = decodePacket(encoded)
|
2021-09-13 12:54:06 +00:00
|
|
|
|
|
|
|
check:
|
2021-12-15 12:35:17 +00:00
|
|
|
len(encoded) == 20
|
2021-09-13 12:54:06 +00:00
|
|
|
decoded.isOk()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-15 12:35:17 +00:00
|
|
|
let synPacketDec = decoded.get()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2023-03-14 16:17:39 +00:00
|
|
|
check synPacketDec == synPacket
|
2021-12-15 12:35:17 +00:00
|
|
|
|
2022-11-10 08:01:58 +00:00
|
|
|
test "Encode/decode FIN packet":
|
2023-03-14 16:17:39 +00:00
|
|
|
let
|
|
|
|
finPacket = finPacket(5, 10, 20, 30, 40)
|
|
|
|
encoded = encodePacket(finPacket)
|
|
|
|
decoded = decodePacket(encoded)
|
2021-12-15 12:35:17 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
len(encoded) == 20
|
|
|
|
decoded.isOk()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-15 12:35:17 +00:00
|
|
|
let finPacketDec = decoded.get()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2023-03-14 16:17:39 +00:00
|
|
|
check finPacketDec == finPacket
|
2021-12-15 12:35:17 +00:00
|
|
|
|
2022-11-10 08:01:58 +00:00
|
|
|
test "Encode/decode RESET packet":
|
2023-03-14 16:17:39 +00:00
|
|
|
let
|
|
|
|
resetPacket = resetPacket(5, 10, 20)
|
|
|
|
encoded = encodePacket(resetPacket)
|
|
|
|
decoded = decodePacket(encoded)
|
2021-12-15 12:35:17 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
len(encoded) == 20
|
|
|
|
decoded.isOk()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-15 12:35:17 +00:00
|
|
|
let resetPacketDec = decoded.get()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2023-03-14 16:17:39 +00:00
|
|
|
check resetPacketDec == resetPacket
|
2021-12-15 12:35:17 +00:00
|
|
|
|
2022-11-10 08:01:58 +00:00
|
|
|
test "Encode/decode ACK packet: without extensions":
|
2023-03-14 16:17:39 +00:00
|
|
|
let
|
|
|
|
ackPacket = ackPacket(5, 10, 20, 30, 40)
|
|
|
|
encoded = encodePacket(ackPacket)
|
|
|
|
decoded = decodePacket(encoded)
|
2021-12-15 12:35:17 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
len(encoded) == 20
|
|
|
|
decoded.isOk()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-15 12:35:17 +00:00
|
|
|
let ackPacketDec = decoded.get()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2023-03-14 16:17:39 +00:00
|
|
|
check ackPacketDec == ackPacket
|
2021-12-15 12:35:17 +00:00
|
|
|
|
2022-11-10 08:01:58 +00:00
|
|
|
test "Encode/decode ACK packet: with extensions":
|
2023-03-14 16:17:39 +00:00
|
|
|
let
|
|
|
|
bitMask: array[4, byte] = [1'u8, 2, 3, 4]
|
|
|
|
ackPacket = ackPacket(5, 10, 20, 30, 40, some(bitMask))
|
|
|
|
encoded = encodePacket(ackPacket)
|
|
|
|
decoded = decodePacket(encoded)
|
2021-12-15 12:35:17 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
len(encoded) == 26
|
|
|
|
decoded.isOk()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-15 12:35:17 +00:00
|
|
|
let ackPacketDec = decoded.get()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-15 12:35:17 +00:00
|
|
|
check:
|
|
|
|
ackPacketDec == ackPacket
|
|
|
|
ackPacketDec.eack.isSome()
|
|
|
|
|
|
|
|
test "Fail to decode packet with malformed extensions":
|
|
|
|
let bitMask: array[4, byte] = [1'u8, 2, 3, 4]
|
|
|
|
let ackPacket = ackPacket(5, 10, 20, 30, 40, some(bitMask))
|
|
|
|
|
2023-03-14 16:17:39 +00:00
|
|
|
block: # nextExtension to non zero
|
|
|
|
var encoded = encodePacket(ackPacket)
|
|
|
|
encoded[20] = 1
|
|
|
|
let err = decodePacket(encoded)
|
|
|
|
check err.isErr()
|
|
|
|
|
|
|
|
block: # len of extension to value different than 4
|
|
|
|
var encoded = encodePacket(ackPacket)
|
|
|
|
encoded[21] = 7
|
|
|
|
let err = decodePacket(encoded)
|
|
|
|
check err.isErr()
|
|
|
|
|
|
|
|
block: # delete last byte, now packet is too short
|
|
|
|
var encoded = encodePacket(ackPacket)
|
|
|
|
encoded.del(encoded.high)
|
|
|
|
let err = decodePacket(encoded)
|
|
|
|
check err.isErr()
|
|
|
|
|
|
|
|
block: # change extension field to something other than 0 or 1
|
|
|
|
var encoded = encodePacket(ackPacket)
|
|
|
|
encoded[1] = 2
|
|
|
|
let err = decodePacket(encoded)
|
|
|
|
check: err.isErr()
|
2021-09-13 12:54:06 +00:00
|
|
|
|
2022-11-10 08:01:58 +00:00
|
|
|
test "Decode STATE packet":
|
2021-09-13 12:54:06 +00:00
|
|
|
# Packet obtained by interaction with c reference implementation
|
|
|
|
let pack: array[20, uint8] = [
|
2022-11-10 08:01:58 +00:00
|
|
|
0x21'u8, 0x0, 0x15, 0x72, 0x00, 0xBA, 0x4D, 0x71, 0x0, 0x0,
|
|
|
|
0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x41, 0xA7, 0x00, 0x01]
|
2021-09-13 12:54:06 +00:00
|
|
|
let decoded = decodePacket(pack)
|
|
|
|
|
2023-03-14 16:17:39 +00:00
|
|
|
check decoded.isOk()
|
2021-09-13 12:54:06 +00:00
|
|
|
|
|
|
|
let packet = decoded.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
packet.header.pType == ST_STATE
|
|
|
|
packet.header.version == 1
|
|
|
|
packet.header.extension == 0
|
|
|
|
packet.header.connectionId == 5490
|
|
|
|
packet.header.timestamp == 12209521
|
|
|
|
packet.header.timestampDiff == 0
|
|
|
|
packet.header.wndSize == 1048576
|
|
|
|
packet.header.seqNr == 16807
|
|
|
|
packet.header.ackNr == 1
|
2021-12-10 09:18:00 +00:00
|
|
|
|
|
|
|
test "Modify timestamp of encoded packet":
|
2023-03-14 16:17:39 +00:00
|
|
|
let
|
|
|
|
synPacket = synPacket(5, 10, 20)
|
|
|
|
initialTimestamp = synPacket.header.timestamp
|
|
|
|
initialAckNr = synPacket.header.ackNr
|
|
|
|
modifiedTimeStamp = initialTimestamp + 120324
|
|
|
|
modifiedAckNr = initialAckNr + 20
|
2021-12-10 09:18:00 +00:00
|
|
|
var encoded = encodePacket(synPacket)
|
|
|
|
modifyTimeStampAndAckNr(encoded, modifiedTimeStamp, modifiedAckNr)
|
|
|
|
|
|
|
|
let decoded = decodePacket(encoded)
|
|
|
|
|
|
|
|
check:
|
|
|
|
decoded.isOk()
|
|
|
|
decoded.get().header.timestamp == modifiedTimeStamp
|
|
|
|
decoded.get().header.ackNr == modifiedAckNr
|