Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/http/test_31_vsftpds.py
2659 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 shutil
32
import pytest
33
34
from testenv import Env, CurlClient, VsFTPD
35
36
37
log = logging.getLogger(__name__)
38
39
40
@pytest.mark.skipif(condition=not Env.has_vsftpd(), reason="missing vsftpd")
41
class TestVsFTPD:
42
43
SUPPORTS_SSL = True
44
45
@pytest.fixture(autouse=True, scope='class')
46
def vsftpds(self, env):
47
if not TestVsFTPD.SUPPORTS_SSL:
48
pytest.skip('vsftpd does not seem to support SSL')
49
vsftpds = VsFTPD(env=env, with_ssl=True)
50
if not vsftpds.initial_start():
51
vsftpds.stop()
52
TestVsFTPD.SUPPORTS_SSL = False
53
pytest.skip('vsftpd does not seem to support SSL')
54
yield vsftpds
55
vsftpds.stop()
56
57
def _make_docs_file(self, docs_dir: str, fname: str, fsize: int):
58
fpath = os.path.join(docs_dir, fname)
59
data1k = 1024*'x'
60
flen = 0
61
with open(fpath, 'w') as fd:
62
while flen < fsize:
63
fd.write(data1k)
64
flen += len(data1k)
65
return flen
66
67
@pytest.fixture(autouse=True, scope='class')
68
def _class_scope(self, env, vsftpds):
69
if os.path.exists(vsftpds.docs_dir):
70
shutil.rmtree(vsftpds.docs_dir)
71
if not os.path.exists(vsftpds.docs_dir):
72
os.makedirs(vsftpds.docs_dir)
73
self._make_docs_file(docs_dir=vsftpds.docs_dir, fname='data-1k', fsize=1024)
74
self._make_docs_file(docs_dir=vsftpds.docs_dir, fname='data-10k', fsize=10*1024)
75
self._make_docs_file(docs_dir=vsftpds.docs_dir, fname='data-1m', fsize=1024*1024)
76
self._make_docs_file(docs_dir=vsftpds.docs_dir, fname='data-10m', fsize=10*1024*1024)
77
env.make_data_file(indir=env.gen_dir, fname="upload-1k", fsize=1024)
78
env.make_data_file(indir=env.gen_dir, fname="upload-100k", fsize=100*1024)
79
env.make_data_file(indir=env.gen_dir, fname="upload-1m", fsize=1024*1024)
80
81
def test_31_01_list_dir(self, env: Env, vsftpds: VsFTPD):
82
curl = CurlClient(env=env)
83
url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'
84
r = curl.ftp_ssl_get(urls=[url], with_stats=True)
85
r.check_stats(count=1, http_status=226)
86
lines = open(os.path.join(curl.run_dir, 'download_#1.data')).readlines()
87
assert len(lines) == 4, f'list: {lines}'
88
89
# download 1 file, no SSL
90
@pytest.mark.parametrize("docname", [
91
'data-1k', 'data-1m', 'data-10m'
92
])
93
def test_31_02_download_1(self, env: Env, vsftpds: VsFTPD, docname):
94
curl = CurlClient(env=env)
95
srcfile = os.path.join(vsftpds.docs_dir, f'{docname}')
96
count = 1
97
url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'
98
r = curl.ftp_ssl_get(urls=[url], with_stats=True)
99
r.check_stats(count=count, http_status=226)
100
self.check_downloads(curl, srcfile, count)
101
102
@pytest.mark.parametrize("docname", [
103
'data-1k', 'data-1m', 'data-10m'
104
])
105
def test_31_03_download_10_serial(self, env: Env, vsftpds: VsFTPD, docname):
106
curl = CurlClient(env=env)
107
srcfile = os.path.join(vsftpds.docs_dir, f'{docname}')
108
count = 10
109
url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'
110
r = curl.ftp_ssl_get(urls=[url], with_stats=True)
111
r.check_stats(count=count, http_status=226)
112
self.check_downloads(curl, srcfile, count)
113
assert r.total_connects == count + 1, 'should reuse the control conn'
114
115
@pytest.mark.parametrize("docname", [
116
'data-1k', 'data-1m', 'data-10m'
117
])
118
def test_31_04_download_10_parallel(self, env: Env, vsftpds: VsFTPD, docname):
119
curl = CurlClient(env=env)
120
srcfile = os.path.join(vsftpds.docs_dir, f'{docname}')
121
count = 10
122
url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'
123
r = curl.ftp_ssl_get(urls=[url], with_stats=True, extra_args=[
124
'--parallel'
125
])
126
r.check_stats(count=count, http_status=226)
127
self.check_downloads(curl, srcfile, count)
128
assert r.total_connects > count + 1, 'should have used several control conns'
129
130
@pytest.mark.parametrize("docname", [
131
'upload-1k', 'upload-100k', 'upload-1m'
132
])
133
def test_31_05_upload_1(self, env: Env, vsftpds: VsFTPD, docname):
134
curl = CurlClient(env=env)
135
srcfile = os.path.join(env.gen_dir, docname)
136
dstfile = os.path.join(vsftpds.docs_dir, docname)
137
self._rmf(dstfile)
138
count = 1
139
url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'
140
r = curl.ftp_ssl_upload(urls=[url], fupload=f'{srcfile}', with_stats=True)
141
r.check_stats(count=count, http_status=226)
142
self.check_upload(env, vsftpds, docname=docname)
143
144
def _rmf(self, path):
145
if os.path.exists(path):
146
return os.remove(path)
147
148
# check with `tcpdump` if curl causes any TCP RST packets
149
@pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")
150
@pytest.mark.skipif(condition=not Env.curl_is_debug(), reason="needs curl debug")
151
@pytest.mark.skipif(condition=not Env.curl_is_verbose(), reason="needs curl verbose strings")
152
def test_31_06_shutdownh_download(self, env: Env, vsftpds: VsFTPD):
153
docname = 'data-1k'
154
curl = CurlClient(env=env)
155
count = 1
156
url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'
157
r = curl.ftp_ssl_get(urls=[url], with_stats=True, with_tcpdump=True)
158
r.check_stats(count=count, http_status=226)
159
# vsftp closes control connection without niceties,
160
# look only at ports from DATA connection.
161
data_ports = vsftpds.get_data_ports(r)
162
assert len(data_ports), f'unable to find FTP data port connected to\n{r.dump_logs()}'
163
assert len(r.tcpdump.get_rsts(ports=data_ports)) == 0, 'Unexpected TCP RST packets'
164
165
# check with `tcpdump` if curl causes any TCP RST packets
166
@pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")
167
@pytest.mark.skipif(condition=not Env.curl_is_debug(), reason="needs curl debug")
168
@pytest.mark.skipif(condition=not Env.curl_is_verbose(), reason="needs curl verbose strings")
169
def test_31_07_shutdownh_upload(self, env: Env, vsftpds: VsFTPD):
170
docname = 'upload-1k'
171
curl = CurlClient(env=env)
172
srcfile = os.path.join(env.gen_dir, docname)
173
dstfile = os.path.join(vsftpds.docs_dir, docname)
174
self._rmf(dstfile)
175
count = 1
176
url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'
177
r = curl.ftp_ssl_upload(urls=[url], fupload=f'{srcfile}', with_stats=True, with_tcpdump=True)
178
r.check_stats(count=count, http_status=226)
179
# vsftp closes control connection without niceties,
180
# look only at ports from DATA connection.
181
data_ports = vsftpds.get_data_ports(r)
182
assert len(data_ports), f'unable to find FTP data port connected to\n{r.dump_logs()}'
183
assert len(r.tcpdump.get_rsts(ports=data_ports)) == 0, 'Unexpected TCP RST packets'
184
185
def test_31_08_upload_ascii(self, env: Env, vsftpds: VsFTPD):
186
docname = 'upload-ascii'
187
line_length = 21
188
srcfile = os.path.join(env.gen_dir, docname)
189
dstfile = os.path.join(vsftpds.docs_dir, docname)
190
env.make_data_file(indir=env.gen_dir, fname=docname, fsize=100*1024,
191
line_length=line_length)
192
srcsize = os.path.getsize(srcfile)
193
self._rmf(dstfile)
194
count = 1
195
curl = CurlClient(env=env)
196
url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'
197
r = curl.ftp_ssl_upload(urls=[url], fupload=f'{srcfile}', with_stats=True,
198
extra_args=['--use-ascii'])
199
r.check_stats(count=count, http_status=226)
200
# expect the uploaded file to be number of converted newlines larger
201
dstsize = os.path.getsize(dstfile)
202
newlines = len(open(srcfile).readlines())
203
assert (srcsize + newlines) == dstsize, \
204
f'expected source with {newlines} lines to be that much larger,'\
205
f'instead srcsize={srcsize}, upload size={dstsize}, diff={dstsize-srcsize}'
206
207
def test_31_08_active_download(self, env: Env, vsftpds: VsFTPD):
208
docname = 'data-10k'
209
curl = CurlClient(env=env)
210
srcfile = os.path.join(vsftpds.docs_dir, f'{docname}')
211
count = 1
212
url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'
213
r = curl.ftp_ssl_get(urls=[url], with_stats=True, extra_args=[
214
'--ftp-port', '127.0.0.1'
215
])
216
r.check_stats(count=count, http_status=226)
217
self.check_downloads(curl, srcfile, count)
218
219
def test_31_09_active_upload(self, env: Env, vsftpds: VsFTPD):
220
docname = 'upload-1k'
221
curl = CurlClient(env=env)
222
srcfile = os.path.join(env.gen_dir, docname)
223
dstfile = os.path.join(vsftpds.docs_dir, docname)
224
self._rmf(dstfile)
225
count = 1
226
url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'
227
r = curl.ftp_ssl_upload(urls=[url], fupload=f'{srcfile}', with_stats=True, extra_args=[
228
'--ftp-port', '127.0.0.1'
229
])
230
r.check_stats(count=count, http_status=226)
231
self.check_upload(env, vsftpds, docname=docname)
232
233
@pytest.mark.parametrize("indata", [
234
pytest.param('1234567890', id='10-bytes'),
235
pytest.param('', id='0-bytes'),
236
])
237
def test_31_10_upload_stdin(self, env: Env, vsftpds: VsFTPD, indata):
238
curl = CurlClient(env=env)
239
docname = "upload_31_10"
240
dstfile = os.path.join(vsftpds.docs_dir, docname)
241
self._rmf(dstfile)
242
count = 1
243
url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}'
244
r = curl.ftp_ssl_upload(urls=[url], updata=indata, with_stats=True)
245
r.check_stats(count=count, http_status=226)
246
assert os.path.exists(dstfile)
247
destdata = open(dstfile).readlines()
248
expdata = [indata] if len(indata) else []
249
assert expdata == destdata, f'expected: {expdata}, got: {destdata}'
250
251
def test_31_11_download_non_existing(self, env: Env, vsftpds: VsFTPD):
252
curl = CurlClient(env=env)
253
url = f'ftp://{env.ftp_domain}:{vsftpds.port}/does-not-exist'
254
r = curl.ftp_ssl_get(urls=[url], with_stats=True)
255
r.check_exit_code(78)
256
r.check_stats(count=1, exitcode=78)
257
258
def check_downloads(self, client, srcfile: str, count: int,
259
complete: bool = True):
260
for i in range(count):
261
dfile = client.download_file(i)
262
assert os.path.exists(dfile)
263
if complete and not filecmp.cmp(srcfile, dfile, shallow=False):
264
diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),
265
b=open(dfile).readlines(),
266
fromfile=srcfile,
267
tofile=dfile,
268
n=1))
269
assert False, f'download {dfile} differs:\n{diff}'
270
271
def check_upload(self, env, vsftpd: VsFTPD, docname):
272
srcfile = os.path.join(env.gen_dir, docname)
273
dstfile = os.path.join(vsftpd.docs_dir, docname)
274
assert os.path.exists(srcfile)
275
assert os.path.exists(dstfile)
276
if not filecmp.cmp(srcfile, dstfile, shallow=False):
277
diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),
278
b=open(dstfile).readlines(),
279
fromfile=srcfile,
280
tofile=dstfile,
281
n=1))
282
assert False, f'upload {dstfile} differs:\n{diff}'
283
284