xref: /curl/tests/http/test_31_vsftpds.py (revision 0f7ba5c5)
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="missing vsftpd")
41class TestVsFTPD:
42
43    SUPPORTS_SSL = True
44
45    @pytest.fixture(autouse=True, scope='class')
46    def vsftpds(self, env):
47        if not TestVsFTPD.SUPPORTS_SSL:
48            pytest.skip('vsftpd does not seem to support SSL')
49        vsftpds = VsFTPD(env=env, with_ssl=True)
50        if not vsftpds.start():
51            vsftpds.stop()
52            TestVsFTPD.SUPPORTS_SSL = False
53            pytest.skip('vsftpd does not seem to support SSL')
54        yield vsftpds
55        vsftpds.stop()
56
57    def _make_docs_file(self, docs_dir: str, fname: str, fsize: int):
58        fpath = os.path.join(docs_dir, fname)
59        data1k = 1024*'x'
60        flen = 0
61        with open(fpath, 'w') as fd:
62            while flen < fsize:
63                fd.write(data1k)
64                flen += len(data1k)
65        return flen
66
67    @pytest.fixture(autouse=True, scope='class')
68    def _class_scope(self, env, vsftpds):
69        if os.path.exists(vsftpds.docs_dir):
70            shutil.rmtree(vsftpds.docs_dir)
71        if not os.path.exists(vsftpds.docs_dir):
72            os.makedirs(vsftpds.docs_dir)
73        self._make_docs_file(docs_dir=vsftpds.docs_dir, fname='data-1k', fsize=1024)
74        self._make_docs_file(docs_dir=vsftpds.docs_dir, fname='data-10k', fsize=10*1024)
75        self._make_docs_file(docs_dir=vsftpds.docs_dir, fname='data-1m', fsize=1024*1024)
76        self._make_docs_file(docs_dir=vsftpds.docs_dir, fname='data-10m', fsize=10*1024*1024)
77        env.make_data_file(indir=env.gen_dir, fname="upload-1k", fsize=1024)
78        env.make_data_file(indir=env.gen_dir, fname="upload-100k", fsize=100*1024)
79        env.make_data_file(indir=env.gen_dir, fname="upload-1m", fsize=1024*1024)
80
81    def test_31_01_list_dir(self, env: Env, vsftpds: VsFTPD, repeat):
82        curl = CurlClient(env=env)
83        url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'
84        r = curl.ftp_ssl_get(urls=[url], with_stats=True)
85        r.check_stats(count=1, http_status=226)
86        lines = open(os.path.join(curl.run_dir, 'download_#1.data')).readlines()
87        assert len(lines) == 4, f'list: {lines}'
88
89    # download 1 file, no SSL
90    @pytest.mark.parametrize("docname", [
91        'data-1k', 'data-1m', 'data-10m'
92    ])
93    def test_31_02_download_1(self, env: Env, vsftpds: VsFTPD, docname, repeat):
94        curl = CurlClient(env=env)
95        srcfile = os.path.join(vsftpds.docs_dir, f'{docname}')
96        count = 1
97        url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'
98        r = curl.ftp_ssl_get(urls=[url], with_stats=True)
99        r.check_stats(count=count, http_status=226)
100        self.check_downloads(curl, srcfile, count)
101
102    @pytest.mark.parametrize("docname", [
103        'data-1k', 'data-1m', 'data-10m'
104    ])
105    def test_31_03_download_10_serial(self, env: Env, vsftpds: VsFTPD, docname, repeat):
106        curl = CurlClient(env=env)
107        srcfile = os.path.join(vsftpds.docs_dir, f'{docname}')
108        count = 10
109        url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'
110        r = curl.ftp_ssl_get(urls=[url], with_stats=True)
111        r.check_stats(count=count, http_status=226)
112        self.check_downloads(curl, srcfile, count)
113
114    @pytest.mark.parametrize("docname", [
115        'data-1k', 'data-1m', 'data-10m'
116    ])
117    def test_31_04_download_10_parallel(self, env: Env, vsftpds: VsFTPD, docname, repeat):
118        curl = CurlClient(env=env)
119        srcfile = os.path.join(vsftpds.docs_dir, f'{docname}')
120        count = 10
121        url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'
122        r = curl.ftp_ssl_get(urls=[url], with_stats=True, extra_args=[
123            '--parallel'
124        ])
125        r.check_stats(count=count, http_status=226)
126        self.check_downloads(curl, srcfile, count)
127
128    @pytest.mark.parametrize("docname", [
129        'upload-1k', 'upload-100k', 'upload-1m'
130    ])
131    def test_31_05_upload_1(self, env: Env, vsftpds: VsFTPD, docname, repeat):
132        curl = CurlClient(env=env)
133        srcfile = os.path.join(env.gen_dir, docname)
134        dstfile = os.path.join(vsftpds.docs_dir, docname)
135        self._rmf(dstfile)
136        count = 1
137        url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'
138        r = curl.ftp_ssl_upload(urls=[url], fupload=f'{srcfile}', with_stats=True)
139        r.check_stats(count=count, http_status=226)
140        self.check_upload(env, vsftpds, docname=docname)
141
142    def _rmf(self, path):
143        if os.path.exists(path):
144            return os.remove(path)
145
146    # check with `tcpdump` if curl causes any TCP RST packets
147    @pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")
148    def test_31_06_shutdownh_download(self, env: Env, vsftpds: VsFTPD, repeat):
149        docname = 'data-1k'
150        curl = CurlClient(env=env)
151        count = 1
152        url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'
153        r = curl.ftp_ssl_get(urls=[url], with_stats=True, with_tcpdump=True)
154        r.check_stats(count=count, http_status=226)
155        # vsftp closes control connection without niceties,
156        # disregard RST packets it sent from its port to curl
157        assert len(r.tcpdump.stats_excluding(src_port=env.ftps_port)) == 0, 'Unexpected TCP RSTs packets'
158
159    # check with `tcpdump` if curl causes any TCP RST packets
160    @pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")
161    def test_31_07_shutdownh_upload(self, env: Env, vsftpds: VsFTPD, repeat):
162        docname = 'upload-1k'
163        curl = CurlClient(env=env)
164        srcfile = os.path.join(env.gen_dir, docname)
165        dstfile = os.path.join(vsftpds.docs_dir, docname)
166        self._rmf(dstfile)
167        count = 1
168        url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'
169        r = curl.ftp_ssl_upload(urls=[url], fupload=f'{srcfile}', with_stats=True, with_tcpdump=True)
170        r.check_stats(count=count, http_status=226)
171        # vsftp closes control connection without niceties,
172        # disregard RST packets it sent from its port to curl
173        assert len(r.tcpdump.stats_excluding(src_port=env.ftps_port)) == 0, 'Unexpected TCP RSTs packets'
174
175    def test_31_08_upload_ascii(self, env: Env, vsftpds: VsFTPD):
176        docname = 'upload-ascii'
177        line_length = 21
178        srcfile = os.path.join(env.gen_dir, docname)
179        dstfile = os.path.join(vsftpds.docs_dir, docname)
180        env.make_data_file(indir=env.gen_dir, fname=docname, fsize=100*1024,
181                           line_length=line_length)
182        srcsize = os.path.getsize(srcfile)
183        self._rmf(dstfile)
184        count = 1
185        curl = CurlClient(env=env)
186        url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'
187        r = curl.ftp_ssl_upload(urls=[url], fupload=f'{srcfile}', with_stats=True,
188                                extra_args=['--use-ascii'])
189        r.check_stats(count=count, http_status=226)
190        # expect the uploaded file to be number of converted newlines larger
191        dstsize = os.path.getsize(dstfile)
192        newlines = len(open(srcfile).readlines())
193        assert (srcsize + newlines) == dstsize, \
194            f'expected source with {newlines} lines to be that much larger,'\
195            f'instead srcsize={srcsize}, upload size={dstsize}, diff={dstsize-srcsize}'
196
197    def test_31_08_active_download(self, env: Env, vsftpds: VsFTPD):
198        docname = 'data-10k'
199        curl = CurlClient(env=env)
200        srcfile = os.path.join(vsftpds.docs_dir, f'{docname}')
201        count = 1
202        url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}?[0-{count-1}]'
203        r = curl.ftp_ssl_get(urls=[url], with_stats=True, extra_args=[
204            '--ftp-port', '127.0.0.1'
205        ])
206        r.check_stats(count=count, http_status=226)
207        self.check_downloads(curl, srcfile, count)
208
209    def test_31_09_active_upload(self, env: Env, vsftpds: VsFTPD):
210        docname = 'upload-1k'
211        curl = CurlClient(env=env)
212        srcfile = os.path.join(env.gen_dir, docname)
213        dstfile = os.path.join(vsftpds.docs_dir, docname)
214        self._rmf(dstfile)
215        count = 1
216        url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'
217        r = curl.ftp_ssl_upload(urls=[url], fupload=f'{srcfile}', with_stats=True, extra_args=[
218            '--ftp-port', '127.0.0.1'
219        ])
220        r.check_stats(count=count, http_status=226)
221        self.check_upload(env, vsftpds, docname=docname)
222
223    @pytest.mark.parametrize("indata", [
224        '1234567890', ''
225    ])
226    def test_31_10_upload_stdin(self, env: Env, vsftpds: VsFTPD, indata):
227        curl = CurlClient(env=env)
228        docname = "upload_31_10"
229        dstfile = os.path.join(vsftpds.docs_dir, docname)
230        self._rmf(dstfile)
231        count = 1
232        url = f'ftp://{env.ftp_domain}:{vsftpds.port}/{docname}'
233        r = curl.ftp_ssl_upload(urls=[url], updata=indata, with_stats=True)
234        r.check_stats(count=count, http_status=226)
235        assert os.path.exists(dstfile)
236        destdata = open(dstfile).readlines()
237        expdata = [indata] if len(indata) else []
238        assert expdata == destdata, f'exected: {expdata}, got: {destdata}'
239
240    def check_downloads(self, client, srcfile: str, count: int,
241                        complete: bool = True):
242        for i in range(count):
243            dfile = client.download_file(i)
244            assert os.path.exists(dfile)
245            if complete and not filecmp.cmp(srcfile, dfile, shallow=False):
246                diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),
247                                                    b=open(dfile).readlines(),
248                                                    fromfile=srcfile,
249                                                    tofile=dfile,
250                                                    n=1))
251                assert False, f'download {dfile} differs:\n{diff}'
252
253    def check_upload(self, env, vsftpd: VsFTPD, docname):
254        srcfile = os.path.join(env.gen_dir, docname)
255        dstfile = os.path.join(vsftpd.docs_dir, docname)
256        assert os.path.exists(srcfile)
257        assert os.path.exists(dstfile)
258        if not filecmp.cmp(srcfile, dstfile, shallow=False):
259            diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),
260                                                b=open(dstfile).readlines(),
261                                                fromfile=srcfile,
262                                                tofile=dstfile,
263                                                n=1))
264            assert False, f'upload {dstfile} differs:\n{diff}'
265