Commit 9ac7f0ee by lwilms

added test pdf creation and modified old tests

parent c5f5546a
{"device_name": "A devices name", "device_class": "Router", "device_part": "", "vendor": "a vendor", "version": "version 42.13", "release_date": "1970-01-01", "hid": "some specs", "size": 2315412323, "number_of_included_files": 21, "included_files": [1,2,3,4,5,6,7], "total_files_in_firmware": 12}
\ No newline at end of file
import pytest
from pdf_generator.tex_generation.template_engine import TemplateEngine
from test.data.test_dict import TEST_DICT
from test.data.test_dict import TEST_DICT, META_DICT
# pylint: disable=redefined-outer-name
TEST_DATA = {
'analysis': {'file_hashes': {'ssdeep': 'bla', 'sha1': 'blah'}},
'meta_data': {'device_name': 'test_device'}
}
# pylint: disable=redefined-outer-name
@pytest.fixture(scope='function')
......@@ -16,13 +12,10 @@ def stub_engine():
def test_latex_code_generation(stub_engine: TemplateEngine):
result = stub_engine.render_meta_template(TEST_DICT)
result = stub_engine.render_meta_template(META_DICT)
assert result
def test_render_template(tmpdir):
engine = TemplateEngine(template_folder='test', tmp_dir=tmpdir)
test_data = {'meta_data': '123', 'analysis': '456'}
output = engine.render_analysis_template(plugin='render_test', analysis=test_data)
assert output == 'Test - '
def test_render_template(stub_engine, tmpdir):
output = stub_engine.render_main_template(analysis=[TEST_DICT, META_DICT])
assert output
import PyPDF2
from docker_entry import main as main_docker_entry
import pathlib
import shutil
# pylint: disable=redefined-outer-name
def test_docker_entry(template_style='default'):
pathlib.Path("/tmp/interface/data").mkdir(parents=True, exist_ok=True)
pathlib.Path("/tmp/interface/pdf").mkdir(parents=True, exist_ok=True)
shutil.copyfile('data/analysis.json', '/tmp/interface/data/analysis.json')
shutil.copyfile('data/meta.json', '/tmp/interface/data/meta.json')
output = main_docker_entry()
try:
PyPDF2.PdfFileReader(open('/tmp/interface/pdf/A_devices_name_analysis_report.pdf', "rb"))
except PyPDF2.utils.PdfReadError:
assert False
assert pathlib.Path('/tmp/interface/pdf/A_devices_name_analysis_report.pdf').exists()
assert output == 0
......@@ -3,9 +3,10 @@ from pathlib import Path
import pytest
from pdf_generator.generator import (
LOGO_FILE, MAIN_TEMPLATE, META_TEMPLATE, PLUGIN_TEMPLATE_BLUEPRINT, copy_fact_image, create_report_filename,
create_templates, execute_latex, render_analysis_templates
LOGO_FILE, MAIN_TEMPLATE, META_TEMPLATE, copy_fact_image, create_report_filename,
create_templates, execute_latex
)
from test.data.test_dict import TEST_DICT, META_DICT
class MockEngine:
......@@ -13,8 +14,8 @@ class MockEngine:
pass
@staticmethod
def render_main_template(analysis, meta_data):
return '{}\n{}'.format(json.dumps(analysis), json.dumps(meta_data))
def render_main_template(analysis):
return '{}'.format(json.dumps(analysis))
@staticmethod
def render_meta_template(meta_data):
......@@ -24,6 +25,10 @@ class MockEngine:
def render_analysis_template(_, analysis):
return json.dumps(analysis)
@staticmethod
def render_template_class():
return json.dumps('template_class.cls')
def exec_mock(*_, **__):
Path('test').write_text('works')
......@@ -51,22 +56,9 @@ def test_create_report_filename(device_name, pdf_name):
assert create_report_filename({'device_name': device_name}) == pdf_name
def test_create_analysis_templates():
templates = render_analysis_templates(engine=MockEngine(), analysis={'test': {'result': 'data'}})
assert len(templates) == 1
filename, result_code = templates[0]
assert filename == PLUGIN_TEMPLATE_BLUEPRINT.format('test')
assert result_code == '{"result": "data"}'
def test_create_templates(monkeypatch, tmpdir):
monkeypatch.setattr('pdf_generator.generator.TemplateEngine', MockEngine)
create_templates(analysis={'test': {'result': 'data'}}, meta_data={}, tmp_dir=str(tmpdir))
create_templates(analysis=TEST_DICT, meta_data=META_DICT, tmp_dir=str(tmpdir))
assert Path(str(tmpdir), MAIN_TEMPLATE).exists()
assert Path(str(tmpdir), META_TEMPLATE).exists()
assert Path(str(tmpdir), PLUGIN_TEMPLATE_BLUEPRINT.format('test')).exists()
assert Path(str(tmpdir), PLUGIN_TEMPLATE_BLUEPRINT.format('test')).read_text() == '{"result": "data"}'
......@@ -8,7 +8,6 @@ from pdf_generator.tex_generation.template_engine import (
from test.data.test_dict import TEST_DICT
# pylint: disable=redefined-outer-name
......@@ -49,15 +48,11 @@ def test_render_meta_template(stub_engine):
def test_render_main_template(stub_engine):
assert stub_engine.render_main_template(meta_data='anything', analysis='else') == 'Test anything - else'
def test_render_analysis_template(stub_engine):
assert stub_engine.render_analysis_template(plugin='non_existing', analysis='result') == 'Presenting: result'
assert stub_engine.render_main_template(analysis='else') == 'Test - else'
def test_get_five_longest_entries():
assert len(get_five_longest_entries(TEST_DICT['firmware']['analysis']['file_type']['summary'], top=3)) <= 3
longest_dict = get_five_longest_entries(TEST_DICT['firmware']['analysis']['file_type']['summary'], top=1)
assert len(get_five_longest_entries(TEST_DICT['file_type']['summary'], top=3)) <= 3
longest_dict = get_five_longest_entries(TEST_DICT['file_type']['summary'], top=1)
assert len(longest_dict) == 1
assert 'text/plain' in longest_dict.keys()
assert 'compression/zlib' in longest_dict.keys()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment