s3:pylibsmb: Make .list() work for SMBv2
[samba.git] / source3 / utils / dbwrap_torture.c
1 /*
2    Samba Linux/Unix CIFS implementation
3
4    simple tool to test persistent databases
5
6    Copyright (C) Michael Adam     2009
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "popt_common.h"
25 #include "dbwrap/dbwrap.h"
26 #include "dbwrap/dbwrap_open.h"
27 #include "messages.h"
28 #include "lib/util/util_tdb.h"
29
30 #if 0
31 #include "lib/events/events.h"
32 #include "system/filesys.h"
33 #include "popt.h"
34 #include "cmdline.h"
35
36 #include <sys/time.h>
37 #include <time.h>
38 #endif
39
40 #define DEFAULT_DB_NAME "transaction.tdb"
41
42 static int timelimit = 10;
43 static int torture_delay = 0;
44 static int verbose = 0;
45 static int no_trans = 0;
46 static const char *db_name = DEFAULT_DB_NAME;
47
48
49 static unsigned int pnn;
50
51 static TDB_DATA old_data;
52
53 static int success = true;
54
55 static void print_counters(void)
56 {
57         int i;
58         uint32_t *old_counters;
59
60         printf("[%4u] Counters: ", (unsigned int)getpid());
61         old_counters = (uint32_t *)old_data.dptr;
62         for (i=0; i < old_data.dsize/sizeof(uint32_t); i++) {
63                 printf("%6u ", old_counters[i]);
64         }
65         printf("\n");
66 }
67
68 static void each_second(struct tevent_context *ev,
69                         struct tevent_timer *te,
70                         struct timeval t,
71                         void *private_data)
72 {
73         struct db_context *db = talloc_get_type(private_data, struct db_context);
74
75         print_counters();
76
77         tevent_add_timer(ev, db, timeval_current_ofs(1, 0), each_second, db);
78 }
79
80 static bool check_counters(struct db_context *db, TDB_DATA data)
81 {
82         int i;
83         uint32_t *counters, *old_counters;
84
85         counters     = (uint32_t *)data.dptr;
86         old_counters = (uint32_t *)old_data.dptr;
87
88         /* check that all the counters are monotonic increasing */
89         for (i=0; i < old_data.dsize/sizeof(uint32_t); i++) {
90                 if (counters[i] < old_counters[i]) {
91                         printf("[%4u] ERROR: counters has decreased for node %u  From %u to %u\n",
92                                (unsigned int)getpid(), i, old_counters[i], counters[i]);
93                         success = false;
94                         return false;
95                 }
96         }
97
98         if (old_data.dsize != data.dsize) {
99                 old_data.dsize = data.dsize;
100                 old_data.dptr = (unsigned char*)talloc_realloc_size(db, old_data.dptr, old_data.dsize);
101         }
102
103         memcpy(old_data.dptr, data.dptr, data.dsize);
104         if (verbose) print_counters();
105
106         return true;
107 }
108
109
110 static void do_sleep(unsigned int sec)
111 {
112         unsigned int i;
113
114         if (sec == 0) {
115                 return;
116         }
117
118         for (i=0; i<sec; i++) {
119                 if (verbose) printf(".");
120                 sleep(1);
121         }
122
123         if (verbose) printf("\n");
124 }
125
126 static void test_store_records(struct db_context *db, struct tevent_context *ev)
127 {
128         TDB_DATA key;
129         uint32_t *counters;
130         TALLOC_CTX *tmp_ctx = talloc_stackframe();
131         struct timeval start;
132
133         key = string_term_tdb_data("testkey");
134
135         start = timeval_current();
136         while ((timelimit == 0) || (timeval_elapsed(&start) < timelimit)) {
137                 struct db_record *rec;
138                 TDB_DATA data;
139                 TDB_DATA value;
140                 int ret;
141                 NTSTATUS status;
142
143                 if (!no_trans) {
144                         if (verbose) DEBUG(1, ("starting transaction\n"));
145                         ret = dbwrap_transaction_start(db);
146                         if (ret != 0) {
147                                 DEBUG(0, ("Failed to start transaction on node "
148                                           "%d\n", pnn));
149                                 goto fail;
150                         }
151                         if (verbose) DEBUG(1, ("transaction started\n"));
152                         do_sleep(torture_delay);
153                 }
154
155                 if (verbose) DEBUG(1, ("calling fetch_lock\n"));
156                 rec = dbwrap_fetch_locked(db, tmp_ctx, key);
157                 if (rec == NULL) {
158                         DEBUG(0, ("Failed to fetch record\n"));
159                         goto fail;
160                 }
161                 if (verbose) DEBUG(1, ("fetched record ok\n"));
162                 do_sleep(torture_delay);
163                 value = dbwrap_record_get_value(rec);
164
165                 data.dsize = MAX(value.dsize, sizeof(uint32_t) * (pnn+1));
166                 data.dptr = (unsigned char *)talloc_zero_size(tmp_ctx,
167                                                               data.dsize);
168                 if (data.dptr == NULL) {
169                         DEBUG(0, ("Failed to allocate data\n"));
170                         goto fail;
171                 }
172                 memcpy(data.dptr, value.dptr, value.dsize);
173
174                 counters = (uint32_t *)data.dptr;
175
176                 /* bump our counter */
177                 counters[pnn]++;
178
179                 if (verbose) DEBUG(1, ("storing data\n"));
180                 status = dbwrap_record_store(rec, data, TDB_REPLACE);
181                 if (!NT_STATUS_IS_OK(status)) {
182                         DEBUG(0, ("Failed to store record\n"));
183                         if (!no_trans) {
184                                 ret = dbwrap_transaction_cancel(db);
185                                 if (ret != 0) {
186                                         DEBUG(0, ("Error cancelling transaction.\n"));
187                                 }
188                         }
189                         goto fail;
190                 }
191                 talloc_free(rec);
192                 if (verbose) DEBUG(1, ("stored data ok\n"));
193                 do_sleep(torture_delay);
194
195                 if (!no_trans) {
196                         if (verbose) DEBUG(1, ("calling transaction_commit\n"));
197                         ret = dbwrap_transaction_commit(db);
198                         if (ret != 0) {
199                                 DEBUG(0, ("Failed to commit transaction\n"));
200                                 goto fail;
201                         }
202                         if (verbose) DEBUG(1, ("transaction committed\n"));
203                 }
204
205                 /* store the counters and verify that they are sane */
206                 if (verbose || (pnn == 0)) {
207                         if (!check_counters(db, data)) {
208                                 goto fail;
209                         }
210                 }
211                 talloc_free(data.dptr);
212
213                 do_sleep(torture_delay);
214         }
215
216         goto done;
217
218 fail:
219         success = false;
220
221 done:
222         talloc_free(tmp_ctx);
223         return;
224 }
225
226 /*
227   main program
228 */
229 int main(int argc, const char *argv[])
230 {
231         TALLOC_CTX *mem_ctx;
232         struct tevent_context *ev_ctx;
233         struct messaging_context *msg_ctx;
234         struct db_context *db;
235
236         int unsafe_writes = 0;
237         struct poptOption popt_options[] = {
238                 POPT_AUTOHELP
239                 POPT_COMMON_SAMBA
240                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "INTEGER" },
241                 { "delay", 'D', POPT_ARG_INT, &torture_delay, 0, "delay (in seconds) between operations", "INTEGER" },
242                 { "verbose", 'v', POPT_ARG_NONE,  &verbose, 0, "switch on verbose mode", NULL },
243                 { "db-name", 'N', POPT_ARG_STRING, &db_name, 0, "name of the test db", "NAME" },
244                 { "no-trans", 'n', POPT_ARG_NONE, &no_trans, 0, "use fetch_lock/record store instead of transactions", NULL },
245                 { "unsafe-writes", 'u', POPT_ARG_NONE, &unsafe_writes, 0, "do not use tdb transactions when writing", NULL },
246                 POPT_TABLEEND
247         };
248         int opt;
249         const char **extra_argv;
250         int extra_argc = 0;
251         poptContext pc;
252         int tdb_flags;
253
254         int ret = 1;
255
256         mem_ctx = talloc_stackframe();
257
258         if (verbose) {
259                 setbuf(stdout, (char *)NULL); /* don't buffer */
260         } else {
261                 setlinebuf(stdout);
262         }
263
264         smb_init_locale();
265
266         setup_logging(argv[0], DEBUG_STDERR);
267         lp_set_cmdline("log level", "0");
268
269         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
270
271         while ((opt = poptGetNextOpt(pc)) != -1) {
272                 switch (opt) {
273                 default:
274                         fprintf(stderr, "Invalid option %s: %s\n",
275                                 poptBadOption(pc, 0), poptStrerror(opt));
276                         goto done;
277                 }
278         }
279
280         /* setup the remaining options for the main program to use */
281         extra_argv = poptGetArgs(pc);
282         if (extra_argv) {
283                 extra_argv++;
284                 while (extra_argv[extra_argc]) extra_argc++;
285         }
286
287         lp_load_global(get_dyn_CONFIGFILE());
288
289         ev_ctx = samba_tevent_context_init(mem_ctx);
290         if (ev_ctx == NULL) {
291                 d_fprintf(stderr, "ERROR: could not init event context\n");
292                 goto done;
293         }
294
295         msg_ctx = messaging_init(mem_ctx, ev_ctx);
296         if (msg_ctx == NULL) {
297                 d_fprintf(stderr, "ERROR: could not init messaging context\n");
298                 goto done;
299         }
300
301         if (unsafe_writes == 1) {
302                 tdb_flags = TDB_NOSYNC;
303         } else {
304                 tdb_flags = TDB_DEFAULT;
305         }
306
307         if (no_trans) {
308                 tdb_flags |= TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH;
309         }
310
311         db = db_open(mem_ctx, db_name, 0, tdb_flags,  O_RDWR | O_CREAT, 0644,
312                      DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
313
314         if (db == NULL) {
315                 d_fprintf(stderr, "failed to open db '%s': %s\n", db_name,
316                           strerror(errno));
317                 goto done;
318         }
319
320         if (get_my_vnn() == NONCLUSTER_VNN) {
321                 set_my_vnn(0);
322         }
323         pnn = get_my_vnn();
324
325         printf("Starting test on node %u. running for %u seconds. "
326                "sleep delay: %u seconds.\n", pnn, timelimit, torture_delay);
327
328         if (!verbose && (pnn == 0)) {
329                 tevent_add_timer(ev_ctx, db, timeval_current_ofs(1, 0), each_second, db);
330         }
331
332         test_store_records(db, ev_ctx);
333
334         if (verbose || (pnn == 0)) {
335                 if (success != true) {
336                         printf("The test FAILED\n");
337                         ret = 2;
338                 } else {
339                         printf("SUCCESS!\n");
340                         ret = 0;
341                 }
342         }
343
344 done:
345         talloc_free(mem_ctx);
346         return ret;
347 }