diff --git a/.gitignore b/.gitignore index b883f1fdc6..3e9ad65df4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.exe +nimcache diff --git a/tests/testcommon.nim b/tests/all_tests.nim similarity index 100% rename from tests/testcommon.nim rename to tests/all_tests.nim diff --git a/tests/extensions/testcompression.nim b/tests/extensions/testcompression.nim index 72dc0d5c3e..3efcc642a0 100644 --- a/tests/extensions/testcompression.nim +++ b/tests/extensions/testcompression.nim @@ -8,8 +8,7 @@ ## those terms. import std/os -import pkg/[chronos, stew/io2] -import pkg/asynctest/unittest2 +import pkg/[chronicles, chronos/unittest2/asynctests, stew/io2] import ../../websock/websock import ../../websock/extensions/compression/deflate @@ -17,15 +16,17 @@ const dataFolder = "tests" / "extensions" / "data" suite "permessage deflate compression": - var server: HttpServer - let address = initTAddress("127.0.0.1:8888") - let deflateFactory = deflateFactory() + setup: + var server: HttpServer + let address = initTAddress("127.0.0.1:8888") + let deflateFactory = deflateFactory() teardown: - server.stop() - await server.closeWait() + if server != nil: + server.stop() + waitFor server.closeWait() - test "text compression": + asyncTest "text compression": let textData = io2.readAllBytes(dataFolder / "alice29.txt").get() proc handle(request: HttpRequest) {.async.} = let server = WSServer.new( @@ -66,7 +67,7 @@ suite "permessage deflate compression": check textData == recvData await client.close() - test "binary data compression": + asyncTest "binary data compression": let binaryData = io2.readAllBytes(dataFolder / "fireworks.jpg").get() proc handle(request: HttpRequest) {.async.} = let server = WSServer.new( diff --git a/tests/extensions/testexts.nim b/tests/extensions/testexts.nim index fa7b1c3f69..391fb95ea4 100644 --- a/tests/extensions/testexts.nim +++ b/tests/extensions/testexts.nim @@ -7,22 +7,23 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -import pkg/[chronos, stew/byteutils] -import pkg/asynctest/unittest2 +import pkg/[chronicles, chronos/unittest2/asynctests, stew/byteutils] import ./base64ext, ./hexext import ../../websock/websock, ../helpers suite "multiple extensions flow": - var server: HttpServer - let address = initTAddress("127.0.0.1:8888") - let hexFactory = hexFactory() - let base64Factory = base64Factory(padding = true) + setup: + var server: HttpServer + let address = initTAddress("127.0.0.1:8888") + let hexFactory = hexFactory() + let base64Factory = base64Factory(padding = true) teardown: - server.stop() - await server.closeWait() + if server != nil: + server.stop() + waitFor server.closeWait() - test "hex to base64 ext flow": + asyncTest "hex to base64 ext flow": let testData = "hello world" proc handle(request: HttpRequest) {.async.} = let server = WSServer.new( @@ -54,7 +55,7 @@ suite "multiple extensions flow": check testData.toBytes() == res await client.close() - test "base64 to hex ext flow": + asyncTest "base64 to hex ext flow": let testData = "hello world" proc handle(request: HttpRequest) {.async.} = let server = WSServer.new( diff --git a/tests/testextutils.nim b/tests/testextutils.nim index 957378cac4..8639d94b88 100644 --- a/tests/testextutils.nim +++ b/tests/testextutils.nim @@ -8,8 +8,7 @@ ## those terms. import - pkg/chronos, - pkg/asynctest/unittest2, + unittest2, ../websock/extensions suite "extension parser": diff --git a/tests/testframes.nim b/tests/testframes.nim index cb0f506b6c..d7766b747b 100644 --- a/tests/testframes.nim +++ b/tests/testframes.nim @@ -7,8 +7,9 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -import pkg/stew/byteutils -import pkg/asynctest/unittest2 +import + pkg/stew/byteutils, + pkg/chronos/unittest2/asynctests include ../websock/frame include ../websock/utils @@ -16,10 +17,10 @@ include ../websock/utils # TODO: Fix Test. suite "Test data frames": + setup: + var maskKey: array[4, char] - var maskKey: array[4, char] - - test "# 7bit length text": + asyncTest "# 7bit length text": check (await Frame( fin: false, rsv1: false, @@ -30,7 +31,7 @@ suite "Test data frames": data: toBytes("hi there") ).encode()) == toBytes("\1\8hi there") - test "# 7bit length text fin bit": + asyncTest "# 7bit length text fin bit": check (await Frame( fin: true, rsv1: false, @@ -42,7 +43,7 @@ suite "Test data frames": maskKey: maskKey ).encode()) == toBytes("\129\8hi there") - test "# 7bit length binary": + asyncTest "# 7bit length binary": check (await Frame( fin: false, rsv1: false, @@ -54,7 +55,7 @@ suite "Test data frames": maskKey: maskKey ).encode()) == toBytes("\2\8hi there") - test "# 7bit length binary fin bit": + asyncTest "# 7bit length binary fin bit": check (await Frame( fin: true, rsv1: false, @@ -66,7 +67,7 @@ suite "Test data frames": maskKey: maskKey ).encode()) == toBytes("\130\8hi there") - test "# 7bit length continuation": + asyncTest "# 7bit length continuation": check (await Frame( fin: false, rsv1: false, @@ -78,7 +79,7 @@ suite "Test data frames": maskKey: maskKey ).encode()) == toBytes("\0\8hi there") - test "# 7+16 length text": + asyncTest "# 7+16 length text": var data = "" for i in 0..32: data.add "How are you this is the payload!!!" @@ -94,7 +95,7 @@ suite "Test data frames": maskKey: maskKey ).encode()) == toBytes("\1\126\4\98" & data) - test "# 7+16 length text fin bit": + asyncTest "# 7+16 length text fin bit": var data = "" for i in 0..32: data.add "How are you this is the payload!!!" @@ -110,7 +111,7 @@ suite "Test data frames": maskKey: maskKey ).encode()) == toBytes("\1\126\4\98" & data) - test "# 7+16 length binary": + asyncTest "# 7+16 length binary": var data = "" for i in 0..32: data.add "How are you this is the payload!!!" @@ -126,7 +127,7 @@ suite "Test data frames": maskKey: maskKey ).encode()) == toBytes("\2\126\4\98" & data) - test "# 7+16 length binary fin bit": + asyncTest "# 7+16 length binary fin bit": var data = "" for i in 0..32: data.add "How are you this is the payload!!!" @@ -142,7 +143,7 @@ suite "Test data frames": maskKey: maskKey ).encode()) == toBytes("\130\126\4\98" & data) - test "# 7+16 length continuation": + asyncTest "# 7+16 length continuation": var data = "" for i in 0..32: data.add "How are you this is the payload!!!" @@ -158,7 +159,7 @@ suite "Test data frames": maskKey: maskKey ).encode()) == toBytes("\0\126\4\98" & data) - test "# 7+64 length text": + asyncTest "# 7+64 length text": var data = "" for i in 0..3200: data.add "How are you this is the payload!!!" @@ -174,7 +175,7 @@ suite "Test data frames": maskKey: maskKey ).encode()) == toBytes("\1\127\0\0\0\0\0\1\169\34" & data) - test "# 7+64 length fin bit": + asyncTest "# 7+64 length fin bit": var data = "" for i in 0..3200: data.add "How are you this is the payload!!!" @@ -190,7 +191,7 @@ suite "Test data frames": maskKey: maskKey ).encode()) == toBytes("\129\127\0\0\0\0\0\1\169\34" & data) - test "# 7+64 length binary": + asyncTest "# 7+64 length binary": var data = "" for i in 0..3200: data.add "How are you this is the payload!!!" @@ -206,7 +207,7 @@ suite "Test data frames": maskKey: maskKey ).encode()) == toBytes("\2\127\0\0\0\0\0\1\169\34" & data) - test "# 7+64 length binary fin bit": + asyncTest "# 7+64 length binary fin bit": var data = "" for i in 0..3200: data.add "How are you this is the payload!!!" @@ -222,7 +223,7 @@ suite "Test data frames": maskKey: maskKey ).encode()) == toBytes("\130\127\0\0\0\0\0\1\169\34" & data) - test "# 7+64 length binary": + asyncTest "# 7+64 length binary": var data = "" for i in 0..3200: data.add "How are you this is the payload!!!" @@ -238,7 +239,7 @@ suite "Test data frames": maskKey: maskKey ).encode()) == toBytes("\0\127\0\0\0\0\0\1\169\34" & data) - test "# masking": + asyncTest "# masking": let data = (await Frame( fin: true, rsv1: false, @@ -253,10 +254,10 @@ suite "Test data frames": check data == toBytes("\129\136\207\216\5e\167\177%\17\167\189w\0") suite "Test control frames": + setup: + var maskKey: array[4, char] - var maskKey: array[4, char] - - test "Close": + asyncTest "Close": check (await Frame( fin: true, rsv1: false, @@ -268,7 +269,7 @@ suite "Test control frames": maskKey: maskKey ).encode()) == toBytes("\136\10\3\232hi there") - test "Ping": + asyncTest "Ping": check (await Frame( fin: false, rsv1: false, @@ -279,7 +280,7 @@ suite "Test control frames": maskKey: maskKey ).encode()) == toBytes("\9\0") - test "Pong": + asyncTest "Pong": check (await Frame( fin: false, rsv1: false, diff --git a/tests/testhooks.nim b/tests/testhooks.nim index 1119bdb5dc..3a2d1e8ad6 100644 --- a/tests/testhooks.nim +++ b/tests/testhooks.nim @@ -7,13 +7,11 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -import std/strutils import pkg/[ httputils, - chronos, + chronos/unittest2/asynctests, chronicles, - stew/byteutils, - asynctest/unittest2] + ] import ../websock/websock @@ -96,16 +94,18 @@ proc serverHookWithCode(request: HttpRequest): Hook = ) suite "Test Hooks": - var - server: HttpServer - goodCP = goodClientHook() - badCP = badClientHook() + setup: + var + server: HttpServer + goodCP = goodClientHook() + badCP = badClientHook() teardown: - server.stop() - await server.closeWait() + if server != nil: + server.stop() + waitFor server.closeWait() - test "client with valid token": + asyncTest "client with valid token": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath let @@ -129,7 +129,7 @@ suite "Test Hooks": check TokenHook(goodCP).token == "accept" await session.stream.closeWait() - test "client with bad token": + asyncTest "client with bad token": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath let @@ -153,7 +153,7 @@ suite "Test Hooks": check TokenHook(badCP).token == "reject" await session.stream.closeWait() - test "server hook with code get good client": + asyncTest "server hook with code get good client": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath let @@ -177,7 +177,7 @@ suite "Test Hooks": check TokenHook(goodCP).token == "accept" await session.stream.closeWait() - test "server hook with code get bad client": + asyncTest "server hook with code get bad client": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath let diff --git a/tests/testutf8.nim b/tests/testutf8.nim index dcea2c9bf8..01aab18429 100644 --- a/tests/testutf8.nim +++ b/tests/testutf8.nim @@ -11,8 +11,7 @@ import std/[strutils], pkg/[ stew/byteutils, - asynctest/unittest2, - chronos, + chronos/unittest2/asynctests, chronicles ], ../websock/[websock, utf8dfa] @@ -79,15 +78,16 @@ proc waitForClose(ws: WSSession) {.async.} = trace "Closing websocket" suite "UTF-8 validator in action": - - var server: HttpServer - let address = initTAddress("127.0.0.1:8888") + setup: + var server: HttpServer + let address = initTAddress("127.0.0.1:8888") teardown: - server.stop() - await server.closeWait() + if server != nil: + server.stop() + waitFor server.closeWait() - test "valid UTF-8 sequence": + asyncTest "valid UTF-8 sequence": let testData = "hello world" proc handle(request: HttpRequest) {.async.} = check request.uri.path == "/ws" @@ -117,7 +117,7 @@ suite "UTF-8 validator in action": await session.send(testData) await session.close() - test "valid UTF-8 sequence in close reason": + asyncTest "valid UTF-8 sequence in close reason": let testData = "hello world" let closeReason = "i want to close" proc handle(request: HttpRequest) {.async.} = @@ -158,7 +158,7 @@ suite "UTF-8 validator in action": await session.send(testData) await session.close(reason = closeReason) - test "invalid UTF-8 sequence": + asyncTest "invalid UTF-8 sequence": let testData = "hello world\xc0\xaf" proc handle(request: HttpRequest) {.async.} = check request.uri.path == "/ws" @@ -183,7 +183,7 @@ suite "UTF-8 validator in action": expect WSInvalidUTF8: let data = await session.recvMsg() - test "invalid UTF-8 sequence close code": + asyncTest "invalid UTF-8 sequence close code": let closeReason = "i want to close\xc0\xaf" proc handle(request: HttpRequest) {.async.} = check request.uri.path == "/ws" diff --git a/tests/testwebsockets.nim b/tests/testwebsockets.nim index bbc28f9c4d..40c9d6413b 100644 --- a/tests/testwebsockets.nim +++ b/tests/testwebsockets.nim @@ -10,10 +10,9 @@ import std/strutils import pkg/[ httputils, - chronos, + chronos/unittest2/asynctests, chronicles, - stew/byteutils, - asynctest/unittest2] + stew/byteutils] import ../websock/websock @@ -22,15 +21,16 @@ import ./helpers let address = initTAddress("127.0.0.1:8888") suite "Test handshake": - - var - server: HttpServer + setup: + var + server: HttpServer teardown: - server.stop() - await server.closeWait() + if server != nil: + server.stop() + waitFor server.closeWait() - test "Should not select incorrect protocol": + asyncTest "Should not select incorrect protocol": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath let @@ -50,7 +50,7 @@ suite "Test handshake": check session.proto == "" await session.stream.closeWait() - test "Test for incorrect version": + asyncTest "Test for incorrect version": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath let server = WSServer.new(protos = ["ws"]) @@ -68,7 +68,7 @@ suite "Test handshake": address = initTAddress("127.0.0.1:8888"), version = 14) - test "Test for client headers": + asyncTest "Test for client headers": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath check request.headers.getString("Connection").toUpperAscii() == @@ -90,7 +90,7 @@ suite "Test handshake": expect WSFailedUpgradeError: discard await connectClient() - test "Test for incorrect scheme": + asyncTest "Test for incorrect scheme": let uri = "wx://127.0.0.1:8888/ws" expect WSWrongUriSchemeError: discard await WebSocket.connect( @@ -98,14 +98,16 @@ suite "Test handshake": protocols = @["proto"]) suite "Test transmission": - var - server: HttpServer + setup: + var + server: HttpServer teardown: - server.stop() - await server.closeWait() + if server != nil: + server.stop() + waitFor server.closeWait() - test "Server - test reading simple frame": + asyncTest "Server - asyncTest reading simple frame": let testString = "Hello!" proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath @@ -126,7 +128,7 @@ suite "Test transmission": await session.send(testString) await session.close() - test "Send text message message with payload of length 65535": + asyncTest "Send text message message with payload of length 65535": let testString = rndStr(65535) proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath @@ -145,7 +147,7 @@ suite "Test transmission": await session.send(testString) await session.close() - test "Client - test reading simple frame": + asyncTest "Client - asyncTest reading simple frame": let testString = "Hello!" proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath @@ -166,15 +168,16 @@ suite "Test transmission": await waitForClose(session) suite "Test ping-pong": - - var - server: HttpServer + setup: + var + server: HttpServer teardown: - server.stop() - await server.closeWait() + if server != nil: + server.stop() + waitFor server.closeWait() - test "Server - test ping-pong control messages": + asyncTest "Server - asyncTest ping-pong control messages": var ping, pong = false proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath @@ -204,7 +207,7 @@ suite "Test ping-pong": ping pong - test "Client - test ping-pong control messages": + asyncTest "Client - asyncTest ping-pong control messages": var ping, pong = false proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath @@ -234,7 +237,7 @@ suite "Test ping-pong": await session.ping() await session.close() - test "Send ping with small text payload": + asyncTest "Send ping with small text payload": let testData = toBytes("Hello, world!") var ping, pong = false proc handle(request: HttpRequest) {.async.} = @@ -265,7 +268,7 @@ suite "Test ping-pong": ping pong - test "Test ping payload message length": + asyncTest "Test ping payload message length": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath let server = WSServer.new(protos = ["proto"]) @@ -287,15 +290,16 @@ suite "Test ping-pong": await session.close() suite "Test framing": - - var - server: HttpServer + setup: + var + server: HttpServer teardown: - server.stop() - await server.closeWait() + if server != nil: + server.stop() + waitFor server.closeWait() - test "should split message into frames": + asyncTest "should split message into frames": let testString = "1234567890" proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath @@ -328,7 +332,7 @@ suite "Test framing": await session.send(testString) await session.close() - test "should fail to read past max message size": + asyncTest "should fail to read past max message size": let testString = "1234567890" proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath @@ -349,15 +353,16 @@ suite "Test framing": await waitForClose(session) suite "Test Closing": - - var - server: HttpServer + setup: + var + server: HttpServer teardown: - server.stop() - await server.closeWait() + if server != nil: + server.stop() + waitFor server.closeWait() - test "Server closing": + asyncTest "Server closing": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath let server = WSServer.new(protos = ["proto"]) @@ -373,7 +378,7 @@ suite "Test Closing": await waitForClose(session) check session.readyState == ReadyState.Closed - test "Server closing with status": + asyncTest "Server closing with status": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath @@ -415,7 +420,7 @@ suite "Test Closing": await waitForClose(session) check session.readyState == ReadyState.Closed - test "Client closing": + asyncTest "Client closing": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath let server = WSServer.new(protos = ["proto"]) @@ -430,7 +435,7 @@ suite "Test Closing": let session = await connectClient() await session.close() - test "Client closing with status": + asyncTest "Client closing with status": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath proc closeServer(status: StatusCodes, reason: string): CloseResult{.gcsafe, @@ -470,7 +475,7 @@ suite "Test Closing": await session.close() check session.readyState == ReadyState.Closed - test "Mutual closing": + asyncTest "Mutual closing": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath let server = WSServer.new(protos = ["proto"]) @@ -487,7 +492,7 @@ suite "Test Closing": await waitForClose(session) check session.readyState == ReadyState.Closed - test "Server closing with valid close code 3999": + asyncTest "Server closing with valid close code 3999": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath let server = WSServer.new(protos = ["proto"]) @@ -514,7 +519,7 @@ suite "Test Closing": await waitForClose(session) - test "Client closing with valid close code 3999": + asyncTest "Client closing with valid close code 3999": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath proc closeServer(status: StatusCodes, reason: string): CloseResult{.gcsafe, @@ -541,7 +546,7 @@ suite "Test Closing": let session = await connectClient() await session.close(code = StatusCodes(3999)) - test "Server closing with Payload of length 2": + asyncTest "Server closing with Payload of length 2": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath @@ -559,7 +564,7 @@ suite "Test Closing": let session = await connectClient() await waitForClose(session) - test "Client closing with Payload of length 2": + asyncTest "Client closing with Payload of length 2": proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath @@ -579,14 +584,16 @@ suite "Test Closing": await session.close(reason = "HH") suite "Test Payload": - var - server: HttpServer + setup: + var + server: HttpServer teardown: - server.stop() - await server.closeWait() + if server != nil: + server.stop() + waitFor server.closeWait() - test "Test payload of length 0": + asyncTest "Test payload of length 0": let emptyStr = "" proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath @@ -617,7 +624,7 @@ suite "Test Payload": await session.close() - test "Test multiple payloads of length 0": + asyncTest "Test multiple payloads of length 0": let emptyStr = "" proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath @@ -655,7 +662,7 @@ suite "Test Payload": await session.close() - test "Send two fragments": + asyncTest "Send two fragments": var ping, pong = false let testString = "1234567890" let msg = toBytes(testString) @@ -709,7 +716,7 @@ suite "Test Payload": await session.close() - test "Send two fragments with a ping with payload in-between": + asyncTest "Send two fragments with a ping with payload in-between": var ping, pong = false let testString = "1234567890" let msg = toBytes(testString) @@ -774,7 +781,7 @@ suite "Test Payload": ping pong - test "Send text message with multiple frames": + asyncTest "Send text message with multiple frames": const FrameSize = 3000 let testData = rndStr(FrameSize * 3) proc handle(request: HttpRequest) {.async.} = @@ -807,14 +814,16 @@ suite "Test Payload": ws.binary == false suite "Test Binary message with Payload": - var - server: HttpServer + setup: + var + server: HttpServer teardown: - server.stop() - await server.closeWait() + if server != nil: + server.stop() + waitFor server.closeWait() - test "Test binary message with single empty payload message": + asyncTest "Test binary message with single empty payload message": let emptyData = newSeq[byte](0) proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath @@ -838,7 +847,7 @@ suite "Test Binary message with Payload": await session.send(emptyData, Opcode.Binary) await session.close() - test "Test binary message with multiple empty payload": + asyncTest "Test binary message with multiple empty payload": let emptyData = newSeq[byte](0) proc handle(request: HttpRequest) {.async.} = check request.uri.path == WSPath @@ -865,7 +874,7 @@ suite "Test Binary message with Payload": await session.send(emptyData, Opcode.Binary) await session.close() - test "Send binary data with small text payload": + asyncTest "Send binary data with small text payload": let testData = rndBin(10) trace "testData", testData = testData var ping, pong = false @@ -900,7 +909,7 @@ suite "Test Binary message with Payload": await session.send(testData, Opcode.Binary) await session.close() - test "Send binary message message with payload of length 125": + asyncTest "Send binary message message with payload of length 125": let testData = rndBin(125) var ping, pong = false proc handle(request: HttpRequest) {.async.} = @@ -934,7 +943,7 @@ suite "Test Binary message with Payload": await session.send(testData, Opcode.Binary) await session.close() - test "Send binary message with multiple frames": + asyncTest "Send binary message with multiple frames": const FrameSize = 3000 let testData = rndBin(FrameSize * 3) proc handle(request: HttpRequest) {.async.} = @@ -974,62 +983,64 @@ suite "Test Binary message with Payload": ws.binary == true suite "Partial frames": - var - server: HttpServer + setup: + var + server: HttpServer + + proc lowLevelRecv( + senderFrameSize, receiverFrameSize, readChunkSize: int) {.async.} = + + const + howMuchWood = "How much wood could a wood chuck chuck ..." + + proc handle(request: HttpRequest) {.async.} = + check request.uri.path == WSPath + + let + server = WSServer.new(frameSize = receiverFrameSize) + ws = await server.handleRequest(request) + + var + res = newSeq[byte](howMuchWood.len) + pos = 0 + + while ws.readyState != ReadyState.Closed: + let read = await ws.recv(addr res[pos], min(res.len - pos, readChunkSize)) + pos += read + + if pos >= res.len: + break + + res.setlen(pos) + check res.len == howMuchWood.toBytes().len + check res == howMuchWood.toBytes() + await ws.waitForClose() + + server = createServer( + address = address, + handler = handle, + flags = {ReuseAddr}) + + let session = await connectClient( + address = address, + frameSize = senderFrameSize) + + await session.send(howMuchWood) + await session.close() teardown: - server.stop() - await server.closeWait() + if server != nil: + server.stop() + waitFor server.closeWait() - proc lowLevelRecv( - senderFrameSize, receiverFrameSize, readChunkSize: int) {.async.} = - - const - howMuchWood = "How much wood could a wood chuck chuck ..." - - proc handle(request: HttpRequest) {.async.} = - check request.uri.path == WSPath - - let - server = WSServer.new(frameSize = receiverFrameSize) - ws = await server.handleRequest(request) - - var - res = newSeq[byte](howMuchWood.len) - pos = 0 - - while ws.readyState != ReadyState.Closed: - let read = await ws.recv(addr res[pos], min(res.len - pos, readChunkSize)) - pos += read - - if pos >= res.len: - break - - res.setlen(pos) - check res.len == howMuchWood.toBytes().len - check res == howMuchWood.toBytes() - await ws.waitForClose() - - server = createServer( - address = address, - handler = handle, - flags = {ReuseAddr}) - - let session = await connectClient( - address = address, - frameSize = senderFrameSize) - - await session.send(howMuchWood) - await session.close() - - test "read in chunks less than sender frameSize": + asyncTest "read in chunks less than sender frameSize": await lowLevelRecv(7, 7, 5) - test "read in chunks greater than sender frameSize": + asyncTest "read in chunks greater than sender frameSize": await lowLevelRecv(3, 7, 5) - test "sender frameSize greater than receiver": + asyncTest "sender frameSize greater than receiver": await lowLevelRecv(7, 5, 5) - test "receiver frameSize greater than sender": + asyncTest "receiver frameSize greater than sender": await lowLevelRecv(7, 10, 5) diff --git a/websock.nimble b/websock.nimble index 4c553e76f9..4644a836e5 100644 --- a/websock.nimble +++ b/websock.nimble @@ -19,28 +19,31 @@ requires "chronos >= 3.0.0" requires "httputils >= 0.2.0" requires "chronicles >= 0.10.2" requires "stew >= 0.1.0" -requires "asynctest >= 0.3.0 & < 0.4.0" requires "nimcrypto" requires "bearssl" requires "zlib" task test, "run tests": - let envNimflags = getEnv("NIMFLAGS") + let + envNimflags = getEnv("NIMFLAGS") + nimFlags = envNimFlags & + " --verbosity:0 --hints:off --hint:Name:on " & + "--styleCheck:usages --styleCheck:hint -d:chronosStrictException" # dont't need to run it, only want to test if it is compileable - exec "nim c -c " & envNimflags & " --verbosity:0 --hints:off --hint:Name:on -d:chronicles_log_level=TRACE -d:chronicles_sinks:json --styleCheck:usages --styleCheck:hint ./tests/testcommon" + exec "nim c -c " & nimFlags & " -d:chronicles_log_level=TRACE -d:chronicles_sinks:json --styleCheck:usages --styleCheck:hint ./tests/all_tests" - exec "nim --hints:off c -r " & envNimflags & " --opt:speed -d:debug --verbosity:0 --hints:off -d:chronicles_log_level=INFO ./tests/testcommon.nim" - rmFile "./tests/testcommon" + exec "nim c -r " & nimFlags & " --opt:speed -d:debug -d:chronicles_log_level=INFO ./tests/all_tests.nim" + rmFile "./tests/all_tests" - exec "nim --hints:off c -r " & envNimflags & " --opt:speed -d:debug --verbosity:0 --hints:off -d:chronicles_log_level=INFO ./tests/testwebsockets.nim" + exec "nim c -r " & nimFlags & " --opt:speed -d:debug -d:chronicles_log_level=INFO ./tests/testwebsockets.nim" rmFile "./tests/testwebsockets" - exec "nim --hints:off -d:secure c -r " & envNimflags & " --opt:speed -d:debug --verbosity:0 --hints:off -d:chronicles_log_level=INFO ./tests/testwebsockets.nim" + exec "nim -d:secure c -r " & nimFlags & " --opt:speed -d:debug -d:chronicles_log_level=INFO ./tests/testwebsockets.nim" rmFile "./tests/testwebsockets" - exec "nim --hints:off -d:accepts c -r " & envNimflags & " --opt:speed -d:debug --verbosity:0 --hints:off -d:chronicles_log_level=INFO ./tests/testwebsockets.nim" + exec "nim -d:accepts c -r " & nimFlags & " --opt:speed -d:debug -d:chronicles_log_level=INFO ./tests/testwebsockets.nim" rmFile "./tests/testwebsockets" - exec "nim --hints:off -d:secure -d:accepts c -r " & envNimflags & " --opt:speed -d:debug --verbosity:0 --hints:off -d:chronicles_log_level=INFO ./tests/testwebsockets.nim" + exec "nim -d:secure -d:accepts c -r " & nimFlags & " --opt:speed -d:debug -d:chronicles_log_level=INFO ./tests/testwebsockets.nim" rmFile "./tests/testwebsockets" diff --git a/websock/utils.nim b/websock/utils.nim index 24edf1e97a..c3504191f5 100644 --- a/websock/utils.nim +++ b/websock/utils.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -import bearssl/[hash, rand] +import bearssl/[rand] export rand ## Random helpers: similar as in stdlib, but with HmacDrbgContext rng