From 8daa83a594a2e98f39d764422bfbdbc62c9efd44 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 19:20:00 +0200 Subject: Adding upstream version 2:4.20.0+dfsg. Signed-off-by: Daniel Baumann --- source3/torture/test_dbwrap_do_locked.c | 161 ++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 source3/torture/test_dbwrap_do_locked.c (limited to 'source3/torture/test_dbwrap_do_locked.c') diff --git a/source3/torture/test_dbwrap_do_locked.c b/source3/torture/test_dbwrap_do_locked.c new file mode 100644 index 0000000..93648ce --- /dev/null +++ b/source3/torture/test_dbwrap_do_locked.c @@ -0,0 +1,161 @@ +/* + * Unix SMB/CIFS implementation. + * Test dbwrap_watch API + * Copyright (C) Volker Lendecke 2017 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "includes.h" +#include "torture/proto.h" +#include "system/filesys.h" +#include "lib/dbwrap/dbwrap.h" +#include "lib/dbwrap/dbwrap_open.h" +#include "lib/dbwrap/dbwrap_watch.h" +#include "lib/util/util_tdb.h" +#include "source3/include/util_tdb.h" +#include "lib/global_contexts.h" + +struct do_locked1_state { + TDB_DATA value; + NTSTATUS status; +}; + +static void do_locked1_cb( + struct db_record *rec, + TDB_DATA value, + void *private_data) +{ + struct do_locked1_state *state = + (struct do_locked1_state *)private_data; + + state->status = dbwrap_record_store(rec, state->value, 0); +} + +static void do_locked1_check(TDB_DATA key, TDB_DATA value, + void *private_data) +{ + struct do_locked1_state *state = + (struct do_locked1_state *)private_data; + int ret; + + ret = tdb_data_cmp(value, state->value); + if (ret != 0) { + state->status = NT_STATUS_DATA_ERROR; + return; + } + + state->status = NT_STATUS_OK; +} + +static void do_locked1_del( + struct db_record *rec, + TDB_DATA value, + void *private_data) +{ + struct do_locked1_state *state = + (struct do_locked1_state *)private_data; + + state->status = dbwrap_record_delete(rec); +} + +bool run_dbwrap_do_locked1(int dummy) +{ + struct tevent_context *ev; + struct messaging_context *msg; + struct db_context *backend; + struct db_context *db; + const char *dbname = "test_do_locked.tdb"; + const char *keystr = "key"; + TDB_DATA key = string_term_tdb_data(keystr); + const char *valuestr = "value"; + TDB_DATA value = string_term_tdb_data(valuestr); + struct do_locked1_state state = { .value = value }; + int ret = false; + NTSTATUS status; + + ev = global_event_context(); + if (ev == NULL) { + fprintf(stderr, "global_event_context() failed\n"); + return false; + } + msg = global_messaging_context(); + if (msg == NULL) { + fprintf(stderr, "global_messaging_context() failed\n"); + return false; + } + + backend = db_open(talloc_tos(), 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)); + return false; + } + + db = db_open_watched(talloc_tos(), &backend, msg); + if (db == NULL) { + fprintf(stderr, "db_open_watched failed: %s\n", + strerror(errno)); + return false; + } + + status = dbwrap_do_locked(db, key, do_locked1_cb, &state); + if (!NT_STATUS_IS_OK(status)) { + fprintf(stderr, "dbwrap_do_locked failed: %s\n", + nt_errstr(status)); + goto fail; + } + if (!NT_STATUS_IS_OK(state.status)) { + fprintf(stderr, "store returned %s\n", + nt_errstr(state.status)); + goto fail; + } + + status = dbwrap_parse_record(db, key, do_locked1_check, &state); + if (!NT_STATUS_IS_OK(status)) { + fprintf(stderr, "dbwrap_parse_record failed: %s\n", + nt_errstr(status)); + goto fail; + } + if (!NT_STATUS_IS_OK(state.status)) { + fprintf(stderr, "data compare returned %s\n", + nt_errstr(status)); + goto fail; + } + + status = dbwrap_do_locked(db, key, do_locked1_del, &state); + if (!NT_STATUS_IS_OK(status)) { + fprintf(stderr, "dbwrap_do_locked failed: %s\n", + nt_errstr(status)); + goto fail; + } + if (!NT_STATUS_IS_OK(state.status)) { + fprintf(stderr, "delete returned %s\n", nt_errstr(status)); + goto fail; + } + + status = dbwrap_parse_record(db, key, do_locked1_check, &state); + if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) { + fprintf(stderr, "parse_record returned %s, " + "expected NOT_FOUND\n", nt_errstr(status)); + goto fail; + } + + ret = true; +fail: + TALLOC_FREE(db); + unlink(dbname); + return ret; +} -- cgit v1.2.3