From cebeb5a22e3c2318a32c0c3777143a963b7bc9ad Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Mon, 16 Mar 2020 11:48:27 -0700 Subject: [PATCH 1/2] 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 --- Lib/test/test_grp.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_grp.py b/Lib/test/test_grp.py index e511947858c0a32..82ef8bcf539c56a 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() From f45c86f2b0bda389dcd1ef7154d4c4dbb6e2c1dc Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 16 Mar 2020 21:48:32 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Misc/NEWS.d/next/Tests/2020-03-16-21-48-31.bpo-36017.DH1nwW.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Tests/2020-03-16-21-48-31.bpo-36017.DH1nwW.rst 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 000000000000000..fd6d8f5df1bd39b --- /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