diff --git a/Lib/test/test_grp.py b/Lib/test/test_grp.py index e511947858c0a3..82ef8bcf539c56 100644 --- a/Lib/test/test_grp.py +++ b/Lib/test/test_grp.py @@ -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)): @@ -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() diff --git a/Misc/NEWS.d/next/Tests/2020-03-16-21-48-31.bpo-36017.DH1nwW.rst b/Misc/NEWS.d/next/Tests/2020-03-16-21-48-31.bpo-36017.DH1nwW.rst new file mode 100644 index 00000000000000..fd6d8f5df1bd39 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2020-03-16-21-48-31.bpo-36017.DH1nwW.rst @@ -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. \ No newline at end of file