From 29d7b5cb7cf54d4adb4f8ac79dabf82427983e29 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 17 Sep 2020 11:19:23 +0200 Subject: [PATCH] Ensure that second bit of the header is always 1 --- quic/packets.nim | 9 ++++++++- tests/quic/testPackets.nim | 10 +++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/quic/packets.nim b/quic/packets.nim index d7ce934..be93edb 100644 --- a/quic/packets.nim +++ b/quic/packets.nim @@ -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 diff --git a/tests/quic/testPackets.nim b/tests/quic/testPackets.nim index 9aa15ee..696779c 100644 --- a/tests/quic/testPackets.nim +++ b/tests/quic/testPackets.nim @@ -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