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