s4:selftest: explicitly set NSS/RESOLV_WAPPER_* in wait_for_start
[Samba.git] / source3 / torture / test_dbwrap_do_locked.c
blob46b326bf16354e124bbd1bc14b9d9f9d043caf35
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"
29 struct do_locked1_state {
30 TDB_DATA value;
31 NTSTATUS status;
34 static void do_locked1_cb(struct db_record *rec, void *private_data)
36 struct do_locked1_state *state =
37 (struct do_locked1_state *)private_data;
39 state->status = dbwrap_record_store(rec, state->value, 0);
42 static void do_locked1_check(TDB_DATA key, TDB_DATA value,
43 void *private_data)
45 struct do_locked1_state *state =
46 (struct do_locked1_state *)private_data;
47 int ret;
49 ret = tdb_data_cmp(value, state->value);
50 if (ret != 0) {
51 state->status = NT_STATUS_DATA_ERROR;
52 return;
55 state->status = NT_STATUS_OK;
58 static void do_locked1_del(struct db_record *rec, void *private_data)
60 struct do_locked1_state *state =
61 (struct do_locked1_state *)private_data;
63 state->status = dbwrap_record_delete(rec);
66 bool run_dbwrap_do_locked1(int dummy)
68 struct tevent_context *ev;
69 struct messaging_context *msg;
70 struct db_context *backend;
71 struct db_context *db;
72 const char *dbname = "test_do_locked.tdb";
73 const char *keystr = "key";
74 TDB_DATA key = string_term_tdb_data(keystr);
75 const char *valuestr = "value";
76 TDB_DATA value = string_term_tdb_data(valuestr);
77 struct do_locked1_state state = { .value = value };
78 int ret = false;
79 NTSTATUS status;
81 ev = server_event_context();
82 if (ev == NULL) {
83 fprintf(stderr, "server_event_context() failed\n");
84 return false;
86 msg = server_messaging_context();
87 if (msg == NULL) {
88 fprintf(stderr, "server_messaging_context() failed\n");
89 return false;
92 backend = db_open(talloc_tos(), dbname, 0,
93 TDB_CLEAR_IF_FIRST, O_CREAT|O_RDWR, 0644,
94 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
95 if (backend == NULL) {
96 fprintf(stderr, "db_open failed: %s\n", strerror(errno));
97 return false;
100 db = db_open_watched(talloc_tos(), backend, msg);
101 if (db == NULL) {
102 fprintf(stderr, "db_open_watched failed: %s\n",
103 strerror(errno));
104 return false;
107 status = dbwrap_do_locked(db, key, do_locked1_cb, &state);
108 if (!NT_STATUS_IS_OK(status)) {
109 fprintf(stderr, "dbwrap_do_locked failed: %s\n",
110 nt_errstr(status));
111 goto fail;
113 if (!NT_STATUS_IS_OK(state.status)) {
114 fprintf(stderr, "store returned %s\n",
115 nt_errstr(state.status));
116 goto fail;
119 status = dbwrap_parse_record(db, key, do_locked1_check, &state);
120 if (!NT_STATUS_IS_OK(status)) {
121 fprintf(stderr, "dbwrap_parse_record failed: %s\n",
122 nt_errstr(status));
123 goto fail;
125 if (!NT_STATUS_IS_OK(state.status)) {
126 fprintf(stderr, "data compare returned %s\n",
127 nt_errstr(status));
128 goto fail;
131 status = dbwrap_do_locked(db, key, do_locked1_del, &state);
132 if (!NT_STATUS_IS_OK(status)) {
133 fprintf(stderr, "dbwrap_do_locked failed: %s\n",
134 nt_errstr(status));
135 goto fail;
137 if (!NT_STATUS_IS_OK(state.status)) {
138 fprintf(stderr, "delete returned %s\n", nt_errstr(status));
139 goto fail;
142 status = dbwrap_parse_record(db, key, do_locked1_check, &state);
143 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
144 fprintf(stderr, "parse_record returned %s, "
145 "expected NOT_FOUND\n", nt_errstr(status));
146 goto fail;
149 ret = true;
150 fail:
151 TALLOC_FREE(db);
152 unlink(dbname);
153 return ret;