g_lock: Use dbwrap_do_locked for g_lock_lock
[Samba.git] / source3 / torture / test_dbwrap_watch.c
blob5aa0430b1111a22a4a39c24c05520d4f47faa386
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 *backend = NULL;
33 struct db_context *db = NULL;
34 const char *keystr = "key";
35 TDB_DATA key = string_term_tdb_data(keystr);
36 struct db_record *rec = NULL;
37 struct tevent_req *req = NULL;
38 NTSTATUS status;
39 bool ret = false;
41 ev = samba_tevent_context_init(talloc_tos());
42 if (ev == NULL) {
43 fprintf(stderr, "tevent_context_init failed\n");
44 goto fail;
46 msg = messaging_init(ev, ev);
47 if (msg == NULL) {
48 fprintf(stderr, "messaging_init failed\n");
49 goto fail;
51 backend = db_open(msg, "test_watch.tdb", 0, TDB_CLEAR_IF_FIRST,
52 O_CREAT|O_RDWR, 0644, DBWRAP_LOCK_ORDER_1,
53 DBWRAP_FLAG_NONE);
54 if (backend == NULL) {
55 fprintf(stderr, "db_open failed: %s\n", strerror(errno));
56 goto fail;
59 db = db_open_watched(ev, backend, msg);
61 rec = dbwrap_fetch_locked(db, db, key);
62 if (rec == NULL) {
63 fprintf(stderr, "dbwrap_fetch_locked failed\n");
64 goto fail;
66 req = dbwrap_watched_watch_send(talloc_tos(), ev, rec,
67 (struct server_id){0});
68 if (req == NULL) {
69 fprintf(stderr, "dbwrap_record_watch_send failed\n");
70 goto fail;
72 TALLOC_FREE(rec);
74 status = dbwrap_store_int32_bystring(db, "different_key", 1);
75 if (!NT_STATUS_IS_OK(status)) {
76 fprintf(stderr, "dbwrap_store_int32 failed: %s\n",
77 nt_errstr(status));
78 goto fail;
81 status = dbwrap_store_int32_bystring(db, keystr, 1);
82 if (!NT_STATUS_IS_OK(status)) {
83 fprintf(stderr, "dbwrap_store_int32 failed: %s\n",
84 nt_errstr(status));
85 goto fail;
88 if (!tevent_req_poll(req, ev)) {
89 fprintf(stderr, "tevent_req_poll failed\n");
90 goto fail;
93 status = dbwrap_watched_watch_recv(req, talloc_tos(), &rec, NULL,
94 NULL);
95 if (!NT_STATUS_IS_OK(status)) {
96 fprintf(stderr, "dbwrap_record_watch_recv failed: %s\n",
97 nt_errstr(status));
98 goto fail;
101 (void)unlink("test_watch.tdb");
102 ret = true;
103 fail:
104 TALLOC_FREE(req);
105 TALLOC_FREE(rec);
106 TALLOC_FREE(db);
107 TALLOC_FREE(msg);
108 TALLOC_FREE(ev);
109 return ret;
113 * Make sure dbwrap_parse_record does not return NT_STATUS_OK on
114 * invalid data
117 bool run_dbwrap_watch2(int dummy)
119 struct tevent_context *ev = NULL;
120 struct messaging_context *msg = NULL;
121 struct db_context *backend = NULL;
122 struct db_context *db = NULL;
123 const char *keystr = "key";
124 TDB_DATA key = string_term_tdb_data(keystr);
125 NTSTATUS status;
126 bool ret = false;
128 ev = samba_tevent_context_init(talloc_tos());
129 if (ev == NULL) {
130 fprintf(stderr, "tevent_context_init failed\n");
131 goto fail;
133 msg = messaging_init(ev, ev);
134 if (msg == NULL) {
135 fprintf(stderr, "messaging_init failed\n");
136 goto fail;
138 backend = db_open(msg, "test_watch.tdb", 0, TDB_CLEAR_IF_FIRST,
139 O_CREAT|O_RDWR, 0644, DBWRAP_LOCK_ORDER_1,
140 DBWRAP_FLAG_NONE);
141 if (backend == NULL) {
142 fprintf(stderr, "db_open failed: %s\n", strerror(errno));
143 goto fail;
147 * Store invalid data (from the dbwrap_watch point of view)
148 * directly into the backend database
150 status = dbwrap_store_uint32_bystring(backend, keystr, UINT32_MAX);
151 if (!NT_STATUS_IS_OK(status)) {
152 fprintf(stderr, "dbwrap_store_uint32_bystring failed: %s\n",
153 nt_errstr(status));
154 goto fail;
157 db = db_open_watched(ev, backend, msg);
158 if (db == NULL) {
159 fprintf(stderr, "db_open_watched failed\n");
160 goto fail;
162 backend = NULL; /* open_watch talloc_moves backend */
164 status = dbwrap_parse_record(db, key, NULL, NULL);
165 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
166 fprintf(stderr, "dbwrap_parse_record returned %s, expected "
167 "NT_STATUS_NOT_FOUND\n", nt_errstr(status));
168 goto fail;
171 (void)unlink("test_watch.tdb");
172 ret = true;
173 fail:
174 TALLOC_FREE(db);
175 TALLOC_FREE(msg);
176 TALLOC_FREE(ev);
177 return ret;