Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions Lib/test/test_grp.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def test_errors(self):

allnames = list(bynames.keys())
namei = 0
fakename = allnames[namei]
# Start with a name that very likely does not exist. Typos deliberate.
fakename = "should_noot_exxist"
while fakename in bynames:
chars = list(fakename)
for i in range(len(chars)):
Expand All @@ -87,12 +88,21 @@ def test_errors(self):

self.assertRaises(KeyError, grp.getgrnam, fakename)

# Choose a non-existent gid.
fakegid = 4127
while fakegid in bygids:
fakegid = (fakegid * 3) % 0x10000

self.assertRaises(KeyError, grp.getgrgid, fakegid)
# Picking a nonexistent GID is hard, since getgrall() will not
# necessarily report all existing groups (typical for LDAP based
# directories in big organisations).
# Try to find one in the range 1-99 as the lower IDs are typically
# statically assigned and more likely to be reported accurately.
nonexistent_gid = None
candidates = list(range(1, 99))
candidates = candidates[90:] + candidates[:90]
for i in candidates:
if i not in bygids:
nonexistent_gid = i
break

if nonexistent_gid is not None:
self.assertRaises(KeyError, grp.getgrgid, nonexistent_gid)

def test_noninteger_gid(self):
entries = grp.getgrall()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use a heuristic to find nonexistent gid in test_grp test_errors test that works better for network directory services such as LDAP.
Loading