blob: 25c307eaa1372fbaf593ba248d6c746594543698 [file] [log] [blame]
Sapan Bhatiadb183c22017-06-23 02:47:42 -07001import unittest
2from xosgenx.generator import XOSGenerator
3from helpers import FakeArgs, XProtoTestHelpers
Sapan Bhatia5ea307d2017-07-19 00:13:21 -04004import pdb
Sapan Bhatiadb183c22017-06-23 02:47:42 -07005
6"""
7The tests below convert the policy logic expression
8into Python, set up an appropriate environment and execute the Python.
9"""
10
11class XProtoPolicyTest(unittest.TestCase):
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040012 def test_annotation(self):
13 xproto = \
14"""
15 policy true_policy < True >
16
17 message always::true_policy {
18 required int still = 9;
19 }
20"""
21
22 target = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0 }}")
23
24 args = FakeArgs()
25 args.inputs = xproto
26 args.target = target
27
28 output = XOSGenerator.generate(args)
29 self.assertIn("true_policy", output)
30
Sapan Bhatiadb183c22017-06-23 02:47:42 -070031 def test_constant(self):
32 xproto = \
33"""
34 policy true_policy < True >
35"""
36
37 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.true_policy }}")
38
39 args = FakeArgs()
40 args.inputs = xproto
41 args.target = target
42
43 output = XOSGenerator.generate(args).replace('t','T')
44 self.assertTrue(eval(output))
45
Sapan Bhatia5ea307d2017-07-19 00:13:21 -040046 def test_function_term(self):
47 xproto = \
48"""
49 policy slice_user < slice.user.compute_is_admin() >
50"""
51
52 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}")
53 args = FakeArgs()
54 args.inputs = xproto
55 args.target = target
56
57 output = XOSGenerator.generate(args)
58
59 slice = FakeArgs()
60 slice.user = FakeArgs()
61 slice.user.compute_is_admin = lambda: True
62
63 expr = eval(output)
64 self.assertTrue(expr)
65
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040066 def test_term(self):
67 xproto = \
68"""
69 policy slice_user < slice.user.is_admin >
70"""
71
72 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}")
73 args = FakeArgs()
74 args.inputs = xproto
75 args.target = target
76
77 output = XOSGenerator.generate(args)
78
79 slice = FakeArgs()
80 slice.user = FakeArgs()
81 slice.user.is_admin = True
82
83 expr = eval(output)
84 self.assertTrue(expr)
85
86 def test_num_constant(self):
87 xproto = \
88"""
89 policy slice_user < slice.user.age = 57 >
90"""
91
92 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}")
93 args = FakeArgs()
94 args.inputs = xproto
95 args.target = target
96
97 output = XOSGenerator.generate(args)
98
99 slice = FakeArgs()
100 slice.user = FakeArgs()
101 slice.user.is_admin = True
102
103 expr = eval(output)
104 self.assertTrue(expr)
105
106 def test_string_constant(self):
107 xproto = \
108"""
109 policy slice_user < slice.user.email = "admin@opencord.org" >
110"""
111
112 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}")
113 args = FakeArgs()
114 args.inputs = xproto
115 args.target = target
116
117 output = XOSGenerator.generate(args)
118
119 slice = FakeArgs()
120 slice.user = FakeArgs()
121 slice.user.is_admin = True
122
123 expr = eval(output)
124 self.assertTrue(expr)
125
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700126 def test_equal(self):
127 xproto = \
128"""
129 policy slice_user < slice.user = obj.user >
130"""
131
132 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_user }}")
133 args = FakeArgs()
134 args.inputs = xproto
135 args.target = target
136
137 output = XOSGenerator.generate(args)
138
139 slice = FakeArgs()
140 slice.user = 'twin'
141 obj = FakeArgs()
142 obj.user = 'twin'
143
144 (op, operands), = eval(output).items()
145 expr = op.join(operands).replace('=','==')
146
147 self.assertTrue(eval(expr))
148
149 def test_bin(self):
150 xproto = \
151"""
152 policy slice_admin < slice.is_admin | obj.empty >
153"""
154 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.slice_admin }}")
155 args = FakeArgs()
156 args.inputs = xproto
157 args.target = target
158
159 output = XOSGenerator.generate(args)
160
161 slice = FakeArgs()
162 slice.is_admin = False
163 obj = FakeArgs()
164 obj.empty = []
165
166 (op, operands), = eval(output).items()
167 expr = op.join(operands).replace('|',' or ')
168
169 self.assertFalse(eval(expr))
170
Sapan Bhatia5ea307d2017-07-19 00:13:21 -0400171 def test_implies(self):
172 xproto = \
173"""
174 policy implies < obj.name -> obj.creator >
175"""
176 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.implies }}")
177 args = FakeArgs()
178 args.inputs = xproto
179 args.target = target
180
181 output = XOSGenerator.generate(args)
182
183 slice = FakeArgs()
184 slice.is_admin = False
185 obj = FakeArgs()
186 obj.name = 'Thing 1'
187 obj.creator = None
188
189 (op, operands), = eval(output).items()
190 expr = 'not ' + op.join(operands).replace('->',' or ')
191
192 self.assertFalse(eval(expr))
193
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700194 def test_exists(self):
195 xproto = \
196"""
197 policy privilege < exists Privilege: Privilege.object_id = obj.id >
198"""
199
200 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.privilege }} ")
201 args = FakeArgs()
202 args.inputs = xproto
203 args.target = target
204
205 output = XOSGenerator.generate(args)
206
207 Privilege = FakeArgs()
208 Privilege.object_id = 1
209 obj = FakeArgs()
210 obj.id = 1
211
212 (op, operands), = eval(output).items()
213 (op2, operands2), = operands[1].items()
214 expr = op2.join(operands2).replace('=','==')
215
216 self.assertTrue(eval(expr))
217
218 def test_forall(self):
219 # This one we only parse
220 xproto = \
221"""
222 policy instance < forall Instance: exists Credential: Credential.obj_id = Instance.obj_id >
223"""
224
225 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.instance }}")
226
227 args = FakeArgs()
228 args.inputs = xproto
229 args.target = target
230
231 output = XOSGenerator.generate(args)
232 (op, operands), = eval(output).items()
233
234 self.assertEqual(op,'forall')
235
236
237if __name__ == '__main__':
238 unittest.main()
239
240