Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 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 Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 17 | import unittest |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 18 | from xosgenx.generator import XOSProcessor |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 19 | from helpers import FakeArgs, XProtoTestHelpers |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 20 | import pdb |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 21 | |
| 22 | """ |
| 23 | The tests below convert the policy logic expression |
| 24 | into Python, set up an appropriate environment and execute the Python. |
| 25 | """ |
| 26 | |
| 27 | class XProtoPolicyTest(unittest.TestCase): |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 28 | 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 44 | output = XOSProcessor.process(args) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 45 | self.assertIn("true_policy", output) |
| 46 | |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 47 | 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 59 | output = XOSProcessor.process(args).replace('t','T') |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 60 | self.assertTrue(eval(output)) |
| 61 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 62 | 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 73 | output = XOSProcessor.process(args) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 74 | |
| 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 Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 82 | 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 93 | output = XOSProcessor.process(args) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 94 | |
| 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 113 | output = XOSProcessor.process(args) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 114 | |
| 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 133 | output = XOSProcessor.process(args) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 134 | |
| 135 | slice = FakeArgs() |
| 136 | slice.user = FakeArgs() |
| 137 | slice.user.is_admin = True |
| 138 | |
| 139 | expr = eval(output) |
| 140 | self.assertTrue(expr) |
| 141 | |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 142 | 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 153 | output = XOSProcessor.process(args) |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 154 | |
| 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 175 | output = XOSProcessor.process(args) |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 176 | |
| 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 Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 187 | 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 197 | output = XOSProcessor.process(args) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 198 | |
| 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 Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 210 | 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 221 | output = XOSProcessor.process(args) |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 222 | |
| 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 Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 234 | 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 246 | output = XOSProcessor.process(args) |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 247 | |
| 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 266 | output = XOSProcessor.process(args) |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 267 | |
| 268 | |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 269 | 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 282 | output = XOSProcessor.process(args) |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 283 | (op, operands), = eval(output).items() |
| 284 | |
| 285 | self.assertEqual(op,'forall') |
| 286 | |
| 287 | |
| 288 | if __name__ == '__main__': |
| 289 | unittest.main() |
| 290 | |
| 291 | |