mirror of
https://github.com/vacp2p/nim-quic.git
synced 2026-01-09 22:08:09 -05:00
remove tests
This commit is contained in:
@@ -1,69 +0,0 @@
|
||||
import pkg/unittest2
|
||||
import std/sequtils
|
||||
import pkg/quic/transport/packets
|
||||
import pkg/quic/transport/packets/varints
|
||||
|
||||
suite "packet length":
|
||||
const destination = ConnectionId(@[3'u8, 4'u8, 5'u8])
|
||||
const source = ConnectionId(@[1'u8, 2'u8])
|
||||
const token = @[0xA'u8, 0xB'u8, 0xC'u8]
|
||||
|
||||
test "knows the length of a version negotiation packet":
|
||||
var packet = versionNegotiationPacket()
|
||||
const versions = @[1'u32, 2'u32, 3'u32]
|
||||
packet.destination = destination
|
||||
packet.source = source
|
||||
packet.negotiation.supportedVersions = versions
|
||||
check packet.len == 7 + destination.len + source.len + 4 * versions.len
|
||||
|
||||
test "knows the length of a retry packet":
|
||||
var packet = retryPacket()
|
||||
packet.destination = destination
|
||||
packet.source = source
|
||||
packet.retry.token = token
|
||||
packet.retry.integrity[0 .. 15] = repeat(0xA'u8, 16)
|
||||
check packet.len == 7 + destination.len + source.len + token.len + 16
|
||||
|
||||
test "knows the length of a handshake packet":
|
||||
var packet = handshakePacket()
|
||||
packet.destination = destination
|
||||
packet.source = source
|
||||
packet.handshake.packetnumber = 0x00BBCCDD'u32.int64
|
||||
packet.handshake.payload = repeat(0xEE'u8, 1024)
|
||||
check packet.len ==
|
||||
7 + destination.len + source.len + 1024.toVarInt.len + # packet length
|
||||
3 + # packet number
|
||||
1024 # payload
|
||||
|
||||
test "knows the length of a 0-RTT packet":
|
||||
var packet = zeroRttPacket()
|
||||
packet.destination = destination
|
||||
packet.source = source
|
||||
packet.rtt.packetnumber = 0x00BBCCDD'u32.int64
|
||||
packet.rtt.payload = repeat(0xEE'u8, 1024)
|
||||
check packet.len ==
|
||||
7 + destination.len + source.len + 1024.toVarInt.len + # packet length
|
||||
3 + # packet number
|
||||
1024 # payload
|
||||
|
||||
test "knows the length of an initial packet":
|
||||
var packet = initialPacket()
|
||||
packet.destination = destination
|
||||
packet.source = source
|
||||
packet.initial.token = token
|
||||
packet.initial.packetnumber = 0x00BBCCDD'u32.int64
|
||||
packet.initial.payload = repeat(0xEE'u8, 1024)
|
||||
check packet.len ==
|
||||
7 + destination.len + source.len + token.len.toVarInt.len + token.len +
|
||||
1024.toVarInt.len + # packet length
|
||||
3 + # packet number
|
||||
1024 # payload
|
||||
|
||||
test "knows the length of a short packet":
|
||||
var packet = shortPacket()
|
||||
packet.destination = destination
|
||||
packet.short.packetnumber = 0x00BBCCDD'u32.int64
|
||||
packet.short.payload = repeat(0xEE'u8, 1024)
|
||||
check packet.len ==
|
||||
1 + destination.len + 3 + # packet number
|
||||
1024 # payload
|
||||
@@ -1,16 +0,0 @@
|
||||
import pkg/unittest2
|
||||
import std/math
|
||||
import pkg/quic/transport/packets/packetnumber
|
||||
|
||||
suite "packet numbers":
|
||||
test "packet numbers are in the range 0 to 2^62-1":
|
||||
check PacketNumber.low == 0
|
||||
check PacketNumber.high == 2'i64 ^ 62 - 1
|
||||
|
||||
test "conversion to bytes":
|
||||
check 0.toMinimalBytes == @[0'u8]
|
||||
check 0xFF.toMinimalBytes == @[0xFF'u8]
|
||||
check 0x01FF.toMinimalBytes == @[0x01'u8, 0xFF'u8]
|
||||
check 0xAABBCCDD.toMinimalBytes == @[0xAA'u8, 0xBB'u8, 0xCC'u8, 0xDD'u8]
|
||||
check PacketNumber.high.toMinimalBytes ==
|
||||
@[0b00111111'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8]
|
||||
@@ -1,165 +0,0 @@
|
||||
import std/sequtils
|
||||
import pkg/unittest2
|
||||
import pkg/quic/transport/packets
|
||||
import pkg/quic/helpers/bits
|
||||
|
||||
suite "packet reading":
|
||||
var datagram: seq[byte]
|
||||
|
||||
setup:
|
||||
datagram = newSeq[byte](4096)
|
||||
|
||||
test "reads long/short form":
|
||||
datagram.write(shortPacket())
|
||||
check readPacket(datagram).form == formShort
|
||||
datagram.write(initialPacket())
|
||||
check readPacket(datagram).form == formLong
|
||||
|
||||
test "checks fixed bit":
|
||||
datagram.write(shortPacket())
|
||||
datagram[0].bits[1] = 0
|
||||
expect CatchableError:
|
||||
discard readPacket(datagram)
|
||||
|
||||
test "reads packet type":
|
||||
datagram.write(initialPacket())
|
||||
check readPacket(datagram).kind == packetInitial
|
||||
datagram.write(zeroRttPacket())
|
||||
check readPacket(datagram).kind == packet0RTT
|
||||
datagram.write(handshakePacket())
|
||||
check readPacket(datagram).kind == packetHandshake
|
||||
datagram.write(retryPacket())
|
||||
check readPacket(datagram).kind == packetRetry
|
||||
|
||||
test "reads version negotiation packet":
|
||||
let length = datagram.write(versionNegotiationPacket())
|
||||
check readPacket(datagram[0 ..< length]).kind == packetVersionNegotiation
|
||||
|
||||
test "reads version":
|
||||
const version = 0xAABBCCDD'u32
|
||||
datagram.write(initialPacket(version = version))
|
||||
check readPacket(datagram).initial.version == version
|
||||
|
||||
test "reads source connection id":
|
||||
const source = ConnectionId(@[1'u8, 2'u8, 3'u8])
|
||||
var packet = initialPacket()
|
||||
packet.source = source
|
||||
datagram.write(packet)
|
||||
check readPacket(datagram).source == source
|
||||
|
||||
test "reads destination connection id":
|
||||
const destination = ConnectionId(@[4'u8, 5'u8, 6'u8])
|
||||
var packet = initialPacket()
|
||||
packet.destination = destination
|
||||
datagram.write(packet)
|
||||
check readPacket(datagram).destination == destination
|
||||
|
||||
test "reads supported versions in version negotiation packet":
|
||||
const versions = @[0xAABBCCDD'u32, 0x11223344'u32]
|
||||
let length = datagram.write(versionNegotiationPacket(versions = versions))
|
||||
let packet = readPacket(datagram[0 ..< length])
|
||||
check packet.negotiation.supportedVersions == versions
|
||||
|
||||
test "reads token from retry packet":
|
||||
const token = @[1'u8, 2'u8, 3'u8]
|
||||
var packet = retryPacket()
|
||||
packet.retry.token = token
|
||||
let length = datagram.write(packet)
|
||||
check readPacket(datagram[0 ..< length]).retry.token == token
|
||||
|
||||
test "reads integrity tag from retry packet":
|
||||
const integrity = repeat(0xA'u8, 16)
|
||||
var packet = retryPacket()
|
||||
packet.retry.integrity[0 ..< 16] = integrity
|
||||
let length = datagram.write(packet)
|
||||
check readPacket(datagram[0 ..< length]).retry.integrity == integrity
|
||||
|
||||
test "reads packet number from handshake packet":
|
||||
const packetnumber = 0xABCD
|
||||
var packet = handshakePacket()
|
||||
packet.handshake.packetnumber = packetnumber
|
||||
datagram.write(packet)
|
||||
check readPacket(datagram).handshake.packetnumber == packetnumber
|
||||
|
||||
test "reads payload from handshake packet":
|
||||
const payload = repeat(0xAB'u8, 1024)
|
||||
var packet = handshakePacket()
|
||||
packet.handshake.payload = payload
|
||||
datagram.write(packet)
|
||||
check readPacket(datagram).handshake.payload == payload
|
||||
|
||||
test "reads packet number from 0-RTT packet":
|
||||
const packetnumber = 0xABCD
|
||||
var packet = zeroRttPacket()
|
||||
packet.rtt.packetnumber = packetnumber
|
||||
datagram.write(packet)
|
||||
check readPacket(datagram).rtt.packetnumber == packetnumber
|
||||
|
||||
test "reads payload from 0-RTT packet":
|
||||
const payload = repeat(0xAB'u8, 1024)
|
||||
var packet = zeroRttPacket()
|
||||
packet.rtt.payload = payload
|
||||
datagram.write(packet)
|
||||
check readPacket(datagram).rtt.payload == payload
|
||||
|
||||
test "reads token from initial packet":
|
||||
const token = repeat(0xAA'u8, 1024)
|
||||
var packet = initialPacket()
|
||||
packet.initial.token = token
|
||||
datagram.write(packet)
|
||||
check readPacket(datagram).initial.token == token
|
||||
|
||||
test "reads packet number from initial packet":
|
||||
const packetnumber = 0xABCD
|
||||
var packet = initialPacket()
|
||||
packet.initial.packetnumber = packetnumber
|
||||
datagram.write(packet)
|
||||
check readPacket(datagram).initial.packetnumber == packetnumber
|
||||
|
||||
test "reads payload from initial packet":
|
||||
const payload = repeat(0xAB'u8, 1024)
|
||||
var packet = initialPacket()
|
||||
packet.initial.payload = payload
|
||||
datagram.write(packet)
|
||||
check readPacket(datagram).initial.payload == payload
|
||||
|
||||
test "reads spin bit from short packet":
|
||||
var packet = shortPacket()
|
||||
packet.short.spinBit = false
|
||||
datagram.write(packet)
|
||||
check readPacket(datagram).short.spinBit == false
|
||||
packet.short.spinBit = true
|
||||
datagram.write(packet)
|
||||
check readPacket(datagram).short.spinBit == true
|
||||
|
||||
test "reads key phase from short packet":
|
||||
var packet = shortPacket()
|
||||
packet.short.keyPhase = false
|
||||
datagram.write(packet)
|
||||
check readPacket(datagram).short.keyPhase == false
|
||||
packet.short.keyPhase = true
|
||||
datagram.write(packet)
|
||||
check readPacket(datagram).short.keyPhase == true
|
||||
|
||||
test "reads destination id from short packet":
|
||||
const destination = ConnectionId(repeat(0xAB'u8, 16))
|
||||
var packet = shortPacket()
|
||||
packet.destination = destination
|
||||
datagram.write(packet)
|
||||
check readPacket(datagram).destination == destination
|
||||
|
||||
test "reads packet number from short packet":
|
||||
const packetnumber = 0xABCD
|
||||
var packet = shortPacket()
|
||||
packet.short.packetnumber = packetnumber
|
||||
packet.destination = ConnectionId(repeat(0'u8, DefaultConnectionIdLength))
|
||||
datagram.write(packet)
|
||||
check readPacket(datagram).short.packetnumber == packetnumber
|
||||
|
||||
test "reads payload from short packet":
|
||||
const payload = repeat(0xAB'u8, 1024)
|
||||
var packet = shortPacket()
|
||||
packet.short.payload = payload
|
||||
packet.destination = ConnectionId(repeat(0'u8, DefaultConnectionIdLength))
|
||||
let length = datagram.write(packet)
|
||||
check readPacket(datagram[0 ..< length]).short.payload == payload
|
||||
@@ -1,202 +0,0 @@
|
||||
import std/sequtils
|
||||
import pkg/unittest2
|
||||
import pkg/stew/endians2
|
||||
import pkg/quic/transport/packets
|
||||
import pkg/quic/helpers/bits
|
||||
import pkg/quic/transport/packets/varints
|
||||
|
||||
suite "packet writing":
|
||||
var datagram: seq[byte]
|
||||
|
||||
setup:
|
||||
datagram = newSeq[byte](4096)
|
||||
|
||||
test "writes short/long form":
|
||||
datagram.write(shortPacket())
|
||||
check datagram[0].bits[0] == 0
|
||||
datagram.write(initialPacket())
|
||||
check datagram[0].bits[0] == 1
|
||||
|
||||
test "writes fixed bit":
|
||||
datagram.write(shortPacket())
|
||||
check datagram[0].bits[1] == 1
|
||||
datagram.write(initialPacket())
|
||||
check datagram[0].bits[1] == 1
|
||||
|
||||
test "writes packet type":
|
||||
datagram.write(initialPacket())
|
||||
check datagram[0] == 0b11000000
|
||||
datagram.write(zeroRttPacket())
|
||||
check datagram[0] == 0b11010000
|
||||
datagram.write(handshakePacket())
|
||||
check datagram[0] == 0b11100000
|
||||
datagram.write(retryPacket())
|
||||
check datagram[0] == 0b11110000
|
||||
|
||||
test "writes version":
|
||||
var packet = initialPacket(version = 0xAABBCCDD'u32)
|
||||
datagram.write(packet)
|
||||
check datagram[1 .. 4] == @[0xAA'u8, 0xBB'u8, 0xCC'u8, 0xDD'u8]
|
||||
|
||||
test "writes source and destination":
|
||||
const source = @[1'u8, 2'u8]
|
||||
const destination = @[3'u8, 4'u8, 5'u8]
|
||||
var packet = initialPacket()
|
||||
packet.source = ConnectionId(source)
|
||||
packet.destination = ConnectionId(destination)
|
||||
datagram.write(packet)
|
||||
check datagram[5] == destination.len.uint8
|
||||
check datagram[6 .. 8] == destination
|
||||
check datagram[9] == source.len.uint8
|
||||
check datagram[10 .. 11] == source
|
||||
|
||||
test "writes supported version for a version negotiation packet":
|
||||
const versions = @[0xAABBCCDD'u32, 0x11223344'u32]
|
||||
datagram.write(versionNegotiationPacket(versions = versions))
|
||||
check datagram[7 .. 10] == versions[0].toBytesBE
|
||||
check datagram[11 .. 14] == versions[1].toBytesBE
|
||||
|
||||
test "writes retry token":
|
||||
var packet = retryPacket()
|
||||
packet.retry.token = @[1'u8, 2'u8, 3'u8]
|
||||
datagram.write(packet)
|
||||
check datagram[7 ..< packet.len - 16] == packet.retry.token
|
||||
|
||||
test "writes retry integrity tag":
|
||||
var packet = retryPacket()
|
||||
packet.retry.integrity[0 ..< 16] = repeat(0xB'u8, 16)
|
||||
datagram.write(packet)
|
||||
check datagram[packet.len - 16 ..< packet.len] == packet.retry.integrity
|
||||
|
||||
test "writes handshake packet length":
|
||||
const packetnumber = 0xAABBCCDD'u32
|
||||
const payload = repeat(0xAB'u8, 1024)
|
||||
var packet = handshakePacket()
|
||||
packet.handshake.packetnumber = packetnumber.int64
|
||||
packet.handshake.payload = payload
|
||||
datagram.write(packet)
|
||||
check datagram[7 .. 8] == (sizeof(packetnumber) + payload.len).toVarInt
|
||||
|
||||
test "writes handshake packet number":
|
||||
const packetnumber = 0xAABBCCDD'u32
|
||||
var packet = handshakePacket()
|
||||
packet.handshake.packetnumber = packetnumber.int64
|
||||
datagram.write(packet)
|
||||
check int(datagram[0] and 0b11'u8) + 1 == sizeof(packetnumber)
|
||||
check datagram[8 .. 11] == @[0xAA'u8, 0xBB'u8, 0xCC'u8, 0xDD'u8]
|
||||
|
||||
test "writes handshake payload":
|
||||
const payload = repeat(0xAB'u8, 1024)
|
||||
var packet = handshakePacket()
|
||||
packet.handshake.payload = payload
|
||||
datagram.write(packet)
|
||||
check datagram[10 .. 1033] == payload
|
||||
|
||||
test "writes 0-RTT packet length":
|
||||
const packetnumber = 0xAABBCCDD'u32
|
||||
const payload = repeat(0xAB'u8, 1024)
|
||||
var packet = zeroRttPacket()
|
||||
packet.rtt.packetnumber = packetnumber.int64
|
||||
packet.rtt.payload = payload
|
||||
datagram.write(packet)
|
||||
check datagram[7 .. 8] == (sizeof(packetnumber) + payload.len).toVarInt
|
||||
|
||||
test "writes 0-RTT packet number":
|
||||
const packetnumber = 0xAABBCCDD'u32
|
||||
var packet = zeroRttPacket()
|
||||
packet.rtt.packetnumber = packetnumber.int64
|
||||
datagram.write(packet)
|
||||
check int(datagram[0] and 0b11'u8) + 1 == sizeof(packetnumber)
|
||||
check datagram[8 .. 11] == @[0xAA'u8, 0xBB'u8, 0xCC'u8, 0xDD'u8]
|
||||
|
||||
test "writes 0-RTT payload":
|
||||
const payload = repeat(0xAB'u8, 1024)
|
||||
var packet = zeroRttPacket()
|
||||
packet.rtt.payload = payload
|
||||
datagram.write(packet)
|
||||
check datagram[10 .. 1033] == payload
|
||||
|
||||
test "writes initial token":
|
||||
const token = repeat(0xAA'u8, 1024)
|
||||
var packet = initialPacket()
|
||||
packet.initial.token = token
|
||||
datagram.write(packet)
|
||||
check datagram[7 .. 8] == token.len.toVarInt
|
||||
check datagram[9 .. 1032] == token
|
||||
|
||||
test "writes initial packet length":
|
||||
const packetnumber = 0xAABBCCDD'u32
|
||||
const payload = repeat(0xAB'u8, 1024)
|
||||
var packet = initialPacket()
|
||||
packet.initial.packetnumber = packetnumber.int64
|
||||
packet.initial.payload = payload
|
||||
datagram.write(packet)
|
||||
check datagram[8 .. 9] == (sizeof(packetnumber) + payload.len).toVarInt
|
||||
|
||||
test "writes initial packet number":
|
||||
const packetnumber = 0xAABBCCDD'u32
|
||||
var packet = initialPacket()
|
||||
packet.initial.packetnumber = packetnumber.int64
|
||||
datagram.write(packet)
|
||||
check int(datagram[0] and 0b11'u8) + 1 == sizeof(packetnumber)
|
||||
check datagram[9 .. 12] == @[0xAA'u8, 0xBB'u8, 0xCC'u8, 0xDD'u8]
|
||||
|
||||
test "writes initial payload":
|
||||
const payload = repeat(0xAB'u8, 1024)
|
||||
var packet = initialPacket()
|
||||
packet.initial.payload = payload
|
||||
datagram.write(packet)
|
||||
check datagram[11 .. 1034] == payload
|
||||
|
||||
test "writes spin bit":
|
||||
var packet = shortPacket()
|
||||
packet.short.spinBit = false
|
||||
datagram.write(packet)
|
||||
check datagram[0].bits[2] == 0
|
||||
packet.short.spinBit = true
|
||||
datagram.write(packet)
|
||||
check datagram[0].bits[2] == 1
|
||||
|
||||
test "writes reserved bits for short packet":
|
||||
datagram[0].bits[3] = 1
|
||||
datagram[0].bits[4] = 1
|
||||
datagram.write(shortPacket())
|
||||
check datagram[0].bits[3] == 0
|
||||
check datagram[0].bits[4] == 0
|
||||
|
||||
test "writes key phase for short packet":
|
||||
var packet = shortPacket()
|
||||
packet.short.keyPhase = false
|
||||
datagram.write(packet)
|
||||
check datagram[0].bits[5] == 0
|
||||
packet.short.keyPhase = true
|
||||
datagram.write(packet)
|
||||
check datagram[0].bits[5] == 1
|
||||
|
||||
test "writes destination connection id for short packet":
|
||||
const destination = @[1'u8, 2'u8, 3'u8]
|
||||
var packet = shortPacket()
|
||||
packet.destination = ConnectionId(destination)
|
||||
datagram.write(packet)
|
||||
check datagram[1 .. 3] == destination
|
||||
|
||||
test "writes packet number for short packet":
|
||||
const packetnumber = 0xAABB'u16
|
||||
var packet = shortPacket()
|
||||
packet.short.packetnumber = packetnumber.int64
|
||||
datagram.write(packet)
|
||||
check int(datagram[0] and 0b11'u8) + 1 == sizeof(packetnumber)
|
||||
check datagram[1 .. 2] == @[0xAA'u8, 0xBB'u8]
|
||||
|
||||
test "writes payload for short packet":
|
||||
const payload = repeat(0xAB'u8, 1024)
|
||||
var packet = shortPacket()
|
||||
packet.short.payload = payload
|
||||
datagram.write(packet)
|
||||
check datagram[2 .. 1025] == payload
|
||||
|
||||
test "returns the number of bytes that were written":
|
||||
let payload = repeat(0xAA'u8, 1024)
|
||||
var packet = shortPacket()
|
||||
packet.short.payload = payload
|
||||
check datagram.write(packet) == packet.len
|
||||
@@ -1,69 +0,0 @@
|
||||
import std/unittest
|
||||
import pkg/quic/transport/packets
|
||||
import pkg/quic/transport/version
|
||||
|
||||
suite "packet creation":
|
||||
test "short packets":
|
||||
check shortPacket() == Packet(form: formShort)
|
||||
|
||||
test "initial packets":
|
||||
let packet = initialPacket()
|
||||
check packet.form == formLong
|
||||
check packet.kind == packetInitial
|
||||
check packet.initial.version == CurrentQuicVersion
|
||||
|
||||
test "0-RTT packets":
|
||||
let packet = zeroRttPacket()
|
||||
check packet.form == formLong
|
||||
check packet.kind == packet0RTT
|
||||
check packet.rtt.version == CurrentQuicVersion
|
||||
|
||||
test "handshake packets":
|
||||
let packet = handshakePacket()
|
||||
check packet.form == formLong
|
||||
check packet.kind == packetHandshake
|
||||
check packet.handshake.version == CurrentQuicVersion
|
||||
|
||||
test "retry packets":
|
||||
let packet = retryPacket()
|
||||
check packet.form == formLong
|
||||
check packet.kind == packetRetry
|
||||
check packet.retry.version == CurrentQuicVersion
|
||||
|
||||
test "version negotiation packets":
|
||||
let packet = versionNegotiationPacket()
|
||||
check packet.form == formLong
|
||||
check packet.kind == packetVersionNegotiation
|
||||
check packet.negotiation.supportedVersions == @[CurrentQuicVersion]
|
||||
|
||||
suite "multiple packets":
|
||||
var datagram: seq[byte]
|
||||
|
||||
setup:
|
||||
datagram = newSeq[byte](4096)
|
||||
|
||||
test "writes multiple packets into a datagram":
|
||||
let packet1 = initialPacket()
|
||||
let packet2 = handshakePacket()
|
||||
datagram.write(@[packet1, packet2])
|
||||
check datagram[0] == 0b11_00_0000 # initial
|
||||
check datagram[packet1.len] == 0b11_10_0000 # handshake
|
||||
|
||||
test "writes packet number lengths correctly":
|
||||
var packet1, packet2 = initialPacket()
|
||||
packet1.initial.packetnumber = 0xAABBCC
|
||||
packet2.initial.packetnumber = 0xAABBCCDD
|
||||
datagram.write(@[packet1, packet2])
|
||||
check datagram[0] == 0b110000_10 # packet number length 3
|
||||
check datagram[packet1.len] == 0b110000_11 # packet number length 4
|
||||
|
||||
test "returns the number of bytes that were written":
|
||||
let packet1 = initialPacket()
|
||||
let packet2 = handshakePacket()
|
||||
check datagram.write(@[packet1, packet2]) == packet1.len + packet2.len
|
||||
|
||||
test "reads multiple packets from a datagram":
|
||||
var packet1 = initialPacket()
|
||||
var packet2 = handshakePacket()
|
||||
let length = datagram.write(@[packet1, packet2])
|
||||
check datagram[0 ..< length].readPackets() == @[packet1, packet2]
|
||||
@@ -7,7 +7,7 @@ import pkg/stew/endians2
|
||||
import ../helpers/simulation
|
||||
|
||||
const
|
||||
runs = 10
|
||||
runs = 1
|
||||
uploadSize = 100000 # 100KB
|
||||
downloadSize = 100000000 # 100MB
|
||||
chunkSize = 65536 # 64KB chunks like perf
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
import std/math
|
||||
import pkg/unittest2
|
||||
import pkg/quic/transport/packets/varints
|
||||
|
||||
suite "variable length integer encoding":
|
||||
test "encodes 6 bit numbers as 1 byte":
|
||||
check toVarInt(0) == @[0'u8]
|
||||
check toVarInt(42) == @[42'u8]
|
||||
check toVarInt(63) == @[63'u8]
|
||||
|
||||
test "encodes 7-14 bit numbers as 2 bytes":
|
||||
check toVarInt(64) == @[0b01000000'u8, 64'u8]
|
||||
check toVarInt(256) == @[0b01000001'u8, 0'u8]
|
||||
check toVarInt(2 ^ 14 - 1) == @[0b01111111'u8, 0xFF'u8]
|
||||
|
||||
test "encodes 15-30 bit numbers as 4 bytes":
|
||||
check toVarInt(2 ^ 14) == @[0b10000000'u8, 0'u8, 0b01000000'u8, 0'u8]
|
||||
check toVarInt(2 ^ 16) == @[0b10000000'u8, 1'u8, 0'u8, 0'u8]
|
||||
check toVarInt(2 ^ 24) == @[0b10000001'u8, 0'u8, 0'u8, 0'u8]
|
||||
check toVarInt(2 ^ 30 - 1) == @[0b10111111'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8]
|
||||
|
||||
test "encodes 30-62 bit numbers as 8 bytes":
|
||||
check toVarInt(2 ^ 30) ==
|
||||
@[0b11000000'u8, 0'u8, 0'u8, 0'u8, 0b01000000'u8, 0'u8, 0'u8, 0'u8]
|
||||
check toVarInt(2'i64 ^ 32) ==
|
||||
@[0b11000000'u8, 0'u8, 0'u8, 1'u8, 0'u8, 0'u8, 0'u8, 0'u8]
|
||||
check toVarInt(2'i64 ^ 56) ==
|
||||
@[0b11000001'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8]
|
||||
check toVarInt(2'i64 ^ 62 - 1) ==
|
||||
@[0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8]
|
||||
|
||||
suite "variable length integer decoding":
|
||||
test "decodes 1 byte integers":
|
||||
check fromVarInt(@[0'u8]) == 0
|
||||
check fromVarInt(@[42'u8]) == 42
|
||||
check fromVarInt(@[63'u8]) == 63
|
||||
|
||||
test "decodes 2 byte integers":
|
||||
check fromVarInt(@[0b01000000'u8, 0'u8]) == 0
|
||||
check fromVarInt(@[0b01000001'u8, 0'u8]) == 256
|
||||
check fromVarInt(@[0b01111111'u8, 0xFF'u8]) == 2 ^ 14 - 1
|
||||
|
||||
test "decodes 4 byte integers":
|
||||
check fromVarInt(@[0b10000000'u8, 0'u8, 0'u8, 0'u8]) == 0
|
||||
check fromVarInt(@[0b10000001'u8, 0'u8, 0'u8, 0'u8]) == 2 ^ 24
|
||||
check fromVarInt(@[0b10111111'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8]) == 2 ^ 30 - 1
|
||||
|
||||
test "decodes 8 byte integers":
|
||||
check fromVarInt(@[0b11000000'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8]) == 0
|
||||
check fromVarInt(@[0b11000001'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8]) ==
|
||||
2'i64 ^ 56
|
||||
check fromVarInt(
|
||||
@[0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8]
|
||||
) == 2'i64 ^ 62 - 1
|
||||
|
||||
suite "variable length":
|
||||
test "determines length correctly":
|
||||
var buffer: array[8, byte]
|
||||
buffer[0] = 0b00000000'u8
|
||||
check varintlen(buffer) == 1
|
||||
buffer[0] = 0b01000000'u8
|
||||
check varintlen(buffer) == 2
|
||||
buffer[0] = 0b10000000'u8
|
||||
check varintlen(buffer) == 4
|
||||
buffer[0] = 0b11000000'u8
|
||||
check varintlen(buffer) == 8
|
||||
@@ -1,10 +1,4 @@
|
||||
import ./quic/testBits
|
||||
import ./quic/testPacketWriting
|
||||
import ./quic/testPacketReading
|
||||
import ./quic/testPacketLength
|
||||
import ./quic/testPackets
|
||||
import ./quic/testVarInts
|
||||
import ./quic/testPacketNumber
|
||||
import ./quic/testConnection
|
||||
import ./quic/testConnectionId
|
||||
import ./quic/testNgtcp2TransportParameters
|
||||
|
||||
Reference in New Issue
Block a user