test_engine: add test_complex_nested()
[kconfig-hardened-check.git] / kconfig_hardened_check / engine.py
index e88781e5cfbf19f9c5ecd274bfa7a7e2d2d8171c..4fdc222e7e8ac3d9fec6a2cf0a3c60c411f8c918 100644 (file)
@@ -1,9 +1,7 @@
 #!/usr/bin/python3
 
 """
-This tool helps me to check Linux kernel options against
-my security hardening preferences for X86_64, ARM64, X86_32, and ARM.
-Let the computers do their job!
+This tool is for checking the security hardening options of the Linux kernel.
 
 Author: Alexander Popov <alex.popov@linux.com>
 
@@ -13,6 +11,21 @@ This module is the engine of checks.
 # pylint: disable=missing-class-docstring,missing-function-docstring
 # pylint: disable=line-too-long,invalid-name,too-many-branches
 
+GREEN_COLOR = '\x1b[32m'
+RED_COLOR = '\x1b[31m'
+COLOR_END = '\x1b[0m'
+
+def colorize_result(input_text):
+    if input_text is None:
+        return input_text
+    if input_text.startswith('OK'):
+        color = GREEN_COLOR
+    elif input_text.startswith('FAIL:'):
+        color = RED_COLOR
+    else:
+        assert(False), f'unexpected result "{input_text}"'
+    return f'{color}{input_text}{COLOR_END}'
+
 
 class OptCheck:
     def __init__(self, reason, decision, name, expected):
@@ -80,7 +93,7 @@ class OptCheck:
     def table_print(self, _mode, with_results):
         print(f'{self.name:<40}|{self.type:^7}|{self.expected:^12}|{self.decision:^10}|{self.reason:^18}', end='')
         if with_results:
-            print(f'| {self.result}', end='')
+            print(f'| {colorize_result(self.result)}', end='')
 
     def json_dump(self, with_results):
         dump = [self.name, self.type, self.expected, self.decision, self.reason]
@@ -105,6 +118,12 @@ class CmdlineCheck(OptCheck):
         return 'cmdline'
 
 
+class SysctlCheck(OptCheck):
+    @property
+    def type(self):
+        return 'sysctl'
+
+
 class VersionCheck:
     def __init__(self, ver_expected):
         assert(ver_expected and isinstance(ver_expected, tuple) and len(ver_expected) == 2), \
@@ -133,7 +152,7 @@ class VersionCheck:
         ver_req = f'kernel version >= {self.ver_expected[0]}.{self.ver_expected[1]}'
         print(f'{ver_req:<91}', end='')
         if with_results:
-            print(f'| {self.result}', end='')
+            print(f'| {colorize_result(self.result)}', end='')
 
 
 class ComplexOptCheck:
@@ -143,7 +162,7 @@ class ComplexOptCheck:
                f'empty {self.__class__.__name__} check'
         assert(len(self.opts) != 1), \
                f'useless {self.__class__.__name__} check: {opts}'
-        assert(isinstance(opts[0], (KconfigCheck, CmdlineCheck))), \
+        assert(isinstance(opts[0], (KconfigCheck, CmdlineCheck, SysctlCheck))), \
                f'invalid {self.__class__.__name__} check: {opts}'
         self.result = None
 
@@ -163,7 +182,7 @@ class ComplexOptCheck:
         if mode == 'verbose':
             print(f'    {"<<< " + self.__class__.__name__ + " >>>":87}', end='')
             if with_results:
-                print(f'| {self.result}', end='')
+                print(f'| {colorize_result(self.result)}', end='')
             for o in self.opts:
                 print()
                 o.table_print(mode, with_results)
@@ -171,7 +190,7 @@ class ComplexOptCheck:
             o = self.opts[0]
             o.table_print(mode, False)
             if with_results:
-                print(f'| {self.result}', end='')
+                print(f'| {colorize_result(self.result)}', end='')
 
     def json_dump(self, with_results):
         dump = self.opts[0].json_dump(False)
@@ -240,7 +259,7 @@ class AND(ComplexOptCheck):
                 return
 
 
-SIMPLE_OPTION_TYPES = ('kconfig', 'version', 'cmdline')
+SIMPLE_OPTION_TYPES = ('kconfig', 'cmdline', 'sysctl', 'version')
 
 
 def populate_simple_opt_with_data(opt, data, data_type):
@@ -256,7 +275,7 @@ def populate_simple_opt_with_data(opt, data, data_type):
     if data_type != opt.type:
         return
 
-    if data_type in ('kconfig', 'cmdline'):
+    if data_type in ('kconfig', 'cmdline', 'sysctl'):
         opt.state = data.get(opt.name, None)
     else:
         assert(data_type == 'version'), \
@@ -265,17 +284,16 @@ def populate_simple_opt_with_data(opt, data, data_type):
 
 
 def populate_opt_with_data(opt, data, data_type):
-    if opt.type == 'complex':
+    assert(opt.type != 'version'), 'a single VersionCheck is useless'
+    if opt.type != 'complex':
+        populate_simple_opt_with_data(opt, data, data_type)
+    else:
         for o in opt.opts:
-            if o.type == 'complex':
+            if o.type != 'complex':
+                populate_simple_opt_with_data(o, data, data_type)
+            else:
                 # Recursion for nested ComplexOptCheck objects
                 populate_opt_with_data(o, data, data_type)
-            else:
-                populate_simple_opt_with_data(o, data, data_type)
-    else:
-        assert(opt.type in ('kconfig', 'cmdline')), \
-               f'bad type "{opt.type}" for a simple check'
-        populate_simple_opt_with_data(opt, data, data_type)
 
 
 def populate_with_data(checklist, data, data_type):
@@ -283,6 +301,14 @@ def populate_with_data(checklist, data, data_type):
         populate_opt_with_data(opt, data, data_type)
 
 
+def override_expected_value(checklist, name, new_val):
+    for opt in checklist:
+        if opt.name == name:
+            assert(opt.type in ('kconfig', 'cmdline', 'sysctl')), \
+                   f'overriding an expected value for "{opt.type}" checks is not supported yet'
+            opt.expected = new_val
+
+
 def perform_checks(checklist):
     for opt in checklist:
         opt.check()