Skip to content

Commit

Permalink
pytest, add ftp upload tests
Browse files Browse the repository at this point in the history
- refs curl#13556
- allow anon uploads on vsftpd test server
- add test_30_05 for plain upload of 1k, 100k, 1m
- add test_31_05 for SSL upload of 1k, 100k, 1m
- verify file size and contents
  • Loading branch information
icing committed May 21, 2024
1 parent dbd626a commit c91ef2f
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
32 changes: 30 additions & 2 deletions tests/http/test_30_vsftpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ def test_30_04_download_10_parallel(self, env: Env, vsftpd: VsFTPD, docname, rep
r.check_stats(count=count, http_status=226)
self.check_downloads(curl, srcfile, count)

@pytest.mark.parametrize("docname", [
'upload-1k', 'upload-100k', 'upload-1m'
])
def test_30_05_upload_1(self, env: Env, vsftpd: VsFTPD, docname, repeat):
curl = CurlClient(env=env)
srcfile = os.path.join(env.gen_dir, docname)
dstfile = os.path.join(vsftpd.docs_dir, docname)
self._rmf(dstfile)
count = 1
url = f'ftp://{env.ftp_domain}:{vsftpd.port}/'
r = curl.ftp_upload(urls=[url], fupload=f'{srcfile}', with_stats=True)
r.check_stats(count=count, http_status=226)
self.check_upload(env, vsftpd, docname=docname)

def _rmf(self, path):
if os.path.exists(path):
return os.remove(path)

def check_downloads(self, client, srcfile: str, count: int,
complete: bool = True):
for i in range(count):
Expand All @@ -128,5 +146,15 @@ def check_downloads(self, client, srcfile: str, count: int,
n=1))
assert False, f'download {dfile} differs:\n{diff}'



def check_upload(self, env, vsftpd: VsFTPD, docname):
srcfile = os.path.join(env.gen_dir, docname)
dstfile = os.path.join(vsftpd.docs_dir, docname)
assert os.path.exists(srcfile)
assert os.path.exists(dstfile)
if not filecmp.cmp(srcfile, dstfile, shallow=False):
diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),
b=open(dstfile).readlines(),
fromfile=srcfile,
tofile=dstfile,
n=1))
assert False, f'upload {dstfile} differs:\n{diff}'
34 changes: 33 additions & 1 deletion tests/http/test_31_vsftpds.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def _class_scope(self, env, vsftpds):
self._make_docs_file(docs_dir=vsftpds.docs_dir, fname='data-10k', fsize=10*1024)
self._make_docs_file(docs_dir=vsftpds.docs_dir, fname='data-1m', fsize=1024*1024)
self._make_docs_file(docs_dir=vsftpds.docs_dir, fname='data-10m', fsize=10*1024*1024)
env.make_data_file(indir=env.gen_dir, fname="upload-1k", fsize=1024)
env.make_data_file(indir=env.gen_dir, fname="upload-100k", fsize=100*1024)
env.make_data_file(indir=env.gen_dir, fname="upload-1m", fsize=1024*1024)

def test_31_01_list_dir(self, env: Env, vsftpds: VsFTPD, repeat):
curl = CurlClient(env=env)
Expand Down Expand Up @@ -125,6 +128,24 @@ def test_31_04_download_10_parallel(self, env: Env, vsftpds: VsFTPD, docname, re
r.check_stats(count=count, http_status=226)
self.check_downloads(curl, srcfile, count)

@pytest.mark.parametrize("docname", [
'upload-1k', 'upload-100k', 'upload-1m'
])
def test_31_05_upload_1(self, env: Env, vsftpds: VsFTPD, docname, repeat):
curl = CurlClient(env=env)
srcfile = os.path.join(env.gen_dir, docname)
dstfile = os.path.join(vsftpds.docs_dir, docname)
self._rmf(dstfile)
count = 1
url = f'ftp://{env.ftp_domain}:{vsftpds.port}/'
r = curl.ftp_ssl_upload(urls=[url], fupload=f'{srcfile}', with_stats=True)
r.check_stats(count=count, http_status=226)
self.check_upload(env, vsftpds, docname=docname)

def _rmf(self, path):
if os.path.exists(path):
return os.remove(path)

def check_downloads(self, client, srcfile: str, count: int,
complete: bool = True):
for i in range(count):
Expand All @@ -138,5 +159,16 @@ def check_downloads(self, client, srcfile: str, count: int,
n=1))
assert False, f'download {dfile} differs:\n{diff}'


def check_upload(self, env, vsftpd: VsFTPD, docname):
srcfile = os.path.join(env.gen_dir, docname)
dstfile = os.path.join(vsftpd.docs_dir, docname)
assert os.path.exists(srcfile)
assert os.path.exists(dstfile)
if not filecmp.cmp(srcfile, dstfile, shallow=False):
diff = "".join(difflib.unified_diff(a=open(srcfile).readlines(),
b=open(dstfile).readlines(),
fromfile=srcfile,
tofile=dstfile,
n=1))
assert False, f'upload {dstfile} differs:\n{diff}'

31 changes: 31 additions & 0 deletions tests/http/testenv/curl.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,37 @@ def ftp_ssl_get(self, urls: List[str],
with_profile=with_profile, no_save=no_save,
extra_args=extra_args)

def ftp_upload(self, urls: List[str], fupload,
with_stats: bool = True,
with_profile: bool = False,
extra_args: List[str] = None):
if extra_args is None:
extra_args = []
extra_args.extend([
'--upload-file', fupload
])
if with_stats:
extra_args.extend([
'-w', '%{json}\\n'
])
return self._raw(urls, options=extra_args,
with_stats=with_stats,
with_headers=False,
with_profile=with_profile)

def ftp_ssl_upload(self, urls: List[str], fupload,
with_stats: bool = True,
with_profile: bool = False,
extra_args: List[str] = None):
if extra_args is None:
extra_args = []
extra_args.extend([
'--ssl-reqd',
])
return self.ftp_upload(urls=urls, fupload=fupload,
with_stats=with_stats, with_profile=with_profile,
extra_args=extra_args)

def response_file(self, idx: int):
return os.path.join(self._run_dir, f'download_{idx}.data')

Expand Down
2 changes: 2 additions & 0 deletions tests/http/testenv/vsftpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ def _write_config(self):
f'anonymous_enable=YES',
f'anon_root={self._docs_dir}',
f'dirmessage_enable=YES',
f'write_enable=YES',
f'anon_upload_enable=YES',
f'log_ftp_protocol=YES',
f'xferlog_enable=YES',
f'xferlog_std_format=YES',
Expand Down

0 comments on commit c91ef2f

Please sign in to comment.