]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
python:tests/nss: Add NSS group enumeration test
authorSamuel Cabrero <scabrero@samba.org>
Mon, 26 May 2025 09:20:13 +0000 (11:20 +0200)
committerAndreas Schneider <asn@cryptomilk.org>
Tue, 24 Jun 2025 07:53:41 +0000 (07:53 +0000)
Signed-off-by: Samuel Cabrero <scabrero@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
python/samba/tests/nss/base.py [new file with mode: 0644]
python/samba/tests/nss/group.py [new file with mode: 0644]
source4/selftest/tests.py

diff --git a/python/samba/tests/nss/base.py b/python/samba/tests/nss/base.py
new file mode 100644 (file)
index 0000000..3f2d1a4
--- /dev/null
@@ -0,0 +1,87 @@
+# Unix SMB/CIFS implementation.
+#
+# Copyright (C) Samuel Cabrero <scabrero@samba.org> 2025
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+"""Winbind nss tests, base class"""
+
+import grp
+import os
+import pwd
+import subprocess
+
+import samba
+from samba.auth import system_session
+from samba.credentials import Credentials
+from samba.samdb import SamDB
+from samba.tests.samba_tool.base import SambaToolCmdTest
+
+
+class NssTestCase(SambaToolCmdTest):
+    def setUp(self):
+        super().setUp()
+
+        self.domain = samba.tests.env_get_var_value("DOMAIN")
+
+        self.bindir = os.path.normpath(os.getenv("BINDIR", "./bin"))
+        self.netcmd = os.path.join(self.bindir, "net")
+
+        self.users = []
+        self.groups = []
+        members = []
+        for i in range(0, 3):
+            username = "nss_test_user_%d" % i
+            groupname = "nss_test_group_%d" % i
+
+            subprocess.Popen(
+                [
+                    self.netcmd,
+                    "cache",
+                    "del",
+                    "NAME2SID/%s\\%s" % (self.domain, username.upper()),
+                ],
+                stdout=subprocess.PIPE,
+            )
+            self.runsubcmd("user", "create", username, self.random_password())
+
+            subprocess.Popen(
+                [
+                    self.netcmd,
+                    "cache",
+                    "del",
+                    "NAME2SID/%s\\%s" % (self.domain, groupname.upper()),
+                ],
+                stdout=subprocess.PIPE,
+            )
+            self.runsubcmd("group", "create", groupname)
+
+            members.append(username)
+            for m in members:
+                self.runsubcmd("group", "addmembers", groupname, m)
+
+            grent = grp.getgrnam(groupname)
+            self.groups.append(grent)
+
+            pwent = pwd.getpwnam(username)
+            self.users.append(pwent)
+
+    def tearDown(self):
+        for test_group in self.groups:
+            self.runsubcmd("group", "delete", test_group.gr_name)
+
+        for test_user in self.users:
+            self.runsubcmd("user", "delete", test_user.pw_name)
+
+        super().tearDown()
diff --git a/python/samba/tests/nss/group.py b/python/samba/tests/nss/group.py
new file mode 100644 (file)
index 0000000..412d89c
--- /dev/null
@@ -0,0 +1,44 @@
+# Unix SMB/CIFS implementation.
+#
+# Copyright (C) Samuel Cabrero <scabrero@samba.org> 2025
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+"""Winbind nss tests"""
+
+import sys
+import os
+import grp
+
+sys.path.insert(0, "bin/python")
+os.environ["PYTHONUNBUFFERED"] = "1"
+
+from samba.tests.nss.base import NssTestCase
+
+
+class NssGroupTests(NssTestCase):
+    def testGroupEnum(self):
+        grlst = grp.getgrall()
+        self.assertIsNotNone(grlst)
+        self.assertGreaterEqual(
+            len(grlst), len(self.groups), "Unexpected groups length"
+        )
+        for test_group in self.groups:
+            self.assertIn(test_group, grlst)
+
+
+if __name__ == "__main__":
+    import unittest
+
+    unittest.main()
index 6ddea31e696d9883024b91d911a007d357325acf..1929d44421e9a798533ae7a00a35013134b18bbb 100755 (executable)
@@ -2252,6 +2252,8 @@ if have_cluster_support:
                         "samba.tests.blackbox.rpcd_witness_samba_only",
                         environ=cluster_environ)
 
+planpythontestsuite("ad_dc:local", "samba.tests.nss.group")
+
 if 'WITH_SYSTEMD_USERDB' in config_hash:
     planpythontestsuite("ad_dc:local", "samba.tests.varlink.getuserrecord")
     planpythontestsuite("ad_dc:local", "samba.tests.varlink.getgrouprecord")