Path: blob/main/external/curl/tests/http/test_32_ftps_vsftpd.py
2654 views
#!/usr/bin/env python31# -*- coding: utf-8 -*-2#***************************************************************************3# _ _ ____ _4# Project ___| | | | _ \| |5# / __| | | | |_) | |6# | (__| |_| | _ <| |___7# \___|\___/|_| \_\_____|8#9# Copyright (C) Daniel Stenberg, <[email protected]>, et al.10#11# This software is licensed as described in the file COPYING, which12# you should have received as part of this distribution. The terms13# are also available at https://curl.se/docs/copyright.html.14#15# You may opt to use, copy, modify, merge, publish, distribute and/or sell16# copies of the Software, and permit persons to whom the Software is17# furnished to do so, under the terms of the COPYING file.18#19# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY20# KIND, either express or implied.21#22# SPDX-License-Identifier: curl23#24###########################################################################25#26import difflib27import filecmp28import logging29import os30import shutil31import pytest3233from testenv import Env, CurlClient, VsFTPD343536log = logging.getLogger(__name__)373839@pytest.mark.skipif(condition=not Env.has_vsftpd(), reason="missing vsftpd")40class TestFtpsVsFTPD:4142SUPPORTS_SSL = True4344@pytest.fixture(autouse=True, scope='class')45def vsftpds(self, env):46if not TestFtpsVsFTPD.SUPPORTS_SSL:47pytest.skip('vsftpd does not seem to support SSL')48vsftpds = VsFTPD(env=env, with_ssl=True, ssl_implicit=True)49if not vsftpds.initial_start():50vsftpds.stop()51TestFtpsVsFTPD.SUPPORTS_SSL = False52pytest.skip('vsftpd does not seem to support SSL')53yield vsftpds54vsftpds.stop()5556def _make_docs_file(self, docs_dir: str, fname: str, fsize: int):57fpath = os.path.join(docs_dir, fname)58data1k = 1024*'x'59flen = 060with open(fpath, 'w') as fd:61while flen < fsize:62fd.write(data1k)63flen += len(data1k)64return flen6566@pytest.fixture(autouse=True, scope='class')67def _class_scope(self, env, vsftpds):68if os.path.exists(vsftpds.docs_dir):69shutil.rmtree(vsftpds.docs_dir)70if not os.path.exists(vsftpds.docs_dir):71os.makedirs(vsftpds.docs_dir)72self._make_docs_file(docs_dir=vsftpds.docs_dir, fname='data-1k', fsize=1024)73self._make_docs_file(docs_dir=vsftpds.docs_dir, fname='data-10k', fsize=10*1024)74self._make_docs_file(docs_dir=vsftpds.docs_dir, fname='data-1m', fsize=1024*1024)75self._make_docs_file(docs_dir=vsftpds.docs_dir, fname='data-10m', fsize=10*1024*1024)76env.make_data_file(indir=env.gen_dir, fname="upload-1k", fsize=1024)77env.make_data_file(indir=env.gen_dir, fname="upload-100k", fsize=100*1024)78env.make_data_file(indir=env.gen_dir, fname="upload-1m", fsize=1024*1024)7980def test_32_01_list_dir(self, env: Env, vsftpds: VsFTPD):81curl = CurlClient(env=env)82url = f'ftps://{env.ftp_domain}:{vsftpds.port}/'83r = curl.ftp_get(urls=[url], with_stats=True)84r.check_stats(count=1, http_status=226)85lines = open(os.path.join(curl.run_dir, 'download_#1.data')).readlines()86assert len(lines) == 4, f'list: {lines}'8788# download 1 file, no SSL89@pytest.mark.parametrize("docname", [90'data-1k', 'data-1m', 'data-10m'91])92def test_32_02_download_1(self, env: Env, vsftpds: VsFTPD, docname):93curl = CurlClient(env=env)94srcfile = os.path.join(vsftpds.docs_dir, f'{docname}')95count = 196url = f'ftps://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'97r = curl.ftp_get(urls=[url], with_stats=True)98r.check_stats(count=count, http_status=226)99self.check_downloads(curl, srcfile, count)100101@pytest.mark.parametrize("docname", [102'data-1k', 'data-1m', 'data-10m'103])104def test_32_03_download_10_serial(self, env: Env, vsftpds: VsFTPD, docname):105curl = CurlClient(env=env)106srcfile = os.path.join(vsftpds.docs_dir, f'{docname}')107count = 10108url = f'ftps://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'109r = curl.ftp_get(urls=[url], with_stats=True)110r.check_stats(count=count, http_status=226)111self.check_downloads(curl, srcfile, count)112assert r.total_connects == count + 1, 'should reuse the control conn'113114# 2 serial transfers, first with 'ftps://' and second with 'ftp://'115# we want connection reuse in this case116def test_32_03b_ftp_compat_ftps(self, env: Env, vsftpds: VsFTPD):117curl = CurlClient(env=env)118docname = 'data-1k'119count = 2120url1= f'ftps://{env.ftp_domain}:{vsftpds.port}/{docname}'121url2 = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}'122r = curl.ftp_get(urls=[url1, url2], with_stats=True)123r.check_stats(count=count, http_status=226)124assert r.total_connects == count + 1, 'should reuse the control conn'125126@pytest.mark.parametrize("docname", [127'data-1k', 'data-1m', 'data-10m'128])129def test_32_04_download_10_parallel(self, env: Env, vsftpds: VsFTPD, docname):130curl = CurlClient(env=env)131srcfile = os.path.join(vsftpds.docs_dir, f'{docname}')132count = 10133url = f'ftps://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'134r = curl.ftp_get(urls=[url], with_stats=True, extra_args=[135'--parallel'136])137r.check_stats(count=count, http_status=226)138self.check_downloads(curl, srcfile, count)139assert r.total_connects > count + 1, 'should have used several control conns'140141@pytest.mark.parametrize("docname", [142'upload-1k', 'upload-100k', 'upload-1m'143])144def test_32_05_upload_1(self, env: Env, vsftpds: VsFTPD, docname):145curl = CurlClient(env=env)146srcfile = os.path.join(env.gen_dir, docname)147dstfile = os.path.join(vsftpds.docs_dir, docname)148self._rmf(dstfile)149count = 1150url = f'ftps://{env.ftp_domain}:{vsftpds.port}/'151r = curl.ftp_upload(urls=[url], fupload=f'{srcfile}', with_stats=True)152r.check_stats(count=count, http_status=226)153self.check_upload(env, vsftpds, docname=docname)154155def _rmf(self, path):156if os.path.exists(path):157return os.remove(path)158159# check with `tcpdump` if curl causes any TCP RST packets160@pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")161@pytest.mark.skipif(condition=not Env.curl_is_debug(), reason="needs curl debug")162@pytest.mark.skipif(condition=not Env.curl_is_verbose(), reason="needs curl verbose strings")163def test_32_06_shutdownh_download(self, env: Env, vsftpds: VsFTPD):164docname = 'data-1k'165curl = CurlClient(env=env)166count = 1167url = f'ftps://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'168r = curl.ftp_get(urls=[url], with_stats=True, with_tcpdump=True)169r.check_stats(count=count, http_status=226)170# vsftp closes control connection without niceties,171# look only at ports from DATA connection.172data_ports = vsftpds.get_data_ports(r)173assert len(data_ports), f'unable to find FTP data port connected to\n{r.dump_logs()}'174assert len(r.tcpdump.get_rsts(ports=data_ports)) == 0, 'Unexpected TCP RST packets'175176# check with `tcpdump` if curl causes any TCP RST packets177@pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")178@pytest.mark.skipif(condition=not Env.curl_is_debug(), reason="needs curl debug")179@pytest.mark.skipif(condition=not Env.curl_is_verbose(), reason="needs curl verbose strings")180def test_32_07_shutdownh_upload(self, env: Env, vsftpds: VsFTPD):181docname = 'upload-1k'182curl = CurlClient(env=env)183srcfile = os.path.join(env.gen_dir, docname)184dstfile = os.path.join(vsftpds.docs_dir, docname)185self._rmf(dstfile)186count = 1187url = f'ftps://{env.ftp_domain}:{vsftpds.port}/'188r = curl.ftp_upload(urls=[url], fupload=f'{srcfile}', with_stats=True, with_tcpdump=True)189r.check_stats(count=count, http_status=226)190# vsftp closes control connection without niceties,191# look only at ports from DATA connection.192data_ports = vsftpds.get_data_ports(r)193assert len(data_ports), f'unable to find FTP data port connected to\n{r.dump_logs()}'194assert len(r.tcpdump.get_rsts(ports=data_ports)) == 0, 'Unexpected TCP RST packets'195196def test_32_08_upload_ascii(self, env: Env, vsftpds: VsFTPD):197docname = 'upload-ascii'198line_length = 21199srcfile = os.path.join(env.gen_dir, docname)200dstfile = os.path.join(vsftpds.docs_dir, docname)201env.make_data_file(indir=env.gen_dir, fname=docname, fsize=100*1024,202line_length=line_length)203srcsize = os.path.getsize(srcfile)204self._rmf(dstfile)205count = 1206curl = CurlClient(env=env)207url = f'ftps://{env.ftp_domain}:{vsftpds.port}/'208r = curl.ftp_upload(urls=[url], fupload=f'{srcfile}', with_stats=True,209extra_args=['--use-ascii'])210r.check_stats(count=count, http_status=226)211# expect the uploaded file to be number of converted newlines larger212dstsize = os.path.getsize(dstfile)213newlines = len(open(srcfile).readlines())214assert (srcsize + newlines) == dstsize, \215f'expected source with {newlines} lines to be that much larger,'\216f'instead srcsize={srcsize}, upload size={dstsize}, diff={dstsize-srcsize}'217218def test_32_08_active_download(self, env: Env, vsftpds: VsFTPD):219docname = 'data-10k'220curl = CurlClient(env=env)221srcfile = os.path.join(vsftpds.docs_dir, f'{docname}')222count = 1223url = f'ftps://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'224r = curl.ftp_get(urls=[url], with_stats=True, extra_args=[225'--ftp-port', '127.0.0.1'226])227r.check_stats(count=count, http_status=226)228self.check_downloads(curl, srcfile, count)229230def test_32_09_active_upload(self, env: Env, vsftpds: VsFTPD):231docname = 'upload-1k'232curl = CurlClient(env=env)233srcfile = os.path.join(env.gen_dir, docname)234dstfile = os.path.join(vsftpds.docs_dir, docname)235self._rmf(dstfile)236count = 1237url = f'ftps://{env.ftp_domain}:{vsftpds.port}/'238r = curl.ftp_upload(urls=[url], fupload=f'{srcfile}', with_stats=True, extra_args=[239'--ftp-port', '127.0.0.1'240])241r.check_stats(count=count, http_status=226)242self.check_upload(env, vsftpds, docname=docname)243244@pytest.mark.parametrize("indata", [245pytest.param('1234567890', id='10-bytes'),246pytest.param('', id='0-bytes'),247])248def test_32_10_upload_stdin(self, env: Env, vsftpds: VsFTPD, indata):249curl = CurlClient(env=env)250docname = "upload_31_10"251dstfile = os.path.join(vsftpds.docs_dir, docname)252self._rmf(dstfile)253count = 1254url = f'ftps://{env.ftp_domain}:{vsftpds.port}/{docname}'255r = curl.ftp_upload(urls=[url], updata=indata, with_stats=True)256r.check_stats(count=count, http_status=226)257assert os.path.exists(dstfile)258destdata = open(dstfile).readlines()259expdata = [indata] if len(indata) else []260assert expdata == destdata, f'expected: {expdata}, got: {destdata}'261262def test_32_11_download_non_existing(self, env: Env, vsftpds: VsFTPD):263curl = CurlClient(env=env)264url = f'ftps://{env.ftp_domain}:{vsftpds.port}/does-not-exist'265r = curl.ftp_get(urls=[url], with_stats=True)266r.check_exit_code(78)267r.check_stats(count=1, exitcode=78)268269def check_downloads(self, client, srcfile: str, count: int,270complete: bool = True):271for i in range(count):272dfile = client.download_file(i)273assert os.path.exists(dfile)274if complete and not filecmp.cmp(srcfile, dfile, shallow=False):275diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),276b=open(dfile).readlines(),277fromfile=srcfile,278tofile=dfile,279n=1))280assert False, f'download {dfile} differs:\n{diff}'281282def check_upload(self, env, vsftpd: VsFTPD, docname):283srcfile = os.path.join(env.gen_dir, docname)284dstfile = os.path.join(vsftpd.docs_dir, docname)285assert os.path.exists(srcfile)286assert os.path.exists(dstfile)287if not filecmp.cmp(srcfile, dstfile, shallow=False):288diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),289b=open(dstfile).readlines(),290fromfile=srcfile,291tofile=dstfile,292n=1))293assert False, f'upload {dstfile} differs:\n{diff}'294295296