test_java_websockets.py (1477:b93d1acf81bd) test_java_websockets.py (1596:b7e2d4d92624)
1import pytest
1import struct
2import time
2import struct
3import time
3import unittest
4
5from unit.applications.lang.java import TestApplicationJava
6from unit.applications.websockets import TestApplicationWebsocket
4
5from unit.applications.lang.java import TestApplicationJava
6from unit.applications.websockets import TestApplicationWebsocket
7from conftest import option, skip_alert
7
8
9class TestJavaWebsockets(TestApplicationJava):
10 prerequisites = {'modules': {'java': 'any'}}
11
12 ws = TestApplicationWebsocket()
13
8
9
10class TestJavaWebsockets(TestApplicationJava):
11 prerequisites = {'modules': {'java': 'any'}}
12
13 ws = TestApplicationWebsocket()
14
14 def setUp(self):
15 super().setUp()
15 def setup_method(self):
16 super().setup_method()
16
17
17 self.assertIn(
18 'success',
19 self.conf(
20 {'http': {'websocket': {'keepalive_interval': 0}}}, 'settings'
21 ),
22 'clear keepalive_interval',
23 )
18 assert 'success' in self.conf(
19 {'http': {'websocket': {'keepalive_interval': 0}}}, 'settings'
20 ), 'clear keepalive_interval'
24
21
25 self.skip_alerts.extend(
26 [r'socket close\(\d+\) failed']
27 )
22 skip_alert(r'socket close\(\d+\) failed')
28
29 def close_connection(self, sock):
23
24 def close_connection(self, sock):
30 self.assertEqual(self.recvall(sock, read_timeout=0.1), b'', 'empty soc')
25 assert self.recvall(sock, read_timeout=0.1) == b'', 'empty soc'
31
32 self.ws.frame_write(sock, self.ws.OP_CLOSE, self.ws.serialize_close())
33
34 self.check_close(sock)
35
36 def check_close(self, sock, code=1000, no_close=False):
37 frame = self.ws.frame_read(sock)
38
26
27 self.ws.frame_write(sock, self.ws.OP_CLOSE, self.ws.serialize_close())
28
29 self.check_close(sock)
30
31 def check_close(self, sock, code=1000, no_close=False):
32 frame = self.ws.frame_read(sock)
33
39 self.assertEqual(frame['fin'], True, 'close fin')
40 self.assertEqual(frame['opcode'], self.ws.OP_CLOSE, 'close opcode')
41 self.assertEqual(frame['code'], code, 'close code')
34 assert frame['fin'] == True, 'close fin'
35 assert frame['opcode'] == self.ws.OP_CLOSE, 'close opcode'
36 assert frame['code'] == code, 'close code'
42
43 if not no_close:
44 sock.close()
45
46 def check_frame(self, frame, fin, opcode, payload, decode=True):
47 if opcode == self.ws.OP_BINARY or not decode:
48 data = frame['data']
49 else:
50 data = frame['data'].decode('utf-8')
51
37
38 if not no_close:
39 sock.close()
40
41 def check_frame(self, frame, fin, opcode, payload, decode=True):
42 if opcode == self.ws.OP_BINARY or not decode:
43 data = frame['data']
44 else:
45 data = frame['data'].decode('utf-8')
46
52 self.assertEqual(frame['fin'], fin, 'fin')
53 self.assertEqual(frame['opcode'], opcode, 'opcode')
54 self.assertEqual(data, payload, 'payload')
47 assert frame['fin'] == fin, 'fin'
48 assert frame['opcode'] == opcode, 'opcode'
49 assert data == payload, 'payload'
55
56 def test_java_websockets_handshake(self):
57 self.load('websockets_mirror')
58
59 resp, sock, key = self.ws.upgrade()
60 sock.close()
61
50
51 def test_java_websockets_handshake(self):
52 self.load('websockets_mirror')
53
54 resp, sock, key = self.ws.upgrade()
55 sock.close()
56
62 self.assertEqual(resp['status'], 101, 'status')
63 self.assertEqual(resp['headers']['Upgrade'], 'websocket', 'upgrade')
64 self.assertEqual(
65 resp['headers']['Connection'], 'Upgrade', 'connection'
66 )
67 self.assertEqual(
68 resp['headers']['Sec-WebSocket-Accept'], self.ws.accept(key), 'key'
69 )
57 assert resp['status'] == 101, 'status'
58 assert resp['headers']['Upgrade'] == 'websocket', 'upgrade'
59 assert resp['headers']['Connection'] == 'Upgrade', 'connection'
60 assert resp['headers']['Sec-WebSocket-Accept'] == self.ws.accept(
61 key
62 ), 'key'
70
71 def test_java_websockets_mirror(self):
72 self.load('websockets_mirror')
73
74 message = 'blah'
75
76 _, sock, _ = self.ws.upgrade()
77
78 self.ws.frame_write(sock, self.ws.OP_TEXT, message)
79 frame = self.ws.frame_read(sock)
80
63
64 def test_java_websockets_mirror(self):
65 self.load('websockets_mirror')
66
67 message = 'blah'
68
69 _, sock, _ = self.ws.upgrade()
70
71 self.ws.frame_write(sock, self.ws.OP_TEXT, message)
72 frame = self.ws.frame_read(sock)
73
81 self.assertEqual(message, frame['data'].decode('utf-8'), 'mirror')
74 assert message == frame['data'].decode('utf-8'), 'mirror'
82
83 self.ws.frame_write(sock, self.ws.OP_TEXT, message)
84 frame = self.ws.frame_read(sock)
85
75
76 self.ws.frame_write(sock, self.ws.OP_TEXT, message)
77 frame = self.ws.frame_read(sock)
78
86 self.assertEqual(message, frame['data'].decode('utf-8'), 'mirror 2')
79 assert message == frame['data'].decode('utf-8'), 'mirror 2'
87
88 sock.close()
89
90 def test_java_websockets_no_mask(self):
91 self.load('websockets_mirror')
92
93 message = 'blah'
94
95 _, sock, _ = self.ws.upgrade()
96
97 self.ws.frame_write(sock, self.ws.OP_TEXT, message, mask=False)
98
99 frame = self.ws.frame_read(sock)
100
80
81 sock.close()
82
83 def test_java_websockets_no_mask(self):
84 self.load('websockets_mirror')
85
86 message = 'blah'
87
88 _, sock, _ = self.ws.upgrade()
89
90 self.ws.frame_write(sock, self.ws.OP_TEXT, message, mask=False)
91
92 frame = self.ws.frame_read(sock)
93
101 self.assertEqual(frame['opcode'], self.ws.OP_CLOSE, 'no mask opcode')
102 self.assertEqual(frame['code'], 1002, 'no mask close code')
94 assert frame['opcode'] == self.ws.OP_CLOSE, 'no mask opcode'
95 assert frame['code'] == 1002, 'no mask close code'
103
104 sock.close()
105
106 def test_java_websockets_fragmentation(self):
107 self.load('websockets_mirror')
108
109 message = 'blah'
110
111 _, sock, _ = self.ws.upgrade()
112
113 self.ws.frame_write(sock, self.ws.OP_TEXT, message, fin=False)
114 self.ws.frame_write(sock, self.ws.OP_CONT, ' ', fin=False)
115 self.ws.frame_write(sock, self.ws.OP_CONT, message)
116
117 frame = self.ws.frame_read(sock)
118
96
97 sock.close()
98
99 def test_java_websockets_fragmentation(self):
100 self.load('websockets_mirror')
101
102 message = 'blah'
103
104 _, sock, _ = self.ws.upgrade()
105
106 self.ws.frame_write(sock, self.ws.OP_TEXT, message, fin=False)
107 self.ws.frame_write(sock, self.ws.OP_CONT, ' ', fin=False)
108 self.ws.frame_write(sock, self.ws.OP_CONT, message)
109
110 frame = self.ws.frame_read(sock)
111
119 self.assertEqual(
120 message + ' ' + message,
121 frame['data'].decode('utf-8'),
122 'mirror framing',
123 )
112 assert message + ' ' + message == frame['data'].decode(
113 'utf-8'
114 ), 'mirror framing'
124
125 sock.close()
126
127 def test_java_websockets_frame_fragmentation_invalid(self):
128 self.load('websockets_mirror')
129
130 message = 'blah'
131
132 _, sock, _ = self.ws.upgrade()
133
134 self.ws.frame_write(sock, self.ws.OP_PING, message, fin=False)
135
136 frame = self.ws.frame_read(sock)
137
138 frame.pop('data')
115
116 sock.close()
117
118 def test_java_websockets_frame_fragmentation_invalid(self):
119 self.load('websockets_mirror')
120
121 message = 'blah'
122
123 _, sock, _ = self.ws.upgrade()
124
125 self.ws.frame_write(sock, self.ws.OP_PING, message, fin=False)
126
127 frame = self.ws.frame_read(sock)
128
129 frame.pop('data')
139 self.assertDictEqual(
140 frame,
141 {
142 'fin': True,
143 'rsv1': False,
144 'rsv2': False,
145 'rsv3': False,
146 'opcode': self.ws.OP_CLOSE,
147 'mask': 0,
148 'code': 1002,
149 'reason': 'Fragmented control frame',
150 },
151 'close frame',
152 )
130 assert frame == {
131 'fin': True,
132 'rsv1': False,
133 'rsv2': False,
134 'rsv3': False,
135 'opcode': self.ws.OP_CLOSE,
136 'mask': 0,
137 'code': 1002,
138 'reason': 'Fragmented control frame',
139 }, 'close frame'
153
154 sock.close()
155
156 def test_java_websockets_two_clients(self):
157 self.load('websockets_mirror')
158
159 message1 = 'blah1'
160 message2 = 'blah2'
161
162 _, sock1, _ = self.ws.upgrade()
163 _, sock2, _ = self.ws.upgrade()
164
165 self.ws.frame_write(sock1, self.ws.OP_TEXT, message1)
166 self.ws.frame_write(sock2, self.ws.OP_TEXT, message2)
167
168 frame1 = self.ws.frame_read(sock1)
169 frame2 = self.ws.frame_read(sock2)
170
140
141 sock.close()
142
143 def test_java_websockets_two_clients(self):
144 self.load('websockets_mirror')
145
146 message1 = 'blah1'
147 message2 = 'blah2'
148
149 _, sock1, _ = self.ws.upgrade()
150 _, sock2, _ = self.ws.upgrade()
151
152 self.ws.frame_write(sock1, self.ws.OP_TEXT, message1)
153 self.ws.frame_write(sock2, self.ws.OP_TEXT, message2)
154
155 frame1 = self.ws.frame_read(sock1)
156 frame2 = self.ws.frame_read(sock2)
157
171 self.assertEqual(message1, frame1['data'].decode('utf-8'), 'client 1')
172 self.assertEqual(message2, frame2['data'].decode('utf-8'), 'client 2')
158 assert message1 == frame1['data'].decode('utf-8'), 'client 1'
159 assert message2 == frame2['data'].decode('utf-8'), 'client 2'
173
174 sock1.close()
175 sock2.close()
176
160
161 sock1.close()
162 sock2.close()
163
177 @unittest.skip('not yet')
164 @pytest.mark.skip('not yet')
178 def test_java_websockets_handshake_upgrade_absent(
179 self
180 ): # FAIL https://tools.ietf.org/html/rfc6455#section-4.2.1
181 self.load('websockets_mirror')
182
183 resp = self.get(
184 headers={
185 'Host': 'localhost',
186 'Connection': 'Upgrade',
187 'Sec-WebSocket-Key': self.ws.key(),
188 'Sec-WebSocket-Protocol': 'chat',
189 'Sec-WebSocket-Version': 13,
190 },
191 )
192
165 def test_java_websockets_handshake_upgrade_absent(
166 self
167 ): # FAIL https://tools.ietf.org/html/rfc6455#section-4.2.1
168 self.load('websockets_mirror')
169
170 resp = self.get(
171 headers={
172 'Host': 'localhost',
173 'Connection': 'Upgrade',
174 'Sec-WebSocket-Key': self.ws.key(),
175 'Sec-WebSocket-Protocol': 'chat',
176 'Sec-WebSocket-Version': 13,
177 },
178 )
179
193 self.assertEqual(resp['status'], 400, 'upgrade absent')
180 assert resp['status'] == 400, 'upgrade absent'
194
195 def test_java_websockets_handshake_case_insensitive(self):
196 self.load('websockets_mirror')
197
198 resp, sock, _ = self.ws.upgrade(
199 headers={
200 'Host': 'localhost',
201 'Upgrade': 'WEBSOCKET',
202 'Connection': 'UPGRADE',
203 'Sec-WebSocket-Key': self.ws.key(),
204 'Sec-WebSocket-Protocol': 'chat',
205 'Sec-WebSocket-Version': 13,
206 }
207 )
208 sock.close()
209
181
182 def test_java_websockets_handshake_case_insensitive(self):
183 self.load('websockets_mirror')
184
185 resp, sock, _ = self.ws.upgrade(
186 headers={
187 'Host': 'localhost',
188 'Upgrade': 'WEBSOCKET',
189 'Connection': 'UPGRADE',
190 'Sec-WebSocket-Key': self.ws.key(),
191 'Sec-WebSocket-Protocol': 'chat',
192 'Sec-WebSocket-Version': 13,
193 }
194 )
195 sock.close()
196
210 self.assertEqual(resp['status'], 101, 'status')
197 assert resp['status'] == 101, 'status'
211
198
212 @unittest.skip('not yet')
199 @pytest.mark.skip('not yet')
213 def test_java_websockets_handshake_connection_absent(self): # FAIL
214 self.load('websockets_mirror')
215
216 resp = self.get(
217 headers={
218 'Host': 'localhost',
219 'Upgrade': 'websocket',
220 'Sec-WebSocket-Key': self.ws.key(),
221 'Sec-WebSocket-Protocol': 'chat',
222 'Sec-WebSocket-Version': 13,
223 },
224 )
225
200 def test_java_websockets_handshake_connection_absent(self): # FAIL
201 self.load('websockets_mirror')
202
203 resp = self.get(
204 headers={
205 'Host': 'localhost',
206 'Upgrade': 'websocket',
207 'Sec-WebSocket-Key': self.ws.key(),
208 'Sec-WebSocket-Protocol': 'chat',
209 'Sec-WebSocket-Version': 13,
210 },
211 )
212
226 self.assertEqual(resp['status'], 400, 'status')
213 assert resp['status'] == 400, 'status'
227
228 def test_java_websockets_handshake_version_absent(self):
229 self.load('websockets_mirror')
230
231 resp = self.get(
232 headers={
233 'Host': 'localhost',
234 'Upgrade': 'websocket',
235 'Connection': 'Upgrade',
236 'Sec-WebSocket-Key': self.ws.key(),
237 'Sec-WebSocket-Protocol': 'chat',
238 },
239 )
240
214
215 def test_java_websockets_handshake_version_absent(self):
216 self.load('websockets_mirror')
217
218 resp = self.get(
219 headers={
220 'Host': 'localhost',
221 'Upgrade': 'websocket',
222 'Connection': 'Upgrade',
223 'Sec-WebSocket-Key': self.ws.key(),
224 'Sec-WebSocket-Protocol': 'chat',
225 },
226 )
227
241 self.assertEqual(resp['status'], 426, 'status')
228 assert resp['status'] == 426, 'status'
242
229
243 @unittest.skip('not yet')
230 @pytest.mark.skip('not yet')
244 def test_java_websockets_handshake_key_invalid(self):
245 self.load('websockets_mirror')
246
247 resp = self.get(
248 headers={
249 'Host': 'localhost',
250 'Upgrade': 'websocket',
251 'Connection': 'Upgrade',
252 'Sec-WebSocket-Key': '!',
253 'Sec-WebSocket-Protocol': 'chat',
254 'Sec-WebSocket-Version': 13,
255 },
256 )
257
231 def test_java_websockets_handshake_key_invalid(self):
232 self.load('websockets_mirror')
233
234 resp = self.get(
235 headers={
236 'Host': 'localhost',
237 'Upgrade': 'websocket',
238 'Connection': 'Upgrade',
239 'Sec-WebSocket-Key': '!',
240 'Sec-WebSocket-Protocol': 'chat',
241 'Sec-WebSocket-Version': 13,
242 },
243 )
244
258 self.assertEqual(resp['status'], 400, 'key length')
245 assert resp['status'] == 400, 'key length'
259
260 key = self.ws.key()
261 resp = self.get(
262 headers={
263 'Host': 'localhost',
264 'Upgrade': 'websocket',
265 'Connection': 'Upgrade',
266 'Sec-WebSocket-Key': [key, key],
267 'Sec-WebSocket-Protocol': 'chat',
268 'Sec-WebSocket-Version': 13,
269 },
270 )
271
246
247 key = self.ws.key()
248 resp = self.get(
249 headers={
250 'Host': 'localhost',
251 'Upgrade': 'websocket',
252 'Connection': 'Upgrade',
253 'Sec-WebSocket-Key': [key, key],
254 'Sec-WebSocket-Protocol': 'chat',
255 'Sec-WebSocket-Version': 13,
256 },
257 )
258
272 self.assertEqual(
273 resp['status'], 400, 'key double'
274 ) # FAIL https://tools.ietf.org/html/rfc6455#section-11.3.1
259 assert resp['status'] == 400, 'key double' # FAIL https://tools.ietf.org/html/rfc6455#section-11.3.1
275
276 def test_java_websockets_handshake_method_invalid(self):
277 self.load('websockets_mirror')
278
279 resp = self.post(
280 headers={
281 'Host': 'localhost',
282 'Upgrade': 'websocket',
283 'Connection': 'Upgrade',
284 'Sec-WebSocket-Key': self.ws.key(),
285 'Sec-WebSocket-Protocol': 'chat',
286 'Sec-WebSocket-Version': 13,
287 },
288 )
289
260
261 def test_java_websockets_handshake_method_invalid(self):
262 self.load('websockets_mirror')
263
264 resp = self.post(
265 headers={
266 'Host': 'localhost',
267 'Upgrade': 'websocket',
268 'Connection': 'Upgrade',
269 'Sec-WebSocket-Key': self.ws.key(),
270 'Sec-WebSocket-Protocol': 'chat',
271 'Sec-WebSocket-Version': 13,
272 },
273 )
274
290 self.assertEqual(resp['status'], 400, 'status')
275 assert resp['status'] == 400, 'status'
291
292 def test_java_websockets_handshake_http_10(self):
293 self.load('websockets_mirror')
294
295 resp = self.get(
296 headers={
297 'Host': 'localhost',
298 'Upgrade': 'websocket',
299 'Connection': 'Upgrade',
300 'Sec-WebSocket-Key': self.ws.key(),
301 'Sec-WebSocket-Protocol': 'chat',
302 'Sec-WebSocket-Version': 13,
303 },
304 http_10=True,
305 )
306
276
277 def test_java_websockets_handshake_http_10(self):
278 self.load('websockets_mirror')
279
280 resp = self.get(
281 headers={
282 'Host': 'localhost',
283 'Upgrade': 'websocket',
284 'Connection': 'Upgrade',
285 'Sec-WebSocket-Key': self.ws.key(),
286 'Sec-WebSocket-Protocol': 'chat',
287 'Sec-WebSocket-Version': 13,
288 },
289 http_10=True,
290 )
291
307 self.assertEqual(resp['status'], 400, 'status')
292 assert resp['status'] == 400, 'status'
308
309 def test_java_websockets_handshake_uri_invalid(self):
310 self.load('websockets_mirror')
311
312 resp = self.get(
313 headers={
314 'Host': 'localhost',
315 'Upgrade': 'websocket',
316 'Connection': 'Upgrade',
317 'Sec-WebSocket-Key': self.ws.key(),
318 'Sec-WebSocket-Protocol': 'chat',
319 'Sec-WebSocket-Version': 13,
320 },
321 url='!',
322 )
323
293
294 def test_java_websockets_handshake_uri_invalid(self):
295 self.load('websockets_mirror')
296
297 resp = self.get(
298 headers={
299 'Host': 'localhost',
300 'Upgrade': 'websocket',
301 'Connection': 'Upgrade',
302 'Sec-WebSocket-Key': self.ws.key(),
303 'Sec-WebSocket-Protocol': 'chat',
304 'Sec-WebSocket-Version': 13,
305 },
306 url='!',
307 )
308
324 self.assertEqual(resp['status'], 400, 'status')
309 assert resp['status'] == 400, 'status'
325
326 def test_java_websockets_protocol_absent(self):
327 self.load('websockets_mirror')
328
329 key = self.ws.key()
330 resp, sock, _ = self.ws.upgrade(
331 headers={
332 'Host': 'localhost',
333 'Upgrade': 'websocket',
334 'Connection': 'Upgrade',
335 'Sec-WebSocket-Key': key,
336 'Sec-WebSocket-Version': 13,
337 }
338 )
339 sock.close()
340
310
311 def test_java_websockets_protocol_absent(self):
312 self.load('websockets_mirror')
313
314 key = self.ws.key()
315 resp, sock, _ = self.ws.upgrade(
316 headers={
317 'Host': 'localhost',
318 'Upgrade': 'websocket',
319 'Connection': 'Upgrade',
320 'Sec-WebSocket-Key': key,
321 'Sec-WebSocket-Version': 13,
322 }
323 )
324 sock.close()
325
341 self.assertEqual(resp['status'], 101, 'status')
342 self.assertEqual(resp['headers']['Upgrade'], 'websocket', 'upgrade')
343 self.assertEqual(
344 resp['headers']['Connection'], 'Upgrade', 'connection'
345 )
346 self.assertEqual(
347 resp['headers']['Sec-WebSocket-Accept'], self.ws.accept(key), 'key'
348 )
326 assert resp['status'] == 101, 'status'
327 assert resp['headers']['Upgrade'] == 'websocket', 'upgrade'
328 assert resp['headers']['Connection'] == 'Upgrade', 'connection'
329 assert resp['headers']['Sec-WebSocket-Accept'] == self.ws.accept(
330 key
331 ), 'key'
349
350 # autobahn-testsuite
351 #
352 # Some following tests fail because of Unit does not support UTF-8
353 # validation for websocket frames. It should be implemented
354 # by application, if necessary.
355
356 def test_java_websockets_1_1_1__1_1_8(self):

