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
8173ca00
Commit
8173ca00
authored
Jul 12, 2019
by
dorp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added more unit tests
parent
9180fb4e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
103 additions
and
37 deletions
+103
-37
generator.py
pdf_generator/generator.py
+18
-15
test_code_generation.py
test/integration/test_code_generation.py
+0
-15
test_generator.py
test/integration/test_generator.py
+18
-7
test_generator.py
test/unit/test_generator.py
+67
-0
No files found.
pdf_generator/generator.py
View file @
8173ca00
...
...
@@ -20,7 +20,7 @@ def copy_fact_image(target):
shutil
.
copy
(
str
(
Path
(
__file__
)
.
parent
/
'templates'
/
'fact_logo.png'
),
str
(
Path
(
target
)
/
'fact_logo.png'
))
def
generate_analysis_
cod
es
(
engine
,
analysis
):
def
generate_analysis_
templat
es
(
engine
,
analysis
):
return
[
(
'{}.tex'
.
format
(
analysis_plugin
),
engine
.
render_analysis_template
(
analysis_plugin
,
analysis
[
analysis_plugin
]))
for
analysis_plugin
in
analysis
]
...
...
@@ -30,6 +30,21 @@ def create_report_filename(meta_data):
return
'{}_analysis_report.pdf'
.
format
(
meta_data
[
'device_name'
]
.
replace
(
' '
,
'_'
)
.
replace
(
'/'
,
'__'
))
def
compile_pdf
(
meta_data
,
tmp_dir
):
copy_fact_image
(
tmp_dir
)
execute_latex
(
tmp_dir
)
shutil
.
move
(
str
(
Path
(
tmp_dir
,
'main.pdf'
)),
str
(
Path
(
create_report_filename
(
meta_data
))))
def
create_templates
(
analysis
,
meta_data
,
tmp_dir
):
engine
=
Engine
(
tmp_dir
=
tmp_dir
)
Path
(
tmp_dir
,
'main.tex'
)
.
write_text
(
engine
.
render_main_template
(
analysis
=
analysis
,
meta_data
=
meta_data
))
Path
(
tmp_dir
,
'meta.tex'
)
.
write_text
(
engine
.
render_meta_template
(
meta_data
))
for
filename
,
result_code
in
generate_analysis_templates
(
engine
=
engine
,
analysis
=
analysis
):
Path
(
tmp_dir
,
filename
)
.
write_text
(
result_code
)
def
generate_report
(
firmware_uid
,
server_url
=
None
):
request_url
=
create_request_url
(
firmware_uid
,
server_url
)
try
:
...
...
@@ -39,19 +54,7 @@ def generate_report(firmware_uid, server_url=None):
return
1
with
TemporaryDirectory
()
as
tmp_dir
:
engine
=
Engine
(
tmp_dir
=
tmp_dir
)
Path
(
tmp_dir
,
'meta.tex'
)
.
write_text
(
engine
.
render_meta_template
(
meta_data
))
for
filename
,
result_code
in
generate_analysis_codes
(
engine
=
engine
,
analysis
=
analysis
):
Path
(
tmp_dir
,
filename
)
.
write_text
(
result_code
)
Path
(
tmp_dir
,
'main.tex'
)
.
write_text
(
engine
.
render_main_template
(
analysis
=
analysis
,
meta_data
=
meta_data
))
copy_fact_image
(
tmp_dir
)
execute_latex
(
tmp_dir
)
pdf_filename
=
create_report_filename
(
meta_data
)
shutil
.
move
(
str
(
Path
(
tmp_dir
,
'main.pdf'
)),
str
(
Path
(
'.'
,
pdf_filename
)))
create_templates
(
analysis
,
meta_data
,
tmp_dir
)
compile_pdf
(
meta_data
,
tmp_dir
)
return
0
test/integration/test_code_generation.py
deleted
100644 → 0
View file @
9180fb4e
import
pytest
from
pdf_generator.tex_generation.template_engine
import
Engine
from
test.data.test_dict
import
TEST_DICT
# pylint: disable=redefined-outer-name
@pytest.fixture
(
scope
=
'function'
)
def
stub_engine
():
return
Engine
()
def
test_latex_code_generation
(
stub_engine
:
Engine
):
result
=
stub_engine
.
render_meta_template
(
TEST_DICT
)
assert
result
test/
unit/test_code_generation
.py
→
test/
integration/test_generator
.py
View file @
8173ca00
from
tempfile
import
TemporaryDirectory
import
pytest
from
pdf_generator.generator
import
generate_report
from
pdf_generator.tex_generation.template_engine
import
Engine
from
test.data.test_dict
import
TEST_DICT
# pylint: disable=redefined-outer-name
TEST_DATA
=
{
'analysis'
:
{
'file_hashes'
:
{
'ssdeep'
:
'bla'
,
'sha1'
:
'blah'
}},
...
...
@@ -9,11 +11,20 @@ TEST_DATA = {
}
def
test_render_template
():
with
TemporaryDirectory
()
as
tmp_dir
:
engine
=
Engine
(
template_folder
=
'test'
,
tmp_dir
=
tmp_dir
)
test_data
=
{
'meta_data'
:
'123'
,
'analysis'
:
'456'
}
output
=
engine
.
render_analysis_template
(
plugin
=
'render_test'
,
analysis
=
test_data
)
@pytest.fixture
(
scope
=
'function'
)
def
stub_engine
():
return
Engine
()
def
test_latex_code_generation
(
stub_engine
:
Engine
):
result
=
stub_engine
.
render_meta_template
(
TEST_DICT
)
assert
result
def
test_render_template
(
tmpdir
):
engine
=
Engine
(
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 - '
...
...
test/unit/test_generator.py
0 → 100644
View file @
8173ca00
import
json
from
pathlib
import
Path
from
pdf_generator.generator
import
(
copy_fact_image
,
create_report_filename
,
create_templates
,
execute_latex
,
generate_analysis_templates
)
class
MockEngine
:
def
__init__
(
self
,
*
_
,
**
__
):
pass
@staticmethod
def
render_main_template
(
analysis
,
meta_data
):
return
'{}
\n
{}'
.
format
(
json
.
dumps
(
analysis
),
json
.
dumps
(
meta_data
))
@staticmethod
def
render_meta_template
(
meta_data
):
return
json
.
dumps
(
meta_data
)
@staticmethod
def
render_analysis_template
(
_
,
analysis
):
return
json
.
dumps
(
analysis
)
def
exec_mock
(
*
_
,
**
__
):
Path
(
'test'
)
.
write_text
(
'works'
)
def
test_execute_latex
(
monkeypatch
,
tmpdir
):
monkeypatch
.
setattr
(
'pdf_generator.generator.execute_shell_command_get_return_code'
,
exec_mock
)
execute_latex
(
tmpdir
)
assert
Path
(
tmpdir
,
'test'
)
.
exists
()
assert
Path
(
tmpdir
,
'test'
)
.
read_text
()
==
'works'
def
test_copy_fact_image
(
tmpdir
):
copy_fact_image
(
tmpdir
)
assert
Path
(
tmpdir
,
'fact_logo.png'
)
.
exists
()
def
test_create_report_filename
():
assert
create_report_filename
({
'device_name'
:
'simple'
})
==
'simple_analysis_report.pdf'
assert
create_report_filename
({
'device_name'
:
'harder name'
})
==
'harder_name_analysis_report.pdf'
assert
create_report_filename
({
'device_name'
:
'dangerous/name'
})
==
'dangerous__name_analysis_report.pdf'
def
test_create_analysis_templates
():
templates
=
generate_analysis_templates
(
engine
=
MockEngine
(),
analysis
=
{
'test'
:
{
'result'
:
'data'
}})
assert
len
(
templates
)
==
1
filename
,
result_code
=
templates
[
0
]
assert
filename
==
'test.tex'
assert
result_code
==
'{"result": "data"}'
def
test_create_templates
(
monkeypatch
,
tmpdir
):
monkeypatch
.
setattr
(
'pdf_generator.generator.Engine'
,
MockEngine
)
create_templates
(
analysis
=
{
'test'
:
{
'result'
:
'data'
}},
meta_data
=
{},
tmp_dir
=
tmpdir
)
assert
Path
(
tmpdir
,
'main.tex'
)
.
exists
()
assert
Path
(
tmpdir
,
'meta.tex'
)
.
exists
()
assert
Path
(
tmpdir
,
'test.tex'
)
.
exists
()
assert
Path
(
tmpdir
,
'test.tex'
)
.
read_text
()
==
'{"result": "data"}'
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