From: Tim Beale Date: Thu, 10 May 2018 23:03:03 +0000 (+1200) Subject: tests: Split out setUp code into separate function for reuse X-Git-Tag: ldb-1.4.0~364 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d0a9e19114cfdaa9cd083e46d51f9e5de9e907e5;p=thirdparty%2Fsamba.git tests: Split out setUp code into separate function for reuse Any test that wants to change a password has to set the dSHeuristics and minPwdAge first in order for the password change to work. The code that does this is duplicated in several tests. This patch splits it out into a static method so that the code can be reused rather than duplicated. Reviewed-by: Andrew Bartlett Reviewed-by: Garming Sam Signed-off-by: Tim Beale --- diff --git a/python/samba/tests/auth_log_pass_change.py b/python/samba/tests/auth_log_pass_change.py index 8890694d37e..1bbb0ea6b0c 100644 --- a/python/samba/tests/auth_log_pass_change.py +++ b/python/samba/tests/auth_log_pass_change.py @@ -29,6 +29,7 @@ from samba.net import Net import samba from subprocess import call from ldb import LdbError +from samba.tests.password_test import PasswordCommon USER_NAME = "authlogtestuser" USER_PASS = samba.generate_random_password(32, 32) @@ -53,26 +54,11 @@ class AuthLogPassChangeTests(samba.tests.auth_log_base.AuthLogTestBase): base_dn = self.ldb.domain_dn() print("base_dn %s" % base_dn) - # Get the old "dSHeuristics" if it was set - dsheuristics = self.ldb.get_dsheuristics() + # permit password changes during this test + PasswordCommon.allow_password_changes(self, self.ldb) - # Set the "dSHeuristics" to activate the correct "userPassword" - # behaviour - self.ldb.set_dsheuristics("000000001") - - # Reset the "dSHeuristics" as they were before - self.addCleanup(self.ldb.set_dsheuristics, dsheuristics) - - # Get the old "minPwdAge" - minPwdAge = self.ldb.get_minPwdAge() - - # Set it temporarily to "0" - self.ldb.set_minPwdAge("0") self.base_dn = self.ldb.domain_dn() - # Reset the "minPwdAge" as it was before - self.addCleanup(self.ldb.set_minPwdAge, minPwdAge) - # (Re)adds the test user USER_NAME with password USER_PASS delete_force(self.ldb, "cn=" + USER_NAME + ",cn=users," + self.base_dn) self.ldb.add({ diff --git a/python/samba/tests/password_hash.py b/python/samba/tests/password_hash.py index a3a74aa25df..0a295d57c0d 100644 --- a/python/samba/tests/password_hash.py +++ b/python/samba/tests/password_hash.py @@ -29,6 +29,7 @@ from samba.dcerpc import drsblobs from samba.dcerpc.samr import DOMAIN_PASSWORD_STORE_CLEARTEXT from samba.dsdb import UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED from samba.tests import delete_force +from samba.tests.password_test import PasswordCommon import ldb import samba import binascii @@ -101,26 +102,11 @@ class PassWordHashTests(TestCase): # Gets back the configuration basedn configuration_dn = self.ldb.get_config_basedn().get_linearized() - # Get the old "dSHeuristics" if it was set - dsheuristics = self.ldb.get_dsheuristics() + # permit password changes during this test + PasswordCommon.allow_password_changes(self, self.ldb) - # Set the "dSHeuristics" to activate the correct "userPassword" - # behaviour - self.ldb.set_dsheuristics("000000001") - - # Reset the "dSHeuristics" as they were before - self.addCleanup(self.ldb.set_dsheuristics, dsheuristics) - - # Get the old "minPwdAge" - minPwdAge = self.ldb.get_minPwdAge() - - # Set it temporarily to "0" - self.ldb.set_minPwdAge("0") self.base_dn = self.ldb.domain_dn() - # Reset the "minPwdAge" as it was before - self.addCleanup(self.ldb.set_minPwdAge, minPwdAge) - account_control = 0 if clear_text: # get the current pwdProperties diff --git a/python/samba/tests/password_test.py b/python/samba/tests/password_test.py new file mode 100644 index 00000000000..3e78277adf6 --- /dev/null +++ b/python/samba/tests/password_test.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +# +# Common functionality for all password change tests +# +# Copyright (C) Andrew Bartlett 2018 +# +# 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 . +# + +import samba.tests +from samba.samdb import SamDB + +class PasswordCommon: + + @staticmethod + def allow_password_changes(testcase, samdb): + """Updates the DC to allow password changes during the current test""" + + # Get the old "dSHeuristics" if it was set + dsheuristics = samdb.get_dsheuristics() + + # Reset the "dSHeuristics" as they were before + testcase.addCleanup(samdb.set_dsheuristics, dsheuristics) + + # Set the "dSHeuristics" to activate the correct "userPassword" behaviour + samdb.set_dsheuristics("000000001") + + # Get the old "minPwdAge" + minPwdAge = samdb.get_minPwdAge() + + # Reset the "minPwdAge" as it was before + testcase.addCleanup(samdb.set_minPwdAge, minPwdAge) + + # Set it temporarily to "0" + samdb.set_minPwdAge("0") + + +class PasswordTestCase(samba.tests.TestCase): + + # this requires that an LDB connection has already been setup (so is not + # part of the inherited setUp()) + def allow_password_changes(self, samdb=None): + """Updates the DC to allow password changes during the current test""" + + if samdb is None: + samdb = self.ldb + + PasswordCommon.allow_password_changes(self, samdb) + diff --git a/source4/dsdb/tests/python/acl.py b/source4/dsdb/tests/python/acl.py index 357e19b2ea9..25588f6fe10 100755 --- a/source4/dsdb/tests/python/acl.py +++ b/source4/dsdb/tests/python/acl.py @@ -31,6 +31,7 @@ from samba.credentials import Credentials, DONT_USE_KERBEROS import samba.tests from samba.tests import delete_force import samba.dsdb +from samba.tests.password_test import PasswordCommon parser = optparse.OptionParser("acl.py [options] ") sambaopts = options.SambaOptions(parser) @@ -650,18 +651,9 @@ class AclSearchTests(AclTests): def setUp(self): super(AclSearchTests, self).setUp() - # Get the old "dSHeuristics" if it was set - dsheuristics = self.ldb_admin.get_dsheuristics() - # Reset the "dSHeuristics" as they were before - self.addCleanup(self.ldb_admin.set_dsheuristics, dsheuristics) - # Set the "dSHeuristics" to activate the correct "userPassword" behaviour - self.ldb_admin.set_dsheuristics("000000001") - # Get the old "minPwdAge" - minPwdAge = self.ldb_admin.get_minPwdAge() - # Reset the "minPwdAge" as it was before - self.addCleanup(self.ldb_admin.set_minPwdAge, minPwdAge) - # Set it temporarely to "0" - self.ldb_admin.set_minPwdAge("0") + + # permit password changes during this test + PasswordCommon.allow_password_changes(self, self.ldb_admin) self.u1 = "search_u1" self.u2 = "search_u2" diff --git a/source4/dsdb/tests/python/password_lockout_base.py b/source4/dsdb/tests/python/password_lockout_base.py index 164b86f38a6..721948ce00f 100644 --- a/source4/dsdb/tests/python/password_lockout_base.py +++ b/source4/dsdb/tests/python/password_lockout_base.py @@ -14,10 +14,11 @@ import samba.tests from samba.tests import delete_force from samba.dcerpc import security, samr from samba.ndr import ndr_unpack +from samba.tests.password_test import PasswordTestCase import time -class BasePasswordTestCase(samba.tests.TestCase): +class BasePasswordTestCase(PasswordTestCase): def _open_samr_user(self, res): self.assertTrue("objectSid" in res[0]) @@ -272,19 +273,12 @@ userPassword: """ + userpass + """ self.template_creds.set_gensec_features(self.global_creds.get_gensec_features()) self.template_creds.set_kerberos_state(self.global_creds.get_kerberos_state()) - # Gets back the basedn base_dn = self.ldb.domain_dn() # Gets back the configuration basedn configuration_dn = self.ldb.get_config_basedn().get_linearized() - # Get the old "dSHeuristics" if it was set - dsheuristics = self.ldb.get_dsheuristics() - - # Reset the "dSHeuristics" as they were before - self.addCleanup(self.ldb.set_dsheuristics, dsheuristics) - res = self.ldb.search(base_dn, scope=SCOPE_BASE, attrs=["lockoutDuration", "lockOutObservationWindow", "lockoutThreshold"]) @@ -335,17 +329,8 @@ lockoutThreshold: """ + str(lockoutThreshold) + """ self.ldb.modify(m) - # Set the "dSHeuristics" to activate the correct "userPassword" behaviour - self.ldb.set_dsheuristics("000000001") - - # Get the old "minPwdAge" - minPwdAge = self.ldb.get_minPwdAge() - - # Reset the "minPwdAge" as it was before - self.addCleanup(self.ldb.set_minPwdAge, minPwdAge) - - # Set it temporarely to "0" - self.ldb.set_minPwdAge("0") + # update DC to allow password changes for the duration of this test + self.allow_password_changes() self.base_dn = self.ldb.domain_dn() diff --git a/source4/dsdb/tests/python/passwords.py b/source4/dsdb/tests/python/passwords.py index bbb8be1d2ca..5e174de65f6 100755 --- a/source4/dsdb/tests/python/passwords.py +++ b/source4/dsdb/tests/python/passwords.py @@ -19,6 +19,7 @@ sys.path.insert(0, "bin/python") import samba from samba.tests.subunitrun import SubunitOptions, TestProgram +from samba.tests.password_test import PasswordTestCase import samba.getopt as options @@ -35,6 +36,7 @@ from samba import gensec from samba.samdb import SamDB import samba.tests from samba.tests import delete_force +from password_lockout_base import BasePasswordTestCase parser = optparse.OptionParser("passwords.py [options] ") sambaopts = options.SambaOptions(parser) @@ -64,7 +66,7 @@ creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL) # Tests start here # -class PasswordTests(samba.tests.TestCase): +class PasswordTests(PasswordTestCase): def setUp(self): super(PasswordTests, self).setUp() @@ -76,25 +78,11 @@ class PasswordTests(samba.tests.TestCase): # Gets back the configuration basedn configuration_dn = self.ldb.get_config_basedn().get_linearized() - # Get the old "dSHeuristics" if it was set - dsheuristics = self.ldb.get_dsheuristics() + # permit password changes during this test + self.allow_password_changes() - # Set the "dSHeuristics" to activate the correct "userPassword" behaviour - self.ldb.set_dsheuristics("000000001") - - # Reset the "dSHeuristics" as they were before - self.addCleanup(self.ldb.set_dsheuristics, dsheuristics) - - # Get the old "minPwdAge" - minPwdAge = self.ldb.get_minPwdAge() - - # Set it temporarily to "0" - self.ldb.set_minPwdAge("0") self.base_dn = self.ldb.domain_dn() - # Reset the "minPwdAge" as it was before - self.addCleanup(self.ldb.set_minPwdAge, minPwdAge) - # (Re)adds the test user "testuser" with no password atm delete_force(self.ldb, "cn=testuser,cn=users," + self.base_dn) self.ldb.add({ diff --git a/source4/dsdb/tests/python/tombstone_reanimation.py b/source4/dsdb/tests/python/tombstone_reanimation.py index 829998d90de..6185922dcf5 100755 --- a/source4/dsdb/tests/python/tombstone_reanimation.py +++ b/source4/dsdb/tests/python/tombstone_reanimation.py @@ -30,6 +30,7 @@ from samba.dcerpc import misc from samba.dcerpc import security from samba.dcerpc import drsblobs from samba.dcerpc.drsuapi import * +from samba.tests.password_test import PasswordCommon import samba.tests from ldb import (SCOPE_BASE, FLAG_MOD_ADD, FLAG_MOD_DELETE, FLAG_MOD_REPLACE, Dn, Message, @@ -49,21 +50,12 @@ class RestoredObjectAttributesBaseTestCase(samba.tests.TestCase): self.base_dn = self.samdb.domain_dn() self.schema_dn = self.samdb.get_schema_basedn().get_linearized() self.configuration_dn = self.samdb.get_config_basedn().get_linearized() - # Get the old "dSHeuristics" if it was set - self.dsheuristics = self.samdb.get_dsheuristics() - # Set the "dSHeuristics" to activate the correct "userPassword" behaviour - self.samdb.set_dsheuristics("000000001") - # Get the old "minPwdAge" - self.minPwdAge = self.samdb.get_minPwdAge() - # Set it temporary to "0" - self.samdb.set_minPwdAge("0") + + # permit password changes during this test + PasswordCommon.allow_password_changes(self, self.samdb) def tearDown(self): super(RestoredObjectAttributesBaseTestCase, self).tearDown() - # Reset the "dSHeuristics" as they were before - self.samdb.set_dsheuristics(self.dsheuristics) - # Reset the "minPwdAge" as it was before - self.samdb.set_minPwdAge(self.minPwdAge) def GUID_string(self, guid): return self.samdb.schema_format_value("objectGUID", guid)