Path: blob/main/external/curl/tests/http/test_31_vsftpds.py
2066 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")149def test_31_06_shutdownh_download(self, env: Env, vsftpds: VsFTPD):150docname = 'data-1k'151curl = CurlClient(env=env)152count = 1153url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'154r = curl.ftp_ssl_get(urls=[url], with_stats=True, with_tcpdump=True)155r.check_stats(count=count, http_status=226)156# vsftp closes control connection without niceties,157# look only at ports from DATA connection.158data_ports = vsftpds.get_data_ports(r)159assert len(data_ports), f'unable to find FTP data port connected to\n{r.dump_logs()}'160assert len(r.tcpdump.get_rsts(ports=data_ports)) == 0, 'Unexpected TCP RST packets'161162# check with `tcpdump` if curl causes any TCP RST packets163@pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")164def test_31_07_shutdownh_upload(self, env: Env, vsftpds: VsFTPD):165docname = 'upload-1k'166curl = CurlClient(env=env)167srcfile = os.path.join(env.gen_dir, docname)168dstfile = os.path.join(vsftpds.docs_dir, docname)169self._rmf(dstfile)170count = 1171url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'172r = curl.ftp_ssl_upload(urls=[url], fupload=f'{srcfile}', with_stats=True, with_tcpdump=True)173r.check_stats(count=count, http_status=226)174# vsftp closes control connection without niceties,175# look only at ports from DATA connection.176data_ports = vsftpds.get_data_ports(r)177assert len(data_ports), f'unable to find FTP data port connected to\n{r.dump_logs()}'178assert len(r.tcpdump.get_rsts(ports=data_ports)) == 0, 'Unexpected TCP RST packets'179180def test_31_08_upload_ascii(self, env: Env, vsftpds: VsFTPD):181docname = 'upload-ascii'182line_length = 21183srcfile = os.path.join(env.gen_dir, docname)184dstfile = os.path.join(vsftpds.docs_dir, docname)185env.make_data_file(indir=env.gen_dir, fname=docname, fsize=100*1024,186line_length=line_length)187srcsize = os.path.getsize(srcfile)188self._rmf(dstfile)189count = 1190curl = CurlClient(env=env)191url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'192r = curl.ftp_ssl_upload(urls=[url], fupload=f'{srcfile}', with_stats=True,193extra_args=['--use-ascii'])194r.check_stats(count=count, http_status=226)195# expect the uploaded file to be number of converted newlines larger196dstsize = os.path.getsize(dstfile)197newlines = len(open(srcfile).readlines())198assert (srcsize + newlines) == dstsize, \199f'expected source with {newlines} lines to be that much larger,'\200f'instead srcsize={srcsize}, upload size={dstsize}, diff={dstsize-srcsize}'201202def test_31_08_active_download(self, env: Env, vsftpds: VsFTPD):203docname = 'data-10k'204curl = CurlClient(env=env)205srcfile = os.path.join(vsftpds.docs_dir, f'{docname}')206count = 1207url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'208r = curl.ftp_ssl_get(urls=[url], with_stats=True, extra_args=[209'--ftp-port', '127.0.0.1'210])211r.check_stats(count=count, http_status=226)212self.check_downloads(curl, srcfile, count)213214def test_31_09_active_upload(self, env: Env, vsftpds: VsFTPD):215docname = 'upload-1k'216curl = CurlClient(env=env)217srcfile = os.path.join(env.gen_dir, docname)218dstfile = os.path.join(vsftpds.docs_dir, docname)219self._rmf(dstfile)220count = 1221url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'222r = curl.ftp_ssl_upload(urls=[url], fupload=f'{srcfile}', with_stats=True, extra_args=[223'--ftp-port', '127.0.0.1'224])225r.check_stats(count=count, http_status=226)226self.check_upload(env, vsftpds, docname=docname)227228@pytest.mark.parametrize("indata", [229pytest.param('1234567890', id='10-bytes'),230pytest.param('', id='0-bytes'),231])232def test_31_10_upload_stdin(self, env: Env, vsftpds: VsFTPD, indata):233curl = CurlClient(env=env)234docname = "upload_31_10"235dstfile = os.path.join(vsftpds.docs_dir, docname)236self._rmf(dstfile)237count = 1238url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}'239r = curl.ftp_ssl_upload(urls=[url], updata=indata, with_stats=True)240r.check_stats(count=count, http_status=226)241assert os.path.exists(dstfile)242destdata = open(dstfile).readlines()243expdata = [indata] if len(indata) else []244assert expdata == destdata, f'exected: {expdata}, got: {destdata}'245246def check_downloads(self, client, srcfile: str, count: int,247complete: bool = True):248for i in range(count):249dfile = client.download_file(i)250assert os.path.exists(dfile)251if complete and not filecmp.cmp(srcfile, dfile, shallow=False):252diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),253b=open(dfile).readlines(),254fromfile=srcfile,255tofile=dfile,256n=1))257assert False, f'download {dfile} differs:\n{diff}'258259def check_upload(self, env, vsftpd: VsFTPD, docname):260srcfile = os.path.join(env.gen_dir, docname)261dstfile = os.path.join(vsftpd.docs_dir, docname)262assert os.path.exists(srcfile)263assert os.path.exists(dstfile)264if not filecmp.cmp(srcfile, dstfile, shallow=False):265diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),266b=open(dstfile).readlines(),267fromfile=srcfile,268tofile=dstfile,269n=1))270assert False, f'upload {dstfile} differs:\n{diff}'271272273