aboutsummaryrefslogtreecommitdiffstats
path: root/sepolgen/tests/test_matching.py
blob: 63e89ed9e996c93171e510b49a344b0e6e635f48 (plain)
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
# 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.matching as matching
import sepolgen.refparser as refparser
import sepolgen.interfaces as interfaces
import sepolgen.access as access

class TestMatch(unittest.TestCase):
    def test(self):
        a = matching.Match()
        a.dist = 100
        a.info_dir_change = True

        b = matching.Match()
        b.dist = 100
        b.info_dir_change = True

        self.assertEquals(a, b)
        b.info_dir_change = False
        self.assertEquals(cmp(a, b), 1)
        self.assertEquals(cmp(b, a), -1)

        b.dist = 200

        self.assertEquals(cmp(a, b), -1)
        self.assertEquals(cmp(b, a), 1)

class TestMatchList(unittest.TestCase):
    def test_append(self):
        ml = matching.MatchList()
        ml.threshold = 100

        a = matching.Match()
        a.dist = 100
        ml.append(a)
        self.assertEqual(len(ml), 1)

        a = matching.Match()
        a.dist = 200
        ml.append(a)
        self.assertEqual(len(ml), 1)
        self.assertEqual(len(ml.bastards), 1)

        ml.allow_info_dir_change = False
        a = matching.Match()
        a.dist = 0
        a.info_dir_change = True
        ml.append(a)
        self.assertEqual(len(ml), 1)
        self.assertEqual(len(ml.bastards), 2)

    def test_sort(self):
        ml = matching.MatchList()
        ml.threshold = 100

        a = matching.Match()
        a.dist = 100
        ml.append(a)

        b = matching.Match()
        b.dist = 5
        ml.append(b)

        c = matching.Match()
        c.dist = 0
        ml.append(c)

        l = [c, b, a]

        ml.sort()

        for x, y in zip(l, ml):
            self.assertEqual(x, y)

        self.assertEquals(ml.best(), c)


test_expansion = """
interface(`foo',`
   gen_require(`
       type usr_t;
   ')
   allow $1 usr_t : dir { create add_name };
   allow $1 usr_t : file { read write };
')

interface(`map', `
   gen_require(`
       type bar_t;
   ')
   allow $1 bar_t : file read;
   allow $2 bar_t : file write;

   foo($2)
')

interface(`hard_map', `
   gen_require(`
      type baz_t;
   ')
   allow $1 baz_t : file getattr;
   allow $2 baz_t : file read;
   allow $3 baz_t : file write;

   map($1, $2)
   map($2, $3)

   # This should have no effect
   foo($2)
')
"""

class AccessMatcher(unittest.TestCase):
    def test_search(self):
        h = refparser.parse(test_expansion)
        i = interfaces.InterfaceSet()
        i.add_headers(h)

        a = access.AccessVector(["foo_t", "usr_t", "dir", "create"])
        m = matching.AccessMatcher()
        ml = matching.MatchList()

        ans = m.search_ifs(i, a, ml)
                
        
        pass