pyhbac-test.py revision 22d268c88f6d324b3a66846af007b06488eddae7
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub HrozekMODPATH = srcdir + "/.libs" #FIXME - is there a way to get this from libtool?
7797e361155f7ce937085fd98e360469d7baf1b6Jakub Hrozekdef compat_assertItemsEqual(this, expected_seq, actual_seq, msg=None):
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek return self.assertEqual(sorted(expected_seq), sorted(actual_seq))
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagherdef compat_assertIsInstance(this, obj, cls, msg=None):
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher return self.assertTrue(isinstance(obj, cls))
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)
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek " Make sure we load the in-tree module "
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher " Restore the system path "
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher " Import the module and assert it comes from tree "
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher print >>sys.stderr, "Could not load the pyhbac module. Please check if it is compiled"
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek self.assertEqual(pyhbac.__file__, MODPATH + "/pyhbac.so")
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(el.category, set([pyhbac.HBAC_CATEGORY_NULL]))
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher el = pyhbac.HbacRuleElement(groups=groups)
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek # Test other iterables than list
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher assert pyhbac.HBAC_CATEGORY_NULL in el.category
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher assert pyhbac.HBAC_CATEGORY_ALL not in el.category
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher assert pyhbac.HBAC_CATEGORY_ALL in el.category
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher el.category = set([pyhbac.HBAC_CATEGORY_ALL])
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher assert pyhbac.HBAC_CATEGORY_ALL in el.category
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)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertRaises(TypeError, pyhbac.HbacRuleElement, names=123)
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher self.assertRaises(TypeError, pyhbac.HbacRuleElement, names=None)
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher return pyhbac.HbacRuleElement(names=users, groups=user_groups)
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher self.assertItemsEqual(el.names, [ "foo", "bar" ])
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher self.assertItemsEqual(el.groups, [ "abc", "def" ])
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher self.assertEquals(el.__repr__(), u'<category 0 names [] groups []>')
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEquals(el.__repr__(), u'<category 1 names [foo] groups [bar, baz]>')
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(rule.name, unicode(name))
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(rule.name, unicode(new_name))
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher rule = pyhbac.HbacRule("testRuleGetSetEnabled")
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)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher # rule should contain empty elements after instantiation
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)
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 # Assign by copying a HbacRuleElement
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher user_el = pyhbac.HbacRuleElement(names=users, groups=user_groups)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(rule.users.names, users)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(rule.users.groups, user_groups)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher # Assign directly
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(rule.users.names, users)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(rule.users.groups, user_groups)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher " Test that references to RuleElement are kept even if element goes out of scope "
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher el = pyhbac.HbacRuleElement(names=users, groups=user_groups)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(rule.users.names, [ "foo", "bar" ])
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertItemsEqual(rule.users.groups, [ "abc", "def" ])
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 []>>")
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 Hrozekclass PyHbacRequestElementTest(unittest.TestCase):
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek # Test other iterables than list
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek self.assertRaises(TypeError, pyhbac.HbacRequestElement, groups=None)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek self.assertRaises(TypeError, pyhbac.HbacRequestElement, groups=123)
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertEqual(r.__repr__(), u"<name groups []>")
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek self.assertEqual(r.__repr__(), u"<name foo groups [bar,baz]>")
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek # The request should be empty after instantiation
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)
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek # Assign by copying a HbacRequestElement
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek user_el = pyhbac.HbacRequestElement(name=name, groups=groups)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek # Assign directly
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek self.assertRaises(AttributeError, req.__setattr__, "rule_name", "foo")
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek allow_rule = pyhbac.HbacRule("allowRule", enabled=True)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher # Test that an allow rule on its own allows access
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher self.assertEqual(res, pyhbac.HBAC_EVAL_ALLOW)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher # Test that a user not in the rule is not allowed
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(res, pyhbac.HBAC_EVAL_DENY)
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher # But allows if the rule is an ALL rule
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek allow_rule.users.category.add(pyhbac.HBAC_CATEGORY_ALL)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(res, pyhbac.HBAC_EVAL_ALLOW)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertEqual(req.__repr__(), "<user <name groups []> "
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher "service <name groups []> "
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek "targethost <name groups []> "
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher "srchost <name groups []>>")
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 []>>" %
1008001f34abb42df75f840db17f14a83f0c21d4Stephen Gallagher allow_rule = pyhbac.HbacRule("allowRule", enabled=True)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher allow_rule.targethosts.names = [ targethost ]
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,))
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher # Test that invalid type is raised
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher self.assertRaises(TypeError, req.evaluate, (allow_rule,))
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek self.assertRaises(TypeError, req.evaluate, (allow_rule,))
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher # catch invalid rule type
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher self.assertRaises(TypeError, req.evaluate, (allow_rule, None))
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher assert hasattr(pyhbac, "HBAC_ERROR_UNKNOWN")
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")
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher results = [ pyhbac.HBAC_EVAL_ALLOW, pyhbac.HBAC_EVAL_DENY,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek suite = unittest.TestLoader().loadTestsFromTestCase(PyHbacImport)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek # need to bail out here because pyhbac could not be imported
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek # import the pyhbac module into the global namespace, but make sure it's
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek # the one in tree
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek suite = unittest.TestLoader().loadTestsFromTestCase(PyHbacRuleElementTest)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek suite = unittest.TestLoader().loadTestsFromTestCase(PyHbacRuleTest)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek suite = unittest.TestLoader().loadTestsFromTestCase(PyHbacRequestElementTest)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek suite = unittest.TestLoader().loadTestsFromTestCase(PyHbacRequestTest)
6b0f9cd2ee601121cb7fe1d9ad8ebce782aa8f39Stephen Gallagher res = unittest.TextTestRunner().run(suite)
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher suite = unittest.TestLoader().loadTestsFromTestCase(PyHbacModuleTest)