Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/http/test_08_caddy.py
2654 views
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
#***************************************************************************
4
# _ _ ____ _
5
# Project ___| | | | _ \| |
6
# / __| | | | |_) | |
7
# | (__| |_| | _ <| |___
8
# \___|\___/|_| \_\_____|
9
#
10
# Copyright (C) Daniel Stenberg, <[email protected]>, 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
#
27
import difflib
28
import filecmp
29
import logging
30
import os
31
import re
32
import pytest
33
34
from testenv import Env, CurlClient, Caddy, LocalClient
35
36
37
log = logging.getLogger(__name__)
38
39
40
@pytest.mark.skipif(condition=not Env.has_caddy(), reason="missing caddy")
41
@pytest.mark.skipif(condition=not Env.have_ssl_curl(), reason="curl without SSL")
42
class TestCaddy:
43
44
@pytest.fixture(autouse=True, scope='class')
45
def caddy(self, env):
46
caddy = Caddy(env=env)
47
assert caddy.initial_start()
48
yield caddy
49
caddy.stop()
50
51
def _make_docs_file(self, docs_dir: str, fname: str, fsize: int):
52
fpath = os.path.join(docs_dir, fname)
53
data1k = 1024*'x'
54
flen = 0
55
with open(fpath, 'w') as fd:
56
while flen < fsize:
57
fd.write(data1k)
58
flen += len(data1k)
59
return flen
60
61
@pytest.fixture(autouse=True, scope='class')
62
def _class_scope(self, env, caddy):
63
self._make_docs_file(docs_dir=caddy.docs_dir, fname='data10k.data', fsize=10*1024)
64
self._make_docs_file(docs_dir=caddy.docs_dir, fname='data1.data', fsize=1024*1024)
65
self._make_docs_file(docs_dir=caddy.docs_dir, fname='data5.data', fsize=5*1024*1024)
66
self._make_docs_file(docs_dir=caddy.docs_dir, fname='data10.data', fsize=10*1024*1024)
67
self._make_docs_file(docs_dir=caddy.docs_dir, fname='data100.data', fsize=100*1024*1024)
68
env.make_data_file(indir=env.gen_dir, fname="data-10m", fsize=10*1024*1024)
69
70
# download 1 file
71
@pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
72
def test_08_01_download_1(self, env: Env, caddy: Caddy, proto):
73
if proto == 'h3' and not env.have_h3_curl():
74
pytest.skip("h3 not supported in curl")
75
curl = CurlClient(env=env)
76
url = f'https://{env.domain1}:{caddy.port}/data.json'
77
r = curl.http_download(urls=[url], alpn_proto=proto)
78
r.check_response(count=1, http_status=200)
79
80
# download 1MB files sequentially
81
@pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
82
def test_08_02_download_1mb_sequential(self, env: Env, caddy: Caddy, proto):
83
if proto == 'h3' and not env.have_h3_curl():
84
pytest.skip("h3 not supported in curl")
85
count = 50
86
curl = CurlClient(env=env)
87
urln = f'https://{env.domain1}:{caddy.port}/data1.data?[0-{count-1}]'
88
r = curl.http_download(urls=[urln], alpn_proto=proto)
89
r.check_response(count=count, http_status=200, connect_count=1)
90
91
# download 1MB files parallel
92
@pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
93
def test_08_03_download_1mb_parallel(self, env: Env, caddy: Caddy, proto):
94
if proto == 'h3' and not env.have_h3_curl():
95
pytest.skip("h3 not supported in curl")
96
count = 20
97
curl = CurlClient(env=env)
98
urln = f'https://{env.domain1}:{caddy.port}/data1.data?[0-{count-1}]'
99
r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
100
'--parallel'
101
])
102
r.check_response(count=count, http_status=200)
103
if proto == 'http/1.1':
104
# http/1.1 parallel transfers will open multiple connections
105
assert r.total_connects > 1, r.dump_logs()
106
else:
107
assert r.total_connects == 1, r.dump_logs()
108
109
# download 5MB files sequentially
110
@pytest.mark.skipif(condition=Env().slow_network, reason="not suitable for slow network tests")
111
@pytest.mark.parametrize("proto", ['h2', 'h3'])
112
def test_08_04a_download_10mb_sequential(self, env: Env, caddy: Caddy, proto):
113
if proto == 'h3' and not env.have_h3_curl():
114
pytest.skip("h3 not supported in curl")
115
count = 40
116
curl = CurlClient(env=env)
117
urln = f'https://{env.domain1}:{caddy.port}/data5.data?[0-{count-1}]'
118
r = curl.http_download(urls=[urln], alpn_proto=proto)
119
r.check_response(count=count, http_status=200, connect_count=1)
120
121
# download 10MB files sequentially
122
@pytest.mark.skipif(condition=Env().slow_network, reason="not suitable for slow network tests")
123
@pytest.mark.parametrize("proto", ['h2', 'h3'])
124
def test_08_04b_download_10mb_sequential(self, env: Env, caddy: Caddy, proto):
125
if proto == 'h3' and not env.have_h3_curl():
126
pytest.skip("h3 not supported in curl")
127
count = 20
128
curl = CurlClient(env=env)
129
urln = f'https://{env.domain1}:{caddy.port}/data10.data?[0-{count-1}]'
130
r = curl.http_download(urls=[urln], alpn_proto=proto)
131
r.check_response(count=count, http_status=200, connect_count=1)
132
133
# download 10MB files parallel
134
@pytest.mark.skipif(condition=Env().slow_network, reason="not suitable for slow network tests")
135
@pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
136
def test_08_05_download_1mb_parallel(self, env: Env, caddy: Caddy, proto):
137
if proto == 'h3' and not env.have_h3_curl():
138
pytest.skip("h3 not supported in curl")
139
if proto == 'http/1.1' and env.curl_uses_lib('mbedtls'):
140
pytest.skip("mbedtls 3.6.0 fails on 50 connections with: "
141
"ssl_handshake returned: (-0x7F00) SSL - Memory allocation failed")
142
count = 50
143
curl = CurlClient(env=env)
144
urln = f'https://{env.domain1}:{caddy.port}/data10.data?[0-{count-1}]'
145
r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
146
'--parallel'
147
])
148
r.check_response(count=count, http_status=200)
149
if proto == 'http/1.1':
150
# http/1.1 parallel transfers will open multiple connections
151
assert r.total_connects > 1, r.dump_logs()
152
else:
153
assert r.total_connects == 1, r.dump_logs()
154
155
# post data parallel, check that they were echoed
156
@pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
157
def test_08_06_post_parallel(self, env: Env, httpd, caddy, proto):
158
if proto == 'h3' and not env.have_h3():
159
pytest.skip("h3 not supported")
160
# limit since we use a separate connection in h1
161
count = 20
162
data = '0123456789'
163
curl = CurlClient(env=env)
164
url = f'https://{env.domain2}:{caddy.port}/curltest/echo?id=[0-{count-1}]'
165
r = curl.http_upload(urls=[url], data=data, alpn_proto=proto,
166
extra_args=['--parallel'])
167
r.check_stats(count=count, http_status=200, exitcode=0)
168
for i in range(count):
169
respdata = open(curl.response_file(i)).readlines()
170
assert respdata == [data]
171
172
# put large file, check that they length were echoed
173
@pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
174
def test_08_07_put_large(self, env: Env, httpd, caddy, proto):
175
if proto == 'h3' and not env.have_h3():
176
pytest.skip("h3 not supported")
177
# limit since we use a separate connection in h1<
178
count = 1
179
fdata = os.path.join(env.gen_dir, 'data-10m')
180
curl = CurlClient(env=env)
181
url = f'https://{env.domain2}:{caddy.port}/curltest/put?id=[0-{count-1}]'
182
r = curl.http_put(urls=[url], fdata=fdata, alpn_proto=proto)
183
exp_data = [f'{os.path.getsize(fdata)}']
184
r.check_response(count=count, http_status=200)
185
for i in range(count):
186
respdata = open(curl.response_file(i)).readlines()
187
assert respdata == exp_data
188
189
@pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
190
def test_08_08_earlydata(self, env: Env, httpd, caddy, proto):
191
if not env.curl_can_early_data():
192
pytest.skip('TLS earlydata not implemented')
193
if proto == 'h3' and \
194
(not env.have_h3() or not env.curl_can_h3_early_data()):
195
pytest.skip("h3 not supported")
196
count = 2
197
docname = 'data10k.data'
198
url = f'https://{env.domain1}:{caddy.port}/{docname}'
199
client = LocalClient(name='cli_hx_download', env=env)
200
if not client.exists():
201
pytest.skip(f'example client not built: {client.name}')
202
r = client.run(args=[
203
'-n', f'{count}',
204
'-e', # use TLS earlydata
205
'-f', # forbid reuse of connections
206
'-r', f'{env.domain1}:{caddy.port}:127.0.0.1',
207
'-V', proto, url
208
])
209
r.check_exit_code(0)
210
srcfile = os.path.join(caddy.docs_dir, docname)
211
self.check_downloads(client, srcfile, count)
212
earlydata = {}
213
for line in r.trace_lines:
214
m = re.match(r'^\[t-(\d+)] EarlyData: (-?\d+)', line)
215
if m:
216
earlydata[int(m.group(1))] = int(m.group(2))
217
assert earlydata[0] == 0, f'{earlydata}'
218
if proto == 'h3':
219
assert earlydata[1] == 113, f'{earlydata}'
220
else:
221
# Caddy does not support early data on TCP
222
assert earlydata[1] == 0, f'{earlydata}'
223
224
def check_downloads(self, client, srcfile: str, count: int,
225
complete: bool = True):
226
for i in range(count):
227
dfile = client.download_file(i)
228
assert os.path.exists(dfile)
229
if complete and not filecmp.cmp(srcfile, dfile, shallow=False):
230
diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),
231
b=open(dfile).readlines(),
232
fromfile=srcfile,
233
tofile=dfile,
234
n=1))
235
assert False, f'download {dfile} differs:\n{diff}'
236
237