--- 80 unchanged lines hidden (view full) ---

437 def test_java_websockets_2_7__2_9(self):
438 self.load('websockets_mirror')
439
440 # 2_7
441
442 _, sock, _ = self.ws.upgrade()
443
444 self.ws.frame_write(sock, self.ws.OP_PONG, '')
332
333 # autobahn-testsuite
334 #
335 # Some following tests fail because of Unit does not support UTF-8
336 # validation for websocket frames. It should be implemented
337 # by application, if necessary.
338
339 def test_java_websockets_1_1_1__1_1_8(self):

--- 80 unchanged lines hidden (view full) ---

420 def test_java_websockets_2_7__2_9(self):
421 self.load('websockets_mirror')
422
423 # 2_7
424
425 _, sock, _ = self.ws.upgrade()
426
427 self.ws.frame_write(sock, self.ws.OP_PONG, '')
445 self.assertEqual(self.recvall(sock, read_timeout=0.1), b'', '2_7')
428 assert self.recvall(sock, read_timeout=0.1) == b'', '2_7'
446
447 # 2_8
448
449 self.ws.frame_write(sock, self.ws.OP_PONG, 'unsolicited pong payload')
429
430 # 2_8
431
432 self.ws.frame_write(sock, self.ws.OP_PONG, 'unsolicited pong payload')
450 self.assertEqual(self.recvall(sock, read_timeout=0.1), b'', '2_8')
433 assert self.recvall(sock, read_timeout=0.1) == b'', '2_8'
451
452 # 2_9
453
454 payload = 'ping payload'
455
456 self.ws.frame_write(sock, self.ws.OP_PONG, 'unsolicited pong payload')
457 self.ws.frame_write(sock, self.ws.OP_PING, payload)
458

