import unittest
import sys
import os
import copy
import tempfile
# add compat assertIsInstance for old unittest.TestCase versions
# (python < 2.7, RHEL6 for instance)
# Python3 renamed assertItemsEqual to assertCountEqual but at the same time
# Python2 doesn't have assertCountEqual, see http://bugs.python.org/issue17866
# This is RHEL-6
"assertCountEqual",
" Make sure we load the in-tree module "
" Restore the system path "
" Import the module and assert it comes from tree "
try:
else:
import pyhbac
except ImportError as e:
raise e
# Test other iterables than list
# negative tests
def _get_rule():
name = "testGetRule"
new_name = "testGetNewRule"
# negative test
# rule should contain empty elements after instantiation
# Assign by copying a HbacRuleElement
# Assign directly
" Test that references to RuleElement are kept even if element goes out of scope "
def _get_rule():
return rule
"users <category 0 names [] groups []> "
"services <category 0 names [] groups []> "
"targethosts <category 0 names [] groups []> "
"srchosts <category 0 names [] groups []>>")
name = "someuser"
service = "ssh"
srchost = "host1"
targethost = "host2"
"users <category 0 names [%s] groups []> "
"services <category 0 names [%s] groups []> "
"targethosts <category 0 names [%s] groups []> "
"srchosts <category 0 names [%s] groups []>>" %
name = "foo"
name = "foo"
# Test other iterables than list
r = pyhbac.HbacRequestElement()
r.name = 'foo'
name = "req_name"
# The request should be empty after instantiation
# Assign by copying a HbacRequestElement
# Assign directly
# python 2.4 raises TypError, 2.7 raises AttributeError
name = "someuser"
service = "ssh"
srchost = "host1"
targethost = "host2"
# Test that an allow rule on its own allows access
# Test that a user not in the rule is not allowed
# But allows if the rule is an ALL rule
name = "someuser"
service = "ssh"
srchost = "host1"
targethost = "host2"
"service <name groups []> "
"targethost <name groups []> "
"srchost <name groups []>>")
"service <name %s groups []> "
"targethost <name %s groups []> "
"srchost <name %s groups []>>" %
name = "someuser"
service = "ssh"
srchost = "host1"
targethost = "host2"
# catch invalid category value
# Test that invalid type is raised
# catch invalid rule type
for r in results:
s = pyhbac.hbac_result_string(r)
assert len(s) > 0
for e in errors:
s = pyhbac.hbac_error_string(e)
assert len(s) > 0
if __name__ == "__main__":
if not res.wasSuccessful():
error |= 0x1
# need to bail out here because pyhbac could not be imported
# import the pyhbac module into the global namespace, but make sure it's
# the one in tree
import pyhbac
if not res.wasSuccessful():
error |= 0x2
if not res.wasSuccessful():
error |= 0x3
if not res.wasSuccessful():
error |= 0x4
if not res.wasSuccessful():
error |= 0x5
if not res.wasSuccessful():
error |= 0x6