blob: 9cac78aaff6a8687087239f2ca5ffe6d1583d450 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
Zack Williams9a42f872019-02-15 17:56:04 -070016from __future__ import absolute_import
Sapan Bhatiadb183c22017-06-23 02:47:42 -070017import unittest
Scott Baker1f7791d2018-10-04 13:21:20 -070018from xosgenx.generator import XOSProcessor, XOSProcessorArgs
19from helpers import FakeObject, XProtoTestHelpers
Sapan Bhatiadb183c22017-06-23 02:47:42 -070020
21"""
22The tests below convert the policy logic expression
23into Python, set up an appropriate environment and execute the Python.
24"""
25
Zack Williams045b63d2019-01-22 16:30:57 -070026
Sapan Bhatiadb183c22017-06-23 02:47:42 -070027class XProtoPolicyTest(unittest.TestCase):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040028 def test_annotation(self):
Zack Williams045b63d2019-01-22 16:30:57 -070029 xproto = """
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040030 policy true_policy < True >
31
32 message always::true_policy {
33 required int still = 9;
34 }
35"""
36
37 target = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0 }}")
38
Scott Baker1f7791d2018-10-04 13:21:20 -070039 args = XOSProcessorArgs()
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040040 args.inputs = xproto
41 args.target = target
42
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080043 output = XOSProcessor.process(args)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040044 self.assertIn("true_policy", output)
45
Sapan Bhatiadb183c22017-06-23 02:47:42 -070046 def test_constant(self):
Zack Williams045b63d2019-01-22 16:30:57 -070047 xproto = """
Sapan Bhatiadb183c22017-06-23 02:47:42 -070048 policy true_policy < True >
49"""
50
51 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.true_policy }}")
52
Scott Baker1f7791d2018-10-04 13:21:20 -070053 args = XOSProcessorArgs()
Sapan Bhatiadb183c22017-06-23 02:47:42 -070054 args.inputs = xproto
55 args.target = target
56
Zack Williams045b63d2019-01-22 16:30:57 -070057 output = XOSProcessor.process(args).replace("t", "T")
58 self.assertTrue(eval(output))
Sapan Bhatiadb183c22017-06-23 02:47:42 -070059
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040060 def test_function_term(self):
Zack Williams045b63d2019-01-22 16:30:57 -070061 xproto = """
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040062 policy slice_user < slice.user.compute_is_admin() >
63"""
64
65 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}")
Scott Baker1f7791d2018-10-04 13:21:20 -070066 args = XOSProcessorArgs()
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040067 args.inputs = xproto
68 args.target = target
69
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080070 output = XOSProcessor.process(args)
Zack Williams045b63d2019-01-22 16:30:57 -070071
Scott Baker1f7791d2018-10-04 13:21:20 -070072 slice = FakeObject()
73 slice.user = FakeObject()
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040074 slice.user.compute_is_admin = lambda: True
75
76 expr = eval(output)
77 self.assertTrue(expr)
78
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040079 def test_term(self):
Zack Williams045b63d2019-01-22 16:30:57 -070080 xproto = """
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040081 policy slice_user < slice.user.is_admin >
82"""
83
84 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}")
Scott Baker1f7791d2018-10-04 13:21:20 -070085 args = XOSProcessorArgs()
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040086 args.inputs = xproto
87 args.target = target
88
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080089 output = XOSProcessor.process(args)
Zack Williams045b63d2019-01-22 16:30:57 -070090
Scott Baker1f7791d2018-10-04 13:21:20 -070091 slice = FakeObject()
92 slice.user = FakeObject()
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040093 slice.user.is_admin = True
94
95 expr = eval(output)
96 self.assertTrue(expr)
97
98 def test_num_constant(self):
Zack Williams045b63d2019-01-22 16:30:57 -070099 xproto = """
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400100 policy slice_user < slice.user.age = 57 >
101"""
102
103 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}")
Scott Baker1f7791d2018-10-04 13:21:20 -0700104 args = XOSProcessorArgs()
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400105 args.inputs = xproto
106 args.target = target
107
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800108 output = XOSProcessor.process(args)
Zack Williams045b63d2019-01-22 16:30:57 -0700109
Scott Baker1f7791d2018-10-04 13:21:20 -0700110 slice = FakeObject()
111 slice.user = FakeObject()
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400112 slice.user.is_admin = True
113
114 expr = eval(output)
115 self.assertTrue(expr)
116
117 def test_string_constant(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700118 xproto = """
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400119 policy slice_user < slice.user.email = "admin@opencord.org" >
120"""
121
122 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}")
Scott Baker1f7791d2018-10-04 13:21:20 -0700123 args = XOSProcessorArgs()
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400124 args.inputs = xproto
125 args.target = target
126
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800127 output = XOSProcessor.process(args)
Zack Williams045b63d2019-01-22 16:30:57 -0700128
Scott Baker1f7791d2018-10-04 13:21:20 -0700129 slice = FakeObject()
130 slice.user = FakeObject()
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400131 slice.user.is_admin = True
132
133 expr = eval(output)
134 self.assertTrue(expr)
135
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700136 def test_equal(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700137 xproto = """
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700138 policy slice_user < slice.user = obj.user >
139"""
140
141 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}")
Scott Baker1f7791d2018-10-04 13:21:20 -0700142 args = XOSProcessorArgs()
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700143 args.inputs = xproto
144 args.target = target
145
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800146 output = XOSProcessor.process(args)
Zack Williams045b63d2019-01-22 16:30:57 -0700147
Scott Baker1f7791d2018-10-04 13:21:20 -0700148 slice = FakeObject()
Zack Williams045b63d2019-01-22 16:30:57 -0700149 slice.user = "twin"
Scott Baker1f7791d2018-10-04 13:21:20 -0700150 obj = FakeObject()
Zack Williams045b63d2019-01-22 16:30:57 -0700151 obj.user = "twin"
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700152
153 (op, operands), = eval(output).items()
Zack Williams045b63d2019-01-22 16:30:57 -0700154 expr = op.join(operands).replace("=", "==")
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700155
156 self.assertTrue(eval(expr))
157
158 def test_bin(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700159 xproto = """
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700160 policy slice_admin < slice.is_admin | obj.empty >
161"""
162 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_admin }}")
Scott Baker1f7791d2018-10-04 13:21:20 -0700163 args = XOSProcessorArgs()
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700164 args.inputs = xproto
165 args.target = target
166
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800167 output = XOSProcessor.process(args)
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700168
Scott Baker1f7791d2018-10-04 13:21:20 -0700169 slice = FakeObject()
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700170 slice.is_admin = False
Scott Baker1f7791d2018-10-04 13:21:20 -0700171 obj = FakeObject()
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700172 obj.empty = []
173
174 (op, operands), = eval(output).items()
Zack Williams045b63d2019-01-22 16:30:57 -0700175 expr = op.join(operands).replace("|", " or ")
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700176
177 self.assertFalse(eval(expr))
178
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400179 def test_implies(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700180 xproto = """
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400181 policy implies < obj.name -> obj.creator >
182"""
183 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.implies }}")
Scott Baker1f7791d2018-10-04 13:21:20 -0700184 args = XOSProcessorArgs()
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400185 args.inputs = xproto
186 args.target = target
187
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800188 output = XOSProcessor.process(args)
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400189
Scott Baker1f7791d2018-10-04 13:21:20 -0700190 slice = FakeObject()
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400191 slice.is_admin = False
Scott Baker1f7791d2018-10-04 13:21:20 -0700192 obj = FakeObject()
Zack Williams045b63d2019-01-22 16:30:57 -0700193 obj.name = "Thing 1"
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400194 obj.creator = None
195
196 (op, operands), = eval(output).items()
Zack Williams045b63d2019-01-22 16:30:57 -0700197 expr = "not " + op.join(operands).replace("->", " or ")
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400198
199 self.assertFalse(eval(expr))
Zack Williams045b63d2019-01-22 16:30:57 -0700200
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700201 def test_exists(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700202 xproto = """
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700203 policy privilege < exists Privilege: Privilege.object_id = obj.id >
204"""
205
206 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.privilege }} ")
Scott Baker1f7791d2018-10-04 13:21:20 -0700207 args = XOSProcessorArgs()
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700208 args.inputs = xproto
209 args.target = target
210
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800211 output = XOSProcessor.process(args)
Zack Williams045b63d2019-01-22 16:30:57 -0700212
Scott Baker1f7791d2018-10-04 13:21:20 -0700213 Privilege = FakeObject()
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700214 Privilege.object_id = 1
Scott Baker1f7791d2018-10-04 13:21:20 -0700215 obj = FakeObject()
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700216 obj.id = 1
217
218 (op, operands), = eval(output).items()
219 (op2, operands2), = operands[1].items()
Zack Williams045b63d2019-01-22 16:30:57 -0700220 expr = op2.join(operands2).replace("=", "==")
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700221
222 self.assertTrue(eval(expr))
223
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400224 def test_policy_function(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700225 xproto = """
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400226 policy slice_policy < exists Privilege: Privilege.object_id = obj.id >
227 policy network_slice_policy < *slice_policy(slice) >
228"""
229
Zack Williams045b63d2019-01-22 16:30:57 -0700230 target = XProtoTestHelpers.write_tmp_target(
231 "{{ proto.policies.network_slice_policy }} "
232 )
Scott Baker1f7791d2018-10-04 13:21:20 -0700233 args = XOSProcessorArgs()
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400234 args.inputs = xproto
235 args.target = target
236
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800237 output = XOSProcessor.process(args)
Zack Williams045b63d2019-01-22 16:30:57 -0700238
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400239 (op, operands), = eval(output).items()
240
Zack Williams045b63d2019-01-22 16:30:57 -0700241 self.assertIn("slice_policy", operands)
242 self.assertIn("slice", operands)
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400243
244 def test_policy_missing_function(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700245 xproto = """
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400246 policy slice_policy < exists Privilege: Privilege.object_id = obj.id >
247 policy network_slice_policy < *slice_policyX(slice) >
248"""
249
Zack Williams045b63d2019-01-22 16:30:57 -0700250 target = XProtoTestHelpers.write_tmp_target(
251 "{{ proto.policies.network_slice_policy }} "
252 )
Scott Baker1f7791d2018-10-04 13:21:20 -0700253 args = XOSProcessorArgs()
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400254 args.inputs = xproto
255 args.target = target
256
257 with self.assertRaises(Exception):
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800258 output = XOSProcessor.process(args)
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400259
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700260 def test_forall(self):
261 # This one we only parse
Zack Williams045b63d2019-01-22 16:30:57 -0700262 xproto = """
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700263 policy instance < forall Instance: exists Credential: Credential.obj_id = Instance.obj_id >
264"""
265
266 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.instance }}")
267
Scott Baker1f7791d2018-10-04 13:21:20 -0700268 args = XOSProcessorArgs()
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700269 args.inputs = xproto
270 args.target = target
271
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800272 output = XOSProcessor.process(args)
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700273 (op, operands), = eval(output).items()
274
Zack Williams045b63d2019-01-22 16:30:57 -0700275 self.assertEqual(op, "forall")
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700276
277
Zack Williams045b63d2019-01-22 16:30:57 -0700278if __name__ == "__main__":
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700279 unittest.main()