Ensure that second bit of the header is always 1

This commit is contained in:
Mark Spanbroek 2020-09-17 11:19:23 +02:00 committed by markspanbroek
parent db9febbb19
commit 29d7b5cb7c
2 changed files with 15 additions and 4 deletions

View File

@ -6,11 +6,18 @@ type
headerShort
headerLong
PacketHeader* = object
bytes*: seq[byte]
bytes: seq[byte]
PacketNumber* = range[0'u64..2'u64^62-1]
proc newPacketHeader*(bytes: seq[byte]): PacketHeader =
assert bytes[0].bits[1] == 1
PacketHeader(bytes: bytes)
proc kind*(header: PacketHeader): PacketHeaderKind =
PacketHeaderKind(header.bytes[0].bits[0])
proc `kind=`*(header: var PacketHeader, kind: PacketHeaderKind) =
header.bytes[0].bits[0] = Bit(kind)
proc bytes*(header: PacketHeader): seq[byte] =
header.bytes

View File

@ -6,14 +6,18 @@ import quic/bits
suite "packets":
test "first bit of the header indicates its kind":
check PacketHeader(bytes: @[0b00000000'u8]).kind == headerShort
check PacketHeader(bytes: @[0b10000000'u8]).kind == headerLong
check newPacketHeader(@[0b01000000'u8]).kind == headerShort
check newPacketHeader(@[0b11000000'u8]).kind == headerLong
test "header kind can be set":
var header = PacketHeader(bytes: @[0b00000000'u8])
var header = newPacketHeader(@[0b01000000'u8])
header.kind = headerLong
check header.bytes[0].bits[0] == 1
test "second bit of the header should always be 1":
expect Exception:
discard newPacketHeader(@[0b00000000'u8])
test "packet numbers are in the range 0 to 2^62-1":
check PacketNumber.low == 0
check PacketNumber.high == 2'u64 ^ 62 - 1