File: test_access.py

package info (click to toggle)
android-platform-external-libselinux 10.0.0%2Br36-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 34,176 kB
  • sloc: ansic: 147,112; python: 25,790; makefile: 1,930; yacc: 1,389; sh: 1,206; lex: 452; xml: 180
file content (390 lines) | stat: -rw-r--r-- 13,456 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# Authors: Karl MacMillan <kmacmillan@mentalrootkit.com>
#
# Copyright (C) 2006 Red Hat 
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

import unittest
import sepolgen.refpolicy as refpolicy
import sepolgen.refparser as refparser
import sepolgen.policygen as policygen
import sepolgen.access as access

class TestAccessVector(unittest.TestCase):
    def test_init(self):
        # Default construction
        a = access.AccessVector()
        self.assertEqual(a.src_type, None)
        self.assertEqual(a.tgt_type, None)
        self.assertEqual(a.obj_class, None)
        self.assertTrue(isinstance(a.perms, refpolicy.IdSet))
        self.assertTrue(isinstance(a.audit_msgs, type([])))
        self.assertTrue(isinstance(a.xperms, type({})))
        self.assertEqual(len(a.audit_msgs), 0)

        # Construction from a list
        a = access.AccessVector()
        a.src_type = "foo"
        a.tgt_type = "bar"
        a.obj_class = "file"
        a.perms.update(["read", "write"])

        l = access.AccessVector(['foo', 'bar', 'file', 'read', 'write'])
        self.assertEqual(a.src_type, l.src_type)
        self.assertEqual(a.tgt_type, l.tgt_type)
        self.assertEqual(a.obj_class, l.obj_class)
        self.assertEqual(a.perms, l.perms)

    def test_from_list(self):
        a = access.AccessVector()
        a.src_type = "foo"
        a.tgt_type = "bar"
        a.obj_class = "file"
        a.perms.update(["read", "write"])

        l = access.AccessVector()
        l.from_list(['foo', 'bar', 'file', 'read', 'write'])
        self.assertEqual(a.src_type, l.src_type)
        self.assertEqual(a.tgt_type, l.tgt_type)
        self.assertEqual(a.obj_class, l.obj_class)
        self.assertEqual(a.perms, l.perms)

        l2 = access.AccessVector()
        with self.assertRaises(ValueError):
            l2.from_list(['foo', 'bar', 'file'])

    def test_to_list(self):
        a = access.AccessVector()
        a.src_type = "foo"
        a.tgt_type = "bar"
        a.obj_class = "file"
        a.perms.update(["read", "write"])

        l = a.to_list()
        self.assertEqual(l[0], "foo")
        self.assertEqual(l[1], "bar")
        self.assertEqual(l[2], "file")
        perms = l[3:]
        perms.sort()
        self.assertEqual(perms[0], "read")
        self.assertEqual(perms[1], "write")

    def test_to_string(self):
        a = access.AccessVector()
        a.src_type = "foo"
        a.tgt_type = "bar"
        a.obj_class = "file"
        a.perms.update(["read", "write"])

        first, second = str(a).split(':')
        self.assertEqual(first, "allow foo bar")
        second = second.split(' ')
        second.sort()
        expected = "file { read write };".split(' ')
        expected.sort()
        self.assertEqual(second, expected)

        first, second = a.to_string().split(':')
        self.assertEqual(first, "allow foo bar")
        second = second.split(' ')
        second.sort()
        expected = "file { read write };".split(' ')
        expected.sort()
        self.assertEqual(second, expected)

    def test_cmp(self):
        a = access.AccessVector()
        a.src_type = "foo"
        a.tgt_type = "bar"
        a.obj_class = "file"
        a.perms.update(["read", "write"])

        b = access.AccessVector()
        b.src_type = "foo"
        b.tgt_type = "bar"
        b.obj_class = "file"
        b.perms.update(["read", "write"])

        self.assertEqual(a, b)

        # Source Type
        b.src_type = "baz"
        self.assertNotEqual(a, b)
        self.assertTrue(a > b)

        b.src_type = "gaz"
        self.assertNotEqual(a, b)
        self.assertTrue(a < b)

        # Target Type
        b.src_type = "foo"
        b.tgt_type = "aar"
        self.assertNotEqual(a, b)
        self.assertTrue(a > b)

        b.tgt_type = "gaz"
        self.assertNotEqual(a, b)
        self.assertTrue(a < b)

        # Perms
        b.tgt_type = "bar"
        b.perms = refpolicy.IdSet(["read"])
        self.assertNotEqual(a, b)
        self.assertTrue(a > b)

        b.perms = refpolicy.IdSet(["read", "write", "append"])
        self.assertNotEqual(a, b)

        b.perms = refpolicy.IdSet(["read", "append"])
        self.assertNotEqual(a, b)

    def test_merge_noxperm(self):
        """Test merging two AVs without xperms"""
        a = access.AccessVector(["foo", "bar", "file", "read", "write"])
        b = access.AccessVector(["foo", "bar", "file", "append"])

        a.merge(b)
        self.assertEqual(sorted(list(a.perms)), ["append", "read", "write"])

    def text_merge_xperm1(self):
        """Test merging AV that contains xperms with AV that does not"""
        a = access.AccessVector(["foo", "bar", "file", "read"])
        b = access.AccessVector(["foo", "bar", "file", "read"])
        xp = refpolicy.XpermSet()
        xp.add(42)
        xp.add(12345)
        b.xperms = {"ioctl": xp}

        a.merge(b)
        self.assertEqual(sorted(list(a.perms)), ["append", "read", "write"])
        self.assertEqual(list(a.xperms.keys()), ["ioctl"])
        self.assertEqual(a.xperms["ioctl"].to_string(), "{ 42 12345 }")

    def text_merge_xperm2(self):
        """Test merging AV that does not contain xperms with AV that does"""
        a = access.AccessVector(["foo", "bar", "file", "read"])
        xp = refpolicy.XpermSet()
        xp.add(42)
        xp.add(12345)
        a.xperms = {"ioctl": xp}
        b = access.AccessVector(["foo", "bar", "file", "read"])

        a.merge(b)
        self.assertEqual(sorted(list(a.perms)), ["append", "read", "write"])
        self.assertEqual(list(a.xperms.keys()), ["ioctl"])
        self.assertEqual(a.xperms["ioctl"].to_string(), "{ 42 12345 }")

    def test_merge_xperm_diff_op(self):
        """Test merging two AVs that contain xperms with different operation"""
        a = access.AccessVector(["foo", "bar", "file", "read"])
        xp1 = refpolicy.XpermSet()
        xp1.add(23)
        a.xperms = {"asdf": xp1}

        b = access.AccessVector(["foo", "bar", "file", "read"])
        xp2 = refpolicy.XpermSet()
        xp2.add(42)
        xp2.add(12345)
        b.xperms = {"ioctl": xp2}

        a.merge(b)
        self.assertEqual(list(a.perms), ["read"])
        self.assertEqual(sorted(list(a.xperms.keys())), ["asdf", "ioctl"])
        self.assertEqual(a.xperms["asdf"].to_string(), "23")
        self.assertEqual(a.xperms["ioctl"].to_string(), "{ 42 12345 }")
                         
    def test_merge_xperm_same_op(self):
        """Test merging two AVs that contain xperms with same operation"""
        a = access.AccessVector(["foo", "bar", "file", "read"])
        xp1 = refpolicy.XpermSet()
        xp1.add(23)
        a.xperms = {"ioctl": xp1}

        b = access.AccessVector(["foo", "bar", "file", "read"])
        xp2 = refpolicy.XpermSet()
        xp2.add(42)
        xp2.add(12345)
        b.xperms = {"ioctl": xp2}

        a.merge(b)
        self.assertEqual(list(a.perms), ["read"])
        self.assertEqual(list(a.xperms.keys()), ["ioctl"])
        self.assertEqual(a.xperms["ioctl"].to_string(), "{ 23 42 12345 }")

