Test about privileged action
This commit is contained in:
25
tests/daemon_uid_gid_action.py
Normal file
25
tests/daemon_uid_gid_action.py
Normal file
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from os import getuid, geteuid, getgid, getegid
|
||||
from sys import argv
|
||||
from time import sleep
|
||||
|
||||
from daemonize import Daemonize
|
||||
|
||||
pid = argv[1]
|
||||
log = argv[2]
|
||||
|
||||
|
||||
def priv():
|
||||
return open(log, "w"),
|
||||
|
||||
|
||||
def main(f):
|
||||
uids = getuid(), geteuid()
|
||||
gids = getgid(), getegid()
|
||||
f.write(" ".join(map(str, uids + gids)))
|
||||
|
||||
|
||||
daemon = Daemonize(app="test_app", pid=pid, action=main, user="nobody", group="nobody",
|
||||
privileged_action=priv)
|
||||
daemon.start()
|
@@ -71,5 +71,17 @@ class UidGidTest(unittest.TestCase):
|
||||
with open(self.logfile, "r") as f:
|
||||
self.assertEqual(f.read(), " ".join(map(str, [nobody_uid] * 2 + [nobody_gid] * 2)))
|
||||
|
||||
@unittest.skipIf(os.getuid() != 0, "Requires supersuer privileges.")
|
||||
def test_uid_gid_action(self):
|
||||
nobody_uid = pwd.getpwnam("nobody").pw_uid
|
||||
nobody_gid = grp.getgrnam("nobody").gr_gid
|
||||
os.chown(self.pidfile, nobody_uid, nobody_gid)
|
||||
|
||||
os.system("python tests/daemon_uid_gid_action.py %s %s" % (self.pidfile, self.logfile))
|
||||
sleep(.1)
|
||||
|
||||
with open(self.logfile, "r") as f:
|
||||
self.assertEqual(f.read(), " ".join(map(str, [nobody_uid] * 2 + [nobody_gid] * 2)))
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
Reference in New Issue
Block a user