2 Unix SMB/CIFS implementation.
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/>.
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(
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
);
43 fprintf(stderr
, "tevent_context_init failed\n");
47 msg
= messaging_init(ev
, ev
);
49 fprintf(stderr
, "messaging_init failed\n");
62 if (backend
== NULL
) {
63 fprintf(stderr
, "db_open failed: %s\n", strerror(errno
));
68 struct db_context
*backend_copy
= backend
;
70 db
= db_open_watched(ev
, &backend_copy
, msg
);
72 fprintf(stderr
, "db_open_watched failed\n");
83 if (pbackend
!= NULL
) {
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
;
111 ret
= test_dbwrap_watch_init(
112 talloc_tos(), "test_watch.tdb", &ev
, &msg
, &backend
, &db
);
117 rec
= dbwrap_fetch_locked(db
, db
, key
);
119 fprintf(stderr
, "dbwrap_fetch_locked failed\n");
122 req
= dbwrap_watched_watch_send(talloc_tos(), ev
, rec
,
123 0, /* resume_instance */
124 (struct server_id
){0});
126 fprintf(stderr
, "dbwrap_record_watch_send failed\n");
131 status
= dbwrap_store_int32_bystring(db
, "different_key", 1);
132 if (!NT_STATUS_IS_OK(status
)) {
133 fprintf(stderr
, "dbwrap_store_int32 failed: %s\n",
138 status
= dbwrap_store_int32_bystring(db
, keystr
, 1);
139 if (!NT_STATUS_IS_OK(status
)) {
140 fprintf(stderr
, "dbwrap_store_int32 failed: %s\n",
145 if (!tevent_req_poll(req
, ev
)) {
146 fprintf(stderr
, "tevent_req_poll failed\n");
150 status
= dbwrap_watched_watch_recv(req
, NULL
, NULL
, NULL
);
151 if (!NT_STATUS_IS_OK(status
)) {
152 fprintf(stderr
, "dbwrap_record_watch_recv failed: %s\n",
157 (void)unlink("test_watch.tdb");
169 * Make sure dbwrap_parse_record does not return NT_STATUS_OK on
173 bool run_dbwrap_watch2(int dummy
)
175 struct tevent_context
*ev
= NULL
;
176 struct messaging_context
*msg
= NULL
;
177 struct db_context
*backend
= NULL
;
178 struct db_context
*db
= NULL
;
179 const char *keystr
= "key";
180 TDB_DATA key
= string_term_tdb_data(keystr
);
184 ret
= test_dbwrap_watch_init(
185 talloc_tos(), "test_watch.tdb", &ev
, &msg
, &backend
, &db
);
191 * Store invalid data (from the dbwrap_watch point of view)
192 * directly into the backend database
194 status
= dbwrap_store_uint32_bystring(backend
, keystr
, UINT32_MAX
);
195 if (!NT_STATUS_IS_OK(status
)) {
196 fprintf(stderr
, "dbwrap_store_uint32_bystring failed: %s\n",
201 status
= dbwrap_parse_record(db
, key
, NULL
, NULL
);
202 if (!NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
203 fprintf(stderr
, "dbwrap_parse_record returned %s, expected "
204 "NT_STATUS_NOT_FOUND\n", nt_errstr(status
));
208 (void)unlink("test_watch.tdb");
218 * Test autocleanup of dead watchers
221 bool run_dbwrap_watch3(int dummy
)
223 struct tevent_context
*ev
= NULL
;
224 struct messaging_context
*msg
= NULL
;
225 struct db_context
*backend
= NULL
;
226 struct db_context
*db
= NULL
;
227 const char *keystr
= "key";
228 TDB_DATA key
= string_term_tdb_data(keystr
);
232 int wstatus
, exit_status
;
234 BlockSignals(true, SIGCHLD
);
244 ret
= test_dbwrap_watch_init(
245 talloc_tos(), "test_watch.tdb", &ev
, &msg
, &backend
, &db
);
251 struct db_record
*rec
= dbwrap_fetch_locked(db
, db
, key
);
252 struct tevent_req
*req
= NULL
;
255 fprintf(stderr
, "dbwrap_fetch_locked failed\n");
259 req
= dbwrap_watched_watch_send(
260 db
, ev
, rec
, 0, (struct server_id
) { 0 });
262 fprintf(stderr
, "dbwrap_watched_watch_send failed\n");
269 waited
= waitpid(child
, &wstatus
, 0);
271 fprintf(stderr
, "waitpid failed: %s\n", strerror(errno
));
274 if (!WIFEXITED(wstatus
)) {
275 fprintf(stderr
, "child did not exit normally\n");
279 exit_status
= WEXITSTATUS(wstatus
);
280 if (exit_status
!= 0) {
281 fprintf(stderr
, "exit status is %d\n", exit_status
);
285 status
= dbwrap_store_uint32_bystring(db
, keystr
, 1);
286 if (!NT_STATUS_EQUAL(status
, NT_STATUS_OK
)) {
288 "dbwrap_store_uint32 returned %s\n",
293 (void)unlink("test_watch.tdb");
303 * Test that we can't add two watchers in the same
304 * fetch_lock/do_locked round
307 struct dbwrap_watch4_state
{
309 struct tevent_context
*ev
;
310 struct db_context
*db
;
315 struct tevent_req
*req1
;
318 struct tevent_req
*req2
;
322 static void dbwrap_watch4_done1(struct tevent_req
*subreq
);
323 static void dbwrap_watch4_done2(struct tevent_req
*subreq
);
325 static void dbwrap_watch4_fn(struct db_record
*rec
,
329 struct dbwrap_watch4_state
*state
= private_data
;
332 state
->req1
= dbwrap_watched_watch_send(
333 state
->mem_ctx
, state
->ev
, rec
, 0, (struct server_id
) { .pid
=0 });
334 if (state
->req1
== NULL
) {
337 tevent_req_set_callback(state
->req1
, dbwrap_watch4_done1
, state
);
338 state
->status1
= NT_STATUS_EVENT_PENDING
;
340 ok
= tevent_req_set_endtime(
341 state
->req1
, state
->ev
, timeval_current_ofs(1, 0));
346 state
->req2
= dbwrap_watched_watch_send(
347 state
->mem_ctx
, state
->ev
, rec
, 0, (struct server_id
) { .pid
=0 });
348 if (state
->req2
== NULL
) {
351 tevent_req_set_callback(state
->req2
, dbwrap_watch4_done2
, state
);
352 state
->status2
= NT_STATUS_EVENT_PENDING
;
354 ok
= tevent_req_set_endtime(
355 state
->req2
, state
->ev
, timeval_current_ofs(1, 0));
360 state
->status
= NT_STATUS_OK
;
364 state
->status
= NT_STATUS_NO_MEMORY
;
367 static void dbwrap_watch4_done1(struct tevent_req
*subreq
)
369 struct dbwrap_watch4_state
*state
= tevent_req_callback_data_void(subreq
);
370 state
->status1
= dbwrap_watched_watch_recv(subreq
, NULL
, NULL
, NULL
);
372 printf("req1 finished: %s\n", nt_errstr(state
->status1
));
376 static void dbwrap_watch4_done2(struct tevent_req
*subreq
)
378 struct dbwrap_watch4_state
*state
= tevent_req_callback_data_void(subreq
);
379 state
->status2
= dbwrap_watched_watch_recv(subreq
, NULL
, NULL
, NULL
);
381 printf("req2 finished: %s\n", nt_errstr(state
->status2
));
385 bool run_dbwrap_watch4(int dummy
)
387 struct tevent_context
*ev
= NULL
;
388 struct messaging_context
*msg
= NULL
;
389 struct db_context
*backend
= NULL
;
390 struct db_context
*db
= NULL
;
391 const char *keystr
= "key";
392 TDB_DATA key
= string_term_tdb_data(keystr
);
393 struct dbwrap_watch4_state state
= { 0 };
398 ok
= test_dbwrap_watch_init(
399 talloc_tos(), "test_watch.tdb", &ev
, &msg
, &backend
, &db
);
404 state
= (struct dbwrap_watch4_state
) {
405 .mem_ctx
= talloc_tos(),
411 status
= dbwrap_do_locked(db
, key
, dbwrap_watch4_fn
, &state
);
412 if (!NT_STATUS_IS_OK(status
)) {
414 "dbwrap_do_locked failed: %s\n",
418 if (!NT_STATUS_IS_OK(state
.status
)) {
420 "dbwrap_watch4_fn failed: %s\n",
425 status
= dbwrap_store(db
, key
, key
, 0);
426 if (!NT_STATUS_IS_OK(status
)) {
428 "dbwrap_store failed: %s\n",
433 while (NT_STATUS_EQUAL(state
.status1
, NT_STATUS_EVENT_PENDING
) ||
434 NT_STATUS_EQUAL(state
.status2
, NT_STATUS_EVENT_PENDING
)) {
435 int res
= tevent_loop_once(ev
);
438 "tevent_loop_once failed: %s\n",
444 if (!NT_STATUS_IS_OK(state
.status1
)) {
446 "req1 returned %s\n",
447 nt_errstr(state
.status1
));
451 if (!NT_STATUS_EQUAL(state
.status2
, NT_STATUS_REQUEST_NOT_ACCEPTED
)) {
453 "req2 returned %s\n",
454 nt_errstr(state
.status2
));
458 (void)unlink("test_watch.tdb");
461 TALLOC_FREE(state
.req2
);
462 TALLOC_FREE(state
.req1
);