2021-06-27 06:35:36 +00:00
|
|
|
## nim-websock
|
2021-05-28 16:47:24 +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.
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[strutils],
|
|
|
|
pkg/[
|
|
|
|
stew/byteutils,
|
2021-09-21 07:35:12 +00:00
|
|
|
asynctest/unittest2,
|
2021-05-28 16:47:24 +00:00
|
|
|
chronos,
|
|
|
|
chronicles
|
|
|
|
],
|
2021-06-27 06:35:36 +00:00
|
|
|
../websock/[websock, utf8dfa]
|
2021-05-28 16:47:24 +00:00
|
|
|
|
|
|
|
suite "UTF-8 DFA validator":
|
|
|
|
test "single octet":
|
|
|
|
check:
|
|
|
|
validateUTF8("\x01")
|
|
|
|
validateUTF8("\x32")
|
|
|
|
validateUTF8("\x7f")
|
|
|
|
validateUTF8("\x80") == false
|
|
|
|
|
|
|
|
test "two octets":
|
|
|
|
check:
|
|
|
|
validateUTF8("\xc2\x80")
|
|
|
|
validateUTF8("\xc4\x80")
|
|
|
|
validateUTF8("\xdf\xbf")
|
|
|
|
validateUTF8("\xdfu\xc0") == false
|
|
|
|
validateUTF8("\xdf") == false
|
|
|
|
|
|
|
|
test "three octets":
|
|
|
|
check:
|
|
|
|
validateUTF8("\xe0\xa0\x80")
|
|
|
|
validateUTF8("\xe1\x80\x80")
|
|
|
|
validateUTF8("\xef\xbf\xbf")
|
|
|
|
validateUTF8("\xef\xbf\xc0") == false
|
|
|
|
validateUTF8("\xef\xbf") == false
|
|
|
|
|
|
|
|
test "four octets":
|
|
|
|
check:
|
|
|
|
validateUTF8("\xf0\x90\x80\x80")
|
|
|
|
validateUTF8("\xf0\x92\x80\x80")
|
|
|
|
validateUTF8("\xf0\x9f\xbf\xbf")
|
|
|
|
validateUTF8("\xf0\x9f\xbf\xc0") == false
|
|
|
|
validateUTF8("\xf0\x9f\xbf") == false
|
|
|
|
|
|
|
|
test "overlong sequence":
|
|
|
|
check:
|
|
|
|
validateUTF8("\xc0\xaf") == false
|
|
|
|
validateUTF8("\xe0\x80\xaf") == false
|
|
|
|
validateUTF8("\xf0\x80\x80\xaf") == false
|
|
|
|
validateUTF8("\xf8\x80\x80\x80\xaf") == false
|
|
|
|
validateUTF8("\xfc\x80\x80\x80\x80\xaf") == false
|
|
|
|
|
|
|
|
test "max overlong sequence":
|
|
|
|
check:
|
|
|
|
validateUTF8("\xc1\xbf") == false
|
|
|
|
validateUTF8("\xe0\x9f\xbf") == false
|
|
|
|
validateUTF8("\xf0\x8f\xbf\xbf") == false
|
|
|
|
validateUTF8("\xf8\x87\xbf\xbf\xbf") == false
|
|
|
|
validateUTF8("\xfc\x83\xbf\xbf\xbf\xbf") == false
|
|
|
|
|
|
|
|
test "distinct codepoint":
|
|
|
|
check:
|
|
|
|
validateUTF8("foobar")
|
|
|
|
validateUTF8("foob\xc3\xa6r")
|
|
|
|
validateUTF8("foob\xf0\x9f\x99\x88r")
|
|
|
|
|
|
|
|
proc waitForClose(ws: WSSession) {.async.} =
|
|
|
|
try:
|
2021-12-27 10:27:21 +00:00
|
|
|
while ws.readyState != ReadyState.Closed:
|
2021-08-04 16:23:56 +00:00
|
|
|
discard await ws.recvMsg()
|
2021-05-28 16:47:24 +00:00
|
|
|
except CatchableError:
|
2021-06-11 20:04:09 +00:00
|
|
|
trace "Closing websocket"
|
2021-05-28 16:47:24 +00:00
|
|
|
|
|
|
|
suite "UTF-8 validator in action":
|
2021-09-21 07:35:12 +00:00
|
|
|
|
|
|
|
var server: HttpServer
|
|
|
|
let address = initTAddress("127.0.0.1:8888")
|
|
|
|
|
2021-05-28 16:47:24 +00:00
|
|
|
teardown:
|
2021-06-01 02:39:14 +00:00
|
|
|
server.stop()
|
2021-05-28 16:47:24 +00:00
|
|
|
await server.closeWait()
|
|
|
|
|
|
|
|
test "valid UTF-8 sequence":
|
|
|
|
let testData = "hello world"
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
2021-05-28 16:47:24 +00:00
|
|
|
check request.uri.path == "/ws"
|
|
|
|
|
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
|
|
|
|
2021-08-04 16:23:56 +00:00
|
|
|
let res = await ws.recvMsg()
|
2021-05-28 16:47:24 +00:00
|
|
|
check:
|
|
|
|
string.fromBytes(res) == testData
|
|
|
|
ws.binary == false
|
|
|
|
|
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = HttpServer.create(
|
|
|
|
address,
|
|
|
|
handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-28 16:47:24 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await WebSocket.connect(
|
2021-07-15 00:51:39 +00:00
|
|
|
"127.0.0.1:8888",
|
2021-05-28 16:47:24 +00:00
|
|
|
path = "/ws",
|
|
|
|
protocols = @["proto"],
|
|
|
|
)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
await session.send(testData)
|
|
|
|
await session.close()
|
2021-05-28 16:47:24 +00:00
|
|
|
|
|
|
|
test "valid UTF-8 sequence in close reason":
|
|
|
|
let testData = "hello world"
|
|
|
|
let closeReason = "i want to close"
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
2021-05-28 16:47:24 +00:00
|
|
|
check request.uri.path == "/ws"
|
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
proc onClose(status: StatusCodes, reason: string):
|
2021-06-01 02:39:14 +00:00
|
|
|
CloseResult {.gcsafe, raises: [Defect].} =
|
2021-05-28 16:47:24 +00:00
|
|
|
try:
|
2021-06-11 20:04:09 +00:00
|
|
|
check status == StatusFulfilled
|
2021-05-28 16:47:24 +00:00
|
|
|
check reason == closeReason
|
|
|
|
return (status, reason)
|
|
|
|
except Exception as exc:
|
|
|
|
raise newException(Defect, exc.msg)
|
|
|
|
|
|
|
|
let server = WSServer.new(protos = ["proto"], onClose = onClose)
|
|
|
|
let ws = await server.handleRequest(request)
|
2021-08-04 16:23:56 +00:00
|
|
|
let res = await ws.recvMsg()
|
2021-06-11 20:04:09 +00:00
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-05-28 16:47:24 +00:00
|
|
|
check:
|
|
|
|
string.fromBytes(res) == testData
|
|
|
|
ws.binary == false
|
|
|
|
|
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = HttpServer.create(
|
|
|
|
address,
|
|
|
|
handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-28 16:47:24 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await WebSocket.connect(
|
2021-07-15 00:51:39 +00:00
|
|
|
"127.0.0.1:8888",
|
2021-05-28 16:47:24 +00:00
|
|
|
path = "/ws",
|
|
|
|
protocols = @["proto"],
|
|
|
|
)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
await session.send(testData)
|
|
|
|
await session.close(reason = closeReason)
|
2021-05-28 16:47:24 +00:00
|
|
|
|
|
|
|
test "invalid UTF-8 sequence":
|
|
|
|
let testData = "hello world\xc0\xaf"
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
2021-05-28 16:47:24 +00:00
|
|
|
check request.uri.path == "/ws"
|
|
|
|
|
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
2021-06-12 05:35:56 +00:00
|
|
|
await ws.send(testData)
|
2021-06-11 20:04:09 +00:00
|
|
|
await waitForClose(ws)
|
2021-05-28 16:47:24 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = HttpServer.create(
|
|
|
|
address,
|
|
|
|
handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-28 16:47:24 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await WebSocket.connect(
|
2021-07-15 00:51:39 +00:00
|
|
|
"127.0.0.1:8888",
|
2021-05-28 16:47:24 +00:00
|
|
|
path = "/ws",
|
|
|
|
protocols = @["proto"]
|
|
|
|
)
|
|
|
|
|
2021-06-12 05:35:56 +00:00
|
|
|
expect WSInvalidUTF8:
|
2021-08-04 16:23:56 +00:00
|
|
|
let data = await session.recvMsg()
|
2021-05-28 16:47:24 +00:00
|
|
|
|
|
|
|
test "invalid UTF-8 sequence close code":
|
|
|
|
let closeReason = "i want to close\xc0\xaf"
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
2021-05-28 16:47:24 +00:00
|
|
|
check request.uri.path == "/ws"
|
|
|
|
|
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
2021-06-12 05:35:56 +00:00
|
|
|
await ws.close(reason = closeReason)
|
2021-06-11 20:04:09 +00:00
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = HttpServer.create(
|
|
|
|
address,
|
|
|
|
handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-28 16:47:24 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await WebSocket.connect(
|
2021-07-15 00:51:39 +00:00
|
|
|
"127.0.0.1:8888",
|
2021-05-28 16:47:24 +00:00
|
|
|
path = "/ws",
|
|
|
|
protocols = @["proto"]
|
|
|
|
)
|
|
|
|
|
2021-06-12 05:35:56 +00:00
|
|
|
expect WSInvalidUTF8:
|
2021-08-04 16:23:56 +00:00
|
|
|
let data = await session.recvMsg()
|