4147da96db1355d628b83a40d93c8ac29ae89ab7
[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 class TestEngine(unittest.TestCase):
19     @staticmethod
20     def run_engine(checklist, parsed_kconfig_options, parsed_cmdline_options, kernel_version):
21         # populate the checklist with data
22         populate_with_data(checklist, parsed_kconfig_options, 'kconfig')
23         populate_with_data(checklist, parsed_cmdline_options, 'cmdline')
24         populate_with_data(checklist, kernel_version, 'version')
25
26         # now everything is ready, perform the checks
27         perform_checks(checklist)
28
29         # print the results in JSON
30         output = []
31         print('JSON:')
32         for opt in checklist:
33             output.append(opt.json_dump(True)) # with_results
34         print(json.dumps(output))
35
36         # print the table with the results
37         print('TABLE:')
38         for opt in checklist:
39             opt.table_print(None, True) # default mode, with_results
40             print()
41         print()
42
43     def test_1(self):
44         # 1. prepare the checklist
45         config_checklist = []
46         config_checklist += [KconfigCheck('reason_1', 'decision_1', 'KCONFIG_NAME', 'expected_1')]
47         config_checklist += [CmdlineCheck('reason_2', 'decision_2', 'cmdline_name', 'expected_2')]
48
49         # 2. prepare the parsed kconfig options
50         parsed_kconfig_options = OrderedDict()
51         parsed_kconfig_options['CONFIG_KCONFIG_NAME'] = 'UNexpected_1'
52
53         # 3. prepare the parsed cmdline options
54         parsed_cmdline_options = OrderedDict()
55         parsed_cmdline_options['cmdline_name'] = 'expected_2'
56
57         # 4. prepare the kernel version
58         kernel_version = (42, 43)
59
60         # 5. run the engine
61         self.run_engine(config_checklist, parsed_kconfig_options, parsed_cmdline_options, kernel_version)
62
63         # 6. check that the results are correct
64         self.assertEqual('foo'.upper(), 'FOO')
65
66     def test_2(self):
67         self.assertTrue('FOO'.isupper())