| 1 | import importlib |
| 2 | import pathlib |
| 3 | import json |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | NS = "http://checklists.nist.gov/xccdf/1.2" |
| 8 | |
| 9 | |
| 10 | def import_arbitrary_file_as_module(path, module_name): |
| 11 | spec = importlib.util.spec_from_loader( |
| 12 | module_name, importlib.machinery.SourceFileLoader(module_name, path)) |
| 13 | module = importlib.util.module_from_spec(spec) |
| 14 | spec.loader.exec_module(module) |
| 15 | return module |
| 16 | |
| 17 | |
| 18 | autotailor = import_arbitrary_file_as_module("../../../utils/autotailor", "autotailor") |
| 19 | |
| 20 | |
| 21 | def test_is_valid_xccdf_id(): |
| 22 | assert autotailor.is_valid_xccdf_id( |
| 23 | "xccdf_com.example.www_profile_abcd") |
| 24 | assert autotailor.is_valid_xccdf_id( |
| 25 | "xccdf_com.example.www_rule_selinux_state") |
| 26 | assert not autotailor.is_valid_xccdf_id("") |
| 27 | assert not autotailor.is_valid_xccdf_id("foo") |
| 28 | assert not autotailor.is_valid_xccdf_id( |
| 29 | "xccdf_com_example_www_rule_selinux_state") |
| 30 | assert not autotailor.is_valid_xccdf_id("xccdf_rule_selinux_state") |
| 31 | assert not autotailor.is_valid_xccdf_id("xccdf_com.example.www_rule_") |
| 32 | |
| 33 | |
| 34 | def test_full_id(): |
| 35 | p = autotailor.Profile() |
| 36 | assert p._full_rule_id("accounts_tmout") == \ |
| 37 | "xccdf_org.ssgproject.content_rule_accounts_tmout" |
| 38 | assert p._full_rule_id( |
| 39 | "xccdf_org.ssgproject.content_rule_accounts_tmout") == \ |
| 40 | "xccdf_org.ssgproject.content_rule_accounts_tmout" |
| 41 | assert p._full_profile_id("stig") == \ |
| 42 | "xccdf_org.ssgproject.content_profile_stig" |
| 43 | assert p._full_profile_id( |
| 44 | "xccdf_org.ssgproject.content_profile_stig") == \ |
| 45 | "xccdf_org.ssgproject.content_profile_stig" |
| 46 | assert p._full_var_id("var_crypto_policy") == \ |
| 47 | "xccdf_org.ssgproject.content_value_var_crypto_policy" |
| 48 | assert p._full_var_id( |
| 49 | "xccdf_org.ssgproject.content_value_var_crypto_policy") == \ |
| 50 | "xccdf_org.ssgproject.content_value_var_crypto_policy" |
| 51 | |
| 52 | |
| 53 | def test_customized_profile_id(): |
| 54 | p = autotailor.Profile() |
| 55 | p.extends = "stig" |
| 56 | assert p.profile_id == "stig_customized" |
| 57 | p.profile_id = "my_cool_profile" |
| 58 | assert p.profile_id == "my_cool_profile" |
| 59 | |
| 60 | |
| 61 | def test_refine_rule(): |
| 62 | p = autotailor.Profile() |
| 63 | with pytest.raises(ValueError) as e: |
| 64 | p.refine_rule("selinux_state", "severity", "high") |
| 65 | assert str(e.value) == "Rule id 'selinux_state' is invalid!" |
| 66 | with pytest.raises(ValueError) as e: |
| 67 | p.refine_rule( |
| 68 | "xccdf_org.ssgproject.content_rule_accounts_tmout", "foo", "bar") |
| 69 | assert str(e.value) == "Unsupported refine-rule attribute foo" |
| 70 | with pytest.raises(ValueError) as e: |
| 71 | p.refine_rule( |
| 72 | "xccdf_org.ssgproject.content_rule_accounts_tmout", |
| 73 | "role", "mnau") |
| 74 | assert str(e.value) == ( |
| 75 | "Can't refine role of rule 'xccdf_org.ssgproject.content_rule_accounts" |
| 76 | "_tmout' to 'mnau'. Allowed role values are: \"full\", \"unscored\", " |
| 77 | "\"unchecked\".") |
| 78 | with pytest.raises(ValueError) as e: |
| 79 | p.refine_rule( |
| 80 | "xccdf_org.ssgproject.content_rule_accounts_tmout", |
| 81 | "severity", "mnau") |
| 82 | assert str(e.value) == ( |
| 83 | "Can't refine severity of rule 'xccdf_org.ssgproject.content_rule_" |
| 84 | "accounts_tmout' to 'mnau'. Allowed severity values are: \"unknown\", " |
| 85 | "\"info\", \"low\", \"medium\", \"high\".") |
| 86 | fav = "xccdf_org.ssgproject.content_rule_accounts_tmout" |
| 87 | p.refine_rule(fav, "severity", "high") |
| 88 | assert p.rule_refinements(fav, "severity") == "high" |
| 89 | p.refine_rule(fav, "role", "full") |
| 90 | assert p.rule_refinements(fav, "severity") == "high" |
| 91 | assert p.rule_refinements(fav, "role") == "full" |
| 92 | with pytest.raises(ValueError) as e: |
| 93 | p.refine_rule(fav, "severity", "low") |
| 94 | assert str(e.value) == ( |
| 95 | "Can't refine severity of rule 'xccdf_org.ssgproject.content_rule_" |
| 96 | "accounts_tmout' to 'low'. This rule severity is already refined to " |
| 97 | "'high'.") |
| 98 | assert p.rule_refinements(fav, "severity") == "high" |
| 99 | assert p.rule_refinements(fav, "role") == "full" |
| 100 | |
| 101 | def test_no_id(): |
| 102 | p = autotailor.Profile() |
| 103 | profile_dict = None |
| 104 | file_path = pathlib.Path(__file__).parent.joinpath("custom_no_ids.json") |
| 105 | with open(file_path) as fp: |
| 106 | json_data = json.load(fp) |
| 107 | profile_dict = json_data["profiles"][0] |
| 108 | with pytest.raises(ValueError) as e: |
| 109 | p.import_json_tailoring_profile(profile_dict) |
| 110 | assert str(e.value) == "You must define a base_profile_id or an id" |