summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVolker Lendecke <vl@samba.org>2019-11-16 12:41:13 +0100
committerJeremy Allison <jra@samba.org>2019-11-22 23:57:47 +0000
commite341911ddad078c3c0c95f7e0ec16701510cb273 (patch)
tree77f3652ad1e52411bc25fdd16e6e7d5e1c5faf73
parentca8c23bb42e501d5fe7f47f6c59feb5e7524a0cf (diff)
downloadsamba-e341911ddad078c3c0c95f7e0ec16701510cb273.tar.gz
torture3: Consolidate dbwrap_watch test initialization
More lines, but less error-prone copy&paste Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
-rw-r--r--source3/torture/test_dbwrap_watch.c129
1 files changed, 74 insertions, 55 deletions
diff --git a/source3/torture/test_dbwrap_watch.c b/source3/torture/test_dbwrap_watch.c
index cdfd8117522..fc27d896341 100644
--- a/source3/torture/test_dbwrap_watch.c
+++ b/source3/torture/test_dbwrap_watch.c
@@ -25,38 +25,94 @@
#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;
}