Commit 876e82b4 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-36234: Add more tests to PosixUidGidTests (GH-12234)

test_posix.PosixUidGidTests:

* Add tests for invalid uid/gid type (str)
* Add UID_OVERFLOW and GID_OVERFLOW constants to replace (1 << 32)

Initial patch written by David Malcolm.
Co-Authored-By: default avatarDavid Malcolm <dmalcolm@redhat.com>
parent cc353a0c
...@@ -1796,36 +1796,46 @@ class LinkTests(unittest.TestCase): ...@@ -1796,36 +1796,46 @@ class LinkTests(unittest.TestCase):
@unittest.skipIf(sys.platform == "win32", "Posix specific tests") @unittest.skipIf(sys.platform == "win32", "Posix specific tests")
class PosixUidGidTests(unittest.TestCase): class PosixUidGidTests(unittest.TestCase):
# uid_t and gid_t are 32-bit unsigned integers on Linux
UID_OVERFLOW = (1 << 32)
GID_OVERFLOW = (1 << 32)
@unittest.skipUnless(hasattr(os, 'setuid'), 'test needs os.setuid()') @unittest.skipUnless(hasattr(os, 'setuid'), 'test needs os.setuid()')
def test_setuid(self): def test_setuid(self):
if os.getuid() != 0: if os.getuid() != 0:
self.assertRaises(OSError, os.setuid, 0) self.assertRaises(OSError, os.setuid, 0)
self.assertRaises(OverflowError, os.setuid, 1<<32) self.assertRaises(TypeError, os.setuid, 'not an int')
self.assertRaises(OverflowError, os.setuid, self.UID_OVERFLOW)
@unittest.skipUnless(hasattr(os, 'setgid'), 'test needs os.setgid()') @unittest.skipUnless(hasattr(os, 'setgid'), 'test needs os.setgid()')
def test_setgid(self): def test_setgid(self):
if os.getuid() != 0 and not HAVE_WHEEL_GROUP: if os.getuid() != 0 and not HAVE_WHEEL_GROUP:
self.assertRaises(OSError, os.setgid, 0) self.assertRaises(OSError, os.setgid, 0)
self.assertRaises(OverflowError, os.setgid, 1<<32) self.assertRaises(TypeError, os.setgid, 'not an int')
self.assertRaises(OverflowError, os.setgid, self.GID_OVERFLOW)
@unittest.skipUnless(hasattr(os, 'seteuid'), 'test needs os.seteuid()') @unittest.skipUnless(hasattr(os, 'seteuid'), 'test needs os.seteuid()')
def test_seteuid(self): def test_seteuid(self):
if os.getuid() != 0: if os.getuid() != 0:
self.assertRaises(OSError, os.seteuid, 0) self.assertRaises(OSError, os.seteuid, 0)
self.assertRaises(OverflowError, os.seteuid, 1<<32) self.assertRaises(TypeError, os.setegid, 'not an int')
self.assertRaises(OverflowError, os.seteuid, self.UID_OVERFLOW)
@unittest.skipUnless(hasattr(os, 'setegid'), 'test needs os.setegid()') @unittest.skipUnless(hasattr(os, 'setegid'), 'test needs os.setegid()')
def test_setegid(self): def test_setegid(self):
if os.getuid() != 0 and not HAVE_WHEEL_GROUP: if os.getuid() != 0 and not HAVE_WHEEL_GROUP:
self.assertRaises(OSError, os.setegid, 0) self.assertRaises(OSError, os.setegid, 0)
self.assertRaises(OverflowError, os.setegid, 1<<32) self.assertRaises(TypeError, os.setegid, 'not an int')
self.assertRaises(OverflowError, os.setegid, self.GID_OVERFLOW)
@unittest.skipUnless(hasattr(os, 'setreuid'), 'test needs os.setreuid()') @unittest.skipUnless(hasattr(os, 'setreuid'), 'test needs os.setreuid()')
def test_setreuid(self): def test_setreuid(self):
if os.getuid() != 0: if os.getuid() != 0:
self.assertRaises(OSError, os.setreuid, 0, 0) self.assertRaises(OSError, os.setreuid, 0, 0)
self.assertRaises(OverflowError, os.setreuid, 1<<32, 0) self.assertRaises(TypeError, os.setreuid, 'not an int', 0)
self.assertRaises(OverflowError, os.setreuid, 0, 1<<32) self.assertRaises(TypeError, os.setreuid, 0, 'not an int')
self.assertRaises(OverflowError, os.setreuid, self.UID_OVERFLOW, 0)
self.assertRaises(OverflowError, os.setreuid, 0, self.UID_OVERFLOW)
@unittest.skipUnless(hasattr(os, 'setreuid'), 'test needs os.setreuid()') @unittest.skipUnless(hasattr(os, 'setreuid'), 'test needs os.setreuid()')
def test_setreuid_neg1(self): def test_setreuid_neg1(self):
...@@ -1839,8 +1849,10 @@ class PosixUidGidTests(unittest.TestCase): ...@@ -1839,8 +1849,10 @@ class PosixUidGidTests(unittest.TestCase):
def test_setregid(self): def test_setregid(self):
if os.getuid() != 0 and not HAVE_WHEEL_GROUP: if os.getuid() != 0 and not HAVE_WHEEL_GROUP:
self.assertRaises(OSError, os.setregid, 0, 0) self.assertRaises(OSError, os.setregid, 0, 0)
self.assertRaises(OverflowError, os.setregid, 1<<32, 0) self.assertRaises(TypeError, os.setregid, 'not an int', 0)
self.assertRaises(OverflowError, os.setregid, 0, 1<<32) self.assertRaises(TypeError, os.setregid, 0, 'not an int')
self.assertRaises(OverflowError, os.setregid, self.GID_OVERFLOW, 0)
self.assertRaises(OverflowError, os.setregid, 0, self.GID_OVERFLOW)
@unittest.skipUnless(hasattr(os, 'setregid'), 'test needs os.setregid()') @unittest.skipUnless(hasattr(os, 'setregid'), 'test needs os.setregid()')
def test_setregid_neg1(self): def test_setregid_neg1(self):
......
test_posix.PosixUidGidTests: add tests for invalid uid/gid type (str).
Initial patch written by David Malcolm.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment