2003-04-15 08:10:33 -03:00
|
|
|
import unittest
|
|
|
|
from test import test_support
|
|
|
|
|
1996-12-11 12:53:51 -04:00
|
|
|
import pwd
|
|
|
|
|
2003-04-15 08:10:33 -03:00
|
|
|
class PwdTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_values(self):
|
|
|
|
entries = pwd.getpwall()
|
2003-04-22 08:05:57 -03:00
|
|
|
entriesbyname = {}
|
2003-04-15 12:39:08 -03:00
|
|
|
entriesbyuid = {}
|
2003-04-15 08:10:33 -03:00
|
|
|
|
|
|
|
for e in entries:
|
|
|
|
self.assertEqual(len(e), 7)
|
|
|
|
self.assertEqual(e[0], e.pw_name)
|
2007-10-16 15:12:55 -03:00
|
|
|
self.assert_(isinstance(e.pw_name, str))
|
2003-04-15 08:10:33 -03:00
|
|
|
self.assertEqual(e[1], e.pw_passwd)
|
2007-10-16 15:12:55 -03:00
|
|
|
self.assert_(isinstance(e.pw_passwd, str))
|
2003-04-15 08:10:33 -03:00
|
|
|
self.assertEqual(e[2], e.pw_uid)
|
|
|
|
self.assert_(isinstance(e.pw_uid, int))
|
|
|
|
self.assertEqual(e[3], e.pw_gid)
|
|
|
|
self.assert_(isinstance(e.pw_gid, int))
|
|
|
|
self.assertEqual(e[4], e.pw_gecos)
|
2007-10-16 15:12:55 -03:00
|
|
|
self.assert_(isinstance(e.pw_gecos, str))
|
2003-04-15 08:10:33 -03:00
|
|
|
self.assertEqual(e[5], e.pw_dir)
|
2007-10-16 15:12:55 -03:00
|
|
|
self.assert_(isinstance(e.pw_dir, str))
|
2003-04-15 08:10:33 -03:00
|
|
|
self.assertEqual(e[6], e.pw_shell)
|
2007-10-16 15:12:55 -03:00
|
|
|
self.assert_(isinstance(e.pw_shell, str))
|
2003-04-15 08:10:33 -03:00
|
|
|
|
2003-04-15 12:39:08 -03:00
|
|
|
# The following won't work, because of duplicate entries
|
|
|
|
# for one uid
|
|
|
|
# self.assertEqual(pwd.getpwuid(e.pw_uid), e)
|
|
|
|
# instead of this collect all entries for one uid
|
|
|
|
# and check afterwards
|
2003-04-22 08:05:57 -03:00
|
|
|
entriesbyname.setdefault(e.pw_name, []).append(e)
|
2003-04-15 12:39:08 -03:00
|
|
|
entriesbyuid.setdefault(e.pw_uid, []).append(e)
|
|
|
|
|
|
|
|
# check whether the entry returned by getpwuid()
|
|
|
|
# for each uid is among those from getpwall() for this uid
|
|
|
|
for e in entries:
|
2005-12-12 16:53:40 -04:00
|
|
|
if not e[0] or e[0] == '+':
|
|
|
|
continue # skip NIS entries etc.
|
2003-04-22 08:05:57 -03:00
|
|
|
self.assert_(pwd.getpwnam(e.pw_name) in entriesbyname[e.pw_name])
|
2003-04-15 12:39:08 -03:00
|
|
|
self.assert_(pwd.getpwuid(e.pw_uid) in entriesbyuid[e.pw_uid])
|
2003-04-15 08:10:33 -03:00
|
|
|
|
|
|
|
def test_errors(self):
|
|
|
|
self.assertRaises(TypeError, pwd.getpwuid)
|
|
|
|
self.assertRaises(TypeError, pwd.getpwnam)
|
|
|
|
self.assertRaises(TypeError, pwd.getpwall, 42)
|
|
|
|
|
|
|
|
# try to get some errors
|
|
|
|
bynames = {}
|
|
|
|
byuids = {}
|
|
|
|
for (n, p, u, g, gecos, d, s) in pwd.getpwall():
|
|
|
|
bynames[n] = u
|
|
|
|
byuids[u] = n
|
|
|
|
|
2007-02-11 14:54:18 -04:00
|
|
|
allnames = list(bynames.keys())
|
2003-04-15 08:10:33 -03:00
|
|
|
namei = 0
|
|
|
|
fakename = allnames[namei]
|
|
|
|
while fakename in bynames:
|
Merged revisions 56125-56153 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
........
r56127 | georg.brandl | 2007-06-30 09:32:49 +0200 (Sat, 30 Jun 2007) | 2 lines
Fix a place where floor division would be in order.
........
r56135 | guido.van.rossum | 2007-07-01 06:13:54 +0200 (Sun, 01 Jul 2007) | 28 lines
Make map() and filter() identical to itertools.imap() and .ifilter(),
respectively.
I fixed two bootstrap issues, due to the dynamic import of itertools:
1. Starting python requires that map() and filter() are not used until
site.py has added build/lib.<arch> to sys.path.
2. Building python requires that setup.py and distutils and everything
they use is free of map() and filter() calls.
Beyond this, I only fixed the tests in test_builtin.py.
Others, please help fixing the remaining tests that are now broken!
The fixes are usually simple:
a. map(None, X) -> list(X)
b. map(F, X) -> list(map(F, X))
c. map(lambda x: F(x), X) -> [F(x) for x in X]
d. filter(F, X) -> list(filter(F, X))
e. filter(lambda x: P(x), X) -> [x for x in X if P(x)]
Someone, please also contribute a fixer for 2to3 to do this.
It can leave map()/filter() calls alone that are already
inside a list() or sorted() call or for-loop.
Only in rare cases have I seen code that depends on map() of lists
of different lengths going to the end of the longest, or on filter()
of a string or tuple returning an object of the same type; these
will need more thought to fix.
........
r56136 | guido.van.rossum | 2007-07-01 06:22:01 +0200 (Sun, 01 Jul 2007) | 3 lines
Make it so that test_decimal fails instead of hangs, to help automated
test runners.
........
r56139 | georg.brandl | 2007-07-01 18:20:58 +0200 (Sun, 01 Jul 2007) | 2 lines
Fix a few test cases after the map->imap change.
........
r56142 | neal.norwitz | 2007-07-02 06:38:12 +0200 (Mon, 02 Jul 2007) | 1 line
Get a bunch more tests passing after converting map/filter to return iterators.
........
r56147 | guido.van.rossum | 2007-07-02 15:32:02 +0200 (Mon, 02 Jul 2007) | 4 lines
Fix the remaining failing unit tests (at least on OSX).
Also tweaked urllib2 so it doesn't raise socket.gaierror when
all network interfaces are turned off.
........
2007-07-03 05:25:58 -03:00
|
|
|
chars = list(fakename)
|
2007-05-07 19:24:25 -03:00
|
|
|
for i in range(len(chars)):
|
2003-04-15 08:10:33 -03:00
|
|
|
if chars[i] == 'z':
|
|
|
|
chars[i] = 'A'
|
|
|
|
break
|
|
|
|
elif chars[i] == 'Z':
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
chars[i] = chr(ord(chars[i]) + 1)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
namei = namei + 1
|
|
|
|
try:
|
|
|
|
fakename = allnames[namei]
|
|
|
|
except IndexError:
|
|
|
|
# should never happen... if so, just forget it
|
|
|
|
break
|
Merged revisions 56125-56153 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
........
r56127 | georg.brandl | 2007-06-30 09:32:49 +0200 (Sat, 30 Jun 2007) | 2 lines
Fix a place where floor division would be in order.
........
r56135 | guido.van.rossum | 2007-07-01 06:13:54 +0200 (Sun, 01 Jul 2007) | 28 lines
Make map() and filter() identical to itertools.imap() and .ifilter(),
respectively.
I fixed two bootstrap issues, due to the dynamic import of itertools:
1. Starting python requires that map() and filter() are not used until
site.py has added build/lib.<arch> to sys.path.
2. Building python requires that setup.py and distutils and everything
they use is free of map() and filter() calls.
Beyond this, I only fixed the tests in test_builtin.py.
Others, please help fixing the remaining tests that are now broken!
The fixes are usually simple:
a. map(None, X) -> list(X)
b. map(F, X) -> list(map(F, X))
c. map(lambda x: F(x), X) -> [F(x) for x in X]
d. filter(F, X) -> list(filter(F, X))
e. filter(lambda x: P(x), X) -> [x for x in X if P(x)]
Someone, please also contribute a fixer for 2to3 to do this.
It can leave map()/filter() calls alone that are already
inside a list() or sorted() call or for-loop.
Only in rare cases have I seen code that depends on map() of lists
of different lengths going to the end of the longest, or on filter()
of a string or tuple returning an object of the same type; these
will need more thought to fix.
........
r56136 | guido.van.rossum | 2007-07-01 06:22:01 +0200 (Sun, 01 Jul 2007) | 3 lines
Make it so that test_decimal fails instead of hangs, to help automated
test runners.
........
r56139 | georg.brandl | 2007-07-01 18:20:58 +0200 (Sun, 01 Jul 2007) | 2 lines
Fix a few test cases after the map->imap change.
........
r56142 | neal.norwitz | 2007-07-02 06:38:12 +0200 (Mon, 02 Jul 2007) | 1 line
Get a bunch more tests passing after converting map/filter to return iterators.
........
r56147 | guido.van.rossum | 2007-07-02 15:32:02 +0200 (Mon, 02 Jul 2007) | 4 lines
Fix the remaining failing unit tests (at least on OSX).
Also tweaked urllib2 so it doesn't raise socket.gaierror when
all network interfaces are turned off.
........
2007-07-03 05:25:58 -03:00
|
|
|
fakename = ''.join(chars)
|
1996-12-11 12:53:51 -04:00
|
|
|
|
2003-04-15 08:10:33 -03:00
|
|
|
self.assertRaises(KeyError, pwd.getpwnam, fakename)
|
1996-12-11 12:53:51 -04:00
|
|
|
|
2003-04-15 08:10:33 -03:00
|
|
|
# Choose a non-existent uid.
|
|
|
|
fakeuid = 4127
|
|
|
|
while fakeuid in byuids:
|
|
|
|
fakeuid = (fakeuid * 3) % 0x10000
|
1996-12-11 12:53:51 -04:00
|
|
|
|
2003-04-15 08:10:33 -03:00
|
|
|
self.assertRaises(KeyError, pwd.getpwuid, fakeuid)
|
2000-10-23 14:22:08 -03:00
|
|
|
|
2003-04-15 08:10:33 -03:00
|
|
|
def test_main():
|
2003-05-01 14:45:56 -03:00
|
|
|
test_support.run_unittest(PwdTest)
|
1996-12-11 12:53:51 -04:00
|
|
|
|
2003-04-15 08:10:33 -03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|