1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3#*************************************************************************** 4# _ _ ____ _ 5# Project ___| | | | _ \| | 6# / __| | | | |_) | | 7# | (__| |_| | _ <| |___ 8# \___|\___/|_| \_\_____| 9# 10# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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# 27import difflib 28import filecmp 29import logging 30import os 31import shutil 32import pytest 33 34from testenv import Env, CurlClient, VsFTPD 35 36 37log = logging.getLogger(__name__) 38 39 40@pytest.mark.skipif(condition=not Env.has_vsftpd(), reason=f"missing vsftpd") 41class TestVsFTPD: 42 43 @pytest.fixture(autouse=True, scope='class') 44 def vsftpd(self, env): 45 vsftpd = VsFTPD(env=env) 46 assert vsftpd.start() 47 yield vsftpd 48 vsftpd.stop() 49 50 def _make_docs_file(self, docs_dir: str, fname: str, fsize: int): 51 fpath = os.path.join(docs_dir, fname) 52 data1k = 1024*'x' 53 flen = 0 54 with open(fpath, 'w') as fd: 55 while flen < fsize: 56 fd.write(data1k) 57 flen += len(data1k) 58 return flen 59 60 @pytest.fixture(autouse=True, scope='class') 61 def _class_scope(self, env, vsftpd): 62 if os.path.exists(vsftpd.docs_dir): 63 shutil.rmtree(vsftpd.docs_dir) 64 if not os.path.exists(vsftpd.docs_dir): 65 os.makedirs(vsftpd.docs_dir) 66 self._make_docs_file(docs_dir=vsftpd.docs_dir, fname='data-1k', fsize=1024) 67 self._make_docs_file(docs_dir=vsftpd.docs_dir, fname='data-10k', fsize=10*1024) 68 self._make_docs_file(docs_dir=vsftpd.docs_dir, fname='data-1m', fsize=1024*1024) 69 self._make_docs_file(docs_dir=vsftpd.docs_dir, fname='data-10m', fsize=10*1024*1024) 70 71 def test_30_01_list_dir(self, env: Env, vsftpd: VsFTPD, repeat): 72 curl = CurlClient(env=env) 73 url = f'ftp://{env.ftp_domain}:{vsftpd.port}/' 74 r = curl.ftp_get(urls=[url], with_stats=True) 75 r.check_stats(count=1, http_status=226) 76 lines = open(os.path.join(curl.run_dir, 'download_#1.data')).readlines() 77 assert len(lines) == 4, f'list: {lines}' 78 79 # download 1 file, no SSL 80 @pytest.mark.parametrize("docname", [ 81 'data-1k', 'data-1m', 'data-10m' 82 ]) 83 def test_30_02_download_1(self, env: Env, vsftpd: VsFTPD, docname, repeat): 84 curl = CurlClient(env=env) 85 srcfile = os.path.join(vsftpd.docs_dir, f'{docname}') 86 count = 1 87 url = f'ftp://{env.ftp_domain}:{vsftpd.port}/{docname}?[0-{count-1}]' 88 r = curl.ftp_get(urls=[url], with_stats=True) 89 r.check_stats(count=count, http_status=226) 90 self.check_downloads(curl, srcfile, count) 91 92 @pytest.mark.parametrize("docname", [ 93 'data-1k', 'data-1m', 'data-10m' 94 ]) 95 def test_30_03_download_10_serial(self, env: Env, vsftpd: VsFTPD, docname, repeat): 96 curl = CurlClient(env=env) 97 srcfile = os.path.join(vsftpd.docs_dir, f'{docname}') 98 count = 10 99 url = f'ftp://{env.ftp_domain}:{vsftpd.port}/{docname}?[0-{count-1}]' 100 r = curl.ftp_get(urls=[url], with_stats=True) 101 r.check_stats(count=count, http_status=226) 102 self.check_downloads(curl, srcfile, count) 103 104 @pytest.mark.parametrize("docname", [ 105 'data-1k', 'data-1m', 'data-10m' 106 ]) 107 def test_30_04_download_10_parallel(self, env: Env, vsftpd: VsFTPD, docname, repeat): 108 curl = CurlClient(env=env) 109 srcfile = os.path.join(vsftpd.docs_dir, f'{docname}') 110 count = 10 111 url = f'ftp://{env.ftp_domain}:{vsftpd.port}/{docname}?[0-{count-1}]' 112 r = curl.ftp_get(urls=[url], with_stats=True, extra_args=[ 113 '--parallel' 114 ]) 115 r.check_stats(count=count, http_status=226) 116 self.check_downloads(curl, srcfile, count) 117 118 def check_downloads(self, client, srcfile: str, count: int, 119 complete: bool = True): 120 for i in range(count): 121 dfile = client.download_file(i) 122 assert os.path.exists(dfile) 123 if complete and not filecmp.cmp(srcfile, dfile, shallow=False): 124 diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(), 125 b=open(dfile).readlines(), 126 fromfile=srcfile, 127 tofile=dfile, 128 n=1)) 129 assert False, f'download {dfile} differs:\n{diff}' 130 131 132 133