s4:kdc: split out a sdb_keys_free() helper function
[Samba.git] / source3 / torture / test_dbwrap_do_locked.c
blob93648ced79f114807f0838f50455050ab4affea8
1 /*
2 * Unix SMB/CIFS implementation.
3 * Test dbwrap_watch API
4 * Copyright (C) Volker Lendecke 2017
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "torture/proto.h"
22 #include "system/filesys.h"
23 #include "lib/dbwrap/dbwrap.h"
24 #include "lib/dbwrap/dbwrap_open.h"
25 #include "lib/dbwrap/dbwrap_watch.h"
26 #include "lib/util/util_tdb.h"
27 #include "source3/include/util_tdb.h"
28 #include "lib/global_contexts.h"
30 struct do_locked1_state {
31 TDB_DATA value;
32 NTSTATUS status;
35 static void do_locked1_cb(
36 struct db_record *rec,
37 TDB_DATA value,
38 void *private_data)
40 struct do_locked1_state *state =
41 (struct do_locked1_state *)private_data;
43 state->status = dbwrap_record_store(rec, state->value, 0);
46 static void do_locked1_check(TDB_DATA key, TDB_DATA value,
47 void *private_data)
49 struct do_locked1_state *state =
50 (struct do_locked1_state *)private_data;
51 int ret;
53 ret = tdb_data_cmp(value, state->value);
54 if (ret != 0) {
55 state->status = NT_STATUS_DATA_ERROR;
56 return;
59 state->status = NT_STATUS_OK;
62 static void do_locked1_del(
63 struct db_record *rec,
64 TDB_DATA value,
65 void *private_data)
67 struct do_locked1_state *state =
68 (struct do_locked1_state *)private_data;
70 state->status = dbwrap_record_delete(rec);
73 bool run_dbwrap_do_locked1(int dummy)
75 struct tevent_context *ev;
76 struct messaging_context *msg;
77 struct db_context *backend;
78 struct db_context *db;
79 const char *dbname = "test_do_locked.tdb";
80 const char *keystr = "key";
81 TDB_DATA key = string_term_tdb_data(keystr);
82 const char *valuestr = "value";
83 TDB_DATA value = string_term_tdb_data(valuestr);
84 struct do_locked1_state state = { .value = value };
85 int ret = false;
86 NTSTATUS status;
88 ev = global_event_context();
89 if (ev == NULL) {
90 fprintf(stderr, "global_event_context() failed\n");
91 return false;
93 msg = global_messaging_context();
94 if (msg == NULL) {
95 fprintf(stderr, "global_messaging_context() failed\n");
96 return false;
99 backend = db_open(talloc_tos(), dbname, 0,
100 TDB_CLEAR_IF_FIRST, O_CREAT|O_RDWR, 0644,
101 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
102 if (backend == NULL) {
103 fprintf(stderr, "db_open failed: %s\n", strerror(errno));
104 return false;
107 db = db_open_watched(talloc_tos(), &backend, msg);
108 if (db == NULL) {
109 fprintf(stderr, "db_open_watched failed: %s\n",
110 strerror(errno));
111 return false;
114 status = dbwrap_do_locked(db, key, do_locked1_cb, &state);
115 if (!NT_STATUS_IS_OK(status)) {
116 fprintf(stderr, "dbwrap_do_locked failed: %s\n",
117 nt_errstr(status));
118 goto fail;
120 if (!NT_STATUS_IS_OK(state.status)) {
121 fprintf(stderr, "store returned %s\n",
122 nt_errstr(state.status));
123 goto fail;
126 status = dbwrap_parse_record(db, key, do_locked1_check, &state);
127 if (!NT_STATUS_IS_OK(status)) {
128 fprintf(stderr, "dbwrap_parse_record failed: %s\n",
129 nt_errstr(status));
130 goto fail;
132 if (!NT_STATUS_IS_OK(state.status)) {
133 fprintf(stderr, "data compare returned %s\n",
134 nt_errstr(status));
135 goto fail;
138 status = dbwrap_do_locked(db, key, do_locked1_del, &state);
139 if (!NT_STATUS_IS_OK(status)) {
140 fprintf(stderr, "dbwrap_do_locked failed: %s\n",
141 nt_errstr(status));
142 goto fail;
144 if (!NT_STATUS_IS_OK(state.status)) {
145 fprintf(stderr, "delete returned %s\n", nt_errstr(status));
146 goto fail;
149 status = dbwrap_parse_record(db, key, do_locked1_check, &state);
150 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
151 fprintf(stderr, "parse_record returned %s, "
152 "expected NOT_FOUND\n", nt_errstr(status));
153 goto fail;
156 ret = true;
157 fail:
158 TALLOC_FREE(db);
159 unlink(dbname);
160 return ret;