Path: blob/main/external/curl/tests/http/test_31_vsftpds.py
2659 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 TestVsFTPD:4142SUPPORTS_SSL = True4344@pytest.fixture(autouse=True, scope='class')45def vsftpds(self, env):46if not TestVsFTPD.SUPPORTS_SSL:47pytest.skip('vsftpd does not seem to support SSL')48vsftpds = VsFTPD(env=env, with_ssl=True)49if not vsftpds.initial_start():50vsftpds.stop()51TestVsFTPD.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_31_01_list_dir(self, env: Env, vsftpds: VsFTPD):81curl = CurlClient(env=env)82url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'83r = curl.ftp_ssl_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_31_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'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'97r = curl.ftp_ssl_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_31_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'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'109r = curl.ftp_ssl_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@pytest.mark.parametrize("docname", [115'data-1k', 'data-1m', 'data-10m'116])117def test_31_04_download_10_parallel(self, env: Env, vsftpds: VsFTPD, docname):118curl = CurlClient(env=env)119srcfile = os.path.join(vsftpds.docs_dir, f'{docname}')120count = 10121url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'122r = curl.ftp_ssl_get(urls=[url], with_stats=True, extra_args=[123'--parallel'124])125r.check_stats(count=count, http_status=226)126self.check_downloads(curl, srcfile, count)127assert r.total_connects > count + 1, 'should have used several control conns'128129@pytest.mark.parametrize("docname", [130'upload-1k', 'upload-100k', 'upload-1m'131])132def test_31_05_upload_1(self, env: Env, vsftpds: VsFTPD, docname):133curl = CurlClient(env=env)134srcfile = os.path.join(env.gen_dir, docname)135dstfile = os.path.join(vsftpds.docs_dir, docname)136self._rmf(dstfile)137count = 1138url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'139r = curl.ftp_ssl_upload(urls=[url], fupload=f'{srcfile}', with_stats=True)140r.check_stats(count=count, http_status=226)141self.check_upload(env, vsftpds, docname=docname)142143def _rmf(self, path):144if os.path.exists(path):145return os.remove(path)146147# check with `tcpdump` if curl causes any TCP RST packets148@pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")149@pytest.mark.skipif(condition=not Env.curl_is_debug(), reason="needs curl debug")150@pytest.mark.skipif(condition=not Env.curl_is_verbose(), reason="needs curl verbose strings")151def test_31_06_shutdownh_download(self, env: Env, vsftpds: VsFTPD):152docname = 'data-1k'153curl = CurlClient(env=env)154count = 1155url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'156r = curl.ftp_ssl_get(urls=[url], with_stats=True, with_tcpdump=True)157r.check_stats(count=count, http_status=226)158# vsftp closes control connection without niceties,159# look only at ports from DATA connection.160data_ports = vsftpds.get_data_ports(r)161assert len(data_ports), f'unable to find FTP data port connected to\n{r.dump_logs()}'162assert len(r.tcpdump.get_rsts(ports=data_ports)) == 0, 'Unexpected TCP RST packets'163164# check with `tcpdump` if curl causes any TCP RST packets165@pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")166@pytest.mark.skipif(condition=not Env.curl_is_debug(), reason="needs curl debug")167@pytest.mark.skipif(condition=not Env.curl_is_verbose(), reason="needs curl verbose strings")168def test_31_07_shutdownh_upload(self, env: Env, vsftpds: VsFTPD):169docname = 'upload-1k'170curl = CurlClient(env=env)171srcfile = os.path.join(env.gen_dir, docname)172dstfile = os.path.join(vsftpds.docs_dir, docname)173self._rmf(dstfile)174count = 1175url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'176r = curl.ftp_ssl_upload(urls=[url], fupload=f'{srcfile}', with_stats=True, with_tcpdump=True)177r.check_stats(count=count, http_status=226)178# vsftp closes control connection without niceties,179# look only at ports from DATA connection.180data_ports = vsftpds.get_data_ports(r)181assert len(data_ports), f'unable to find FTP data port connected to\n{r.dump_logs()}'182assert len(r.tcpdump.get_rsts(ports=data_ports)) == 0, 'Unexpected TCP RST packets'183184def test_31_08_upload_ascii(self, env: Env, vsftpds: VsFTPD):185docname = 'upload-ascii'186line_length = 21187srcfile = os.path.join(env.gen_dir, docname)188dstfile = os.path.join(vsftpds.docs_dir, docname)189env.make_data_file(indir=env.gen_dir, fname=docname, fsize=100*1024,190line_length=line_length)191srcsize = os.path.getsize(srcfile)192self._rmf(dstfile)193count = 1194curl = CurlClient(env=env)195url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'196r = curl.ftp_ssl_upload(urls=[url], fupload=f'{srcfile}', with_stats=True,197extra_args=['--use-ascii'])198r.check_stats(count=count, http_status=226)199# expect the uploaded file to be number of converted newlines larger200dstsize = os.path.getsize(dstfile)201newlines = len(open(srcfile).readlines())202assert (srcsize + newlines) == dstsize, \203f'expected source with {newlines} lines to be that much larger,'\204f'instead srcsize={srcsize}, upload size={dstsize}, diff={dstsize-srcsize}'205206def test_31_08_active_download(self, env: Env, vsftpds: VsFTPD):207docname = 'data-10k'208curl = CurlClient(env=env)209srcfile = os.path.join(vsftpds.docs_dir, f'{docname}')210count = 1211url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'212r = curl.ftp_ssl_get(urls=[url], with_stats=True, extra_args=[213'--ftp-port', '127.0.0.1'214])215r.check_stats(count=count, http_status=226)216self.check_downloads(curl, srcfile, count)217218def test_31_09_active_upload(self, env: Env, vsftpds: VsFTPD):219docname = 'upload-1k'220curl = CurlClient(env=env)221srcfile = os.path.join(env.gen_dir, docname)222dstfile = os.path.join(vsftpds.docs_dir, docname)223self._rmf(dstfile)224count = 1225url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'226r = curl.ftp_ssl_upload(urls=[url], fupload=f'{srcfile}', with_stats=True, extra_args=[227'--ftp-port', '127.0.0.1'228])229r.check_stats(count=count, http_status=226)230self.check_upload(env, vsftpds, docname=docname)231232@pytest.mark.parametrize("indata", [233pytest.param('1234567890', id='10-bytes'),234pytest.param('', id='0-bytes'),235])236def test_31_10_upload_stdin(self, env: Env, vsftpds: VsFTPD, indata):237curl = CurlClient(env=env)238docname = "upload_31_10"239dstfile = os.path.join(vsftpds.docs_dir, docname)240self._rmf(dstfile)241count = 1242url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}'243r = curl.ftp_ssl_upload(urls=[url], updata=indata, with_stats=True)244r.check_stats(count=count, http_status=226)245assert os.path.exists(dstfile)246destdata = open(dstfile).readlines()247expdata = [indata] if len(indata) else []248assert expdata == destdata, f'expected: {expdata}, got: {destdata}'249250def test_31_11_download_non_existing(self, env: Env, vsftpds: VsFTPD):251curl = CurlClient(env=env)252url = f'ftp://{env.ftp_domain}:{vsftpds.port}/does-not-exist'253r = curl.ftp_ssl_get(urls=[url], with_stats=True)254r.check_exit_code(78)255r.check_stats(count=1, exitcode=78)256257def check_downloads(self, client, srcfile: str, count: int,258complete: bool = True):259for i in range(count):260dfile = client.download_file(i)261assert os.path.exists(dfile)262if complete and not filecmp.cmp(srcfile, dfile, shallow=False):263diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),264b=open(dfile).readlines(),265fromfile=srcfile,266tofile=dfile,267n=1))268assert False, f'download {dfile} differs:\n{diff}'269270def check_upload(self, env, vsftpd: VsFTPD, docname):271srcfile = os.path.join(env.gen_dir, docname)272dstfile = os.path.join(vsftpd.docs_dir, docname)273assert os.path.exists(srcfile)274assert os.path.exists(dstfile)275if not filecmp.cmp(srcfile, dstfile, shallow=False):276diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),277b=open(dstfile).readlines(),278fromfile=srcfile,279tofile=dstfile,280n=1))281assert False, f'upload {dstfile} differs:\n{diff}'282283284