Skip to content

Commit cebeb5a

Browse files
committed
bpo-36017: Improve test_grp.test_errors for (big) LDAP directories
There is no guarantee that the group database returned on unix system is complete. It is typically cut short when LDAP directories are configured for bigger network setups. This makes it tricky to find a nonexistent group id by looking at the group database. This changes the test to pick a nonexistent gid within gid 1-99 which are typically static assigned and not managed by network directory systems. Also pick a group name that is extremely likely to not exist instead of modifying the name of the first group in the database. skip news
1 parent 6ff79f6 commit cebeb5a

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

Lib/test/test_grp.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ def test_errors(self):
6464

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

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

90-
# Choose a non-existent gid.
91-
fakegid = 4127
92-
while fakegid in bygids:
93-
fakegid = (fakegid * 3) % 0x10000
94-
95-
self.assertRaises(KeyError, grp.getgrgid, fakegid)
91+
# Picking a nonexistent GID is hard, since getgrall() will not
92+
# necessarily report all existing groups (typical for LDAP based
93+
# directories in big organisations).
94+
# Try to find one in the range 1-99 as the lower IDs are typically
95+
# statically assigned and more likely to be reported accurately.
96+
nonexistent_gid = None
97+
candidates = list(range(1, 99))
98+
candidates = candidates[90:] + candidates[:90]
99+
for i in candidates:
100+
if i not in bygids:
101+
nonexistent_gid = i
102+
break
103+
104+
if nonexistent_gid is not None:
105+
self.assertRaises(KeyError, grp.getgrgid, nonexistent_gid)
96106

97107
def test_noninteger_gid(self):
98108
entries = grp.getgrall()

0 commit comments

Comments
 (0)