Line data Source code
1 : /*
2 : * Unix SMB/CIFS implementation.
3 : * Test dbwrap_watch API
4 : * Copyright (C) Volker Lendecke 2017
5 : *
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.
10 : *
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.
15 : *
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/>.
18 : */
19 :
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"
27 : #include "source3/include/util_tdb.h"
28 : #include "lib/global_contexts.h"
29 :
30 : struct do_locked1_state {
31 : TDB_DATA value;
32 : NTSTATUS status;
33 : };
34 :
35 1 : static void do_locked1_cb(
36 : struct db_record *rec,
37 : TDB_DATA value,
38 : void *private_data)
39 : {
40 1 : struct do_locked1_state *state =
41 : (struct do_locked1_state *)private_data;
42 :
43 1 : state->status = dbwrap_record_store(rec, state->value, 0);
44 1 : }
45 :
46 1 : static void do_locked1_check(TDB_DATA key, TDB_DATA value,
47 : void *private_data)
48 : {
49 1 : struct do_locked1_state *state =
50 : (struct do_locked1_state *)private_data;
51 1 : int ret;
52 :
53 1 : ret = tdb_data_cmp(value, state->value);
54 1 : if (ret != 0) {
55 0 : state->status = NT_STATUS_DATA_ERROR;
56 0 : return;
57 : }
58 :
59 1 : state->status = NT_STATUS_OK;
60 : }
61 :
62 1 : static void do_locked1_del(
63 : struct db_record *rec,
64 : TDB_DATA value,
65 : void *private_data)
66 : {
67 1 : struct do_locked1_state *state =
68 : (struct do_locked1_state *)private_data;
69 :
70 1 : state->status = dbwrap_record_delete(rec);
71 1 : }
72 :
73 1 : bool run_dbwrap_do_locked1(int dummy)
74 : {
75 1 : struct tevent_context *ev;
76 1 : struct messaging_context *msg;
77 1 : struct db_context *backend;
78 1 : struct db_context *db;
79 1 : const char *dbname = "test_do_locked.tdb";
80 1 : const char *keystr = "key";
81 1 : TDB_DATA key = string_term_tdb_data(keystr);
82 1 : const char *valuestr = "value";
83 1 : TDB_DATA value = string_term_tdb_data(valuestr);
84 1 : struct do_locked1_state state = { .value = value };
85 1 : int ret = false;
86 1 : NTSTATUS status;
87 :
88 1 : ev = global_event_context();
89 1 : if (ev == NULL) {
90 0 : fprintf(stderr, "global_event_context() failed\n");
91 0 : return false;
92 : }
93 1 : msg = global_messaging_context();
94 1 : if (msg == NULL) {
95 0 : fprintf(stderr, "global_messaging_context() failed\n");
96 0 : return false;
97 : }
98 :
99 1 : 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 1 : if (backend == NULL) {
103 0 : fprintf(stderr, "db_open failed: %s\n", strerror(errno));
104 0 : return false;
105 : }
106 :
107 1 : db = db_open_watched(talloc_tos(), &backend, msg);
108 1 : if (db == NULL) {
109 0 : fprintf(stderr, "db_open_watched failed: %s\n",
110 0 : strerror(errno));
111 0 : return false;
112 : }
113 :
114 1 : status = dbwrap_do_locked(db, key, do_locked1_cb, &state);
115 1 : if (!NT_STATUS_IS_OK(status)) {
116 0 : fprintf(stderr, "dbwrap_do_locked failed: %s\n",
117 : nt_errstr(status));
118 0 : goto fail;
119 : }
120 1 : if (!NT_STATUS_IS_OK(state.status)) {
121 0 : fprintf(stderr, "store returned %s\n",
122 : nt_errstr(state.status));
123 0 : goto fail;
124 : }
125 :
126 1 : status = dbwrap_parse_record(db, key, do_locked1_check, &state);
127 1 : if (!NT_STATUS_IS_OK(status)) {
128 0 : fprintf(stderr, "dbwrap_parse_record failed: %s\n",
129 : nt_errstr(status));
130 0 : goto fail;
131 : }
132 1 : if (!NT_STATUS_IS_OK(state.status)) {
133 0 : fprintf(stderr, "data compare returned %s\n",
134 : nt_errstr(status));
135 0 : goto fail;
136 : }
137 :
138 1 : status = dbwrap_do_locked(db, key, do_locked1_del, &state);
139 1 : if (!NT_STATUS_IS_OK(status)) {
140 0 : fprintf(stderr, "dbwrap_do_locked failed: %s\n",
141 : nt_errstr(status));
142 0 : goto fail;
143 : }
144 1 : if (!NT_STATUS_IS_OK(state.status)) {
145 0 : fprintf(stderr, "delete returned %s\n", nt_errstr(status));
146 0 : goto fail;
147 : }
148 :
149 1 : status = dbwrap_parse_record(db, key, do_locked1_check, &state);
150 1 : if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
151 0 : fprintf(stderr, "parse_record returned %s, "
152 : "expected NOT_FOUND\n", nt_errstr(status));
153 0 : goto fail;
154 : }
155 :
156 0 : ret = true;
157 1 : fail:
158 1 : TALLOC_FREE(db);
159 1 : unlink(dbname);
160 1 : return ret;
161 : }
|