r15389: Add some better torture tests for LdbMessage.
authorTim Potter <tpot@samba.org>
Tue, 2 May 2006 05:14:00 +0000 (05:14 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 19:05:31 +0000 (14:05 -0500)
Fix up behaviour of Ldb.__setitem__() function.  It should overwrite the
element data.

Add wrapper for ldb_msg_sanity_check().
(This used to be commit d67e055f86a62d0e61fd20d75b252a6211618f7b)

source4/lib/ldb/swig/Ldb.py
source4/lib/ldb/swig/ldb.i
source4/scripting/swig/torture/torture_ldb.py

index e78d56efdae23e25026686490223466a49b29e76..c7e6191c8a6202ccebd156c52701129dda5da87b 100644 (file)
 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 #
 
+#
+# Interface notes:
+#
+#   - should an empty dn be represented as None, or an empty string?
+#
+#   - should single-valued attributes be a string, or a list with one
+#     element?
+#
+
 from ldb import *
 
 # Global initialisation
@@ -62,6 +71,9 @@ class LdbMessage:
     def __setattr__(self, attr, value):
         if attr == 'dn':
             self.msg.dn = ldb_dn_explode(self.msg, value)
+            if self.msg.dn == None:
+                err = LDB_ERR_INVALID_DN_SYNTAX
+                raise LdbError(err, ldb_strerror(err))
             return
         self.__dict__[attr] = value
         
@@ -78,6 +90,7 @@ class LdbMessage:
                 for i in range(elt.num_values)]
 
     def __setitem__(self, key, value):
+        ldb_msg_remove_attr(self.msg, key)
         if type(value) in (list, tuple):
             [ldb_msg_add_value(self.msg, key, v) for v in value]
         else:
@@ -99,6 +112,11 @@ class LdbMessage:
     def items(self):
         return [(k, self[k]) for k in self.keys()]
 
+    # Misc stuff
+
+    def sanity_check(self):
+        return ldb_msg_sanity_check(self.msg)
+
 class Ldb:
     """A class representing a binding to a ldb file."""
 
index 744857b0a53dd2c1ef389de122d3da825e18a811..09d3461c2a920349f9d81fa08ba97f2bb3800bcb 100644 (file)
@@ -199,13 +199,18 @@ struct ldb_result {
  * Wrap ldb functions 
  */
 
-int ldb_global_init(void);
+/* Initialisation */
 
+int ldb_global_init(void);
 struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx);
 
+/* Error handling */
+
 const char *ldb_errstring(struct ldb_context *ldb);
 const char *ldb_strerror(int ldb_err);
 
+/* Top-level ldb operations */
+
 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[]);
 
 int ldb_search(struct ldb_context *ldb, const struct ldb_dn *base, enum ldb_scope scope, const char *expression, const char * const *attrs, struct ldb_result **OUT);
@@ -216,9 +221,20 @@ int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct
 
 int ldb_add(struct ldb_context *ldb, const struct ldb_message *message);
 
+/* Ldb message operations */
+
 struct ldb_message *ldb_msg_new(void *mem_ctx);
+
 struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg, const char *attr_name);
+
 int ldb_msg_add_value(struct ldb_message *msg, const char *attr_name, const struct ldb_val *INPUT);
 
+void ldb_msg_remove_attr(struct ldb_message *msg, const char *attr);
+
+int ldb_msg_sanity_check(struct ldb_message *msg);
+
+/* DN operations */
+
 struct ldb_dn *ldb_dn_explode(void *mem_ctx, const char *dn);
+
 char *ldb_dn_linearize(void *mem_ctx, const struct ldb_dn *dn);
index 311a2b1d668704723b202b550f5947c7a6ecff83..fe79f4f843b1d23318123338d286d5aebd5bba7e 100755 (executable)
@@ -1,18 +1,83 @@
 #!/usr/bin/python
+#
+# A torture test for the Python Ldb bindings.  Also a short guide on
+# how the API works.
+#
 
-import Ldb, sys
+from Ldb import *
 
-def test(cond, msg):
+# Helpers
+
+def t(cond, msg):
+    """Test a condition."""
     if not cond:
-        print 'FAILED:', msg
-        sys.exit(1)
+        raise RuntimeError('FAILED: %s' % msg)
 
+#
 # Torture LdbMessage
+#
+
+m = LdbMessage()
+
+# Empty message
+
+t(m.keys() == [], 'empty msg')
+t(m.dn == None, 'empty dn')
+
+t(m.sanity_check() == LDB_ERR_INVALID_DN_SYNTAX, 'sanity check')
+
+# Test invalid dn
+
+try:
+    m.dn = 'invalid dn'
+except LdbError, arg:
+    if arg[0] != LDB_ERR_INVALID_DN_SYNTAX:
+        raise
+else:
+    t(False, 'LdbError not raised')
+
+# Test valid dn
+
+m.dn = 'name=spotty'
+t(m.dn == 'name=spotty', 'specified dn')
+
+t(m.sanity_check() == LDB_SUCCESS, 'sanity check')
+
+# Test some single-valued attributes
 
-m = Ldb.LdbMessage()
 m['animal'] = 'dog'
 m['name'] = 'spotty'
 
-test(m.keys() == ['animal', 'name'], 'keys() test failed')
-test(m.values() == [['dog'], ['spotty']], 'values() test failed')
-test(m.items() == [('animal', ['dog']), ('name', ['spotty'])], 'items() test failed')
+t(m.keys() == ['animal', 'name'], 'keys() test failed')
+t(m.values() == [['dog'], ['spotty']], 'values() test failed')
+t(m.items() == [('animal', ['dog']), ('name', ['spotty'])],
+  'items() test failed')
+
+t(m.sanity_check() == LDB_SUCCESS, 'sanity check')
+
+m['animal'] = 'canine'
+t(m['animal'] == ['canine'], 'replace value failed')
+
+# Test a multi-valued attribute
+
+names = ['spotty', 'foot']
+m['name'] = names
+
+t(m['name'] == names, 'multi-valued attr failed')
+
+t(m.sanity_check() == LDB_SUCCESS, 'sanity check')
+
+# Test non-string attributes
+
+try:
+    m['foo'] = 42
+except TypeError:
+    pass
+else:
+    t(False, 'TypeError not raised')
+
+#
+# Torture Ldb
+#
+
+l = Ldb('foo.ldb')