Back to index

python3.2  3.2.2
Public Member Functions
test.test_pwd.PwdTest Class Reference

List of all members.

Public Member Functions

def test_values
def test_errors

Detailed Description

Definition at line 7 of file test_pwd.py.


Member Function Documentation

Definition at line 50 of file test_pwd.py.

00050 
00051     def test_errors(self):
00052         self.assertRaises(TypeError, pwd.getpwuid)
00053         self.assertRaises(TypeError, pwd.getpwnam)
00054         self.assertRaises(TypeError, pwd.getpwall, 42)
00055 
00056         # try to get some errors
00057         bynames = {}
00058         byuids = {}
00059         for (n, p, u, g, gecos, d, s) in pwd.getpwall():
00060             bynames[n] = u
00061             byuids[u] = n
00062 
00063         allnames = list(bynames.keys())
00064         namei = 0
00065         fakename = allnames[namei]
00066         while fakename in bynames:
00067             chars = list(fakename)
00068             for i in range(len(chars)):
00069                 if chars[i] == 'z':
00070                     chars[i] = 'A'
00071                     break
00072                 elif chars[i] == 'Z':
00073                     continue
00074                 else:
00075                     chars[i] = chr(ord(chars[i]) + 1)
00076                     break
00077             else:
00078                 namei = namei + 1
00079                 try:
00080                     fakename = allnames[namei]
00081                 except IndexError:
00082                     # should never happen... if so, just forget it
00083                     break
00084             fakename = ''.join(chars)
00085 
00086         self.assertRaises(KeyError, pwd.getpwnam, fakename)
00087 
00088         # In some cases, byuids isn't a complete list of all users in the
00089         # system, so if we try to pick a value not in byuids (via a perturbing
00090         # loop, say), pwd.getpwuid() might still be able to find data for that
00091         # uid. Using sys.maxint may provoke the same problems, but hopefully
00092         # it will be a more repeatable failure.
00093         fakeuid = sys.maxsize
00094         self.assertNotIn(fakeuid, byuids)
00095         self.assertRaises(KeyError, pwd.getpwuid, fakeuid)

Here is the call graph for this function:

Definition at line 9 of file test_pwd.py.

00009 
00010     def test_values(self):
00011         entries = pwd.getpwall()
00012         entriesbyname = {}
00013         entriesbyuid = {}
00014 
00015         for e in entries:
00016             self.assertEqual(len(e), 7)
00017             self.assertEqual(e[0], e.pw_name)
00018             self.assertIsInstance(e.pw_name, str)
00019             self.assertEqual(e[1], e.pw_passwd)
00020             self.assertIsInstance(e.pw_passwd, str)
00021             self.assertEqual(e[2], e.pw_uid)
00022             self.assertIsInstance(e.pw_uid, int)
00023             self.assertEqual(e[3], e.pw_gid)
00024             self.assertIsInstance(e.pw_gid, int)
00025             self.assertEqual(e[4], e.pw_gecos)
00026             self.assertIsInstance(e.pw_gecos, str)
00027             self.assertEqual(e[5], e.pw_dir)
00028             self.assertIsInstance(e.pw_dir, str)
00029             self.assertEqual(e[6], e.pw_shell)
00030             self.assertIsInstance(e.pw_shell, str)
00031 
00032             # The following won't work, because of duplicate entries
00033             # for one uid
00034             #    self.assertEqual(pwd.getpwuid(e.pw_uid), e)
00035             # instead of this collect all entries for one uid
00036             # and check afterwards
00037             entriesbyname.setdefault(e.pw_name, []).append(e)
00038             entriesbyuid.setdefault(e.pw_uid, []).append(e)
00039 
00040         if len(entries) > 1000:  # Huge passwd file (NIS?) -- skip the rest
00041             return
00042 
00043         # check whether the entry returned by getpwuid()
00044         # for each uid is among those from getpwall() for this uid
00045         for e in entries:
00046             if not e[0] or e[0] == '+':
00047                 continue # skip NIS entries etc.
00048             self.assertIn(pwd.getpwnam(e.pw_name), entriesbyname[e.pw_name])
00049             self.assertIn(pwd.getpwuid(e.pw_uid), entriesbyuid[e.pw_uid])

Here is the call graph for this function:


The documentation for this class was generated from the following file: