File: test_controls.py

package info (click to toggle)
scap-security-guide 0.1.76-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 110,644 kB
  • sloc: xml: 241,883; sh: 73,777; python: 32,527; makefile: 27
file content (595 lines) | stat: -rw-r--r-- 23,812 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
import pytest
import os
import sys

import ssg.controls
import ssg.build_yaml
from ssg.environment import open_environment

ssg_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "data"))
controls_dir = os.path.join(data_dir, "controls_dir")
profiles_dir = os.path.join(data_dir, "profiles_dir")


def assert_control_confirms_to_standard(controls_manager, profile):
    c_r1 = controls_manager.get_control(profile, "R1")
    assert c_r1.title == "User session timeout"
    assert c_r1.description == "Remote user sessions must be closed after " \
                               "a certain period of inactivity."
    assert c_r1.automated == "yes"
    c_r1_rules = c_r1.selected
    assert "sshd_set_idle_timeout" in c_r1_rules
    assert "accounts_tmout" in c_r1_rules
    assert "var_accounts_tmout=10_min" not in c_r1_rules
    assert "var_accounts_tmout" in c_r1.variables
    assert c_r1.variables["var_accounts_tmout"] == "10_min"
    # abcd is a level-less policy
    assert c_r1.levels == ["default"]
    assert "vague" in c_r1.notes
    c_r2 = controls_manager.get_control(profile, "R2")
    assert c_r2.automated == "no"
    assert c_r2.notes == "This is individual depending on the system " \
                        "workload therefore needs to be audited manually."
    assert c_r2.rationale == "Minimization of configuration helps to reduce attack surface."
    c_r4 = controls_manager.get_control(profile, "R4")
    assert len(c_r4.selected) == 3
    c_r4_rules = c_r4.selected
    assert "accounts_passwords_pam_faillock_deny_root" in c_r4_rules
    assert "accounts_password_pam_minlen" in c_r4_rules
    assert "accounts_password_pam_ocredit" in c_r4_rules
    assert "var_password_pam_ocredit" in c_r4.variables
    assert c_r4.variables["var_password_pam_ocredit"] == "1"
    assert "R4.a" in c_r4.controls
    assert "R4.b" in c_r4.controls
    c_r5 = controls_manager.get_control(profile, "R5")
    assert c_r5.id == "R5"
    assert c_r5.status == "does not meet"
    assert c_r5.fixtext == "There is no fixtext."
    assert c_r5.check == "There is no check."
    assert "Although the listed mitigation is supporting the security function" in c_r5.mitigation
    assert c_r5.description == \
           "The operating system must provide automated mechanisms for supporting account " \
           "management functions."
    assert "Enterprise environments make account management challenging and complex." in \
           c_r5.rationale
    assert c_r5.status_justification == "Mitigate with third-party software."


@pytest.fixture
def env_yaml():
    product_yaml = os.path.join(ssg_root, "products", "rhel8", "product.yml")
    build_config_yaml = os.path.join(ssg_root, "build", "build_config.yml")
    env_yaml = open_environment(build_config_yaml, product_yaml)
    return env_yaml


@pytest.fixture
def controls_manager(env_yaml):
    controls_manager = ssg.controls.ControlsManager(controls_dir, env_yaml)
    controls_manager.load()
    return controls_manager


@pytest.fixture
def compiled_controls_dir_py2(tmpdir):
    return str(tmpdir)


@pytest.fixture
def compiled_controls_dir_py3(tmp_path):
    return tmp_path


@pytest.fixture
def compiled_controls_manager(env_yaml, controls_manager,compiled_controls_dir_py2):
    controls_manager.save_everything(compiled_controls_dir_py2)
    controls_manager = ssg.controls.ControlsManager(compiled_controls_dir_py2, env_yaml)
    controls_manager.load()
    return controls_manager


def _load_test(controls_manager, profile):
    assert_control_confirms_to_standard(controls_manager, profile)


def test_controls_load(controls_manager):
    _load_test(controls_manager, "abcd")


