Skip to content

Commit 0653b4b

Browse files
Samuel Cabrerocryptomilk
authored andcommitted
pytests/varlink: Add varlink tests
Signed-off-by: Samuel Cabrero <[email protected]> Reviewed-by: Andreas Schneider <[email protected]>
1 parent 5185d3e commit 0653b4b

File tree

7 files changed

+308
-1
lines changed

7 files changed

+308
-1
lines changed

python/samba/tests/varlink/base.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Unix SMB/CIFS implementation.
2+
#
3+
# Copyright (C) Samuel Cabrero <[email protected]> 2023
4+
#
5+
# This program is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation; either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
""" Winbind varlink service tests, base class """
19+
20+
from samba.tests.samba_tool.base import SambaToolCmdTest
21+
from samba.auth import system_session
22+
from samba.samdb import SamDB
23+
from samba.credentials import Credentials
24+
import os, pwd, grp
25+
import varlink
26+
import samba
27+
import subprocess
28+
29+
class VarlinkTestCase(SambaToolCmdTest):
30+
31+
def setUp(self):
32+
super().setUp()
33+
sdir = samba.tests.env_get_var_value("SELFTEST_WINBINDD_SOCKET_DIR")
34+
uri = "unix:" + os.path.join(sdir, "org.samba.selftest")
35+
self.cli = varlink.Client.new_with_address(uri)
36+
self.assertIsNotNone(self.cli)
37+
38+
self.lp = samba.tests.env_loadparm()
39+
self.domain = samba.tests.env_get_var_value("DOMAIN")
40+
self.winbind_separator = self.lp.get('winbind separator')
41+
self.varlink_service = self.lp.get('winbind varlink : service name')
42+
43+
self.bindir = os.path.normpath(os.getenv("BINDIR", "./bin"))
44+
self.netcmd = os.path.join(self.bindir, "net")
45+
46+
self.ldb = SamDB(
47+
session_info=system_session(),
48+
credentials=Credentials(),
49+
lp=self.lp)
50+
51+
self.users = []
52+
self.groups = []
53+
members = []
54+
for i in range(0, 3):
55+
username = "vl_test_user_%d" % i
56+
groupname = "vl_test_group_%d" % i
57+
58+
subprocess.Popen([self.netcmd, "cache", "del", "NAME2SID/%s\\%s"
59+
% (self.domain, username.upper())], stdout=subprocess.PIPE)
60+
self.runsubcmd("user", "create", username, self.random_password())
61+
62+
subprocess.Popen([self.netcmd, "cache", "del", "NAME2SID/%s\\%s"
63+
% (self.domain, groupname.upper())], stdout=subprocess.PIPE)
64+
self.runsubcmd("group", "create", groupname)
65+
66+
members.append(username)
67+
for m in members:
68+
self.runsubcmd("group", "addmembers", groupname, m)
69+
70+
grent = grp.getgrnam(groupname)
71+
self.groups.append({"groupname": groupname,
72+
"gid": grent.gr_gid,
73+
"members": members.copy()})
74+
75+
pwent = pwd.getpwnam(username)
76+
self.users.append({"username": username,
77+
"uid": pwent.pw_uid,
78+
"gid": pwent.pw_gid,
79+
"shell": pwent.pw_shell,
80+
"dir": pwent.pw_dir})
81+
82+
def tearDown(self):
83+
for group in self.groups:
84+
self.runsubcmd("group", "delete", group["groupname"])
85+
86+
for user in self.users:
87+
self.runsubcmd("user", "delete", user["username"])
88+
89+
super().tearDown()
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Unix SMB/CIFS implementation.
2+
#
3+
# Copyright (C) Samuel Cabrero <[email protected]> 2024
4+
#
5+
# This program is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation; either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
""" Winbind varlink service tests """
19+
20+
import sys
21+
import os
22+
23+
sys.path.insert(0, "bin/python")
24+
os.environ["PYTHONUNBUFFERED"] = "1"
25+
26+
from samba.tests.varlink.base import VarlinkTestCase
27+
28+
29+
class VarlinkGetUserRecordTests(VarlinkTestCase):
30+
def setUp(self):
31+
super().setUp()
32+
33+
def tearDown(self):
34+
super().tearDown()
35+
36+
def testGetGroupRecord(self):
37+
for group in self.groups:
38+
with self.cli.open("io.systemd.UserDatabase", namespaced=True) as conn:
39+
full_groupname = "%s%s%s" % (self.domain,
40+
self.winbind_separator,
41+
group["groupname"])
42+
full_members_names = []
43+
for m in group["members"]:
44+
full_members_names.append("%s%s%s" % (self.domain,
45+
self.winbind_separator,
46+
m))
47+
r = conn.GetGroupRecord(service=self.varlink_service,
48+
groupName=full_groupname)
49+
self.assertIsNotNone(r)
50+
self.assertFalse(r.incomplete)
51+
self.assertIsNotNone(r.record)
52+
self.assertEqual(r.record["service"], self.varlink_service)
53+
self.assertEqual(r.record["groupName"], full_groupname)
54+
self.assertEqual(r.record["gid"], group["gid"])
55+
self.assertEqual(sorted(r.record["members"]),
56+
sorted(full_members_names))
57+
58+
59+
if __name__ == "__main__":
60+
import unittest
61+
unittest.main()
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Unix SMB/CIFS implementation.
2+
#
3+
# Copyright (C) Samuel Cabrero <[email protected]> 2024
4+
#
5+
# This program is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation; either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
""" Winbind varlink service tests """
19+
20+
import sys
21+
import os
22+
import pwd
23+
import grp
24+
25+
sys.path.insert(0, "bin/python")
26+
os.environ["PYTHONUNBUFFERED"] = "1"
27+
28+
from samba.tests.varlink.base import VarlinkTestCase
29+
30+
31+
class VarlinkGetMembershipsTests(VarlinkTestCase):
32+
def setUp(self):
33+
super().setUp()
34+
35+
def tearDown(self):
36+
super().tearDown()
37+
38+
def testGetMembershipsByGroup(self):
39+
for group in self.groups:
40+
full_name = "%s%s%s" % (self.domain,
41+
self.winbind_separator,
42+
group["groupname"])
43+
full_members_names = []
44+
for m in group["members"]:
45+
full_members_names.append("%s%s%s" % (self.domain,
46+
self.winbind_separator,
47+
m))
48+
vl_members = []
49+
with self.cli.open("io.systemd.UserDatabase", namespaced=True) as conn:
50+
for r in conn.GetMemberships(service=self.varlink_service,
51+
groupName=full_name,
52+
_more=True):
53+
self.assertIsNotNone(r)
54+
vl_members.append(r.userName)
55+
self.assertEqual(sorted(vl_members),
56+
sorted(full_members_names))
57+
58+
def testGetMembershipsByUser(self):
59+
for user in self.users:
60+
full_username = "%s%s%s" % (self.domain,
61+
self.winbind_separator,
62+
user["username"])
63+
pwent = pwd.getpwnam(full_username)
64+
glgid = os.getgrouplist(pwent.pw_name, pwent.pw_gid)
65+
nss_list = []
66+
for gid in glgid:
67+
grent = grp.getgrgid(gid)
68+
# nss_wrapper looks into files first, and "ADDOMAIN/domain users" is
69+
# mapped to "users" from files NSS group db.
70+
gname = grent.gr_name
71+
if gname == "users":
72+
gname = "%s%s%s" % (self.domain,
73+
self.winbind_separator,
74+
"domain users")
75+
nss_list.append(gname)
76+
77+
vl_list = []
78+
with self.cli.open("io.systemd.UserDatabase", namespaced=True) as conn:
79+
for r in conn.GetMemberships(service=self.varlink_service,
80+
userName=full_username,
81+
_more=True):
82+
self.assertIsNotNone(r)
83+
vl_list.append(r.groupName)
84+
85+
self.assertEqual(sorted(nss_list), sorted(vl_list))
86+
87+
if __name__ == "__main__":
88+
import unittest
89+
unittest.main()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Unix SMB/CIFS implementation.
2+
#
3+
# Copyright (C) Samuel Cabrero <[email protected]> 2023
4+
#
5+
# This program is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation; either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
""" Winbind varlink service tests """
19+
20+
import sys
21+
import os
22+
23+
sys.path.insert(0, "bin/python")
24+
os.environ["PYTHONUNBUFFERED"] = "1"
25+
26+
from samba.tests.varlink.base import VarlinkTestCase
27+
28+
29+
class VarlinkGetUserRecordTests(VarlinkTestCase):
30+
def setUp(self):
31+
super().setUp()
32+
33+
def tearDown(self):
34+
super().tearDown()
35+
36+
def testGetUserRecord(self):
37+
for user in self.users:
38+
with self.cli.open("io.systemd.UserDatabase", namespaced=True) as conn:
39+
full_username = "%s%s%s" % (self.domain,
40+
self.winbind_separator,
41+
user["username"])
42+
r = conn.GetUserRecord(service=self.varlink_service,
43+
userName=full_username)
44+
self.assertIsNotNone(r)
45+
self.assertFalse(r.incomplete)
46+
self.assertIsNotNone(r.record)
47+
self.assertEqual(r.record["service"], self.varlink_service)
48+
self.assertEqual(r.record["userName"], full_username)
49+
self.assertEqual(r.record["uid"], user["uid"])
50+
self.assertEqual(r.record["gid"], user["gid"])
51+
self.assertEqual(r.record["shell"], user["shell"])
52+
self.assertEqual(r.record["homeDirectory"], user["dir"])
53+
54+
if __name__ == "__main__":
55+
import unittest
56+
unittest.main()

