Commit b8a16648 by Jörg Stucke

fixed tests

parent de62986b
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
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"
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"
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