@pytest.mark.skipif(sys.version_info[0] < 3, reason="requires python3 or higher")
def test_controls_invalid_rules(env_yaml):
    existing_rules = {"accounts_tmout", "configure_crypto_policy"}
    controls_manager = ssg.controls.ControlsManager(
        controls_dir, env_yaml, existing_rules)
    with pytest.raises(ValueError) as exc:
        controls_manager.load()
    assert str(exc.value) == \
        "Control abcd:R1 contains nonexisting rule(s) sshd_set_idle_timeout"


@pytest.mark.skipif(sys.version_info[0] < 3, reason="requires python3 or higher")
def test_controls_levels(controls_manager):
    # Default level is the lowest level
    c_1 = controls_manager.get_control("abcd-levels", "S1")
    assert c_1.levels == ["low"]
    c_4 = controls_manager.get_control("abcd-levels", "S4")
    assert c_4.levels == ["low"]

    # Explicit levels
    c_2 = controls_manager.get_control("abcd-levels", "S2")
    assert c_2.levels == ["low"]

    c_3 = controls_manager.get_control("abcd-levels", "S3")
    assert c_3.levels == ["medium"]

    c_4a = controls_manager.get_control("abcd-levels", "S4.a")
    assert c_4a.levels == ["low"]

    c_4b = controls_manager.get_control("abcd-levels", "S4.b")
    assert c_4b.levels == ["high"]

    c_5 = controls_manager.get_control("abcd-levels", "S5")
    assert c_5.levels == ["low"]

    c_6 = controls_manager.get_control("abcd-levels", "S6")
    assert c_6.levels == ["medium"]

    c_7 = controls_manager.get_control("abcd-levels", "S7")
    assert c_7.levels == ["high"]

    # test if all crypto-policy controls have the rule selected
    assert "configure_crypto_policy" in c_5.selections
    assert "configure_crypto_policy" in c_6.selections
    assert "configure_crypto_policy" in c_7.selections

    # just the essential controls
    low_controls = controls_manager.get_all_controls_of_level(
        "abcd-levels", "low")
    # essential and more advanced together
    medium_controls = controls_manager.get_all_controls_of_level(
        "abcd-levels", "medium")
    high_controls = controls_manager.get_all_controls_of_level(
        "abcd-levels", "high")
    all_controls = controls_manager.get_all_controls("abcd-levels")

    assert len(high_controls) == len(all_controls)
    assert len(low_controls) <= len(high_controls)
    assert len(low_controls) == 5
    assert len(medium_controls) == 7

    # test overriding of variables in levels
    assert c_2.variables["var_password_pam_minlen"] == "1"
    assert "var_password_pam_minlen" not in c_3.variables.keys()
    assert c_4b.variables["var_password_pam_minlen"] == "2"

    variable_found = False
    for c in low_controls:
        if "var_password_pam_minlen" in c.variables.keys():
            variable_found = True
            assert c.variables["var_password_pam_minlen"] == "1"
    assert variable_found

    variable_found = False
    for c in medium_controls:
        if "var_password_pam_minlen" in c.variables.keys():
            variable_found = True
            assert c.variables["var_password_pam_minlen"] == "1"
    assert variable_found

    variable_found = False
    for c in high_controls:
        if "var_password_pam_minlen" in c.variables.keys():
            variable_found = True
            assert c.variables["var_password_pam_minlen"] == "2"
    assert variable_found

    # now test if controls of lower level has the variable definition correctly removed
    # because it is overriden by higher level controls
    s2_high = [c for c in high_controls if c.id == "S2"]
    assert len(s2_high) == 1
    assert "var_some_variable" not in s2_high[0].variables.keys()
    assert "var_password_pam_minlen" not in s2_high[0].variables.keys()
    s4b_high = [c for c in high_controls if c.id == "S4.b"]
    assert len(s4b_high) == 1
    assert s4b_high[0].variables["var_some_variable"] == "3"
    assert s4b_high[0].variables["var_password_pam_minlen"] == "2"

    # check that in low level the variable is correctly placed there in S2
    s2_low = [c for c in low_controls if c.id == "S2"]
    assert len(s2_low) == 1
    assert s2_low[0].variables["var_some_variable"] == "1"
    assert s2_low[0].variables["var_password_pam_minlen"] == "1"

    # check that low, medium and high levels have crypto policy selected
    s5_low = [c for c in low_controls if c.id == "S5"]
    assert len(s5_low) == 1
    assert "configure_crypto_policy" in s5_low[0].selections

    s5_medium = [c for c in medium_controls if c.id == "S5"]
    assert len(s5_medium) == 1
    assert "configure_crypto_policy" in s5_medium[0].selections
    s6_medium = [c for c in medium_controls if c.id == "S6"]
    assert len(s6_medium) == 1
    assert "configure_crypto_policy" in s6_medium[0].selections

    s5_high = [c for c in high_controls if c.id == "S5"]
    assert len(s5_high) == 1
    assert "configure_crypto_policy" in s5_high[0].selections
    s6_high = [c for c in high_controls if c.id == "S6"]
    assert len(s6_high) == 1
    assert "configure_crypto_policy" in s6_high[0].selections
    s7_high = [c for c in high_controls if c.id == "S7"]
    assert len(s7_high) == 1
    assert "configure_crypto_policy" in s7_high[0].selections