--- 23 unchanged lines hidden (view full) ---

482 self.ws.frame_write(sock, opcode, 'payload-%d' % i, chopsize=1)
483
484 for i in range(0, 10):
485 frame = self.ws.frame_read(sock)
486 self.check_frame(frame, True, self.ws.OP_PONG, 'payload-%d' % i)
487
488 self.close_connection(sock)
489
434
435 # 2_9
436
437 payload = 'ping payload'
438
439 self.ws.frame_write(sock, self.ws.OP_PONG, 'unsolicited pong payload')
440 self.ws.frame_write(sock, self.ws.OP_PING, payload)
441

--- 23 unchanged lines hidden (view full) ---

465 self.ws.frame_write(sock, opcode, 'payload-%d' % i, chopsize=1)
466
467 for i in range(0, 10):
468 frame = self.ws.frame_read(sock)
469 self.check_frame(frame, True, self.ws.OP_PONG, 'payload-%d' % i)
470
471 self.close_connection(sock)
472
490 @unittest.skip('not yet')
473 @pytest.mark.skip('not yet')
491 def test_java_websockets_3_1__3_7(self):
492 self.load('websockets_mirror')
493
494 payload = 'Hello, world!'
495
496 # 3_1
497
498 _, sock, _ = self.ws.upgrade()

