From: Alexander Popov Date: Sat, 9 Mar 2024 22:22:46 +0000 (+0300) Subject: Adapt test_version() in the unittest X-Git-Tag: v0.6.10~98 X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=6f3214d43f294c5e467d9aaf90207867b3e87090;p=kconfig-hardened-check.git Adapt test_version() in the unittest Refers to #88, #89, #97 --- diff --git a/kernel_hardening_checker/engine.py b/kernel_hardening_checker/engine.py index 844e9f0..268a142 100644 --- a/kernel_hardening_checker/engine.py +++ b/kernel_hardening_checker/engine.py @@ -135,7 +135,9 @@ class SysctlCheck(OptCheck): class VersionCheck: def __init__(self, ver_expected): assert(ver_expected and isinstance(ver_expected, tuple) and len(ver_expected) == 3), \ - f'invalid expected version "{ver_expected}" for VersionCheck' + f'invalid expected version "{ver_expected}" for VersionCheck (1)' + assert(all(map(lambda x: isinstance(x, int), ver_expected))), \ + f'invalid expected version "{ver_expected}" for VersionCheck (2)' self.ver_expected = ver_expected self.ver = () self.result = None diff --git a/kernel_hardening_checker/test_engine.py b/kernel_hardening_checker/test_engine.py index ccb1b18..f835b7c 100644 --- a/kernel_hardening_checker/test_engine.py +++ b/kernel_hardening_checker/test_engine.py @@ -374,19 +374,24 @@ class TestEngine(unittest.TestCase): config_checklist += [OR(KconfigCheck('reason_1', 'decision_1', 'NAME_1', 'expected_1'), VersionCheck((41, 101, 0)))] config_checklist += [AND(KconfigCheck('reason_2', 'decision_2', 'NAME_2', 'expected_2'), - VersionCheck((44, 1, 0)))] - config_checklist += [AND(KconfigCheck('reason_3', 'decision_3', 'NAME_3', 'expected_3'), - VersionCheck((42, 44, 0)))] - config_checklist += [OR(KconfigCheck('reason_4', 'decision_4', 'NAME_4', 'expected_4'), - VersionCheck((42, 43, 0)))] + VersionCheck((43, 1, 0)))] + config_checklist += [OR(KconfigCheck('reason_3', 'decision_3', 'NAME_3', 'expected_3'), + VersionCheck((42, 42, 101)))] + config_checklist += [AND(KconfigCheck('reason_4', 'decision_4', 'NAME_4', 'expected_4'), + VersionCheck((42, 44, 1)))] + config_checklist += [OR(KconfigCheck('reason_5', 'decision_5', 'NAME_5', 'expected_5'), + VersionCheck((42, 43, 44)))] + config_checklist += [AND(KconfigCheck('reason_6', 'decision_6', 'NAME_6', 'expected_6'), + VersionCheck((42, 43, 45)))] # 2. prepare the parsed kconfig options parsed_kconfig_options = OrderedDict() parsed_kconfig_options['CONFIG_NAME_2'] = 'expected_2' - parsed_kconfig_options['CONFIG_NAME_3'] = 'expected_3' + parsed_kconfig_options['CONFIG_NAME_4'] = 'expected_4' + parsed_kconfig_options['CONFIG_NAME_6'] = 'expected_6' # 3. prepare the kernel version - kernel_version = (42, 43, 0) + kernel_version = (42, 43, 44) # 4. run the engine self.run_engine(config_checklist, parsed_kconfig_options, None, None, kernel_version) @@ -396,10 +401,12 @@ class TestEngine(unittest.TestCase): self.get_engine_result(config_checklist, result, 'json') self.assertEqual( result, - [['CONFIG_NAME_1', 'kconfig', 'expected_1', 'decision_1', 'reason_1', 'OK: version >= 41.101'], - ['CONFIG_NAME_2', 'kconfig', 'expected_2', 'decision_2', 'reason_2', 'FAIL: version < 44.1'], - ['CONFIG_NAME_3', 'kconfig', 'expected_3', 'decision_3', 'reason_3', 'FAIL: version < 42.44'], - ['CONFIG_NAME_4', 'kconfig', 'expected_4', 'decision_4', 'reason_4', 'OK: version >= 42.43']] + [['CONFIG_NAME_1', 'kconfig', 'expected_1', 'decision_1', 'reason_1', 'OK: version >= (41, 101, 0)'], + ['CONFIG_NAME_2', 'kconfig', 'expected_2', 'decision_2', 'reason_2', 'FAIL: version < (43, 1, 0)'], + ['CONFIG_NAME_3', 'kconfig', 'expected_3', 'decision_3', 'reason_3', 'OK: version >= (42, 42, 101)'], + ['CONFIG_NAME_4', 'kconfig', 'expected_4', 'decision_4', 'reason_4', 'FAIL: version < (42, 44, 1)'], + ['CONFIG_NAME_5', 'kconfig', 'expected_5', 'decision_5', 'reason_5', 'OK: version >= (42, 43, 44)'], + ['CONFIG_NAME_6', 'kconfig', 'expected_6', 'decision_6', 'reason_6', 'FAIL: version < (42, 43, 45)']] ) def test_stdout(self):