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