c5f6beb55c8800a5e3c29f343240d5641fca1d9b
[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 import unittest
14 from collections import OrderedDict
15 import json
16 from .engine import KconfigCheck, CmdlineCheck, populate_with_data, perform_checks
17
18
19 class TestEngine(unittest.TestCase):
20     """
21     Example test scenario:
22
23         # 1. prepare the checklist
24         config_checklist = []
25         config_checklist += [KconfigCheck('reason_1', 'decision_1', 'KCONFIG_NAME', 'expected_1')]
26         config_checklist += [CmdlineCheck('reason_2', 'decision_2', 'cmdline_name', 'expected_2')]
27
28         # 2. prepare the parsed kconfig options
29         parsed_kconfig_options = OrderedDict()
30         parsed_kconfig_options['CONFIG_KCONFIG_NAME'] = 'UNexpected_1'
31
32         # 3. prepare the parsed cmdline options
33         parsed_cmdline_options = OrderedDict()
34         parsed_cmdline_options['cmdline_name'] = 'expected_2'
35
36         # 4. prepare the kernel version
37         kernel_version = (42, 43)
38
39         # 5. run the engine
40         result = []
41         self.run_engine(config_checklist,
42                         parsed_kconfig_options, parsed_cmdline_options, kernel_version,
43                         result)
44
45         # 6. check that the results are correct
46         # self.assertEqual(...
47     """
48
49     @staticmethod
50     def run_engine(checklist,
51                    parsed_kconfig_options, parsed_cmdline_options, kernel_version,
52                    result):
53         # populate the checklist with data
54         populate_with_data(checklist, parsed_kconfig_options, 'kconfig')
55         populate_with_data(checklist, parsed_cmdline_options, 'cmdline')
56         populate_with_data(checklist, kernel_version, 'version')
57
58         # now everything is ready, perform the checks
59         perform_checks(checklist)
60
61         # print the table with the results
62         print('TABLE:')
63         for opt in checklist:
64             opt.table_print(None, True) # default mode, with_results
65             print()
66
67         # print the results in JSON
68         print('JSON:')
69         for opt in checklist:
70             result.append(opt.json_dump(True)) # with_results
71         print(json.dumps(result))
72         print()
73
74     def test_1(self):
75         # 1. prepare the checklist
76         config_checklist = []
77         config_checklist += [KconfigCheck('reason_1', 'decision_1', 'KCONFIG_NAME', 'expected_1')]
78         config_checklist += [CmdlineCheck('reason_2', 'decision_2', 'cmdline_name', 'expected_2')]
79
80         # 2. prepare the parsed kconfig options
81         parsed_kconfig_options = OrderedDict()
82         parsed_kconfig_options['CONFIG_KCONFIG_NAME'] = 'UNexpected_1'
83
84         # 3. prepare the parsed cmdline options
85         parsed_cmdline_options = OrderedDict()
86         parsed_cmdline_options['cmdline_name'] = 'expected_2'
87
88         # 4. prepare the kernel version
89         kernel_version = (42, 43)
90
91         # 5. run the engine
92         self.run_engine(config_checklist, parsed_kconfig_options, parsed_cmdline_options, kernel_version)
93
94         # 6. check that the results are correct
95         self.assertEqual('foo'.upper(), 'FOO')
96
97     def test_2(self):
98         self.assertTrue('FOO'.isupper())