Skip to content

Commit ce0b5e0

Browse files
Alex Marchdpgeorge
authored andcommitted
tests/extmod: Add websocket tests.
These short unit tests test the base uPy methods as well as parts of the websocket protocol, as implemented by uPy. @dpgeorge converted the original socket based tests by @hosaka to ones that only require io.BytesIO.
1 parent 38f063e commit ce0b5e0

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

tests/extmod/websocket.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
try:
2+
import uio
3+
import uerrno
4+
import websocket
5+
except ImportError:
6+
import sys
7+
print("SKIP")
8+
sys.exit()
9+
10+
# put raw data in the stream and do a websocket read
11+
def ws_read(msg, sz):
12+
ws = websocket.websocket(uio.BytesIO(msg))
13+
return ws.read(sz)
14+
15+
# do a websocket write and then return the raw data from the stream
16+
def ws_write(msg, sz):
17+
s = uio.BytesIO()
18+
ws = websocket.websocket(s)
19+
ws.write(msg)
20+
s.seek(0)
21+
return s.read(sz)
22+
23+
# basic frame
24+
print(ws_read(b"\x81\x04ping", 4))
25+
print(ws_read(b"\x80\x04ping", 4)) # FRAME_CONT
26+
print(ws_write(b"pong", 6))
27+
28+
# split frames are not supported
29+
# print(ws_read(b"\x01\x04ping", 4))
30+
31+
# extended payloads
32+
print(ws_read(b'\x81~\x00\x80' + b'ping' * 32, 128))
33+
print(ws_write(b"pong" * 32, 132))
34+
35+
# mask (returned data will be 'mask' ^ 'mask')
36+
print(ws_read(b"\x81\x84maskmask", 4))
37+
38+
# close control frame
39+
s = uio.BytesIO(b'\x88\x00') # FRAME_CLOSE
40+
ws = websocket.websocket(s)
41+
print(ws.read(1))
42+
s.seek(2)
43+
print(s.read(4))
44+
45+
# misc control frames
46+
print(ws_read(b"\x89\x00\x81\x04ping", 4)) # FRAME_PING
47+
print(ws_read(b"\x8a\x00\x81\x04pong", 4)) # FRAME_PONG
48+
49+
# close method
50+
ws = websocket.websocket(uio.BytesIO())
51+
ws.close()
52+
53+
# ioctl
54+
ws = websocket.websocket(uio.BytesIO())
55+
print(ws.ioctl(8)) # GET_DATA_OPTS
56+
print(ws.ioctl(9, 2)) # SET_DATA_OPTS
57+
print(ws.ioctl(9))
58+
try:
59+
ws.ioctl(-1)
60+
except OSError as e:
61+
print("ioctl: EINVAL:", e.args[0] == uerrno.EINVAL)

tests/extmod/websocket.py.exp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
b'ping'
2+
b'ping'
3+
b'\x81\x04pong'
4+
b'pingpingpingpingpingpingpingpingpingpingpingpingpingpingpingpingpingpingpingpingpingpingpingpingpingpingpingpingpingpingpingping'
5+
b'\x81~\x00\x80pongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpongpong'
6+
b'\x00\x00\x00\x00'
7+
b''
8+
b'\x81\x02\x88\x00'
9+
b'ping'
10+
b'pong'
11+
0
12+
1
13+
2
14+
ioctl: EINVAL: True

0 commit comments

Comments
 (0)