Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # 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 Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 16 | from __future__ import absolute_import |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 17 | import unittest |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 18 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
| 19 | from helpers import XProtoTestHelpers, FakeObject |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 20 | import pdb |
| 21 | |
| 22 | """The function below is for eliminating warnings arising due to the missing policy_output_validator, |
| 23 | which is generated and loaded dynamically. |
| 24 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 25 | |
| 26 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 27 | def policy_output_validator(x, y): |
| 28 | raise Exception("Validator not generated. Test failed.") |
| 29 | return False |
| 30 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 31 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 32 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 33 | The tests below use the Python code target to generate |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 34 | Python validation policies, set up an appropriate environment and execute the Python. |
| 35 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 36 | |
| 37 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 38 | class XProtoGeneralValidationTest(unittest.TestCase): |
| 39 | def setUp(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 40 | self.target = XProtoTestHelpers.write_tmp_target( |
| 41 | """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 42 | {% for name, policy in proto.policies.items() %} |
| 43 | {{ xproto_fol_to_python_validator(name, policy, None, 'Necessary Failure') }} |
| 44 | {% endfor %} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 45 | """ |
| 46 | ) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 47 | |
| 48 | def test_constant(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 49 | xproto = """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 50 | policy output < False > |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 51 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 52 | args = XOSProcessorArgs(inputs=xproto, target=self.target) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 53 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 54 | output = XOSProcessor.process(args) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 55 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 56 | exec(output) # This loads the generated function, which should look like this: |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 57 | |
| 58 | """ |
| 59 | def policy_output_validator(obj, ctx): |
| 60 | i1 = False |
| 61 | if (not i1): |
| 62 | raise Exception('Necessary Failure') |
| 63 | """ |
| 64 | |
| 65 | with self.assertRaises(Exception): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 66 | policy_output_validator({}, {}) |
| 67 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 68 | def test_equal(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 69 | xproto = """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 70 | policy output < not (ctx.user = obj.user) > |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 71 | """ |
| 72 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 73 | args = XOSProcessorArgs(inputs=xproto, target=self.target) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 74 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 75 | output = XOSProcessor.process(args) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 76 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 77 | exec(output) # This loads the generated function, which should look like this: |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 78 | |
| 79 | """ |
| 80 | def policy_output_validator(obj, ctx): |
| 81 | i2 = (ctx.user == obj.user) |
| 82 | i1 = (not i2) |
| 83 | if (not i1): |
| 84 | raise Exception('Necessary Failure') |
| 85 | """ |
| 86 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 87 | obj = FakeObject() |
| 88 | obj.user = 1 |
| 89 | ctx = FakeObject() |
| 90 | ctx.user = 1 |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 91 | |
| 92 | with self.assertRaises(Exception): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 93 | policy_output_validator(obj, ctx) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 94 | |
| 95 | def test_equal(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 96 | xproto = """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 97 | policy output < not (ctx.user = obj.user) > |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 98 | """ |
| 99 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 100 | args = XOSProcessorArgs(inputs=xproto, target=self.target) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 101 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 102 | output = XOSProcessor.process(args) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 103 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 104 | exec(output) # This loads the generated function, which should look like this: |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 105 | |
| 106 | """ |
| 107 | def policy_output_validator(obj, ctx): |
| 108 | i2 = (ctx.user == obj.user) |
| 109 | i1 = (not i2) |
| 110 | if (not i1): |
| 111 | raise Exception('Necessary Failure') |
| 112 | """ |
| 113 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 114 | obj = FakeObject() |
| 115 | obj.user = 1 |
| 116 | ctx = FakeObject() |
| 117 | ctx.user = 1 |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 118 | |
| 119 | with self.assertRaises(Exception): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 120 | policy_output_validator(obj, ctx) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 121 | |
| 122 | def test_bin(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 123 | xproto = """ |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 124 | policy output < (ctx.is_admin = True | obj.empty = True) | False> |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 125 | """ |
| 126 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 127 | args = XOSProcessorArgs() |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 128 | args.inputs = xproto |
| 129 | args.target = self.target |
| 130 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 131 | output = XOSProcessor.process(args) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 132 | exec(output) # This loads the generated function, which should look like this: |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 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 Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 143 | obj = FakeObject() |
| 144 | obj.empty = False |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 145 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 146 | ctx = FakeObject() |
| 147 | ctx.is_admin = False |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 148 | |
| 149 | with self.assertRaises(Exception): |
| 150 | verdict = policy_output_validator(obj, ctx) |
| 151 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 152 | def test_exists(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 153 | xproto = """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 154 | policy output < exists Privilege: Privilege.object_id = obj.id > |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 155 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 156 | args = XOSProcessorArgs(inputs=xproto, target=self.target) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 157 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 158 | output = XOSProcessor.process(args) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 159 | exec(output) # This loads the generated function, which should look like this: |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 160 | |
| 161 | """ |
| 162 | def policy_output_validator(obj, ctx): |
| 163 | i1 = Privilege.objects.filter(Q(object_id=obj.id))[0] |
| 164 | if (not i1): |
| 165 | raise Exception('Necessary Failure') |
| 166 | """ |
| 167 | |
| 168 | self.assertTrue(policy_output_validator is not None) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 169 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 170 | def test_python(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 171 | xproto = """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 172 | policy output < {{ "jack" in ["the", "box"] }} = True > |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 173 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 174 | args = XOSProcessorArgs(inputs=xproto, target=self.target) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 175 | output = XOSProcessor.process(args) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 176 | exec(output) # This loads the generated function, which should look like this: |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 177 | |
| 178 | """ |
| 179 | def policy_output_validator(obj, ctx): |
| 180 | i2 = ('jack' in ['the', 'box']) |
| 181 | i1 = (i2 == True) |
| 182 | if (not i1): |
| 183 | raise Exception('Necessary Failure') |
| 184 | """ |
| 185 | |
| 186 | with self.assertRaises(Exception): |
| 187 | self.assertTrue(policy_output_validator({}, {}) is True) |
| 188 | |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 189 | def test_call_policy(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 190 | xproto = """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 191 | policy sub_policy < ctx.user = obj.user > |
| 192 | policy output < *sub_policy(child) > |
| 193 | """ |
| 194 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 195 | args = XOSProcessorArgs(inputs=xproto, target=self.target) |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 196 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 197 | output = XOSProcessor.process(args) |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 198 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 199 | exec( |
| 200 | output, globals() |
| 201 | ) # This loads the generated function, which should look like this: |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 202 | |
| 203 | """ |
| 204 | def policy_sub_policy_validator(obj, ctx): |
| 205 | i1 = (ctx.user == obj.user) |
| 206 | if (not i1): |
| 207 | raise ValidationError('Necessary Failure') |
| 208 | |
| 209 | def policy_output_validator(obj, ctx): |
| 210 | i1 = policy_sub_policy_validator(obj.child, ctx) |
| 211 | if (not i1): |
| 212 | raise ValidationError('Necessary Failure') |
| 213 | """ |
| 214 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 215 | obj = FakeObject() |
| 216 | obj.child = FakeObject() |
| 217 | obj.child.user = 1 |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 218 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 219 | ctx = FakeObject() |
| 220 | ctx.user = 1 |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 221 | |
| 222 | with self.assertRaises(Exception): |
| 223 | verdict = policy_output_enforcer(obj, ctx) |
| 224 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 225 | def test_forall(self): |
| 226 | # This one we only parse |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 227 | xproto = """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 228 | policy output < forall Credential: Credential.obj_id = obj_id > |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 229 | """ |
| 230 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 231 | args = XOSProcessorArgs(inputs=xproto, target=self.target) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 232 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 233 | output = XOSProcessor.process(args) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 234 | |
| 235 | """ |
| 236 | def policy_output_enforcer(obj, ctx): |
| 237 | i2 = Credential.objects.filter((~ Q(obj_id=obj_id)))[0] |
| 238 | i1 = (not i2) |
| 239 | return i1 |
| 240 | """ |
| 241 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 242 | self.assertIn("policy_output_validator", output) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 243 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 244 | |
| 245 | if __name__ == "__main__": |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 246 | unittest.main() |