]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
torture3: Consolidate dbwrap_watch test initialization
authorVolker Lendecke <vl@samba.org>
Sat, 16 Nov 2019 11:41:13 +0000 (12:41 +0100)
committerJeremy Allison <jra@samba.org>
Fri, 22 Nov 2019 23:57:47 +0000 (23:57 +0000)
More lines, but less error-prone copy&paste

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/torture/test_dbwrap_watch.c

index cdfd81175221aa6e81bbaaf906178c3b89feb578..fc27d8963412d5f5cd841b4eab50cc5359ed0097 100644 (file)
 #include "lib/dbwrap/dbwrap_watch.h"
 #include "lib/util/util_tdb.h"
 
-bool run_dbwrap_watch1(int dummy)
+static bool test_dbwrap_watch_init(
+       TALLOC_CTX *mem_ctx,
+       const char *dbname,
+       struct tevent_context **pev,
+       struct messaging_context **pmsg,
+       struct db_context **pbackend,
+       struct db_context **pdb)
 {
        struct tevent_context *ev = NULL;
        struct messaging_context *msg = NULL;
        struct db_context *backend = NULL;
        struct db_context *db = NULL;
-       const char *keystr = "key";
-       TDB_DATA key = string_term_tdb_data(keystr);
-       struct db_record *rec = NULL;
-       struct tevent_req *req = NULL;
-       NTSTATUS status;
-       bool ret = false;
 
-       ev = samba_tevent_context_init(talloc_tos());
+       ev = samba_tevent_context_init(mem_ctx);
        if (ev == NULL) {
                fprintf(stderr, "tevent_context_init failed\n");
                goto fail;
        }
+
        msg = messaging_init(ev, ev);
        if (msg == NULL) {
                fprintf(stderr, "messaging_init failed\n");
                goto fail;
        }
-       backend = db_open(msg, "test_watch.tdb", 0, TDB_CLEAR_IF_FIRST,
-                         O_CREAT|O_RDWR, 0644, DBWRAP_LOCK_ORDER_1,
-                         DBWRAP_FLAG_NONE);
+
+       backend = db_open(
+               msg,
+               dbname,
+               0,
+               TDB_CLEAR_IF_FIRST,
+               O_CREAT|O_RDWR,
+               0644,
+               DBWRAP_LOCK_ORDER_1,
+               DBWRAP_FLAG_NONE);
        if (backend == NULL) {
                fprintf(stderr, "db_open failed: %s\n", strerror(errno));
                goto fail;
        }
 
-       db = db_open_watched(ev, &backend, msg);
+       {
+               struct db_context *backend_copy = backend;
+
+               db = db_open_watched(ev, &backend_copy, msg);
+               if (db == NULL) {
+                       fprintf(stderr, "db_open_watched failed\n");
+                       goto fail;
+               }
+       }
+
+       if (pev != NULL) {
+               *pev = ev;
+       }
+       if (pmsg != NULL) {
+               *pmsg = msg;
+       }
+       if (pbackend != NULL) {
+               *pbackend = backend;
+       }
+       if (pdb != NULL) {
+               *pdb = db;
+       }
+       return true;
+
+fail:
+       TALLOC_FREE(backend);
+       TALLOC_FREE(msg);
+       TALLOC_FREE(ev);
+       return false;
+}
+
+bool run_dbwrap_watch1(int dummy)
+{
+       struct tevent_context *ev = NULL;
+       struct messaging_context *msg = NULL;
+       struct db_context *backend = NULL;
+       struct db_context *db = NULL;
+       const char *keystr = "key";
+       TDB_DATA key = string_term_tdb_data(keystr);
+       struct db_record *rec = NULL;
+       struct tevent_req *req = NULL;
+       NTSTATUS status;
+       bool ret = false;
+
+       ret = test_dbwrap_watch_init(
+               talloc_tos(), "test_watch.tdb", &ev, &msg, &backend, &db);
+       if (!ret) {
+               goto fail;
+       }
 
        rec = dbwrap_fetch_locked(db, db, key);
        if (rec == NULL) {
@@ -124,21 +180,9 @@ bool run_dbwrap_watch2(int dummy)
        NTSTATUS status;
        bool ret = false;
 
-       ev = samba_tevent_context_init(talloc_tos());
-       if (ev == NULL) {
-               fprintf(stderr, "tevent_context_init failed\n");
-               goto fail;
-       }
-       msg = messaging_init(ev, ev);
-       if (msg == NULL) {
-               fprintf(stderr, "messaging_init failed\n");
-               goto fail;
-       }
-       backend = db_open(msg, "test_watch.tdb", 0, TDB_CLEAR_IF_FIRST,
-                         O_CREAT|O_RDWR, 0644, DBWRAP_LOCK_ORDER_1,
-                         DBWRAP_FLAG_NONE);
-       if (backend == NULL) {
-               fprintf(stderr, "db_open failed: %s\n", strerror(errno));
+       ret = test_dbwrap_watch_init(
+               talloc_tos(), "test_watch.tdb", &ev, &msg, &backend, &db);
+       if (!ret) {
                goto fail;
        }
 
@@ -153,13 +197,6 @@ bool run_dbwrap_watch2(int dummy)
                goto fail;
        }
 
-       db = db_open_watched(ev, &backend, msg);
-       if (db == NULL) {
-               fprintf(stderr, "db_open_watched failed\n");
-               goto fail;
-       }
-       backend = NULL;         /* open_watch talloc_moves backend */
-
        status = dbwrap_parse_record(db, key, NULL, NULL);
        if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
                fprintf(stderr, "dbwrap_parse_record returned %s, expected "
@@ -203,27 +240,9 @@ bool run_dbwrap_watch3(int dummy)
                goto fail;
        }
 
-       ev = samba_tevent_context_init(talloc_tos());
-       if (ev == NULL) {
-               fprintf(stderr, "tevent_context_init failed\n");
-               goto fail;
-       }
-       msg = messaging_init(ev, ev);
-       if (msg == NULL) {
-               fprintf(stderr, "messaging_init failed\n");
-               goto fail;
-       }
-       backend = db_open(msg, "test_watch.tdb", 0, TDB_CLEAR_IF_FIRST,
-                         O_CREAT|O_RDWR, 0644, DBWRAP_LOCK_ORDER_1,
-                         DBWRAP_FLAG_NONE);
-       if (backend == NULL) {
-               fprintf(stderr, "db_open failed: %s\n", strerror(errno));
-               goto fail;
-       }
-
-       db = db_open_watched(ev, &backend, msg);
-       if (db == NULL) {
-               fprintf(stderr, "db_open_watched failed\n");
+       ret = test_dbwrap_watch_init(
+               talloc_tos(), "test_watch.tdb", &ev, &msg, &backend, &db);
+       if (!ret) {
                goto fail;
        }