def test_controls_load_product(controls_manager):
    c_r1 = controls_manager.get_control("abcd", "R1")
    assert c_r1.title == "User session timeout"
    assert c_r1.description == "Remote user sessions must be closed after " \
        "a certain period of inactivity."
    assert c_r1.automated == "yes"

    c_r1_rules = c_r1.selected
    assert "sshd_set_idle_timeout" in c_r1_rules
    assert "accounts_tmout" in c_r1_rules
    assert "var_accounts_tmout=10_min" not in c_r1_rules
    assert "var_accounts_tmout" in c_r1.variables
    assert c_r1.variables["var_accounts_tmout"] == "10_min"


def test_profile_resolution_inline(env_yaml, controls_manager):
    profile_resolution(
        env_yaml, controls_manager, ssg.build_yaml.ProfileWithInlinePolicies, "abcd-low-inline")


def test_profile_resolution_extends_inline(env_yaml, controls_manager):
    profile_resolution_extends(
        env_yaml, controls_manager,
        ssg.build_yaml.ProfileWithInlinePolicies,
        "abcd-low-inline", "abcd-high-inline")


def test_profile_resolution_all_inline(env_yaml, controls_manager):
    profile_resolution_all(
        env_yaml, controls_manager, ssg.build_yaml.ProfileWithInlinePolicies, "abcd-all-inline")


class DictContainingAnyRule(dict):
    def __getitem__(self, key):
        rule = ssg.build_yaml.Rule(key)
        rule.product = "all"
        return rule

    def __contains__(self, rid):
        return True


def profile_resolution(env_yaml, controls_manager, cls, profile_low):
    low_profile_path = os.path.join(profiles_dir, profile_low + ".profile")
    profile = cls.from_yaml(low_profile_path, env_yaml)
    all_profiles = {"abcd-low": profile}
    rules_by_id = DictContainingAnyRule()

    profile.resolve(all_profiles, rules_by_id, controls_manager=controls_manager)

    # Profile 'abcd-low' selects controls R1, R2, R3 from 'abcd' policy,
    # which should add the following rules to the profile:
    selected = profile.get_rule_selectors()
    assert "sshd_set_idle_timeout" in selected
    assert "accounts_tmout" in selected
    assert "configure_crypto_policy" in selected
    assert "var_accounts_tmout" in profile.variables

    # The rule "security_patches_up_to_date" has been selected directly
    # by profile selections, not by using controls, so it should be in
    # the resolved profile as well.
    assert "security_patches_up_to_date" in selected


