s4:gensec/gssapi: use gensec_gssapi_max_{input,wrapped}_size() for all backends
[Samba.git] / lib / tdb / test / run-rescue.c
blobe43f53be9d88d6dc0d9e71bc4194b7e9db78bdd1
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 "../common/mutex.c"
14 #include "tap-interface.h"
15 #include <stdlib.h>
16 #include "logging.h"
18 struct walk_data {
19 TDB_DATA key;
20 TDB_DATA data;
21 bool fail;
22 unsigned count;
25 static inline bool tdb_deq(TDB_DATA a, TDB_DATA b)
27 return a.dsize == b.dsize && memcmp(a.dptr, b.dptr, a.dsize) == 0;
30 static inline TDB_DATA tdb_mkdata(const void *p, size_t len)
32 TDB_DATA d;
33 d.dptr = discard_const_p(uint8_t, p);
34 d.dsize = len;
35 return d;
38 static void walk(TDB_DATA key, TDB_DATA data, void *_wd)
40 struct walk_data *wd = _wd;
42 if (!tdb_deq(key, wd->key)) {
43 wd->fail = true;
46 if (!tdb_deq(data, wd->data)) {
47 wd->fail = true;
49 wd->count++;
52 static void count_records(TDB_DATA key, TDB_DATA data, void *_wd)
54 struct walk_data *wd = _wd;
56 if (!tdb_deq(key, wd->key) || !tdb_deq(data, wd->data))
57 diag("%.*s::%.*s",
58 (int)key.dsize, key.dptr, (int)data.dsize, data.dptr);
59 wd->count++;
62 static void log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
64 unsigned int *count = tdb_get_logging_private(tdb);
65 (*count)++;
68 int main(int argc, char *argv[])
70 struct tdb_context *tdb;
71 struct walk_data wd;
72 unsigned int i, size, log_count = 0;
73 struct tdb_logging_context log_ctx = { log_fn, &log_count };
75 plan_tests(8);
76 tdb = tdb_open_ex("run-rescue.tdb", 1, TDB_CLEAR_IF_FIRST,
77 O_CREAT|O_TRUNC|O_RDWR, 0600, &log_ctx, NULL);
79 wd.key.dsize = strlen("hi");
80 wd.key.dptr = discard_const_p(uint8_t, "hi");
81 wd.data.dsize = strlen("world");
82 wd.data.dptr = discard_const_p(uint8_t, "world");
83 wd.count = 0;
84 wd.fail = false;
86 ok1(tdb_store(tdb, wd.key, wd.data, TDB_INSERT) == 0);
88 ok1(tdb_rescue(tdb, walk, &wd) == 0);
89 ok1(!wd.fail);
90 ok1(wd.count == 1);
92 /* Corrupt the database, walk should either get it or not. */
93 size = tdb->map_size;
94 for (i = sizeof(struct tdb_header); i < size; i++) {
95 char c;
96 if (tdb->methods->tdb_read(tdb, i, &c, 1, false) != 0)
97 fail("Reading offset %i", i);
98 if (tdb->methods->tdb_write(tdb, i, "X", 1) != 0)
99 fail("Writing X at offset %i", i);
101 wd.count = 0;
102 if (tdb_rescue(tdb, count_records, &wd) != 0) {
103 wd.fail = true;
104 break;
106 /* Could be 0 or 1. */
107 if (wd.count > 1) {
108 wd.fail = true;
109 break;
111 if (tdb->methods->tdb_write(tdb, i, &c, 1) != 0)
112 fail("Restoring offset %i", i);
114 ok1(log_count == 0);
115 ok1(!wd.fail);
116 tdb_close(tdb);
118 /* Now try our known-corrupt db. */
119 tdb = tdb_open_ex("test/tdb.corrupt", 1024, 0, O_RDWR, 0,
120 &taplogctx, NULL);
121 wd.count = 0;
122 ok1(tdb_rescue(tdb, count_records, &wd) == 0);
123 ok1(wd.count == 1627);
124 tdb_close(tdb);
126 return exit_status();