Skip to content

Commit

Permalink
Create test.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
flaribbit committed Aug 6, 2021
1 parent 6e1170c commit 2892f53
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 34 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: test

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: setup luajit
run: sudo apt install luajit
- name: test
run: luajit ./test.lua
84 changes: 50 additions & 34 deletions test.lua
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
local band, bor, bxor = bit.band, bit.bor, bit.bxor
local shl, shr = bit.lshift, bit.rshift

local function check(ws)
if #ws._buffer<2 then
return nil, nil, "buffer length less than 2"
end
local length = band(ws._buffer:byte(2), 0x7f)
if length==126 then
if #ws._buffer<4 then
return nil, nil, "buffer length less than 4"
package.preload["socket"] = function()end
local ws = require"websocket"
local client = {
socket = {},
_buffer = "",
_length = 2,
_head = nil,
}
local res, head, err
local function receive(t)
return function()
if #t>0 then
local ret = t[1]
table.remove(t, 1)
return ret, nil, nil
else
return nil, "timeout", nil
end
local b1, b2 = ws._buffer:byte(3, 4)
ws._expect = 2 + shl(b1, 8) + b2
elseif length==127 then
if #ws._buffer<10 then
return nil, nil, "[test] buffer length less than 10"
end
local b5, b6, b7, b8 = ws._buffer:byte(7, 10)
ws._expect = 2 + shl(b5, 24) + shl(b6, 16) + shl(b7, 8) + b8
else
ws._expect = 2 + length
end
if #ws._buffer>=ws._expect then
return ws._buffer:sub(3), ws._buffer:byte(1), nil
else
return nil, nil, "[test] buffer length less than "..ws._expect
end
end

print(check{_expect=2,_buffer="\x81"})
print(check{_expect=2,_buffer="\x81\x00"})
print(check{_expect=3,_buffer="\x81\x01"})
print(check{_expect=3,_buffer="\x81\x01\x30"})
print(check{_expect=2,_buffer="\x81\x7e"})
print(check{_expect=2,_buffer="\x81\x7f"})
print(check{_expect=2,_buffer="\x81\x7e\x01\x02"})
print(check{_expect=2,_buffer="\x81\x7f\x00\x00\x00\x00\x00\x01\xbf\x50"})
--空消息
client.socket.receive = receive{"\x81", "\x00"}
res, head, err = ws.read(client)
assert(res==nil and head==nil and err=="buffer length less than 2")
res, head, err = ws.read(client)
assert(res=="" and head==0x81 and err==nil)

--1字节消息
client.socket.receive = receive{"\x81\x01"}
res, head, err = ws.read(client)
assert(res==nil and head==nil and err==nil)
client.socket.receive = receive{"\x31"}
res, head, err = ws.read(client)
assert(res=="1" and head==0x81 and err==nil)

--5字节消息
client.socket.receive = receive{"\x81\x05", "12", "345"}
res, head, err = ws.read(client)
assert(res==nil and head==nil and err=="buffer length less than 5")
res, head, err = ws.read(client)
assert(res=="12345" and head==0x81 and err==nil)

--200字节消息
local s = "" for i=1,100 do s=s..i%5 end
client.socket.receive = receive{"\x81\x7e", "\x00", "\xc8", s, s}
res, head, err = ws.read(client)
assert(res==nil and head==nil and err=="buffer length less than 4")
res, head, err = ws.read(client)
assert(res==nil and head==nil and err=="buffer length less than 200")
res, head, err = ws.read(client)
assert(res==s..s and head==0x81 and err==nil)

print(ws.read(client))

0 comments on commit 2892f53

Please sign in to comment.