sepolgen: Edit tests so they pass even on Python3 where hash is random.

By default in Python3 hash uses random seed as salt, this leads to
different order in output from functions which rely on hash as are
dicts and sets. Tests in sepolgen relied on the frozen order.

Signed-off-by: Robert Kuska <rkuska@redhat.com>
This commit is contained in:
Robert Kuska 2015-07-16 13:48:21 +02:00 committed by Stephen Smalley
parent a9fb9053f7
commit a280b06dd9
2 changed files with 37 additions and 10 deletions

View File

@ -72,8 +72,10 @@ class TestAccessVector(unittest.TestCase):
self.assertEqual(l[0], "foo") self.assertEqual(l[0], "foo")
self.assertEqual(l[1], "bar") self.assertEqual(l[1], "bar")
self.assertEqual(l[2], "file") self.assertEqual(l[2], "file")
self.assertEqual(l[3], "read") perms = l[3:]
self.assertEqual(l[4], "write") perms.sort()
self.assertEqual(perms[0], "read")
self.assertEqual(perms[1], "write")
def test_to_string(self): def test_to_string(self):
a = access.AccessVector() a = access.AccessVector()
@ -82,8 +84,21 @@ class TestAccessVector(unittest.TestCase):
a.obj_class = "file" a.obj_class = "file"
a.perms.update(["read", "write"]) a.perms.update(["read", "write"])
self.assertEqual(str(a), "allow foo bar:file { read write };") first, second = str(a).split(':')
self.assertEqual(a.to_string(), "allow foo bar:file { read write };") 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): def test_cmp(self):
a = access.AccessVector() a = access.AccessVector()
@ -127,11 +142,9 @@ class TestAccessVector(unittest.TestCase):
b.perms = refpolicy.IdSet(["read", "write", "append"]) b.perms = refpolicy.IdSet(["read", "write", "append"])
self.assertNotEqual(a, b) self.assertNotEqual(a, b)
self.assertTrue(a < b)
b.perms = refpolicy.IdSet(["read", "append"]) b.perms = refpolicy.IdSet(["read", "append"])
self.assertNotEqual(a, b) self.assertNotEqual(a, b)
self.assertTrue(a > b)
class TestUtilFunctions(unittest.TestCase): class TestUtilFunctions(unittest.TestCase):
def test_is_idparam(self): def test_is_idparam(self):
@ -227,15 +240,22 @@ class TestAccessVectorSet(unittest.TestCase):
a.add("what", "bar", "file", refpolicy.IdSet(["read", "write"])) a.add("what", "bar", "file", refpolicy.IdSet(["read", "write"]))
avl = a.to_list() avl = a.to_list()
avl.sort()
test_l = [['what','bar','file','read','write'], test_l = [['what','bar','file','read','write'],
['$1','foo','file','read','write'], ['$1','foo','file','read','write'],
['$1','bar','file','read','write']] ['$1','bar','file','read','write']]
test_l.sort()
for a,b in zip(test_l, avl): for a,b in zip(test_l, avl):
self.assertEqual(len(a), len(b)) self.assertEqual(len(a), len(b))
for x,y in zip(a,b): for x,y in list(zip(a,b))[:3]:
self.assertEqual(x, y) self.assertEqual(x, y)
perms1 = a[3:]
perms2 = b[3:]
perms1.sort()
perms2.sort()
self.assertEqual(perms1, perms2)
b = access.AccessVectorSet() b = access.AccessVectorSet()
b.from_list(avl) b.from_list(avl)

View File

@ -24,7 +24,11 @@ import selinux
class TestIdSet(unittest.TestCase): class TestIdSet(unittest.TestCase):
def test_set_to_str(self): def test_set_to_str(self):
s = refpolicy.IdSet(["read", "write", "getattr"]) s = refpolicy.IdSet(["read", "write", "getattr"])
self.assertEqual(s.to_space_str(), "{ read write getattr }") s = s.to_space_str().split(' ')
s.sort()
expected = "{ read write getattr }".split(' ')
expected.sort()
self.assertEqual(s, expected)
s = refpolicy.IdSet() s = refpolicy.IdSet()
s.add("read") s.add("read")
self.assertEqual(s.to_space_str(), "read") self.assertEqual(s.to_space_str(), "read")
@ -100,8 +104,11 @@ class TestAVRule(unittest.TestCase):
a.obj_classes.add("lnk_file") a.obj_classes.add("lnk_file")
a.perms.add("write") a.perms.add("write")
# This test might need to go because set ordering is not guaranteed # This test might need to go because set ordering is not guaranteed
self.assertEqual(a.to_string(), a = a.to_string().split(' ')
"dontaudit { foo_t user_t } { user_home_t bar_t }:{ lnk_file file } { read write };") a.sort()
b = "dontaudit { foo_t user_t } { user_home_t bar_t }:{ lnk_file file } { read write };".split(' ')
b.sort()
self.assertEqual(a, b)
class TestTypeRule(unittest.TestCase): class TestTypeRule(unittest.TestCase):
def test_init(self): def test_init(self):