pyhbac-test.py revision 22d268c88f6d324b3a66846af007b06488eddae7
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher#!/usr/bin/python
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagherimport unittest
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagherimport sys
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagherimport os
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozekimport copy
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallaghersrcdir = os.getenv('builddir')
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozekif not srcdir:
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher srcdir = "."
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub HrozekMODPATH = srcdir + "/.libs" #FIXME - is there a way to get this from libtool?
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
7797e361155f7ce937085fd98e360469d7baf1b6Jakub Hrozekdef compat_assertItemsEqual(this, expected_seq, actual_seq, msg=None):
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek return self.assertEqual(sorted(expected_seq), sorted(actual_seq))
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagherdef compat_assertIsInstance(this, obj, cls, msg=None):
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher return self.assertTrue(isinstance(obj, cls))
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher# add compat methods for old unittest.TestCase versions
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek# (python < 2.7, RHEL5 for instance)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekif not hasattr(unittest.TestCase, "assertItemsEqual"):
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher setattr(unittest.TestCase, "assertItemsEqual", compat_assertItemsEqual)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagherif not hasattr(unittest.TestCase, "assertIsInstance"):
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher setattr(unittest.TestCase, "assertIsInstance", compat_assertIsInstance)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozekclass PyHbacImport(unittest.TestCase):
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek def setUp(self):
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek " Make sure we load the in-tree module "
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek self.system_path = sys.path[:]
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek sys.path = [ MODPATH ]
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek def tearDown(self):
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher " Restore the system path "
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher sys.path = self.system_path
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher def testImport(self):
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher " Import the module and assert it comes from tree "
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher try:
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher import pyhbac
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher except ImportError, e:
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher print >>sys.stderr, "Could not load the pyhbac module. Please check if it is compiled"
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher raise e
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek self.assertEqual(pyhbac.__file__, MODPATH + "/pyhbac.so")
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozekclass PyHbacRuleElementTest(unittest.TestCase):
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek def testInstantiateEmpty(self):
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher el = pyhbac.HbacRuleElement()
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(el.names, [])
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(el.groups, [])
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(el.category, set([pyhbac.HBAC_CATEGORY_NULL]))
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher def testInit(self):
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher names = [ "foo", "bar" ]
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher el = pyhbac.HbacRuleElement(names=names)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(el.names, names)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher groups = [ "abc", "def" ]
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher el = pyhbac.HbacRuleElement(groups=groups)
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher self.assertItemsEqual(el.groups, groups)
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher def testGetSet(self):
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher names = [ "foo", "bar" ]
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher el = pyhbac.HbacRuleElement()
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher self.assertItemsEqual(el.names, [])
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher el.names = names
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(el.names, names)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher groups = [ "abc", "def" ]
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek el = pyhbac.HbacRuleElement()
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek self.assertItemsEqual(el.groups, [])
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek el.groups = groups
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek self.assertItemsEqual(el.groups, groups)
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek # Test other iterables than list
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher groups = ( "abc", "def" )
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher el = pyhbac.HbacRuleElement()
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(el.groups, [])
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher el.groups = groups
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(el.groups, groups)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher def testCategory(self):
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher el = pyhbac.HbacRuleElement()
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher assert pyhbac.HBAC_CATEGORY_NULL in el.category
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher assert pyhbac.HBAC_CATEGORY_ALL not in el.category
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher el.category.add(pyhbac.HBAC_CATEGORY_ALL)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher assert pyhbac.HBAC_CATEGORY_ALL in el.category
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher el.category = set([pyhbac.HBAC_CATEGORY_ALL])
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher assert pyhbac.HBAC_CATEGORY_ALL in el.category
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek # negative tests
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertRaises(TypeError, el.__setattr__, "category", [pyhbac.HBAC_CATEGORY_ALL])
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertRaises(TypeError, el.__setattr__, "category", None)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertRaises(TypeError, el.__setattr__, "category", 1)
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher def testNotIterable(self):
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertRaises(TypeError, pyhbac.HbacRuleElement, names=123)
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher self.assertRaises(TypeError, pyhbac.HbacRuleElement, names=None)
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher def testRuleElementReference(self):
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher def _get_rule():
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher users = [ "foo", "bar" ]
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher user_groups = [ "abc", "def" ]
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher return pyhbac.HbacRuleElement(names=users, groups=user_groups)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher el = _get_rule()
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher self.assertItemsEqual(el.names, [ "foo", "bar" ])
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher self.assertItemsEqual(el.groups, [ "abc", "def" ])
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher def testRepr(self):
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher el = pyhbac.HbacRuleElement()
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher self.assertEquals(el.__repr__(), u'<category 0 names [] groups []>')
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher el.category.add(pyhbac.HBAC_CATEGORY_ALL)
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher el.names = ['foo']
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher el.groups = ['bar, baz']
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEquals(el.__repr__(), u'<category 1 names [foo] groups [bar, baz]>')
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagherclass PyHbacRuleTest(unittest.TestCase):
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher def testRuleGetSetName(self):
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher name = "testGetRule"
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher new_name = "testGetNewRule"
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule = pyhbac.HbacRule(name)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(rule.name, unicode(name))
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher rule.name = new_name
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(rule.name, unicode(new_name))
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher def testRuleGetSetEnabled(self):
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule = pyhbac.HbacRule("testRuleGetSetEnabled")
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule.enabled = True
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(rule.enabled, True)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule.enabled = False
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(rule.enabled, False)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule.enabled = "TRUE"
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek self.assertEqual(rule.enabled, True)
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek rule.enabled = "FALSE"
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(rule.enabled, False)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule.enabled = "true"
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(rule.enabled, True)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule.enabled = "false"
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek self.assertEqual(rule.enabled, False)
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule.enabled = "True"
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(rule.enabled, True)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule.enabled = "False"
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(rule.enabled, False)
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule.enabled = 1
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(rule.enabled, True)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule.enabled = 0
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(rule.enabled, False)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher # negative test
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertRaises(TypeError, rule.__setattr__, "enabled", None)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertRaises(TypeError, rule.__setattr__, "enabled", [])
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertRaises(ValueError, rule.__setattr__, "enabled", "foo")
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertRaises(ValueError, rule.__setattr__, "enabled", 5)
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek def testRuleElementInRule(self):
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek users = [ "foo", "bar" ]
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek user_groups = [ "abc", "def" ]
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher # rule should contain empty elements after instantiation
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule = pyhbac.HbacRule("testRuleElement")
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertIsInstance(rule.users, pyhbac.HbacRuleElement)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertIsInstance(rule.services, pyhbac.HbacRuleElement)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertIsInstance(rule.targethosts, pyhbac.HbacRuleElement)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertIsInstance(rule.srchosts, pyhbac.HbacRuleElement)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher self.assertIsInstance(rule.users.names, list)
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher self.assertIsInstance(rule.users.groups, list)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(rule.users.names, [])
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(rule.users.groups, [])
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher # Assign by copying a HbacRuleElement
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher user_el = pyhbac.HbacRuleElement(names=users, groups=user_groups)
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher rule = pyhbac.HbacRule("testRuleElement")
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher rule.users = user_el
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(rule.users.names, users)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(rule.users.groups, user_groups)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher # Assign directly
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule = pyhbac.HbacRule("testRuleElement")
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule.users.names = users
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule.users.groups = user_groups
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(rule.users.names, users)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(rule.users.groups, user_groups)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher def testRuleElementInRuleReference(self):
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher " Test that references to RuleElement are kept even if element goes out of scope "
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher def _get_rule():
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher users = [ "foo", "bar" ]
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher user_groups = [ "abc", "def" ]
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher el = pyhbac.HbacRuleElement(names=users, groups=user_groups)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule = pyhbac.HbacRule("testRuleElement")
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule.users = el
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher return rule
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule = _get_rule()
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(rule.users.names, [ "foo", "bar" ])
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(rule.users.groups, [ "abc", "def" ])
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher def testRepr(self):
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher r = pyhbac.HbacRule('foo')
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(r.__repr__(), u"<name foo enabled 0 "
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher "users <category 0 names [] groups []> "
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher "services <category 0 names [] groups []> "
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher "targethosts <category 0 names [] groups []> "
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher "srchosts <category 0 names [] groups []>>")
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher name = "someuser"
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher service = "ssh"
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher srchost = "host1"
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher targethost = "host2"
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek r.users.names = [ name ]
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek r.services.names = [ service ]
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek r.srchosts.names = [ srchost ]
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek r.targethosts.names = [ targethost ]
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertEqual(r.__repr__(), u"<name foo enabled 0 "
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek "users <category 0 names [%s] groups []> "
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek "services <category 0 names [%s] groups []> "
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek "targethosts <category 0 names [%s] groups []> "
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek "srchosts <category 0 names [%s] groups []>>" %
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek (name, service, targethost, srchost))
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozekclass PyHbacRequestElementTest(unittest.TestCase):
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek def testInstantiateEmpty(self):
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek el = pyhbac.HbacRequestElement()
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertItemsEqual(el.name, "")
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertItemsEqual(el.groups, [])
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek def testInit(self):
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek name = "foo"
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek el = pyhbac.HbacRequestElement(name=name)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek self.assertItemsEqual(el.name, name)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek groups = [ "abc", "def" ]
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek el = pyhbac.HbacRequestElement(groups=groups)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek self.assertItemsEqual(el.groups, groups)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek def testGetSet(self):
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek name = "foo"
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek el = pyhbac.HbacRequestElement()
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek self.assertItemsEqual(el.name, "")
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek el.name = name
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek self.assertItemsEqual(el.name, name)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek groups = [ "abc", "def" ]
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek el = pyhbac.HbacRequestElement()
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertItemsEqual(el.groups, [])
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek el.groups = groups
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertItemsEqual(el.groups, groups)
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek # Test other iterables than list
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek groups = ( "abc", "def" )
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek el = pyhbac.HbacRequestElement()
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek self.assertItemsEqual(el.groups, [])
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek el.groups = groups
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertItemsEqual(el.groups, groups)
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek def testGroupsNotIterable(self):
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek self.assertRaises(TypeError, pyhbac.HbacRequestElement, groups=None)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek self.assertRaises(TypeError, pyhbac.HbacRequestElement, groups=123)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek def testRepr(self):
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek r = pyhbac.HbacRequestElement()
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertEqual(r.__repr__(), u"<name groups []>")
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek r.name = 'foo'
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek r.groups = ['bar', 'baz']
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertEqual(r.__repr__(), u"<name foo groups [bar,baz]>")
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozekclass PyHbacRequestTest(unittest.TestCase):
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek def testRequestElementHandling(self):
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek name = "req_name"
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek groups = [ "g1", "g2" ]
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek # The request should be empty after instantiation
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek req = pyhbac.HbacRequest()
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertIsInstance(req.user, pyhbac.HbacRequestElement)
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertIsInstance(req.service, pyhbac.HbacRequestElement)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek self.assertIsInstance(req.targethost, pyhbac.HbacRequestElement)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek self.assertIsInstance(req.srchost, pyhbac.HbacRequestElement)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek self.assertEqual(req.user.name, "")
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek self.assertIsInstance(req.user.groups, list)
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertItemsEqual(req.user.groups, [])
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek # Assign by copying a HbacRequestElement
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek user_el = pyhbac.HbacRequestElement(name=name, groups=groups)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek req = pyhbac.HbacRequest()
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek req.user = user_el
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertItemsEqual(req.user.name, name)
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertItemsEqual(req.user.groups, groups)
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek # Assign directly
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek req = pyhbac.HbacRequest()
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek req.user.name = name
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek req.user.groups = groups
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertItemsEqual(req.user.name, name)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek self.assertItemsEqual(req.user.groups, groups)
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek def testRuleName(self):
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek req = pyhbac.HbacRequest()
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertEqual(req.rule_name, None)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek self.assertRaises(AttributeError, req.__setattr__, "rule_name", "foo")
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek def testEvaluate(self):
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek name = "someuser"
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek service = "ssh"
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek srchost = "host1"
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek targethost = "host2"
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek allow_rule = pyhbac.HbacRule("allowRule", enabled=True)
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek allow_rule.users.names = [ name ]
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek allow_rule.services.names = [ service ]
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek allow_rule.srchosts.names = [ srchost ]
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek allow_rule.targethosts.names = [ targethost ]
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher req = pyhbac.HbacRequest()
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher req.user.name = name
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher req.service.name = service
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher req.srchost.name = srchost
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek req.targethost.name = targethost
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher # Test that an allow rule on its own allows access
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher res = req.evaluate((allow_rule,))
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher self.assertEqual(res, pyhbac.HBAC_EVAL_ALLOW)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek self.assertEqual(req.rule_name, "allowRule")
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher # Test that a user not in the rule is not allowed
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher savename = req.user.name
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher req.user.name = "someotheruser"
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek res = req.evaluate((allow_rule, ))
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(res, pyhbac.HBAC_EVAL_DENY)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(req.rule_name, None)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher # But allows if the rule is an ALL rule
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek allow_rule.users.category.add(pyhbac.HBAC_CATEGORY_ALL)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher res = req.evaluate((allow_rule, ))
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(res, pyhbac.HBAC_EVAL_ALLOW)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher def testRepr(self):
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher name = "someuser"
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher service = "ssh"
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher srchost = "host1"
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher targethost = "host2"
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher req = pyhbac.HbacRequest()
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(req.__repr__(), "<user <name groups []> "
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher "service <name groups []> "
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek "targethost <name groups []> "
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher "srchost <name groups []>>")
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher req.user.name = name
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher req.service.name = service
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher req.srchost.name = srchost
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher req.targethost.name = targethost
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
056302a92862fda16351d7192600746746f38e5dStephen Gallagher self.assertEqual(req.__repr__(), "<user <name %s groups []> "
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher "service <name %s groups []> "
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher "targethost <name %s groups []> "
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher "srchost <name %s groups []>>" %
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek (name, service, targethost, srchost))
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher def testEvaluateNegative(self):
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher name = "someuser"
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek service = "ssh"
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher srchost = "host1"
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher targethost = "host2"
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher allow_rule = pyhbac.HbacRule("allowRule", enabled=True)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek allow_rule.users.names = [ name ]
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher allow_rule.services.names = [ service ]
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher allow_rule.srchosts.names = [ srchost ]
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher allow_rule.targethosts.names = [ targethost ]
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher req = pyhbac.HbacRequest()
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher req.service.name = service
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher req.srchost.name = srchost
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher req.targethost.name = targethost
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek req.user.name = name
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher saveuser = req.user
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher req.user = None # need to catch this
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek # catch invalid category value
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher savecat = copy.copy(allow_rule.users.category)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher allow_rule.users.category.add(pyhbac.HBAC_EVAL_ERROR)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertRaises(ValueError, req.evaluate, (allow_rule,))
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher allow_rule.users.category = savecat
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher # Test that invalid type is raised
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertRaises(TypeError, req.evaluate, (allow_rule,))
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher req.user = saveuser
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek allow_rule.users = None # need to catch this
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek self.assertRaises(TypeError, req.evaluate, (allow_rule,))
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher # catch invalid rule type
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher self.assertRaises(TypeError, req.evaluate, (allow_rule, None))
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagherclass PyHbacModuleTest(unittest.TestCase):
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher def testHasResultTypes(self):
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher assert hasattr(pyhbac, "HBAC_EVAL_ALLOW")
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher assert hasattr(pyhbac, "HBAC_EVAL_DENY")
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek assert hasattr(pyhbac, "HBAC_EVAL_ERROR")
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher def testHasErrorTypes(self):
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher assert hasattr(pyhbac, "HBAC_ERROR_UNKNOWN")
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher assert hasattr(pyhbac, "HBAC_SUCCESS")
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher assert hasattr(pyhbac, "HBAC_ERROR_NOT_IMPLEMENTED")
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher assert hasattr(pyhbac, "HBAC_ERROR_OUT_OF_MEMORY")
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek assert hasattr(pyhbac, "HBAC_ERROR_UNPARSEABLE_RULE")
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek def testHasCategories(self):
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek assert hasattr(pyhbac, "HBAC_CATEGORY_NULL")
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek assert hasattr(pyhbac, "HBAC_CATEGORY_ALL")
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher def testHbacResultString(self):
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher results = [ pyhbac.HBAC_EVAL_ALLOW, pyhbac.HBAC_EVAL_DENY,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek pyhbac.HBAC_EVAL_ERROR ]
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher for r in results:
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher s = pyhbac.hbac_result_string(r)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertIsInstance(s, unicode)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek assert len(s) > 0
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek def testHbacErrorString(self):
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek errors = [ pyhbac.HBAC_ERROR_UNKNOWN,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek pyhbac.HBAC_SUCCESS,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek pyhbac.HBAC_ERROR_NOT_IMPLEMENTED,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek pyhbac.HBAC_ERROR_OUT_OF_MEMORY,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek pyhbac.HBAC_ERROR_UNPARSEABLE_RULE ]
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek for e in errors:
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek s = pyhbac.hbac_error_string(e)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek self.assertIsInstance(s, unicode)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek assert len(s) > 0
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekif __name__ == "__main__":
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek error = 0
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek suite = unittest.TestLoader().loadTestsFromTestCase(PyHbacImport)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek res = unittest.TextTestRunner().run(suite)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if not res.wasSuccessful():
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek error |= 0x1
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek # need to bail out here because pyhbac could not be imported
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek sys.exit(error)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek # import the pyhbac module into the global namespace, but make sure it's
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek # the one in tree
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek sys.path.insert(0, MODPATH)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek import pyhbac
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek suite = unittest.TestLoader().loadTestsFromTestCase(PyHbacRuleElementTest)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek res = unittest.TextTestRunner().run(suite)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if not res.wasSuccessful():
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek error |= 0x2
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek suite = unittest.TestLoader().loadTestsFromTestCase(PyHbacRuleTest)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek res = unittest.TextTestRunner().run(suite)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if not res.wasSuccessful():
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek error |= 0x3
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek suite = unittest.TestLoader().loadTestsFromTestCase(PyHbacRequestElementTest)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek res = unittest.TextTestRunner().run(suite)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if not res.wasSuccessful():
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher error |= 0x4
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek suite = unittest.TestLoader().loadTestsFromTestCase(PyHbacRequestTest)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher res = unittest.TextTestRunner().run(suite)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher if not res.wasSuccessful():
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher error |= 0x5
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher suite = unittest.TestLoader().loadTestsFromTestCase(PyHbacModuleTest)
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher res = unittest.TextTestRunner().run(suite)
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if not res.wasSuccessful():
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher error |= 0x6
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher sys.exit(error)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher