Path: blob/main/external/curl/tests/http/test_32_ftps_vsftpd.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 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")161def test_32_06_shutdownh_download(self, env: Env, vsftpds: VsFTPD):162docname = 'data-1k'163curl = CurlClient(env=env)164count = 1165url = f'ftps://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'166r = curl.ftp_get(urls=[url], with_stats=True, with_tcpdump=True)167r.check_stats(count=count, http_status=226)168# vsftp closes control connection without niceties,169# look only at ports from DATA connection.170data_ports = vsftpds.get_data_ports(r)171assert len(data_ports), f'unable to find FTP data port connected to\n{r.dump_logs()}'172assert len(r.tcpdump.get_rsts(ports=data_ports)) == 0, 'Unexpected TCP RST packets'173174# check with `tcpdump` if curl causes any TCP RST packets175@pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")176def test_32_07_shutdownh_upload(self, env: Env, vsftpds: VsFTPD):177docname = 'upload-1k'178curl = CurlClient(env=env)179srcfile = os.path.join(env.gen_dir, docname)180dstfile = os.path.join(vsftpds.docs_dir, docname)181self._rmf(dstfile)182count = 1183url = f'ftps://{env.ftp_domain}:{vsftpds.port}/'184r = curl.ftp_upload(urls=[url], fupload=f'{srcfile}', with_stats=True, with_tcpdump=True)185r.check_stats(count=count, http_status=226)186# vsftp closes control connection without niceties,187# look only at ports from DATA connection.188data_ports = vsftpds.get_data_ports(r)189assert len(data_ports), f'unable to find FTP data port connected to\n{r.dump_logs()}'190assert len(r.tcpdump.get_rsts(ports=data_ports)) == 0, 'Unexpected TCP RST packets'191192def test_32_08_upload_ascii(self, env: Env, vsftpds: VsFTPD):193docname = 'upload-ascii'194line_length = 21195srcfile = os.path.join(env.gen_dir, docname)196dstfile = os.path.join(vsftpds.docs_dir, docname)197env.make_data_file(indir=env.gen_dir, fname=docname, fsize=100*1024,198line_length=line_length)199srcsize = os.path.getsize(srcfile)200self._rmf(dstfile)201count = 1202curl = CurlClient(env=env)203url = f'ftps://{env.ftp_domain}:{vsftpds.port}/'204r = curl.ftp_upload(urls=[url], fupload=f'{srcfile}', with_stats=True,205extra_args=['--use-ascii'])206r.check_stats(count=count, http_status=226)207# expect the uploaded file to be number of converted newlines larger208dstsize = os.path.getsize(dstfile)209newlines = len(open(srcfile).readlines())210assert (srcsize + newlines) == dstsize, \211f'expected source with {newlines} lines to be that much larger,'\212f'instead srcsize={srcsize}, upload size={dstsize}, diff={dstsize-srcsize}'213214def test_32_08_active_download(self, env: Env, vsftpds: VsFTPD):215docname = 'data-10k'216curl = CurlClient(env=env)217srcfile = os.path.join(vsftpds.docs_dir, f'{docname}')218count = 1219url = f'ftps://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'220r = curl.ftp_get(urls=[url], with_stats=True, extra_args=[221'--ftp-port', '127.0.0.1'222])223r.check_stats(count=count, http_status=226)224self.check_downloads(curl, srcfile, count)225226def test_32_09_active_upload(self, env: Env, vsftpds: VsFTPD):227docname = 'upload-1k'228curl = CurlClient(env=env)229srcfile = os.path.join(env.gen_dir, docname)230dstfile = os.path.join(vsftpds.docs_dir, docname)231self._rmf(dstfile)232count = 1233url = f'ftps://{env.ftp_domain}:{vsftpds.port}/'234r = curl.ftp_upload(urls=[url], fupload=f'{srcfile}', with_stats=True, extra_args=[235'--ftp-port', '127.0.0.1'236])237r.check_stats(count=count, http_status=226)238self.check_upload(env, vsftpds, docname=docname)239240@pytest.mark.parametrize("indata", [241pytest.param('1234567890', id='10-bytes'),242pytest.param('', id='0-bytes'),243])244def test_32_10_upload_stdin(self, env: Env, vsftpds: VsFTPD, indata):245curl = CurlClient(env=env)246docname = "upload_31_10"247dstfile = os.path.join(vsftpds.docs_dir, docname)248self._rmf(dstfile)249count = 1250url = f'ftps://{env.ftp_domain}:{vsftpds.port}/{docname}'251r = curl.ftp_upload(urls=[url], updata=indata, with_stats=True)252r.check_stats(count=count, http_status=226)253assert os.path.exists(dstfile)254destdata = open(dstfile).readlines()255expdata = [indata] if len(indata) else []256assert expdata == destdata, f'exected: {expdata}, got: {destdata}'257258def check_downloads(self, client, srcfile: str, count: int,259complete: bool = True):260for i in range(count):261dfile = client.download_file(i)262assert os.path.exists(dfile)263if complete and not filecmp.cmp(srcfile, dfile, shallow=False):264diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),265b=open(dfile).readlines(),266fromfile=srcfile,267tofile=dfile,268n=1))269assert False, f'download {dfile} differs:\n{diff}'270271def check_upload(self, env, vsftpd: VsFTPD, docname):272srcfile = os.path.join(env.gen_dir, docname)273dstfile = os.path.join(vsftpd.docs_dir, docname)274assert os.path.exists(srcfile)275assert os.path.exists(dstfile)276if not filecmp.cmp(srcfile, dstfile, shallow=False):277diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),278b=open(dstfile).readlines(),279fromfile=srcfile,280tofile=dstfile,281n=1))282assert False, f'upload {dstfile} differs:\n{diff}'283284285