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 |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 18 | from xosgenx.generator import XOSProcessor |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 19 | from helpers import FakeArgs, XProtoTestHelpers |
| 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 | """ |
| 46 | args = FakeArgs() |
| 47 | args.inputs = xproto |
| 48 | args.target = self.target |
| 49 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 50 | output = XOSProcessor.process(args) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 51 | |
| 52 | exec(output) # This loads the generated function, which should look like this: |
| 53 | |
| 54 | """ |
| 55 | def policy_output_validator(obj, ctx): |
| 56 | i1 = False |
| 57 | if (not i1): |
| 58 | raise Exception('Necessary Failure') |
| 59 | """ |
| 60 | |
| 61 | with self.assertRaises(Exception): |
| 62 | policy_output_validator({}, {}) |
| 63 | |
| 64 | def test_equal(self): |
| 65 | xproto = \ |
| 66 | """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 67 | policy output < not (ctx.user = obj.user) > |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 68 | """ |
| 69 | |
| 70 | args = FakeArgs() |
| 71 | args.inputs = xproto |
| 72 | args.target = self.target |
| 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 | |
| 76 | exec(output) # This loads the generated function, which should look like this: |
| 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 | |
| 86 | obj = FakeArgs() |
| 87 | obj.user = 1 |
| 88 | ctx = FakeArgs() |
| 89 | ctx.user = 1 |
| 90 | |
| 91 | with self.assertRaises(Exception): |
| 92 | policy_output_validator(obj, ctx) |
| 93 | |
| 94 | def test_equal(self): |
| 95 | xproto = \ |
| 96 | """ |
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 | |
| 100 | args = FakeArgs() |
| 101 | args.inputs = xproto |
| 102 | args.target = self.target |
| 103 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 104 | output = XOSProcessor.process(args) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 105 | |
| 106 | exec(output) # This loads the generated function, which should look like this: |
| 107 | |
| 108 | """ |
| 109 | def policy_output_validator(obj, ctx): |
| 110 | i2 = (ctx.user == obj.user) |
| 111 | i1 = (not i2) |
| 112 | if (not i1): |
| 113 | raise Exception('Necessary Failure') |
| 114 | """ |
| 115 | |
| 116 | obj = FakeArgs() |
| 117 | obj.user = 1 |
| 118 | ctx = FakeArgs() |
| 119 | ctx.user = 1 |
| 120 | |
| 121 | with self.assertRaises(Exception): |
| 122 | policy_output_validator(obj, ctx) |
| 123 | |
| 124 | def test_bin(self): |
| 125 | xproto = \ |
| 126 | """ |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 127 | policy output < (ctx.is_admin = True | obj.empty = True) | False> |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 128 | """ |
| 129 | |
| 130 | args = FakeArgs() |
| 131 | args.inputs = xproto |
| 132 | args.target = self.target |
| 133 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 134 | output = XOSProcessor.process(args) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 135 | exec(output) # This loads the generated function, which should look like this: |
| 136 | |
| 137 | """ |
| 138 | def policy_output_validator(obj, ctx): |
| 139 | i2 = (ctx.is_admin == True) |
| 140 | i3 = (obj.empty == True) |
| 141 | i1 = (i2 or i3) |
| 142 | if (not i1): |
| 143 | raise Exception('Necessary Failure') |
| 144 | """ |
| 145 | |
| 146 | obj = FakeArgs() |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 147 | obj.empty = False |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 148 | |
| 149 | ctx = FakeArgs() |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 150 | ctx.is_admin = False |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 151 | |
| 152 | with self.assertRaises(Exception): |
| 153 | verdict = policy_output_validator(obj, ctx) |
| 154 | |
| 155 | |
| 156 | def test_exists(self): |
| 157 | xproto = \ |
| 158 | """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 159 | policy output < exists Privilege: Privilege.object_id = obj.id > |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 160 | """ |
| 161 | args = FakeArgs() |
| 162 | args.inputs = xproto |
| 163 | args.target = self.target |
| 164 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 165 | output = XOSProcessor.process(args) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 166 | exec(output) # This loads the generated function, which should look like this: |
| 167 | |
| 168 | """ |
| 169 | def policy_output_validator(obj, ctx): |
| 170 | i1 = Privilege.objects.filter(Q(object_id=obj.id))[0] |
| 171 | if (not i1): |
| 172 | raise Exception('Necessary Failure') |
| 173 | """ |
| 174 | |
| 175 | self.assertTrue(policy_output_validator is not None) |
| 176 | |
| 177 | def test_python(self): |
| 178 | xproto = \ |
| 179 | """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 180 | policy output < {{ "jack" in ["the", "box"] }} = True > |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 181 | """ |
| 182 | args = FakeArgs() |
| 183 | args.inputs = xproto |
| 184 | args.target = self.target |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 185 | output = XOSProcessor.process(args) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 186 | exec(output) # This loads the generated function, which should look like this: |
| 187 | |
| 188 | """ |
| 189 | def policy_output_validator(obj, ctx): |
| 190 | i2 = ('jack' in ['the', 'box']) |
| 191 | i1 = (i2 == True) |
| 192 | if (not i1): |
| 193 | raise Exception('Necessary Failure') |
| 194 | """ |
| 195 | |
| 196 | with self.assertRaises(Exception): |
| 197 | self.assertTrue(policy_output_validator({}, {}) is True) |
| 198 | |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 199 | def test_call_policy(self): |
| 200 | xproto = \ |
| 201 | """ |
| 202 | policy sub_policy < ctx.user = obj.user > |
| 203 | policy output < *sub_policy(child) > |
| 204 | """ |
| 205 | |
| 206 | args = FakeArgs() |
| 207 | args.inputs = xproto |
| 208 | args.target = self.target |
| 209 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 210 | output = XOSProcessor.process(args) |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 211 | |
| 212 | exec(output,globals()) # This loads the generated function, which should look like this: |
| 213 | |
| 214 | """ |
| 215 | def policy_sub_policy_validator(obj, ctx): |
| 216 | i1 = (ctx.user == obj.user) |
| 217 | if (not i1): |
| 218 | raise ValidationError('Necessary Failure') |
| 219 | |
| 220 | def policy_output_validator(obj, ctx): |
| 221 | i1 = policy_sub_policy_validator(obj.child, ctx) |
| 222 | if (not i1): |
| 223 | raise ValidationError('Necessary Failure') |
| 224 | """ |
| 225 | |
| 226 | obj = FakeArgs() |
| 227 | obj.child = FakeArgs() |
| 228 | obj.child.user = 1 |
| 229 | |
| 230 | ctx = FakeArgs() |
| 231 | ctx.user = 1 |
| 232 | |
| 233 | with self.assertRaises(Exception): |
| 234 | verdict = policy_output_enforcer(obj, ctx) |
| 235 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 236 | def test_forall(self): |
| 237 | # This one we only parse |
| 238 | xproto = \ |
| 239 | """ |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 240 | policy output < forall Credential: Credential.obj_id = obj_id > |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 241 | """ |
| 242 | |
| 243 | args = FakeArgs() |
| 244 | args.inputs = xproto |
| 245 | args.target = self.target |
| 246 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 247 | output = XOSProcessor.process(args) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 248 | |
| 249 | """ |
| 250 | def policy_output_enforcer(obj, ctx): |
| 251 | i2 = Credential.objects.filter((~ Q(obj_id=obj_id)))[0] |
| 252 | i1 = (not i2) |
| 253 | return i1 |
| 254 | """ |
| 255 | |
| 256 | self.assertIn('policy_output_validator', output) |
| 257 | |
| 258 | if __name__ == '__main__': |
| 259 | unittest.main() |