xref: /unit/test/test_java_websockets.py (revision 2073:bc6ad31ce286)
11477Szelenkov@nginx.comimport struct
21158Szelenkov@nginx.comimport time
31477Szelenkov@nginx.com
41635Szelenkov@nginx.comimport pytest
51158Szelenkov@nginx.comfrom unit.applications.lang.java import TestApplicationJava
61158Szelenkov@nginx.comfrom unit.applications.websockets import TestApplicationWebsocket
71730Szelenkov@nginx.comfrom unit.option import option
81158Szelenkov@nginx.com
91158Szelenkov@nginx.com
101158Szelenkov@nginx.comclass TestJavaWebsockets(TestApplicationJava):
111467Szelenkov@nginx.com    prerequisites = {'modules': {'java': 'any'}}
121158Szelenkov@nginx.com
131246Szelenkov@nginx.com    ws = TestApplicationWebsocket()
141158Szelenkov@nginx.com
151736Szelenkov@nginx.com    @pytest.fixture(autouse=True)
161736Szelenkov@nginx.com    def setup_method_fixture(self, request, skip_alert):
171596Szelenkov@nginx.com        assert 'success' in self.conf(
181596Szelenkov@nginx.com            {'http': {'websocket': {'keepalive_interval': 0}}}, 'settings'
191596Szelenkov@nginx.com        ), 'clear keepalive_interval'
201158Szelenkov@nginx.com
211596Szelenkov@nginx.com        skip_alert(r'socket close\(\d+\) failed')
221158Szelenkov@nginx.com
231158Szelenkov@nginx.com    def close_connection(self, sock):
241596Szelenkov@nginx.com        assert self.recvall(sock, read_timeout=0.1) == b'', 'empty soc'
251158Szelenkov@nginx.com
261158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CLOSE, self.ws.serialize_close())
271158Szelenkov@nginx.com
281158Szelenkov@nginx.com        self.check_close(sock)
291158Szelenkov@nginx.com
301819Smax.romanov@nginx.com    def check_close(self, sock, code=1000, no_close=False, frame=None):
311819Smax.romanov@nginx.com        if frame == None:
321819Smax.romanov@nginx.com            frame = self.ws.frame_read(sock)
331158Szelenkov@nginx.com
341596Szelenkov@nginx.com        assert frame['fin'] == True, 'close fin'
351596Szelenkov@nginx.com        assert frame['opcode'] == self.ws.OP_CLOSE, 'close opcode'
361596Szelenkov@nginx.com        assert frame['code'] == code, 'close code'
371158Szelenkov@nginx.com
381158Szelenkov@nginx.com        if not no_close:
391158Szelenkov@nginx.com            sock.close()
401158Szelenkov@nginx.com
411158Szelenkov@nginx.com    def check_frame(self, frame, fin, opcode, payload, decode=True):
421158Szelenkov@nginx.com        if opcode == self.ws.OP_BINARY or not decode:
431158Szelenkov@nginx.com            data = frame['data']
441158Szelenkov@nginx.com        else:
451158Szelenkov@nginx.com            data = frame['data'].decode('utf-8')
461158Szelenkov@nginx.com
471596Szelenkov@nginx.com        assert frame['fin'] == fin, 'fin'
481596Szelenkov@nginx.com        assert frame['opcode'] == opcode, 'opcode'
491596Szelenkov@nginx.com        assert data == payload, 'payload'
501158Szelenkov@nginx.com
511158Szelenkov@nginx.com    def test_java_websockets_handshake(self):
521158Szelenkov@nginx.com        self.load('websockets_mirror')
531158Szelenkov@nginx.com
541158Szelenkov@nginx.com        resp, sock, key = self.ws.upgrade()
551158Szelenkov@nginx.com        sock.close()
561158Szelenkov@nginx.com
571596Szelenkov@nginx.com        assert resp['status'] == 101, 'status'
581596Szelenkov@nginx.com        assert resp['headers']['Upgrade'] == 'websocket', 'upgrade'
591596Szelenkov@nginx.com        assert resp['headers']['Connection'] == 'Upgrade', 'connection'
601596Szelenkov@nginx.com        assert resp['headers']['Sec-WebSocket-Accept'] == self.ws.accept(
611596Szelenkov@nginx.com            key
621596Szelenkov@nginx.com        ), 'key'
631158Szelenkov@nginx.com
641158Szelenkov@nginx.com    def test_java_websockets_mirror(self):
651158Szelenkov@nginx.com        self.load('websockets_mirror')
661158Szelenkov@nginx.com
671158Szelenkov@nginx.com        message = 'blah'
681158Szelenkov@nginx.com
691158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
701158Szelenkov@nginx.com
711158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, message)
721158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
731158Szelenkov@nginx.com
741596Szelenkov@nginx.com        assert message == frame['data'].decode('utf-8'), 'mirror'
751158Szelenkov@nginx.com
761158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, message)
771158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
781158Szelenkov@nginx.com
791596Szelenkov@nginx.com        assert message == frame['data'].decode('utf-8'), 'mirror 2'
801158Szelenkov@nginx.com
811158Szelenkov@nginx.com        sock.close()
821158Szelenkov@nginx.com
831158Szelenkov@nginx.com    def test_java_websockets_no_mask(self):
841158Szelenkov@nginx.com        self.load('websockets_mirror')
851158Szelenkov@nginx.com
861158Szelenkov@nginx.com        message = 'blah'
871158Szelenkov@nginx.com
881158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
891158Szelenkov@nginx.com
901158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, message, mask=False)
911158Szelenkov@nginx.com
921158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
931158Szelenkov@nginx.com
941596Szelenkov@nginx.com        assert frame['opcode'] == self.ws.OP_CLOSE, 'no mask opcode'
951596Szelenkov@nginx.com        assert frame['code'] == 1002, 'no mask close code'
961158Szelenkov@nginx.com
971158Szelenkov@nginx.com        sock.close()
981158Szelenkov@nginx.com
991158Szelenkov@nginx.com    def test_java_websockets_fragmentation(self):
1001158Szelenkov@nginx.com        self.load('websockets_mirror')
1011158Szelenkov@nginx.com
1021158Szelenkov@nginx.com        message = 'blah'
1031158Szelenkov@nginx.com
1041158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
1051158Szelenkov@nginx.com
1061158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, message, fin=False)
1071158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, ' ', fin=False)
1081158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, message)
1091158Szelenkov@nginx.com
1101158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
1111158Szelenkov@nginx.com
1121596Szelenkov@nginx.com        assert message + ' ' + message == frame['data'].decode(
1131596Szelenkov@nginx.com            'utf-8'
1141596Szelenkov@nginx.com        ), 'mirror framing'
1151158Szelenkov@nginx.com
1161158Szelenkov@nginx.com        sock.close()
1171158Szelenkov@nginx.com
1181158Szelenkov@nginx.com    def test_java_websockets_frame_fragmentation_invalid(self):
1191158Szelenkov@nginx.com        self.load('websockets_mirror')
1201158Szelenkov@nginx.com
1211158Szelenkov@nginx.com        message = 'blah'
1221158Szelenkov@nginx.com
1231158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
1241158Szelenkov@nginx.com
1251158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, message, fin=False)
1261158Szelenkov@nginx.com
1271158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
1281158Szelenkov@nginx.com
1291158Szelenkov@nginx.com        frame.pop('data')
1301596Szelenkov@nginx.com        assert frame == {
1311596Szelenkov@nginx.com            'fin': True,
1321596Szelenkov@nginx.com            'rsv1': False,
1331596Szelenkov@nginx.com            'rsv2': False,
1341596Szelenkov@nginx.com            'rsv3': False,
1351596Szelenkov@nginx.com            'opcode': self.ws.OP_CLOSE,
1361596Szelenkov@nginx.com            'mask': 0,
1371596Szelenkov@nginx.com            'code': 1002,
1381596Szelenkov@nginx.com            'reason': 'Fragmented control frame',
1391596Szelenkov@nginx.com        }, 'close frame'
1401158Szelenkov@nginx.com
1411158Szelenkov@nginx.com        sock.close()
1421158Szelenkov@nginx.com
1431158Szelenkov@nginx.com    def test_java_websockets_two_clients(self):
1441158Szelenkov@nginx.com        self.load('websockets_mirror')
1451158Szelenkov@nginx.com
1461158Szelenkov@nginx.com        message1 = 'blah1'
1471158Szelenkov@nginx.com        message2 = 'blah2'
1481158Szelenkov@nginx.com
1491158Szelenkov@nginx.com        _, sock1, _ = self.ws.upgrade()
1501158Szelenkov@nginx.com        _, sock2, _ = self.ws.upgrade()
1511158Szelenkov@nginx.com
1521158Szelenkov@nginx.com        self.ws.frame_write(sock1, self.ws.OP_TEXT, message1)
1531158Szelenkov@nginx.com        self.ws.frame_write(sock2, self.ws.OP_TEXT, message2)
1541158Szelenkov@nginx.com
1551158Szelenkov@nginx.com        frame1 = self.ws.frame_read(sock1)
1561158Szelenkov@nginx.com        frame2 = self.ws.frame_read(sock2)
1571158Szelenkov@nginx.com
1581596Szelenkov@nginx.com        assert message1 == frame1['data'].decode('utf-8'), 'client 1'
1591596Szelenkov@nginx.com        assert message2 == frame2['data'].decode('utf-8'), 'client 2'
1601158Szelenkov@nginx.com
1611158Szelenkov@nginx.com        sock1.close()
1621158Szelenkov@nginx.com        sock2.close()
1631158Szelenkov@nginx.com
1641596Szelenkov@nginx.com    @pytest.mark.skip('not yet')
1651158Szelenkov@nginx.com    def test_java_websockets_handshake_upgrade_absent(
1661848Szelenkov@nginx.com        self,
1671158Szelenkov@nginx.com    ):  # FAIL https://tools.ietf.org/html/rfc6455#section-4.2.1
1681158Szelenkov@nginx.com        self.load('websockets_mirror')
1691158Szelenkov@nginx.com
1701158Szelenkov@nginx.com        resp = self.get(
1711158Szelenkov@nginx.com            headers={
1721158Szelenkov@nginx.com                'Host': 'localhost',
1731158Szelenkov@nginx.com                'Connection': 'Upgrade',
1741262Szelenkov@nginx.com                'Sec-WebSocket-Key': self.ws.key(),
1751158Szelenkov@nginx.com                'Sec-WebSocket-Protocol': 'chat',
1761158Szelenkov@nginx.com                'Sec-WebSocket-Version': 13,
1771158Szelenkov@nginx.com            },
1781158Szelenkov@nginx.com        )
1791158Szelenkov@nginx.com
1801596Szelenkov@nginx.com        assert resp['status'] == 400, 'upgrade absent'
1811158Szelenkov@nginx.com
1821158Szelenkov@nginx.com    def test_java_websockets_handshake_case_insensitive(self):
1831158Szelenkov@nginx.com        self.load('websockets_mirror')
1841158Szelenkov@nginx.com
1851262Szelenkov@nginx.com        resp, sock, _ = self.ws.upgrade(
1861158Szelenkov@nginx.com            headers={
1871158Szelenkov@nginx.com                'Host': 'localhost',
1881158Szelenkov@nginx.com                'Upgrade': 'WEBSOCKET',
1891158Szelenkov@nginx.com                'Connection': 'UPGRADE',
1901262Szelenkov@nginx.com                'Sec-WebSocket-Key': self.ws.key(),
1911158Szelenkov@nginx.com                'Sec-WebSocket-Protocol': 'chat',
1921158Szelenkov@nginx.com                'Sec-WebSocket-Version': 13,
1931262Szelenkov@nginx.com            }
1941158Szelenkov@nginx.com        )
1951262Szelenkov@nginx.com        sock.close()
1961158Szelenkov@nginx.com
1971596Szelenkov@nginx.com        assert resp['status'] == 101, 'status'
1981158Szelenkov@nginx.com
1991596Szelenkov@nginx.com    @pytest.mark.skip('not yet')
2001158Szelenkov@nginx.com    def test_java_websockets_handshake_connection_absent(self):  # FAIL
2011158Szelenkov@nginx.com        self.load('websockets_mirror')
2021158Szelenkov@nginx.com
2031158Szelenkov@nginx.com        resp = self.get(
2041158Szelenkov@nginx.com            headers={
2051158Szelenkov@nginx.com                'Host': 'localhost',
2061158Szelenkov@nginx.com                'Upgrade': 'websocket',
2071262Szelenkov@nginx.com                'Sec-WebSocket-Key': self.ws.key(),
2081158Szelenkov@nginx.com                'Sec-WebSocket-Protocol': 'chat',
2091158Szelenkov@nginx.com                'Sec-WebSocket-Version': 13,
2101158Szelenkov@nginx.com            },
2111158Szelenkov@nginx.com        )
2121158Szelenkov@nginx.com
2131596Szelenkov@nginx.com        assert resp['status'] == 400, 'status'
2141158Szelenkov@nginx.com
2151158Szelenkov@nginx.com    def test_java_websockets_handshake_version_absent(self):
2161158Szelenkov@nginx.com        self.load('websockets_mirror')
2171158Szelenkov@nginx.com
2181158Szelenkov@nginx.com        resp = self.get(
2191158Szelenkov@nginx.com            headers={
2201158Szelenkov@nginx.com                'Host': 'localhost',
2211158Szelenkov@nginx.com                'Upgrade': 'websocket',
2221158Szelenkov@nginx.com                'Connection': 'Upgrade',
2231262Szelenkov@nginx.com                'Sec-WebSocket-Key': self.ws.key(),
2241158Szelenkov@nginx.com                'Sec-WebSocket-Protocol': 'chat',
2251158Szelenkov@nginx.com            },
2261158Szelenkov@nginx.com        )
2271158Szelenkov@nginx.com
2281596Szelenkov@nginx.com        assert resp['status'] == 426, 'status'
2291158Szelenkov@nginx.com
2301596Szelenkov@nginx.com    @pytest.mark.skip('not yet')
2311158Szelenkov@nginx.com    def test_java_websockets_handshake_key_invalid(self):
2321158Szelenkov@nginx.com        self.load('websockets_mirror')
2331158Szelenkov@nginx.com
2341158Szelenkov@nginx.com        resp = self.get(
2351158Szelenkov@nginx.com            headers={
2361158Szelenkov@nginx.com                'Host': 'localhost',
2371158Szelenkov@nginx.com                'Upgrade': 'websocket',
2381158Szelenkov@nginx.com                'Connection': 'Upgrade',
2391158Szelenkov@nginx.com                'Sec-WebSocket-Key': '!',
2401158Szelenkov@nginx.com                'Sec-WebSocket-Protocol': 'chat',
2411158Szelenkov@nginx.com                'Sec-WebSocket-Version': 13,
2421158Szelenkov@nginx.com            },
2431158Szelenkov@nginx.com        )
2441158Szelenkov@nginx.com
2451596Szelenkov@nginx.com        assert resp['status'] == 400, 'key length'
2461158Szelenkov@nginx.com
2471158Szelenkov@nginx.com        key = self.ws.key()
2481158Szelenkov@nginx.com        resp = self.get(
2491158Szelenkov@nginx.com            headers={
2501158Szelenkov@nginx.com                'Host': 'localhost',
2511158Szelenkov@nginx.com                'Upgrade': 'websocket',
2521158Szelenkov@nginx.com                'Connection': 'Upgrade',
2531158Szelenkov@nginx.com                'Sec-WebSocket-Key': [key, key],
2541158Szelenkov@nginx.com                'Sec-WebSocket-Protocol': 'chat',
2551158Szelenkov@nginx.com                'Sec-WebSocket-Version': 13,
2561158Szelenkov@nginx.com            },
2571158Szelenkov@nginx.com        )
2581158Szelenkov@nginx.com
2591848Szelenkov@nginx.com        assert (
2601848Szelenkov@nginx.com            resp['status'] == 400
2611848Szelenkov@nginx.com        ), 'key double'  # FAIL https://tools.ietf.org/html/rfc6455#section-11.3.1
2621158Szelenkov@nginx.com
2631158Szelenkov@nginx.com    def test_java_websockets_handshake_method_invalid(self):
2641158Szelenkov@nginx.com        self.load('websockets_mirror')
2651158Szelenkov@nginx.com
2661158Szelenkov@nginx.com        resp = self.post(
2671158Szelenkov@nginx.com            headers={
2681158Szelenkov@nginx.com                'Host': 'localhost',
2691158Szelenkov@nginx.com                'Upgrade': 'websocket',
2701158Szelenkov@nginx.com                'Connection': 'Upgrade',
2711262Szelenkov@nginx.com                'Sec-WebSocket-Key': self.ws.key(),
2721158Szelenkov@nginx.com                'Sec-WebSocket-Protocol': 'chat',
2731158Szelenkov@nginx.com                'Sec-WebSocket-Version': 13,
2741158Szelenkov@nginx.com            },
2751158Szelenkov@nginx.com        )
2761158Szelenkov@nginx.com
2771596Szelenkov@nginx.com        assert resp['status'] == 400, 'status'
2781158Szelenkov@nginx.com
2791158Szelenkov@nginx.com    def test_java_websockets_handshake_http_10(self):
2801158Szelenkov@nginx.com        self.load('websockets_mirror')
2811158Szelenkov@nginx.com
2821158Szelenkov@nginx.com        resp = self.get(
2831158Szelenkov@nginx.com            headers={
2841158Szelenkov@nginx.com                'Host': 'localhost',
2851158Szelenkov@nginx.com                'Upgrade': 'websocket',
2861158Szelenkov@nginx.com                'Connection': 'Upgrade',
2871262Szelenkov@nginx.com                'Sec-WebSocket-Key': self.ws.key(),
2881158Szelenkov@nginx.com                'Sec-WebSocket-Protocol': 'chat',
2891158Szelenkov@nginx.com                'Sec-WebSocket-Version': 13,
2901158Szelenkov@nginx.com            },
2911158Szelenkov@nginx.com            http_10=True,
2921158Szelenkov@nginx.com        )
2931158Szelenkov@nginx.com
2941596Szelenkov@nginx.com        assert resp['status'] == 400, 'status'
2951158Szelenkov@nginx.com
2961158Szelenkov@nginx.com    def test_java_websockets_handshake_uri_invalid(self):
2971158Szelenkov@nginx.com        self.load('websockets_mirror')
2981158Szelenkov@nginx.com
2991158Szelenkov@nginx.com        resp = self.get(
3001158Szelenkov@nginx.com            headers={
3011158Szelenkov@nginx.com                'Host': 'localhost',
3021158Szelenkov@nginx.com                'Upgrade': 'websocket',
3031158Szelenkov@nginx.com                'Connection': 'Upgrade',
3041262Szelenkov@nginx.com                'Sec-WebSocket-Key': self.ws.key(),
3051158Szelenkov@nginx.com                'Sec-WebSocket-Protocol': 'chat',
3061158Szelenkov@nginx.com                'Sec-WebSocket-Version': 13,
3071158Szelenkov@nginx.com            },
3081158Szelenkov@nginx.com            url='!',
3091158Szelenkov@nginx.com        )
3101158Szelenkov@nginx.com
3111596Szelenkov@nginx.com        assert resp['status'] == 400, 'status'
3121158Szelenkov@nginx.com
3131158Szelenkov@nginx.com    def test_java_websockets_protocol_absent(self):
3141158Szelenkov@nginx.com        self.load('websockets_mirror')
3151158Szelenkov@nginx.com
3161158Szelenkov@nginx.com        key = self.ws.key()
3171262Szelenkov@nginx.com        resp, sock, _ = self.ws.upgrade(
3181158Szelenkov@nginx.com            headers={
3191158Szelenkov@nginx.com                'Host': 'localhost',
3201158Szelenkov@nginx.com                'Upgrade': 'websocket',
3211158Szelenkov@nginx.com                'Connection': 'Upgrade',
3221158Szelenkov@nginx.com                'Sec-WebSocket-Key': key,
3231158Szelenkov@nginx.com                'Sec-WebSocket-Version': 13,
3241262Szelenkov@nginx.com            }
3251158Szelenkov@nginx.com        )
3261262Szelenkov@nginx.com        sock.close()
3271158Szelenkov@nginx.com
3281596Szelenkov@nginx.com        assert resp['status'] == 101, 'status'
3291596Szelenkov@nginx.com        assert resp['headers']['Upgrade'] == 'websocket', 'upgrade'
3301596Szelenkov@nginx.com        assert resp['headers']['Connection'] == 'Upgrade', 'connection'
3311596Szelenkov@nginx.com        assert resp['headers']['Sec-WebSocket-Accept'] == self.ws.accept(
3321596Szelenkov@nginx.com            key
3331596Szelenkov@nginx.com        ), 'key'
3341158Szelenkov@nginx.com
3351158Szelenkov@nginx.com    # autobahn-testsuite
3361158Szelenkov@nginx.com    #
3371158Szelenkov@nginx.com    # Some following tests fail because of Unit does not support UTF-8
3381158Szelenkov@nginx.com    # validation for websocket frames.  It should be implemented
3391158Szelenkov@nginx.com    # by application, if necessary.
3401158Szelenkov@nginx.com
3411158Szelenkov@nginx.com    def test_java_websockets_1_1_1__1_1_8(self):
3421158Szelenkov@nginx.com        self.load('websockets_mirror')
3431158Szelenkov@nginx.com
3441158Szelenkov@nginx.com        opcode = self.ws.OP_TEXT
3451158Szelenkov@nginx.com
3461158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
3471158Szelenkov@nginx.com
3481158Szelenkov@nginx.com        def check_length(length, chopsize=None):
3491158Szelenkov@nginx.com            payload = '*' * length
3501158Szelenkov@nginx.com
3511158Szelenkov@nginx.com            self.ws.frame_write(sock, opcode, payload, chopsize=chopsize)
3521158Szelenkov@nginx.com
3531158Szelenkov@nginx.com            frame = self.ws.message_read(sock)
3541158Szelenkov@nginx.com            self.check_frame(frame, True, opcode, payload)
3551158Szelenkov@nginx.com
3561848Szelenkov@nginx.com        check_length(0)  # 1_1_1
3571848Szelenkov@nginx.com        check_length(125)  # 1_1_2
3581848Szelenkov@nginx.com        check_length(126)  # 1_1_3
3591848Szelenkov@nginx.com        check_length(127)  # 1_1_4
3601848Szelenkov@nginx.com        check_length(128)  # 1_1_5
3611848Szelenkov@nginx.com        check_length(65535)  # 1_1_6
3621848Szelenkov@nginx.com        check_length(65536)  # 1_1_7
3631848Szelenkov@nginx.com        check_length(65536, chopsize=997)  # 1_1_8
3641158Szelenkov@nginx.com
3651158Szelenkov@nginx.com        self.close_connection(sock)
3661158Szelenkov@nginx.com
3671158Szelenkov@nginx.com    def test_java_websockets_1_2_1__1_2_8(self):
3681158Szelenkov@nginx.com        self.load('websockets_mirror')
3691158Szelenkov@nginx.com
3701158Szelenkov@nginx.com        opcode = self.ws.OP_BINARY
3711158Szelenkov@nginx.com
3721158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
3731158Szelenkov@nginx.com
3741158Szelenkov@nginx.com        def check_length(length, chopsize=None):
3751158Szelenkov@nginx.com            payload = b'\xfe' * length
3761158Szelenkov@nginx.com
3771158Szelenkov@nginx.com            self.ws.frame_write(sock, opcode, payload, chopsize=chopsize)
3781158Szelenkov@nginx.com
3791158Szelenkov@nginx.com            frame = self.ws.message_read(sock)
3801158Szelenkov@nginx.com            self.check_frame(frame, True, opcode, payload)
3811158Szelenkov@nginx.com
3821848Szelenkov@nginx.com        check_length(0)  # 1_2_1
3831848Szelenkov@nginx.com        check_length(125)  # 1_2_2
3841848Szelenkov@nginx.com        check_length(126)  # 1_2_3
3851848Szelenkov@nginx.com        check_length(127)  # 1_2_4
3861848Szelenkov@nginx.com        check_length(128)  # 1_2_5
3871848Szelenkov@nginx.com        check_length(65535)  # 1_2_6
3881848Szelenkov@nginx.com        check_length(65536)  # 1_2_7
3891848Szelenkov@nginx.com        check_length(65536, chopsize=997)  # 1_2_8
3901158Szelenkov@nginx.com
3911158Szelenkov@nginx.com        self.close_connection(sock)
3921158Szelenkov@nginx.com
3931158Szelenkov@nginx.com    def test_java_websockets_2_1__2_6(self):
3941158Szelenkov@nginx.com        self.load('websockets_mirror')
3951158Szelenkov@nginx.com
3961158Szelenkov@nginx.com        op_ping = self.ws.OP_PING
3971158Szelenkov@nginx.com        op_pong = self.ws.OP_PONG
3981158Szelenkov@nginx.com
3991158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
4001158Szelenkov@nginx.com
4011158Szelenkov@nginx.com        def check_ping(payload, chopsize=None, decode=True):
4021158Szelenkov@nginx.com            self.ws.frame_write(sock, op_ping, payload, chopsize=chopsize)
4031158Szelenkov@nginx.com            frame = self.ws.frame_read(sock)
4041158Szelenkov@nginx.com
4051158Szelenkov@nginx.com            self.check_frame(frame, True, op_pong, payload, decode=decode)
4061158Szelenkov@nginx.com
4071848Szelenkov@nginx.com        check_ping('')  # 2_1
4081848Szelenkov@nginx.com        check_ping('Hello, world!')  # 2_2
4091158Szelenkov@nginx.com        check_ping(b'\x00\xff\xfe\xfd\xfc\xfb\x00\xff', decode=False)  # 2_3
4101848Szelenkov@nginx.com        check_ping(b'\xfe' * 125, decode=False)  # 2_4
4111848Szelenkov@nginx.com        check_ping(b'\xfe' * 125, chopsize=1, decode=False)  # 2_6
4121158Szelenkov@nginx.com
4131158Szelenkov@nginx.com        self.close_connection(sock)
4141158Szelenkov@nginx.com
4151158Szelenkov@nginx.com        # 2_5
4161158Szelenkov@nginx.com
4171158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
4181158Szelenkov@nginx.com
4191158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, b'\xfe' * 126)
4201158Szelenkov@nginx.com        self.check_close(sock, 1002)
4211158Szelenkov@nginx.com
4221158Szelenkov@nginx.com    def test_java_websockets_2_7__2_9(self):
4231158Szelenkov@nginx.com        self.load('websockets_mirror')
4241158Szelenkov@nginx.com
4251158Szelenkov@nginx.com        # 2_7
4261158Szelenkov@nginx.com
4271158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
4281158Szelenkov@nginx.com
4291158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PONG, '')
4301596Szelenkov@nginx.com        assert self.recvall(sock, read_timeout=0.1) == b'', '2_7'
4311158Szelenkov@nginx.com
4321158Szelenkov@nginx.com        # 2_8
4331158Szelenkov@nginx.com
4341158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PONG, 'unsolicited pong payload')
4351596Szelenkov@nginx.com        assert self.recvall(sock, read_timeout=0.1) == b'', '2_8'
4361158Szelenkov@nginx.com
4371158Szelenkov@nginx.com        # 2_9
4381158Szelenkov@nginx.com
4391158Szelenkov@nginx.com        payload = 'ping payload'
4401158Szelenkov@nginx.com
4411158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PONG, 'unsolicited pong payload')
4421158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, payload)
4431158Szelenkov@nginx.com
4441158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
4451158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_PONG, payload)
4461158Szelenkov@nginx.com
4471158Szelenkov@nginx.com        self.close_connection(sock)
4481158Szelenkov@nginx.com
4491158Szelenkov@nginx.com    def test_java_websockets_2_10__2_11(self):
4501158Szelenkov@nginx.com        self.load('websockets_mirror')
4511158Szelenkov@nginx.com
4521158Szelenkov@nginx.com        # 2_10
4531158Szelenkov@nginx.com
4541158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
4551158Szelenkov@nginx.com
4561158Szelenkov@nginx.com        for i in range(0, 10):
4571158Szelenkov@nginx.com            self.ws.frame_write(sock, self.ws.OP_PING, 'payload-%d' % i)
4581158Szelenkov@nginx.com
4591158Szelenkov@nginx.com        for i in range(0, 10):
4601158Szelenkov@nginx.com            frame = self.ws.frame_read(sock)
4611158Szelenkov@nginx.com            self.check_frame(frame, True, self.ws.OP_PONG, 'payload-%d' % i)
4621158Szelenkov@nginx.com
4631158Szelenkov@nginx.com        # 2_11
4641158Szelenkov@nginx.com
4651158Szelenkov@nginx.com        for i in range(0, 10):
4661158Szelenkov@nginx.com            opcode = self.ws.OP_PING
4671158Szelenkov@nginx.com            self.ws.frame_write(sock, opcode, 'payload-%d' % i, chopsize=1)
4681158Szelenkov@nginx.com
4691158Szelenkov@nginx.com        for i in range(0, 10):
4701158Szelenkov@nginx.com            frame = self.ws.frame_read(sock)
4711158Szelenkov@nginx.com            self.check_frame(frame, True, self.ws.OP_PONG, 'payload-%d' % i)
4721158Szelenkov@nginx.com
4731158Szelenkov@nginx.com        self.close_connection(sock)
4741158Szelenkov@nginx.com
4751596Szelenkov@nginx.com    @pytest.mark.skip('not yet')
4761158Szelenkov@nginx.com    def test_java_websockets_3_1__3_7(self):
4771158Szelenkov@nginx.com        self.load('websockets_mirror')
4781158Szelenkov@nginx.com
4791158Szelenkov@nginx.com        payload = 'Hello, world!'
4801158Szelenkov@nginx.com
4811158Szelenkov@nginx.com        # 3_1
4821158Szelenkov@nginx.com
4831158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
4841158Szelenkov@nginx.com
4851158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload, rsv1=True)
4861158Szelenkov@nginx.com        self.check_close(sock, 1002)
4871158Szelenkov@nginx.com
4881158Szelenkov@nginx.com        # 3_2
4891158Szelenkov@nginx.com
4901158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
4911158Szelenkov@nginx.com
4921158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
4931158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload, rsv2=True)
4941158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, '')
4951158Szelenkov@nginx.com
4961158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
4971158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
4981158Szelenkov@nginx.com
4991158Szelenkov@nginx.com        self.check_close(sock, 1002, no_close=True)
5001158Szelenkov@nginx.com
5011596Szelenkov@nginx.com        assert self.recvall(sock, read_timeout=0.1) == b'', 'empty 3_2'
5021158Szelenkov@nginx.com        sock.close()
5031158Szelenkov@nginx.com
5041158Szelenkov@nginx.com        # 3_3
5051158Szelenkov@nginx.com
5061158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
5071158Szelenkov@nginx.com
5081158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
5091158Szelenkov@nginx.com
5101158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
5111158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
5121158Szelenkov@nginx.com
5131158Szelenkov@nginx.com        self.ws.frame_write(
5141158Szelenkov@nginx.com            sock, self.ws.OP_TEXT, payload, rsv1=True, rsv2=True
5151158Szelenkov@nginx.com        )
5161158Szelenkov@nginx.com
5171158Szelenkov@nginx.com        self.check_close(sock, 1002, no_close=True)
5181158Szelenkov@nginx.com
5191596Szelenkov@nginx.com        assert self.recvall(sock, read_timeout=0.1) == b'', 'empty 3_3'
5201158Szelenkov@nginx.com        sock.close()
5211158Szelenkov@nginx.com
5221158Szelenkov@nginx.com        # 3_4
5231158Szelenkov@nginx.com
5241158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
5251158Szelenkov@nginx.com
5261158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload, chopsize=1)
5271158Szelenkov@nginx.com        self.ws.frame_write(
5281158Szelenkov@nginx.com            sock, self.ws.OP_TEXT, payload, rsv3=True, chopsize=1
5291158Szelenkov@nginx.com        )
5301158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, '')
5311158Szelenkov@nginx.com
5321158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
5331158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
5341158Szelenkov@nginx.com
5351158Szelenkov@nginx.com        self.check_close(sock, 1002, no_close=True)
5361158Szelenkov@nginx.com
5371596Szelenkov@nginx.com        assert self.recvall(sock, read_timeout=0.1) == b'', 'empty 3_4'
5381158Szelenkov@nginx.com        sock.close()
5391158Szelenkov@nginx.com
5401158Szelenkov@nginx.com        # 3_5
5411158Szelenkov@nginx.com
5421158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
5431158Szelenkov@nginx.com
5441158Szelenkov@nginx.com        self.ws.frame_write(
5451158Szelenkov@nginx.com            sock,
5461158Szelenkov@nginx.com            self.ws.OP_BINARY,
5471158Szelenkov@nginx.com            b'\x00\xff\xfe\xfd\xfc\xfb\x00\xff',
5481158Szelenkov@nginx.com            rsv1=True,
5491158Szelenkov@nginx.com            rsv3=True,
5501158Szelenkov@nginx.com        )
5511158Szelenkov@nginx.com
5521158Szelenkov@nginx.com        self.check_close(sock, 1002)
5531158Szelenkov@nginx.com
5541158Szelenkov@nginx.com        # 3_6
5551158Szelenkov@nginx.com
5561158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
5571158Szelenkov@nginx.com
5581158Szelenkov@nginx.com        self.ws.frame_write(
5591158Szelenkov@nginx.com            sock, self.ws.OP_PING, payload, rsv2=True, rsv3=True
5601158Szelenkov@nginx.com        )
5611158Szelenkov@nginx.com
5621158Szelenkov@nginx.com        self.check_close(sock, 1002)
5631158Szelenkov@nginx.com
5641158Szelenkov@nginx.com        # 3_7
5651158Szelenkov@nginx.com
5661158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
5671158Szelenkov@nginx.com
5681158Szelenkov@nginx.com        self.ws.frame_write(
5691158Szelenkov@nginx.com            sock, self.ws.OP_CLOSE, payload, rsv1=True, rsv2=True, rsv3=True
5701158Szelenkov@nginx.com        )
5711158Szelenkov@nginx.com
5721158Szelenkov@nginx.com        self.check_close(sock, 1002)
5731158Szelenkov@nginx.com
5741158Szelenkov@nginx.com    def test_java_websockets_4_1_1__4_2_5(self):
5751158Szelenkov@nginx.com        self.load('websockets_mirror')
5761158Szelenkov@nginx.com
5771158Szelenkov@nginx.com        payload = 'Hello, world!'
5781158Szelenkov@nginx.com
5791158Szelenkov@nginx.com        # 4_1_1
5801158Szelenkov@nginx.com
5811158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
5821158Szelenkov@nginx.com
5831158Szelenkov@nginx.com        self.ws.frame_write(sock, 0x03, '')
5841158Szelenkov@nginx.com        self.check_close(sock, 1002)
5851158Szelenkov@nginx.com
5861158Szelenkov@nginx.com        # 4_1_2
5871158Szelenkov@nginx.com
5881158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
5891158Szelenkov@nginx.com
5901158Szelenkov@nginx.com        self.ws.frame_write(sock, 0x04, 'reserved opcode payload')
5911158Szelenkov@nginx.com        self.check_close(sock, 1002)
5921158Szelenkov@nginx.com
5931158Szelenkov@nginx.com        # 4_1_3
5941158Szelenkov@nginx.com
5951158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
5961158Szelenkov@nginx.com
5971158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
5981158Szelenkov@nginx.com
5991158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
6001158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
6011158Szelenkov@nginx.com
6021158Szelenkov@nginx.com        self.ws.frame_write(sock, 0x05, '')
6031158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, '')
6041158Szelenkov@nginx.com
6051158Szelenkov@nginx.com        self.check_close(sock, 1002)
6061158Szelenkov@nginx.com
6071158Szelenkov@nginx.com        # 4_1_4
6081158Szelenkov@nginx.com
6091158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
6101158Szelenkov@nginx.com
6111158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
6121158Szelenkov@nginx.com
6131158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
6141158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
6151158Szelenkov@nginx.com
6161158Szelenkov@nginx.com        self.ws.frame_write(sock, 0x06, payload)
6171158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, '')
6181158Szelenkov@nginx.com
6191158Szelenkov@nginx.com        self.check_close(sock, 1002)
6201158Szelenkov@nginx.com
6211158Szelenkov@nginx.com        # 4_1_5
6221158Szelenkov@nginx.com
6231158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
6241158Szelenkov@nginx.com
6251158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload, chopsize=1)
6261158Szelenkov@nginx.com
6271158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
6281158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
6291158Szelenkov@nginx.com
6301158Szelenkov@nginx.com        self.ws.frame_write(sock, 0x07, payload, chopsize=1)
6311158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, '')
6321158Szelenkov@nginx.com
6331158Szelenkov@nginx.com        self.check_close(sock, 1002)
6341158Szelenkov@nginx.com
6351158Szelenkov@nginx.com        # 4_2_1
6361158Szelenkov@nginx.com
6371158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
6381158Szelenkov@nginx.com
6391158Szelenkov@nginx.com        self.ws.frame_write(sock, 0x0B, '')
6401158Szelenkov@nginx.com        self.check_close(sock, 1002)
6411158Szelenkov@nginx.com
6421158Szelenkov@nginx.com        # 4_2_2
6431158Szelenkov@nginx.com
6441158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
6451158Szelenkov@nginx.com
6461158Szelenkov@nginx.com        self.ws.frame_write(sock, 0x0C, 'reserved opcode payload')
6471158Szelenkov@nginx.com        self.check_close(sock, 1002)
6481158Szelenkov@nginx.com
6491158Szelenkov@nginx.com        # 4_2_3
6501158Szelenkov@nginx.com
6511158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
6521158Szelenkov@nginx.com
6531158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
6541158Szelenkov@nginx.com
6551158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
6561158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
6571158Szelenkov@nginx.com
6581158Szelenkov@nginx.com        self.ws.frame_write(sock, 0x0D, '')
6591158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, '')
6601158Szelenkov@nginx.com
6611158Szelenkov@nginx.com        self.check_close(sock, 1002)
6621158Szelenkov@nginx.com
6631158Szelenkov@nginx.com        # 4_2_4
6641158Szelenkov@nginx.com
6651158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
6661158Szelenkov@nginx.com
6671158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
6681158Szelenkov@nginx.com
6691158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
6701158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
6711158Szelenkov@nginx.com
6721158Szelenkov@nginx.com        self.ws.frame_write(sock, 0x0E, payload)
6731158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, '')
6741158Szelenkov@nginx.com
6751158Szelenkov@nginx.com        self.check_close(sock, 1002)
6761158Szelenkov@nginx.com
6771158Szelenkov@nginx.com        # 4_2_5
6781158Szelenkov@nginx.com
6791158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
6801158Szelenkov@nginx.com
6811158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload, chopsize=1)
6821158Szelenkov@nginx.com
6831158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
6841158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
6851158Szelenkov@nginx.com
6861158Szelenkov@nginx.com        self.ws.frame_write(sock, 0x0F, payload, chopsize=1)
6871158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, '')
6881158Szelenkov@nginx.com
6891158Szelenkov@nginx.com        self.check_close(sock, 1002)
6901158Szelenkov@nginx.com
6911158Szelenkov@nginx.com    def test_java_websockets_5_1__5_20(self):
6921158Szelenkov@nginx.com        self.load('websockets_mirror')
6931158Szelenkov@nginx.com
6941158Szelenkov@nginx.com        # 5_1
6951158Szelenkov@nginx.com
6961158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
6971158Szelenkov@nginx.com
6981158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, 'fragment1', fin=False)
6991158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
7001158Szelenkov@nginx.com        self.check_close(sock, 1002)
7011158Szelenkov@nginx.com
7021158Szelenkov@nginx.com        # 5_2
7031158Szelenkov@nginx.com
7041158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
7051158Szelenkov@nginx.com
7061158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PONG, 'fragment1', fin=False)
7071158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
7081158Szelenkov@nginx.com        self.check_close(sock, 1002)
7091158Szelenkov@nginx.com
7101158Szelenkov@nginx.com        # 5_3
7111158Szelenkov@nginx.com
7121158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
7131158Szelenkov@nginx.com
7141158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
7151158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
7161158Szelenkov@nginx.com
7171158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
7181158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
7191158Szelenkov@nginx.com
7201158Szelenkov@nginx.com        # 5_4
7211158Szelenkov@nginx.com
7221158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
7231596Szelenkov@nginx.com        assert self.recvall(sock, read_timeout=0.1) == b'', '5_4'
7241158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
7251158Szelenkov@nginx.com
7261158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
7271158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
7281158Szelenkov@nginx.com
7291158Szelenkov@nginx.com        # 5_5
7301158Szelenkov@nginx.com
7311158Szelenkov@nginx.com        self.ws.frame_write(
7321158Szelenkov@nginx.com            sock, self.ws.OP_TEXT, 'fragment1', fin=False, chopsize=1
7331158Szelenkov@nginx.com        )
7341158Szelenkov@nginx.com        self.ws.frame_write(
7351158Szelenkov@nginx.com            sock, self.ws.OP_CONT, 'fragment2', fin=True, chopsize=1
7361158Szelenkov@nginx.com        )
7371158Szelenkov@nginx.com
7381158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
7391158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
7401158Szelenkov@nginx.com
7411158Szelenkov@nginx.com        # 5_6
7421158Szelenkov@nginx.com
7431158Szelenkov@nginx.com        ping_payload = 'ping payload'
7441158Szelenkov@nginx.com
7451158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
7461158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, ping_payload)
7471158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
7481158Szelenkov@nginx.com
7491158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
7501158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_PONG, ping_payload)
7511158Szelenkov@nginx.com
7521158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
7531158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
7541158Szelenkov@nginx.com
7551158Szelenkov@nginx.com        # 5_7
7561158Szelenkov@nginx.com
7571158Szelenkov@nginx.com        ping_payload = 'ping payload'
7581158Szelenkov@nginx.com
7591158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
7601596Szelenkov@nginx.com        assert self.recvall(sock, read_timeout=0.1) == b'', '5_7'
7611158Szelenkov@nginx.com
7621158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, ping_payload)
7631158Szelenkov@nginx.com
7641158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
7651158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_PONG, ping_payload)
7661158Szelenkov@nginx.com
7671158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
7681158Szelenkov@nginx.com
7691158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
7701158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
7711158Szelenkov@nginx.com
7721158Szelenkov@nginx.com        # 5_8
7731158Szelenkov@nginx.com
7741158Szelenkov@nginx.com        ping_payload = 'ping payload'
7751158Szelenkov@nginx.com
7761158Szelenkov@nginx.com        self.ws.frame_write(
7771158Szelenkov@nginx.com            sock, self.ws.OP_TEXT, 'fragment1', fin=False, chopsize=1
7781158Szelenkov@nginx.com        )
7791158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, ping_payload, chopsize=1)
7801158Szelenkov@nginx.com        self.ws.frame_write(
7811158Szelenkov@nginx.com            sock, self.ws.OP_CONT, 'fragment2', fin=True, chopsize=1
7821158Szelenkov@nginx.com        )
7831158Szelenkov@nginx.com
7841158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
7851158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_PONG, ping_payload)
7861158Szelenkov@nginx.com
7871158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
7881158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
7891158Szelenkov@nginx.com
7901158Szelenkov@nginx.com        # 5_9
7911158Szelenkov@nginx.com
7921158Szelenkov@nginx.com        self.ws.frame_write(
7931158Szelenkov@nginx.com            sock, self.ws.OP_CONT, 'non-continuation payload', fin=True
7941158Szelenkov@nginx.com        )
7951158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'Hello, world!', fin=True)
7961158Szelenkov@nginx.com        self.check_close(sock, 1002)
7971158Szelenkov@nginx.com
7981158Szelenkov@nginx.com        # 5_10
7991158Szelenkov@nginx.com
8001158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
8011158Szelenkov@nginx.com
8021158Szelenkov@nginx.com        self.ws.frame_write(
8031158Szelenkov@nginx.com            sock, self.ws.OP_CONT, 'non-continuation payload', fin=True
8041158Szelenkov@nginx.com        )
8051158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'Hello, world!', fin=True)
8061158Szelenkov@nginx.com        self.check_close(sock, 1002)
8071158Szelenkov@nginx.com
8081158Szelenkov@nginx.com        # 5_11
8091158Szelenkov@nginx.com
8101158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
8111158Szelenkov@nginx.com
8121158Szelenkov@nginx.com        self.ws.frame_write(
8131158Szelenkov@nginx.com            sock,
8141158Szelenkov@nginx.com            self.ws.OP_CONT,
8151158Szelenkov@nginx.com            'non-continuation payload',
8161158Szelenkov@nginx.com            fin=True,
8171158Szelenkov@nginx.com            chopsize=1,
8181158Szelenkov@nginx.com        )
8191158Szelenkov@nginx.com        self.ws.frame_write(
8201158Szelenkov@nginx.com            sock, self.ws.OP_TEXT, 'Hello, world!', fin=True, chopsize=1
8211158Szelenkov@nginx.com        )
8221158Szelenkov@nginx.com        self.check_close(sock, 1002)
8231158Szelenkov@nginx.com
8241158Szelenkov@nginx.com        # 5_12
8251158Szelenkov@nginx.com
8261158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
8271158Szelenkov@nginx.com
8281158Szelenkov@nginx.com        self.ws.frame_write(
8291158Szelenkov@nginx.com            sock, self.ws.OP_CONT, 'non-continuation payload', fin=False
8301158Szelenkov@nginx.com        )
8311158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'Hello, world!', fin=True)
8321158Szelenkov@nginx.com        self.check_close(sock, 1002)
8331158Szelenkov@nginx.com
8341158Szelenkov@nginx.com        # 5_13
8351158Szelenkov@nginx.com
8361158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
8371158Szelenkov@nginx.com
8381158Szelenkov@nginx.com        self.ws.frame_write(
8391158Szelenkov@nginx.com            sock, self.ws.OP_CONT, 'non-continuation payload', fin=False
8401158Szelenkov@nginx.com        )
8411158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'Hello, world!', fin=True)
8421158Szelenkov@nginx.com        self.check_close(sock, 1002)
8431158Szelenkov@nginx.com
8441158Szelenkov@nginx.com        # 5_14
8451158Szelenkov@nginx.com
8461158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
8471158Szelenkov@nginx.com
8481158Szelenkov@nginx.com        self.ws.frame_write(
8491158Szelenkov@nginx.com            sock,
8501158Szelenkov@nginx.com            self.ws.OP_CONT,
8511158Szelenkov@nginx.com            'non-continuation payload',
8521158Szelenkov@nginx.com            fin=False,
8531158Szelenkov@nginx.com            chopsize=1,
8541158Szelenkov@nginx.com        )
8551158Szelenkov@nginx.com        self.ws.frame_write(
8561158Szelenkov@nginx.com            sock, self.ws.OP_TEXT, 'Hello, world!', fin=True, chopsize=1
8571158Szelenkov@nginx.com        )
8581158Szelenkov@nginx.com        self.check_close(sock, 1002)
8591158Szelenkov@nginx.com
8601158Szelenkov@nginx.com        # 5_15
8611158Szelenkov@nginx.com
8621158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
8631158Szelenkov@nginx.com
8641158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
8651158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
8661158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment3', fin=False)
8671158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment4', fin=True)
8681819Smax.romanov@nginx.com
8691819Smax.romanov@nginx.com        frame = self.ws.frame_read(sock)
8701819Smax.romanov@nginx.com
8711819Smax.romanov@nginx.com        if frame['opcode'] == self.ws.OP_TEXT:
872*2073Szelenkov@nginx.com            self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
8731819Smax.romanov@nginx.com            frame = None
8741819Smax.romanov@nginx.com
8751819Smax.romanov@nginx.com        self.check_close(sock, 1002, frame=frame)
8761158Szelenkov@nginx.com
8771158Szelenkov@nginx.com        # 5_16
8781158Szelenkov@nginx.com
8791158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
8801158Szelenkov@nginx.com
8811158Szelenkov@nginx.com        for i in range(0, 2):
8821158Szelenkov@nginx.com            self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment1', fin=False)
8831158Szelenkov@nginx.com            self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment2', fin=False)
8841158Szelenkov@nginx.com            self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment3', fin=True)
8851158Szelenkov@nginx.com        self.check_close(sock, 1002)
8861158Szelenkov@nginx.com
8871158Szelenkov@nginx.com        # 5_17
8881158Szelenkov@nginx.com
8891158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
8901158Szelenkov@nginx.com
8911158Szelenkov@nginx.com        for i in range(0, 2):
8921158Szelenkov@nginx.com            self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment1', fin=True)
8931158Szelenkov@nginx.com            self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment2', fin=False)
8941158Szelenkov@nginx.com            self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment3', fin=True)
8951158Szelenkov@nginx.com        self.check_close(sock, 1002)
8961158Szelenkov@nginx.com
8971158Szelenkov@nginx.com        # 5_18
8981158Szelenkov@nginx.com
8991158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
9001158Szelenkov@nginx.com
9011158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
9021158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment2')
9031158Szelenkov@nginx.com        self.check_close(sock, 1002)
9041158Szelenkov@nginx.com
9051158Szelenkov@nginx.com        # 5_19
9061158Szelenkov@nginx.com
9071158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
9081158Szelenkov@nginx.com
9091158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
9101158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=False)
9111158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, 'pongme 1!')
9121158Szelenkov@nginx.com
9131158Szelenkov@nginx.com        time.sleep(1)
9141158Szelenkov@nginx.com
9151158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment3', fin=False)
9161158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment4', fin=False)
9171158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, 'pongme 2!')
9181158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment5')
9191158Szelenkov@nginx.com
9201158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
9211158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_PONG, 'pongme 1!')
9221158Szelenkov@nginx.com
9231158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
9241158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_PONG, 'pongme 2!')
9251158Szelenkov@nginx.com
9261158Szelenkov@nginx.com        self.check_frame(
9271158Szelenkov@nginx.com            self.ws.frame_read(sock),
9281158Szelenkov@nginx.com            True,
9291158Szelenkov@nginx.com            self.ws.OP_TEXT,
9301158Szelenkov@nginx.com            'fragment1fragment2fragment3fragment4fragment5',
9311158Szelenkov@nginx.com        )
9321158Szelenkov@nginx.com
9331158Szelenkov@nginx.com        # 5_20
9341158Szelenkov@nginx.com
9351158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
9361158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=False)
9371158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, 'pongme 1!')
9381158Szelenkov@nginx.com
9391158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
9401158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_PONG, 'pongme 1!')
9411158Szelenkov@nginx.com
9421158Szelenkov@nginx.com        time.sleep(1)
9431158Szelenkov@nginx.com
9441158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment3', fin=False)
9451158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment4', fin=False)
9461158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, 'pongme 2!')
9471158Szelenkov@nginx.com
9481158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
9491158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_PONG, 'pongme 2!')
9501158Szelenkov@nginx.com
9511596Szelenkov@nginx.com        assert self.recvall(sock, read_timeout=0.1) == b'', '5_20'
9521158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment5')
9531158Szelenkov@nginx.com
9541158Szelenkov@nginx.com        self.check_frame(
9551158Szelenkov@nginx.com            self.ws.frame_read(sock),
9561158Szelenkov@nginx.com            True,
9571158Szelenkov@nginx.com            self.ws.OP_TEXT,
9581158Szelenkov@nginx.com            'fragment1fragment2fragment3fragment4fragment5',
9591158Szelenkov@nginx.com        )
9601158Szelenkov@nginx.com
9611158Szelenkov@nginx.com        self.close_connection(sock)
9621158Szelenkov@nginx.com
9631158Szelenkov@nginx.com    def test_java_websockets_6_1_1__6_4_4(self):
9641158Szelenkov@nginx.com        self.load('websockets_mirror')
9651158Szelenkov@nginx.com
9661158Szelenkov@nginx.com        # 6_1_1
9671158Szelenkov@nginx.com
9681158Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
9691158Szelenkov@nginx.com
9701158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, '')
9711158Szelenkov@nginx.com        frame = self.ws.frame_read(sock, read_timeout=3)
9721158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, '')
9731158Szelenkov@nginx.com
9741158Szelenkov@nginx.com        # 6_1_2
9751158Szelenkov@nginx.com
9761158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, '', fin=False)
9771158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, '', fin=False)
9781158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, '')
9791158Szelenkov@nginx.com
9801158Szelenkov@nginx.com        frame = self.ws.frame_read(sock, read_timeout=3)
9811158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, '')
9821158Szelenkov@nginx.com
9831158Szelenkov@nginx.com        # 6_1_3
9841158Szelenkov@nginx.com
9851158Szelenkov@nginx.com        payload = 'middle frame payload'
9861158Szelenkov@nginx.com
9871158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, '', fin=False)
9881158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, payload, fin=False)
9891158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, '')
9901158Szelenkov@nginx.com
9911158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
9921158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
9931158Szelenkov@nginx.com
9941158Szelenkov@nginx.com        # 6_2_1
9951158Szelenkov@nginx.com
9961158Szelenkov@nginx.com        payload = 'Hello-µ@ßöäüàá-UTF-8!!'
9971158Szelenkov@nginx.com
9981158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
9991158Szelenkov@nginx.com
10001158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
10011158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
10021158Szelenkov@nginx.com
10031158Szelenkov@nginx.com        # 6_2_2
10041158Szelenkov@nginx.com
10051158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload[:12], fin=False)
10061158Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, payload[12:])
10071158Szelenkov@nginx.com
10081158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
10091158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
10101158Szelenkov@nginx.com
10111158Szelenkov@nginx.com        # 6_2_3
10121158Szelenkov@nginx.com
10131158Szelenkov@nginx.com        self.ws.message(sock, self.ws.OP_TEXT, payload, fragmention_size=1)
10141158Szelenkov@nginx.com
10151158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
10161158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
10171158Szelenkov@nginx.com
10181158Szelenkov@nginx.com        # 6_2_4
10191158Szelenkov@nginx.com
10201158Szelenkov@nginx.com        payload = '\xce\xba\xe1\xbd\xb9\xcf\x83\xce\xbc\xce\xb5'
10211158Szelenkov@nginx.com
10221158Szelenkov@nginx.com        self.ws.message(sock, self.ws.OP_TEXT, payload, fragmention_size=1)
10231158Szelenkov@nginx.com
10241158Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
10251158Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
10261158Szelenkov@nginx.com
10271158Szelenkov@nginx.com        self.close_connection(sock)
10281158Szelenkov@nginx.com
10291848Szelenkov@nginx.com    #        Unit does not support UTF-8 validation
10301848Szelenkov@nginx.com    #
10311848Szelenkov@nginx.com    #        # 6_3_1 FAIL
10321848Szelenkov@nginx.com    #
10331848Szelenkov@nginx.com    #        payload_1 = '\xce\xba\xe1\xbd\xb9\xcf\x83\xce\xbc\xce\xb5'
10341848Szelenkov@nginx.com    #        payload_2 = '\xed\xa0\x80'
10351848Szelenkov@nginx.com    #        payload_3 = '\x65\x64\x69\x74\x65\x64'
10361848Szelenkov@nginx.com    #
10371848Szelenkov@nginx.com    #        payload = payload_1 + payload_2 + payload_3
10381848Szelenkov@nginx.com    #
10391848Szelenkov@nginx.com    #        self.ws.message(sock, self.ws.OP_TEXT, payload)
10401848Szelenkov@nginx.com    #        self.check_close(sock, 1007)
10411848