Debian Codegraph @

tests/test_param_substitution.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_param_substitution.py
9
10 <Author>
11 Santiago Torres-Arias <[email protected]>
12
13 <Started>
14 May 15, 2018
15
16 <Copyright>
17 See LICENSE for licensing information.
18
19 <Purpose>
20 Test the parameter substitution functions within verifylib. These tests were
21 placed in a separate module to ease refactoring in case the substitution
22 layer is to be removed.
23
24 """
25
26 import os
27 import shutil
28 import unittest
29
30 from securesystemslib.interface import (
31 import_publickeys_from_file,
32 import_rsa_privatekey_from_file,
33 )
34 from securesystemslib.signer import SSlibSigner
35
36 import in_toto.exceptions
37 import in_toto.settings
38 from in_toto.models.layout import Layout
39 from in_toto.models.metadata import Envelope, Metablock
40 from in_toto.verifylib import in_toto_verify, substitute_parameters
41 from tests.common import TmpDirMixin
42
43
44 class Test_SubstituteArtifacts(unittest.TestCase):
45 """Test parameter substitution on artifact rules."""
46
47 def setUp(self):
48 self.layout = Layout.read(
49 {
50 "_type": "layout",
51 "inspect": [
52 {
53 "name": "do-the-thing",
54 "expected_materials": [
55 [
56 "MATCH",
57 "{SOURCE_THING}",
58 "WITH",
59 "MATERIALS",
60 "FROM",
61 "{SOURCE_STEP}",
62 ]
63 ],
64 "expected_products": [["CREATE", "{NEW_THING}"]],
65 }
66 ],
67 "steps": [
68 {
69 "name": "artifacts",
70 "expected_command": [],
71 "expected_materials": [
72 [
73 "MATCH",
74 "{SOURCE_THING}",
75 "WITH",
76 "MATERIALS",
77 "FROM",
78 "{SOURCE_STEP}",
79 ]
80 ],
81 "expected_products": [["CREATE", "{NEW_THING}"]],
82 }
83 ],
84 }
85 )
86
87 def test_substitute(self):
88 """Do a simple substitution on the expected_command field"""
89 substitute_parameters(
90 self.layout,
91 {
92 "SOURCE_THING": "vim",
93 "SOURCE_STEP": "source_step",
94 "NEW_THING": "new_thing",
95 },
96 )
97 self.assertEqual(self.layout.steps[0].expected_materials[0][1], "vim")
98 self.assertEqual(
99 self.layout.steps[0].expected_materials[0][5], "source_step"
100 )
101 self.assertEqual(
102 self.layout.steps[0].expected_products[0][1], "new_thing"
103 )
104 self.assertEqual(self.layout.inspect[0].expected_materials[0][1], "vim")
105 self.assertEqual(
106 self.layout.inspect[0].expected_materials[0][5], "source_step"
107 )
108 self.assertEqual(
109 self.layout.inspect[0].expected_products[0][1], "new_thing"
110 )
111
112 def test_substitute_no_var(self):
113 """Raise an error if the parameter is not filled-in"""
114 with self.assertRaises(KeyError):
115 substitute_parameters(self.layout, {})
116
117
118 class Test_SubstituteRunField(unittest.TestCase):
119 """Test substitution on the run field of the layout"""
120
121 def setUp(self):
122 """
123 Create layout with dummy inspection
124 """
125
126 # Create layout with one inspection
127 self.layout = Layout.read(
128 {
129 "_type": "layout",
130 "steps": [],
131 "inspect": [
132 {
133 "name": "run-command",
134 "run": ["{COMMAND}"],
135 }
136 ],
137 }
138 )
139
140 def test_substitute(self):
141 """Check that the substitution is performed on the run field."""
142 substitute_parameters(self.layout, {"COMMAND": "touch"})
143 self.assertEqual(self.layout.inspect[0].run[0], "touch")
144
145 def test_inspection_fail_with_non_zero_retval(self):
146 """Check that the substitution raises TypeError if the key is missing"""
147
148 with self.assertRaises(KeyError):
149 substitute_parameters(self.layout, {})
150
151
152 class Test_SubstituteExpectedCommand(unittest.TestCase):
153 """Test verifylib.verify_command_alignment(command, expected_command)"""
154
155 def setUp(self):
156 # Create layout with one inspection
157 self.layout = Layout.read(
158 {
159 "_type": "layout",
160 "inspect": [],
161 "steps": [
162 {
163 "name": "run-command",
164 "expected_command": ["{EDITOR}"],
165 }
166 ],
167 }
168 )
169
170 def test_substitute(self):
171 """Do a simple substitution on the expected_command field"""
172 substitute_parameters(self.layout, {"EDITOR": "vim"})
173 self.assertEqual(self.layout.steps[0].expected_command[0], "vim")
174
175 def test_substitute_no_var(self):
176 """Raise an error if the parameter is not filled-in"""
177
178 with self.assertRaises(KeyError):
179 substitute_parameters(self.layout, {"NOEDITOR": "vim"})
180
181
182 class Test_SubstituteOnVerify(unittest.TestCase, TmpDirMixin):
183 """Test verifylib.verify_command_alignment(command, expected_command)"""
184
185 @classmethod
186 def setUpClass(self):
187 # Create layout with one inspection
188 self.layout = Layout.read(
189 {
190 "_type": "layout",
191 "inspect": [],
192 "steps": [
193 {
194 "name": "run-command",
195 "expected_command": ["{EDITOR}"],
196 }
197 ],
198 }
199 )
200
201 # Find demo files
202 demo_files = os.path.join(
203 os.path.dirname(os.path.realpath(__file__)), "demo_files"
204 )
205
206 self.set_up_test_dir()
207
208 # Copy demo files to temp dir
209 for fn in os.listdir(demo_files):
210 shutil.copy(os.path.join(demo_files, fn), self.test_dir)
211
212 # load alice's key
213 self.alice = import_rsa_privatekey_from_file("alice")
214 self.alice_pub_dict = import_publickeys_from_file(["alice.pub"])
215
216 @classmethod
217 def tearDownClass(self):
218 self.tear_down_test_dir()
219
220 def test_substitute(self):
221 """Do a simple substitution on the expected_command field"""
222 signed_layout = Metablock(signed=self.layout)
223 signed_layout.sign(self.alice)
224
225 # we will catch a LinkNotFound error because we don't have (and don't need)
226 # the metadata.
227 with self.assertRaises(in_toto.exceptions.LinkNotFoundError):
228 in_toto_verify(
229 signed_layout,
230 self.alice_pub_dict,
231 substitution_parameters={"EDITOR": "vim"},
232 )
233
234 self.assertEqual(self.layout.steps[0].expected_command[0], "vim")
235
236 def test_substitute_for_envelope(self):
237 """Do a simple substitution on the expected_command field for DSSE
238 envelope."""
239 signed_layout = Envelope.from_signable(self.layout)
240 signed_layout.create_signature(SSlibSigner(self.alice))
241
242 # we will catch a LinkNotFound error because we don't have (and don't need)
243 # the metadata.
244 with self.assertRaises(in_toto.exceptions.LinkNotFoundError):
245 in_toto_verify(
246 signed_layout,
247 self.alice_pub_dict,
248 substitution_parameters={"EDITOR": "vim"},
249 )
250
251 self.assertEqual(self.layout.steps[0].expected_command[0], "vim")
252
253
254 if __name__ == "__main__":
255 unittest.main()