Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/autobahn_client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

import
std/[strutils],
pkg/[chronos, chronicles, stew/byteutils],
../websock/[websock, types, frame, extensions/compression/deflate]
chronos,
chronicles,
stew/byteutils,
../websock/[websock, types, extensions/compression/deflate]

const
clientFlags = {NoVerifyHost, NoVerifyServerName}
Expand Down
4 changes: 2 additions & 2 deletions examples/client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
## This file may not be copied, modified, or distributed except according to
## those terms.

import pkg/[
import
chronos,
chronicles,
stew/byteutils]
stew/byteutils

import ../websock/websock

Expand Down
13 changes: 7 additions & 6 deletions examples/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
## This file may not be copied, modified, or distributed except according to
## those terms.

import std/uri
import pkg/[chronos,
chronicles,
httputils]
import
std/uri,
chronos,
chronicles,
../websock/[websock, extensions/compression/deflate]

import ../websock/[websock, extensions/compression/deflate]
import ../tests/keys
when defined tls:
import ../tests/keys

proc handle(request: HttpRequest) {.async.} =
trace "Handling request:", uri = request.uri.path
Expand Down
16 changes: 9 additions & 7 deletions scripts/ws.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# prevent issue https://github.com/status-im/nimbus-eth1/issues/3661

set -e
trap "trap - SIGTERM && pkill -P $$" SIGINT SIGTERM EXIT

