blob: 15cdb562c07db7848308a4b4bcca3bdb2f952a6f [file] [log] [blame]
Sapan Bhatia5ea307d2017-07-19 00:13:21 -04001import unittest
2from xosgenx.generator import XOSGenerator
3from helpers import FakeArgs, XProtoTestHelpers
4import pdb
5
6"""The function below is for eliminating warnings arising due to the missing policy_output_validator,
7which is generated and loaded dynamically.
8"""
9def policy_output_validator(x, y):
10 raise Exception("Validator not generated. Test failed.")
11 return False
12
13"""
14The tests below use the Python code target to generate
15Python validation policies, set up an appropriate environment and execute the Python.
16"""
17class XProtoGeneralValidationTest(unittest.TestCase):
18 def setUp(self):
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040019 self.target = XProtoTestHelpers.write_tmp_target("""
20{% for name, policy in proto.policies.items() %}
21{{ xproto_fol_to_python_validator(name, policy, None, 'Necessary Failure') }}
22{% endfor %}
23""")
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040024
25 def test_constant(self):
26 xproto = \
27"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040028 policy output < False >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040029"""
30 args = FakeArgs()
31 args.inputs = xproto
32 args.target = self.target
33
34 output = XOSGenerator.generate(args)
35
36 exec(output) # This loads the generated function, which should look like this:
37
38 """
39 def policy_output_validator(obj, ctx):
40 i1 = False
41 if (not i1):
42 raise Exception('Necessary Failure')
43 """
44
45 with self.assertRaises(Exception):
46 policy_output_validator({}, {})
47
48 def test_equal(self):
49 xproto = \
50"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040051 policy output < not (ctx.user = obj.user) >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040052"""
53
54 args = FakeArgs()
55 args.inputs = xproto
56 args.target = self.target
57
58 output = XOSGenerator.generate(args)
59
60 exec(output) # This loads the generated function, which should look like this:
61
62 """
63 def policy_output_validator(obj, ctx):
64 i2 = (ctx.user == obj.user)
65 i1 = (not i2)
66 if (not i1):
67 raise Exception('Necessary Failure')
68 """
69
70 obj = FakeArgs()
71 obj.user = 1
72 ctx = FakeArgs()
73 ctx.user = 1
74
75 with self.assertRaises(Exception):
76 policy_output_validator(obj, ctx)
77
78 def test_equal(self):
79 xproto = \
80"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040081 policy output < not (ctx.user = obj.user) >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040082"""
83
84 args = FakeArgs()
85 args.inputs = xproto
86 args.target = self.target
87
88 output = XOSGenerator.generate(args)
89
90 exec(output) # This loads the generated function, which should look like this:
91
92 """
93 def policy_output_validator(obj, ctx):
94 i2 = (ctx.user == obj.user)
95 i1 = (not i2)
96 if (not i1):
97 raise Exception('Necessary Failure')
98 """
99
100 obj = FakeArgs()
101 obj.user = 1
102 ctx = FakeArgs()
103 ctx.user = 1
104
105 with self.assertRaises(Exception):
106 policy_output_validator(obj, ctx)
107
108 def test_bin(self):
109 xproto = \
110"""
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400111 policy output < (ctx.is_admin = True | obj.empty = True) | False>
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400112"""
113
114 args = FakeArgs()
115 args.inputs = xproto
116 args.target = self.target
117
118 output = XOSGenerator.generate(args)
119 exec(output) # This loads the generated function, which should look like this:
120
121 """
122 def policy_output_validator(obj, ctx):
123 i2 = (ctx.is_admin == True)
124 i3 = (obj.empty == True)
125 i1 = (i2 or i3)
126 if (not i1):
127 raise Exception('Necessary Failure')
128 """
129
130 obj = FakeArgs()
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400131 obj.empty = False
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400132
133 ctx = FakeArgs()
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400134 ctx.is_admin = False
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400135
136 with self.assertRaises(Exception):
137 verdict = policy_output_validator(obj, ctx)
138
139
140 def test_exists(self):
141 xproto = \
142"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400143 policy output < exists Privilege: Privilege.object_id = obj.id >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400144"""
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 i1 = Privilege.objects.filter(Q(object_id=obj.id))[0]
155 if (not i1):
156 raise Exception('Necessary Failure')
157 """
158
159 self.assertTrue(policy_output_validator is not None)
160
161 def test_python(self):
162 xproto = \
163"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400164 policy output < {{ "jack" in ["the", "box"] }} = True >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400165"""
166 args = FakeArgs()
167 args.inputs = xproto
168 args.target = self.target
169 output = XOSGenerator.generate(args)
170 exec(output) # This loads the generated function, which should look like this:
171
172 """
173 def policy_output_validator(obj, ctx):
174 i2 = ('jack' in ['the', 'box'])
175 i1 = (i2 == True)
176 if (not i1):
177 raise Exception('Necessary Failure')
178 """
179
180 with self.assertRaises(Exception):
181 self.assertTrue(policy_output_validator({}, {}) is True)
182
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400183 def test_call_policy(self):
184 xproto = \
185"""
186 policy sub_policy < ctx.user = obj.user >
187 policy output < *sub_policy(child) >
188"""
189
190 args = FakeArgs()
191 args.inputs = xproto
192 args.target = self.target
193
194 output = XOSGenerator.generate(args)
195
196 exec(output,globals()) # This loads the generated function, which should look like this:
197
198 """
199 def policy_sub_policy_validator(obj, ctx):
200 i1 = (ctx.user == obj.user)
201 if (not i1):
202 raise ValidationError('Necessary Failure')
203
204 def policy_output_validator(obj, ctx):
205 i1 = policy_sub_policy_validator(obj.child, ctx)
206 if (not i1):
207 raise ValidationError('Necessary Failure')
208 """
209
210 obj = FakeArgs()
211 obj.child = FakeArgs()
212 obj.child.user = 1
213
214 ctx = FakeArgs()
215 ctx.user = 1
216
217 with self.assertRaises(Exception):
218 verdict = policy_output_enforcer(obj, ctx)
219
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400220 def test_forall(self):
221 # This one we only parse
222 xproto = \
223"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400224 policy output < forall Credential: Credential.obj_id = obj_id >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400225"""
226
227 args = FakeArgs()
228 args.inputs = xproto
229 args.target = self.target
230
231 output = XOSGenerator.generate(args)
232
233 """
234 def policy_output_enforcer(obj, ctx):
235 i2 = Credential.objects.filter((~ Q(obj_id=obj_id)))[0]
236 i1 = (not i2)
237 return i1
238 """
239
240 self.assertIn('policy_output_validator', output)
241
242if __name__ == '__main__':
243 unittest.main()