Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
common_helper_yara
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
common_helper_yara
Commits
b8a16648
Commit
b8a16648
authored
Sep 07, 2021
by
Jörg Stucke
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed tests
parent
de62986b
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
31 deletions
+55
-31
test_common.py
tests/test_common.py
+25
-6
test_compile.py
tests/test_compile.py
+16
-16
test_interpretation.py
tests/test_interpretation.py
+14
-9
No files found.
tests/test_common.py
View file @
b8a16648
import
unittest
from
distutils.version
import
LooseVersion
from
common_helper_yara.common
import
convert_external_variables
import
pytest
import
common_helper_yara.common
as
common
from
common_helper_yara.common
import
convert_external_variables
,
get_yara_version
class
TestYaraCommon
(
unittest
.
TestCase
):
def
test_convert_external_variables
(
self
):
self
.
assertEqual
(
convert_external_variables
({
'a'
:
'b'
}),
'-d a=b'
,
'converted output not correct'
)
self
.
assertEqual
(
convert_external_variables
({
'a'
:
1
,
'b'
:
'c'
}),
'-d a=1 -d b=c'
,
'converted output not correct'
)
@pytest.mark.parametrize
(
'test_input, expected_output'
,
[
({
'a'
:
'b'
},
'-d a=b'
),
({
'a'
:
1
,
'b'
:
'c'
},
'-d a=1 -d b=c'
),
])
def
test_convert_external_variables
(
test_input
,
expected_output
):
assert
convert_external_variables
(
test_input
)
==
expected_output
def
test_get_yara_version
():
assert
LooseVersion
(
'3.0'
)
<
get_yara_version
()
<
LooseVersion
(
'5.0'
)
@pytest.fixture
()
def
yara_not_found
(
monkeypatch
):
def
raise_error
(
_
):
raise
FileNotFoundError
monkeypatch
.
setattr
(
common
,
'check_output'
,
raise_error
)
def
test_get_yara_version_error
(
yara_not_found
):
assert
get_yara_version
()
is
None
tests/test_compile.py
View file @
b8a16648
import
os
import
unittest
from
common_helper_yara.yara_compile
import
compile_rules
from
common_helper_yara.yara_scan
import
scan
from
distutils.version
import
LooseVersion
from
pathlib
import
Path
from
tempfile
import
TemporaryDirectory
from
common_helper_yara.common
import
get_yara_version
from
common_helper_yara.yara_compile
import
compile_rules
from
common_helper_yara.yara_scan
import
scan
DIR_OF_CURRENT_FILE
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
DIR_OF_CURRENT_FILE
=
Path
(
__file__
)
.
parent
COMPILED_FLAG
=
get_yara_version
()
>=
LooseVersion
(
'3.9'
)
class
TestYaraCompile
(
unittest
.
TestCase
):
def
test_compile_and_scan
(
self
):
tmp_dir
=
TemporaryDirectory
(
prefix
=
"common_helper_yara_test_"
)
input_dir
=
os
.
path
.
join
(
DIR_OF_CURRENT_FILE
,
'data/rules'
)
signature_file
=
os
.
path
.
join
(
tmp_dir
.
name
,
'test.yc'
)
data_files
=
os
.
path
.
join
(
DIR_OF_CURRENT_FILE
,
'data/data_files'
)
def
test_compile_and_scan
(
):
with
TemporaryDirectory
(
prefix
=
"common_helper_yara_test_"
)
as
tmp_dir
:
input_dir
=
DIR_OF_CURRENT_FILE
/
'data/rules'
signature_file
=
Path
(
tmp_dir
)
/
'test.yc'
data_files
=
DIR_OF_CURRENT_FILE
/
'data/data_files'
compile_rules
(
input_dir
,
signature_file
,
external_variables
=
{
'test_flag'
:
'true'
})
self
.
assertTrue
(
os
.
path
.
exists
(
signature_file
),
"file not created"
)
assert
signature_file
.
exists
(),
"file not created"
result
=
scan
(
signature_file
,
data_files
,
recursive
=
True
)
self
.
assertIn
(
'lighttpd'
,
result
.
keys
(),
"at least one match missing"
)
self
.
assertIn
(
'lighttpd_simple'
,
result
.
keys
(),
"at least one match missing"
)
result
=
scan
(
signature_file
,
data_files
,
recursive
=
True
,
compiled
=
COMPILED_FLAG
)
assert
'lighttpd'
in
result
.
keys
(),
"at least one match missing"
assert
'lighttpd_simple'
in
result
.
keys
(),
"at least one match missing"
tests/test_interpretation.py
View file @
b8a16648
import
unittest
from
common_helper_yara.yara_interpretation
import
get_all_matched_strings
TEST_DATA
=
{
'test_rule'
:
{
'rule'
:
'test_rule'
,
'meta'
:
{},
'strings'
:
[(
0
,
'$a'
,
b
'test_1'
),
(
10
,
'$b'
,
b
'test_2'
)],
'matches'
:
True
},
'test_rule2'
:
{
'rule'
:
'test_rule2'
,
'meta'
:
{},
'strings'
:
[(
0
,
'$a'
,
b
'test_1'
),
(
10
,
'$b'
,
b
'test_3'
)],
'matches'
:
True
},
}
class
TestYaraInterpretation
(
unittest
.
TestCase
):
def
test_get_all_matched_strings
(
self
):
test_data
=
{
'test_rule'
:
{
'rule'
:
'test_rule'
,
'meta'
:
{},
'strings'
:
[(
0
,
'$a'
,
b
'test_1'
),
(
10
,
'$b'
,
b
'test_2'
)],
'matches'
:
True
},
'test_rule2'
:
{
'rule'
:
'test_rule2'
,
'meta'
:
{},
'strings'
:
[(
0
,
'$a'
,
b
'test_1'
),
(
10
,
'$b'
,
b
'test_3'
)],
'matches'
:
True
},
}
result
=
get_all_matched_strings
(
test_data
)
self
.
assertEqual
(
result
,
set
([
'test_1'
,
'test_2'
,
'test_3'
]),
"resulting strings not correct"
)
def
test_get_all_matched_strings
():
assert
get_all_matched_strings
(
TEST_DATA
)
==
{
'test_1'
,
'test_2'
,
'test_3'
},
"resulting strings not correct"
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