Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fact_pdf_report
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fact-depend
fact_pdf_report
Commits
29203d12
Commit
29203d12
authored
Jul 07, 2019
by
dorp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed tests and added some more config
parent
d6d4d318
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
56 additions
and
73 deletions
+56
-73
.isort.cfg
.isort.cfg
+4
-0
.pylintrc
.pylintrc
+0
-0
code_generation.py
src/latex_code_generation/code_generation.py
+19
-29
test_code_generation.py
src/test/integration/test_code_generation.py
+18
-15
test_latex_code_generation.py
src/test/integration/test_latex_code_generation.py
+0
-12
test_code_generation.py
src/test/unit/test_code_generation.py
+15
-17
No files found.
.isort.cfg
0 → 100644
View file @
29203d12
[settings]
line_length=120
known_first_party=test
multi_line_output=6
.pylintrc
0 → 100644
View file @
29203d12
This diff is collapsed.
Click to expand it.
src/latex_code_generation/code_generation.py
View file @
29203d12
import
logging
import
os
import
shutil
from
pathlib
import
Path
from
tempfile
import
TemporaryDirectory
import
shutil
import
jinja2
import
jinja2
from
common_helper_process
import
execute_shell_command_get_return_code
from
jinja_filters.filter
import
(
nice_unix_time
,
nice_number_filter
,
filter_latex_special_chars
,
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
byte_number_filter
,
check_if_list_empty
,
convert_base64_to_png_filter
,
count_elements_in_list
,
filter_chars_in_list
,
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
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
)
return
jinja2
.
Environment
(
block_start_string
=
'
\
BLOCK{'
,
environment
=
jinja2
.
Environment
(
block_start_string
=
r
'\BLOCK{'
,
block_end_string
=
'}'
,
variable_start_string
=
'
\
VAR{'
,
variable_start_string
=
r
'\VAR{'
,
variable_end_string
=
'}'
,
comment_start_string
=
'
\
#{'
,
comment_start_string
=
r
'\#{'
,
comment_end_string
=
'}'
,
line_statement_prefix
=
'
%%
'
,
line_comment_prefix
=
'
%#
'
,
...
...
@@ -31,9 +30,11 @@ def _set_jinja_env(templates_to_use='default'):
autoescape
=
False
,
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
[
'nice_unix_time'
]
=
nice_unix_time
environment
.
filters
[
'nice_number'
]
=
nice_number_filter
...
...
@@ -75,20 +76,10 @@ def _render_analysis_result(analysis, environment, analysis_plugin, 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
):
main_tex_filename
=
meta_data
[
'device_name'
]
+
"_analysis_report.pdf"
main_tex_filename
=
main_tex_filename
.
replace
(
" "
,
"_"
)
return
main_tex_filename
.
replace
(
"/"
,
"__"
)
main_tex_filename
=
meta_data
[
'device_name'
]
+
'_analysis_report.pdf'
main_tex_filename
=
main_tex_filename
.
replace
(
' '
,
'_'
)
return
main_tex_filename
.
replace
(
'/'
,
'__'
)
def
_copy_fact_image
(
target
):
...
...
@@ -111,16 +102,15 @@ def generate_pdf_report(firmware_uid):
logging
.
warning
(
'No firmware found with UID {}'
.
format
(
firmware_uid
))
return
None
jinja_environment
=
_set_jinja_env
()
_setup_jinja_filters
(
environment
=
jinja_environment
)
environment
=
create_jinja_environment
()
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
,
'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
)
...
...
src/test/
unit/test_new_tests
.py
→
src/test/
integration/test_code_generation
.py
View file @
29203d12
from
pathlib
import
Path
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
from
jinja2
import
Environment
,
FileSystemLoader
# pylint: disable=redefined-outer-name
class
MockResponse
:
@staticmethod
def
json
():
return
dict
()
return
{
'firmware'
:
{
'analysis'
:
{},
'meta_data'
:
{}}}
@pytest.fixture
(
scope
=
'function'
)
def
mock_environment
():
env
=
Environment
(
block_start_string
=
"
\
BLOCK{"
,
block_end_string
=
"}"
,
variable_start_string
=
"
\
VAR{"
,
variable_end_string
=
"}"
,
comment_start_string
=
"
\
#{"
,
comment_end_string
=
"}"
,
line_statement_prefix
=
"
%%
"
,
line_comment_prefix
=
"
%#
"
,
block_start_string
=
r'\BLOCK{'
,
block_end_string
=
'}'
,
variable_start_string
=
r'\VAR{'
,
variable_end_string
=
'}'
,
comment_start_string
=
r'\#{'
,
comment_end_string
=
'}'
,
line_statement_prefix
=
'
%%
'
,
line_comment_prefix
=
'
%#
'
,
trim_blocks
=
True
,
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
...
...
@@ -39,6 +42,6 @@ def test_anything_mocked(monkeypatch):
assert
anything
def
test_
generate_meta_code
(
mock_environment
):
result
=
generate_meta_data_code
(
mock_environment
,
{}
)
def
test_
latex_code_generation
(
mock_environment
):
result
=
generate_meta_data_code
(
mock_environment
,
test_dict
)
assert
result
src/test/integration/test_latex_code_generation.py
deleted
100644 → 0
View file @
d6d4d318
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
()
src/test/unit/test_code_generation.py
View file @
29203d12
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
():
test_data
=
{
'meta_data'
:
'123'
,
'analysis'
:
'456'
}
jinja_env
=
_set_jinja_env
(
templates_to_use
=
'test'
)
output
=
_render_analysis_result
(
test_data
,
jinja_env
,
'render_test'
)
assert
output
==
'Test 123 - 456'
def
test_render_template
():
with
TemporaryDirectory
()
as
tmp_dir
:
test_data
=
{
'meta_data'
:
'123'
,
'analysis'
:
'456'
}
jinja_env
=
create_jinja_environment
(
templates_to_use
=
'test'
)
output
=
_render_analysis_result
(
test_data
,
jinja_env
,
'render_test'
,
tmp_dir
)
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
()
assert
output
==
'Test - '
def
test_main
():
def
test_main
(
monkeypatch
):
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'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment