Debian Codegraph @

tests/bindings/python/xccdf_policy.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 # Author:
4 # Dominique Blaze <[email protected]>
5 #
6
7 import os
8 from import_handler import oscap, result2str, get_path
9
10 ''' Story
11
12 1) ======= import benchmark with xccdf.init (and not import_benchmark) ====
13
14 Benchmark id should be xccdf_com.example.www_benchmark_test
15 Profile should contains : xccdf_com.example.www_profile_1
16
17
18 2) ===== Testing XCCDF_POLICY ========
19
20 When using this profile, get_tailoring_items() should returns this.
21 But instead of testing exactly this result (which will probably be improved
22 we only test expected required features, for instance it's better to check
23 if a feature is included (in order the test pass if more features are added later
24
25 [{'choices': {},
26 'descs': {None: ''},
27 'id': 'xccdf_com.example.www_value_1',
28 'lang': None,
29 'langs': {None, 'en-US'},
30 'match': '^.*$',
31 'options': {'dhcp_service': 'dhcpd',
32 'ftp_service': 'tftpd',
33 'telnet_service': 'telnet-server'},
34 'selected': ('telnet_service', 'telnet-server'),
35 'titles': {None: '',
36 'en-US': 'Which package is installed ? '
37 '(telnet-server/dhcpd/tftpd) '},
38 'type': 2}]
39
40 3) ====== Testing values ========
41
42
43 Expected items in list returned by get_all_values():
44 first value id should be : xccdf_com.example.www_value_1 -----
45 with title (en-us) = Which package is installed ? (telnet-server/dhcpd/tftpd)
46
47 4) ======== Refining some values and ensure it's working ======
48
49 TODO : export the tailored file, re-import it, check the modifications
50 and ensure that the initial file isn't modified.
51
52 '''
53
54 # ====================== Part 1 ============================
55
56 print("opening " + get_path("samples/redhat_bench_example/benchmark_example_redhat.xml") + " ...")
57 benchmark_components = oscap.xccdf.init(get_path("samples/redhat_bench_example/"
58 "benchmark_example_redhat.xml"))
59
60 pm = benchmark_components['policy_model']
61 benchmark = pm.get_benchmark()
62 expected_benchmark_id = "xccdf_com.example.www_benchmark_test"
63 if benchmark.get_id() != expected_benchmark_id:
64 raise Exception("Benchmark id should be {0} but is currently {1}"
65 .format(expected_benchmark_id, benchmark.get_id()))
66
67 print("Browsing profiles ...")
68 profiles = set()
69 for p in pm.get_benchmark().get_profiles():
70 print("\t" + p.get_id())
71 profiles.add(p.get_id())
72
73 if 'xccdf_com.example.www_profile_1' not in profiles:
74 raise Exception("Profile xccdf_com.example.www_profile_1 should be present in get_profiles"
75 "but haven't be found. Current profiles : {0}".format(', '.join(profiles)))
76
77 profile = pm.get_benchmark().get_profile_by_id("xccdf_com.example.www_profile_1")
78 print("selected profile : " + profile.get_id())
79
80 profile_not_defined = pm.get_benchmark().get_profile_by_id("xccdf_com.example.profile_not_exists")
81 if profile_not_defined is not None:
82 raise Exception("get_profile_by_id('xccdf_com.example.profile_not_exists') should returns "
83 "None but returned value is {0}".format(profile_not_defined))
84
85 # ====================== Part 2 ============================
86
87 policy = oscap.xccdf.policy_new(pm, profile)
88
89 if "'xccdf_policy'" not in policy.__repr__():
90 raise Exception("Variable policy should be a swig object of type 'xccdf_policy'. "
91 "Current object representation : {0}".format(policy.__repr__()))
92
93
94 # ====================== Part 3 ============================
95
96
97 tailor_items = policy.get_tailor_items()
98
99 expected_val1_id = 'xccdf_com.example.www_value_1'
100 expected_val1_options = {'dhcp_service': 'dhcpd',
101 'ftp_service': 'tftpd',
102 'telnet_service': 'telnet-server'}
103
104 expected_val1_selected = ('telnet_service', 'telnet-server')
105
106 val1 = tailor_items[0]
107 print("First tailored value raw_content: {0}".format(val1))
108
109 if val1['id'] != expected_val1_id:
110 raise Exception("Id of first tailored value should be {0} but is {1}"
111 .format(expected_val1_id, val1["id"]))
112
113 if val1['options'] != expected_val1_options:
114 raise Exception("Options of tailored value {0} should be {1} but is {2}"
115 .format(val1['id'], expected_val1_options, val1["options"]))
116
117
118 if val1['selected'] != expected_val1_selected:
119 raise Exception("Selected option of tailored value {0} should be {1} but is {2}"
120 .format(val1['id'], expected_val1_selected, val1["selected"]))
121
122 if 'en-US' not in val1['langs']:
123 raise Exception("Available langs of tailored value {0} should contains en-US but is {1}"
124 .format(val1['id'], ', '.join(val1["langs"])))
125
126 if val1['match'] != '^.*$':
127 raise Exception("Match propety of tailored value {0} should be '^.*$' but is {1}"
128 .format(val1['id'], val1["match"]))
129
130 if int(val1['type']) != 2:
131 raise Exception("Type of tailored value {0} should be '2' but is {1}"
132 .format(val1['type'], val1["type"]))
133
134 print("All retrievied values seems OK (id, options, selected option, lang, match, type)")
135
136 # ====================== Part 4 ============================
137
138
139 ''' Should replace the selected option with ('ftp_service', 'tftpd') '''
140 new_tailored_value = {'id': expected_val1_id,
141 'value': 'tftpd'
142 }
143
144 new_expected_selected_value = ()
145
146 policy.set_tailor_items([new_tailored_value])
147 val1bis = policy.get_tailor_items()
148
149 if val1bis[0]['selected'] != ('ftp_service', 'tftpd'):
150 raise Exception("After setting of the default value {0}, its selected option "
151 "should be ('ftp_service', 'tftpd') but is currently {1}"
152 .format(expected_val1_id, val1bis['selected']))
153
154 print("Default value refining (set_tailoring_item) seems to work fine.")