s4-tests: Added DsdbTestCase class, containing some common methods used by the dsdb...
[nivanova/samba.git] / source4 / scripting / python / samba / tests / __init__.py
index 7a5c2aa694af165d60649bd488e348b8378c86bf..6458b3f585cb0fe6a4f7fdeb14cb182846e3c580 100644 (file)
@@ -113,7 +113,6 @@ class BlackboxTestCase(TestCase):
         line = " ".join(parts)
         subprocess.check_call(line, shell=True)
 
-
 def connect_samdb(samdb_url, lp=None, session_info=None,
                   credentials=None, flags=0, ldb_options=None, ldap_only=False):
     """Creates SamDB instance and connects to samdb_url database.
@@ -157,3 +156,41 @@ def connect_samdb(samdb_url, lp=None, session_info=None,
                  credentials=credentials,
                  flags=flags,
                  options=ldb_options)
+
+from ldb import (
+    SCOPE_BASE, SCOPE_SUBTREE, LdbError, ERR_NO_SUCH_OBJECT)
+from ldb import Message, MessageElement, Dn
+from ldb import FLAG_MOD_REPLACE, FLAG_MOD_DELETE
+from samba.ndr import ndr_unpack
+from samba.dcerpc import security
+
+class DsdbTestCase(TestCase):
+    """Base test case for dsdb tests."""
+
+    def delete_force(self, ldb, dn):
+        try:
+            ldb.delete(dn)
+        except LdbError, (num, _):
+            self.assertEquals(num, ERR_NO_SUCH_OBJECT)
+
+    def find_domain_sid(self, ldb):
+        res = ldb.search(base=ldb.domain_dn(), expression="(objectClass=*)", scope=SCOPE_BASE)
+        return ndr_unpack(security.dom_sid,res[0]["objectSid"][0])
+
+    def set_dsheuristics(self, ldb, dsheuristics):
+        configuration_dn = ldb.get_config_basedn().get_linearized()
+        m = Message()
+        m.dn = Dn(ldb, "CN=Directory Service, CN=Windows NT, CN=Services, "
+                  + configuration_dn)
+        if dsheuristics is not None:
+            m["dSHeuristics"] = MessageElement(dsheuristics, FLAG_MOD_REPLACE,
+                                               "dSHeuristics")
+        else:
+            m["dSHeuristics"] = MessageElement([], FLAG_MOD_DELETE, "dsHeuristics")
+        ldb.modify(m)
+
+    def set_minPwdAge(self, ldb, value):
+        m = Message()
+        m.dn = Dn(ldb, ldb.domain_dn())
+        m["minPwdAge"] = MessageElement(value, FLAG_MOD_REPLACE, "minPwdAge")
+        ldb.modify(m)