3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """Samba Python tests."""
26 from samba import param
30 # Other modules import these two classes from here, for convenience:
31 from testtools.testcase import TestCase, TestSkipped
34 class LdbTestCase(TestCase):
35 """Trivial test case for running tests against a LDB."""
38 super(LdbTestCase, self).setUp()
39 self.filename = os.tempnam()
40 self.ldb = samba.Ldb(self.filename)
42 def set_modules(self, modules=[]):
43 """Change the modules for this Ldb."""
45 m.dn = ldb.Dn(self.ldb, "@MODULES")
46 m["@LIST"] = ",".join(modules)
48 self.ldb = samba.Ldb(self.filename)
51 class TestCaseInTempDir(TestCase):
54 super(TestCaseInTempDir, self).setUp()
55 self.tempdir = tempfile.mkdtemp()
58 super(TestCaseInTempDir, self).tearDown()
59 self.assertEquals([], os.listdir(self.tempdir))
60 os.rmdir(self.tempdir)
66 lp.load(os.environ["SMB_CONF_PATH"])
68 raise Exception("SMB_CONF_PATH not set")
71 def env_get_var_value(var_name):
72 """Returns value for variable in os.environ
74 Function throws AssertionError if variable is defined.
75 Unit-test based python tests require certain input params
76 to be set in environment, otherwise they can't be run
78 assert var_name in os.environ.keys(), "Please supply %s in environment" % var_name
79 return os.environ[var_name]
82 cmdline_credentials = None
84 class RpcInterfaceTestCase(TestCase):
86 def get_loadparm(self):
89 def get_credentials(self):
90 return cmdline_credentials
93 class ValidNetbiosNameTests(TestCase):
96 self.assertTrue(samba.valid_netbios_name("FOO"))
98 def test_too_long(self):
99 self.assertFalse(samba.valid_netbios_name("FOO"*10))
101 def test_invalid_characters(self):
102 self.assertFalse(samba.valid_netbios_name("*BLA"))
105 class BlackboxTestCase(TestCase):
106 """Base test case for blackbox tests."""
108 def check_run(self, line):
109 bindir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../bin"))
110 parts = line.split(" ")
111 if os.path.exists(os.path.join(bindir, parts[0])):
112 parts[0] = os.path.join(bindir, parts[0])
113 line = " ".join(parts)
114 subprocess.check_call(line, shell=True)
117 def connect_samdb(samdb_url, lp=None, session_info=None,
118 credentials=None, flags=0, ldb_options=None, ldap_only=False):
119 """Creates SamDB instance and connects to samdb_url database.
121 :param samdb_url: Url for database to connect to.
122 :param lp: Optional loadparm object
123 :param session_info: Optional session information
124 :param credentials: Optional credentials, defaults to anonymous.
125 :param flags: Optional LDB flags
126 :param ldap_only: If set, only remote LDAP connection will be created.
128 Added value for tests is that we have a shorthand function
129 to make proper URL for ldb.connect() while using default
130 parameters for connection based on test environment
132 samdb_url = samdb_url.lower()
133 if not "://" in samdb_url:
134 if not ldap_only and os.path.isfile(samdb_url):
135 samdb_url = "tdb://%s" % samdb_url
137 samdb_url = "ldap://%s" % samdb_url
138 # use 'paged_search' module when connecting remotely
139 if samdb_url.startswith("ldap://"):
140 ldb_options = ["modules:paged_searches"]
142 assert not ldap_only, \
143 "Trying to connect to %s while remote connection is required" % samdb_url
145 # set defaults for test environment
149 session_info=samba.auth.system_session(lp)
151 credentials=cmdline_credentials
153 from samba.samdb import SamDB
154 return SamDB(url=samdb_url,
156 session_info=session_info,
157 credentials=credentials,
161 def connect_samdb_ex(samdb_url, lp=None, session_info=None,
162 credentials=None, flags=0, ldb_options=None, ldap_only=False):
163 """Connects to samdb_url database
165 :param samdb_url: Url for database to connect to.
166 :param lp: Optional loadparm object
167 :param session_info: Optional session information
168 :param credentials: Optional credentials, defaults to anonymous.
169 :param flags: Optional LDB flags
170 :param ldap_only: If set, only remote LDAP connection will be created.
171 :return: (sam_db_connection, rootDse_record) tuple
173 sam_db = connect_samdb(samdb_url, lp, session_info, credentials,
174 flags, ldb_options, ldap_only)
176 res = sam_db.search(base="", expression="", scope=ldb.SCOPE_BASE, attrs=["*"])
177 return (sam_db, res[0])