Mute some pylint warnings for test_engine.py
[kconfig-hardened-check.git] / kconfig_hardened_check / test_engine.py
1 #!/usr/bin/python3
2
3 """
4 This tool helps me to check Linux kernel options against
5 my security hardening preferences for X86_64, ARM64, X86_32, and ARM.
6 Let the computers do their job!
7
8 Author: Alexander Popov <alex.popov@linux.com>
9
10 This module performs unit-testing of the kconfig-hardened-check engine.
11 """
12
13 # pylint: disable=missing-function-docstring,line-too-long
14
15 import unittest
16 from collections import OrderedDict
17 import json
18 from .engine import KconfigCheck, CmdlineCheck, populate_with_data, perform_checks
19
20
21 class TestEngine(unittest.TestCase):
22     """
23     Example test scenario:
24
25         # 1. prepare the checklist
26         config_checklist = []
27         config_checklist += [KconfigCheck('reason_1', 'decision_1', 'KCONFIG_NAME', 'expected_1')]
28         config_checklist += [CmdlineCheck('reason_2', 'decision_2', 'cmdline_name', 'expected_2')]
29
30         # 2. prepare the parsed kconfig options
31         parsed_kconfig_options = OrderedDict()
32         parsed_kconfig_options['CONFIG_KCONFIG_NAME'] = 'UNexpected_1'
33
34         # 3. prepare the parsed cmdline options
35         parsed_cmdline_options = OrderedDict()
36         parsed_cmdline_options['cmdline_name'] = 'expected_2'
37
38         # 4. prepare the kernel version
39         kernel_version = (42, 43)
40
41         # 5. run the engine
42         result = []
43         self.run_engine(config_checklist,
44                         parsed_kconfig_options, parsed_cmdline_options, kernel_version,
45                         result)
46
47         # 6. check that the results are correct
48         # self.assertEqual(...
49     """
50
51     @staticmethod
52     def run_engine(checklist,
53                    parsed_kconfig_options, parsed_cmdline_options, kernel_version,
54                    result):
55         # populate the checklist with data
56         populate_with_data(checklist, parsed_kconfig_options, 'kconfig')
57         populate_with_data(checklist, parsed_cmdline_options, 'cmdline')
58         populate_with_data(checklist, kernel_version, 'version')
59
60         # now everything is ready, perform the checks
61         perform_checks(checklist)
62
63         # print the table with the results
64         print('TABLE:')
65         for opt in checklist:
66             opt.table_print(None, True) # default mode, with_results
67             print()
68
69         # print the results in JSON
70         print('JSON:')
71         for opt in checklist:
72             result.append(opt.json_dump(True)) # with_results
73         print(json.dumps(result))
74         print()
75
76     def test_kconfig_ok(self):
77         # 1. prepare the checklist
78         config_checklist = []
79         config_checklist += [KconfigCheck('reason_1', 'decision_1', 'NAME_1', 'expected_1')]
80
81         # 2. prepare the parsed kconfig options
82         parsed_kconfig_options = OrderedDict()
83         parsed_kconfig_options['CONFIG_NAME_1'] = 'expected_1'
84
85         # 3. run the engine
86         result = []
87         self.run_engine(config_checklist, parsed_kconfig_options, None, None, result)
88
89         # 4. check that the results are correct
90         self.assertEqual(
91                 result,
92                 [["CONFIG_NAME_1", "kconfig", "expected_1", "decision_1", "reason_1", "OK"]]
93         )
94
95
96