Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 1 | import unittest |
| 2 | from xosgenx.generator import XOSGenerator |
| 3 | from helpers import FakeArgs, XProtoTestHelpers |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 4 | |
| 5 | """ |
| 6 | The tests below convert the policy logic expression |
| 7 | into Python, set up an appropriate environment and execute the Python. |
| 8 | """ |
| 9 | |
| 10 | class XProtoPolicyTest(unittest.TestCase): |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 11 | def test_annotation(self): |
| 12 | xproto = \ |
| 13 | """ |
| 14 | policy true_policy < True > |
| 15 | |
| 16 | message always::true_policy { |
| 17 | required int still = 9; |
| 18 | } |
| 19 | """ |
| 20 | |
| 21 | target = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0 }}") |
| 22 | |
| 23 | args = FakeArgs() |
| 24 | args.inputs = xproto |
| 25 | args.target = target |
| 26 | |
| 27 | output = XOSGenerator.generate(args) |
| 28 | self.assertIn("true_policy", output) |
| 29 | |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 30 | def test_constant(self): |
| 31 | xproto = \ |
| 32 | """ |
| 33 | policy true_policy < True > |
| 34 | """ |
| 35 | |
| 36 | target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.true_policy }}") |
| 37 | |
| 38 | args = FakeArgs() |
| 39 | args.inputs = xproto |
| 40 | args.target = target |
| 41 | |
| 42 | output = XOSGenerator.generate(args).replace('t','T') |
| 43 | self.assertTrue(eval(output)) |
| 44 | |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 45 | def test_term(self): |
| 46 | xproto = \ |
| 47 | """ |
| 48 | policy slice_user < slice.user.is_admin > |
| 49 | """ |
| 50 | |
| 51 | target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}") |
| 52 | args = FakeArgs() |
| 53 | args.inputs = xproto |
| 54 | args.target = target |
| 55 | |
| 56 | output = XOSGenerator.generate(args) |
| 57 | |
| 58 | slice = FakeArgs() |
| 59 | slice.user = FakeArgs() |
| 60 | slice.user.is_admin = True |
| 61 | |
| 62 | expr = eval(output) |
| 63 | self.assertTrue(expr) |
| 64 | |
| 65 | def test_num_constant(self): |
| 66 | xproto = \ |
| 67 | """ |
| 68 | policy slice_user < slice.user.age = 57 > |
| 69 | """ |
| 70 | |
| 71 | target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}") |
| 72 | args = FakeArgs() |
| 73 | args.inputs = xproto |
| 74 | args.target = target |
| 75 | |
| 76 | output = XOSGenerator.generate(args) |
| 77 | |
| 78 | slice = FakeArgs() |
| 79 | slice.user = FakeArgs() |
| 80 | slice.user.is_admin = True |
| 81 | |
| 82 | expr = eval(output) |
| 83 | self.assertTrue(expr) |
| 84 | |
| 85 | def test_string_constant(self): |
| 86 | xproto = \ |
| 87 | """ |
| 88 | policy slice_user < slice.user.email = "admin@opencord.org" > |
| 89 | """ |
| 90 | |
| 91 | target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}") |
| 92 | args = FakeArgs() |
| 93 | args.inputs = xproto |
| 94 | args.target = target |
| 95 | |
| 96 | output = XOSGenerator.generate(args) |
| 97 | |
| 98 | slice = FakeArgs() |
| 99 | slice.user = FakeArgs() |
| 100 | slice.user.is_admin = True |
| 101 | |
| 102 | expr = eval(output) |
| 103 | self.assertTrue(expr) |
| 104 | |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 105 | def test_equal(self): |
| 106 | xproto = \ |
| 107 | """ |
| 108 | policy slice_user < slice.user = obj.user > |
| 109 | """ |
| 110 | |
| 111 | target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}") |
| 112 | args = FakeArgs() |
| 113 | args.inputs = xproto |
| 114 | args.target = target |
| 115 | |
| 116 | output = XOSGenerator.generate(args) |
| 117 | |
| 118 | slice = FakeArgs() |
| 119 | slice.user = 'twin' |
| 120 | obj = FakeArgs() |
| 121 | obj.user = 'twin' |
| 122 | |
| 123 | (op, operands), = eval(output).items() |
| 124 | expr = op.join(operands).replace('=','==') |
| 125 | |
| 126 | self.assertTrue(eval(expr)) |
| 127 | |
| 128 | def test_bin(self): |
| 129 | xproto = \ |
| 130 | """ |
| 131 | policy slice_admin < slice.is_admin | obj.empty > |
| 132 | """ |
| 133 | target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_admin }}") |
| 134 | args = FakeArgs() |
| 135 | args.inputs = xproto |
| 136 | args.target = target |
| 137 | |
| 138 | output = XOSGenerator.generate(args) |
| 139 | |
| 140 | slice = FakeArgs() |
| 141 | slice.is_admin = False |
| 142 | obj = FakeArgs() |
| 143 | obj.empty = [] |
| 144 | |
| 145 | (op, operands), = eval(output).items() |
| 146 | expr = op.join(operands).replace('|',' or ') |
| 147 | |
| 148 | self.assertFalse(eval(expr)) |
| 149 | |
| 150 | |
| 151 | def test_exists(self): |
| 152 | xproto = \ |
| 153 | """ |
| 154 | policy privilege < exists Privilege: Privilege.object_id = obj.id > |
| 155 | """ |
| 156 | |
| 157 | target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.privilege }} ") |
| 158 | args = FakeArgs() |
| 159 | args.inputs = xproto |
| 160 | args.target = target |
| 161 | |
| 162 | output = XOSGenerator.generate(args) |
| 163 | |
| 164 | Privilege = FakeArgs() |
| 165 | Privilege.object_id = 1 |
| 166 | obj = FakeArgs() |
| 167 | obj.id = 1 |
| 168 | |
| 169 | (op, operands), = eval(output).items() |
| 170 | (op2, operands2), = operands[1].items() |
| 171 | expr = op2.join(operands2).replace('=','==') |
| 172 | |
| 173 | self.assertTrue(eval(expr)) |
| 174 | |
| 175 | def test_forall(self): |
| 176 | # This one we only parse |
| 177 | xproto = \ |
| 178 | """ |
| 179 | policy instance < forall Instance: exists Credential: Credential.obj_id = Instance.obj_id > |
| 180 | """ |
| 181 | |
| 182 | target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.instance }}") |
| 183 | |
| 184 | args = FakeArgs() |
| 185 | args.inputs = xproto |
| 186 | args.target = target |
| 187 | |
| 188 | output = XOSGenerator.generate(args) |
| 189 | (op, operands), = eval(output).items() |
| 190 | |
| 191 | self.assertEqual(op,'forall') |
| 192 | |
| 193 | |
| 194 | if __name__ == '__main__': |
| 195 | unittest.main() |
| 196 | |
| 197 | |