blob: 4134fe1652f0eb93b9365b710fa5bca3afb5b6e8 [file] [log] [blame]
Sapan Bhatia5ea307d2017-07-19 00:13:21 -04001import unittest
2from xosgenx.generator import XOSGenerator
3from helpers import FakeArgs, XProtoTestHelpers
4import pdb
5
6"""The function below is for eliminating warnings arising due to the missing policy_output_validator,
7which is generated and loaded dynamically.
8"""
9def policy_output_validator(x, y):
10 raise Exception("Validator not generated. Test failed.")
11 return False
12
13"""
14The tests below use the Python code target to generate
15Python validation policies, set up an appropriate environment and execute the Python.
16"""
17class XProtoGeneralValidationTest(unittest.TestCase):
18 def setUp(self):
19 self.target = XProtoTestHelpers.write_tmp_target("{{ xproto_fol_to_python_validator('output', proto.policies.test_policy, None, 'Necessary Failure') }}")
20
21 def test_constant(self):
22 xproto = \
23"""
24 policy test_policy < False >
25"""
26 args = FakeArgs()
27 args.inputs = xproto
28 args.target = self.target
29
30 output = XOSGenerator.generate(args)
31
32 exec(output) # This loads the generated function, which should look like this:
33
34 """
35 def policy_output_validator(obj, ctx):
36 i1 = False
37 if (not i1):
38 raise Exception('Necessary Failure')
39 """
40
41 with self.assertRaises(Exception):
42 policy_output_validator({}, {})
43
44 def test_equal(self):
45 xproto = \
46"""
47 policy test_policy < not (ctx.user = obj.user) >
48"""
49
50 args = FakeArgs()
51 args.inputs = xproto
52 args.target = self.target
53
54 output = XOSGenerator.generate(args)
55
56 exec(output) # This loads the generated function, which should look like this:
57
58 """
59 def policy_output_validator(obj, ctx):
60 i2 = (ctx.user == obj.user)
61 i1 = (not i2)
62 if (not i1):
63 raise Exception('Necessary Failure')
64 """
65
66 obj = FakeArgs()
67 obj.user = 1
68 ctx = FakeArgs()
69 ctx.user = 1
70
71 with self.assertRaises(Exception):
72 policy_output_validator(obj, ctx)
73
74 def test_equal(self):
75 xproto = \
76"""
77 policy test_policy < not (ctx.user = obj.user) >
78"""
79
80 args = FakeArgs()
81 args.inputs = xproto
82 args.target = self.target
83
84 output = XOSGenerator.generate(args)
85
86 exec(output) # This loads the generated function, which should look like this:
87
88 """
89 def policy_output_validator(obj, ctx):
90 i2 = (ctx.user == obj.user)
91 i1 = (not i2)
92 if (not i1):
93 raise Exception('Necessary Failure')
94 """
95
96 obj = FakeArgs()
97 obj.user = 1
98 ctx = FakeArgs()
99 ctx.user = 1
100
101 with self.assertRaises(Exception):
102 policy_output_validator(obj, ctx)
103
104 def test_bin(self):
105 xproto = \
106"""
107 policy test_policy < (ctx.is_admin = True | obj.empty = True) & False>
108"""
109
110 args = FakeArgs()
111 args.inputs = xproto
112 args.target = self.target
113
114 output = XOSGenerator.generate(args)
115 exec(output) # This loads the generated function, which should look like this:
116
117 """
118 def policy_output_validator(obj, ctx):
119 i2 = (ctx.is_admin == True)
120 i3 = (obj.empty == True)
121 i1 = (i2 or i3)
122 if (not i1):
123 raise Exception('Necessary Failure')
124 """
125
126 obj = FakeArgs()
127 obj.empty = True
128
129 ctx = FakeArgs()
130 ctx.is_admin = True
131
132 with self.assertRaises(Exception):
133 verdict = policy_output_validator(obj, ctx)
134
135
136 def test_exists(self):
137 xproto = \
138"""
139 policy test_policy < exists Privilege: Privilege.object_id = obj.id >
140"""
141 args = FakeArgs()
142 args.inputs = xproto
143 args.target = self.target
144
145 output = XOSGenerator.generate(args)
146 exec(output) # This loads the generated function, which should look like this:
147
148 """
149 def policy_output_validator(obj, ctx):
150 i1 = Privilege.objects.filter(Q(object_id=obj.id))[0]
151 if (not i1):
152 raise Exception('Necessary Failure')
153 """
154
155 self.assertTrue(policy_output_validator is not None)
156
157 def test_python(self):
158 xproto = \
159"""
160 policy test_policy < {{ "jack" in ["the", "box"] }} = True >
161"""
162 args = FakeArgs()
163 args.inputs = xproto
164 args.target = self.target
165 output = XOSGenerator.generate(args)
166 exec(output) # This loads the generated function, which should look like this:
167
168 """
169 def policy_output_validator(obj, ctx):
170 i2 = ('jack' in ['the', 'box'])
171 i1 = (i2 == True)
172 if (not i1):
173 raise Exception('Necessary Failure')
174 """
175
176 with self.assertRaises(Exception):
177 self.assertTrue(policy_output_validator({}, {}) is True)
178
179 def test_forall(self):
180 # This one we only parse
181 xproto = \
182"""
183 policy test_policy < forall Credential: Credential.obj_id = obj_id >
184"""
185
186 args = FakeArgs()
187 args.inputs = xproto
188 args.target = self.target
189
190 output = XOSGenerator.generate(args)
191
192 """
193 def policy_output_enforcer(obj, ctx):
194 i2 = Credential.objects.filter((~ Q(obj_id=obj_id)))[0]
195 i1 = (not i2)
196 return i1
197 """
198
199 self.assertIn('policy_output_validator', output)
200
201if __name__ == '__main__':
202 unittest.main()