Skip to content

Commit 2892f53

Browse files
committed
Create test.yml
1 parent 6e1170c commit 2892f53

File tree

2 files changed

+68
-34
lines changed

2 files changed

+68
-34
lines changed

.github/workflows/test.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: test
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: setup luajit
16+
run: sudo apt install luajit
17+
- name: test
18+
run: luajit ./test.lua

test.lua

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
1-
local band, bor, bxor = bit.band, bit.bor, bit.bxor
2-
local shl, shr = bit.lshift, bit.rshift
3-
4-
local function check(ws)
5-
if #ws._buffer<2 then
6-
return nil, nil, "buffer length less than 2"
7-
end
8-
local length = band(ws._buffer:byte(2), 0x7f)
9-
if length==126 then
10-
if #ws._buffer<4 then
11-
return nil, nil, "buffer length less than 4"
1+
package.preload["socket"] = function()end
2+
local ws = require"websocket"
3+
local client = {
4+
socket = {},
5+
_buffer = "",
6+
_length = 2,
7+
_head = nil,
8+
}
9+
local res, head, err
10+
local function receive(t)
11+
return function()
12+
if #t>0 then
13+
local ret = t[1]
14+
table.remove(t, 1)
15+
return ret, nil, nil
16+
else
17+
return nil, "timeout", nil
1218
end
13-
local b1, b2 = ws._buffer:byte(3, 4)
14-
ws._expect = 2 + shl(b1, 8) + b2
15-
elseif length==127 then
16-
if #ws._buffer<10 then
17-
return nil, nil, "[test] buffer length less than 10"
18-
end
19-
local b5, b6, b7, b8 = ws._buffer:byte(7, 10)
20-
ws._expect = 2 + shl(b5, 24) + shl(b6, 16) + shl(b7, 8) + b8
21-
else
22-
ws._expect = 2 + length
23-
end
24-
if #ws._buffer>=ws._expect then
25-
return ws._buffer:sub(3), ws._buffer:byte(1), nil
26-
else
27-
return nil, nil, "[test] buffer length less than "..ws._expect
2819
end
2920
end
3021

31-
print(check{_expect=2,_buffer="\x81"})
32-
print(check{_expect=2,_buffer="\x81\x00"})
33-
print(check{_expect=3,_buffer="\x81\x01"})
34-
print(check{_expect=3,_buffer="\x81\x01\x30"})
35-
print(check{_expect=2,_buffer="\x81\x7e"})
36-
print(check{_expect=2,_buffer="\x81\x7f"})
37-
print(check{_expect=2,_buffer="\x81\x7e\x01\x02"})
38-
print(check{_expect=2,_buffer="\x81\x7f\x00\x00\x00\x00\x00\x01\xbf\x50"})
22+
--空消息
23+
client.socket.receive = receive{"\x81", "\x00"}
24+
res, head, err = ws.read(client)
25+
assert(res==nil and head==nil and err=="buffer length less than 2")
26+
res, head, err = ws.read(client)
27+
assert(res=="" and head==0x81 and err==nil)
28+
29+
--1字节消息
30+
client.socket.receive = receive{"\x81\x01"}
31+
res, head, err = ws.read(client)
32+
assert(res==nil and head==nil and err==nil)
33+
client.socket.receive = receive{"\x31"}
34+
res, head, err = ws.read(client)
35+
assert(res=="1" and head==0x81 and err==nil)
36+
37+
--5字节消息
38+
client.socket.receive = receive{"\x81\x05", "12", "345"}
39+
res, head, err = ws.read(client)
40+
assert(res==nil and head==nil and err=="buffer length less than 5")
41+
res, head, err = ws.read(client)
42+
assert(res=="12345" and head==0x81 and err==nil)
43+
44+
--200字节消息
45+
local s = "" for i=1,100 do s=s..i%5 end
46+
client.socket.receive = receive{"\x81\x7e", "\x00", "\xc8", s, s}
47+
res, head, err = ws.read(client)
48+
assert(res==nil and head==nil and err=="buffer length less than 4")
49+
res, head, err = ws.read(client)
50+
assert(res==nil and head==nil and err=="buffer length less than 200")
51+
res, head, err = ws.read(client)
52+
assert(res==s..s and head==0x81 and err==nil)
53+
54+
print(ws.read(client))

0 commit comments

Comments
 (0)