mirror of https://github.com/CIRCL/PyCIRCLean
Reorganize filecheck tests
* Move all SampleFile code into test_filecheck.py * Make a stub for logging tests in test_filecheck_logging.pypull/16/head
parent
0bec2a8345
commit
e977966480
Binary file not shown.
|
@ -4,23 +4,46 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
import yaml
|
||||||
|
|
||||||
from tests.utils import SampleFile
|
|
||||||
try:
|
try:
|
||||||
from bin.filecheck import KittenGroomerFileCheck, File, GroomerLogger
|
from bin.filecheck import KittenGroomerFileCheck, File, GroomerLogger
|
||||||
NODEPS = False
|
NODEPS = False
|
||||||
except ImportError:
|
except ImportError:
|
||||||
NODEPS = True
|
NODEPS = True
|
||||||
|
pytestmark = pytest.mark.skipif(NODEPS, reason="Dependencies aren't installed")
|
||||||
|
|
||||||
|
|
||||||
fixture = pytest.fixture
|
fixture = pytest.fixture
|
||||||
skip = pytest.mark.skip
|
skip = pytest.mark.skip
|
||||||
parametrize = pytest.mark.parametrize
|
parametrize = pytest.mark.parametrize
|
||||||
pytestmark = pytest.mark.skipif(NODEPS, reason="Dependencies aren't installed")
|
|
||||||
|
|
||||||
|
|
||||||
NORMAL_FILES_PATH = 'tests/normal/'
|
NORMAL_FILES_PATH = 'tests/normal/'
|
||||||
DANGEROUS_FILES_PATH = 'tests/dangerous/'
|
DANGEROUS_FILES_PATH = 'tests/dangerous/'
|
||||||
|
CATALOG_PATH = 'tests/file_catalog.yaml'
|
||||||
|
|
||||||
|
|
||||||
|
class SampleFile():
|
||||||
|
def __init__(self, path, expect_dangerous):
|
||||||
|
self.path = path
|
||||||
|
self.expect_dangerous = expect_dangerous
|
||||||
|
self.filename = os.path.basename(self.path)
|
||||||
|
|
||||||
|
@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():
|
||||||
|
@ -45,17 +68,11 @@ def list_files(dir_path):
|
||||||
|
|
||||||
def construct_sample_files(file_paths, expect_dangerous):
|
def construct_sample_files(file_paths, expect_dangerous):
|
||||||
"""Construct a list of a sample files from list `file_paths`."""
|
"""Construct a list of a sample files from list `file_paths`."""
|
||||||
complex_exts = {'.gif', '.jpg', '.png', '.svg', '.rar', '.zip'}
|
|
||||||
files = []
|
files = []
|
||||||
for path in file_paths:
|
for path in file_paths:
|
||||||
newfile = SampleFile(path, expect_dangerous)
|
newfile = SampleFile(path, expect_dangerous)
|
||||||
if newfile.has_expect_file:
|
if newfile.has_expect_file:
|
||||||
newfile.parse_expect()
|
newfile.parse_expect()
|
||||||
_, extension = os.path.splitext(path)
|
|
||||||
if extension in complex_exts:
|
|
||||||
newfile.groomer_needed = True
|
|
||||||
else:
|
|
||||||
newfile.groomer_needed = False
|
|
||||||
files.append(newfile)
|
files.append(newfile)
|
||||||
return files
|
return files
|
||||||
|
|
||||||
|
@ -64,33 +81,30 @@ def get_filename(sample_file):
|
||||||
return os.path.basename(sample_file.path)
|
return os.path.basename(sample_file.path)
|
||||||
|
|
||||||
|
|
||||||
|
@fixture(scope='session')
|
||||||
|
def dest_dir_path(tmpdir_factory):
|
||||||
|
return tmpdir_factory.mktemp('dest').strpath
|
||||||
|
|
||||||
|
|
||||||
|
@fixture
|
||||||
|
def groomer(dest_dir_path):
|
||||||
|
dummy_src_path = os.getcwd()
|
||||||
|
return KittenGroomerFileCheck(dummy_src_path, dest_dir_path, debug=True)
|
||||||
|
|
||||||
|
|
||||||
|
@fixture
|
||||||
|
def logger(dest_dir_path):
|
||||||
|
return 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, tmpdir):
|
def test_sample_files(sample_file, groomer, dest_dir_path):
|
||||||
# make groomer (from ? to tmpdir)
|
file_dest_path = dest_dir_path + sample_file.filename
|
||||||
# make file (from file.strpath to tmpdir)
|
file = File(sample_file.path, file_dest_path, groomer.logger)
|
||||||
# run groomer.process_file on it
|
groomer.process_file(file)
|
||||||
# do asserts
|
|
||||||
if not sample_file.groomer_needed:
|
|
||||||
file = File(sample_file.path, tmpdir.strpath, GroomerLogger)
|
|
||||||
file.check()
|
|
||||||
assert file.is_dangerous is sample_file.expect_dangerous
|
assert file.is_dangerous is sample_file.expect_dangerous
|
||||||
if sample_file.groomer_needed:
|
|
||||||
pass
|
|
||||||
# TODO: make a groomer and process the sample file here
|
|
||||||
if sample_file.has_expect_file:
|
if sample_file.has_expect_file:
|
||||||
assert file.mimetype == sample_file.expected_mimetype
|
assert file.mimetype == sample_file.expected_mimetype
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def dest_dir(tmpdir_factory):
|
|
||||||
return tmpdir_factory.mktemp('dest')
|
|
||||||
|
|
||||||
|
|
||||||
@fixture
|
|
||||||
def groomer(tmpdir):
|
|
||||||
dummy_src_path = os.getcwd()
|
|
||||||
dest_dir = tmpdir.strpath
|
|
||||||
return KittenGroomerFileCheck(dummy_src_path, dest_dir, debug=True)
|
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import datetime
|
||||||
|
|
||||||
from datetime import datetime
|
import pytest
|
||||||
|
|
||||||
import yaml
|
|
||||||
|
|
||||||
|
|
||||||
def save_logs(groomer, test_description):
|
def save_logs(groomer, test_description):
|
||||||
|
@ -29,24 +31,3 @@ def save_logs(groomer, test_description):
|
||||||
with open(groomer.logger.log_debug_out, 'rb') as debug_out:
|
with open(groomer.logger.log_debug_out, 'rb') as debug_out:
|
||||||
out = debug_out.read()
|
out = debug_out.read()
|
||||||
test_log.write(out)
|
test_log.write(out)
|
||||||
|
|
||||||
|
|
||||||
class SampleFile():
|
|
||||||
def __init__(self, path, expect_dangerous):
|
|
||||||
self.path = path
|
|
||||||
self.expect_dangerous = expect_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']
|
|
Loading…
Reference in New Issue