blob: 9e77976a730affd3d46728d615d1799b425cfb36 [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 Bhatiadb183c22017-06-23 02:47:42 -070017import unittest
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080018from xosgenx.generator import XOSProcessor
Sapan Bhatiadb183c22017-06-23 02:47:42 -070019from helpers import FakeArgs, XProtoTestHelpers
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040020import pdb
Sapan Bhatiadb183c22017-06-23 02:47:42 -070021
22"""
23The tests below convert the policy logic expression
24into Python, set up an appropriate environment and execute the Python.
25"""
26
27class XProtoPolicyTest(unittest.TestCase):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040028 def test_annotation(self):
29 xproto = \
30"""
31 policy true_policy < True >
32
33 message always::true_policy {
34 required int still = 9;
35 }
36"""
37
38 target = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0 }}")
39
40 args = FakeArgs()
41 args.inputs = xproto
42 args.target = target
43
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080044 output = XOSProcessor.process(args)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040045 self.assertIn("true_policy", output)
46
Sapan Bhatiadb183c22017-06-23 02:47:42 -070047 def test_constant(self):
48 xproto = \
49"""
50 policy true_policy < True >
51"""
52
53 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.true_policy }}")
54
55 args = FakeArgs()
56 args.inputs = xproto
57 args.target = target
58
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080059 output = XOSProcessor.process(args).replace('t','T')
Sapan Bhatiadb183c22017-06-23 02:47:42 -070060 self.assertTrue(eval(output))
61
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040062 def test_function_term(self):
63 xproto = \
64"""
65 policy slice_user < slice.user.compute_is_admin() >
66"""
67
68 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}")
69 args = FakeArgs()
70 args.inputs = xproto
71 args.target = target
72
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080073 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040074
75 slice = FakeArgs()
76 slice.user = FakeArgs()
77 slice.user.compute_is_admin = lambda: True
78
79 expr = eval(output)
80 self.assertTrue(expr)
81
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040082 def test_term(self):
83 xproto = \
84"""
85 policy slice_user < slice.user.is_admin >
86"""
87
88 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}")
89 args = FakeArgs()
90 args.inputs = xproto
91 args.target = target
92
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080093 output = XOSProcessor.process(args)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040094
95 slice = FakeArgs()
96 slice.user = FakeArgs()
97 slice.user.is_admin = True
98
99 expr = eval(output)
100 self.assertTrue(expr)
101
102 def test_num_constant(self):
103 xproto = \
104"""
105 policy slice_user < slice.user.age = 57 >
106"""
107
108 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}")
109 args = FakeArgs()
110 args.inputs = xproto
111 args.target = target
112
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800113 output = XOSProcessor.process(args)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400114
115 slice = FakeArgs()
116 slice.user = FakeArgs()
117 slice.user.is_admin = True
118
119 expr = eval(output)
120 self.assertTrue(expr)
121
122 def test_string_constant(self):
123 xproto = \
124"""
125 policy slice_user < slice.user.email = "admin@opencord.org" >
126"""
127
128 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}")
129 args = FakeArgs()
130 args.inputs = xproto
131 args.target = target
132
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800133 output = XOSProcessor.process(args)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400134
135 slice = FakeArgs()
136 slice.user = FakeArgs()
137 slice.user.is_admin = True
138
139 expr = eval(output)
140 self.assertTrue(expr)
141
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700142 def test_equal(self):
143 xproto = \
144"""
145 policy slice_user < slice.user = obj.user >
146"""
147
148 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}")
149 args = FakeArgs()
150 args.inputs = xproto
151 args.target = target
152
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800153 output = XOSProcessor.process(args)
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700154
155 slice = FakeArgs()
156 slice.user = 'twin'
157 obj = FakeArgs()
158 obj.user = 'twin'
159
160 (op, operands), = eval(output).items()
161 expr = op.join(operands).replace('=','==')
162
163 self.assertTrue(eval(expr))
164
165 def test_bin(self):
166 xproto = \
167"""
168 policy slice_admin < slice.is_admin | obj.empty >
169"""
170 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_admin }}")
171 args = FakeArgs()
172 args.inputs = xproto
173 args.target = target
174
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800175 output = XOSProcessor.process(args)
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700176
177 slice = FakeArgs()
178 slice.is_admin = False
179 obj = FakeArgs()
180 obj.empty = []
181
182 (op, operands), = eval(output).items()
183 expr = op.join(operands).replace('|',' or ')
184
185 self.assertFalse(eval(expr))
186
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400187 def test_implies(self):
188 xproto = \
189"""
190 policy implies < obj.name -> obj.creator >
191"""
192 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.implies }}")
193 args = FakeArgs()
194 args.inputs = xproto
195 args.target = target
196
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800197 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400198
199 slice = FakeArgs()
200 slice.is_admin = False
201 obj = FakeArgs()
202 obj.name = 'Thing 1'
203 obj.creator = None
204
205 (op, operands), = eval(output).items()
206 expr = 'not ' + op.join(operands).replace('->',' or ')
207
208 self.assertFalse(eval(expr))
209
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700210 def test_exists(self):
211 xproto = \
212"""
213 policy privilege < exists Privilege: Privilege.object_id = obj.id >
214"""
215
216 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.privilege }} ")
217 args = FakeArgs()
218 args.inputs = xproto
219 args.target = target
220
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800221 output = XOSProcessor.process(args)
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700222
223 Privilege = FakeArgs()
224 Privilege.object_id = 1
225 obj = FakeArgs()
226 obj.id = 1
227
228 (op, operands), = eval(output).items()
229 (op2, operands2), = operands[1].items()
230 expr = op2.join(operands2).replace('=','==')
231
232 self.assertTrue(eval(expr))
233
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400234 def test_policy_function(self):
235 xproto = \
236"""
237 policy slice_policy < exists Privilege: Privilege.object_id = obj.id >
238 policy network_slice_policy < *slice_policy(slice) >
239"""
240
241 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.network_slice_policy }} ")
242 args = FakeArgs()
243 args.inputs = xproto
244 args.target = target
245
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800246 output = XOSProcessor.process(args)
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400247
248 (op, operands), = eval(output).items()
249
250 self.assertIn('slice_policy', operands)
251 self.assertIn('slice', operands)
252
253 def test_policy_missing_function(self):
254 xproto = \
255"""
256 policy slice_policy < exists Privilege: Privilege.object_id = obj.id >
257 policy network_slice_policy < *slice_policyX(slice) >
258"""
259
260 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.network_slice_policy }} ")
261 args = FakeArgs()
262 args.inputs = xproto
263 args.target = target
264
265 with self.assertRaises(Exception):
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800266 output = XOSProcessor.process(args)
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400267
268
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700269 def test_forall(self):
270 # This one we only parse
271 xproto = \
272"""
273 policy instance < forall Instance: exists Credential: Credential.obj_id = Instance.obj_id >
274"""
275
276 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.instance }}")
277
278 args = FakeArgs()
279 args.inputs = xproto
280 args.target = target
281
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800282 output = XOSProcessor.process(args)
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700283 (op, operands), = eval(output).items()
284
285 self.assertEqual(op,'forall')
286
287
288if __name__ == '__main__':
289 unittest.main()
290
291