2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-01-04 04:31:12 -05:00
|
|
|
import { BufReader, BufWriter } from "../io/bufio.ts";
|
2019-09-28 12:46:21 -04:00
|
|
|
import { assert, assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
|
2019-05-14 15:19:12 -04:00
|
|
|
import { runIfMain, test } from "../testing/mod.ts";
|
2020-01-04 04:31:12 -05:00
|
|
|
import { TextProtoReader } from "../textproto/mod.ts";
|
2019-02-10 18:45:24 -05:00
|
|
|
import {
|
|
|
|
acceptable,
|
2019-09-28 12:46:21 -04:00
|
|
|
connectWebSocket,
|
2019-02-10 18:45:24 -05:00
|
|
|
createSecAccept,
|
2020-01-04 04:31:12 -05:00
|
|
|
handshake,
|
2019-02-10 18:45:24 -05:00
|
|
|
OpCode,
|
|
|
|
readFrame,
|
2019-05-14 15:19:12 -04:00
|
|
|
unmask,
|
|
|
|
writeFrame
|
2019-02-10 18:45:24 -05:00
|
|
|
} from "./mod.ts";
|
2019-05-26 19:58:31 -04:00
|
|
|
import { encode } from "../strings/mod.ts";
|
2019-05-14 15:19:12 -04:00
|
|
|
|
|
|
|
const { Buffer } = Deno;
|
2019-01-06 14:26:18 -05:00
|
|
|
|
2019-05-14 15:19:12 -04:00
|
|
|
test(async function wsReadUnmaskedTextFrame(): Promise<void> {
|
2019-01-06 14:26:18 -05:00
|
|
|
// unmasked single text frame with payload "Hello"
|
|
|
|
const buf = new BufReader(
|
|
|
|
new Buffer(new Uint8Array([0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]))
|
|
|
|
);
|
|
|
|
const frame = await readFrame(buf);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(frame.opcode, OpCode.TextFrame);
|
|
|
|
assertEquals(frame.mask, undefined);
|
|
|
|
assertEquals(new Buffer(frame.payload).toString(), "Hello");
|
|
|
|
assertEquals(frame.isLastFrame, true);
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2019-05-14 15:19:12 -04:00
|
|
|
test(async function wsReadMaskedTextFrame(): Promise<void> {
|
2019-01-06 14:26:18 -05:00
|
|
|
//a masked single text frame with payload "Hello"
|
|
|
|
const buf = new BufReader(
|
|
|
|
new Buffer(
|
|
|
|
new Uint8Array([
|
|
|
|
0x81,
|
|
|
|
0x85,
|
|
|
|
0x37,
|
|
|
|
0xfa,
|
|
|
|
0x21,
|
|
|
|
0x3d,
|
|
|
|
0x7f,
|
|
|
|
0x9f,
|
|
|
|
0x4d,
|
|
|
|
0x51,
|
|
|
|
0x58
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const frame = await readFrame(buf);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(frame.opcode, OpCode.TextFrame);
|
2019-01-06 14:26:18 -05:00
|
|
|
unmask(frame.payload, frame.mask);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(new Buffer(frame.payload).toString(), "Hello");
|
|
|
|
assertEquals(frame.isLastFrame, true);
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2019-05-14 15:19:12 -04:00
|
|
|
test(async function wsReadUnmaskedSplitTextFrames(): Promise<void> {
|
2019-01-06 14:26:18 -05:00
|
|
|
const buf1 = new BufReader(
|
|
|
|
new Buffer(new Uint8Array([0x01, 0x03, 0x48, 0x65, 0x6c]))
|
|
|
|
);
|
|
|
|
const buf2 = new BufReader(
|
|
|
|
new Buffer(new Uint8Array([0x80, 0x02, 0x6c, 0x6f]))
|
|
|
|
);
|
|
|
|
const [f1, f2] = await Promise.all([readFrame(buf1), readFrame(buf2)]);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(f1.isLastFrame, false);
|
|
|
|
assertEquals(f1.mask, undefined);
|
|
|
|
assertEquals(f1.opcode, OpCode.TextFrame);
|
|
|
|
assertEquals(new Buffer(f1.payload).toString(), "Hel");
|
2019-01-06 14:26:18 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(f2.isLastFrame, true);
|
|
|
|
assertEquals(f2.mask, undefined);
|
|
|
|
assertEquals(f2.opcode, OpCode.Continue);
|
|
|
|
assertEquals(new Buffer(f2.payload).toString(), "lo");
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2019-05-14 15:19:12 -04:00
|
|
|
test(async function wsReadUnmaskedPingPongFrame(): Promise<void> {
|
2019-01-06 14:26:18 -05:00
|
|
|
// unmasked ping with payload "Hello"
|
|
|
|
const buf = new BufReader(
|
|
|
|
new Buffer(new Uint8Array([0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]))
|
|
|
|
);
|
|
|
|
const ping = await readFrame(buf);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(ping.opcode, OpCode.Ping);
|
|
|
|
assertEquals(new Buffer(ping.payload).toString(), "Hello");
|
2019-01-06 14:26:18 -05:00
|
|
|
|
|
|
|
const buf2 = new BufReader(
|
|
|
|
new Buffer(
|
|
|
|
new Uint8Array([
|
|
|
|
0x8a,
|
|
|
|
0x85,
|
|
|
|
0x37,
|
|
|
|
0xfa,
|
|
|
|
0x21,
|
|
|
|
0x3d,
|
|
|
|
0x7f,
|
|
|
|
0x9f,
|
|
|
|
0x4d,
|
|
|
|
0x51,
|
|
|
|
0x58
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const pong = await readFrame(buf2);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(pong.opcode, OpCode.Pong);
|
2019-01-06 14:26:18 -05:00
|
|
|
assert(pong.mask !== undefined);
|
|
|
|
unmask(pong.payload, pong.mask);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(new Buffer(pong.payload).toString(), "Hello");
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2019-05-14 15:19:12 -04:00
|
|
|
test(async function wsReadUnmaskedBigBinaryFrame(): Promise<void> {
|
2019-05-23 22:04:06 -04:00
|
|
|
const payloadLength = 0x100;
|
2019-03-04 18:23:04 -05:00
|
|
|
const a = [0x82, 0x7e, 0x01, 0x00];
|
2019-05-23 22:04:06 -04:00
|
|
|
for (let i = 0; i < payloadLength; i++) {
|
2019-01-06 14:26:18 -05:00
|
|
|
a.push(i);
|
|
|
|
}
|
|
|
|
const buf = new BufReader(new Buffer(new Uint8Array(a)));
|
|
|
|
const bin = await readFrame(buf);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(bin.opcode, OpCode.BinaryFrame);
|
|
|
|
assertEquals(bin.isLastFrame, true);
|
|
|
|
assertEquals(bin.mask, undefined);
|
2019-05-23 22:04:06 -04:00
|
|
|
assertEquals(bin.payload.length, payloadLength);
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2019-05-14 15:19:12 -04:00
|
|
|
test(async function wsReadUnmaskedBigBigBinaryFrame(): Promise<void> {
|
2019-05-23 22:04:06 -04:00
|
|
|
const payloadLength = 0x10000;
|
2019-03-04 18:23:04 -05:00
|
|
|
const a = [0x82, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00];
|
2019-05-23 22:04:06 -04:00
|
|
|
for (let i = 0; i < payloadLength; i++) {
|
2019-01-06 14:26:18 -05:00
|
|
|
a.push(i);
|
|
|
|
}
|
|
|
|
const buf = new BufReader(new Buffer(new Uint8Array(a)));
|
|
|
|
const bin = await readFrame(buf);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(bin.opcode, OpCode.BinaryFrame);
|
|
|
|
assertEquals(bin.isLastFrame, true);
|
|
|
|
assertEquals(bin.mask, undefined);
|
2019-05-23 22:04:06 -04:00
|
|
|
assertEquals(bin.payload.length, payloadLength);
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2019-05-14 15:19:12 -04:00
|
|
|
test(async function wsCreateSecAccept(): Promise<void> {
|
2019-01-06 14:26:18 -05:00
|
|
|
const nonce = "dGhlIHNhbXBsZSBub25jZQ==";
|
|
|
|
const d = createSecAccept(nonce);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(d, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
2019-02-10 18:45:24 -05:00
|
|
|
|
2019-05-14 15:19:12 -04:00
|
|
|
test(function wsAcceptable(): void {
|
2019-02-10 18:45:24 -05:00
|
|
|
const ret = acceptable({
|
|
|
|
headers: new Headers({
|
|
|
|
upgrade: "websocket",
|
|
|
|
"sec-websocket-key": "aaa"
|
|
|
|
})
|
|
|
|
});
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(ret, true);
|
2019-05-14 17:45:52 -04:00
|
|
|
|
|
|
|
assert(
|
|
|
|
acceptable({
|
|
|
|
headers: new Headers([
|
|
|
|
["connection", "Upgrade"],
|
|
|
|
["host", "127.0.0.1:9229"],
|
|
|
|
[
|
|
|
|
"sec-websocket-extensions",
|
|
|
|
"permessage-deflate; client_max_window_bits"
|
|
|
|
],
|
|
|
|
["sec-websocket-key", "dGhlIHNhbXBsZSBub25jZQ=="],
|
|
|
|
["sec-websocket-version", "13"],
|
|
|
|
["upgrade", "WebSocket"]
|
|
|
|
])
|
|
|
|
})
|
|
|
|
);
|
2019-02-10 18:45:24 -05:00
|
|
|
});
|
|
|
|
|
2019-05-14 15:19:12 -04:00
|
|
|
test(function wsAcceptableInvalid(): void {
|
2019-05-30 08:59:30 -04:00
|
|
|
assertEquals(
|
|
|
|
acceptable({
|
|
|
|
headers: new Headers({ "sec-websocket-key": "aaa" })
|
|
|
|
}),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
acceptable({
|
|
|
|
headers: new Headers({ upgrade: "websocket" })
|
|
|
|
}),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
acceptable({
|
|
|
|
headers: new Headers({ upgrade: "invalid", "sec-websocket-key": "aaa" })
|
|
|
|
}),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
acceptable({
|
|
|
|
headers: new Headers({ upgrade: "websocket", "sec-websocket-ky": "" })
|
|
|
|
}),
|
|
|
|
false
|
|
|
|
);
|
2019-02-10 18:45:24 -05:00
|
|
|
});
|
2019-05-14 15:19:12 -04:00
|
|
|
|
2019-09-28 12:46:21 -04:00
|
|
|
test("connectWebSocket should throw invalid scheme of url", async (): Promise<
|
|
|
|
void
|
|
|
|
> => {
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await connectWebSocket("file://hoge/hoge");
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-05-14 15:19:12 -04:00
|
|
|
test(async function wsWriteReadMaskedFrame(): Promise<void> {
|
|
|
|
const mask = new Uint8Array([0, 1, 2, 3]);
|
|
|
|
const msg = "hello";
|
|
|
|
const buf = new Buffer();
|
|
|
|
const r = new BufReader(buf);
|
|
|
|
await writeFrame(
|
|
|
|
{
|
|
|
|
isLastFrame: true,
|
|
|
|
mask,
|
|
|
|
opcode: OpCode.TextFrame,
|
|
|
|
payload: encode(msg)
|
|
|
|
},
|
|
|
|
buf
|
|
|
|
);
|
|
|
|
const frame = await readFrame(r);
|
|
|
|
assertEquals(frame.opcode, OpCode.TextFrame);
|
|
|
|
assertEquals(frame.isLastFrame, true);
|
|
|
|
assertEquals(frame.mask, mask);
|
|
|
|
unmask(frame.payload, frame.mask);
|
|
|
|
assertEquals(frame.payload, encode(msg));
|
|
|
|
});
|
|
|
|
|
2020-01-04 04:31:12 -05:00
|
|
|
test("handshake should not send search when it's empty", async function wsHandshakeWithEmptySearch(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const writer = new Buffer();
|
|
|
|
const reader = new Buffer(encode("HTTP/1.1 400\r\n"));
|
|
|
|
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await handshake(
|
|
|
|
new URL("ws://example.com"),
|
|
|
|
new Headers(),
|
|
|
|
new BufReader(reader),
|
|
|
|
new BufWriter(writer)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const tpReader = new TextProtoReader(new BufReader(writer));
|
|
|
|
const statusLine = await tpReader.readLine();
|
|
|
|
|
|
|
|
assertEquals(statusLine, "GET / HTTP/1.1");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("handshake should send search correctly", async function wsHandshakeWithSearch(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const writer = new Buffer();
|
|
|
|
const reader = new Buffer(encode("HTTP/1.1 400\r\n"));
|
|
|
|
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await handshake(
|
|
|
|
new URL("ws://example.com?a=1"),
|
|
|
|
new Headers(),
|
|
|
|
new BufReader(reader),
|
|
|
|
new BufWriter(writer)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const tpReader = new TextProtoReader(new BufReader(writer));
|
|
|
|
const statusLine = await tpReader.readLine();
|
|
|
|
|
|
|
|
assertEquals(statusLine, "GET /?a=1 HTTP/1.1");
|
|
|
|
});
|
|
|
|
|
2019-05-14 15:19:12 -04:00
|
|
|
runIfMain(import.meta);
|