xref: /aosp_15_r20/external/curl/tests/http/test_20_websockets.py (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*6236dae4SAndroid Build Coastguard Worker# -*- coding: utf-8 -*-
3*6236dae4SAndroid Build Coastguard Worker#***************************************************************************
4*6236dae4SAndroid Build Coastguard Worker#                                  _   _ ____  _
5*6236dae4SAndroid Build Coastguard Worker#  Project                     ___| | | |  _ \| |
6*6236dae4SAndroid Build Coastguard Worker#                             / __| | | | |_) | |
7*6236dae4SAndroid Build Coastguard Worker#                            | (__| |_| |  _ <| |___
8*6236dae4SAndroid Build Coastguard Worker#                             \___|\___/|_| \_\_____|
9*6236dae4SAndroid Build Coastguard Worker#
10*6236dae4SAndroid Build Coastguard Worker# Copyright (C) Daniel Stenberg, <[email protected]>, et al.
11*6236dae4SAndroid Build Coastguard Worker#
12*6236dae4SAndroid Build Coastguard Worker# This software is licensed as described in the file COPYING, which
13*6236dae4SAndroid Build Coastguard Worker# you should have received as part of this distribution. The terms
14*6236dae4SAndroid Build Coastguard Worker# are also available at https://curl.se/docs/copyright.html.
15*6236dae4SAndroid Build Coastguard Worker#
16*6236dae4SAndroid Build Coastguard Worker# You may opt to use, copy, modify, merge, publish, distribute and/or sell
17*6236dae4SAndroid Build Coastguard Worker# copies of the Software, and permit persons to whom the Software is
18*6236dae4SAndroid Build Coastguard Worker# furnished to do so, under the terms of the COPYING file.
19*6236dae4SAndroid Build Coastguard Worker#
20*6236dae4SAndroid Build Coastguard Worker# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21*6236dae4SAndroid Build Coastguard Worker# KIND, either express or implied.
22*6236dae4SAndroid Build Coastguard Worker#
23*6236dae4SAndroid Build Coastguard Worker# SPDX-License-Identifier: curl
24*6236dae4SAndroid Build Coastguard Worker#
25*6236dae4SAndroid Build Coastguard Worker###########################################################################
26*6236dae4SAndroid Build Coastguard Worker#
27*6236dae4SAndroid Build Coastguard Workerimport logging
28*6236dae4SAndroid Build Coastguard Workerimport os
29*6236dae4SAndroid Build Coastguard Workerimport shutil
30*6236dae4SAndroid Build Coastguard Workerimport subprocess
31*6236dae4SAndroid Build Coastguard Workerimport time
32*6236dae4SAndroid Build Coastguard Workerfrom datetime import datetime, timedelta
33*6236dae4SAndroid Build Coastguard Workerimport pytest
34*6236dae4SAndroid Build Coastguard Worker
35*6236dae4SAndroid Build Coastguard Workerfrom testenv import Env, CurlClient, LocalClient
36*6236dae4SAndroid Build Coastguard Worker
37*6236dae4SAndroid Build Coastguard Worker
38*6236dae4SAndroid Build Coastguard Workerlog = logging.getLogger(__name__)
39*6236dae4SAndroid Build Coastguard Worker
40*6236dae4SAndroid Build Coastguard Worker
41*6236dae4SAndroid Build Coastguard Worker@pytest.mark.skipif(condition=not Env.curl_has_protocol('ws'),
42*6236dae4SAndroid Build Coastguard Worker                    reason='curl lacks ws protocol support')
43*6236dae4SAndroid Build Coastguard Workerclass TestWebsockets:
44*6236dae4SAndroid Build Coastguard Worker
45*6236dae4SAndroid Build Coastguard Worker    def check_alive(self, env, timeout=5):
46*6236dae4SAndroid Build Coastguard Worker        curl = CurlClient(env=env)
47*6236dae4SAndroid Build Coastguard Worker        url = f'http://localhost:{env.ws_port}/'
48*6236dae4SAndroid Build Coastguard Worker        end = datetime.now() + timedelta(seconds=timeout)
49*6236dae4SAndroid Build Coastguard Worker        while datetime.now() < end:
50*6236dae4SAndroid Build Coastguard Worker            r = curl.http_download(urls=[url])
51*6236dae4SAndroid Build Coastguard Worker            if r.exit_code == 0:
52*6236dae4SAndroid Build Coastguard Worker                return True
53*6236dae4SAndroid Build Coastguard Worker            time.sleep(.1)
54*6236dae4SAndroid Build Coastguard Worker        return False
55*6236dae4SAndroid Build Coastguard Worker
56*6236dae4SAndroid Build Coastguard Worker    def _mkpath(self, path):
57*6236dae4SAndroid Build Coastguard Worker        if not os.path.exists(path):
58*6236dae4SAndroid Build Coastguard Worker            return os.makedirs(path)
59*6236dae4SAndroid Build Coastguard Worker
60*6236dae4SAndroid Build Coastguard Worker    def _rmrf(self, path):
61*6236dae4SAndroid Build Coastguard Worker        if os.path.exists(path):
62*6236dae4SAndroid Build Coastguard Worker            return shutil.rmtree(path)
63*6236dae4SAndroid Build Coastguard Worker
64*6236dae4SAndroid Build Coastguard Worker    @pytest.fixture(autouse=True, scope='class')
65*6236dae4SAndroid Build Coastguard Worker    def ws_echo(self, env):
66*6236dae4SAndroid Build Coastguard Worker        run_dir = os.path.join(env.gen_dir, 'ws-echo-server')
67*6236dae4SAndroid Build Coastguard Worker        err_file = os.path.join(run_dir, 'stderr')
68*6236dae4SAndroid Build Coastguard Worker        self._rmrf(run_dir)
69*6236dae4SAndroid Build Coastguard Worker        self._mkpath(run_dir)
70*6236dae4SAndroid Build Coastguard Worker
71*6236dae4SAndroid Build Coastguard Worker        with open(err_file, 'w') as cerr:
72*6236dae4SAndroid Build Coastguard Worker            cmd = os.path.join(env.project_dir,
73*6236dae4SAndroid Build Coastguard Worker                               'tests/http/testenv/ws_echo_server.py')
74*6236dae4SAndroid Build Coastguard Worker            args = [cmd, '--port', str(env.ws_port)]
75*6236dae4SAndroid Build Coastguard Worker            p = subprocess.Popen(args=args, cwd=run_dir, stderr=cerr,
76*6236dae4SAndroid Build Coastguard Worker                                 stdout=cerr)
77*6236dae4SAndroid Build Coastguard Worker            assert self.check_alive(env)
78*6236dae4SAndroid Build Coastguard Worker            yield
79*6236dae4SAndroid Build Coastguard Worker            p.terminate()
80*6236dae4SAndroid Build Coastguard Worker
81*6236dae4SAndroid Build Coastguard Worker    def test_20_01_basic(self, env: Env, ws_echo, repeat):
82*6236dae4SAndroid Build Coastguard Worker        curl = CurlClient(env=env)
83*6236dae4SAndroid Build Coastguard Worker        url = f'http://localhost:{env.ws_port}/'
84*6236dae4SAndroid Build Coastguard Worker        r = curl.http_download(urls=[url])
85*6236dae4SAndroid Build Coastguard Worker        r.check_response(http_status=426)
86*6236dae4SAndroid Build Coastguard Worker
87*6236dae4SAndroid Build Coastguard Worker    def test_20_02_pingpong_small(self, env: Env, ws_echo, repeat):
88*6236dae4SAndroid Build Coastguard Worker        payload = 125 * "x"
89*6236dae4SAndroid Build Coastguard Worker        client = LocalClient(env=env, name='ws-pingpong')
90*6236dae4SAndroid Build Coastguard Worker        if not client.exists():
91*6236dae4SAndroid Build Coastguard Worker            pytest.skip(f'example client not built: {client.name}')
92*6236dae4SAndroid Build Coastguard Worker        url = f'ws://localhost:{env.ws_port}/'
93*6236dae4SAndroid Build Coastguard Worker        r = client.run(args=[url, payload])
94*6236dae4SAndroid Build Coastguard Worker        r.check_exit_code(0)
95*6236dae4SAndroid Build Coastguard Worker
96*6236dae4SAndroid Build Coastguard Worker    # the python websocket server does not like 'large' control frames
97*6236dae4SAndroid Build Coastguard Worker    def test_20_03_pingpong_too_large(self, env: Env, ws_echo, repeat):
98*6236dae4SAndroid Build Coastguard Worker        payload = 127 * "x"
99*6236dae4SAndroid Build Coastguard Worker        client = LocalClient(env=env, name='ws-pingpong')
100*6236dae4SAndroid Build Coastguard Worker        if not client.exists():
101*6236dae4SAndroid Build Coastguard Worker            pytest.skip(f'example client not built: {client.name}')
102*6236dae4SAndroid Build Coastguard Worker        url = f'ws://localhost:{env.ws_port}/'
103*6236dae4SAndroid Build Coastguard Worker        r = client.run(args=[url, payload])
104*6236dae4SAndroid Build Coastguard Worker        r.check_exit_code(56)
105*6236dae4SAndroid Build Coastguard Worker
106*6236dae4SAndroid Build Coastguard Worker    # the python websocket server does not like 'large' control frames
107*6236dae4SAndroid Build Coastguard Worker    def test_20_04_data_small(self, env: Env, ws_echo, repeat):
108*6236dae4SAndroid Build Coastguard Worker        client = LocalClient(env=env, name='ws-data')
109*6236dae4SAndroid Build Coastguard Worker        if not client.exists():
110*6236dae4SAndroid Build Coastguard Worker            pytest.skip(f'example client not built: {client.name}')
111*6236dae4SAndroid Build Coastguard Worker        url = f'ws://localhost:{env.ws_port}/'
112*6236dae4SAndroid Build Coastguard Worker        r = client.run(args=[url, str(0), str(10)])
113*6236dae4SAndroid Build Coastguard Worker        r.check_exit_code(0)
114*6236dae4SAndroid Build Coastguard Worker
115*6236dae4SAndroid Build Coastguard Worker    # the python websocket server does not like 'large' control frames
116*6236dae4SAndroid Build Coastguard Worker    def test_20_05_data_med(self, env: Env, ws_echo, repeat):
117*6236dae4SAndroid Build Coastguard Worker        client = LocalClient(env=env, name='ws-data')
118*6236dae4SAndroid Build Coastguard Worker        if not client.exists():
119*6236dae4SAndroid Build Coastguard Worker            pytest.skip(f'example client not built: {client.name}')
120*6236dae4SAndroid Build Coastguard Worker        url = f'ws://localhost:{env.ws_port}/'
121*6236dae4SAndroid Build Coastguard Worker        r = client.run(args=[url, str(120), str(130)])
122*6236dae4SAndroid Build Coastguard Worker        r.check_exit_code(0)
123*6236dae4SAndroid Build Coastguard Worker
124*6236dae4SAndroid Build Coastguard Worker    # the python websocket server does not like 'large' control frames
125*6236dae4SAndroid Build Coastguard Worker    def test_20_06_data_large(self, env: Env, ws_echo, repeat):
126*6236dae4SAndroid Build Coastguard Worker        client = LocalClient(env=env, name='ws-data')
127*6236dae4SAndroid Build Coastguard Worker        if not client.exists():
128*6236dae4SAndroid Build Coastguard Worker            pytest.skip(f'example client not built: {client.name}')
129*6236dae4SAndroid Build Coastguard Worker        url = f'ws://localhost:{env.ws_port}/'
130*6236dae4SAndroid Build Coastguard Worker        r = client.run(args=[url, str(65535 - 5), str(65535 + 5)])
131*6236dae4SAndroid Build Coastguard Worker        r.check_exit_code(0)
132*6236dae4SAndroid Build Coastguard Worker
133*6236dae4SAndroid Build Coastguard Worker    # the python websocket server does not like 'large' control frames
134*6236dae4SAndroid Build Coastguard Worker    def test_20_07_data_large_small_recv(self, env: Env, ws_echo, repeat):
135*6236dae4SAndroid Build Coastguard Worker        client = LocalClient(env=env, name='ws-data', run_env={
136*6236dae4SAndroid Build Coastguard Worker            'CURL_WS_CHUNK_SIZE': '1024',
137*6236dae4SAndroid Build Coastguard Worker        })
138*6236dae4SAndroid Build Coastguard Worker        if not client.exists():
139*6236dae4SAndroid Build Coastguard Worker            pytest.skip(f'example client not built: {client.name}')
140*6236dae4SAndroid Build Coastguard Worker        url = f'ws://localhost:{env.ws_port}/'
141*6236dae4SAndroid Build Coastguard Worker        r = client.run(args=[url, str(65535 - 5), str(65535 + 5)])
142*6236dae4SAndroid Build Coastguard Worker        r.check_exit_code(0)
143