blob: 5bc3511a8fa39b2fe25db30b760cf3c4e25475ba [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
Sapan Bhatiad3fcb662017-07-25 21:13:48 -0400218 def test_policy_function(self):
219 xproto = \
220"""
221 policy slice_policy < exists Privilege: Privilege.object_id = obj.id >
222 policy network_slice_policy < *slice_policy(slice) >
223"""
224
225 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.network_slice_policy }} ")
226 args = FakeArgs()
227 args.inputs = xproto
228 args.target = target
229
230 output = XOSGenerator.generate(args)
231
232 (op, operands), = eval(output).items()
233
234 self.assertIn('slice_policy', operands)
235 self.assertIn('slice', operands)
236
237 def test_policy_missing_function(self):
238 xproto = \
239"""
240 policy slice_policy < exists Privilege: Privilege.object_id = obj.id >
241 policy network_slice_policy < *slice_policyX(slice) >
242"""
243
244 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.network_slice_policy }} ")
245 args = FakeArgs()
246 args.inputs = xproto
247 args.target = target
248
249 with self.assertRaises(Exception):
250 output = XOSGenerator.generate(args)
251
252
Sapan Bhatiadb183c22017-06-23 02:47:42 -0700253 def test_forall(self):
254 # This one we only parse
255 xproto = \
256"""
257 policy instance < forall Instance: exists Credential: Credential.obj_id = Instance.obj_id >
258"""
259
260 target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.instance }}")
261
262 args = FakeArgs()
263 args.inputs = xproto
264 args.target = target
265
266 output = XOSGenerator.generate(args)
267 (op, operands), = eval(output).items()
268
269 self.assertEqual(op,'forall')
270
271
272if __name__ == '__main__':
273 unittest.main()
274
275