class TestUtilFunctions(unittest.TestCase):
    def test_is_idparam(self):
        self.assertTrue(access.is_idparam("$1"))
        self.assertTrue(access.is_idparam("$2"))
        self.assertTrue(access.is_idparam("$123"))
        self.assertFalse(access.is_idparam("$123.23"))
        self.assertFalse(access.is_idparam("$A"))

    def test_avrule_to_access_vectors(self):
        rule = refpolicy.AVRule()
        rule.src_types.add("foo")
        rule.src_types.add("baz")
        rule.tgt_types.add("bar")
        rule.tgt_types.add("what")
        rule.obj_classes.add("file")
        rule.obj_classes.add("dir")
        rule.perms.add("read")
        rule.perms.add("write")

        avs = access.avrule_to_access_vectors(rule)
        self.assertEqual(len(avs), 8)
        comps = [("foo", "what", "dir"),
                 ("foo", "what", "file"),
                 ("foo", "bar", "dir"),
                 ("foo", "bar", "file"),
                 ("baz", "what", "dir"),
                 ("baz", "what", "file"),
                 ("baz", "bar", "dir"),
                 ("baz", "bar", "file")]
        status = [False] * 8
        for av in access.avrule_to_access_vectors(rule):
            self.assertEqual(av.perms, refpolicy.IdSet(["read", "write"]))
            for i in range(len(comps)):
                if comps[i][0] == av.src_type and \
                   comps[i][1] == av.tgt_type and \
                   comps[i][2] == av.obj_class:
                    status[i] = True

        for s in status:
            self.assertEqual(s, True)
                   