def profile_resolution_extends(env_yaml, controls_manager, cls, profile_low, profile_high):
    low_profile_path = os.path.join(profiles_dir, profile_low + ".profile")
    low_profile = cls.from_yaml(low_profile_path, env_yaml)
    high_profile_path = os.path.join(profiles_dir, profile_high + ".profile")
    high_profile = cls.from_yaml(high_profile_path, env_yaml)
    all_profiles = {profile_low: low_profile, profile_high: high_profile}
    rules_by_id = DictContainingAnyRule()

    high_profile.resolve(all_profiles, rules_by_id, controls_manager=controls_manager)

    # Profile 'abcd-high' selects controls R1, R2, R3 from 'abcd' policy,
    # which should add the following rules to the profile:
    selected = high_profile.get_rule_selectors()
    assert "sshd_set_idle_timeout" in selected
    assert "accounts_tmout" in selected
    assert "configure_crypto_policy" in selected
    assert "var_accounts_tmout" in high_profile.variables

    # The rule "security_patches_up_to_date" has been selected directly by the
    # abcd-low profile selections, not by using controls, so it should be
    # in the resolved profile as well.
    assert "security_patches_up_to_date" in selected

    assert "accounts_passwords_pam_faillock_deny_root" in selected
    assert "accounts_password_pam_minlen" in selected
    assert "accounts_password_pam_ocredit" in selected
    assert "var_password_pam_ocredit" in high_profile.variables
    assert high_profile.variables["var_password_pam_ocredit"] == "2"


def profile_resolution_all(env_yaml, controls_manager, cls, profile_all):
    profile_path = os.path.join(profiles_dir, profile_all + ".profile")
    profile = cls.from_yaml(profile_path, env_yaml)
    all_profiles = {profile_all: profile}
    rules_by_id = DictContainingAnyRule()

    profile.resolve(all_profiles, rules_by_id, controls_manager=controls_manager)

    # Profile 'abcd-all' selects all controls from 'abcd' policy,
    # which should add the following rules and variables to the profile:
    selected = profile.get_rule_selectors()
    assert "sshd_set_idle_timeout" in selected
    assert "accounts_tmout" in selected
    assert "var_accounts_tmout" in profile.variables
    assert profile.variables["var_accounts_tmout"] == "10_min"
    assert "configure_crypto_policy" in selected
    # Rule "systemd_target_multi_user" is only "related_rules"
    # therefore it should not appear in the resolved profile.
    assert "systemd_target_multi_user" not in selected
    assert "accounts_passwords_pam_faillock_deny_root" in selected
    assert "accounts_password_pam_minlen" in selected
    assert "accounts_password_pam_ocredit" in selected
    assert "var_password_pam_ocredit" in profile.variables
    assert profile.variables["var_password_pam_ocredit"] == "1"

    # The rule "security_patches_up_to_date" has been selected directly
    # by profile selections, not by using controls, so it should be in
    # the resolved profile as well.
    assert "security_patches_up_to_date" in selected


def test_load_control_from_folder(controls_manager):
    _load_test(controls_manager, "qrst")


def test_load_control_from_folder_and_file(controls_manager):
    _load_test(controls_manager, "jklm")


def test_load_compiled_control_from_folder_and_file(compiled_controls_dir_py2, compiled_controls_manager):
    _load_test(compiled_controls_manager, "jklm")


def test_load_control_from_specific_folder_and_file(controls_manager):
    _load_test(controls_manager, "nopq")


@pytest.fixture
def minimal_empty_controls():
    return [dict(title="control", id="c", rules=["a"])]


@pytest.fixture
def one_simple_subcontrol():
    return dict(title="subcontrol", id="s", rules=["b"])


def test_policy_parse_from_dict(minimal_empty_controls):
    policy = ssg.controls.Policy("")
    controls = policy.save_controls_tree(minimal_empty_controls)
    controls = policy.controls
    assert len(controls) == 1
    control = controls[0]
    assert control.title == "control"


def test_policy_fail_parse_from_incomplete_dict(minimal_empty_controls):
    incomplete_controls = minimal_empty_controls
    incomplete_controls[0]["controls"] = [dict(title="nested")]

    policy = ssg.controls.Policy("")

    with pytest.raises(RuntimeError, match="id"):
        policy.save_controls_tree(incomplete_controls)


