s3-libsmb: Don't leak memory on error.
[Samba.git] / lib / tdb / test / run-rescue.c
bloba26c493972ab85a2d20d7c5eb274dda92a4756ae
1 #include "../common/tdb_private.h"
2 #include "../common/io.c"
3 #include "../common/tdb.c"
4 #include "../common/lock.c"
5 #include "../common/freelist.c"
6 #include "../common/traverse.c"
7 #include "../common/transaction.c"
8 #include "../common/error.c"
9 #include "../common/open.c"
10 #include "../common/check.c"
11 #include "../common/hash.c"
12 #include "../common/rescue.c"
13 #include "tap-interface.h"
14 #include <stdlib.h>
15 #include "logging.h"
17 struct walk_data {
18 TDB_DATA key;
19 TDB_DATA data;
20 bool fail;
21 unsigned count;
24 static inline bool tdb_deq(TDB_DATA a, TDB_DATA b)
26 return a.dsize == b.dsize && memcmp(a.dptr, b.dptr, a.dsize) == 0;
29 static inline TDB_DATA tdb_mkdata(const void *p, size_t len)
31 TDB_DATA d;
32 d.dptr = (void *)p;
33 d.dsize = len;
34 return d;
37 static void walk(TDB_DATA key, TDB_DATA data, void *_wd)
39 struct walk_data *wd = _wd;
41 if (!tdb_deq(key, wd->key)) {
42 wd->fail = true;
45 if (!tdb_deq(data, wd->data)) {
46 wd->fail = true;
48 wd->count++;
51 static void count_records(TDB_DATA key, TDB_DATA data, void *_wd)
53 struct walk_data *wd = _wd;
55 if (!tdb_deq(key, wd->key) || !tdb_deq(data, wd->data))
56 diag("%.*s::%.*s\n",
57 (int)key.dsize, key.dptr, (int)data.dsize, data.dptr);
58 wd->count++;
61 static void log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
63 unsigned int *count = tdb_get_logging_private(tdb);
64 (*count)++;
67 int main(int argc, char *argv[])
69 struct tdb_context *tdb;
70 struct walk_data wd;
71 unsigned int i, size, log_count = 0;
72 struct tdb_logging_context log_ctx = { log_fn, &log_count };
74 plan_tests(8);
75 tdb = tdb_open_ex("run-rescue.tdb", 1, TDB_CLEAR_IF_FIRST,
76 O_CREAT|O_TRUNC|O_RDWR, 0600, &log_ctx, NULL);
78 wd.key.dsize = strlen("hi");
79 wd.key.dptr = (void *)"hi";
80 wd.data.dsize = strlen("world");
81 wd.data.dptr = (void *)"world";
82 wd.count = 0;
83 wd.fail = false;
85 ok1(tdb_store(tdb, wd.key, wd.data, TDB_INSERT) == 0);
87 ok1(tdb_rescue(tdb, walk, &wd) == 0);
88 ok1(!wd.fail);
89 ok1(wd.count == 1);
91 /* Corrupt the database, walk should either get it or not. */
92 size = tdb->map_size;
93 for (i = sizeof(struct tdb_header); i < size; i++) {
94 char c;
95 if (tdb->methods->tdb_read(tdb, i, &c, 1, false) != 0)
96 fail("Reading offset %i", i);
97 if (tdb->methods->tdb_write(tdb, i, "X", 1) != 0)
98 fail("Writing X at offset %i", i);
100 wd.count = 0;
101 if (tdb_rescue(tdb, count_records, &wd) != 0) {
102 wd.fail = true;
103 break;
105 /* Could be 0 or 1. */
106 if (wd.count > 1) {
107 wd.fail = true;
108 break;
110 if (tdb->methods->tdb_write(tdb, i, &c, 1) != 0)
111 fail("Restoring offset %i", i);
113 ok1(log_count == 0);
114 ok1(!wd.fail);
115 tdb_close(tdb);
117 /* Now try our known-corrupt db. */
118 tdb = tdb_open_ex("test/tdb.corrupt", 1024, 0, O_RDWR, 0,
119 &taplogctx, NULL);
120 wd.count = 0;
121 ok1(tdb_rescue(tdb, count_records, &wd) == 0);
122 ok1(wd.count == 1627);
123 tdb_close(tdb);
125 return exit_status();