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 (struct server_id
){0});
125 fprintf(stderr
, "dbwrap_record_watch_send failed\n");
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",
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",
144 if (!tevent_req_poll(req
, ev
)) {
145 fprintf(stderr
, "tevent_req_poll failed\n");
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",
156 (void)unlink("test_watch.tdb");
168 * Make sure dbwrap_parse_record does not return NT_STATUS_OK on
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
);
183 ret
= test_dbwrap_watch_init(
184 talloc_tos(), "test_watch.tdb", &ev
, &msg
, &backend
, &db
);
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",
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
));
207 (void)unlink("test_watch.tdb");
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
);
231 int wstatus
, exit_status
;
233 BlockSignals(true, SIGCHLD
);
243 ret
= test_dbwrap_watch_init(
244 talloc_tos(), "test_watch.tdb", &ev
, &msg
, &backend
, &db
);
250 struct db_record
*rec
= dbwrap_fetch_locked(db
, db
, key
);
251 struct tevent_req
*req
= NULL
;
254 fprintf(stderr
, "dbwrap_fetch_locked failed\n");
258 req
= dbwrap_watched_watch_send(
259 db
, ev
, rec
, (struct server_id
) { 0 });
261 fprintf(stderr
, "dbwrap_watched_watch_send failed\n");
268 waited
= waitpid(child
, &wstatus
, 0);
270 fprintf(stderr
, "waitpid failed: %s\n", strerror(errno
));
273 if (!WIFEXITED(wstatus
)) {
274 fprintf(stderr
, "child did not exit normally\n");
278 exit_status
= WEXITSTATUS(wstatus
);
279 if (exit_status
!= 0) {
280 fprintf(stderr
, "exit status is %d\n", exit_status
);
284 status
= dbwrap_store_uint32_bystring(db
, keystr
, 1);
285 if (!NT_STATUS_EQUAL(status
, NT_STATUS_OK
)) {
287 "dbwrap_store_uint32 returned %s\n",
292 (void)unlink("test_watch.tdb");
302 * Test that we can't add two watchers in the same
303 * fetch_lock/do_locked round
306 struct dbwrap_watch4_state
{
308 struct tevent_context
*ev
;
309 struct db_context
*db
;
314 struct tevent_req
*req1
;
317 struct tevent_req
*req2
;
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
,
328 struct dbwrap_watch4_state
*state
= private_data
;
331 state
->req1
= dbwrap_watched_watch_send(
332 state
->mem_ctx
, state
->ev
, rec
, (struct server_id
) { .pid
=0 });
333 if (state
->req1
== NULL
) {
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));
345 state
->req2
= dbwrap_watched_watch_send(
346 state
->mem_ctx
, state
->ev
, rec
, (struct server_id
) { .pid
=0 });
347 if (state
->req2
== NULL
) {
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));
359 state
->status
= NT_STATUS_OK
;
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
);
371 printf("req1 finished: %s\n", nt_errstr(state
->status1
));
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
);
380 printf("req2 finished: %s\n", nt_errstr(state
->status2
));
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 };
397 ok
= test_dbwrap_watch_init(
398 talloc_tos(), "test_watch.tdb", &ev
, &msg
, &backend
, &db
);
403 state
= (struct dbwrap_watch4_state
) {
404 .mem_ctx
= talloc_tos(),
410 status
= dbwrap_do_locked(db
, key
, dbwrap_watch4_fn
, &state
);
411 if (!NT_STATUS_IS_OK(status
)) {
413 "dbwrap_do_locked failed: %s\n",
417 if (!NT_STATUS_IS_OK(state
.status
)) {
419 "dbwrap_watch4_fn failed: %s\n",
424 status
= dbwrap_store(db
, key
, key
, 0);
425 if (!NT_STATUS_IS_OK(status
)) {
427 "dbwrap_store failed: %s\n",
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
);
437 "tevent_loop_once failed: %s\n",
443 if (!NT_STATUS_IS_OK(state
.status1
)) {
445 "req1 returned %s\n",
446 nt_errstr(state
.status1
));
450 if (!NT_STATUS_EQUAL(state
.status2
, NT_STATUS_REQUEST_NOT_ACCEPTED
)) {
452 "req2 returned %s\n",
453 nt_errstr(state
.status2
));
457 (void)unlink("test_watch.tdb");
460 TALLOC_FREE(state
.req2
);
461 TALLOC_FREE(state
.req1
);