Debian Codegraph @

tests/test_rulelib.py

raw
1 #!/usr/bin/env python
2
3 # Copyright New York University and the in-toto contributors
4 # SPDX-License-Identifier: Apache-2.0
5
6 """
7 <Program Name>
8 test_rulelib.py
9
10 <Author>
11 Santiago Torres-Arias <[email protected]>
12 Lukas Puehringer <[email protected]>
13
14 <Started>
15 Nov 19, 2016
16
17 <Copyright>
18 See LICENSE for licensing information.
19
20 <Purpose>
21 Test artifact rule packing and unpacking.
22
23 """
24 import unittest
25
26 import securesystemslib.exceptions
27
28 from in_toto.rulelib import (
29 pack_allow_rule,
30 pack_create_rule,
31 pack_delete_rule,
32 pack_disallow_rule,
33 pack_modify_rule,
34 pack_require_rule,
35 pack_rule,
36 pack_rule_data,
37 unpack_rule,
38 )
39
40
41 class TestArtifactRuleUnpack(unittest.TestCase):
42 """Test artifact rule unpacker/syntax checker."""
43
44 def test_unpack_rule_not_list(self):
45 """Test rule syntax error, not a list."""
46
47 rule = "CREATE stuff"
48 with self.assertRaises(securesystemslib.exceptions.FormatError):
49 unpack_rule(rule)
50
51 def test_unpack_rule_not_enough_keywords(self):
52 """Test rule syntax error, too little arguments."""
53 rule = ["DELETE"]
54 with self.assertRaises(securesystemslib.exceptions.FormatError):
55 unpack_rule(rule)
56
57 def test_unpack_rule_unknown_rule_type(self):
58 """Test generic rule syntax error, too many arguments."""
59 rule = ["SUBVERT", "foo"]
60 with self.assertRaises(securesystemslib.exceptions.FormatError):
61 unpack_rule(rule)
62
63 def test_unpack_rule_pattern_not_string(self):
64 """Test rule syntax error, pattern not a string."""
65 with self.assertRaises(securesystemslib.exceptions.FormatError):
66 rule = ["CREATE", {"abc"}]
67 unpack_rule(rule)
68
69 def test_unpack_generic_rule_too_long(self):
70 """Test generic rule syntax error, too many arguments."""
71 rule = ["CREATE", "foo", "pleaze!!"]
72 with self.assertRaises(securesystemslib.exceptions.FormatError):
73 unpack_rule(rule)
74
75 def test_unpack_and_pack_generic_rule(self):
76 """Test generic rule proper packing and unpacking."""
77 rule = ["CREATE", "foo"]
78 rule_data = unpack_rule(rule)
79 self.assertEqual(len(list(rule_data.keys())), 2)
80 self.assertEqual(rule_data["rule_type"], "create")
81 self.assertEqual(rule_data["pattern"], "foo")
82
83 self.assertEqual(rule, pack_rule_data(rule_data))
84 self.assertEqual(rule, pack_create_rule("foo"))
85
86 rule = ["DELETE", "foo"]
87 rule_data = unpack_rule(rule)
88 self.assertEqual(len(list(rule_data.keys())), 2)
89 self.assertEqual(rule_data["rule_type"], "delete")
90 self.assertEqual(rule_data["pattern"], "foo")
91
92 self.assertEqual(rule, pack_rule_data(rule_data))
93 self.assertEqual(rule, pack_delete_rule("foo"))
94
95 rule = ["MODIFY", "foo"]
96 rule_data = unpack_rule(rule)
97 self.assertEqual(len(list(rule_data.keys())), 2)
98 self.assertEqual(rule_data["rule_type"], "modify")
99 self.assertEqual(rule_data["pattern"], "foo")
100
101 self.assertEqual(rule, pack_rule_data(rule_data))
102 self.assertEqual(rule, pack_modify_rule("foo"))
103
104 rule = ["ALLOW", "foo"]
105 rule_data = unpack_rule(rule)
106 self.assertEqual(len(list(rule_data.keys())), 2)
107 self.assertEqual(rule_data["rule_type"], "allow")
108 self.assertEqual(rule_data["pattern"], "foo")
109
110 self.assertEqual(rule, pack_rule_data(rule_data))
111 self.assertEqual(rule, pack_allow_rule("foo"))
112
113 rule = ["DISALLOW", "foo"]
114 rule_data = unpack_rule(rule)
115 self.assertEqual(len(list(rule_data.keys())), 2)
116 self.assertEqual(rule_data["rule_type"], "disallow")
117 self.assertEqual(rule_data["pattern"], "foo")
118
119 self.assertEqual(rule, pack_rule_data(rule_data))
120 self.assertEqual(rule, pack_disallow_rule("foo"))
121
122 rule = ["REQUIRE", "foo"]
123 rule_data = unpack_rule(rule)
124 self.assertEqual(len(list(rule_data.keys())), 2)
125 self.assertEqual(rule_data["rule_type"], "require")
126 self.assertEqual(rule_data["pattern"], "foo")
127
128 self.assertEqual(rule, pack_rule_data(rule_data))
129 self.assertEqual(rule, pack_require_rule("foo"))
130
131 def test_unpack_and_pack_match_rule(self):
132 """Check match rule proper packing and unpacking."""
133
134 rule = [
135 "MATCH",
136 "foo",
137 "IN",
138 "source-path",
139 "WITH",
140 "PRODUCTS",
141 "IN",
142 "dest-path",
143 "FROM",
144 "step-name",
145 ]
146 rule_data = unpack_rule(rule)
147 self.assertEqual(len(list(rule_data.keys())), 6)
148 self.assertEqual(rule_data["rule_type"], "match")
149 self.assertEqual(rule_data["pattern"], "foo")
150 self.assertEqual(rule_data["source_prefix"], "source-path")
151 self.assertEqual(rule_data["dest_prefix"], "dest-path")
152 self.assertEqual(rule_data["dest_type"], "products")
153 self.assertEqual(rule_data["dest_name"], "step-name")
154
155 self.assertEqual(rule, pack_rule_data(rule_data))
156 self.assertEqual(
157 rule,
158 pack_rule(
159 "MATCH",
160 "foo",
161 source_prefix="source-path",
162 dest_type="PRODUCTS",
163 dest_prefix="dest-path",
164 dest_name="step-name",
165 ),
166 )
167
168 rule = [
169 "MATCH",
170 "foo",
171 "IN",
172 "source-path",
173 "WITH",
174 "MATERIALS",
175 "FROM",
176 "step-name",
177 ]
178 rule_data = unpack_rule(rule)
179 self.assertEqual(len(list(rule_data.keys())), 6)
180 self.assertEqual(rule_data["rule_type"], "match")
181 self.assertEqual(rule_data["pattern"], "foo")
182 self.assertEqual(rule_data["source_prefix"], "source-path")
183 self.assertEqual(rule_data["dest_prefix"], "")
184 self.assertEqual(rule_data["dest_type"], "materials")
185 self.assertEqual(rule_data["dest_name"], "step-name")
186
187 self.assertEqual(rule, pack_rule_data(rule_data))
188 self.assertEqual(
189 rule,
190 pack_rule(
191 "MATCH",
192 "foo",
193 source_prefix="source-path",
194 dest_type="MATERIALS",
195 dest_name="step-name",
196 ),
197 )
198
199 rule = [
200 "MATCH",
201 "foo",
202 "WITH",
203 "PRODUCTS",
204 "IN",
205 "dest-path",
206 "FROM",
207 "step-name",
208 ]
209 rule_data = unpack_rule(rule)
210 self.assertEqual(len(list(rule_data.keys())), 6)
211 self.assertEqual(rule_data["rule_type"], "match")
212 self.assertEqual(rule_data["pattern"], "foo")
213 self.assertEqual(rule_data["source_prefix"], "")
214 self.assertEqual(rule_data["dest_prefix"], "dest-path")
215 self.assertEqual(rule_data["dest_type"], "products")
216 self.assertEqual(rule_data["dest_name"], "step-name")
217
218 self.assertEqual(rule, pack_rule_data(rule_data))
219 self.assertEqual(
220 rule,
221 pack_rule(
222 "MATCH",
223 "foo",
224 dest_type="PRODUCTS",
225 dest_prefix="dest-path",
226 dest_name="step-name",
227 ),
228 )
229
230 rule = ["MATCH", "foo", "WITH", "PRODUCTS", "FROM", "step-name"]
231 rule_data = unpack_rule(rule)
232 self.assertEqual(len(list(rule_data.keys())), 6)
233 self.assertEqual(rule_data["rule_type"], "match")
234 self.assertEqual(rule_data["pattern"], "foo")
235 self.assertEqual(rule_data["source_prefix"], "")
236 self.assertEqual(rule_data["dest_prefix"], "")
237 self.assertEqual(rule_data["dest_type"], "products")
238 self.assertEqual(rule_data["dest_name"], "step-name")
239
240 self.assertEqual(rule, pack_rule_data(rule_data))
241 self.assertEqual(
242 rule,
243 pack_rule(
244 "MATCH", "foo", dest_type="PRODUCTS", dest_name="step-name"
245 ),
246 )
247
248 def test_pack_rule_wrong_types(self):
249 """Test argument validation for pack_rule."""
250 # pattern must be a string
251 with self.assertRaises(securesystemslib.exceptions.FormatError):
252 pack_rule("match", None)
253
254 # rule_type must be a string...
255 with self.assertRaises(securesystemslib.exceptions.FormatError):
256 pack_rule(1, "foo")
257
258 # ... and one of the allowed rule types
259 with self.assertRaises(securesystemslib.exceptions.FormatError):
260 pack_rule("not-a-rule-type", "foo")
261
262 # For match rules a dest_type must be passed ...
263 with self.assertRaises(securesystemslib.exceptions.FormatError):
264 pack_rule("match", "foo", dest_name="step-name")
265
266 # ... and be one of materials or products
267 with self.assertRaises(securesystemslib.exceptions.FormatError):
268 pack_rule(
269 "match",
270 "foo",
271 dest_type="not-a-dest-type",
272 dest_name="step-name",
273 )
274
275 # For match rules dest_name must be a string ...
276 with self.assertRaises(securesystemslib.exceptions.FormatError):
277 pack_rule("match", "foo", dest_type="materials", dest_name=1)
278
279 # ... and non-empty
280 with self.assertRaises(securesystemslib.exceptions.FormatError):
281 pack_rule("match", "foo", dest_type="materials", dest_name="")
282
283 # For match rules, if a source_prefix is passed it must be a string
284 with self.assertRaises(securesystemslib.exceptions.FormatError):
285 pack_rule(
286 "match",
287 "foo",
288 source_prefix=1,
289 dest_type="products",
290 dest_prefix="dest-path",
291 dest_name="step-name",
292 )
293
294 # For match rules, if a dest_prefix is passed it must be a string
295 with self.assertRaises(securesystemslib.exceptions.FormatError):
296 pack_rule(
297 "match",
298 "foo",
299 dest_type="products",
300 dest_prefix=["not-a-string"],
301 dest_name="step-name",
302 )
303
304 def test_unpack_match_rule_wrong_length(self):
305 """Check match rule syntax error, too few or many arguments."""
306
307 rule = ["MATCH", "foo", "WITH", "PRODUCTS", "FROM"]
308 with self.assertRaises(securesystemslib.exceptions.FormatError):
309 unpack_rule(rule)
310
311 rule = [
312 "MATCH",
313 "foo",
314 "WITH",
315 "PRODUCTS",
316 "FROM",
317 "step-name",
318 "really?",
319 ]
320 with self.assertRaises(securesystemslib.exceptions.FormatError):
321 unpack_rule(rule)
322
323 rule = [
324 "MATCH",
325 "foo",
326 "IN",
327 "source-path",
328 "WITH",
329 "PRODUCTS",
330 "IN",
331 "dest-path",
332 "FROM",
333 "step-name",
334 "YES, we can!",
335 ]
336 with self.assertRaises(securesystemslib.exceptions.FormatError):
337 unpack_rule(rule)
338
339 def test_unpack_match_rule_wrong_types(self):
340 """Check match rule syntax error, wrong data type in variable arguments."""
341 # pattern must be string
342 rule = [
343 "MATCH",
344 ["abc"],
345 "IN",
346 "source-path",
347 "WITH",
348 "PRODUCTS",
349 "IN",
350 "dest-path",
351 "FROM",
352 "step-name",
353 ]
354 with self.assertRaises(securesystemslib.exceptions.FormatError):
355 unpack_rule(rule)
356
357 # source path must be a string
358 rule = [
359 "MATCH",
360 "foo",
361 "IN",
362 {"abc": "def"},
363 "WITH",
364 "PRODUCTS",
365 "IN",
366 "dest-path",
367 "FROM",
368 "step-name",
369 ]
370 with self.assertRaises(securesystemslib.exceptions.FormatError):
371 unpack_rule(rule)
372
373 # dest-path must be a string
374 rule = [
375 "MATCH",
376 "foo",
377 "IN",
378 "source-path",
379 "WITH",
380 "PRODUCTS",
381 "IN",
382 123,
383 "FROM",
384 "step-name",
385 ]
386 with self.assertRaises(securesystemslib.exceptions.FormatError):
387 unpack_rule(rule)
388
389 # step-name must be a string
390 rule = [
391 "MATCH",
392 "foo",
393 "IN",
394 "source-path",
395 "WITH",
396 "PRODUCTS",
397 "IN",
398 "dest-path",
399 "FROM",
400 ("456",),
401 ]
402 with self.assertRaises(securesystemslib.exceptions.FormatError):
403 unpack_rule(rule)
404
405 def test_unpack_match_rule_wrong_destination_type(self):
406 """Check match rule syntax error, wrong destination type."""
407 rule = [
408 "MATCH",
409 "foo",
410 "IN",
411 "source-path",
412 "WITH",
413 "PONIES",
414 "IN",
415 "dest-path",
416 "FROM",
417 "step-name",
418 ]
419 with self.assertRaises(securesystemslib.exceptions.FormatError):
420 unpack_rule(rule)
421
422
423 if __name__ == "__main__":
424 unittest.main()