source: vendor/current/lib/tdb/test/run-rescue.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 2.9 KB
Line 
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"
17
18struct walk_data {
19 TDB_DATA key;
20 TDB_DATA data;
21 bool fail;
22 unsigned count;
23};
24
25static inline bool tdb_deq(TDB_DATA a, TDB_DATA b)
26{
27 return a.dsize == b.dsize && memcmp(a.dptr, b.dptr, a.dsize) == 0;
28}
29
30static inline TDB_DATA tdb_mkdata(const void *p, size_t len)
31{
32 TDB_DATA d;
33 d.dptr = discard_const_p(uint8_t, p);
34 d.dsize = len;
35 return d;
36}
37
38static void walk(TDB_DATA key, TDB_DATA data, void *_wd)
39{
40 struct walk_data *wd = _wd;
41
42 if (!tdb_deq(key, wd->key)) {
43 wd->fail = true;
44 }
45
46 if (!tdb_deq(data, wd->data)) {
47 wd->fail = true;
48 }
49 wd->count++;
50}
51
52static void count_records(TDB_DATA key, TDB_DATA data, void *_wd)
53{
54 struct walk_data *wd = _wd;
55
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++;
60}
61
62static void log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
63{
64 unsigned int *count = tdb_get_logging_private(tdb);
65 (*count)++;
66}
67
68int main(int argc, char *argv[])
69{
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 };
74
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);
78
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;
85
86 ok1(tdb_store(tdb, wd.key, wd.data, TDB_INSERT) == 0);
87
88 ok1(tdb_rescue(tdb, walk, &wd) == 0);
89 ok1(!wd.fail);
90 ok1(wd.count == 1);
91
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);
100
101 wd.count = 0;
102 if (tdb_rescue(tdb, count_records, &wd) != 0) {
103 wd.fail = true;
104 break;
105 }
106 /* Could be 0 or 1. */
107 if (wd.count > 1) {
108 wd.fail = true;
109 break;
110 }
111 if (tdb->methods->tdb_write(tdb, i, &c, 1) != 0)
112 fail("Restoring offset %i", i);
113 }
114 ok1(log_count == 0);
115 ok1(!wd.fail);
116 tdb_close(tdb);
117
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);
125
126 return exit_status();
127}
Note: See TracBrowser for help on using the repository browser.