blob: 048167a1ab460760b8a0bc8907bff8ef5a2a0740 [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
Scott Baker1f7791d2018-10-04 13:21:20 -070018from xosgenx.generator import XOSProcessor, XOSProcessorArgs
19from helpers import XProtoTestHelpers, FakeObject
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040020import 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"""
Scott Baker1f7791d2018-10-04 13:21:20 -070046 args = XOSProcessorArgs(inputs = xproto,
47 target = self.target)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040048
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080049 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040050
51 exec(output) # This loads the generated function, which should look like this:
52
53 """
54 def policy_output_validator(obj, ctx):
55 i1 = False
56 if (not i1):
57 raise Exception('Necessary Failure')
58 """
59
60 with self.assertRaises(Exception):
61 policy_output_validator({}, {})
62
63 def test_equal(self):
64 xproto = \
65"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040066 policy output < not (ctx.user = obj.user) >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040067"""
68
Scott Baker1f7791d2018-10-04 13:21:20 -070069 args = XOSProcessorArgs(inputs = xproto,
70 target = self.target)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040071
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080072 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040073
74 exec(output) # This loads the generated function, which should look like this:
75
76 """
77 def policy_output_validator(obj, ctx):
78 i2 = (ctx.user == obj.user)
79 i1 = (not i2)
80 if (not i1):
81 raise Exception('Necessary Failure')
82 """
83
Scott Baker1f7791d2018-10-04 13:21:20 -070084 obj = FakeObject()
85 obj.user = 1
86 ctx = FakeObject()
87 ctx.user = 1
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040088
89 with self.assertRaises(Exception):
90 policy_output_validator(obj, ctx)
91
92 def test_equal(self):
93 xproto = \
94"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -040095 policy output < not (ctx.user = obj.user) >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040096"""
97
Scott Baker1f7791d2018-10-04 13:21:20 -070098 args = XOSProcessorArgs(inputs = xproto,
99 target = self.target)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400100
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800101 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400102
103 exec(output) # This loads the generated function, which should look like this:
104
105 """
106 def policy_output_validator(obj, ctx):
107 i2 = (ctx.user == obj.user)
108 i1 = (not i2)
109 if (not i1):
110 raise Exception('Necessary Failure')
111 """
112
Scott Baker1f7791d2018-10-04 13:21:20 -0700113 obj = FakeObject()
114 obj.user = 1
115 ctx = FakeObject()
116 ctx.user = 1
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400117
118 with self.assertRaises(Exception):
119 policy_output_validator(obj, ctx)
120
121 def test_bin(self):
122 xproto = \
123"""
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400124 policy output < (ctx.is_admin = True | obj.empty = True) | False>
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400125"""
126
Scott Baker1f7791d2018-10-04 13:21:20 -0700127 args = XOSProcessorArgs()
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400128 args.inputs = xproto
129 args.target = self.target
130
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800131 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400132 exec(output) # This loads the generated function, which should look like this:
133
134 """
135 def policy_output_validator(obj, ctx):
136 i2 = (ctx.is_admin == True)
137 i3 = (obj.empty == True)
138 i1 = (i2 or i3)
139 if (not i1):
140 raise Exception('Necessary Failure')
141 """
142
Scott Baker1f7791d2018-10-04 13:21:20 -0700143 obj = FakeObject()
144 obj.empty = False
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400145
Scott Baker1f7791d2018-10-04 13:21:20 -0700146 ctx = FakeObject()
147 ctx.is_admin = False
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400148
149 with self.assertRaises(Exception):
150 verdict = policy_output_validator(obj, ctx)
151
152
153 def test_exists(self):
154 xproto = \
155"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400156 policy output < exists Privilege: Privilege.object_id = obj.id >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400157"""
Scott Baker1f7791d2018-10-04 13:21:20 -0700158 args = XOSProcessorArgs(inputs = xproto,
159 target = self.target)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400160
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800161 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400162 exec(output) # This loads the generated function, which should look like this:
163
164 """
165 def policy_output_validator(obj, ctx):
166 i1 = Privilege.objects.filter(Q(object_id=obj.id))[0]
167 if (not i1):
168 raise Exception('Necessary Failure')
169 """
170
171 self.assertTrue(policy_output_validator is not None)
172
173 def test_python(self):
174 xproto = \
175"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400176 policy output < {{ "jack" in ["the", "box"] }} = True >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400177"""
Scott Baker1f7791d2018-10-04 13:21:20 -0700178 args = XOSProcessorArgs(inputs = xproto,
179 target = self.target)
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800180 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400181 exec(output) # This loads the generated function, which should look like this:
182
183 """
184 def policy_output_validator(obj, ctx):
185 i2 = ('jack' in ['the', 'box'])
186 i1 = (i2 == True)
187 if (not i1):
188 raise Exception('Necessary Failure')
189 """
190
191 with self.assertRaises(Exception):
192 self.assertTrue(policy_output_validator({}, {}) is True)
193
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400194 def test_call_policy(self):
195 xproto = \
196"""
197 policy sub_policy < ctx.user = obj.user >
198 policy output < *sub_policy(child) >
199"""
200
Scott Baker1f7791d2018-10-04 13:21:20 -0700201 args = XOSProcessorArgs(inputs = xproto,
202 target = self.target)
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400203
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800204 output = XOSProcessor.process(args)
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400205
206 exec(output,globals()) # This loads the generated function, which should look like this:
207
208 """
209 def policy_sub_policy_validator(obj, ctx):
210 i1 = (ctx.user == obj.user)
211 if (not i1):
212 raise ValidationError('Necessary Failure')
213
214 def policy_output_validator(obj, ctx):
215 i1 = policy_sub_policy_validator(obj.child, ctx)
216 if (not i1):
217 raise ValidationError('Necessary Failure')
218 """
219
Scott Baker1f7791d2018-10-04 13:21:20 -0700220 obj = FakeObject()
221 obj.child = FakeObject()
222 obj.child.user = 1
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400223
Scott Baker1f7791d2018-10-04 13:21:20 -0700224 ctx = FakeObject()
225 ctx.user = 1
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400226
227 with self.assertRaises(Exception):
228 verdict = policy_output_enforcer(obj, ctx)
229
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400230 def test_forall(self):
231 # This one we only parse
232 xproto = \
233"""
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400234 policy output < forall Credential: Credential.obj_id = obj_id >
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400235"""
236
Scott Baker1f7791d2018-10-04 13:21:20 -0700237 args = XOSProcessorArgs(inputs = xproto,
238 target = self.target)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400239
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800240 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400241
242 """
243 def policy_output_enforcer(obj, ctx):
244 i2 = Credential.objects.filter((~ Q(obj_id=obj_id)))[0]
245 i1 = (not i2)
246 return i1
247 """
248
249 self.assertIn('policy_output_validator', output)
250
251if __name__ == '__main__':
252 unittest.main()