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