def testUser1(self): if not os.path.exists("/etc/passwd"): return passwd = file(os.path.join(self.test_root, "etc", "passwd"), "w") passwd.write( """root:x:0:0::/root:/usr/bin/bash gk:x:0:0::/root:/usr/bin/bash daemon:x:1:1::/: bin:x:2:2::/usr/bin: sys:x:3:3::/: adm:x:4:4:Admin:/var/adm: lp:x:71:8:Line Printer Admin:/usr/spool/lp: uucp:x:5:5:uucp Admin:/usr/lib/uucp: moop:x:999:999:moop:/usr/moop:""" ) passwd.close() self.assertRaises(KeyError, portable.get_user_by_name, "ThisShouldNotExist", self.test_root, True) self.assert_(0 == portable.get_user_by_name("root", self.test_root, True)) self.assert_(0 == portable.get_user_by_name("gk", self.test_root, True)) self.assert_(999 == portable.get_user_by_name("moop", self.test_root, True)) self.assertRaises(KeyError, portable.get_name_by_uid, 12345, self.test_root, True)
def testUser1(self): if not os.path.exists("/etc/passwd"): return passwd = open(os.path.join(self.test_root, "etc", "passwd"), "w") passwd.write( \ """root:x:0:0::/root:/usr/bin/bash gk:x:0:0::/root:/usr/bin/bash daemon:x:1:1::/: bin:x:2:2::/usr/bin: sys:x:3:3::/: adm:x:4:4:Admin:/var/adm: lp:x:71:8:Line Printer Admin:/usr/spool/lp: uucp:x:5:5:uucp Admin:/usr/lib/uucp: moop:x:999:999:moop:/usr/moop:""") passwd.close() self.assertRaises(KeyError, portable.get_user_by_name, "ThisShouldNotExist", self.test_root, True) self.assertTrue(0 == \ portable.get_user_by_name("root", self.test_root, True)) self.assertTrue(0 == \ portable.get_user_by_name("gk", self.test_root, True)) self.assertTrue(999 == \ portable.get_user_by_name("moop", self.test_root, True)) self.assertRaises(KeyError, portable.get_name_by_uid, 12345, self.test_root, True)
def testUser4(self): """ Test with a user name line in the passwd file that starts with a "+". (See bug #4470 for more details). """ if not os.path.exists("/etc/passwd"): return passwd = file(os.path.join(self.test_root, "etc", "passwd"), "w") passwd.write( """root:x:0:0::/root:/usr/bin/bash gk:x:0:0::/root:/usr/bin/bash daemon:x:1:1::/: bin:x:2:2::/usr/bin: sys:x:3:3::/: +plususer adm:x:4:4:Admin:/var/adm: lp:x:71:8:Line Printer Admin:/usr/spool/lp: uucp:x:5:5:uucp Admin:/usr/lib/uucp: +""" ) passwd.close() self.assert_(0 == portable.get_user_by_name("root", self.test_root, True)) self.assert_(0 == portable.get_user_by_name("gk", self.test_root, True)) self.assert_("root" == portable.get_name_by_uid(0, self.test_root, True)) self.assert_("uucp" == portable.get_name_by_uid(5, self.test_root, True)) self.assertRaises(KeyError, portable.get_user_by_name, "plususer", self.test_root, True)
def testUser4(self): """ Test with a user name line in the passwd file that starts with a "+". (See bug #4470 for more details). """ if not os.path.exists("/etc/passwd"): return passwd = open(os.path.join(self.test_root, "etc", "passwd"), "w") passwd.write( \ """root:x:0:0::/root:/usr/bin/bash gk:x:0:0::/root:/usr/bin/bash daemon:x:1:1::/: bin:x:2:2::/usr/bin: sys:x:3:3::/: +plususer adm:x:4:4:Admin:/var/adm: lp:x:71:8:Line Printer Admin:/usr/spool/lp: uucp:x:5:5:uucp Admin:/usr/lib/uucp: +""") passwd.close() self.assertTrue(0 == \ portable.get_user_by_name("root", self.test_root, True)) self.assertTrue(0 == \ portable.get_user_by_name("gk", self.test_root, True)) self.assertTrue("root" == \ portable.get_name_by_uid(0, self.test_root, True)) self.assertTrue("uucp" == \ portable.get_name_by_uid(5, self.test_root, True)) self.assertRaises(KeyError, portable.get_user_by_name, "plususer", self.test_root, True)
def testUser3(self): """ Test with an oddball/corrupt passwd file """ if not os.path.exists("/etc/passwd"): return passwd = file(os.path.join(self.test_root, "etc", "passwd"), "w") passwd.write( """root:x:0:0::/root:/usr/bin/bash daemon:x:1:1::/: bin:x:2:2::/usr/bin: sys:x:3:3::/: blorg corrupt:x gk:x:0:0::/root:/usr/bin/bash adm:x:4:4:Admin:/var/adm: lp:x:71:8:Line Printer Admin:/usr/spool/lp: uucp:x:5:5:uucp Admin:/usr/lib/uucp: +""" ) passwd.close() self.assert_(0 == portable.get_user_by_name("root", self.test_root, True)) self.assert_(0 == portable.get_user_by_name("gk", self.test_root, True)) self.assert_("uucp" == portable.get_name_by_uid(5, self.test_root, True)) self.assertRaises(KeyError, portable.get_user_by_name, "ThisShouldNotExist", self.test_root, True) self.assertRaises(KeyError, portable.get_user_by_name, "corrupt", self.test_root, True) self.assertRaises(KeyError, portable.get_user_by_name, 999, self.test_root, True)
def __do_alter_only_verify(self, pfmri, verbose=False, quiet=False, exit=0, parsable=False, debug=""): # Alter the owner, group, mode of all files (and # directories) to something different than the package declares. m = manifest.Manifest() m.set_content(self.get_img_manifest(pfmri)) for a in m.gen_actions(): if a.name not in ("file", "dir"): # Only want file or dir actions. continue ubin = portable.get_user_by_name("bin", None, False) groot = portable.get_group_by_name("root", None, False) fname = a.attrs["path"] fpath = os.path.join(self.get_img_path(), fname) os.chown(fpath, ubin, groot) os.chmod(fpath, misc.PKG_RO_FILE_MODE) verify_cmd = "verify" if verbose: verify_cmd += " -v" if quiet: verify_cmd += " -q" if parsable: verify_cmd += " --parsable=0" self.pkg("{0}{1} {2}".format(debug, verify_cmd, pfmri), exit=exit) if exit == 0: self.assertTrue("OK" in self.output and "ERROR" not in self.output) return exit
def damage_all_files(self): ubin = portable.get_user_by_name("bin", None, False) groot = portable.get_group_by_name("root", None, False) for path in self.misc_files: file_path = os.path.join(self.get_img_path(), path) with open(file_path, "a+") as f: f.write("\nbogus\n") os.chown(file_path, ubin, groot) os.chmod(file_path, misc.PKG_RO_FILE_MODE)
def damage_files(self, paths): ubin = portable.get_user_by_name("bin", None, False) groot = portable.get_group_by_name("root", None, False) for path in paths: file_path = os.path.join(self.get_img_path(), path) with open(file_path, "a+") as f: f.write("\nbogus\n") os.chown(file_path, ubin, groot) os.chmod(file_path, misc.PKG_RO_FILE_MODE)
def testUser2(self): """ Test with a missing passwd file """ if not os.path.exists("/etc/passwd"): return self.assertRaises(KeyError, portable.get_user_by_name, "ThisShouldNotExist", self.test_root, True) # This should work on unix systems, since we'll "bootstrap" # out to the OS's version. self.assert_(0 == portable.get_user_by_name("root", self.test_root, True)) self.assert_("root" == portable.get_name_by_uid(0, self.test_root, True))
def create_some_files(self, paths): ubin = portable.get_user_by_name("bin", None, False) groot = portable.get_group_by_name("root", None, False) for p in paths: file_path = os.path.join(self.get_img_path(), p) dirpath = os.path.dirname(file_path) if not os.path.exists(dirpath): os.mkdir(dirpath) with open(file_path, "a+") as f: f.write("\ncontents\n") os.chown(file_path, ubin, groot) os.chmod(file_path, misc.PKG_RO_FILE_MODE)
def _chown_cache_dir(dir): """Sets ownership for cache directory as pkg5srv:bin""" uid = portable.get_user_by_name(SYSREPO_USER, None, False) gid = portable.get_group_by_name("bin", None, False) try: os.chown(dir, uid, gid) except OSError as err: if not os.environ.get("PKG5_TEST_ENV", None): raise SysrepoException( _("Unable to chown to {user}:{group}: " "{err}").format(user=SYSREPO_USER, group="bin", err=err))
def _chown_dir(dir): """Sets ownership for the given directory to pkg5srv:pkg5srv""" uid = portable.get_user_by_name(DEPOT_USER, None, False) gid = portable.get_group_by_name(DEPOT_GROUP, None, False) try: os.chown(dir, uid, gid) except OSError as err: if not os.environ.get("PKG5_TEST_ENV", None): raise DepotException(_("Unable to chown {dir} to " "{user}:{group}: {err}").format( dir=dir, user=DEPOT_USER, group=DEPOT_GROUP, err=err))
def _chown_dir(dir): """Sets ownership for the given directory to pkg5srv:pkg5srv""" uid = portable.get_user_by_name(DEPOT_USER, None, False) gid = portable.get_group_by_name(DEPOT_GROUP, None, False) try: os.chown(dir, uid, gid) except OSError, err: if not os.environ.get("PKG5_TEST_ENV", None): raise DepotException(_("Unable to chown %(dir)s to " "%(user)s:%(group)s: %(err)s") % {"dir": dir, "user": DEPOT_USER, "group": DEPOT_GROUP, "err": err})
def testUser2(self): """ Test with a missing passwd file """ if not os.path.exists("/etc/passwd"): return self.assertRaises(KeyError, portable.get_user_by_name, "ThisShouldNotExist", self.test_root, True) # This should work on unix systems, since we'll "bootstrap" # out to the OS's version. self.assertTrue(0 == \ portable.get_user_by_name("root", self.test_root, True)) self.assertTrue("root" == \ portable.get_name_by_uid(0, self.test_root, True))
def _chown_cache_dir(dir): """Sets ownership for cache directory as pkg5srv:bin""" uid = portable.get_user_by_name(SYSREPO_USER, None, False) gid = portable.get_group_by_name("bin", None, False) try: os.chown(dir, uid, gid) except OSError, err: if not os.environ.get("PKG5_TEST_ENV", None): raise SysrepoException( _("Unable to chown to %(user)s:%(group)s: " "%(err)s") % {"user": SYSREPO_USER, "group": "bin", "err": err})
def testUser3(self): """ Test with an oddball/corrupt passwd file """ if not os.path.exists("/etc/passwd"): return passwd = open(os.path.join(self.test_root, "etc", "passwd"), "w") passwd.write( \ """root:x:0:0::/root:/usr/bin/bash daemon:x:1:1::/: bin:x:2:2::/usr/bin: sys:x:3:3::/: blorg corrupt:x gk:x:0:0::/root:/usr/bin/bash adm:x:4:4:Admin:/var/adm: lp:x:71:8:Line Printer Admin:/usr/spool/lp: uucp:x:5:5:uucp Admin:/usr/lib/uucp: +""") passwd.close() self.assertTrue(0 == \ portable.get_user_by_name("root", self.test_root, True)) self.assertTrue(0 == \ portable.get_user_by_name("gk", self.test_root, True)) self.assertTrue("uucp" == \ portable.get_name_by_uid(5, self.test_root, True)) self.assertRaises(KeyError, portable.get_user_by_name, "ThisShouldNotExist", self.test_root, True) self.assertRaises(KeyError, portable.get_user_by_name, "corrupt", self.test_root, True) self.assertRaises(KeyError, portable.get_user_by_name, 999, self.test_root, True)
def _chown_runtime_dir(runtime_dir): """Change the ownership of all files under runtime_dir to our sysrepo user/group""" uid = portable.get_user_by_name(SYSREPO_USER, None, False) gid = portable.get_group_by_name(SYSREPO_GROUP, None, False) try: misc.recursive_chown_dir(runtime_dir, uid, gid) except OSError as err: if not os.environ.get("PKG5_TEST_ENV", None): raise SysrepoException( _("Unable to chown to {user}:{group}: " "{err}").format(user=SYSREPO_USER, group=SYSREPO_GROUP, err=err))
def _chown_runtime_dir(runtime_dir): """Change the ownership of all files under runtime_dir to our sysrepo user/group""" uid = portable.get_user_by_name(SYSREPO_USER, None, False) gid = portable.get_group_by_name(SYSREPO_GROUP, None, False) try: misc.recursive_chown_dir(runtime_dir, uid, gid) except OSError, err: if not os.environ.get("PKG5_TEST_ENV", None): raise SysrepoException( _("Unable to chown to %(user)s:%(group)s: " "%(err)s") % {"user": SYSREPO_USER, "group": SYSREPO_GROUP, "err": err})
def __do_alter_verify(self, pfmri, verbose=False, quiet=False, exit=0, parsable=False): # Alter the owner, group, mode, and timestamp of all files (and # directories) to something different than the package declares. m = manifest.Manifest() m.set_content(self.get_img_manifest(pfmri)) ctime = time.time() - 1000 for a in m.gen_actions(): if a.name not in ("file", "dir"): # Only want file or dir actions. continue ubin = portable.get_user_by_name("bin", None, False) groot = portable.get_group_by_name("root", None, False) fname = a.attrs["path"] fpath = os.path.join(self.get_img_path(), fname) os.chown(fpath, ubin, groot) os.chmod(fpath, misc.PKG_RO_FILE_MODE) os.utime(fpath, (ctime, ctime)) # Call pkg fix to fix them. fix_cmd = "fix" if verbose: fix_cmd += " -v" if quiet: fix_cmd += " -q" if parsable: fix_cmd += " --parsable=0" self.pkg("{0} {1}".format(fix_cmd, pfmri), exit=exit) if exit != 0: return exit editables = [] # Now verify that fix actually fixed them. for a in m.gen_actions(): if a.name not in ("file", "dir"): # Only want file or dir actions. continue # Validate standard attributes. self.validate_fsobj_attrs(a) # Now validate attributes that require special handling. fname = a.attrs["path"] fpath = os.path.join(self.get_img_path(), fname) lstat = os.lstat(fpath) # Verify that preserved files don't get renamed, and # the new ones are not installed if the file wasn't # missing already. preserve = a.attrs.get("preserve") if preserve == "renamenew": self.assert_(not os.path.exists(fpath + ".new")) elif preserve == "renameold": self.assert_(not os.path.exists(fpath + ".old")) if preserve: editables.append("{0}".format(a.attrs["path"])) # Verify timestamp (if applicable). ts = a.attrs.get("timestamp") if ts: expected = misc.timestamp_to_time(ts) actual = lstat.st_mtime if preserve: self.assertNotEqual( expected, actual, "timestamp expected {expected} == " "actual {actual} for " "{fname}".format(expected=expected, actual=actual, fname=fname)) else: self.assertEqual( expected, actual, "timestamp expected {expected} != " "actual {actual} for " "{fname}".format(expected=expected, actual=actual, fname=fname)) # Verify the parsable output (if applicable). if parsable: if editables: self.assertEqualParsable( self.output, affect_packages=["{0}".format(pfmri)], change_editables=[["updated", editables]]) else: self.assertEqualParsable(self.output, affect_packages=["{0}".format(pfmri)])