def order_by_attribute(items, attribute, ordering):
    items_by_attribute = {getattr(i, attribute): i for i in items}
    return [items_by_attribute[val] for val in ordering]


def test_policy_parse_from_nested(minimal_empty_controls, one_simple_subcontrol):
    nested_controls = minimal_empty_controls
    nested_controls[0]["controls"] = [one_simple_subcontrol]

    policy = ssg.controls.Policy("P")
    controls = policy.save_controls_tree(minimal_empty_controls)
    controls = policy.controls
    assert len(controls) == 2
    control, subcontrol = order_by_attribute(controls, "id", ("c", "s"))
    assert control.title == "control"
    assert control.selections == ["a"]
    assert subcontrol.title == "subcontrol"
    assert subcontrol.selections == ["b"]

    controls_manager = ssg.controls.ControlsManager("", dict())
    controls_manager.policies[policy.id] = policy

    controls_manager.resolve_controls()
    control = policy.get_control("c")
    assert control.selections == ["a", "b"]


def test_manager_removes_rules():
    control_dict = dict(id="top", rules=["one", "two", "three", "!four", "!five"])

    policy = ssg.controls.Policy("")
    policy.save_controls_tree([control_dict])
    policy.id = "P"

    controls_manager = ssg.controls.ControlsManager("", dict())
    controls_manager.policies[policy.id] = policy

    control = controls_manager.get_control("P", "top")
    assert len(control.selections) == 5

    controls_manager.remove_selections_not_known(["one", "four"])
    control = controls_manager.get_control("P", "top")
    assert len(control.selections) == 2
    assert "one" in control.selections
    assert "!four" in control.selections

    controls_manager.remove_selections_not_known([])
    control = controls_manager.get_control("P", "top")
    assert len(control.selections) == 0


def test_policy_parse_from_nested():
    top_control_dict = dict(id="top", controls=["nested-1"])
    first_nested_dict = dict(id="nested-1", controls=["nested-2"], rules="Y")
    second_nested_dict = dict(id="nested-2", rules=["X"])

    policy = ssg.controls.Policy("")
    policy.id = "P"

    controls_manager = ssg.controls.ControlsManager("", dict())
    controls_manager.policies[policy.id] = policy

    controls = policy.save_controls_tree([top_control_dict, second_nested_dict, first_nested_dict])
    controls_manager.resolve_controls()
    control = policy.get_control("top")
    assert "Y" in control.selections
    assert "X" in control.selections


def test_policy_parse_from_ours_and_foreign():
    main_control_dict = dict(id="top", controls=["foreign:top", "ours", "P:ours_qualified"])
    main_subcontrol_dicts = [dict(id="ours", rules=["ours"]), dict(id="ours_qualified", rules=["really_ours"])]
    foreign_control_dict = dict(id="top", rules=["foreign"])

    main_policy = ssg.controls.Policy("")
    main_policy.id = "P"
    main_policy.save_controls_tree([main_control_dict] + main_subcontrol_dicts)

    foreign_policy = ssg.controls.Policy("")
    foreign_policy.id = "foreign"
    foreign_policy.save_controls_tree([foreign_control_dict])

    controls_manager = ssg.controls.ControlsManager("", dict())
    controls_manager.policies[main_policy.id] = main_policy
    controls_manager.policies[foreign_policy.id] = foreign_policy

    controls_manager.resolve_controls()
    control = controls_manager.get_control("P", "top")
    assert "ours" in control.selections
    assert "really_ours" in control.selections
    assert "foreign" in control.selections

