torture3: Add a bit more coverage to messaging_read
[Samba.git] / source3 / torture / test_dbwrap_watch.c
blobab9330fffe985cd88ee84a3719e2a56d7ae6dc82
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 bool run_dbwrap_watch1(int dummy)
30 struct tevent_context *ev = NULL;
31 struct messaging_context *msg = NULL;
32 struct db_context *db = NULL;
33 const char *keystr = "key";
34 TDB_DATA key = string_term_tdb_data(keystr);
35 struct db_record *rec = NULL;
36 struct tevent_req *req = NULL;
37 NTSTATUS status;
38 bool ret = false;
40 ev = samba_tevent_context_init(talloc_tos());
41 if (ev == NULL) {
42 fprintf(stderr, "tevent_context_init failed\n");
43 goto fail;
45 msg = messaging_init(ev, ev);
46 if (msg == NULL) {
47 fprintf(stderr, "messaging_init failed\n");
48 goto fail;
50 db = db_open(msg, "test_watch.tdb", 0, TDB_DEFAULT,
51 O_CREAT|O_RDWR, 0644, DBWRAP_LOCK_ORDER_1,
52 DBWRAP_FLAG_NONE);
53 if (db == NULL) {
54 fprintf(stderr, "db_open failed: %s\n", strerror(errno));
55 goto fail;
57 dbwrap_watch_db(db, msg);
58 rec = dbwrap_fetch_locked(db, db, key);
59 if (rec == NULL) {
60 fprintf(stderr, "dbwrap_fetch_locked failed\n");
61 goto fail;
63 req = dbwrap_record_watch_send(talloc_tos(), ev, rec, msg);
64 if (req == NULL) {
65 fprintf(stderr, "dbwrap_record_watch_send failed\n");
66 goto fail;
68 TALLOC_FREE(rec);
70 status = dbwrap_store_int32_bystring(db, "different_key", 1);
71 if (!NT_STATUS_IS_OK(status)) {
72 fprintf(stderr, "dbwrap_store_int32 failed: %s\n",
73 nt_errstr(status));
74 goto fail;
77 status = dbwrap_store_int32_bystring(db, keystr, 1);
78 if (!NT_STATUS_IS_OK(status)) {
79 fprintf(stderr, "dbwrap_store_int32 failed: %s\n",
80 nt_errstr(status));
81 goto fail;
84 if (!tevent_req_poll(req, ev)) {
85 fprintf(stderr, "tevent_req_poll failed\n");
86 goto fail;
89 status = dbwrap_record_watch_recv(req, talloc_tos(), &rec);
90 if (!NT_STATUS_IS_OK(status)) {
91 fprintf(stderr, "dbwrap_record_watch_recv failed: %s\n",
92 nt_errstr(status));
93 goto fail;
96 ret = true;
97 fail:
98 TALLOC_FREE(req);
99 TALLOC_FREE(rec);
100 TALLOC_FREE(db);
101 TALLOC_FREE(msg);
102 TALLOC_FREE(ev);
103 return ret;