-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_ws_ping.py
executable file
·611 lines (498 loc) · 15.6 KB
/
test_ws_ping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#! /usr/bin/python3
import asyncio
import ssl
import time
from multiprocessing import Process
from threading import Thread
import requests
import websockets
from framework import tester
from framework.x509 import CertGenerator
from helpers import tf_cfg
__author__ = "Tempesta Technologies, Inc."
__copyright__ = "Copyright (C) 2017-2024 Tempesta Technologies, Inc."
__license__ = "GPL2"
GENERAL_WORKDIR = tf_cfg.cfg.get("General", "workdir")
CERT_PATH = f"{GENERAL_WORKDIR}/cert.pem"
KEY_PATH = f"{GENERAL_WORKDIR}/key.pem"
TEMPESTA_IP = tf_cfg.cfg.get("Tempesta", "ip")
SERVER_IP = tf_cfg.cfg.get("Server", "ip")
hostname = "localhost"
ping_message = "ping_test"
TEMPESTA_CONFIG = """
listen 81;
listen 82 proto=https;
srv_group default {
server ${server_ip}:8099;
}
vhost default {
tls_certificate ${general_workdir}/cert.pem;
tls_certificate_key ${general_workdir}/key.pem;
tls_match_any_server_name;
proxy_pass default;
}
http_chain {
-> default;
}
%s
"""
TEMPESTA_STRESS_CONFIG = """
listen 81;
listen 82 proto=https;
srv_group default {
server ${server_ip}:8099;
server ${server_ip}:8100;
server ${server_ip}:8101;
server ${server_ip}:8102;
server ${server_ip}:8103;
server ${server_ip}:8104;
server ${server_ip}:8105;
server ${server_ip}:8106;
server ${server_ip}:8107;
server ${server_ip}:8108;
server ${server_ip}:8109;
server ${server_ip}:8110;
server ${server_ip}:8111;
server ${server_ip}:8112;
server ${server_ip}:8113;
server ${server_ip}:8114;
server ${server_ip}:8115;
server ${server_ip}:8116;
server ${server_ip}:8117;
server ${server_ip}:8118;
server ${server_ip}:8119;
server ${server_ip}:8120;
server ${server_ip}:8121;
server ${server_ip}:8122;
server ${server_ip}:8123;
server ${server_ip}:8124;
server ${server_ip}:8125;
server ${server_ip}:8126;
server ${server_ip}:8127;
server ${server_ip}:8128;
server ${server_ip}:8129;
server ${server_ip}:8130;
server ${server_ip}:8131;
server ${server_ip}:8132;
server ${server_ip}:8133;
server ${server_ip}:8134;
server ${server_ip}:8135;
server ${server_ip}:8136;
server ${server_ip}:8137;
server ${server_ip}:8138;
server ${server_ip}:8139;
server ${server_ip}:8140;
server ${server_ip}:8141;
server ${server_ip}:8142;
server ${server_ip}:8143;
server ${server_ip}:8144;
server ${server_ip}:8145;
server ${server_ip}:8146;
server ${server_ip}:8147;
server ${server_ip}:8148;
server ${server_ip}:8149;
}
vhost default {
tls_certificate ${general_workdir}/cert.pem;
tls_certificate_key ${general_workdir}/key.pem;
tls_match_any_server_name;
proxy_pass default;
}
http_chain {
-> default;
}
"""
TEMPESTA_NGINX_CONFIG = """
listen 81;
listen 82 proto=https;
srv_group default {
server ${server_ip}:8000;
}
vhost default {
tls_certificate ${general_workdir}/cert.pem;
tls_certificate_key ${general_workdir}/key.pem;
tls_match_any_server_name;
proxy_pass default;
}
http_chain {
-> default;
}
%s
"""
TEMPESTA_CACHE_CONFIG = """
listen 81;
srv_group default {
server ${server_ip}:8099;
}
vhost default {
proxy_pass default;
}
access_log on;
cache 1;
cache_fulfill * *;
http_chain {
-> default;
}
%s
"""
NGINX_CONFIG = """
pid ${pid};
worker_processes auto;
events {
worker_connections 1024;
use epoll;
}
http {
map $$http_upgrade $$connection_upgrade {
default Upgrade;
'' close;
}
upstream websocket {
ip_hash;
server ${server_ip}:8099;
}
upstream wss_websockets {
ip_hash;
server ${server_ip}:8099;
}
server {
listen 8000;
location / {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $$http_upgrade;
proxy_set_header Connection $$connection_upgrade;
proxy_set_header Host $$host;
}
}
server {
listen 8001 ssl;
ssl_protocols TLSv1.2;
ssl_certificate ${general_workdir}/cert.pem;
ssl_certificate_key ${general_workdir}/key.pem;
location / {
proxy_pass http://wss_websockets;
proxy_http_version 1.1;
proxy_set_header Upgrade $$http_upgrade;
proxy_set_header Connection $$connection_upgrade;
proxy_set_header Host $$host;
}
}
}
"""
def gen_cert(host_name):
cgen = CertGenerator(CERT_PATH, KEY_PATH)
cgen.CN = host_name
cgen.generate()
class WsPing(tester.TempestaTest):
"""Ping test for websocket ws scheme"""
backends = []
clients = []
tempesta = {
"config": TEMPESTA_CONFIG % "",
}
# Client
def run_test(self, port, n):
for _ in range(n):
asyncio.run(self.ws_ping_test(port))
async def ws_ping_test(self, port):
global ping_message
async with websockets.connect(f"ws://{TEMPESTA_IP}:{port}") as websocket:
await websocket.send(ping_message)
await websocket.recv()
await websocket.close()
async def wss_ping_test(self, port):
global ping_message
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
ssl_context.load_verify_locations(f"{GENERAL_WORKDIR}/cert.pem")
async with websockets.connect(f"wss://{TEMPESTA_IP}:{port}", ssl=ssl_context) as websocket:
await websocket.send(ping_message)
await websocket.recv()
await websocket.close()
def run_ws(self, port, count=1, proxy=False):
if proxy:
self.start_all_servers()
loop = asyncio.get_event_loop()
for i in range(count):
asyncio.ensure_future(websockets.serve(self.handler, SERVER_IP, port + i))
loop.run_forever()
def test(self):
self.p1 = Process(target=self.run_ws, args=(8099,))
self.p2 = Process(target=self.run_test, args=(81, 4))
self.p1.start()
self.start_tempesta()
self.p2.start()
self.p2.join(timeout=5)
# Backend
async def handler(self, websocket, path):
global ping_message
data = await websocket.recv()
reply = f"{data}"
if f"{data}" != ping_message:
self.fail("Ping message corrupted")
await websocket.send(reply)
def setUp(self):
self.p1 = None
self.p2 = None
super().setUp()
self.addCleanup(self.cleanup_p1)
self.addCleanup(self.cleanup_p2)
def cleanup_p1(self):
if self.p1:
self.p1.terminate()
self.p1 = None
def cleanup_p2(self):
if self.p2:
self.p2.terminate()
self.p2 = None
class WssPing(WsPing):
"""Ping test for websocket wss scheme."""
def run_test(self, port, n):
for _ in range(n):
asyncio.run(self.wss_ping_test(port))
def run_wss(self, port, count=1, proxy=False):
if proxy:
self.start_all_servers()
loop = asyncio.get_event_loop()
for i in range(count):
asyncio.ensure_future(websockets.serve(self.handler, SERVER_IP, port + i))
loop.run_forever()
def test(self):
gen_cert(hostname)
self.p1 = Process(target=self.run_wss, args=(8099,))
self.p2 = Process(target=self.run_test, args=(82, 4))
self.p1.start()
self.start_tempesta()
self.p2.start()
self.p2.join()
class WssPingProxy(WssPing):
"""
Ping test for websocket wss scheme with nginx proxying TLS
Scheme: WSClient (TLS)-> Tempesta-fw -> NGINX (TLS)-> wss
"""
backends = [
{
"id": "nginx",
"type": "nginx",
"port": "8000",
"status_uri": "http://${server_ip}:8000/nginx_status",
"config": NGINX_CONFIG,
}
]
tempesta = {
"config": TEMPESTA_NGINX_CONFIG % "",
}
def test(self):
gen_cert(TEMPESTA_IP)
self.p1 = Process(target=self.run_wss, args=(8099, 1, True))
self.p2 = Process(target=self.run_test, args=(82, 4))
self.p1.start()
self.start_tempesta()
self.p2.start()
self.p2.join()
self.get_server("nginx").stop_nginx()
class CacheTest(WsPing):
"""
Test case - we never cache 101 responses
First: Send upgrade HTTP connection and - get 101
Second: Terminate websocket, call HTTP upgrade again - get 502
"""
tempesta = {
"config": TEMPESTA_CACHE_CONFIG % "",
}
async def handler(self, websocket, path):
pass
def call_upgrade(self, port, expected_status):
headers_ = {
"Host": TEMPESTA_IP,
"Connection": "Upgrade",
"Pragma": "no-cache",
"Cache-Control": "no-cache",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36",
"Upgrade": "websocket",
"Origin": "null",
"Sec-WebSocket-Version": "13",
"Accept-Encoding": "gzip, deflate",
"Sec-WebSocket-Key": "V4wPm2Z/oOIUvp+uaX3CFQ==",
"Sec-WebSocket-Accept": "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",
"Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits",
}
r = requests.get(f"http://{TEMPESTA_IP}:{port}", auth=("user", "pass"), headers=headers_)
if r.status_code not in expected_status:
self.fail(
f"Test failed cause received invalid status_code: {r.status_code}, expected: {expected_status}"
)
def test(self):
self.p1 = Process(target=self.run_ws, args=(8099,))
self.p1.start()
self.start_tempesta()
self.call_upgrade(81, [101])
self.p1.terminate()
self.call_upgrade(81, [502, 504])
class WssStress(WssPing):
"""
Asynchronously make WSS Connections and restart tempesta
"""
tempesta = {
"config": TEMPESTA_STRESS_CONFIG,
}
def fibo(self, n):
fib = [0, 1]
for i in range(n):
fib.append(fib[-2] + fib[-1])
if fib[-1] > n:
break
return fib
def run_test(self, port, n):
count = 0
for _ in range(n):
count += 1
asyncio.run(self.wss_ping_test(port))
if (4000 - count) in self.fibo(4000):
self.get_tempesta().restart()
def test(self):
gen_cert(hostname)
self.p1 = Process(target=self.run_ws, args=(8099, 50))
self.p2 = Process(target=self.run_test, args=(82, 4000))
self.p1.start()
self.start_tempesta()
self.p2.start()
self.p2.join()
class WsPipelining(WsPing):
"""
We sent 3 pipelined requests against websocket.
Expected - 101, 502, 502 response codes
"""
clients = [{"id": "deproxy", "type": "deproxy", "addr": "${tempesta_ip}", "port": "81"}]
tempesta = {
"config": TEMPESTA_CONFIG % "",
}
request = (
"GET / HTTP/1.1\r\n"
f"Host: {hostname}\r\n"
"Connection: Upgrade\r\n"
"Upgrade: websocket\r\n"
"Sec-WebSocket-Version: 13\r\n"
"Sec-WebSocket-Key: V4wPm2Z/oOIUvp+uaX3CFQ==\r\n"
"Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
"Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n"
"\r\n"
"GET / HTTP/1.1\r\n"
f"Host: {hostname}\r\n"
"Connection: Upgrade\r\n"
"Upgrade: websocket\r\n"
"Sec-WebSocket-Version: 13\r\n"
"Sec-WebSocket-Key: V4wPm2Z/oOIUvp+uaX3CFQ==\r\n"
"Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
"Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n"
"\r\n"
"GET / HTTP/1.1\r\n"
f"Host: {hostname}\r\n"
"Connection: Upgrade\r\n"
"Upgrade: websocket\r\n"
"Sec-WebSocket-Version: 13\r\n"
"Sec-WebSocket-Key: V4wPm2Z/oOIUvp+uaX3CFQ==\r\n"
"Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
"Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n"
"\r\n"
)
async def handler(self, websocket, path):
pass
def test(self):
self.p1 = Process(target=self.run_ws, args=(8099,))
self.p1.start()
self.start_tempesta()
time.sleep(5)
self.deproxy_manager.start()
deproxy_cl = self.get_client("deproxy")
deproxy_cl.start()
deproxy_cl.make_requests(self.request)
deproxy_cl.wait_for_response(timeout=5)
for resp in deproxy_cl.responses:
tf_cfg.dbg(3, resp)
class WsScheduler(WsPing):
"""
Create 4 connections against 1 backend ws
Make 256 async client ws connections
Expected result - All ping messages recieved
"""
tempesta = {
"config": """
listen 81;
srv_group default {
server ${server_ip}:8099 conns_n=16;
}
vhost default {
proxy_pass default;
}
http_chain {
-> default;
}
""",
}
async def handler(self, websocket, path):
global ping_message
data = await websocket.recv()
reply = f"{data}"
if data != ping_message:
self.fail("Wrong Ping Message")
await websocket.send(reply)
def test(self):
self.p1 = Process(target=self.run_ws, args=(8099, 4))
self.p2 = Process(target=self.run_test, args=(81, 1500))
self.p1.start()
self.start_tempesta()
self.p2.start()
self.p2.join()
class RestartOnUpgrade(WsPing):
"""
Asyncly create many Upgrade requests
against WS during tempesta-fw restart.
Expected - 101 response code
"""
tempesta = {
"config": TEMPESTA_CONFIG % "",
}
async def handler(self, websocket, path):
pass
async def call_upgrade(self, port, expected_status):
headers_ = {
"Host": hostname,
"Connection": "Upgrade",
"Pragma": "no-cache",
"Cache-Control": "no-cache",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36",
"Upgrade": "websocket",
"Origin": "null",
"Sec-WebSocket-Version": "13",
"Accept-Encoding": "gzip, deflate",
"Sec-WebSocket-Key": "V4wPm2Z/oOIUvp+uaX3CFQ==",
"Sec-WebSocket-Accept": "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",
"Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits",
}
r = requests.get(f"http://{TEMPESTA_IP}:{port}", auth=("user", "pass"), headers=headers_)
if r.status_code not in expected_status:
self.fail(f"Received invalid status_code {r.status_code}")
def fibo(self, n):
fib = [0, 1]
for _ in range(n):
fib.append(fib[-2] + fib[-1])
if fib[-1] > n:
break
return fib
def run_test(self, port, n):
for i in range(1500):
asyncio.run(self.call_upgrade(port, n))
if i in self.fibo(1500):
self.get_tempesta().restart()
def test(self):
self.p1 = Process(target=self.run_ws, args=(8099,))
self.p2 = Thread(target=self.run_test, args=(81, [101]))
self.p1.start()
self.start_tempesta()
time.sleep(2)
self.p2.start()
self.p2.join(timeout=30)
self.p2 = None