7b3467984a08c3e8e1e0bd5a6c89968735f3dd13
[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 test_1(self):
20         # add checks to the checklist
21         config_checklist = []
22         config_checklist += [KconfigCheck('reason_1', 'decision_1', 'KCONFIG_NAME', 'expected_1')]
23         config_checklist += [CmdlineCheck('reason_2', 'decision_2', 'cmdline_name', 'expected_2')]
24
25         # populate the checklist with the parsed kconfig data
26         parsed_kconfig_options = OrderedDict()
27         parsed_kconfig_options['CONFIG_KCONFIG_NAME'] = 'UNexpected_1'
28         populate_with_data(config_checklist, parsed_kconfig_options, 'kconfig')
29
30         # populate the checklist with the parsed cmdline data
31         parsed_cmdline_options = OrderedDict()
32         parsed_cmdline_options['cmdline_name'] = 'expected_2'
33         populate_with_data(config_checklist, parsed_cmdline_options, 'cmdline')
34
35         # populate the checklist with the kernel version data
36         kernel_version = (42, 43)
37         populate_with_data(config_checklist, kernel_version, 'version')
38
39         # now everything is ready, perform the checks
40         perform_checks(config_checklist)
41
42         # print the results in json
43         output = []
44         print('JSON:')
45         for opt in config_checklist:
46             output.append(opt.json_dump(True))
47         print(json.dumps(output))
48
49         # print the results
50         print('TABLE:')
51         for opt in config_checklist:
52             opt.table_print(None, True)
53             print()
54         print()
55
56         self.assertEqual('foo'.upper(), 'FOO')
57
58     def test_2(self):
59         self.assertTrue('FOO'.isupper())