class TestAccessVectorSet(unittest.TestCase):
    def setUp(self):
        rule = refpolicy.AVRule()
        rule.src_types.add("foo")
        rule.src_types.add("baz")
        rule.tgt_types.add("bar")
        rule.tgt_types.add("what")
        rule.obj_classes.add("file")
        rule.obj_classes.add("dir")
        rule.perms.add("read")
        rule.perms.add("write")

        s = access.AccessVectorSet()
        avs = access.avrule_to_access_vectors(rule)
        for av in avs:
            s.add_av(av)
        self.s = s
    
    def test_init(self):
        a = access.AccessVectorSet()

    def test_iter(self):
        comps = [("foo", "what", "dir"),
                 ("foo", "what", "file"),
                 ("foo", "bar", "dir"),
                 ("foo", "bar", "file"),
                 ("baz", "what", "dir"),
                 ("baz", "what", "file"),
                 ("baz", "bar", "dir"),
                 ("baz", "bar", "file")]
        status = [False] * 8
        for av in self.s:
            self.assertEqual(av.perms, refpolicy.IdSet(["read", "write"]))
            for i in range(len(comps)):
                if comps[i][0] == av.src_type and \
                   comps[i][1] == av.tgt_type and \
                   comps[i][2] == av.obj_class:
                    status[i] = True

        for s in status:
            self.assertEqual(s, True)

    def test_len(self):
        self.assertEqual(len(self.s), 8)

    def test_list(self):
        a = access.AccessVectorSet()
        a.add("$1", "foo", "file", refpolicy.IdSet(["read", "write"]))
        a.add("$1", "bar", "file", refpolicy.IdSet(["read", "write"]))
        a.add("what", "bar", "file", refpolicy.IdSet(["read", "write"]))

        avl = a.to_list()
        avl.sort()

        test_l = [['what','bar','file','read','write'],
                  ['$1','foo','file','read','write'],
                  ['$1','bar','file','read','write']]
        test_l.sort()

        for a,b in zip(test_l, avl):
            self.assertEqual(len(a), len(b))
            for x,y in list(zip(a,b))[:3]:
                self.assertEqual(x, y)
            perms1 = a[3:]
            perms2 = b[3:]
            perms1.sort()
            perms2.sort()
            self.assertEqual(perms1, perms2)
                
        b = access.AccessVectorSet()
        b.from_list(avl)
        self.assertEqual(len(b), 3)

    def test_add_av_first(self):
        """Test adding first AV to the AV set"""
        avs = access.AccessVectorSet()
        av = access.AccessVector(['foo', 'bar', 'file', 'read'])

        avs.add_av(av)

        self.assertEqual(avs.to_list(), [['foo', 'bar', 'file', 'read']])

    def test_add_av_second(self):
        """Test adding second AV to the AV set with same source and target
        context and class"""
        avs = access.AccessVectorSet()
        av1 = access.AccessVector(['foo', 'bar', 'file', 'read'])
        av2 = access.AccessVector(['foo', 'bar', 'file', 'write'])

        avs.add_av(av1)
        avs.add_av(av2)

        self.assertEqual(avs.to_list(), [['foo', 'bar', 'file', 'read',
                         'write']])

    def test_add_av_with_msg(self):
        """Test adding audit message"""
        avs = access.AccessVectorSet()
        av = access.AccessVector(['foo', 'bar', 'file', 'read'])

        avs.add_av(av, 'test message')

        self.assertEqual(avs.src['foo']['bar']['file', av.type].audit_msgs,
                         ['test message'])

    def test_add(self):
        """Test adding AV to the set"""
        s = access.AccessVectorSet()

        def test_add_av(av, audit_msg=None):
            self.assertEqual(av.src_type, 'foo')
            self.assertEqual(av.tgt_type, 'bar')
            self.assertEqual(av.obj_class, 'file')
            self.assertEqual(list(av.perms), ['read'])
            self.assertEqual(av.data, 'test data')
            self.assertEqual(av.type, 42)
            self.assertEqual(audit_msg, 'test message')

        s.add_av = test_add_av

        s.add("foo", "bar", "file", refpolicy.IdSet(["read"]),
              audit_msg='test message', avc_type=42, data='test data')