blob: 20ba5da5ddd88db488cee340b08cc1d0836c8409 [file] [log] [blame]
Sapan Bhatia5ea307d2017-07-19 00:13:21 -04001import unittest
2from xosgenx.generator import XOSGenerator
3from helpers import FakeArgs, XProtoTestHelpers
4
5"""The function below is for eliminating warnings arising due to the missing policy_output_validator,
6which is generated and loaded dynamically.
7"""
8def policy_output_validator(x, y):
9 raise Exception("Validator not generated. Test failed.")
10 return False
11
12"""
13The tests below use the Python code target to generate
14Python validation policies, set up an appropriate environment and execute the Python.
15"""
16class XProtoXOSModelValidationTest(unittest.TestCase):
17 def setUp(self):
18 self.target = XProtoTestHelpers.write_tmp_target("{{ xproto_fol_to_python_validator('output', proto.policies.test_policy, None, 'Necessary Failure') }}")
19
20 def test_instance_container(self):
21 xproto = \
22"""
23 policy test_policy < (obj.isolation = "container" | obj.isolation = "container_vm" ) -> (obj.image.kind = "container") >
24"""
25 args = FakeArgs()
26 args.inputs = xproto
27 args.target = self.target
28
29 output = XOSGenerator.generate(args)
30
31 obj = FakeArgs()
32 obj.isolation = 'container'
33 obj.kind = 'not a container'
34
35 exec(output) # This loads the generated function, which should look like this:
36
37 """
38 def policy_output_validator(obj, ctx):
39 i4 = (obj.isolation == 'container')
40 i5 = (self.isolation == 'container_vm')
41 i2 = (i4 or i5)
42 i3 = (obj.image.kind == 'container')
43 i1 = (i2 or i3)
44 return i1
45 """
46
47 with self.assertRaises(Exception):
48 policy_output_validator(obj, {})
49
50 def test_slice_name_validation(self):
51 xproto = \
52"""
53 policy test_policy < not obj.id -> {{ obj.name.startswith(obj.site.login_base) }} >
54"""
55 args = FakeArgs()
56 args.inputs = xproto
57 args.target = self.target
58
59 output = XOSGenerator.generate(args)
60
61 obj = FakeArgs()
62 obj.isolation = 'container'
63 obj.kind = 'not a container'
64
65 exec(output) # This loads the generated function, which should look like this:
66
67 """
68 def policy_output_validator(obj, ctx):
69 i3 = obj.id
70 i4 = obj.name.startswith(obj.site.login_base)
71 i2 = ((not i3) or i4)
72 i1 = (not i2)
73 if (not i1):
74 raise ValidationError('Necessary Failure')
75 """
76
77 with self.assertRaises(Exception):
78 policy_output_validator(obj, {})
79 def test_equal(self):
80 xproto = \
81"""
82 policy test_policy < not (ctx.user = obj.user) >
83"""
84
85 args = FakeArgs()
86 args.inputs = xproto
87 args.target = self.target
88
89 output = XOSGenerator.generate(args)
90
91 exec(output) # This loads the generated function, which should look like this:
92
93 """
94 def policy_output_validator(obj, ctx):
95 i2 = (ctx.user == obj.user)
96 i1 = (not i2)
97 if (not i1):
98 raise Exception('Necessary Failure')
99 """
100
101 obj = FakeArgs()
102 obj.user = 1
103 ctx = FakeArgs()
104 ctx.user = 1
105
106 with self.assertRaises(Exception):
107 policy_output_validator(obj, ctx)
108
109 def test_equal(self):
110 xproto = \
111"""
112 policy test_policy < not (ctx.user = obj.user) >
113"""
114
115 args = FakeArgs()
116 args.inputs = xproto
117 args.target = self.target
118
119 output = XOSGenerator.generate(args)
120
121 exec(output) # This loads the generated function, which should look like this:
122
123 """
124 def policy_output_validator(obj, ctx):
125 i2 = (ctx.user == obj.user)
126 i1 = (not i2)
127 if (not i1):
128 raise Exception('Necessary Failure')
129 """
130
131 obj = FakeArgs()
132 obj.user = 1
133 ctx = FakeArgs()
134 ctx.user = 1
135
136 with self.assertRaises(Exception):
137 policy_output_validator(obj, ctx)
138
139 def test_bin(self):
140 xproto = \
141"""
142 policy test_policy < (ctx.is_admin = True | obj.empty = True) & False>
143"""
144
145 args = FakeArgs()
146 args.inputs = xproto
147 args.target = self.target
148
149 output = XOSGenerator.generate(args)
150 exec(output) # This loads the generated function, which should look like this:
151
152 """
153 def policy_output_validator(obj, ctx):
154 i2 = (ctx.is_admin == True)
155 i3 = (obj.empty == True)
156 i1 = (i2 or i3)
157 if (not i1):
158 raise Exception('Necessary Failure')
159 """
160
161 obj = FakeArgs()
162 obj.empty = True
163
164 ctx = FakeArgs()
165 ctx.is_admin = True
166
167 with self.assertRaises(Exception):
168 verdict = policy_output_validator(obj, ctx)
169
170
171 def test_exists(self):
172 xproto = \
173"""
174 policy test_policy < exists Privilege: Privilege.object_id = obj.id >
175"""
176 args = FakeArgs()
177 args.inputs = xproto
178 args.target = self.target
179
180 output = XOSGenerator.generate(args)
181 exec(output) # This loads the generated function, which should look like this:
182
183 """
184 def policy_output_validator(obj, ctx):
185 i1 = Privilege.objects.filter(Q(object_id=obj.id))[0]
186 if (not i1):
187 raise Exception('Necessary Failure')
188 """
189
190 self.assertTrue(policy_output_validator is not None)
191
192 def test_python(self):
193 xproto = \
194"""
195 policy test_policy < {{ "jack" in ["the", "box"] }} = True >
196"""
197 args = FakeArgs()
198 args.inputs = xproto
199 args.target = self.target
200 output = XOSGenerator.generate(args)
201 exec(output) # This loads the generated function, which should look like this:
202
203 """
204 def policy_output_validator(obj, ctx):
205 i2 = ('jack' in ['the', 'box'])
206 i1 = (i2 == True)
207 if (not i1):
208 raise Exception('Necessary Failure')
209 """
210
211 with self.assertRaises(Exception):
212 self.assertTrue(policy_output_validator({}, {}) is True)
213
214 def test_forall(self):
215 # This one we only parse
216 xproto = \
217"""
218 policy test_policy < forall Credential: Credential.obj_id = obj_id >
219"""
220
221 args = FakeArgs()
222 args.inputs = xproto
223 args.target = self.target
224
225 output = XOSGenerator.generate(args)
226
227 """
228 def policy_output_enforcer(obj, ctx):
229 i2 = Credential.objects.filter((~ Q(obj_id=obj_id)))[0]
230 i1 = (not i2)
231 return i1
232 """
233
234 self.assertIn('policy_output_validator', output)
235
236if __name__ == '__main__':
237 unittest.main()