2 * Unix SMB/CIFS implementation.
3 * Test dbwrap_watch API
4 * Copyright (C) Volker Lendecke 2017
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"
27 #include "source3/include/util_tdb.h"
28 #include "lib/global_contexts.h"
30 struct do_locked1_state
{
35 static void do_locked1_cb(
36 struct db_record
*rec
,
40 struct do_locked1_state
*state
=
41 (struct do_locked1_state
*)private_data
;
43 state
->status
= dbwrap_record_store(rec
, state
->value
, 0);
46 static void do_locked1_check(TDB_DATA key
, TDB_DATA value
,
49 struct do_locked1_state
*state
=
50 (struct do_locked1_state
*)private_data
;
53 ret
= tdb_data_cmp(value
, state
->value
);
55 state
->status
= NT_STATUS_DATA_ERROR
;
59 state
->status
= NT_STATUS_OK
;
62 static void do_locked1_del(
63 struct db_record
*rec
,
67 struct do_locked1_state
*state
=
68 (struct do_locked1_state
*)private_data
;
70 state
->status
= dbwrap_record_delete(rec
);
73 bool run_dbwrap_do_locked1(int dummy
)
75 struct tevent_context
*ev
;
76 struct messaging_context
*msg
;
77 struct db_context
*backend
;
78 struct db_context
*db
;
79 const char *dbname
= "test_do_locked.tdb";
80 const char *keystr
= "key";
81 TDB_DATA key
= string_term_tdb_data(keystr
);
82 const char *valuestr
= "value";
83 TDB_DATA value
= string_term_tdb_data(valuestr
);
84 struct do_locked1_state state
= { .value
= value
};
88 ev
= global_event_context();
90 fprintf(stderr
, "global_event_context() failed\n");
93 msg
= global_messaging_context();
95 fprintf(stderr
, "global_messaging_context() failed\n");
99 backend
= db_open(talloc_tos(), dbname
, 0,
100 TDB_CLEAR_IF_FIRST
, O_CREAT
|O_RDWR
, 0644,
101 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
102 if (backend
== NULL
) {
103 fprintf(stderr
, "db_open failed: %s\n", strerror(errno
));
107 db
= db_open_watched(talloc_tos(), &backend
, msg
);
109 fprintf(stderr
, "db_open_watched failed: %s\n",
114 status
= dbwrap_do_locked(db
, key
, do_locked1_cb
, &state
);
115 if (!NT_STATUS_IS_OK(status
)) {
116 fprintf(stderr
, "dbwrap_do_locked failed: %s\n",
120 if (!NT_STATUS_IS_OK(state
.status
)) {
121 fprintf(stderr
, "store returned %s\n",
122 nt_errstr(state
.status
));
126 status
= dbwrap_parse_record(db
, key
, do_locked1_check
, &state
);
127 if (!NT_STATUS_IS_OK(status
)) {
128 fprintf(stderr
, "dbwrap_parse_record failed: %s\n",
132 if (!NT_STATUS_IS_OK(state
.status
)) {
133 fprintf(stderr
, "data compare returned %s\n",
138 status
= dbwrap_do_locked(db
, key
, do_locked1_del
, &state
);
139 if (!NT_STATUS_IS_OK(status
)) {
140 fprintf(stderr
, "dbwrap_do_locked failed: %s\n",
144 if (!NT_STATUS_IS_OK(state
.status
)) {
145 fprintf(stderr
, "delete returned %s\n", nt_errstr(status
));
149 status
= dbwrap_parse_record(db
, key
, do_locked1_check
, &state
);
150 if (!NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
151 fprintf(stderr
, "parse_record returned %s, "
152 "expected NOT_FOUND\n", nt_errstr(status
));