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"
29 struct do_locked1_state
{
34 static void do_locked1_cb(struct db_record
*rec
, void *private_data
)
36 struct do_locked1_state
*state
=
37 (struct do_locked1_state
*)private_data
;
39 state
->status
= dbwrap_record_store(rec
, state
->value
, 0);
42 static void do_locked1_check(TDB_DATA key
, TDB_DATA value
,
45 struct do_locked1_state
*state
=
46 (struct do_locked1_state
*)private_data
;
49 ret
= tdb_data_cmp(value
, state
->value
);
51 state
->status
= NT_STATUS_DATA_ERROR
;
55 state
->status
= NT_STATUS_OK
;
58 static void do_locked1_del(struct db_record
*rec
, void *private_data
)
60 struct do_locked1_state
*state
=
61 (struct do_locked1_state
*)private_data
;
63 state
->status
= dbwrap_record_delete(rec
);
66 bool run_dbwrap_do_locked1(int dummy
)
68 struct tevent_context
*ev
;
69 struct messaging_context
*msg
;
70 struct db_context
*backend
;
71 struct db_context
*db
;
72 const char *dbname
= "test_do_locked.tdb";
73 const char *keystr
= "key";
74 TDB_DATA key
= string_term_tdb_data(keystr
);
75 const char *valuestr
= "value";
76 TDB_DATA value
= string_term_tdb_data(valuestr
);
77 struct do_locked1_state state
= { .value
= value
};
81 ev
= server_event_context();
83 fprintf(stderr
, "server_event_context() failed\n");
86 msg
= server_messaging_context();
88 fprintf(stderr
, "server_messaging_context() failed\n");
92 backend
= db_open(talloc_tos(), dbname
, 0,
93 TDB_CLEAR_IF_FIRST
, O_CREAT
|O_RDWR
, 0644,
94 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
95 if (backend
== NULL
) {
96 fprintf(stderr
, "db_open failed: %s\n", strerror(errno
));
100 db
= db_open_watched(talloc_tos(), backend
, msg
);
102 fprintf(stderr
, "db_open_watched failed: %s\n",
107 status
= dbwrap_do_locked(db
, key
, do_locked1_cb
, &state
);
108 if (!NT_STATUS_IS_OK(status
)) {
109 fprintf(stderr
, "dbwrap_do_locked failed: %s\n",
113 if (!NT_STATUS_IS_OK(state
.status
)) {
114 fprintf(stderr
, "store returned %s\n",
115 nt_errstr(state
.status
));
119 status
= dbwrap_parse_record(db
, key
, do_locked1_check
, &state
);
120 if (!NT_STATUS_IS_OK(status
)) {
121 fprintf(stderr
, "dbwrap_parse_record failed: %s\n",
125 if (!NT_STATUS_IS_OK(state
.status
)) {
126 fprintf(stderr
, "data compare returned %s\n",
131 status
= dbwrap_do_locked(db
, key
, do_locked1_del
, &state
);
132 if (!NT_STATUS_IS_OK(status
)) {
133 fprintf(stderr
, "dbwrap_do_locked failed: %s\n",
137 if (!NT_STATUS_IS_OK(state
.status
)) {
138 fprintf(stderr
, "delete returned %s\n", nt_errstr(status
));
142 status
= dbwrap_parse_record(db
, key
, do_locked1_check
, &state
);
143 if (!NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
144 fprintf(stderr
, "parse_record returned %s, "
145 "expected NOT_FOUND\n", nt_errstr(status
));