blob: 0fc0092da8c8236136dce516d34c41d6ba41e70b [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040017import unittest
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080018from xosgenx.generator import XOSProcessor
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040019from helpers import FakeArgs, XProtoTestHelpers
20import pdb
21
22"""The function below is for eliminating warnings arising due to the missing policy_output_validator,
23which is generated and loaded dynamically.
24"""
25def policy_output_validator(x, y):
26 raise Exception("Validator not generated. Test failed.")
27 return False
28
29"""
30The tests below use the Python code target to generate
31Python validation policies, set up an appropriate environment and execute the Python.
32"""
33class XProtoGeneralValidationTest(unittest.TestCase):
34 def setUp(self):
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040035 self.target = XProtoTestHelpers.write_tmp_target("""
36{% for name, policy in proto.policies.items() %}
37{{ xproto_fol_to_python_validator(name, policy, None, 'Necessary Failure') }}
38{% endfor %}
39""")
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040040
41 def test_constant(self):
42 xproto = \
43"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040044 policy output < False >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040045"""
46 args = FakeArgs()
47 args.inputs = xproto
48 args.target = self.target
49
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080050 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040051
52 exec(output) # This loads the generated function, which should look like this:
53
54 """
55 def policy_output_validator(obj, ctx):
56 i1 = False
57 if (not i1):
58 raise Exception('Necessary Failure')
59 """
60
61 with self.assertRaises(Exception):
62 policy_output_validator({}, {})
63
64 def test_equal(self):
65 xproto = \
66"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040067 policy output < not (ctx.user = obj.user) >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040068"""
69
70 args = FakeArgs()
71 args.inputs = xproto
72 args.target = self.target
73
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080074 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040075
76 exec(output) # This loads the generated function, which should look like this:
77
78 """
79 def policy_output_validator(obj, ctx):
80 i2 = (ctx.user == obj.user)
81 i1 = (not i2)
82 if (not i1):
83 raise Exception('Necessary Failure')
84 """
85
86 obj = FakeArgs()
87 obj.user = 1
88 ctx = FakeArgs()
89 ctx.user = 1
90
91 with self.assertRaises(Exception):
92 policy_output_validator(obj, ctx)
93
94 def test_equal(self):
95 xproto = \
96"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040097 policy output < not (ctx.user = obj.user) >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040098"""
99
100 args = FakeArgs()
101 args.inputs = xproto
102 args.target = self.target
103
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800104 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400105
106 exec(output) # This loads the generated function, which should look like this:
107
108 """
109 def policy_output_validator(obj, ctx):
110 i2 = (ctx.user == obj.user)
111 i1 = (not i2)
112 if (not i1):
113 raise Exception('Necessary Failure')
114 """
115
116 obj = FakeArgs()
117 obj.user = 1
118 ctx = FakeArgs()
119 ctx.user = 1
120
121 with self.assertRaises(Exception):
122 policy_output_validator(obj, ctx)
123
124 def test_bin(self):
125 xproto = \
126"""
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400127 policy output < (ctx.is_admin = True | obj.empty = True) | False>
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400128"""
129
130 args = FakeArgs()
131 args.inputs = xproto
132 args.target = self.target
133
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800134 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400135 exec(output) # This loads the generated function, which should look like this:
136
137 """
138 def policy_output_validator(obj, ctx):
139 i2 = (ctx.is_admin == True)
140 i3 = (obj.empty == True)
141 i1 = (i2 or i3)
142 if (not i1):
143 raise Exception('Necessary Failure')
144 """
145
146 obj = FakeArgs()
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400147 obj.empty = False
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400148
149 ctx = FakeArgs()
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400150 ctx.is_admin = False
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400151
152 with self.assertRaises(Exception):
153 verdict = policy_output_validator(obj, ctx)
154
155
156 def test_exists(self):
157 xproto = \
158"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400159 policy output < exists Privilege: Privilege.object_id = obj.id >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400160"""
161 args = FakeArgs()
162 args.inputs = xproto
163 args.target = self.target
164
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800165 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400166 exec(output) # This loads the generated function, which should look like this:
167
168 """
169 def policy_output_validator(obj, ctx):
170 i1 = Privilege.objects.filter(Q(object_id=obj.id))[0]
171 if (not i1):
172 raise Exception('Necessary Failure')
173 """
174
175 self.assertTrue(policy_output_validator is not None)
176
177 def test_python(self):
178 xproto = \
179"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400180 policy output < {{ "jack" in ["the", "box"] }} = True >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400181"""
182 args = FakeArgs()
183 args.inputs = xproto
184 args.target = self.target
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800185 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400186 exec(output) # This loads the generated function, which should look like this:
187
188 """
189 def policy_output_validator(obj, ctx):
190 i2 = ('jack' in ['the', 'box'])
191 i1 = (i2 == True)
192 if (not i1):
193 raise Exception('Necessary Failure')
194 """
195
196 with self.assertRaises(Exception):
197 self.assertTrue(policy_output_validator({}, {}) is True)
198
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400199 def test_call_policy(self):
200 xproto = \
201"""
202 policy sub_policy < ctx.user = obj.user >
203 policy output < *sub_policy(child) >
204"""
205
206 args = FakeArgs()
207 args.inputs = xproto
208 args.target = self.target
209
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800210 output = XOSProcessor.process(args)
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400211
212 exec(output,globals()) # This loads the generated function, which should look like this:
213
214 """
215 def policy_sub_policy_validator(obj, ctx):
216 i1 = (ctx.user == obj.user)
217 if (not i1):
218 raise ValidationError('Necessary Failure')
219
220 def policy_output_validator(obj, ctx):
221 i1 = policy_sub_policy_validator(obj.child, ctx)
222 if (not i1):
223 raise ValidationError('Necessary Failure')
224 """
225
226 obj = FakeArgs()
227 obj.child = FakeArgs()
228 obj.child.user = 1
229
230 ctx = FakeArgs()
231 ctx.user = 1
232
233 with self.assertRaises(Exception):
234 verdict = policy_output_enforcer(obj, ctx)
235
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400236 def test_forall(self):
237 # This one we only parse
238 xproto = \
239"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400240 policy output < forall Credential: Credential.obj_id = obj_id >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400241"""
242
243 args = FakeArgs()
244 args.inputs = xproto
245 args.target = self.target
246
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800247 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400248
249 """
250 def policy_output_enforcer(obj, ctx):
251 i2 = Credential.objects.filter((~ Q(obj_id=obj_id)))[0]
252 i1 = (not i2)
253 return i1
254 """
255
256 self.assertIn('policy_output_validator', output)
257
258if __name__ == '__main__':
259 unittest.main()