mirror of https://github.com/CIRCL/PyCIRCLean
Filecheck tests work with file catalog
parent
e977966480
commit
fed4f75cd7
|
@ -0,0 +1,51 @@
|
||||||
|
#YAML
|
||||||
|
# Possible fields:
|
||||||
|
# description:
|
||||||
|
# mimetype:
|
||||||
|
# xfail:
|
||||||
|
|
||||||
|
normal:
|
||||||
|
Example.gif: # Added: 27-06-2017, source: https://en.wikipedia.org/wiki/File:Example.gif
|
||||||
|
mimetype: image/gif
|
||||||
|
Example.jpg: # Added: 27-06-2017, source: https://en.wikipedia.org/wiki/File:Example.jpg
|
||||||
|
mimetype: image/jpeg
|
||||||
|
Example.ogg: # Added: 27-06-2017, source: https://en.wikipedia.org/wiki/File:Example.ogg
|
||||||
|
description: Ogg vorbis sound file
|
||||||
|
mimetype: audio/ogg
|
||||||
|
Example.png: # Added: 27-06-2017, source: https://en.wikipedia.org/wiki/File:Example.png
|
||||||
|
mimetype: image/png
|
||||||
|
Example.svg: # Added: 27-06-2017, source: https://en.wikipedia.org/wiki/File:Example.svg
|
||||||
|
mimetype: image/svg+xml
|
||||||
|
xfail: True
|
||||||
|
pdf-sample.pdf: # Added: 27-06-2017, source: http://che.org.il/wp-content/uploads/2016/12/pdf-sample.pdf
|
||||||
|
mimetype: application/pdf
|
||||||
|
plaintext.txt: # Added: 27-06-2017, source: hand-generated
|
||||||
|
mimetype: text/plain
|
||||||
|
rar_archive.rar: # Added: 27-06-2017, Rar archive. Source: hand-generated
|
||||||
|
description: rar archive
|
||||||
|
mimetype: application/x-rar
|
||||||
|
xfail: True
|
||||||
|
rich_text.rtf: # Added 27-06-2017), source: hand-generated
|
||||||
|
mimetype: text/rtf
|
||||||
|
sample_mpeg4.mp4: # Added 28-06-2017, source: https://support.apple.com/en-us/HT201549
|
||||||
|
mimetype: video/mp4
|
||||||
|
zip_archive.zip: # Added 27-06-2017, source: hand-generated
|
||||||
|
mimetype: application/zip
|
||||||
|
|
||||||
|
dangerous:
|
||||||
|
42.zip: # Added 27-06-2017, source: http://www.unforgettable.dk/42.zip
|
||||||
|
description: zip archivebomb, password is '42'
|
||||||
|
mimetype: application/zip
|
||||||
|
xfail: True
|
||||||
|
autorun.inf: # Added 27-06-2017, source: hand-generated
|
||||||
|
description: Microsoft autorun file
|
||||||
|
mimetype: text/plain
|
||||||
|
config_file.conf: # Added 27-06-2017, source: hand-generated
|
||||||
|
description: config file
|
||||||
|
mimetype: text/plain
|
||||||
|
message.msg: # Added 27-06-2017, source: ????
|
||||||
|
description: message file, used by Outlook etc
|
||||||
|
mimetype: message/rfc822
|
||||||
|
testRTLexe.txt: # Added 27-06-2017, source: hand-generated
|
||||||
|
description: text file with right-to-left character in filename
|
||||||
|
mimetype: text/plain
|
|
@ -2,6 +2,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import unittest.mock as mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import yaml
|
import yaml
|
||||||
|
@ -25,56 +26,64 @@ CATALOG_PATH = 'tests/file_catalog.yaml'
|
||||||
|
|
||||||
|
|
||||||
class SampleFile():
|
class SampleFile():
|
||||||
def __init__(self, path, expect_dangerous):
|
def __init__(self, path, exp_dangerous):
|
||||||
self.path = path
|
self.path = path
|
||||||
self.expect_dangerous = expect_dangerous
|
self.filename = os.path.basename(path)
|
||||||
self.filename = os.path.basename(self.path)
|
self.exp_dangerous = exp_dangerous
|
||||||
|
|
||||||
@property
|
|
||||||
def expect_path(self):
|
|
||||||
return self.path + '.expect'
|
|
||||||
|
|
||||||
@property
|
|
||||||
def has_expect_file(self):
|
|
||||||
return os.path.isfile(self.expect_path)
|
|
||||||
|
|
||||||
def parse_expect(self):
|
|
||||||
with open(self.expect_path, 'r') as expect_file:
|
|
||||||
self.expect_dict = yaml.safe_load(expect_file)
|
|
||||||
self.expect_dangerous = self.expect_dict['expect_dangerous']
|
|
||||||
self.groomer_needed = self.expect_dict['groomer_needed']
|
|
||||||
self.expected_mimetype = self.expect_dict['expected_mimetype']
|
|
||||||
|
|
||||||
|
|
||||||
def gather_sample_files():
|
def gather_sample_files():
|
||||||
normal_paths = list_files(NORMAL_FILES_PATH)
|
file_catalog = read_file_catalog()
|
||||||
dangerous_paths = list_files(DANGEROUS_FILES_PATH)
|
normal_catalog = file_catalog['normal']
|
||||||
normal_files = construct_sample_files(normal_paths, expect_dangerous=False)
|
dangerous_catalog = file_catalog['dangerous']
|
||||||
dangerous_files = construct_sample_files(dangerous_paths, expect_dangerous=True)
|
sample_files = create_sample_files(
|
||||||
return normal_files + dangerous_files
|
normal_catalog,
|
||||||
|
NORMAL_FILES_PATH,
|
||||||
|
exp_dangerous=False
|
||||||
|
)
|
||||||
|
sample_files.extend(create_sample_files(
|
||||||
|
dangerous_catalog,
|
||||||
|
DANGEROUS_FILES_PATH,
|
||||||
|
exp_dangerous=True
|
||||||
|
))
|
||||||
|
return sample_files
|
||||||
|
|
||||||
|
|
||||||
def list_files(dir_path):
|
def read_file_catalog():
|
||||||
"""List all files in `dir_path`, ignoring .expect files."""
|
with open(os.path.abspath(CATALOG_PATH)) as catalog_file:
|
||||||
|
catalog_dict = yaml.safe_load(catalog_file)
|
||||||
|
return catalog_dict
|
||||||
|
|
||||||
|
|
||||||
|
def create_sample_files(file_catalog, dir_path, exp_dangerous):
|
||||||
|
sample_files = []
|
||||||
|
dir_files = set_of_files(dir_path)
|
||||||
|
# Sorted to make the test cases occur in a consistent order, doesn't have to be
|
||||||
|
for filename, file_dict in sorted(file_catalog.items()):
|
||||||
|
full_path = os.path.abspath(os.path.join(dir_path, filename))
|
||||||
|
try:
|
||||||
|
dir_files.remove(full_path)
|
||||||
|
newfile = SampleFile(full_path, exp_dangerous)
|
||||||
|
newfile.xfail = file_dict.get('xfail', False)
|
||||||
|
sample_files.append(newfile)
|
||||||
|
except KeyError:
|
||||||
|
raise FileNotFoundError("{} could not be found".format(filename))
|
||||||
|
for file_path in dir_files:
|
||||||
|
newfile = SampleFile(file_path, exp_dangerous)
|
||||||
|
newfile.xfail = False
|
||||||
|
sample_files.append(newfile)
|
||||||
|
return sample_files
|
||||||
|
|
||||||
|
|
||||||
|
def set_of_files(dir_path):
|
||||||
|
"""Set of all full file paths in `dir_path`."""
|
||||||
full_dir_path = os.path.abspath(dir_path)
|
full_dir_path = os.path.abspath(dir_path)
|
||||||
files = []
|
file_paths = set()
|
||||||
for file_path in os.listdir(full_dir_path):
|
for path in os.listdir(full_dir_path):
|
||||||
full_file_path = os.path.join(full_dir_path, file_path)
|
full_path = os.path.join(full_dir_path, path)
|
||||||
_, ext = os.path.splitext(full_file_path)
|
if os.path.isfile(full_path):
|
||||||
if os.path.isfile(full_file_path) and not ext.endswith('.expect'):
|
file_paths.add(full_path)
|
||||||
files.append(full_file_path)
|
return file_paths
|
||||||
return files
|
|
||||||
|
|
||||||
|
|
||||||
def construct_sample_files(file_paths, expect_dangerous):
|
|
||||||
"""Construct a list of a sample files from list `file_paths`."""
|
|
||||||
files = []
|
|
||||||
for path in file_paths:
|
|
||||||
newfile = SampleFile(path, expect_dangerous)
|
|
||||||
if newfile.has_expect_file:
|
|
||||||
newfile.parse_expect()
|
|
||||||
files.append(newfile)
|
|
||||||
return files
|
|
||||||
|
|
||||||
|
|
||||||
def get_filename(sample_file):
|
def get_filename(sample_file):
|
||||||
|
@ -94,17 +103,17 @@ def groomer(dest_dir_path):
|
||||||
|
|
||||||
@fixture
|
@fixture
|
||||||
def logger(dest_dir_path):
|
def logger(dest_dir_path):
|
||||||
return GroomerLogger()
|
return mock.Mock(GroomerLogger)
|
||||||
|
|
||||||
|
|
||||||
@parametrize(
|
@parametrize(
|
||||||
argnames="sample_file",
|
argnames="sample_file",
|
||||||
argvalues=gather_sample_files(),
|
argvalues=gather_sample_files(),
|
||||||
ids=get_filename)
|
ids=get_filename)
|
||||||
def test_sample_files(sample_file, groomer, dest_dir_path):
|
def test_sample_files(sample_file, groomer, logger, dest_dir_path):
|
||||||
|
if sample_file.xfail:
|
||||||
|
pytest.xfail(reason='Marked xfail in file catalog')
|
||||||
file_dest_path = dest_dir_path + sample_file.filename
|
file_dest_path = dest_dir_path + sample_file.filename
|
||||||
file = File(sample_file.path, file_dest_path, groomer.logger)
|
file = File(sample_file.path, file_dest_path, logger)
|
||||||
groomer.process_file(file)
|
groomer.process_file(file)
|
||||||
assert file.is_dangerous is sample_file.expect_dangerous
|
assert file.is_dangerous == sample_file.exp_dangerous
|
||||||
if sample_file.has_expect_file:
|
|
||||||
assert file.mimetype == sample_file.expected_mimetype
|
|
||||||
|
|
Loading…
Reference in New Issue