xref: /curl/tests/http/test_30_vsftpd.py (revision c9b95c0b)
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        env.make_data_file(indir=env.gen_dir, fname="upload-1k", fsize=1024)
71        env.make_data_file(indir=env.gen_dir, fname="upload-100k", fsize=100*1024)
72        env.make_data_file(indir=env.gen_dir, fname="upload-1m", fsize=1024*1024)
73
74    def test_30_01_list_dir(self, env: Env, vsftpd: VsFTPD, repeat):
75        curl = CurlClient(env=env)
76        url = f'ftp://{env.ftp_domain}:{vsftpd.port}/'
77        r = curl.ftp_get(urls=[url], with_stats=True)
78        r.check_stats(count=1, http_status=226)
79        lines = open(os.path.join(curl.run_dir, 'download_#1.data')).readlines()
80        assert len(lines) == 4, f'list: {lines}'
81
82    # download 1 file, no SSL
83    @pytest.mark.parametrize("docname", [
84        'data-1k', 'data-1m', 'data-10m'
85    ])
86    def test_30_02_download_1(self, env: Env, vsftpd: VsFTPD, docname, repeat):
87        curl = CurlClient(env=env)
88        srcfile = os.path.join(vsftpd.docs_dir, f'{docname}')
89        count = 1
90        url = f'ftp://{env.ftp_domain}:{vsftpd.port}/{docname}?[0-{count-1}]'
91        r = curl.ftp_get(urls=[url], with_stats=True)
92        r.check_stats(count=count, http_status=226)
93        self.check_downloads(curl, srcfile, count)
94
95    @pytest.mark.parametrize("docname", [
96        'data-1k', 'data-1m', 'data-10m'
97    ])
98    def test_30_03_download_10_serial(self, env: Env, vsftpd: VsFTPD, docname, repeat):
99        curl = CurlClient(env=env)
100        srcfile = os.path.join(vsftpd.docs_dir, f'{docname}')
101        count = 10
102        url = f'ftp://{env.ftp_domain}:{vsftpd.port}/{docname}?[0-{count-1}]'
103        r = curl.ftp_get(urls=[url], with_stats=True)
104        r.check_stats(count=count, http_status=226)
105        self.check_downloads(curl, srcfile, count)
106
107    @pytest.mark.parametrize("docname", [
108        'data-1k', 'data-1m', 'data-10m'
109    ])
110    def test_30_04_download_10_parallel(self, env: Env, vsftpd: VsFTPD, docname, repeat):
111        curl = CurlClient(env=env)
112        srcfile = os.path.join(vsftpd.docs_dir, f'{docname}')
113        count = 10
114        url = f'ftp://{env.ftp_domain}:{vsftpd.port}/{docname}?[0-{count-1}]'
115        r = curl.ftp_get(urls=[url], with_stats=True, extra_args=[
116            '--parallel'
117        ])
118        r.check_stats(count=count, http_status=226)
119        self.check_downloads(curl, srcfile, count)
120
121    @pytest.mark.parametrize("docname", [
122        'upload-1k', 'upload-100k', 'upload-1m'
123    ])
124    def test_30_05_upload_1(self, env: Env, vsftpd: VsFTPD, docname, repeat):
125        curl = CurlClient(env=env)
126        srcfile = os.path.join(env.gen_dir, docname)
127        dstfile = os.path.join(vsftpd.docs_dir, docname)
128        self._rmf(dstfile)
129        count = 1
130        url = f'ftp://{env.ftp_domain}:{vsftpd.port}/'
131        r = curl.ftp_upload(urls=[url], fupload=f'{srcfile}', with_stats=True)
132        r.check_stats(count=count, http_status=226)
133        self.check_upload(env, vsftpd, docname=docname)
134
135    def _rmf(self, path):
136        if os.path.exists(path):
137            return os.remove(path)
138
139    # check with `tcpdump` if curl causes any TCP RST packets
140    @pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")
141    def test_30_06_shutdownh_download(self, env: Env, vsftpd: VsFTPD, repeat):
142        docname = 'data-1k'
143        curl = CurlClient(env=env)
144        count = 1
145        url = f'ftp://{env.ftp_domain}:{vsftpd.port}/{docname}?[0-{count-1}]'
146        r = curl.ftp_get(urls=[url], with_stats=True, with_tcpdump=True)
147        r.check_stats(count=count, http_status=226)
148        assert r.tcpdump
149        assert len(r.tcpdump.stats) == 0, f'Unexpected TCP RSTs packets'
150
151    # check with `tcpdump` if curl causes any TCP RST packets
152    @pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")
153    def test_30_07_shutdownh_upload(self, env: Env, vsftpd: VsFTPD, repeat):
154        docname = 'upload-1k'
155        curl = CurlClient(env=env)
156        srcfile = os.path.join(env.gen_dir, docname)
157        dstfile = os.path.join(vsftpd.docs_dir, docname)
158        self._rmf(dstfile)
159        count = 1
160        url = f'ftp://{env.ftp_domain}:{vsftpd.port}/'
161        r = curl.ftp_upload(urls=[url], fupload=f'{srcfile}', with_stats=True, with_tcpdump=True)
162        r.check_stats(count=count, http_status=226)
163        assert r.tcpdump
164        assert len(r.tcpdump.stats) == 0, f'Unexpected TCP RSTs packets'
165
166    def check_downloads(self, client, srcfile: str, count: int,
167                        complete: bool = True):
168        for i in range(count):
169            dfile = client.download_file(i)
170            assert os.path.exists(dfile)
171            if complete and not filecmp.cmp(srcfile, dfile, shallow=False):
172                diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),
173                                                    b=open(dfile).readlines(),
174                                                    fromfile=srcfile,
175                                                    tofile=dfile,
176                                                    n=1))
177                assert False, f'download {dfile} differs:\n{diff}'
178
179    def check_upload(self, env, vsftpd: VsFTPD, docname):
180        srcfile = os.path.join(env.gen_dir, docname)
181        dstfile = os.path.join(vsftpd.docs_dir, docname)
182        assert os.path.exists(srcfile)
183        assert os.path.exists(dstfile)
184        if not filecmp.cmp(srcfile, dstfile, shallow=False):
185            diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),
186                                                b=open(dstfile).readlines(),
187                                                fromfile=srcfile,
188                                                tofile=dstfile,
189                                                n=1))
190            assert False, f'upload {dstfile} differs:\n{diff}'
191