Commit 29203d12 by dorp

fixed tests and added some more config

parent d6d4d318
[settings]
line_length=120
known_first_party=test
multi_line_output=6
This diff is collapsed. Click to expand it.
import logging import logging
import os import os
import shutil
from pathlib import Path from pathlib import Path
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
import shutil
import jinja2
import jinja2
from common_helper_process import execute_shell_command_get_return_code from common_helper_process import execute_shell_command_get_return_code
from jinja_filters.filter import ( from jinja_filters.filter import (
nice_unix_time, nice_number_filter, filter_latex_special_chars, count_elements_in_list, filter_chars_in_list, byte_number_filter, check_if_list_empty, convert_base64_to_png_filter, count_elements_in_list, filter_chars_in_list,
convert_base64_to_png_filter, check_if_list_empty, split_hash, split_output_lines, byte_number_filter filter_latex_special_chars, nice_number_filter, nice_unix_time, split_hash, split_output_lines
) )
from rest_import.rest import create_request_url, request_firmware_data from rest_import.rest import create_request_url, request_firmware_data
GENERIC_TEMPLATE = 'generic.tex' GENERIC_TEMPLATE = 'generic.tex'
def _set_jinja_env(templates_to_use='default'): def create_jinja_environment(templates_to_use='default'):
template_directory = Path(Path(__file__).parent.parent, 'templates', templates_to_use) template_directory = Path(Path(__file__).parent.parent, 'templates', templates_to_use)
return jinja2.Environment( environment = jinja2.Environment(
block_start_string='\BLOCK{', block_start_string=r'\BLOCK{',
block_end_string='}', block_end_string='}',
variable_start_string='\VAR{', variable_start_string=r'\VAR{',
variable_end_string='}', variable_end_string='}',
comment_start_string='\#{', comment_start_string=r'\#{',
comment_end_string='}', comment_end_string='}',
line_statement_prefix='%%', line_statement_prefix='%%',
line_comment_prefix='%#', line_comment_prefix='%#',
...@@ -31,9 +30,11 @@ def _set_jinja_env(templates_to_use='default'): ...@@ -31,9 +30,11 @@ def _set_jinja_env(templates_to_use='default'):
autoescape=False, autoescape=False,
loader=jinja2.FileSystemLoader(str(template_directory)) loader=jinja2.FileSystemLoader(str(template_directory))
) )
_add_filters_to_jinja(environment)
return environment
def _setup_jinja_filters(environment): def _add_filters_to_jinja(environment):
environment.filters['number_format'] = byte_number_filter environment.filters['number_format'] = byte_number_filter
environment.filters['nice_unix_time'] = nice_unix_time environment.filters['nice_unix_time'] = nice_unix_time
environment.filters['nice_number'] = nice_number_filter environment.filters['nice_number'] = nice_number_filter
...@@ -75,20 +76,10 @@ def _render_analysis_result(analysis, environment, analysis_plugin, tmp_dir): ...@@ -75,20 +76,10 @@ def _render_analysis_result(analysis, environment, analysis_plugin, tmp_dir):
return template.render(selected_analysis=analysis, tmp_dir=tmp_dir) return template.render(selected_analysis=analysis, tmp_dir=tmp_dir)
def _create_tex_files(analysis_dict, jinja_env):
module_list = list(analysis_dict['analysis'].keys())
module_list.append('meta_data')
for module in module_list:
try:
_render_analysis_result(analysis_dict, jinja_env, module)
except Exception as e:
logging.error('Could not generate tex file: {} -> {}'.format(type(Exception), e))
def create_report_filename(meta_data): def create_report_filename(meta_data):
main_tex_filename = meta_data['device_name'] + "_analysis_report.pdf" main_tex_filename = meta_data['device_name'] + '_analysis_report.pdf'
main_tex_filename = main_tex_filename.replace(" ", "_") main_tex_filename = main_tex_filename.replace(' ', '_')
return main_tex_filename.replace("/", "__") return main_tex_filename.replace('/', '__')
def _copy_fact_image(target): def _copy_fact_image(target):
...@@ -111,16 +102,15 @@ def generate_pdf_report(firmware_uid): ...@@ -111,16 +102,15 @@ def generate_pdf_report(firmware_uid):
logging.warning('No firmware found with UID {}'.format(firmware_uid)) logging.warning('No firmware found with UID {}'.format(firmware_uid))
return None return None
jinja_environment = _set_jinja_env() environment = create_jinja_environment()
_setup_jinja_filters(environment=jinja_environment)
with TemporaryDirectory() as tmp_dir: with TemporaryDirectory() as tmp_dir:
Path(tmp_dir, 'meta.tex').write_text(generate_meta_data_code(environment=jinja_environment, meta_data=firmware_meta_data)) Path(tmp_dir, 'meta.tex').write_text(generate_meta_data_code(environment=environment, meta_data=firmware_meta_data))
for filename, result_code in generate_analysis_codes(environment=jinja_environment, analysis=firmware_analyses, tmp_dir=tmp_dir): for filename, result_code in generate_analysis_codes(environment=environment, analysis=firmware_analyses, tmp_dir=tmp_dir):
Path(tmp_dir, filename).write_text(result_code) Path(tmp_dir, filename).write_text(result_code)
Path(tmp_dir, 'main.tex').write_text(generate_main_code(firmware_analyses, firmware_meta_data, jinja_environment)) Path(tmp_dir, 'main.tex').write_text(generate_main_code(firmware_analyses, firmware_meta_data, environment))
_copy_fact_image(tmp_dir) _copy_fact_image(tmp_dir)
......
from pathlib import Path from pathlib import Path
import pytest import pytest
from jinja2 import Environment, FileSystemLoader
from latex_code_generation.code_generation import _add_filters_to_jinja, generate_meta_data_code
from rest_import.rest import request_firmware_data
from latex_code_generation.code_generation import generate_meta_data_code from ..data.test_dict import test_dict
from rest_import.rest import create_request_url, request_firmware_data # pylint: disable=redefined-outer-name
from jinja2 import Environment, FileSystemLoader
class MockResponse: class MockResponse:
@staticmethod @staticmethod
def json(): def json():
return dict() return {'firmware': {'analysis': {}, 'meta_data': {}}}
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def mock_environment(): def mock_environment():
env = Environment( env = Environment(
block_start_string="\BLOCK{", block_start_string=r'\BLOCK{',
block_end_string="}", block_end_string='}',
variable_start_string="\VAR{", variable_start_string=r'\VAR{',
variable_end_string="}", variable_end_string='}',
comment_start_string="\#{", comment_start_string=r'\#{',
comment_end_string="}", comment_end_string='}',
line_statement_prefix="%%", line_statement_prefix='%%',
line_comment_prefix="%#", line_comment_prefix='%#',
trim_blocks=True, trim_blocks=True,
autoescape=False, autoescape=False,
loader=FileSystemLoader(str(Path(Path(__file__).parent.parent.parent, "templates", 'default'))), loader=FileSystemLoader(str(Path(Path(__file__).parent.parent.parent, 'templates', 'default'))),
) )
_add_filters_to_jinja(env)
return env return env
...@@ -39,6 +42,6 @@ def test_anything_mocked(monkeypatch): ...@@ -39,6 +42,6 @@ def test_anything_mocked(monkeypatch):
assert anything assert anything
def test_generate_meta_code(mock_environment): def test_latex_code_generation(mock_environment):
result = generate_meta_data_code(mock_environment, {}) result = generate_meta_data_code(mock_environment, test_dict)
assert result assert result
from tempfile import TemporaryDirectory
from pathlib import Path
from ..data.test_dict import test_dict
from latex_code_generation.code_generation import generate_meta_data_code
def test_latex_code_generation():
output_dir = TemporaryDirectory()
main_tex_path = Path(output_dir.name, 'main.tex')
generate_meta_data_code(test_dict, Path(output_dir.name))
assert main_tex_path.exists()
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from pathlib import Path
from latex_code_generation.code_generation import _set_jinja_env, _render_analysis_result, _create_tex_files, generate_pdf_report from latex_code_generation.code_generation import _render_analysis_result, create_jinja_environment, generate_pdf_report
TEST_DATA = {
'analysis': {'file_hashes': {'ssdeep': 'bla', 'sha1': 'blah'}},
'meta_data': {'device_name': 'test_device'}
}
def test_render_template(): def test_render_template():
with TemporaryDirectory() as tmp_dir:
test_data = {'meta_data': '123', 'analysis': '456'} test_data = {'meta_data': '123', 'analysis': '456'}
jinja_env = _set_jinja_env(templates_to_use='test') jinja_env = create_jinja_environment(templates_to_use='test')
output = _render_analysis_result(test_data, jinja_env, 'render_test') output = _render_analysis_result(test_data, jinja_env, 'render_test', tmp_dir)
assert output == 'Test 123 - 456'
assert output == 'Test - '
def test_create_tex_files():
test_data = {'analysis': {'file_hashes': {'ssdeep': 'bla', 'sha1': 'blah'}, 'meta_data': dict()}}
tmp_dir = TemporaryDirectory()
jinja_env = _set_jinja_env(templates_to_use='default')
for template_name in test_data['analysis']:
_create_tex_files(test_data, jinja_env)
file_path = Path(tmp_dir.name, template_name + '.tex')
assert file_path.exists()
def test_main(monkeypatch):
def test_main(): monkeypatch.setattr('latex_code_generation.code_generation.request_firmware_data', lambda *_: (TEST_DATA['analysis'], TEST_DATA['meta_data']))
monkeypatch.setattr('latex_code_generation.code_generation.shutil.move', lambda *_: None)
generate_pdf_report(firmware_uid='b79ea608e2f0390744642bad472f8d9fd7e4713791857da5d5fcabf70a009e50_29626948') generate_pdf_report(firmware_uid='b79ea608e2f0390744642bad472f8d9fd7e4713791857da5d5fcabf70a009e50_29626948')
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