4 This tool is for checking the security hardening options of the Linux kernel.
6 Author: Alexander Popov <alex.popov@linux.com>
8 This module performs unit-testing of the kernel-hardening-checker engine.
11 # pylint: disable=missing-function-docstring,line-too-long
16 from collections import OrderedDict
19 from .engine import KconfigCheck, CmdlineCheck, SysctlCheck, VersionCheck, OR, AND, populate_with_data, perform_checks, override_expected_value
22 class TestEngine(unittest.TestCase):
24 Example test scenario:
26 # 1. prepare the checklist
28 config_checklist += [KconfigCheck('reason_1', 'decision_1', 'KCONFIG_NAME', 'expected_1')]
29 config_checklist += [CmdlineCheck('reason_2', 'decision_2', 'cmdline_name', 'expected_2')]
30 config_checklist += [SysctlCheck('reason_3', 'decision_3', 'sysctl_name', 'expected_3')]
32 # 2. prepare the parsed kconfig options
33 parsed_kconfig_options = OrderedDict()
34 parsed_kconfig_options['CONFIG_KCONFIG_NAME'] = 'UNexpected_1'
36 # 3. prepare the parsed cmdline options
37 parsed_cmdline_options = OrderedDict()
38 parsed_cmdline_options['cmdline_name'] = 'expected_2'
40 # 4. prepare the parsed sysctl options
41 parsed_sysctl_options = OrderedDict()
42 parsed_sysctl_options['sysctl_name'] = 'expected_3'
44 # 5. prepare the kernel version
45 kernel_version = (42, 43)
48 self.run_engine(config_checklist, parsed_kconfig_options, parsed_cmdline_options, parsed_sysctl_options, kernel_version)
50 # 7. check that the results are correct
52 self.get_engine_result(config_checklist, result, 'json')
59 def run_engine(checklist, parsed_kconfig_options, parsed_cmdline_options, parsed_sysctl_options, kernel_version):
60 # populate the checklist with data
61 if parsed_kconfig_options:
62 populate_with_data(checklist, parsed_kconfig_options, 'kconfig')
63 if parsed_cmdline_options:
64 populate_with_data(checklist, parsed_cmdline_options, 'cmdline')
65 if parsed_sysctl_options:
66 populate_with_data(checklist, parsed_sysctl_options, 'sysctl')
68 populate_with_data(checklist, kernel_version, 'version')
70 # now everything is ready, perform the checks
71 perform_checks(checklist)
73 # print the table with the results
74 print(f'\n{inspect.stack()[1].function}():')
77 opt.table_print('verbose', True) # verbose mode, with_results
81 # print the results in JSON
84 result.append(opt.json_dump(True)) # with_results
85 print(json.dumps(result))
89 def get_engine_result(checklist, result, result_type):
90 assert(result_type in ('json', 'stdout', 'stdout_verbose')), \
91 f'invalid result type "{result_type}"'
93 if result_type == 'json':
95 result.append(opt.json_dump(True)) # with_results
98 captured_output = io.StringIO()
99 stdout_backup = sys.stdout
100 sys.stdout = captured_output
101 for opt in checklist:
102 if result_type == 'stdout_verbose':
103 opt.table_print('verbose', True) # verbose mode, with_results
105 opt.table_print(None, True) # normal mode, with_results
106 sys.stdout = stdout_backup
107 result.append(captured_output.getvalue())
109 def test_simple_kconfig(self):
110 # 1. prepare the checklist
111 config_checklist = []
112 config_checklist += [KconfigCheck('reason_1', 'decision_1', 'NAME_1', 'expected_1')]
113 config_checklist += [KconfigCheck('reason_2', 'decision_2', 'NAME_2', 'expected_2')]
114 config_checklist += [KconfigCheck('reason_3', 'decision_3', 'NAME_3', 'expected_3')]
115 config_checklist += [KconfigCheck('reason_4', 'decision_4', 'NAME_4', 'is not set')]
116 config_checklist += [KconfigCheck('reason_5', 'decision_5', 'NAME_5', 'is present')]
117 config_checklist += [KconfigCheck('reason_6', 'decision_6', 'NAME_6', 'is present')]
118 config_checklist += [KconfigCheck('reason_7', 'decision_7', 'NAME_7', 'is not off')]
119 config_checklist += [KconfigCheck('reason_8', 'decision_8', 'NAME_8', 'is not off')]
120 config_checklist += [KconfigCheck('reason_9', 'decision_9', 'NAME_9', 'is not off')]
121 config_checklist += [KconfigCheck('reason_10', 'decision_10', 'NAME_10', 'is not off')]
123 # 2. prepare the parsed kconfig options
124 parsed_kconfig_options = OrderedDict()
125 parsed_kconfig_options['CONFIG_NAME_1'] = 'expected_1'
126 parsed_kconfig_options['CONFIG_NAME_2'] = 'UNexpected_2'
127 parsed_kconfig_options['CONFIG_NAME_5'] = 'UNexpected_5'
128 parsed_kconfig_options['CONFIG_NAME_7'] = 'really_not_off'
129 parsed_kconfig_options['CONFIG_NAME_8'] = 'off'
130 parsed_kconfig_options['CONFIG_NAME_9'] = '0'
133 self.run_engine(config_checklist, parsed_kconfig_options, None, None, None)
135 # 4. check that the results are correct
137 self.get_engine_result(config_checklist, result, 'json')
140 [{'option_name': 'CONFIG_NAME_1', 'type': 'kconfig', 'desired_val': 'expected_1', 'decision': 'decision_1', 'reason': 'reason_1', 'check_result_text': 'OK', 'check_result': True},
141 {'option_name': 'CONFIG_NAME_2', 'type': 'kconfig', 'desired_val': 'expected_2', 'decision': 'decision_2', 'reason': 'reason_2', 'check_result_text': 'FAIL: "UNexpected_2"', 'check_result': False},
142 {'option_name': 'CONFIG_NAME_3', 'type': 'kconfig', 'desired_val': 'expected_3', 'decision': 'decision_3', 'reason': 'reason_3', 'check_result_text': 'FAIL: is not found', 'check_result': False},
143 {'option_name': 'CONFIG_NAME_4', 'type': 'kconfig', 'desired_val': 'is not set', 'decision': 'decision_4', 'reason': 'reason_4', 'check_result_text': 'OK: is not found', 'check_result': True},
144 {'option_name': 'CONFIG_NAME_5', 'type': 'kconfig', 'desired_val': 'is present', 'decision': 'decision_5', 'reason': 'reason_5', 'check_result_text': 'OK: is present', 'check_result': True},
145 {'option_name': 'CONFIG_NAME_6', 'type': 'kconfig', 'desired_val': 'is present', 'decision': 'decision_6', 'reason': 'reason_6', 'check_result_text': 'FAIL: is not present', 'check_result': False},
146 {'option_name': 'CONFIG_NAME_7', 'type': 'kconfig', 'desired_val': 'is not off', 'decision': 'decision_7', 'reason': 'reason_7', 'check_result_text': 'OK: is not off, "really_not_off"', 'check_result': True},
147 {'option_name': 'CONFIG_NAME_8', 'type': 'kconfig', 'desired_val': 'is not off', 'decision': 'decision_8', 'reason': 'reason_8', 'check_result_text': 'FAIL: is off', 'check_result': False},
148 {'option_name': 'CONFIG_NAME_9', 'type': 'kconfig', 'desired_val': 'is not off', 'decision': 'decision_9', 'reason': 'reason_9', 'check_result_text': 'FAIL: is off, "0"', 'check_result': False},
149 {'option_name': 'CONFIG_NAME_10', 'type': 'kconfig', 'desired_val': 'is not off', 'decision': 'decision_10', 'reason': 'reason_10', 'check_result_text': 'FAIL: is off, not found', 'check_result': False}]
152 def test_simple_cmdline(self):
153 # 1. prepare the checklist
154 config_checklist = []
155 config_checklist += [CmdlineCheck('reason_1', 'decision_1', 'name_1', 'expected_1')]
156 config_checklist += [CmdlineCheck('reason_2', 'decision_2', 'name_2', 'expected_2')]
157 config_checklist += [CmdlineCheck('reason_3', 'decision_3', 'name_3', 'expected_3')]
158 config_checklist += [CmdlineCheck('reason_4', 'decision_4', 'name_4', 'is not set')]
159 config_checklist += [CmdlineCheck('reason_5', 'decision_5', 'name_5', 'is present')]
160 config_checklist += [CmdlineCheck('reason_6', 'decision_6', 'name_6', 'is present')]
161 config_checklist += [CmdlineCheck('reason_7', 'decision_7', 'name_7', 'is not off')]
162 config_checklist += [CmdlineCheck('reason_8', 'decision_8', 'name_8', 'is not off')]
163 config_checklist += [CmdlineCheck('reason_9', 'decision_9', 'name_9', 'is not off')]
164 config_checklist += [CmdlineCheck('reason_10', 'decision_10', 'name_10', 'is not off')]
166 # 2. prepare the parsed cmdline options
167 parsed_cmdline_options = OrderedDict()
168 parsed_cmdline_options['name_1'] = 'expected_1'
169 parsed_cmdline_options['name_2'] = 'UNexpected_2'
170 parsed_cmdline_options['name_5'] = ''
171 parsed_cmdline_options['name_7'] = ''
172 parsed_cmdline_options['name_8'] = 'off'
173 parsed_cmdline_options['name_9'] = '0'
176 self.run_engine(config_checklist, None, parsed_cmdline_options, None, None)
178 # 4. check that the results are correct
180 self.get_engine_result(config_checklist, result, 'json')
183 [{'option_name': 'name_1', 'type': 'cmdline', 'desired_val': 'expected_1', 'decision': 'decision_1', 'reason': 'reason_1', 'check_result_text': 'OK', 'check_result': True},
184 {'option_name': 'name_2', 'type': 'cmdline', 'desired_val': 'expected_2', 'decision': 'decision_2', 'reason': 'reason_2', 'check_result_text': 'FAIL: "UNexpected_2"', 'check_result': False},
185 {'option_name': 'name_3', 'type': 'cmdline', 'desired_val': 'expected_3', 'decision': 'decision_3', 'reason': 'reason_3', 'check_result_text': 'FAIL: is not found', 'check_result': False},
186 {'option_name': 'name_4', 'type': 'cmdline', 'desired_val': 'is not set', 'decision': 'decision_4', 'reason': 'reason_4', 'check_result_text': 'OK: is not found', 'check_result': True},
187 {'option_name': 'name_5', 'type': 'cmdline', 'desired_val': 'is present', 'decision': 'decision_5', 'reason': 'reason_5', 'check_result_text': 'OK: is present', 'check_result': True},
188 {'option_name': 'name_6', 'type': 'cmdline', 'desired_val': 'is present', 'decision': 'decision_6', 'reason': 'reason_6', 'check_result_text': 'FAIL: is not present', 'check_result': False},
189 {'option_name': 'name_7', 'type': 'cmdline', 'desired_val': 'is not off', 'decision': 'decision_7', 'reason': 'reason_7', 'check_result_text': 'OK: is not off, ""', 'check_result': True},
190 {'option_name': 'name_8', 'type': 'cmdline', 'desired_val': 'is not off', 'decision': 'decision_8', 'reason': 'reason_8', 'check_result_text': 'FAIL: is off', 'check_result': False},
191 {'option_name': 'name_9', 'type': 'cmdline', 'desired_val': 'is not off', 'decision': 'decision_9', 'reason': 'reason_9', 'check_result_text': 'FAIL: is off, "0"', 'check_result': False},
192 {'option_name': 'name_10', 'type': 'cmdline', 'desired_val': 'is not off', 'decision': 'decision_10', 'reason': 'reason_10', 'check_result_text': 'FAIL: is off, not found', 'check_result': False}]
195 def test_simple_sysctl(self):
196 # 1. prepare the checklist
197 config_checklist = []
198 config_checklist += [SysctlCheck('reason_1', 'decision_1', 'name_1', 'expected_1')]
199 config_checklist += [SysctlCheck('reason_2', 'decision_2', 'name_2', 'expected_2')]
200 config_checklist += [SysctlCheck('reason_3', 'decision_3', 'name_3', 'expected_3')]
201 config_checklist += [SysctlCheck('reason_4', 'decision_4', 'name_4', 'is not set')]
202 config_checklist += [SysctlCheck('reason_5', 'decision_5', 'name_5', 'is present')]
203 config_checklist += [SysctlCheck('reason_6', 'decision_6', 'name_6', 'is present')]
204 config_checklist += [SysctlCheck('reason_7', 'decision_7', 'name_7', 'is not off')]
205 config_checklist += [SysctlCheck('reason_8', 'decision_8', 'name_8', 'is not off')]
206 config_checklist += [SysctlCheck('reason_9', 'decision_9', 'name_9', 'is not off')]
207 config_checklist += [SysctlCheck('reason_10', 'decision_10', 'name_10', 'is not off')]
209 # 2. prepare the parsed sysctl options
210 parsed_sysctl_options = OrderedDict()
211 parsed_sysctl_options['name_1'] = 'expected_1'
212 parsed_sysctl_options['name_2'] = 'UNexpected_2'
213 parsed_sysctl_options['name_5'] = ''
214 parsed_sysctl_options['name_7'] = ''
215 parsed_sysctl_options['name_8'] = 'off'
216 parsed_sysctl_options['name_9'] = '0'
219 self.run_engine(config_checklist, None, None, parsed_sysctl_options, None)
221 # 4. check that the results are correct
223 self.get_engine_result(config_checklist, result, 'json')
226 [{'option_name': 'name_1', 'type': 'sysctl', 'desired_val': 'expected_1', 'decision': 'decision_1', 'reason': 'reason_1', 'check_result_text': 'OK', 'check_result': True},
227 {'option_name': 'name_2', 'type': 'sysctl', 'desired_val': 'expected_2', 'decision': 'decision_2', 'reason': 'reason_2', 'check_result_text': 'FAIL: "UNexpected_2"', 'check_result': False},
228 {'option_name': 'name_3', 'type': 'sysctl', 'desired_val': 'expected_3', 'decision': 'decision_3', 'reason': 'reason_3', 'check_result_text': 'FAIL: is not found', 'check_result': False},
229 {'option_name': 'name_4', 'type': 'sysctl', 'desired_val': 'is not set', 'decision': 'decision_4', 'reason': 'reason_4', 'check_result_text': 'OK: is not found', 'check_result': True},
230 {'option_name': 'name_5', 'type': 'sysctl', 'desired_val': 'is present', 'decision': 'decision_5', 'reason': 'reason_5', 'check_result_text': 'OK: is present', 'check_result': True},
231 {'option_name': 'name_6', 'type': 'sysctl', 'desired_val': 'is present', 'decision': 'decision_6', 'reason': 'reason_6', 'check_result_text': 'FAIL: is not present', 'check_result': False},
232 {'option_name': 'name_7', 'type': 'sysctl', 'desired_val': 'is not off', 'decision': 'decision_7', 'reason': 'reason_7', 'check_result_text': 'OK: is not off, ""', 'check_result': True},
233 {'option_name': 'name_8', 'type': 'sysctl', 'desired_val': 'is not off', 'decision': 'decision_8', 'reason': 'reason_8', 'check_result_text': 'FAIL: is off', 'check_result': False},
234 {'option_name': 'name_9', 'type': 'sysctl', 'desired_val': 'is not off', 'decision': 'decision_9', 'reason': 'reason_9', 'check_result_text': 'FAIL: is off, "0"', 'check_result': False},
235 {'option_name': 'name_10', 'type': 'sysctl', 'desired_val': 'is not off', 'decision': 'decision_10', 'reason': 'reason_10', 'check_result_text': 'FAIL: is off, not found', 'check_result': False}]
238 def test_complex_or(self):
239 # 1. prepare the checklist
240 config_checklist = []
241 config_checklist += [OR(KconfigCheck('reason_1', 'decision_1', 'NAME_1', 'expected_1'),
242 KconfigCheck('reason_2', 'decision_2', 'NAME_2', 'expected_2'))]
243 config_checklist += [OR(KconfigCheck('reason_3', 'decision_3', 'NAME_3', 'expected_3'),
244 KconfigCheck('reason_4', 'decision_4', 'NAME_4', 'expected_4'))]
245 config_checklist += [OR(KconfigCheck('reason_5', 'decision_5', 'NAME_5', 'expected_5'),
246 KconfigCheck('reason_6', 'decision_6', 'NAME_6', 'expected_6'))]
247 config_checklist += [OR(KconfigCheck('reason_7', 'decision_7', 'NAME_7', 'expected_7'),
248 KconfigCheck('reason_8', 'decision_8', 'NAME_8', 'is not set'))]
249 config_checklist += [OR(KconfigCheck('reason_9', 'decision_9', 'NAME_9', 'expected_9'),
250 KconfigCheck('reason_10', 'decision_10', 'NAME_10', 'is present'))]
251 config_checklist += [OR(KconfigCheck('reason_11', 'decision_11', 'NAME_11', 'expected_11'),
252 KconfigCheck('reason_12', 'decision_12', 'NAME_12', 'is not off'))]
254 # 2. prepare the parsed kconfig options
255 parsed_kconfig_options = OrderedDict()
256 parsed_kconfig_options['CONFIG_NAME_1'] = 'expected_1'
257 parsed_kconfig_options['CONFIG_NAME_2'] = 'UNexpected_2'
258 parsed_kconfig_options['CONFIG_NAME_3'] = 'UNexpected_3'
259 parsed_kconfig_options['CONFIG_NAME_4'] = 'expected_4'
260 parsed_kconfig_options['CONFIG_NAME_5'] = 'UNexpected_5'
261 parsed_kconfig_options['CONFIG_NAME_6'] = 'UNexpected_6'
262 parsed_kconfig_options['CONFIG_NAME_10'] = 'UNexpected_10'
263 parsed_kconfig_options['CONFIG_NAME_12'] = 'really_not_off'
266 self.run_engine(config_checklist, parsed_kconfig_options, None, None, None)
268 # 4. check that the results are correct
270 self.get_engine_result(config_checklist, result, 'json')
273 [{'option_name': 'CONFIG_NAME_1', 'type': 'kconfig', 'desired_val': 'expected_1', 'decision': 'decision_1', 'reason': 'reason_1', 'check_result_text': 'OK', 'check_result': True},
274 {'option_name': 'CONFIG_NAME_3', 'type': 'kconfig', 'desired_val': 'expected_3', 'decision': 'decision_3', 'reason': 'reason_3', 'check_result_text': 'OK: CONFIG_NAME_4 is "expected_4"', 'check_result': True},
275 {'option_name': 'CONFIG_NAME_5', 'type': 'kconfig', 'desired_val': 'expected_5', 'decision': 'decision_5', 'reason': 'reason_5', 'check_result_text': 'FAIL: "UNexpected_5"', 'check_result': False},
276 {'option_name': 'CONFIG_NAME_7', 'type': 'kconfig', 'desired_val': 'expected_7', 'decision': 'decision_7', 'reason': 'reason_7', 'check_result_text': 'OK: CONFIG_NAME_8 is not found', 'check_result': True},
277 {'option_name': 'CONFIG_NAME_9', 'type': 'kconfig', 'desired_val': 'expected_9', 'decision': 'decision_9', 'reason': 'reason_9', 'check_result_text': 'OK: CONFIG_NAME_10 is present', 'check_result': True},
278 {'option_name': 'CONFIG_NAME_11', 'type': 'kconfig', 'desired_val': 'expected_11', 'decision': 'decision_11', 'reason': 'reason_11', 'check_result_text': 'OK: CONFIG_NAME_12 is not off', 'check_result': True}]
281 def test_complex_and(self):
282 # 1. prepare the checklist
283 config_checklist = []
284 config_checklist += [AND(KconfigCheck('reason_1', 'decision_1', 'NAME_1', 'expected_1'),
285 KconfigCheck('reason_2', 'decision_2', 'NAME_2', 'expected_2'))]
286 config_checklist += [AND(KconfigCheck('reason_3', 'decision_3', 'NAME_3', 'expected_3'),
287 KconfigCheck('reason_4', 'decision_4', 'NAME_4', 'expected_4'))]
288 config_checklist += [AND(KconfigCheck('reason_5', 'decision_5', 'NAME_5', 'expected_5'),
289 KconfigCheck('reason_6', 'decision_6', 'NAME_6', 'expected_6'))]
290 config_checklist += [AND(KconfigCheck('reason_7', 'decision_7', 'NAME_7', 'expected_7'),
291 KconfigCheck('reason_8', 'decision_8', 'NAME_8', 'is present'))]
292 config_checklist += [AND(KconfigCheck('reason_9', 'decision_9', 'NAME_9', 'expected_9'),
293 KconfigCheck('reason_10', 'decision_10', 'NAME_10', 'is not off'))]
294 config_checklist += [AND(KconfigCheck('reason_11', 'decision_11', 'NAME_11', 'expected_11'),
295 KconfigCheck('reason_12', 'decision_12', 'NAME_12', 'is not off'))]
297 # 2. prepare the parsed kconfig options
298 parsed_kconfig_options = OrderedDict()
299 parsed_kconfig_options['CONFIG_NAME_1'] = 'expected_1'
300 parsed_kconfig_options['CONFIG_NAME_2'] = 'expected_2'
301 parsed_kconfig_options['CONFIG_NAME_3'] = 'expected_3'
302 parsed_kconfig_options['CONFIG_NAME_4'] = 'UNexpected_4'
303 parsed_kconfig_options['CONFIG_NAME_5'] = 'UNexpected_5'
304 parsed_kconfig_options['CONFIG_NAME_6'] = 'expected_6'
305 parsed_kconfig_options['CONFIG_NAME_7'] = 'expected_7'
306 parsed_kconfig_options['CONFIG_NAME_9'] = 'expected_9'
307 parsed_kconfig_options['CONFIG_NAME_10'] = '0'
308 parsed_kconfig_options['CONFIG_NAME_11'] = 'expected_11'
311 self.run_engine(config_checklist, parsed_kconfig_options, None, None, None)
313 # 4. check that the results are correct
315 self.get_engine_result(config_checklist, result, 'json')
318 [{'option_name': 'CONFIG_NAME_1', 'type': 'kconfig', 'desired_val': 'expected_1', 'decision': 'decision_1', 'reason': 'reason_1', 'check_result_text': 'OK', 'check_result': True},
319 {'option_name': 'CONFIG_NAME_3', 'type': 'kconfig', 'desired_val': 'expected_3', 'decision': 'decision_3', 'reason': 'reason_3', 'check_result_text': 'FAIL: CONFIG_NAME_4 is not "expected_4"', 'check_result': False},
320 {'option_name': 'CONFIG_NAME_5', 'type': 'kconfig', 'desired_val': 'expected_5', 'decision': 'decision_5', 'reason': 'reason_5', 'check_result_text': 'FAIL: "UNexpected_5"', 'check_result': False},
321 {'option_name': 'CONFIG_NAME_7', 'type': 'kconfig', 'desired_val': 'expected_7', 'decision': 'decision_7', 'reason': 'reason_7', 'check_result_text': 'FAIL: CONFIG_NAME_8 is not present', 'check_result': False},
322 {'option_name': 'CONFIG_NAME_9', 'type': 'kconfig', 'desired_val': 'expected_9', 'decision': 'decision_9', 'reason': 'reason_9', 'check_result_text': 'FAIL: CONFIG_NAME_10 is off', 'check_result': False},
323 {'option_name': 'CONFIG_NAME_11', 'type': 'kconfig', 'desired_val': 'expected_11', 'decision': 'decision_11', 'reason': 'reason_11', 'check_result_text': 'FAIL: CONFIG_NAME_12 is off, not found', 'check_result': False}]
326 def test_complex_nested(self):
327 # 1. prepare the checklist
328 config_checklist = []
329 config_checklist += [AND(KconfigCheck('reason_1', 'decision_1', 'NAME_1', 'expected_1'),
330 OR(KconfigCheck('reason_2', 'decision_2', 'NAME_2', 'expected_2'),
331 KconfigCheck('reason_3', 'decision_3', 'NAME_3', 'expected_3')))]
332 config_checklist += [AND(KconfigCheck('reason_4', 'decision_4', 'NAME_4', 'expected_4'),
333 OR(KconfigCheck('reason_5', 'decision_5', 'NAME_5', 'expected_5'),
334 KconfigCheck('reason_6', 'decision_6', 'NAME_6', 'expected_6')))]
335 config_checklist += [OR(KconfigCheck('reason_7', 'decision_7', 'NAME_7', 'expected_7'),
336 AND(KconfigCheck('reason_8', 'decision_8', 'NAME_8', 'expected_8'),
337 KconfigCheck('reason_9', 'decision_9', 'NAME_9', 'expected_9')))]
338 config_checklist += [OR(KconfigCheck('reason_10', 'decision_10', 'NAME_10', 'expected_10'),
339 AND(KconfigCheck('reason_11', 'decision_11', 'NAME_11', 'expected_11'),
340 KconfigCheck('reason_12', 'decision_12', 'NAME_12', 'expected_12')))]
342 # 2. prepare the parsed kconfig options
343 parsed_kconfig_options = OrderedDict()
344 parsed_kconfig_options['CONFIG_NAME_1'] = 'expected_1'
345 parsed_kconfig_options['CONFIG_NAME_2'] = 'UNexpected_2'
346 parsed_kconfig_options['CONFIG_NAME_3'] = 'expected_3'
347 parsed_kconfig_options['CONFIG_NAME_4'] = 'expected_4'
348 parsed_kconfig_options['CONFIG_NAME_5'] = 'UNexpected_5'
349 parsed_kconfig_options['CONFIG_NAME_6'] = 'UNexpected_6'
350 parsed_kconfig_options['CONFIG_NAME_7'] = 'UNexpected_7'
351 parsed_kconfig_options['CONFIG_NAME_8'] = 'expected_8'
352 parsed_kconfig_options['CONFIG_NAME_9'] = 'expected_9'
353 parsed_kconfig_options['CONFIG_NAME_10'] = 'UNexpected_10'
354 parsed_kconfig_options['CONFIG_NAME_11'] = 'UNexpected_11'
355 parsed_kconfig_options['CONFIG_NAME_12'] = 'expected_12'
358 self.run_engine(config_checklist, parsed_kconfig_options, None, None, None)
360 # 4. check that the results are correct
362 self.get_engine_result(config_checklist, result, 'json')
365 [{'option_name': 'CONFIG_NAME_1', 'type': 'kconfig', 'desired_val': 'expected_1', 'decision': 'decision_1', 'reason': 'reason_1', 'check_result_text': 'OK', 'check_result': True},
366 {'option_name': 'CONFIG_NAME_4', 'type': 'kconfig', 'desired_val': 'expected_4', 'decision': 'decision_4', 'reason': 'reason_4', 'check_result_text': 'FAIL: CONFIG_NAME_5 is not "expected_5"', 'check_result': False},
367 {'option_name': 'CONFIG_NAME_7', 'type': 'kconfig', 'desired_val': 'expected_7', 'decision': 'decision_7', 'reason': 'reason_7', 'check_result_text': 'OK: CONFIG_NAME_8 is "expected_8"', 'check_result': True},
368 {'option_name': 'CONFIG_NAME_10', 'type': 'kconfig', 'desired_val': 'expected_10', 'decision': 'decision_10', 'reason': 'reason_10', 'check_result_text': 'FAIL: "UNexpected_10"', 'check_result': False}]
371 def test_version(self):
372 # 1. prepare the checklist
373 config_checklist = []
374 config_checklist += [OR(KconfigCheck('reason_1', 'decision_1', 'NAME_1', 'expected_1'),
375 VersionCheck((41, 101, 0)))]
376 config_checklist += [AND(KconfigCheck('reason_2', 'decision_2', 'NAME_2', 'expected_2'),
377 VersionCheck((43, 1, 0)))]
378 config_checklist += [OR(KconfigCheck('reason_3', 'decision_3', 'NAME_3', 'expected_3'),
379 VersionCheck((42, 42, 101)))]
380 config_checklist += [AND(KconfigCheck('reason_4', 'decision_4', 'NAME_4', 'expected_4'),
381 VersionCheck((42, 44, 1)))]
382 config_checklist += [OR(KconfigCheck('reason_5', 'decision_5', 'NAME_5', 'expected_5'),
383 VersionCheck((42, 43, 44)))]
384 config_checklist += [AND(KconfigCheck('reason_6', 'decision_6', 'NAME_6', 'expected_6'),
385 VersionCheck((42, 43, 45)))]
387 # 2. prepare the parsed kconfig options
388 parsed_kconfig_options = OrderedDict()
389 parsed_kconfig_options['CONFIG_NAME_2'] = 'expected_2'
390 parsed_kconfig_options['CONFIG_NAME_4'] = 'expected_4'
391 parsed_kconfig_options['CONFIG_NAME_6'] = 'expected_6'
393 # 3. prepare the kernel version
394 kernel_version = (42, 43, 44)
397 self.run_engine(config_checklist, parsed_kconfig_options, None, None, kernel_version)
399 # 5. check that the results are correct
401 self.get_engine_result(config_checklist, result, 'json')
404 [{'option_name': 'CONFIG_NAME_1', 'type': 'kconfig', 'desired_val': 'expected_1', 'decision': 'decision_1', 'reason': 'reason_1', 'check_result_text': 'OK: version >= (41, 101, 0)', 'check_result': True},
405 {'option_name': 'CONFIG_NAME_2', 'type': 'kconfig', 'desired_val': 'expected_2', 'decision': 'decision_2', 'reason': 'reason_2', 'check_result_text': 'FAIL: version < (43, 1, 0)', 'check_result': False},
406 {'option_name': 'CONFIG_NAME_3', 'type': 'kconfig', 'desired_val': 'expected_3', 'decision': 'decision_3', 'reason': 'reason_3', 'check_result_text': 'OK: version >= (42, 42, 101)', 'check_result': True},
407 {'option_name': 'CONFIG_NAME_4', 'type': 'kconfig', 'desired_val': 'expected_4', 'decision': 'decision_4', 'reason': 'reason_4', 'check_result_text': 'FAIL: version < (42, 44, 1)', 'check_result': False},
408 {'option_name': 'CONFIG_NAME_5', 'type': 'kconfig', 'desired_val': 'expected_5', 'decision': 'decision_5', 'reason': 'reason_5', 'check_result_text': 'OK: version >= (42, 43, 44)', 'check_result': True},
409 {'option_name': 'CONFIG_NAME_6', 'type': 'kconfig', 'desired_val': 'expected_6', 'decision': 'decision_6', 'reason': 'reason_6', 'check_result_text': 'FAIL: version < (42, 43, 45)', 'check_result': False}]
412 def test_stdout(self):
413 # 1. prepare the checklist
414 config_checklist = []
415 config_checklist += [OR(KconfigCheck('reason_1', 'decision_1', 'NAME_1', 'expected_1'),
416 CmdlineCheck('reason_2', 'decision_2', 'name_2', 'expected_2'),
417 SysctlCheck('reason_3', 'decision_3', 'name_3', 'expected_3'))]
418 config_checklist += [AND(KconfigCheck('reason_4', 'decision_4', 'NAME_4', 'expected_4'),
419 CmdlineCheck('reason_5', 'decision_5', 'name_5', 'expected_5'),
420 SysctlCheck('reason_6', 'decision_6', 'name_6', 'expected_6'))]
422 # 2. prepare the parsed kconfig options
423 parsed_kconfig_options = OrderedDict()
424 parsed_kconfig_options['CONFIG_NAME_1'] = 'UNexpected_1'
426 # 3. prepare the parsed cmdline options
427 parsed_cmdline_options = OrderedDict()
428 parsed_cmdline_options['name_2'] = 'expected_2'
429 parsed_cmdline_options['name_5'] = 'UNexpected_5'
431 # 4. prepare the parsed sysctl options
432 parsed_sysctl_options = OrderedDict()
433 parsed_sysctl_options['name_6'] = 'expected_6'
436 self.run_engine(config_checklist, parsed_kconfig_options, parsed_cmdline_options, parsed_sysctl_options, None)
438 # 6. check that the results are correct
440 self.get_engine_result(config_checklist, json_result, 'json')
443 [{'option_name': 'CONFIG_NAME_1', 'type': 'kconfig', 'desired_val': 'expected_1', 'decision': 'decision_1', 'reason': 'reason_1', 'check_result_text': 'OK: name_2 is "expected_2"', 'check_result': True},
444 {'option_name': 'CONFIG_NAME_4', 'type': 'kconfig', 'desired_val': 'expected_4', 'decision': 'decision_4', 'reason': 'reason_4', 'check_result_text': 'FAIL: name_5 is not "expected_5"', 'check_result': False}]
448 self.get_engine_result(config_checklist, stdout_result, 'stdout')
453 CONFIG_NAME_1 |kconfig| expected_1 |decision_1| reason_1 | \x1b[32mOK: name_2 is "expected_2"\x1b[0m\
454 CONFIG_NAME_4 |kconfig| expected_4 |decision_4| reason_4 | \x1b[31mFAIL: name_5 is not "expected_5"\x1b[0m\
459 self.get_engine_result(config_checklist, stdout_result, 'stdout_verbose')
464 <<< OR >>> | \x1b[32mOK: name_2 is "expected_2"\x1b[0m\n\
465 CONFIG_NAME_1 |kconfig| expected_1 |decision_1| reason_1 | \x1b[31mFAIL: "UNexpected_1"\x1b[0m\n\
466 name_2 |cmdline| expected_2 |decision_2| reason_2 | \x1b[32mOK\x1b[0m\n\
467 name_3 |sysctl | expected_3 |decision_3| reason_3 | None\
470 <<< AND >>> | \x1b[31mFAIL: name_5 is not "expected_5"\x1b[0m\n\
471 CONFIG_NAME_4 |kconfig| expected_4 |decision_4| reason_4 | None\n\
472 name_5 |cmdline| expected_5 |decision_5| reason_5 | \x1b[31mFAIL: "UNexpected_5"\x1b[0m\n\
473 name_6 |sysctl | expected_6 |decision_6| reason_6 | \x1b[32mOK\x1b[0m\
477 def test_value_overriding(self):
478 # 1. prepare the checklist
479 config_checklist = []
480 config_checklist += [KconfigCheck('reason_1', 'decision_1', 'NAME_1', 'expected_1')]
481 config_checklist += [CmdlineCheck('reason_2', 'decision_2', 'name_2', 'expected_2')]
482 config_checklist += [SysctlCheck('reason_3', 'decision_3', 'name_3', 'expected_3')]
484 # 2. prepare the parsed kconfig options
485 parsed_kconfig_options = OrderedDict()
486 parsed_kconfig_options['CONFIG_NAME_1'] = 'expected_1_new'
488 # 3. prepare the parsed cmdline options
489 parsed_cmdline_options = OrderedDict()
490 parsed_cmdline_options['name_2'] = 'expected_2_new'
492 # 4. prepare the parsed sysctl options
493 parsed_sysctl_options = OrderedDict()
494 parsed_sysctl_options['name_3'] = 'expected_3_new'
497 self.run_engine(config_checklist, parsed_kconfig_options, parsed_cmdline_options, parsed_sysctl_options, None)
499 # 6. check that the results are correct
501 self.get_engine_result(config_checklist, result, 'json')
504 [{'option_name': 'CONFIG_NAME_1', 'type': 'kconfig', 'desired_val': 'expected_1', 'decision': 'decision_1', 'reason': 'reason_1', 'check_result_text': 'FAIL: "expected_1_new"', 'check_result': False},
505 {'option_name': 'name_2', 'type': 'cmdline', 'desired_val': 'expected_2', 'decision': 'decision_2', 'reason': 'reason_2', 'check_result_text': 'FAIL: "expected_2_new"', 'check_result': False},
506 {'option_name': 'name_3', 'type': 'sysctl', 'desired_val': 'expected_3', 'decision': 'decision_3', 'reason': 'reason_3', 'check_result_text': 'FAIL: "expected_3_new"', 'check_result': False}]
509 # 7. override expected value and perform the checks again
510 override_expected_value(config_checklist, 'CONFIG_NAME_1', 'expected_1_new')
511 perform_checks(config_checklist)
513 # 8. check that the results are correct
515 self.get_engine_result(config_checklist, result, 'json')
518 [{'option_name': 'CONFIG_NAME_1', 'type': 'kconfig', 'desired_val': 'expected_1_new', 'decision': 'decision_1', 'reason': 'reason_1', 'check_result_text': 'OK', 'check_result': True},
519 {'option_name': 'name_2', 'type': 'cmdline', 'desired_val': 'expected_2', 'decision': 'decision_2', 'reason': 'reason_2', 'check_result_text': 'FAIL: "expected_2_new"', 'check_result': False},
520 {'option_name': 'name_3', 'type': 'sysctl', 'desired_val': 'expected_3', 'decision': 'decision_3', 'reason': 'reason_3', 'check_result_text': 'FAIL: "expected_3_new"', 'check_result': False}]
523 # 9. override expected value and perform the checks again
524 override_expected_value(config_checklist, 'name_2', 'expected_2_new')
525 perform_checks(config_checklist)
527 # 10. check that the results are correct
529 self.get_engine_result(config_checklist, result, 'json')
532 [{'option_name': 'CONFIG_NAME_1', 'type': 'kconfig', 'desired_val': 'expected_1_new', 'decision': 'decision_1', 'reason': 'reason_1', 'check_result_text': 'OK', 'check_result': True},
533 {'option_name': 'name_2', 'type': 'cmdline', 'desired_val': 'expected_2_new', 'decision': 'decision_2', 'reason': 'reason_2', 'check_result_text': 'OK', 'check_result': True},
534 {'option_name': 'name_3', 'type': 'sysctl', 'desired_val': 'expected_3', 'decision': 'decision_3', 'reason': 'reason_3', 'check_result_text': 'FAIL: "expected_3_new"', 'check_result': False}]
537 # 11. override expected value and perform the checks again
538 override_expected_value(config_checklist, 'name_3', 'expected_3_new')
539 perform_checks(config_checklist)
541 # 12. check that the results are correct
543 self.get_engine_result(config_checklist, result, 'json')
546 [{'option_name': 'CONFIG_NAME_1', 'type': 'kconfig', 'desired_val': 'expected_1_new', 'decision': 'decision_1', 'reason': 'reason_1', 'check_result_text': 'OK', 'check_result': True},
547 {'option_name': 'name_2', 'type': 'cmdline', 'desired_val': 'expected_2_new', 'decision': 'decision_2', 'reason': 'reason_2', 'check_result_text': 'OK', 'check_result': True},
548 {'option_name': 'name_3', 'type': 'sysctl', 'desired_val': 'expected_3_new', 'decision': 'decision_3', 'reason': 'reason_3', 'check_result_text': 'OK', 'check_result': True}]