# script arguments
[[ $# -ne 1 ]] && { echo "Usage: $0 NIM_VERSION"; }
Expand All @@ -17,22 +18,23 @@ NIM_VERSION="$1"
cd "$(dirname "${BASH_SOURCE[0]}")"/..

REPO_DIR="${PWD}"
CFG="server"
REPORT_DIR="autobahn/reports/$CFG-$NIM_VERSION"
mkdir -p autobahn/reports/$CFG

nim c -d:release examples/server
examples/server &
server=$!

mkdir -p autobahn/reports

docker run \
docker run --rm \
-v ${REPO_DIR}/autobahn:/config \
-v ${REPO_DIR}/autobahn/reports:/reports \
--network=host \
--name fuzzingclient \
crossbario/autobahn-testsuite wstest --mode fuzzingclient --spec /config/fuzzingclient.json

kill $server
mv autobahn/reports/$CFG "$REPORT_DIR"

mv autobahn/reports/server autobahn/reports/server-${NIM_VERSION}
echo "* [Nim-${NIM_VERSION} $CFG summary report]($CFG-${NIM_VERSION}/index.html)" > "$REPORT_DIR.txt"

echo "* [Nim-${NIM_VERSION} ws server summary report](server-${NIM_VERSION}/index.html)" > "autobahn/reports/server-${NIM_VERSION}.txt"
# squash to single line and look for errors
(cat $REPORT_DIR/index.json | tr '\n' '!' | sed "s|\},\!|\n|g" | tr '!' ' ' | tr -s ' ' | grep -v -e '"behavior": "OK"' -e '"behavior": "NON-STRICT"' -e '"behavior": "INFORMATIONAL"' && exit 1) || true
17 changes: 11 additions & 6 deletions scripts/wsc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# prevent issue https://github.com/status-im/nimbus-eth1/issues/3661

set -e
trap "trap - SIGTERM && pkill -P $$" SIGINT SIGTERM EXIT

# script arguments
[[ $# -ne 1 ]] && { echo "Usage: $0 NIM_VERSION"; }
Expand All @@ -17,21 +18,25 @@ NIM_VERSION="$1"
cd "$(dirname "${BASH_SOURCE[0]}")"/..

REPO_DIR="${PWD}"
CFG="client"
REPORT_DIR="autobahn/reports/$CFG-$NIM_VERSION"
mkdir -p autobahn/reports/$CFG

mkdir -p autobahn/reports

docker run -d \
docker run -d --rm \
-v ${REPO_DIR}/autobahn:/config \
-v ${REPO_DIR}/autobahn/reports:/reports \
--network=host \
--name fuzzingserver \
crossbario/autobahn-testsuite wstest --webport=0 --mode fuzzingserver --spec /config/fuzzingserver.json

trap "docker kill fuzzingserver" SIGINT SIGTERM EXIT

nim c -d:release examples/autobahn_client
examples/autobahn_client

docker kill fuzzingserver
mv autobahn/reports/$CFG $REPORT_DIR

mv autobahn/reports/client autobahn/reports/client-${NIM_VERSION}
echo "* [Nim-${NIM_VERSION} $CFG summary report]($CFG-${NIM_VERSION}/index.html)" > "$REPORT_DIR.txt"

echo "* [Nim-${NIM_VERSION} ws client summary report](client-${NIM_VERSION}/index.html)" > "autobahn/reports/client-${NIM_VERSION}.txt"
# squash to single line and look for errors
(cat $REPORT_DIR/index.json | tr '\n' '!' | sed "s|\},\!|\n|g" | tr '!' ' ' | tr -s ' ' | grep -v -e '"behavior": "OK"' -e '"behavior": "NON-STRICT"' -e '"behavior": "INFORMATIONAL"' && exit 1) || true
17 changes: 9 additions & 8 deletions scripts/wss.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

# prevent issue https://github.com/status-im/nimbus-eth1/issues/3661


set -e
trap "trap - SIGTERM && pkill -P $$" SIGINT SIGTERM EXIT

# script arguments
[[ $# -ne 1 ]] && { echo "Usage: $0 NIM_VERSION"; }
Expand All @@ -18,22 +18,23 @@ NIM_VERSION="$1"
cd "$(dirname "${BASH_SOURCE[0]}")"/..

REPO_DIR="${PWD}"
CFG="server_tls"
REPORT_DIR="autobahn/reports/$CFG-$NIM_VERSION"
mkdir -p autobahn/reports/$CFG

nim c -d:tls -d:release -o:examples/tls_server examples/server.nim
examples/tls_server &
server=$!

mkdir -p autobahn/reports

docker run \
docker run --rm \
-v ${REPO_DIR}/autobahn:/config \
-v ${REPO_DIR}/autobahn/reports:/reports \
--network=host \
--name fuzzingclient_tls \
crossbario/autobahn-testsuite wstest --mode fuzzingclient --spec /config/fuzzingclient_tls.json

kill $server
mv autobahn/reports/$CFG "$REPORT_DIR"

mv autobahn/reports/server_tls autobahn/reports/server_tls-${NIM_VERSION}
echo "* [Nim-${NIM_VERSION} $CFG summary report]($CFG-${NIM_VERSION}/index.html)" > "$REPORT_DIR.txt"

echo "* [Nim-${NIM_VERSION} wss server summary report](server_tls-${NIM_VERSION}/index.html)" > "autobahn/reports/server_tls-${NIM_VERSION}.txt"
# squash to single line and look for errors
(cat $REPORT_DIR/index.json | tr '\n' '!' | sed "s|\},\!|\n|g" | tr '!' ' ' | tr -s ' ' | grep -v -e '"behavior": "OK"' -e '"behavior": "NON-STRICT"' -e '"behavior": "INFORMATIONAL"' && exit 1) || true
17 changes: 11 additions & 6 deletions scripts/wssc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# prevent issue https://github.com/status-im/nimbus-eth1/issues/3661

set -e
trap "trap - SIGTERM && pkill -P $$" SIGINT SIGTERM EXIT

# script arguments
[[ $# -ne 1 ]] && { echo "Usage: $0 NIM_VERSION"; }
Expand All @@ -17,21 +18,25 @@ NIM_VERSION="$1"
cd "$(dirname "${BASH_SOURCE[0]}")"/..

REPO_DIR="${PWD}"
CFG="client_tls"
REPORT_DIR="autobahn/reports/$CFG-$NIM_VERSION"
mkdir -p autobahn/reports/$CFG

mkdir -p autobahn/reports

docker run -d \
docker run -d --rm \
-v ${REPO_DIR}/autobahn:/config \
-v ${REPO_DIR}/autobahn/reports:/reports \
--network=host \
--name fuzzingserver_tls \
crossbario/autobahn-testsuite wstest --webport=0 --mode fuzzingserver --spec /config/fuzzingserver_tls.json

trap "docker kill fuzzingserver_tls" SIGINT SIGTERM EXIT

nim c -d:tls -d:release -o:examples/autobahn_tlsclient examples/autobahn_client
examples/autobahn_tlsclient

docker kill fuzzingserver_tls
mv autobahn/reports/$CFG $REPORT_DIR

mv autobahn/reports/client_tls autobahn/reports/client_tls-${NIM_VERSION}
echo "* [Nim-${NIM_VERSION} $CFG summary report]($CFG-${NIM_VERSION}/index.html)" > "$REPORT_DIR.txt"

echo "* [Nim-${NIM_VERSION} wss client summary report](client_tls-${NIM_VERSION}/index.html)" > "autobahn/reports/client_tls-${NIM_VERSION}.txt"
# squash to single line and look for errors
(cat $REPORT_DIR/index.json | tr '\n' '!' | sed "s|\},\!|\n|g" | tr '!' ' ' | tr -s ' ' | grep -v -e '"behavior": "OK"' -e '"behavior": "NON-STRICT"' -e '"behavior": "INFORMATIONAL"' && exit 1) || true
30 changes: 18 additions & 12 deletions tests/extensions/base64ext.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
## those terms.

import
pkg/[stew/base64,
chronos,
chronicles,
results],
../../websock/types,
../../websock/frame
stew/base64,
chronos,
chronicles,
results,
../../websock/[frame, types]

type
Base64Ext = ref object of Ext
Expand All @@ -23,7 +22,9 @@ type
const
extID = "base64"

method decode(ext: Base64Ext, frame: Frame): Future[Frame] {.async.} =
method decode(
ext: Base64Ext, frame: Frame
): Future[Frame] {.async: (raises: [CancelledError, AsyncStreamError, WebSocketError]).} =
if frame.opcode notin {Opcode.Text, Opcode.Binary, Opcode.Cont}:
return frame

Expand All @@ -48,10 +49,13 @@ method decode(ext: Base64Ext, frame: Frame): Future[Frame] {.async.} =

# bug in Base64.Decode when accepts seq[byte]
let instr = cast[string](data)
if ext.padding:
frame.data = Base64Pad.decode(instr)
else:
frame.data = Base64.decode(instr)
try:
if ext.padding:
frame.data = Base64Pad.decode(instr)
else:
frame.data = Base64.decode(instr)
except Base64Error:
raise newException(WSExtError, "invalid data")

trace "Base64Ext decode", input=frame.length, output=frame.data.len

Expand All @@ -62,7 +66,9 @@ method decode(ext: Base64Ext, frame: Frame): Future[Frame] {.async.} =

return frame

method encode(ext: Base64Ext, frame: Frame): Future[Frame] {.async.} =
method encode(
ext: Base64Ext, frame: Frame
): Future[Frame] {.async: (raises: [CancelledError, WebSocketError]).} =
if frame.opcode notin {Opcode.Text, Opcode.Binary, Opcode.Cont}:
return frame

Expand Down
22 changes: 12 additions & 10 deletions tests/extensions/hexext.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@
## This file may not be copied, modified, or distributed except according to
## those terms.

import
pkg/[results,
stew/byteutils,
chronos,
chronicles],
../../websock/types,
../../websock/frame
import results, stew/byteutils, chronos, chronicles, ../../websock/[frame, types]

type
HexExt = ref object of Ext
Expand All @@ -22,7 +16,9 @@ type
const
extID = "hex"

method decode(ext: HexExt, frame: Frame): Future[Frame] {.async.} =
method decode(
ext: HexExt, frame: Frame
): Future[Frame] {.async: (raises: [CancelledError, AsyncStreamError, WebSocketError]).} =
if frame.opcode notin {Opcode.Text, Opcode.Binary, Opcode.Cont}:
return frame

Expand All @@ -45,7 +41,11 @@ method decode(ext: HexExt, frame: Frame): Future[Frame] {.async.} =
if data.len > ext.session.frameSize:
raise newException(WSPayloadTooLarge, "payload exceeds allowed max frame size")

frame.data = hexToSeqByte(cast[string](data))
frame.data = try:
hexToSeqByte(cast[string](data))
except ValueError:
raise newException(WSExtError, "invalid data")

trace "HexExt decode", input=frame.length, output=frame.data.len

frame.length = frame.data.len.uint64
Expand All @@ -55,7 +55,9 @@ method decode(ext: HexExt, frame: Frame): Future[Frame] {.async.} =

return frame

method encode(ext: HexExt, frame: Frame): Future[Frame] {.async.} =
method encode(
ext: HexExt, frame: Frame
): Future[Frame] {.async: (raises: [CancelledError, WebSocketError]).} =
if frame.opcode notin {Opcode.Text, Opcode.Binary, Opcode.Cont}:
return frame

Expand Down
10 changes: 6 additions & 4 deletions tests/extensions/testcompression.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
## This file may not be copied, modified, or distributed except according to
## those terms.

import std/[os, strutils]
import pkg/[chronos/unittest2/asynctests, stew/io2]
import ../../websock/websock
import ../../websock/extensions/compression/deflate
import
std/[os, strutils],
chronos/unittest2/asynctests,
stew/io2,
../../websock/websock,
../../websock/extensions/compression/deflate

const
dataFolder = currentSourcePath.rsplit(os.DirSep, 1)[0] / "data"
Expand Down
12 changes: 5 additions & 7 deletions tests/extensions/testextflow.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
## This file may not be copied, modified, or distributed except according to
## those terms.

import std/strutils
import pkg/[chronos, stew/byteutils]
import pkg/asynctest/unittest2
import std/strutils, chronos, stew/byteutils, asynctest/unittest

import ../../ws/ws

Expand All @@ -28,13 +26,13 @@ proc new*(
name: "HelperExtension")

method decode*(
self: HelperExtension,
frame: Frame): Future[Frame] {.async.} =
self: HelperExtension, frame: Frame
): Future[Frame] {.async: (raises: [CancelledError, AsyncStreamError, WebSocketError]).} =
return await self.handler(self, frame)

method encode*(
self: HelperExtension,
frame: Frame): Future[Frame] {.async.} =
self: HelperExtension, frame: Frame
): Future[Frame] {.async: (raises: [CancelledError, WebSocketError]).} =
return await self.handler(self, frame)

const TestString = "Hello"
Expand Down
3 changes: 1 addition & 2 deletions tests/extensions/testexts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
## This file may not be copied, modified, or distributed except according to
## those terms.

import pkg/[chronos/unittest2/asynctests, stew/byteutils]
import ./base64ext, ./hexext
import chronos/unittest2/asynctests, stew/byteutils, ./[base64ext, hexext]
import ../../websock/websock, ../helpers

suite "multiple extensions flow":
Expand Down
Loading