def test_policy_parse_foreign_with_all():
    main_control_dict = dict(id="top", controls=["foreign:all:level_1", "ours", "P:ours_qualified"])
    main_subcontrol_dicts = [dict(id="ours", rules=["ours"]), dict(id="ours_qualified", rules=["really_ours"])]
    foreign_control_dicts = [dict(id="req1", rules=["foreign_1"], levels=["level_1"]),
                            dict(id="req2", rules=["foreign_2"], levels=["level_1", "level_2"]),
                            dict(id="req3", rules=["foreign_3"], levels=["level_2"])]

    main_policy = ssg.controls.Policy("")
    main_policy.id = "P"
    main_policy.save_controls_tree([main_control_dict] + main_subcontrol_dicts)

    foreign_policy = ssg.controls.Policy("")
    foreign_policy.id = "foreign"
    level1 = ssg.controls.Level.from_level_dict(dict(id="level_1"))
    level2 = ssg.controls.Level.from_level_dict(dict(id="level_2"))

    foreign_policy.levels = [level1, level2]
    foreign_policy.levels_by_id = {"level_1": level1, "level_2": level2}
    foreign_policy.save_controls_tree(foreign_control_dicts)

    controls_manager = ssg.controls.ControlsManager("", dict())
    controls_manager.policies[main_policy.id] = main_policy
    controls_manager.policies[foreign_policy.id] = foreign_policy

    controls_manager.resolve_controls()
    control = controls_manager.get_control("P", "top")
    assert "ours" in control.selections
    assert "really_ours" in control.selections
    assert "foreign_1" in control.selections
    assert "foreign_2" in control.selections
    assert "foreign_3" not in control.selections

def test_policy_parse_from_referenced(minimal_empty_controls, one_simple_subcontrol):
    nested_controls = minimal_empty_controls
    nested_controls[0]["controls"] = ["s"]
    nested_controls.append(one_simple_subcontrol)

    policy = ssg.controls.Policy("")
    policy.id = "P"
    controls = policy.save_controls_tree(minimal_empty_controls)
    controls = policy.controls
    assert len(controls) == 2
    control, subcontrol = order_by_attribute(controls, "id", ("c", "s"))
    assert control.title == "control"
    assert control.controls == ["s"]
    assert subcontrol.title == "subcontrol"


def test_control_with_bad_key():
    control = {'id': 'abcd', 'badval': 'should not be here', }
    control_obj = None
    try:
        control_obj = ssg.controls.Control.from_control_dict(control)
    except ValueError as e:
        assert type(e) is ValueError
    assert control_obj is None


def test_control_with_bad_level():
    control = {'id': 'abcd', 'levels': 'medium', }
    control_obj = None
    try:
        control_obj = ssg.controls.Control.from_control_dict(control)
    except ValueError as e:
        assert type(e) is ValueError
    assert control_obj is None


def test_validating_policy_levels(env_yaml):
    c1_path = os.path.join(data_dir, "policy_with_different_than_implicit_default_level.yml")
    c2_path = os.path.join(data_dir, "policy_with_invalid_level.yml")
    for policy_path in [c1_path, c2_path]:
        policy = ssg.controls.Policy(policy_path, env_yaml)
        with pytest.raises(ValueError):
            policy.load()


@pytest.fixture
def rules_for_test_references_from_controls():
    rule_filenames = [
        "compiled_references_test_rule.yml",
        "compiled_references_test_rule_2.yml"
    ]
    rules = {}
    for filename in rule_filenames:
        rule_file = os.path.join(data_dir, filename)
        rule = ssg.build_yaml.Rule.from_yaml(rule_file)
        rules[rule.id_] = rule
    return rules


def test_references_from_controls(controls_manager, rules_for_test_references_from_controls):
    rules = rules_for_test_references_from_controls
    # in rule.yml the first rule has no references
    assert rules["compiled_references_test_rule"].references == {}
    # in rule.yml the second rule has only stig references
    assert len(rules["compiled_references_test_rule_2"].references["stig"]) == 1
    assert "cis" not in rules["compiled_references_test_rule_2"].references
    assert rules["compiled_references_test_rule_2"].references["stig"] == ["17"]
    # add references to rules from all controls
    controls_manager.add_references(rules)
    # The "uvwx" control file should add cis references to rules
    assert len(rules["compiled_references_test_rule"].references) == 1
    assert rules["compiled_references_test_rule"].references["cis"] == ["R1", "R2"]
    assert len(rules["compiled_references_test_rule_2"].references) == 2
    assert rules["compiled_references_test_rule_2"].references["cis"] == ["R2"]
    assert rules["compiled_references_test_rule_2"].references["stig"] == ["17"]