--- 9 unchanged lines hidden (view full) ---

508 self.ws.frame_write(sock, self.ws.OP_TEXT, payload, rsv2=True)
509 self.ws.frame_write(sock, self.ws.OP_PING, '')
510
511 frame = self.ws.frame_read(sock)
512 self.check_frame(frame, True, self.ws.OP_TEXT, payload)
513
514 self.check_close(sock, 1002, no_close=True)
515
474 def test_java_websockets_3_1__3_7(self):
475 self.load('websockets_mirror')
476
477 payload = 'Hello, world!'
478
479 # 3_1
480
481 _, sock, _ = self.ws.upgrade()

--- 9 unchanged lines hidden (view full) ---

491 self.ws.frame_write(sock, self.ws.OP_TEXT, payload, rsv2=True)
492 self.ws.frame_write(sock, self.ws.OP_PING, '')
493
494 frame = self.ws.frame_read(sock)
495 self.check_frame(frame, True, self.ws.OP_TEXT, payload)
496
497 self.check_close(sock, 1002, no_close=True)
498
516 self.assertEqual(self.recvall(sock, read_timeout=0.1), b'', 'empty 3_2')
499 assert self.recvall(sock, read_timeout=0.1) == b'', 'empty 3_2'
517 sock.close()
518
519 # 3_3
520
521 _, sock, _ = self.ws.upgrade()
522
523 self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
524
525 frame = self.ws.frame_read(sock)
526 self.check_frame(frame, True, self.ws.OP_TEXT, payload)
527
528 self.ws.frame_write(
529 sock, self.ws.OP_TEXT, payload, rsv1=True, rsv2=True
530 )
531
532 self.check_close(sock, 1002, no_close=True)
533
500 sock.close()
501
502 # 3_3
503
504 _, sock, _ = self.ws.upgrade()
505
506 self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
507
508 frame = self.ws.frame_read(sock)
509 self.check_frame(frame, True, self.ws.OP_TEXT, payload)
510
511 self.ws.frame_write(
512 sock, self.ws.OP_TEXT, payload, rsv1=True, rsv2=True
513 )
514
515 self.check_close(sock, 1002, no_close=True)
516
534 self.assertEqual(self.recvall(sock, read_timeout=0.1), b'', 'empty 3_3')
517 assert self.recvall(sock, read_timeout=0.1) == b'', 'empty 3_3'
535 sock.close()
536
537 # 3_4
538
539 _, sock, _ = self.ws.upgrade()
540
541 self.ws.frame_write(sock, self.ws.OP_TEXT, payload, chopsize=1)
542 self.ws.frame_write(
543 sock, self.ws.OP_TEXT, payload, rsv3=True, chopsize=1
544 )
545 self.ws.frame_write(sock, self.ws.OP_PING, '')
546
547 frame = self.ws.frame_read(sock)
548 self.check_frame(frame, True, self.ws.OP_TEXT, payload)
549
550 self.check_close(sock, 1002, no_close=True)
551
518 sock.close()
519
520 # 3_4
521
522 _, sock, _ = self.ws.upgrade()
523
524 self.ws.frame_write(sock, self.ws.OP_TEXT, payload, chopsize=1)
525 self.ws.frame_write(
526 sock, self.ws.OP_TEXT, payload, rsv3=True, chopsize=1
527 )
528 self.ws.frame_write(sock, self.ws.OP_PING, '')
529
530 frame = self.ws.frame_read(sock)
531 self.check_frame(frame, True, self.ws.OP_TEXT, payload)
532
533 self.check_close(sock, 1002, no_close=True)
534
552 self.assertEqual(self.recvall(sock, read_timeout=0.1), b'', 'empty 3_4')
535 assert self.recvall(sock, read_timeout=0.1) == b'', 'empty 3_4'
553 sock.close()
554
555 # 3_5
556
557 _, sock, _ = self.ws.upgrade()
558
559 self.ws.frame_write(
560 sock,

--- 169 unchanged lines hidden (view full) ---

730 self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
731
732 frame = self.ws.frame_read(sock)
733 self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
734
735 # 5_4
736
737 self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
536 sock.close()
537
538 # 3_5
539
540 _, sock, _ = self.ws.upgrade()
541
542 self.ws.frame_write(
543 sock,

--- 169 unchanged lines hidden (view full) ---

713 self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
714
715 frame = self.ws.frame_read(sock)
716 self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
717
718 # 5_4
719
720 self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
738 self.assertEqual(self.recvall(sock, read_timeout=0.1), b'', '5_4')
721 assert self.recvall(sock, read_timeout=0.1) == b'', '5_4'
739 self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
740
741 frame = self.ws.frame_read(sock)
742 self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
743
744 # 5_5
745
746 self.ws.frame_write(

--- 20 unchanged lines hidden (view full) ---

767 frame = self.ws.frame_read(sock)
768 self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
769
770 # 5_7
771
772 ping_payload = 'ping payload'
773
774 self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
722 self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
723
724 frame = self.ws.frame_read(sock)
725 self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
726
727 # 5_5
728
729 self.ws.frame_write(

--- 20 unchanged lines hidden (view full) ---

750 frame = self.ws.frame_read(sock)
751 self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
752
753 # 5_7
754
755 ping_payload = 'ping payload'
756
757 self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
775 self.assertEqual(self.recvall(sock, read_timeout=0.1), b'', '5_7')
758 assert self.recvall(sock, read_timeout=0.1) == b'', '5_7'
776
777 self.ws.frame_write(sock, self.ws.OP_PING, ping_payload)
778
779 frame = self.ws.frame_read(sock)
780 self.check_frame(frame, True, self.ws.OP_PONG, ping_payload)
781
782 self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
783

--- 167 unchanged lines hidden (view full) ---

951
952 self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment3', fin=False)
953 self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment4', fin=False)
954 self.ws.frame_write(sock, self.ws.OP_PING, 'pongme 2!')
955
956 frame = self.ws.frame_read(sock)
957 self.check_frame(frame, True, self.ws.OP_PONG, 'pongme 2!')
958
759
760 self.ws.frame_write(sock, self.ws.OP_PING, ping_payload)
761
762 frame = self.ws.frame_read(sock)
763 self.check_frame(frame, True, self.ws.OP_PONG, ping_payload)
764
765 self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
766

--- 167 unchanged lines hidden (view full) ---

934
935 self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment3', fin=False)
936 self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment4', fin=False)
937 self.ws.frame_write(sock, self.ws.OP_PING, 'pongme 2!')
938
939 frame = self.ws.frame_read(sock)
940 self.check_frame(frame, True, self.ws.OP_PONG, 'pongme 2!')
941
959 self.assertEqual(self.recvall(sock, read_timeout=0.1), b'', '5_20')
942 assert self.recvall(sock, read_timeout=0.1) == b'', '5_20'
960 self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment5')
961
962 self.check_frame(
963 self.ws.frame_read(sock),
964 True,
965 self.ws.OP_TEXT,
966 'fragment1fragment2fragment3fragment4fragment5',
967 )

--- 116 unchanged lines hidden (view full) ---

1084 # 7_1_3
1085
1086 _, sock, _ = self.ws.upgrade()
1087
1088 self.ws.frame_write(sock, self.ws.OP_CLOSE, self.ws.serialize_close())
1089 self.check_close(sock, no_close=True)
1090
1091 self.ws.frame_write(sock, self.ws.OP_PING, '')
943 self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment5')
944
945 self.check_frame(
946 self.ws.frame_read(sock),
947 True,
948 self.ws.OP_TEXT,
949 'fragment1fragment2fragment3fragment4fragment5',
950 )

--- 116 unchanged lines hidden (view full) ---

1067 # 7_1_3
1068
1069 _, sock, _ = self.ws.upgrade()
1070
1071 self.ws.frame_write(sock, self.ws.OP_CLOSE, self.ws.serialize_close())
1072 self.check_close(sock, no_close=True)
1073
1074 self.ws.frame_write(sock, self.ws.OP_PING, '')
1092 self.assertEqual(self.recvall(sock, read_timeout=0.1), b'', 'empty soc')
1075 assert self.recvall(sock, read_timeout=0.1) == b'', 'empty soc'
1093
1094 sock.close()
1095
1096 # 7_1_4
1097
1098 _, sock, _ = self.ws.upgrade()
1099
1100 self.ws.frame_write(sock, self.ws.OP_CLOSE, self.ws.serialize_close())
1101 self.check_close(sock, no_close=True)
1102
1103 self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
1076
1077 sock.close()
1078
1079 # 7_1_4
1080
1081 _, sock, _ = self.ws.upgrade()
1082
1083 self.ws.frame_write(sock, self.ws.OP_CLOSE, self.ws.serialize_close())
1084 self.check_close(sock, no_close=True)
1085
1086 self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
1104 self.assertEqual(self.recvall(sock, read_timeout=0.1), b'', 'empty soc')
1087 assert self.recvall(sock, read_timeout=0.1) == b'', 'empty soc'
1105
1106 sock.close()
1107
1108 # 7_1_5
1109
1110 _, sock, _ = self.ws.upgrade()
1111
1112 self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
1113 self.ws.frame_write(sock, self.ws.OP_CLOSE, self.ws.serialize_close())
1114 self.check_close(sock, no_close=True)
1115
1116 self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2')
1088
1089 sock.close()
1090
1091 # 7_1_5
1092
1093 _, sock, _ = self.ws.upgrade()
1094
1095 self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
1096 self.ws.frame_write(sock, self.ws.OP_CLOSE, self.ws.serialize_close())
1097 self.check_close(sock, no_close=True)
1098
1099 self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2')
1117 self.assertEqual(self.recvall(sock, read_timeout=0.1), b'', 'empty soc')
1100 assert self.recvall(sock, read_timeout=0.1) == b'', 'empty soc'
1118
1119 sock.close()
1120
1121 # 7_1_6
1122
1123 _, sock, _ = self.ws.upgrade()
1124
1125 self.ws.frame_write(sock, self.ws.OP_TEXT, 'BAsd7&jh23' * 26 * 2 ** 10)
1126 self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
1127 self.ws.frame_write(sock, self.ws.OP_CLOSE, self.ws.serialize_close())
1128
1129 self.recvall(sock, read_timeout=1)
1130
1131 self.ws.frame_write(sock, self.ws.OP_PING, '')
1101
1102 sock.close()
1103
1104 # 7_1_6
1105
1106 _, sock, _ = self.ws.upgrade()
1107
1108 self.ws.frame_write(sock, self.ws.OP_TEXT, 'BAsd7&jh23' * 26 * 2 ** 10)
1109 self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
1110 self.ws.frame_write(sock, self.ws.OP_CLOSE, self.ws.serialize_close())
1111
1112 self.recvall(sock, read_timeout=1)
1113
1114 self.ws.frame_write(sock, self.ws.OP_PING, '')
1132 self.assertEqual(self.recvall(sock, read_timeout=0.1), b'', 'empty soc')
1115 assert self.recvall(sock, read_timeout=0.1) == b'', 'empty soc'
1133
1134 sock.close()
1135
1136 # 7_3_1
1137
1138 _, sock, _ = self.ws.upgrade()
1139
1140 self.ws.frame_write(sock, self.ws.OP_CLOSE, '')

--- 103 unchanged lines hidden (view full) ---

1244
1245 _, sock, _ = self.ws.upgrade()
1246
1247 payload = struct.pack('!I', 65536) + ''.encode('utf-8')
1248
1249 self.ws.frame_write(sock, self.ws.OP_CLOSE, payload)
1250 self.check_close(sock, 1002)
1251
1116
1117 sock.close()
1118
1119 # 7_3_1
1120
1121 _, sock, _ = self.ws.upgrade()
1122
1123 self.ws.frame_write(sock, self.ws.OP_CLOSE, '')

--- 103 unchanged lines hidden (view full) ---

1227
1228 _, sock, _ = self.ws.upgrade()
1229
1230 payload = struct.pack('!I', 65536) + ''.encode('utf-8')
1231
1232 self.ws.frame_write(sock, self.ws.OP_CLOSE, payload)
1233 self.check_close(sock, 1002)
1234
1252 def test_java_websockets_9_1_1__9_6_6(self):
1253 if not self.unsafe:
1254 self.skipTest("unsafe, long run")
1235 def test_java_websockets_9_1_1__9_6_6(self, is_unsafe):
1236 if not is_unsafe:
1237 pytest.skip('unsafe, long run')
1255
1256 self.load('websockets_mirror')
1257
1238
1239 self.load('websockets_mirror')
1240
1258 self.assertIn(
1259 'success',
1260 self.conf(
1261 {
1262 'http': {
1263 'websocket': {
1264 'max_frame_size': 33554432,
1265 'keepalive_interval': 0,
1266 }
1241 assert 'success' in self.conf(
1242 {
1243 'http': {
1244 'websocket': {
1245 'max_frame_size': 33554432,
1246 'keepalive_interval': 0,
1267 }
1247 }
1268 },
1269 'settings',
1270 ),
1271 'increase max_frame_size and keepalive_interval',
1272 )
1248 }
1249 },
1250 'settings',
1251 ), 'increase max_frame_size and keepalive_interval'
1273
1274 _, sock, _ = self.ws.upgrade()
1275
1276 op_text = self.ws.OP_TEXT
1277 op_binary = self.ws.OP_BINARY
1278
1279 def check_payload(opcode, length, chopsize=None):
1280 if opcode == self.ws.OP_TEXT:

--- 24 unchanged lines hidden (view full) ---

1305
1306 check_payload(op_binary, 64 * 2 ** 10) # 9_2_1
1307 check_payload(op_binary, 256 * 2 ** 10) # 9_2_2
1308 check_payload(op_binary, 2 ** 20) # 9_2_3
1309 check_payload(op_binary, 4 * 2 ** 20) # 9_2_4
1310 check_payload(op_binary, 8 * 2 ** 20) # 9_2_5
1311 check_payload(op_binary, 16 * 2 ** 20) # 9_2_6
1312
1252
1253 _, sock, _ = self.ws.upgrade()
1254
1255 op_text = self.ws.OP_TEXT
1256 op_binary = self.ws.OP_BINARY
1257
1258 def check_payload(opcode, length, chopsize=None):
1259 if opcode == self.ws.OP_TEXT:

--- 24 unchanged lines hidden (view full) ---

1284
1285 check_payload(op_binary, 64 * 2 ** 10) # 9_2_1
1286 check_payload(op_binary, 256 * 2 ** 10) # 9_2_2
1287 check_payload(op_binary, 2 ** 20) # 9_2_3
1288 check_payload(op_binary, 4 * 2 ** 20) # 9_2_4
1289 check_payload(op_binary, 8 * 2 ** 20) # 9_2_5
1290 check_payload(op_binary, 16 * 2 ** 20) # 9_2_6
1291
1313 if self.system != 'Darwin' and self.system != 'FreeBSD':
1292 if option.system != 'Darwin' and option.system != 'FreeBSD':
1314 check_message(op_text, 64) # 9_3_1
1315 check_message(op_text, 256) # 9_3_2
1316 check_message(op_text, 2 ** 10) # 9_3_3
1317 check_message(op_text, 4 * 2 ** 10) # 9_3_4
1318 check_message(op_text, 16 * 2 ** 10) # 9_3_5
1319 check_message(op_text, 64 * 2 ** 10) # 9_3_6
1320 check_message(op_text, 256 * 2 ** 10) # 9_3_7
1321 check_message(op_text, 2 ** 20) # 9_3_8

--- 39 unchanged lines hidden (view full) ---

1361
1362 self.close_connection(sock)
1363
1364 # settings
1365
1366 def test_java_websockets_max_frame_size(self):
1367 self.load('websockets_mirror')
1368
1293 check_message(op_text, 64) # 9_3_1
1294 check_message(op_text, 256) # 9_3_2
1295 check_message(op_text, 2 ** 10) # 9_3_3
1296 check_message(op_text, 4 * 2 ** 10) # 9_3_4
1297 check_message(op_text, 16 * 2 ** 10) # 9_3_5
1298 check_message(op_text, 64 * 2 ** 10) # 9_3_6
1299 check_message(op_text, 256 * 2 ** 10) # 9_3_7
1300 check_message(op_text, 2 ** 20) # 9_3_8

--- 39 unchanged lines hidden (view full) ---

1340
1341 self.close_connection(sock)
1342
1343 # settings
1344
1345 def test_java_websockets_max_frame_size(self):
1346 self.load('websockets_mirror')
1347
1369 self.assertIn(
1370 'success',
1371 self.conf(
1372 {'http': {'websocket': {'max_frame_size': 100}}}, 'settings'
1373 ),
1374 'configure max_frame_size',
1375 )
1348 assert 'success' in self.conf(
1349 {'http': {'websocket': {'max_frame_size': 100}}}, 'settings'
1350 ), 'configure max_frame_size'
1376
1377 _, sock, _ = self.ws.upgrade()
1378
1379 payload = '*' * 94
1380 opcode = self.ws.OP_TEXT
1381
1382 self.ws.frame_write(sock, opcode, payload) # frame length is 100
1383
1384 frame = self.ws.frame_read(sock)
1385 self.check_frame(frame, True, opcode, payload)
1386
1387 payload = '*' * 95
1388
1389 self.ws.frame_write(sock, opcode, payload) # frame length is 101
1390 self.check_close(sock, 1009) # 1009 - CLOSE_TOO_LARGE
1391
1392 def test_java_websockets_read_timeout(self):
1393 self.load('websockets_mirror')
1394
1351
1352 _, sock, _ = self.ws.upgrade()
1353
1354 payload = '*' * 94
1355 opcode = self.ws.OP_TEXT
1356
1357 self.ws.frame_write(sock, opcode, payload) # frame length is 100
1358
1359 frame = self.ws.frame_read(sock)
1360 self.check_frame(frame, True, opcode, payload)
1361
1362 payload = '*' * 95
1363
1364 self.ws.frame_write(sock, opcode, payload) # frame length is 101
1365 self.check_close(sock, 1009) # 1009 - CLOSE_TOO_LARGE
1366
1367 def test_java_websockets_read_timeout(self):
1368 self.load('websockets_mirror')
1369
1395 self.assertIn(
1396 'success',
1397 self.conf(
1398 {'http': {'websocket': {'read_timeout': 5}}}, 'settings'
1399 ),
1400 'configure read_timeout',
1401 )
1370 assert 'success' in self.conf(
1371 {'http': {'websocket': {'read_timeout': 5}}}, 'settings'
1372 ), 'configure read_timeout'
1402
1403 _, sock, _ = self.ws.upgrade()
1404
1405 frame = self.ws.frame_to_send(self.ws.OP_TEXT, 'blah')
1406 sock.sendall(frame[:2])
1407
1408 time.sleep(2)
1409
1410 self.check_close(sock, 1001) # 1001 - CLOSE_GOING_AWAY
1411
1412 def test_java_websockets_keepalive_interval(self):
1413 self.load('websockets_mirror')
1414
1373
1374 _, sock, _ = self.ws.upgrade()
1375
1376 frame = self.ws.frame_to_send(self.ws.OP_TEXT, 'blah')
1377 sock.sendall(frame[:2])
1378
1379 time.sleep(2)
1380
1381 self.check_close(sock, 1001) # 1001 - CLOSE_GOING_AWAY
1382
1383 def test_java_websockets_keepalive_interval(self):
1384 self.load('websockets_mirror')
1385
1415 self.assertIn(
1416 'success',
1417 self.conf(
1418 {'http': {'websocket': {'keepalive_interval': 5}}}, 'settings'
1419 ),
1420 'configure keepalive_interval',
1421 )
1386 assert 'success' in self.conf(
1387 {'http': {'websocket': {'keepalive_interval': 5}}}, 'settings'
1388 ), 'configure keepalive_interval'
1422
1423 _, sock, _ = self.ws.upgrade()
1424
1425 frame = self.ws.frame_to_send(self.ws.OP_TEXT, 'blah')
1426 sock.sendall(frame[:2])
1427
1428 time.sleep(2)
1429
1430 frame = self.ws.frame_read(sock)
1431 self.check_frame(frame, True, self.ws.OP_PING, '') # PING frame
1432
1433 sock.close()
1389
1390 _, sock, _ = self.ws.upgrade()
1391
1392 frame = self.ws.frame_to_send(self.ws.OP_TEXT, 'blah')
1393 sock.sendall(frame[:2])
1394
1395 time.sleep(2)
1396
1397 frame = self.ws.frame_read(sock)
1398 self.check_frame(frame, True, self.ws.OP_PING, '') # PING frame
1399
1400 sock.close()
1434
1435
1436if __name__ == '__main__':
1437 TestJavaWebsockets.main()