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