2021-06-27 06:35:36 +00:00
|
|
|
## nim-websock
|
2021-06-20 04:27:50 +00:00
|
|
|
## Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
import
|
|
|
|
pkg/stew/byteutils,
|
|
|
|
pkg/chronos/unittest2/asynctests
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2021-06-27 06:35:36 +00:00
|
|
|
include ../websock/frame
|
|
|
|
include ../websock/utils
|
2021-03-18 15:30:21 +00:00
|
|
|
|
|
|
|
# TODO: Fix Test.
|
|
|
|
|
|
|
|
suite "Test data frames":
|
2022-06-20 07:19:38 +00:00
|
|
|
setup:
|
|
|
|
var maskKey: array[4, char]
|
2021-09-21 07:35:12 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "# 7bit length text":
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: false,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Text,
|
|
|
|
mask: false,
|
2021-05-25 00:47:27 +00:00
|
|
|
data: toBytes("hi there")
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\1\8hi there")
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "# 7bit length text fin bit":
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: true,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Text,
|
|
|
|
mask: false,
|
|
|
|
data: toBytes("hi there"),
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\129\8hi there")
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "# 7bit length binary":
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: false,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Binary,
|
|
|
|
mask: false,
|
|
|
|
data: toBytes("hi there"),
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\2\8hi there")
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "# 7bit length binary fin bit":
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: true,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Binary,
|
|
|
|
mask: false,
|
|
|
|
data: toBytes("hi there"),
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\130\8hi there")
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "# 7bit length continuation":
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: false,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Cont,
|
|
|
|
mask: false,
|
|
|
|
data: toBytes("hi there"),
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\0\8hi there")
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "# 7+16 length text":
|
2021-03-18 15:30:21 +00:00
|
|
|
var data = ""
|
|
|
|
for i in 0..32:
|
|
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: false,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Text,
|
|
|
|
mask: false,
|
|
|
|
data: toBytes(data),
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\1\126\4\98" & data)
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "# 7+16 length text fin bit":
|
2021-03-18 15:30:21 +00:00
|
|
|
var data = ""
|
|
|
|
for i in 0..32:
|
|
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: false,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Text,
|
|
|
|
mask: false,
|
|
|
|
data: toBytes(data),
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\1\126\4\98" & data)
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "# 7+16 length binary":
|
2021-03-18 15:30:21 +00:00
|
|
|
var data = ""
|
|
|
|
for i in 0..32:
|
|
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: false,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Binary,
|
|
|
|
mask: false,
|
|
|
|
data: toBytes(data),
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\2\126\4\98" & data)
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "# 7+16 length binary fin bit":
|
2021-03-18 15:30:21 +00:00
|
|
|
var data = ""
|
|
|
|
for i in 0..32:
|
|
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: true,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Binary,
|
|
|
|
mask: false,
|
|
|
|
data: toBytes(data),
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\130\126\4\98" & data)
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "# 7+16 length continuation":
|
2021-03-18 15:30:21 +00:00
|
|
|
var data = ""
|
|
|
|
for i in 0..32:
|
|
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: false,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Cont,
|
|
|
|
mask: false,
|
|
|
|
data: toBytes(data),
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\0\126\4\98" & data)
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "# 7+64 length text":
|
2021-03-18 15:30:21 +00:00
|
|
|
var data = ""
|
|
|
|
for i in 0..3200:
|
|
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: false,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Text,
|
|
|
|
mask: false,
|
|
|
|
data: toBytes(data),
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\1\127\0\0\0\0\0\1\169\34" & data)
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "# 7+64 length fin bit":
|
2021-03-18 15:30:21 +00:00
|
|
|
var data = ""
|
|
|
|
for i in 0..3200:
|
|
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: true,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Text,
|
|
|
|
mask: false,
|
|
|
|
data: toBytes(data),
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\129\127\0\0\0\0\0\1\169\34" & data)
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "# 7+64 length binary":
|
2021-03-18 15:30:21 +00:00
|
|
|
var data = ""
|
|
|
|
for i in 0..3200:
|
|
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: false,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Binary,
|
|
|
|
mask: false,
|
|
|
|
data: toBytes(data),
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\2\127\0\0\0\0\0\1\169\34" & data)
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "# 7+64 length binary fin bit":
|
2021-03-18 15:30:21 +00:00
|
|
|
var data = ""
|
|
|
|
for i in 0..3200:
|
|
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: true,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Binary,
|
|
|
|
mask: false,
|
|
|
|
data: toBytes(data),
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\130\127\0\0\0\0\0\1\169\34" & data)
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "# 7+64 length binary":
|
2021-03-18 15:30:21 +00:00
|
|
|
var data = ""
|
|
|
|
for i in 0..3200:
|
|
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: false,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Cont,
|
|
|
|
mask: false,
|
|
|
|
data: toBytes(data),
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\0\127\0\0\0\0\0\1\169\34" & data)
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "# masking":
|
2021-05-25 22:39:10 +00:00
|
|
|
let data = (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: true,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Text,
|
|
|
|
mask: true,
|
|
|
|
data: toBytes("hi there"),
|
|
|
|
maskKey: ['\xCF', '\xD8', '\x05', 'e']
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode())
|
2021-03-18 15:30:21 +00:00
|
|
|
|
|
|
|
check data == toBytes("\129\136\207\216\5e\167\177%\17\167\189w\0")
|
|
|
|
|
|
|
|
suite "Test control frames":
|
2022-06-20 07:19:38 +00:00
|
|
|
setup:
|
|
|
|
var maskKey: array[4, char]
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "Close":
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: true,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Close,
|
|
|
|
mask: false,
|
|
|
|
data: @[3'u8, 232'u8] & toBytes("hi there"),
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\136\10\3\232hi there")
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "Ping":
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: false,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Ping,
|
|
|
|
mask: false,
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\9\0")
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "Pong":
|
2021-05-25 22:39:10 +00:00
|
|
|
check (await Frame(
|
2021-03-18 15:30:21 +00:00
|
|
|
fin: false,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Pong,
|
|
|
|
mask: false,
|
|
|
|
maskKey: maskKey
|
2021-05-25 22:39:10 +00:00
|
|
|
).encode()) == toBytes("\10\0")
|