forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
205 lines (189 loc) · 5.56 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import "./sha1_test.ts";
import { BufReader } from "../io/bufio.ts";
import { assert, assertEquals } from "../testing/asserts.ts";
import { runIfMain, test } from "../testing/mod.ts";
import {
acceptable,
createSecAccept,
OpCode,
readFrame,
unmask,
writeFrame
} from "./mod.ts";
import { encode } from "../strings/strings.ts";
const { Buffer } = Deno;
test(async function wsReadUnmaskedTextFrame(): Promise<void> {
// 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);
assertEquals(frame.opcode, OpCode.TextFrame);
assertEquals(frame.mask, undefined);
assertEquals(new Buffer(frame.payload).toString(), "Hello");
assertEquals(frame.isLastFrame, true);
});
test(async function wsReadMaskedTextFrame(): Promise<void> {
//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);
console.dir(frame);
assertEquals(frame.opcode, OpCode.TextFrame);
unmask(frame.payload, frame.mask);
assertEquals(new Buffer(frame.payload).toString(), "Hello");
assertEquals(frame.isLastFrame, true);
});
test(async function wsReadUnmaskedSplitTextFrames(): Promise<void> {
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)]);
assertEquals(f1.isLastFrame, false);
assertEquals(f1.mask, undefined);
assertEquals(f1.opcode, OpCode.TextFrame);
assertEquals(new Buffer(f1.payload).toString(), "Hel");
assertEquals(f2.isLastFrame, true);
assertEquals(f2.mask, undefined);
assertEquals(f2.opcode, OpCode.Continue);
assertEquals(new Buffer(f2.payload).toString(), "lo");
});
test(async function wsReadUnmaskedPingPongFrame(): Promise<void> {
// 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);
assertEquals(ping.opcode, OpCode.Ping);
assertEquals(new Buffer(ping.payload).toString(), "Hello");
const buf2 = new BufReader(
new Buffer(
new Uint8Array([
0x8a,
0x85,
0x37,
0xfa,
0x21,
0x3d,
0x7f,
0x9f,
0x4d,
0x51,
0x58
])
)
);
const pong = await readFrame(buf2);
assertEquals(pong.opcode, OpCode.Pong);
assert(pong.mask !== undefined);
unmask(pong.payload, pong.mask);
assertEquals(new Buffer(pong.payload).toString(), "Hello");
});
test(async function wsReadUnmaskedBigBinaryFrame(): Promise<void> {
const a = [0x82, 0x7e, 0x01, 0x00];
for (let i = 0; i < 256; i++) {
a.push(i);
}
const buf = new BufReader(new Buffer(new Uint8Array(a)));
const bin = await readFrame(buf);
assertEquals(bin.opcode, OpCode.BinaryFrame);
assertEquals(bin.isLastFrame, true);
assertEquals(bin.mask, undefined);
assertEquals(bin.payload.length, 256);
});
test(async function wsReadUnmaskedBigBigBinaryFrame(): Promise<void> {
const a = [0x82, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00];
for (let i = 0; i < 0xffff; i++) {
a.push(i);
}
const buf = new BufReader(new Buffer(new Uint8Array(a)));
const bin = await readFrame(buf);
assertEquals(bin.opcode, OpCode.BinaryFrame);
assertEquals(bin.isLastFrame, true);
assertEquals(bin.mask, undefined);
assertEquals(bin.payload.length, 0xffff + 1);
});
test(async function wsCreateSecAccept(): Promise<void> {
const nonce = "dGhlIHNhbXBsZSBub25jZQ==";
const d = createSecAccept(nonce);
assertEquals(d, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");
});
test(function wsAcceptable(): void {
const ret = acceptable({
headers: new Headers({
upgrade: "websocket",
"sec-websocket-key": "aaa"
})
});
assertEquals(ret, true);
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"]
])
})
);
});
const invalidHeaders = [
{ "sec-websocket-key": "aaa" },
{ upgrade: "websocket" },
{ upgrade: "invalid", "sec-websocket-key": "aaa" },
{ upgrade: "websocket", "sec-websocket-ky": "" }
];
test(function wsAcceptableInvalid(): void {
for (const pat of invalidHeaders) {
const ret = acceptable({
headers: new Headers(pat)
});
assertEquals(ret, false);
}
});
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));
});
runIfMain(import.meta);