r12608: Remove some unused #include lines.
[abartlet/samba.git/.git] / source4 / lib / ldb / tools / ldbtest.c
index 01fa1b8ed7a3a8b6357302bc590fae9ee4877d1e..f79f5c6b4c968789a4c57c814e225e74e71d3e6e 100644 (file)
 
 #include "includes.h"
 #include "ldb/include/ldb.h"
-#include "ldb/include/ldb_private.h"
+#include "ldb/include/ldb_errors.h"
+#include "ldb/tools/cmdline.h"
 
-static const char *ldb_url;
-static const char *base_dn = "ou=Ldb Test,ou=People,o=University of Michigan,c=US";
+#ifdef _SAMBA_BUILD_
+#include "system/filesys.h"
+#include "system/time.h"
+#endif
 
 static struct timeval tp1,tp2;
+static struct ldb_cmdline *options;
 
 static void _start_timer(void)
 {
@@ -54,65 +58,71 @@ static double _end_timer(void)
 }
 
 static void add_records(struct ldb_context *ldb,
-                       const char *basedn,
+                       const struct ldb_dn *basedn,
                        int count)
 {
        struct ldb_message msg;
        int i;
 
+#if 0
+        if (ldb_lock(ldb, "transaction") != 0) {
+                printf("transaction lock failed\n");
+                exit(1);
+        }
+#endif
        for (i=0;i<count;i++) {
                struct ldb_message_element el[6];
                struct ldb_val vals[6][1];
                char *name;
-               int j;
+               TALLOC_CTX *tmp_ctx = talloc_new(ldb);
 
                asprintf(&name, "Test%d", i);
 
-               asprintf(&msg.dn, "cn=%s,%s", name, basedn);
+               msg.dn = ldb_dn_build_child(tmp_ctx, "cn", name, basedn);
                msg.num_elements = 6;
                msg.elements = el;
 
                el[0].flags = 0;
-               el[0].name = strdup("cn");
+               el[0].name = talloc_strdup(tmp_ctx, "cn");
                el[0].num_values = 1;
                el[0].values = vals[0];
-               vals[0][0].data = name;
+               vals[0][0].data = (uint8_t *)name;
                vals[0][0].length = strlen(name);
 
                el[1].flags = 0;
-               el[1].name = strdup("title");
+               el[1].name = "title";
                el[1].num_values = 1;
                el[1].values = vals[1];
-               asprintf((char **)&vals[1][0].data, "The title of %s", name);
-               vals[1][0].length = strlen(vals[1][0].data);
+               vals[1][0].data = (uint8_t *)talloc_asprintf(tmp_ctx, "The title of %s", name);
+               vals[1][0].length = strlen((char *)vals[1][0].data);
 
                el[2].flags = 0;
-               el[2].name = strdup("uid");
+               el[2].name = talloc_strdup(tmp_ctx, "uid");
                el[2].num_values = 1;
                el[2].values = vals[2];
-               vals[2][0].data = ldb_casefold(ldb, name);
-               vals[2][0].length = strlen(vals[2][0].data);
+               vals[2][0].data = (uint8_t *)ldb_casefold(tmp_ctx, name);
+               vals[2][0].length = strlen((char *)vals[2][0].data);
 
                el[3].flags = 0;
-               el[3].name = strdup("mail");
+               el[3].name = talloc_strdup(tmp_ctx, "mail");
                el[3].num_values = 1;
                el[3].values = vals[3];
-               asprintf((char **)&vals[3][0].data, "%s@example.com", name);
-               vals[3][0].length = strlen(vals[3][0].data);
+               vals[3][0].data = (uint8_t *)talloc_asprintf(tmp_ctx, "%s@example.com", name);
+               vals[3][0].length = strlen((char *)vals[3][0].data);
 
                el[4].flags = 0;
-               el[4].name = strdup("objectClass");
+               el[4].name = talloc_strdup(tmp_ctx, "objectClass");
                el[4].num_values = 1;
                el[4].values = vals[4];
-               vals[4][0].data = strdup("OpenLDAPperson");
-               vals[4][0].length = strlen(vals[4][0].data);
+               vals[4][0].data = (uint8_t *)talloc_strdup(tmp_ctx, "OpenLDAPperson");
+               vals[4][0].length = strlen((char *)vals[4][0].data);
 
                el[5].flags = 0;
-               el[5].name = strdup("sn");
+               el[5].name = talloc_strdup(tmp_ctx, "sn");
                el[5].num_values = 1;
                el[5].values = vals[5];
-               vals[5][0].data = name;
-               vals[5][0].length = strlen(vals[5][0].data);
+               vals[5][0].data = (uint8_t *)name;
+               vals[5][0].length = strlen((char *)vals[5][0].data);
 
                ldb_delete(ldb, msg.dn);
 
@@ -124,22 +134,19 @@ static void add_records(struct ldb_context *ldb,
                printf("adding uid %s\r", name);
                fflush(stdout);
 
-               for (j=0;j<msg.num_elements;j++) {
-                       free(el[j].name);
-               }
-               free(name);
-               free(msg.dn);
-               free(vals[1][0].data);
-               talloc_free(vals[2][0].data);
-               free(vals[3][0].data);
-               free(vals[4][0].data);
+               talloc_free(tmp_ctx);
        }
-
+#if 0
+        if (ldb_unlock(ldb, "transaction") != 0) {
+                printf("transaction unlock failed\n");
+                exit(1);
+        }
+#endif
        printf("\n");
 }
 
 static void modify_records(struct ldb_context *ldb,
-                          const char *basedn,
+                          const struct ldb_dn *basedn,
                           int count)
 {
        struct ldb_message msg;
@@ -149,31 +156,31 @@ static void modify_records(struct ldb_context *ldb,
                struct ldb_message_element el[3];
                struct ldb_val vals[3];
                char *name;
-               int j;
+               TALLOC_CTX *tmp_ctx = talloc_new(ldb);
                
-               asprintf(&name, "Test%d", i);
-               asprintf(&msg.dn, "cn=%s,%s", name, basedn);
+               name = talloc_asprintf(tmp_ctx, "Test%d", i);
+               msg.dn = ldb_dn_build_child(tmp_ctx, "cn", name, basedn);
 
                msg.num_elements = 3;
                msg.elements = el;
 
                el[0].flags = LDB_FLAG_MOD_DELETE;
-               el[0].name = strdup("mail");
+               el[0].name = talloc_strdup(tmp_ctx, "mail");
                el[0].num_values = 0;
 
                el[1].flags = LDB_FLAG_MOD_ADD;
-               el[1].name = strdup("mail");
+               el[1].name = talloc_strdup(tmp_ctx, "mail");
                el[1].num_values = 1;
                el[1].values = &vals[1];
-               asprintf((char **)&vals[1].data, "%s@other.example.com", name);
-               vals[1].length = strlen(vals[1].data);
+               vals[1].data = (uint8_t *)talloc_asprintf(tmp_ctx, "%s@other.example.com", name);
+               vals[1].length = strlen((char *)vals[1].data);
 
                el[2].flags = LDB_FLAG_MOD_REPLACE;
-               el[2].name = strdup("mail");
+               el[2].name = talloc_strdup(tmp_ctx, "mail");
                el[2].num_values = 1;
                el[2].values = &vals[2];
-               asprintf((char **)&vals[2].data, "%s@other2.example.com", name);
-               vals[2].length = strlen(vals[2].data);
+               vals[2].data = (uint8_t *)talloc_asprintf(tmp_ctx, "%s@other2.example.com", name);
+               vals[2].length = strlen((char *)vals[2].data);
 
                if (ldb_modify(ldb, &msg) != 0) {
                        printf("Modify of %s failed - %s\n", name, ldb_errstring(ldb));
@@ -183,13 +190,7 @@ static void modify_records(struct ldb_context *ldb,
                printf("Modifying uid %s\r", name);
                fflush(stdout);
 
-               for (j=0;j<msg.num_elements;j++) {
-                       free(el[j].name);
-               }
-               free(name);
-               free(msg.dn);
-               free(vals[1].data);
-               free(vals[2].data);
+               talloc_free(tmp_ctx);
        }
 
        printf("\n");
@@ -197,58 +198,56 @@ static void modify_records(struct ldb_context *ldb,
 
 
 static void delete_records(struct ldb_context *ldb,
-                          const char *basedn,
+                          const struct ldb_dn *basedn,
                           int count)
 {
        int i;
 
        for (i=0;i<count;i++) {
-               char *dn;
-               asprintf(&dn, "cn=Test%d,%s", i, basedn);
+               struct ldb_dn *dn;
+               char *name = talloc_asprintf(ldb, "Test%d", i);
+               dn = ldb_dn_build_child(name, "cn", name, basedn);
 
                printf("Deleting uid Test%d\r", i);
                fflush(stdout);
 
                if (ldb_delete(ldb, dn) != 0) {
-                       printf("Delete of %s failed - %s\n", dn, ldb_errstring(ldb));
+                       printf("Delete of %s failed - %s\n", ldb_dn_linearize(ldb, dn), ldb_errstring(ldb));
                        exit(1);
                }
-               free(dn);
+               talloc_free(name);
        }
 
        printf("\n");
 }
 
-static void search_uid(struct ldb_context *ldb, int nrecords, int nsearches)
+static void search_uid(struct ldb_context *ldb, struct ldb_dn *basedn, int nrecords, int nsearches)
 {
        int i;
 
        for (i=0;i<nsearches;i++) {
                int uid = (i * 700 + 17) % (nrecords * 2);
                char *expr;
-               struct ldb_message **res;
+               struct ldb_result *res = NULL;
                int ret;
 
                asprintf(&expr, "(uid=TEST%d)", uid);
-               ret = ldb_search(ldb, base_dn, LDB_SCOPE_SUBTREE, expr, NULL, &res);
+               ret = ldb_search(ldb, basedn, LDB_SCOPE_SUBTREE, expr, NULL, &res);
 
-               if (uid < nrecords && ret != 1) {
+               if (ret != LDB_SUCCESS || (uid < nrecords && res->count != 1)) {
                        printf("Failed to find %s - %s\n", expr, ldb_errstring(ldb));
                        exit(1);
                }
 
-               if (uid >= nrecords && ret > 0) {
+               if (uid >= nrecords && res->count > 0) {
                        printf("Found %s !? - %d\n", expr, ret);
                        exit(1);
                }
 
-               if (ret > 0) {
-                       ldb_search_free(ldb, res);
-               }
-
-               printf("testing uid %d/%d - %d  \r", i, uid, ret);
+               printf("testing uid %d/%d - %d  \r", i, uid, res->count);
                fflush(stdout);
 
+               talloc_free(res);
                free(expr);
        }
 
@@ -257,19 +256,23 @@ static void search_uid(struct ldb_context *ldb, int nrecords, int nsearches)
 
 static void start_test(struct ldb_context *ldb, int nrecords, int nsearches)
 {
+       struct ldb_dn *basedn;
+
+       basedn = ldb_dn_explode(ldb, options->basedn);
+
        printf("Adding %d records\n", nrecords);
-       add_records(ldb, base_dn, nrecords);
+       add_records(ldb, basedn, nrecords);
 
        printf("Starting search on uid\n");
        _start_timer();
-       search_uid(ldb, nrecords, nsearches);
+       search_uid(ldb, basedn, nrecords, nsearches);
        printf("uid search took %.2f seconds\n", _end_timer());
 
        printf("Modifying records\n");
-       modify_records(ldb, base_dn, nrecords);
+       modify_records(ldb, basedn, nrecords);
 
        printf("Deleting records\n");
-       delete_records(ldb, base_dn, nrecords);
+       delete_records(ldb, basedn, nrecords);
 }
 
 
@@ -289,55 +292,71 @@ be indexed
 static void start_test_index(struct ldb_context **ldb)
 {
        struct ldb_message *msg;
-       struct ldb_message **res;
+       struct ldb_result *res = NULL;
+       struct ldb_dn *indexlist;
+       struct ldb_dn *basedn;
        int ret;
+       int flags = 0;
+
+       if (options->nosync) {
+               flags |= LDB_FLG_NOSYNC;
+       }
 
        printf("Starting index test\n");
 
-       ldb_delete(*ldb, "@INDEXLIST");
+       indexlist = ldb_dn_explode(NULL, "@INDEXLIST");
+
+       ldb_delete(*ldb, indexlist);
 
        msg = ldb_msg_new(NULL);
 
-       msg->dn = strdup("@INDEXLIST");
-       ldb_msg_add_string(*ldb, msg, "@IDXATTR", strdup("uid"));
+       msg->dn = indexlist;
+       ldb_msg_add_string(msg, "@IDXATTR", strdup("uid"));
 
        if (ldb_add(*ldb, msg) != 0) {
-               printf("Add of %s failed - %s\n", msg->dn, ldb_errstring(*ldb));
+               printf("Add of %s failed - %s\n", ldb_dn_linearize(*ldb, msg->dn), ldb_errstring(*ldb));
                exit(1);
        }
 
+       basedn = ldb_dn_explode(NULL, options->basedn);
+
        memset(msg, 0, sizeof(*msg));
-       asprintf(&msg->dn, "cn=%s,%s", "test", base_dn);
-       ldb_msg_add_string(*ldb, msg, "cn", strdup("test"));
-       ldb_msg_add_string(*ldb, msg, "sn", strdup("test"));
-       ldb_msg_add_string(*ldb, msg, "uid", strdup("test"));
-       ldb_msg_add_string(*ldb, msg, "objectClass", strdup("OpenLDAPperson"));
+       msg->dn = ldb_dn_build_child(msg, "cn", "test", basedn);
+       ldb_msg_add_string(msg, "cn", strdup("test"));
+       ldb_msg_add_string(msg, "sn", strdup("test"));
+       ldb_msg_add_string(msg, "uid", strdup("test"));
+       ldb_msg_add_string(msg, "objectClass", strdup("OpenLDAPperson"));
 
        if (ldb_add(*ldb, msg) != 0) {
-               printf("Add of %s failed - %s\n", msg->dn, ldb_errstring(*ldb));
+               printf("Add of %s failed - %s\n", ldb_dn_linearize(*ldb, msg->dn), ldb_errstring(*ldb));
                exit(1);
        }
 
-       if (ldb_close(*ldb) != 0) {
-               printf("ldb_close failed - %s\n", ldb_errstring(*ldb));
+       if (talloc_free(*ldb) != 0) {
+               printf("failed to free/close ldb database");
                exit(1);
        }
 
-       *ldb = ldb_connect(ldb_url, 0, NULL);
-
-       if (!*ldb) {
-               perror("ldb_connect");
+       (*ldb) = ldb_init(options);
+       
+       ret = ldb_connect(*ldb, options->url, flags, NULL);
+       if (ret != 0) {
+               printf("failed to connect to %s\n", options->url);
                exit(1);
        }
 
-       ret = ldb_search(*ldb, base_dn, LDB_SCOPE_SUBTREE, "uid=test", NULL, &res);
-       if (ret != 1) {
-               printf("Should have found 1 record - found %d\n", ret);
+       ret = ldb_search(*ldb, basedn, LDB_SCOPE_SUBTREE, "uid=test", NULL, &res);
+       if (ret != LDB_SUCCESS) { 
+               printf("Search with (uid=test) filter failed!\n");
+               exit(1);
+       }
+       if(res->count != 1) {
+               printf("Should have found 1 record - found %d\n", res->count);
                exit(1);
        }
 
        if (ldb_delete(*ldb, msg->dn) != 0 ||
-           ldb_delete(*ldb, "@INDEXLIST") != 0) {
+           ldb_delete(*ldb, indexlist) != 0) {
                printf("cleanup failed - %s\n", ldb_errstring(*ldb));
                exit(1);
        }
@@ -351,78 +370,38 @@ static void usage(void)
        printf("Usage: ldbtest <options>\n");
        printf("Options:\n");
        printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
-       printf("  -r nrecords      database size to use\n");
-       printf("  -s nsearches     number of searches to do\n");
+       printf("  --num-records  nrecords      database size to use\n");
+       printf("  --num-searches nsearches     number of searches to do\n");
        printf("\n");
        printf("tests ldb API\n\n");
        exit(1);
 }
 
- int main(int argc, char * const argv[])
+ int main(int argc, const char **argv)
 {
+       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        struct ldb_context *ldb;
-       const char **options = NULL;
-       int ldbopts;
-       int opt;
-       int nrecords = 5000;
-       int nsearches = 2000;
-
-       ldb_url = getenv("LDB_URL");
-
-       ldbopts = 0;
-       while ((opt = getopt(argc, argv, "hH:r:s:b:o:")) != EOF) {
-               switch (opt) {
-               case 'H':
-                       ldb_url = optarg;
-                       break;
-
-               case 'r':
-                       nrecords = atoi(optarg);
-                       break;
-
-               case 'b':
-                       base_dn = optarg;
-                       break;
-
-               case 's':
-                       nsearches = atoi(optarg);
-                       break;
-
-               case 'o':
-                       options = ldb_options_parse(options, &ldbopts, optarg);
-                       break;
-
-               case 'h':
-               default:
-                       usage();
-                       break;
-               }
-       }
 
-       if (!ldb_url) {
-               fprintf(stderr, "You must specify a ldb URL\n\n");
-               usage();
-       }
+       ldb = ldb_init(mem_ctx);
 
-       argc -= optind;
-       argv += optind;
+       options = ldb_cmdline_process(ldb, argc, argv, usage);
 
-       ldb = ldb_connect(ldb_url, 0, options);
+       talloc_steal(mem_ctx, options);
 
-       if (!ldb) {
-               perror("ldb_connect");
-               exit(1);
+       if (options->basedn == NULL) {
+               options->basedn = "ou=Ldb Test,ou=People,o=University of Michigan,c=TEST";
        }
 
-       ldb_set_debug_stderr(ldb);
-
        srandom(1);
 
-       start_test(ldb, nrecords, nsearches);
+       printf("Testing with num-records=%d and num-searches=%d\n", 
+              options->num_records, options->num_searches);
+
+       start_test(ldb, options->num_records, options->num_searches);
 
        start_test_index(&ldb);
 
-       ldb_close(ldb);
+       talloc_free(mem_ctx);
 
        return 0;
 }