script/autobuild.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def make_test(
282282
"samba-mit-build": {
283283
"git-clone-required": True,
284284
"sequence": [
285-
("configure", "./configure.developer --with-system-mitkrb5 --with-experimental-mit-ad-dc" + samba_configure_params),
285+
("configure", "./configure.developer --with-system-mitkrb5 --with-experimental-mit-ad-dc --with-systemd-userdb" + samba_configure_params),
286286
("make", "make -j"),
287287
("check-clean-tree", CLEAN_SOURCE_TREE_CMD),
288288
("chmod-R-a-w", "chmod -R a-w ."),

selftest/target/Samba4.pm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,10 @@ sub provision_raw_step1($$)
821821
idmap_ldb:use rfc2307=yes
822822
winbind enum users = yes
823823
winbind enum groups = yes
824+
winbind expand groups = 1
825+
826+
winbind varlink : socket directory = $ctx->{winbindd_socket_dir}
827+
winbind varlink : service name = org.samba.selftest
824828
825829
rpc server port:netlogon = 1026
826830
include system krb5 conf = no
@@ -2165,6 +2169,9 @@ sub provision_ad_dc()
21652169
dsdb event notification = true
21662170
dsdb password event notification = true
21672171
dsdb group change notification = true
2172+
2173+
winbind varlink service = yes
2174+
21682175
$smbconf_args
21692176
";
21702177

source4/selftest/tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,3 +2250,8 @@ def planoldpythontestsuite(env, module, name=None, extra_path=None, environ=None
22502250
planpythontestsuite("clusteredmember:local",
22512251
"samba.tests.blackbox.rpcd_witness_samba_only",
22522252
environ=cluster_environ)
2253+
2254+
if 'WITH_SYSTEMD_USERDB' in config_hash:
2255+
planpythontestsuite("ad_dc:local", "samba.tests.varlink.getuserrecord")
2256+
planpythontestsuite("ad_dc:local", "samba.tests.varlink.getgrouprecord")
2257+
planpythontestsuite("ad_dc:local", "samba.tests.varlink.getmemberships")

0 commit comments

Comments
 (0)