From c677b9e4faa0f5be1cf7ac4a0de4e009b791fd31 Mon Sep 17 00:00:00 2001 From: Ludovic Chenut Date: Thu, 7 Mar 2024 13:25:57 +0100 Subject: [PATCH] Fix teststun & add runalltest.nim --- tests/runalltests.nim | 4 ++++ tests/teststun.nim | 28 +++++++++++++++++++++++++--- webrtc.nimble | 2 +- webrtc/stun/stun.nim | 9 ++++----- 4 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 tests/runalltests.nim diff --git a/tests/runalltests.nim b/tests/runalltests.nim new file mode 100644 index 0000000..5349d2d --- /dev/null +++ b/tests/runalltests.nim @@ -0,0 +1,4 @@ +{.used.} + +import testdatachannel +import teststun diff --git a/tests/teststun.nim b/tests/teststun.nim index f22ebfb..5b3f034 100644 --- a/tests/teststun.nim +++ b/tests/teststun.nim @@ -1,11 +1,33 @@ +import options import ../webrtc/stun/stun +import ../webrtc/stun/stun_attributes import ./asyncunit -import binary_serialization suite "Stun suite": - test "Stun encoding/decoding with padding": + test "Stun decoding": let msg = @[ 0x00'u8, 0x01, 0x00, 0xa4, 0x21, 0x12, 0xa4, 0x42, 0x75, 0x6a, 0x58, 0x46, 0x42, 0x58, 0x4e, 0x72, 0x6a, 0x50, 0x4d, 0x2b, 0x00, 0x06, 0x00, 0x63, 0x6c, 0x69, 0x62, 0x70, 0x32, 0x70, 0x2b, 0x77, 0x65, 0x62, 0x72, 0x74, 0x63, 0x2b, 0x76, 0x31, 0x2f, 0x62, 0x71, 0x36, 0x67, 0x69, 0x43, 0x75, 0x4a, 0x38, 0x6e, 0x78, 0x59, 0x46, 0x4a, 0x36, 0x43, 0x63, 0x67, 0x45, 0x59, 0x58, 0x58, 0x2f, 0x78, 0x51, 0x58, 0x56, 0x4c, 0x74, 0x39, 0x71, 0x7a, 0x3a, 0x6c, 0x69, 0x62, 0x70, 0x32, 0x70, 0x2b, 0x77, 0x65, 0x62, 0x72, 0x74, 0x63, 0x2b, 0x76, 0x31, 0x2f, 0x62, 0x71, 0x36, 0x67, 0x69, 0x43, 0x75, 0x4a, 0x38, 0x6e, 0x78, 0x59, 0x46, 0x4a, 0x36, 0x43, 0x63, 0x67, 0x45, 0x59, 0x58, 0x58, 0x2f, 0x78, 0x51, 0x58, 0x56, 0x4c, 0x74, 0x39, 0x71, 0x7a, 0x00, 0xc0, 0x57, 0x00, 0x04, 0x00, 0x00, 0x03, 0xe7, 0x80, 0x2a, 0x00, 0x08, 0x86, 0x63, 0xfd, 0x45, 0xa9, 0xe5, 0x4c, 0xdb, 0x00, 0x24, 0x00, 0x04, 0x6e, 0x00, 0x1e, 0xff, 0x00, 0x08, 0x00, 0x14, 0x16, 0xff, 0x70, 0x8d, 0x97, 0x0b, 0xd6, 0xa3, 0x5b, 0xac, 0x8f, 0x4c, 0x85, 0xe6, 0xa6, 0xac, 0xaa, 0x7a, 0x68, 0x27, 0x80, 0x28, 0x00, 0x04, 0x79, 0x5e, 0x03, 0xd8 ] - check msg == encode(StunMessage.decode(msg)) + let stunmsg = StunMessage.decode(msg) + check: + stunmsg.msgType == 1 + stunmsg.transactionId.len() == 12 + stunmsg.attributes.len() == 6 + stunmsg.attributes[0].attributeType == 6 # AttrUsername + stunmsg.attributes[^1].attributeType == 0x8028 # AttrFingerprint + + test "Stun encoding": + let transactionId: array[12, byte] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] + var msg = StunMessage(msgType: 0x0001'u16, transactionId: transactionId) + msg.attributes.add(ErrorCode.encode(ECUnknownAttribute)) + let encoded = msg.encode() + let decoded = StunMessage.decode(encoded) + # cannot do `check msg == decoded` because encode add a + # Fingerprint at the end of the attributes + check: + decoded.msgType == 1 + decoded.transactionId == transactionId + decoded.attributes.len() == 2 + decoded.attributes[0].attributeType == 9 # AttrErrorCode + decoded.attributes[^1].attributeType == 0x8028 # AttrFingerprint test "Error while decoding": let msgLengthFailed = @[ 0x00'u8, 0x01, 0x00, 0xa4, 0x21, 0x12, 0xa4, 0x42, 0x75, 0x6a, 0x58, 0x46, 0x42, 0x58, 0x4e, 0x72, 0x6a, 0x50, 0x4d ] diff --git a/webrtc.nimble b/webrtc.nimble index dd3d318..57f3360 100644 --- a/webrtc.nimble +++ b/webrtc.nimble @@ -33,4 +33,4 @@ proc runTest(filename: string) = rmFile "tests/" & filename.toExe task test, "Run test": - runTest("testdatachannel") + runTest("runalltests") diff --git a/webrtc/stun/stun.nim b/webrtc/stun/stun.nim index 3248d53..dcb785d 100644 --- a/webrtc/stun/stun.nim +++ b/webrtc/stun/stun.nim @@ -59,10 +59,9 @@ type RawStunMessage = object msgType: uint16 - # it.conten.len() + 8 Because the Fingerprint is added after the encoding length* {.bin_value: it.content.len().}: uint16 magicCookie: uint32 - transactionId: array[12, byte] + transactionId: array[12, byte] # Down from 16 to 12 bytes in RFC5389 content* {.bin_len: it.length.}: seq[byte] StunMessage* = object @@ -98,11 +97,11 @@ proc decode*(T: typedesc[StunMessage], msg: seq[byte]): StunMessage = transactionId: smi.transactionId, attributes: RawStunAttribute.decode(smi.content)) -proc encode*(msg: StunMessage, userOpt: Option[seq[byte]]): seq[byte] = +proc encode*(msg: StunMessage, userOpt: Option[seq[byte]] = none(seq[byte])): seq[byte] = const pad = @[0, 3, 2, 1] var smi = RawStunMessage(msgType: msg.msgType, - magicCookie: magicCookie, - transactionId: msg.transactionId) + magicCookie: magicCookie, + transactionId: msg.transactionId) for attr in msg.attributes: smi.content.add(Binary.encode(attr)) smi.content.add(newSeq[byte](pad[smi.content.len() mod 4]))