CVE-2023-0614 s4:dsdb/extended_dn_in: Don't modify a search tree we don't own
[Samba.git] / selftest / selftesthelpers.py
blob0320008faf94b26d543b0ebd6f86b68f74f03b2c
1 #!/usr/bin/env python3
3 # This script generates a list of testsuites that should be run as part of
4 # the Samba 4 test suite.
6 # The output of this script is parsed by selftest.pl, which then decides
7 # which of the tests to actually run. It will, for example, skip all tests
8 # listed in selftest/skip or only run a subset during "make quicktest".
10 # The idea is that this script outputs all of the tests of Samba 4, not
11 # just those that are known to pass, and list those that should be skipped
12 # or are known to fail in selftest/skip or selftest/knownfail. This makes it
13 # very easy to see what functionality is still missing in Samba 4 and makes
14 # it possible to run the testsuite against other servers, such as Samba 3 or
15 # Windows that have a different set of features.
17 # The syntax for a testsuite is "-- TEST --" on a single line, followed
18 # by the name of the test, the environment it needs and the command to run, all
19 # three separated by newlines. All other lines in the output are considered
20 # comments.
22 import os
23 import subprocess
24 import sys
27 def srcdir():
28 alternate_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
29 return os.path.normpath(os.getenv("SRCDIR", alternate_path))
32 def source4dir():
33 return os.path.normpath(os.path.join(srcdir(), "source4"))
36 def source3dir():
37 return os.path.normpath(os.path.join(srcdir(), "source3"))
40 def bindir():
41 return os.path.normpath(os.getenv("BINDIR", "./bin"))
44 def binpath(name):
45 return os.path.join(bindir(), name)
48 # Split perl variable to allow $PERL to be set to e.g. "perl -W"
49 perl = os.getenv("PERL", "perl").split()
51 if subprocess.call(perl + ["-e", "eval require Test::More;"]) == 0:
52 has_perl_test_more = True
53 else:
54 has_perl_test_more = False
56 python = os.getenv("PYTHON", "python")
58 tap2subunit = python + " " + os.path.join(srcdir(), "selftest", "tap2subunit")
61 def valgrindify(cmdline):
62 """Run a command under valgrind, if $VALGRIND was set."""
63 valgrind = os.getenv("VALGRIND")
64 if valgrind is None:
65 return cmdline
66 return valgrind + " " + cmdline
69 def plantestsuite(name, env, cmd, environ={}):
70 """Plan a test suite.
72 :param name: Testsuite name
73 :param env: Environment to run the testsuite in
74 :param cmdline: Command line to run
75 """
76 print("-- TEST --")
77 if env == "none":
78 fullname = name
79 else:
80 fullname = "%s(%s)" % (name, env)
81 print(fullname)
82 print(env)
84 cmdline = ""
85 if environ:
86 environ = dict(environ)
87 cmdline_env = ["%s=%s" % item for item in environ.items()]
88 cmdline = " ".join(cmdline_env) + " "
90 if isinstance(cmd, list):
91 cmdline += " ".join(cmd)
92 else:
93 cmdline += cmd
95 if "$LISTOPT" in cmdline:
96 raise AssertionError("test %s supports --list, but not --load-list" % name)
97 print(cmdline + " 2>&1 " + " | " + add_prefix(name, env))
100 def add_prefix(prefix, env, support_list=False):
101 if support_list:
102 listopt = "$LISTOPT "
103 else:
104 listopt = ""
105 return ("%s %s/selftest/filter-subunit %s--fail-on-empty --prefix=\"%s.\" --suffix=\"(%s)\"" %
106 (python, srcdir(), listopt, prefix, env))
109 def plantestsuite_loadlist(name, env, cmdline):
110 print("-- TEST-LOADLIST --")
111 if env == "none":
112 fullname = name
113 else:
114 fullname = "%s(%s)" % (name, env)
115 print(fullname)
116 print(env)
117 if isinstance(cmdline, list):
118 cmdline = " ".join(cmdline)
119 support_list = ("$LISTOPT" in cmdline)
120 if "$LISTOPT" not in cmdline:
121 raise AssertionError("loadlist test %s does not support not --list" % name)
122 if "$LOADLIST" not in cmdline:
123 raise AssertionError("loadlist test %s does not support --load-list" % name)
124 print(("%s | %s" %
125 (cmdline.replace("$LOADLIST", ""),
126 add_prefix(name, env, support_list))).replace("$LISTOPT", "--list "))
127 print(cmdline.replace("$LISTOPT", "") + " 2>&1 " + " | " + add_prefix(name, env, False))
130 def skiptestsuite(name, reason):
131 """Indicate that a testsuite was skipped.
133 :param name: Test suite name
134 :param reason: Reason the test suite was skipped
136 # FIXME: Report this using subunit, but re-adjust the testsuite count somehow
137 print("skipping %s (%s)" % (name, reason), file=sys.stderr)
140 def planperltestsuite(name, path):
141 """Run a perl test suite.
143 :param name: Name of the test suite
144 :param path: Path to the test runner
146 if has_perl_test_more:
147 plantestsuite(name, "none", "%s %s | %s" % (" ".join(perl), path, tap2subunit))
148 else:
149 skiptestsuite(name, "Test::More not available")
152 def planpythontestsuite(env, module, name=None, extra_path=[], environ={}, extra_args=[]):
153 environ = dict(environ)
154 py_path = list(extra_path)
155 if py_path is not None:
156 environ["PYTHONPATH"] = ":".join(["$PYTHONPATH"] + py_path)
157 args = ["%s=%s" % item for item in environ.items()]
158 args += [python, "-m", "samba.subunit.run", "$LISTOPT", "$LOADLIST", module]
159 args += extra_args
160 if name is None:
161 name = module
163 plantestsuite_loadlist(name, env, args)
166 def get_env_torture_options():
167 ret = []
168 if not os.getenv("SELFTEST_VERBOSE"):
169 ret.append("--option=torture:progress=no")
170 if os.getenv("SELFTEST_QUICK"):
171 ret.append("--option=torture:quick=yes")
172 return ret
175 samba4srcdir = source4dir()
176 samba3srcdir = source3dir()
177 bbdir = os.path.join(srcdir(), "testprogs/blackbox")
178 configuration = "--configfile=$SMB_CONF_PATH"
180 smbtorture4 = binpath("smbtorture")
181 smbtorture4_testsuite_list = subprocess.Popen(
182 [smbtorture4, "--list-suites"],
183 stdout=subprocess.PIPE,
184 stderr=subprocess.PIPE).communicate("")[0].decode('utf8').splitlines()
186 smbtorture4_options = [
187 configuration,
188 "--option=\'fss:sequence timeout=1\'",
189 "--maximum-runtime=$SELFTEST_MAXTIME",
190 "--basedir=$SELFTEST_TMPDIR",
191 "--format=subunit"
192 ] + get_env_torture_options()
195 def plansmbtorture4testsuite(name, env, options, target, modname=None, environ={}):
196 if modname is None:
197 modname = "samba4.%s" % name
198 if isinstance(options, list):
199 options = " ".join(options)
200 options = " ".join(smbtorture4_options + ["--target=%s" % target]) + " " + options
201 cmdline = ""
202 if environ:
203 environ = dict(environ)
204 cmdline_env = ["%s=%s" % item for item in environ.items()]
205 cmdline += " ".join(cmdline_env) + " "
206 cmdline += " %s $LISTOPT $LOADLIST %s %s" % (valgrindify(smbtorture4), options, name)
207 plantestsuite_loadlist(modname, env, cmdline)
210 def smbtorture4_testsuites(prefix):
211 return list(filter(lambda x: x.startswith(prefix), smbtorture4_testsuite_list))
214 smbclient3 = binpath('smbclient')
215 smbtorture3 = binpath('smbtorture3')
216 ntlm_auth3 = binpath('ntlm_auth')
217 net = binpath('net')
218 scriptdir = os.path.join(srcdir(), "script/tests")
220 wbinfo = binpath('wbinfo')
221 dbwrap_tool = binpath('dbwrap_tool')
222 vfstest = binpath('vfstest')
223 smbcquotas = binpath('smbcquotas')
224 smbget = binpath('smbget')
225 rpcclient = binpath('rpcclient')
226 smbcacls = binpath('smbcacls')
227 smbcontrol = binpath('smbcontrol')
228 smbstatus = binpath('smbstatus')