lib: talloc: Fix memlimit on pool realloc.
[Samba.git] / source3 / torture / test_dbwrap_watch.c
blobd66bdaa991417010310f600f6cef6e7fb471ab23
1 /*
2 Unix SMB/CIFS implementation.
3 Test dbwrap_watch API
4 Copyright (C) Volker Lendecke 2012
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"
28 static bool test_dbwrap_watch_init(
29 TALLOC_CTX *mem_ctx,
30 const char *dbname,
31 struct tevent_context **pev,
32 struct messaging_context **pmsg,
33 struct db_context **pbackend,
34 struct db_context **pdb)
36 struct tevent_context *ev = NULL;
37 struct messaging_context *msg = NULL;
38 struct db_context *backend = NULL;
39 struct db_context *db = NULL;
41 ev = samba_tevent_context_init(mem_ctx);
42 if (ev == NULL) {
43 fprintf(stderr, "tevent_context_init failed\n");
44 goto fail;
47 msg = messaging_init(ev, ev);
48 if (msg == NULL) {
49 fprintf(stderr, "messaging_init failed\n");
50 goto fail;
53 backend = db_open(
54 msg,
55 dbname,
57 TDB_CLEAR_IF_FIRST,
58 O_CREAT|O_RDWR,
59 0644,
60 DBWRAP_LOCK_ORDER_1,
61 DBWRAP_FLAG_NONE);
62 if (backend == NULL) {
63 fprintf(stderr, "db_open failed: %s\n", strerror(errno));
64 goto fail;
68 struct db_context *backend_copy = backend;
70 db = db_open_watched(ev, &backend_copy, msg);
71 if (db == NULL) {
72 fprintf(stderr, "db_open_watched failed\n");
73 goto fail;
77 if (pev != NULL) {
78 *pev = ev;
80 if (pmsg != NULL) {
81 *pmsg = msg;
83 if (pbackend != NULL) {
84 *pbackend = backend;
86 if (pdb != NULL) {
87 *pdb = db;
89 return true;
91 fail:
92 TALLOC_FREE(backend);
93 TALLOC_FREE(msg);
94 TALLOC_FREE(ev);
95 return false;
98 bool run_dbwrap_watch1(int dummy)
100 struct tevent_context *ev = NULL;
101 struct messaging_context *msg = NULL;
102 struct db_context *backend = NULL;
103 struct db_context *db = NULL;
104 const char *keystr = "key";
105 TDB_DATA key = string_term_tdb_data(keystr);
106 struct db_record *rec = NULL;
107 struct tevent_req *req = NULL;
108 NTSTATUS status;
109 bool ret = false;
111 ret = test_dbwrap_watch_init(
112 talloc_tos(), "test_watch.tdb", &ev, &msg, &backend, &db);
113 if (!ret) {
114 goto fail;
117 rec = dbwrap_fetch_locked(db, db, key);
118 if (rec == NULL) {
119 fprintf(stderr, "dbwrap_fetch_locked failed\n");
120 goto fail;
122 req = dbwrap_watched_watch_send(talloc_tos(), ev, rec,
123 (struct server_id){0});
124 if (req == NULL) {
125 fprintf(stderr, "dbwrap_record_watch_send failed\n");
126 goto fail;
128 TALLOC_FREE(rec);
130 status = dbwrap_store_int32_bystring(db, "different_key", 1);
131 if (!NT_STATUS_IS_OK(status)) {
132 fprintf(stderr, "dbwrap_store_int32 failed: %s\n",
133 nt_errstr(status));
134 goto fail;
137 status = dbwrap_store_int32_bystring(db, keystr, 1);
138 if (!NT_STATUS_IS_OK(status)) {
139 fprintf(stderr, "dbwrap_store_int32 failed: %s\n",
140 nt_errstr(status));
141 goto fail;
144 if (!tevent_req_poll(req, ev)) {
145 fprintf(stderr, "tevent_req_poll failed\n");
146 goto fail;
149 status = dbwrap_watched_watch_recv(req, NULL, NULL);
150 if (!NT_STATUS_IS_OK(status)) {
151 fprintf(stderr, "dbwrap_record_watch_recv failed: %s\n",
152 nt_errstr(status));
153 goto fail;
156 (void)unlink("test_watch.tdb");
157 ret = true;
158 fail:
159 TALLOC_FREE(req);
160 TALLOC_FREE(rec);
161 TALLOC_FREE(db);
162 TALLOC_FREE(msg);
163 TALLOC_FREE(ev);
164 return ret;
168 * Make sure dbwrap_parse_record does not return NT_STATUS_OK on
169 * invalid data
172 bool run_dbwrap_watch2(int dummy)
174 struct tevent_context *ev = NULL;
175 struct messaging_context *msg = NULL;
176 struct db_context *backend = NULL;
177 struct db_context *db = NULL;
178 const char *keystr = "key";
179 TDB_DATA key = string_term_tdb_data(keystr);
180 NTSTATUS status;
181 bool ret = false;
183 ret = test_dbwrap_watch_init(
184 talloc_tos(), "test_watch.tdb", &ev, &msg, &backend, &db);
185 if (!ret) {
186 goto fail;
190 * Store invalid data (from the dbwrap_watch point of view)
191 * directly into the backend database
193 status = dbwrap_store_uint32_bystring(backend, keystr, UINT32_MAX);
194 if (!NT_STATUS_IS_OK(status)) {
195 fprintf(stderr, "dbwrap_store_uint32_bystring failed: %s\n",
196 nt_errstr(status));
197 goto fail;
200 status = dbwrap_parse_record(db, key, NULL, NULL);
201 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
202 fprintf(stderr, "dbwrap_parse_record returned %s, expected "
203 "NT_STATUS_NOT_FOUND\n", nt_errstr(status));
204 goto fail;
207 (void)unlink("test_watch.tdb");
208 ret = true;
209 fail:
210 TALLOC_FREE(db);
211 TALLOC_FREE(msg);
212 TALLOC_FREE(ev);
213 return ret;
217 * Test autocleanup of dead watchers
220 bool run_dbwrap_watch3(int dummy)
222 struct tevent_context *ev = NULL;
223 struct messaging_context *msg = NULL;
224 struct db_context *backend = NULL;
225 struct db_context *db = NULL;
226 const char *keystr = "key";
227 TDB_DATA key = string_term_tdb_data(keystr);
228 NTSTATUS status;
229 bool ret = false;
230 pid_t child, waited;
231 int wstatus, exit_status;
233 BlockSignals(true, SIGCHLD);
235 child = fork();
236 if (child == -1) {
237 fprintf(stderr,
238 "fork failed: %s\n",
239 strerror(errno));
240 goto fail;
243 ret = test_dbwrap_watch_init(
244 talloc_tos(), "test_watch.tdb", &ev, &msg, &backend, &db);
245 if (!ret) {
246 goto fail;
249 if (child == 0) {
250 struct db_record *rec = dbwrap_fetch_locked(db, db, key);
251 struct tevent_req *req = NULL;
253 if (rec == NULL) {
254 fprintf(stderr, "dbwrap_fetch_locked failed\n");
255 exit(1);
258 req = dbwrap_watched_watch_send(
259 db, ev, rec, (struct server_id) { 0 });
260 if (req == NULL) {
261 fprintf(stderr, "dbwrap_watched_watch_send failed\n");
262 exit(2);
265 exit(0);
268 waited = waitpid(child, &wstatus, 0);
269 if (waited == -1) {
270 fprintf(stderr, "waitpid failed: %s\n", strerror(errno));
271 goto fail;
273 if (!WIFEXITED(wstatus)) {
274 fprintf(stderr, "child did not exit normally\n");
275 goto fail;
278 exit_status = WEXITSTATUS(wstatus);
279 if (exit_status != 0) {
280 fprintf(stderr, "exit status is %d\n", exit_status);
281 goto fail;
284 status = dbwrap_store_uint32_bystring(db, keystr, 1);
285 if (!NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
286 fprintf(stderr,
287 "dbwrap_store_uint32 returned %s\n",
288 nt_errstr(status));
289 goto fail;
292 (void)unlink("test_watch.tdb");
293 ret = true;
294 fail:
295 TALLOC_FREE(db);
296 TALLOC_FREE(msg);
297 TALLOC_FREE(ev);
298 return ret;
302 * Test that we can't add two watchers in the same
303 * fetch_lock/do_locked round
306 struct dbwrap_watch4_state {
307 TALLOC_CTX *mem_ctx;
308 struct tevent_context *ev;
309 struct db_context *db;
310 TDB_DATA key;
312 NTSTATUS status;
314 struct tevent_req *req1;
315 NTSTATUS status1;
317 struct tevent_req *req2;
318 NTSTATUS status2;
321 static void dbwrap_watch4_done1(struct tevent_req *subreq);
322 static void dbwrap_watch4_done2(struct tevent_req *subreq);
324 static void dbwrap_watch4_fn(struct db_record *rec,
325 TDB_DATA value,
326 void *private_data)
328 struct dbwrap_watch4_state *state = private_data;
329 bool ok;
331 state->req1 = dbwrap_watched_watch_send(
332 state->mem_ctx, state->ev, rec, (struct server_id) { .pid=0 });
333 if (state->req1 == NULL) {
334 goto nomem;
336 tevent_req_set_callback(state->req1, dbwrap_watch4_done1, state);
337 state->status1 = NT_STATUS_EVENT_PENDING;
339 ok = tevent_req_set_endtime(
340 state->req1, state->ev, timeval_current_ofs(1, 0));
341 if (!ok) {
342 goto nomem;
345 state->req2 = dbwrap_watched_watch_send(
346 state->mem_ctx, state->ev, rec, (struct server_id) { .pid=0 });
347 if (state->req2 == NULL) {
348 goto nomem;
350 tevent_req_set_callback(state->req2, dbwrap_watch4_done2, state);
351 state->status2 = NT_STATUS_EVENT_PENDING;
353 ok = tevent_req_set_endtime(
354 state->req2, state->ev, timeval_current_ofs(1, 0));
355 if (!ok) {
356 goto nomem;
359 state->status = NT_STATUS_OK;
360 return;
362 nomem:
363 state->status = NT_STATUS_NO_MEMORY;
366 static void dbwrap_watch4_done1(struct tevent_req *subreq)
368 struct dbwrap_watch4_state *state = tevent_req_callback_data_void(subreq);
369 state->status1 = dbwrap_watched_watch_recv(subreq, NULL, NULL);
370 TALLOC_FREE(subreq);
371 printf("req1 finished: %s\n", nt_errstr(state->status1));
372 state->req1 = NULL;
375 static void dbwrap_watch4_done2(struct tevent_req *subreq)
377 struct dbwrap_watch4_state *state = tevent_req_callback_data_void(subreq);
378 state->status2 = dbwrap_watched_watch_recv(subreq, NULL, NULL);
379 TALLOC_FREE(subreq);
380 printf("req2 finished: %s\n", nt_errstr(state->status2));
381 state->req2 = NULL;
384 bool run_dbwrap_watch4(int dummy)
386 struct tevent_context *ev = NULL;
387 struct messaging_context *msg = NULL;
388 struct db_context *backend = NULL;
389 struct db_context *db = NULL;
390 const char *keystr = "key";
391 TDB_DATA key = string_term_tdb_data(keystr);
392 struct dbwrap_watch4_state state = { 0 };
393 NTSTATUS status;
394 bool ret = false;
395 bool ok;
397 ok = test_dbwrap_watch_init(
398 talloc_tos(), "test_watch.tdb", &ev, &msg, &backend, &db);
399 if (!ok) {
400 goto fail;
403 state = (struct dbwrap_watch4_state) {
404 .mem_ctx = talloc_tos(),
405 .ev = ev,
406 .db = db,
407 .key = key,
410 status = dbwrap_do_locked(db, key, dbwrap_watch4_fn, &state);
411 if (!NT_STATUS_IS_OK(status)) {
412 fprintf(stderr,
413 "dbwrap_do_locked failed: %s\n",
414 nt_errstr(status));
415 goto fail;
417 if (!NT_STATUS_IS_OK(state.status)) {
418 fprintf(stderr,
419 "dbwrap_watch4_fn failed: %s\n",
420 nt_errstr(status));
421 goto fail;
424 status = dbwrap_store(db, key, key, 0);
425 if (!NT_STATUS_IS_OK(status)) {
426 fprintf(stderr,
427 "dbwrap_store failed: %s\n",
428 nt_errstr(status));
429 goto fail;
432 while (NT_STATUS_EQUAL(state.status1, NT_STATUS_EVENT_PENDING) ||
433 NT_STATUS_EQUAL(state.status2, NT_STATUS_EVENT_PENDING)) {
434 int res = tevent_loop_once(ev);
435 if (res != 0) {
436 fprintf(stderr,
437 "tevent_loop_once failed: %s\n",
438 strerror(errno));
439 goto fail;
443 if (!NT_STATUS_IS_OK(state.status1)) {
444 fprintf(stderr,
445 "req1 returned %s\n",
446 nt_errstr(state.status1));
447 goto fail;
450 if (!NT_STATUS_EQUAL(state.status2, NT_STATUS_REQUEST_NOT_ACCEPTED)) {
451 fprintf(stderr,
452 "req2 returned %s\n",
453 nt_errstr(state.status2));
454 goto fail;
457 (void)unlink("test_watch.tdb");
458 ret = true;
459 fail:
460 TALLOC_FREE(state.req2);
461 TALLOC_FREE(state.req1);
462 TALLOC_FREE(db);
463 TALLOC_FREE(msg);
464 TALLOC_FREE(ev);
465 return ret;