smbd: Remove a confusing comment
[Samba.git] / lib / tdb / test / run-corrupt.c
blobe6fc751842f91d5799feb23f4790d9c92f245f58
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/mutex.c"
13 #include "tap-interface.h"
14 #include <stdlib.h>
15 #include "logging.h"
17 static int check(TDB_DATA key, TDB_DATA data, void *private)
19 unsigned int *sizes = private;
21 if (key.dsize > strlen("hello"))
22 return -1;
23 if (memcmp(key.dptr, "hello", key.dsize) != 0)
24 return -1;
26 if (data.dsize != strlen("world"))
27 return -1;
28 if (memcmp(data.dptr, "world", data.dsize) != 0)
29 return -1;
31 sizes[0] += key.dsize;
32 sizes[1] += data.dsize;
33 return 0;
36 static void tdb_flip_bit(struct tdb_context *tdb, unsigned int bit)
38 unsigned int off = bit / CHAR_BIT;
39 unsigned char mask = (1 << (bit % CHAR_BIT));
41 if (tdb->map_ptr)
42 ((unsigned char *)tdb->map_ptr)[off] ^= mask;
43 else {
44 unsigned char c;
45 if (pread(tdb->fd, &c, 1, off) != 1) {
46 fprintf(stderr, "pread: %s\n", strerror(errno));
47 exit(1);
49 c ^= mask;
50 if (pwrite(tdb->fd, &c, 1, off) != 1) {
51 fprintf(stderr, "pwrite: %s\n", strerror(errno));
52 exit(1);
57 static void check_test(struct tdb_context *tdb)
59 TDB_DATA key, data;
60 unsigned int i, verifiable, corrupt, sizes[2], dsize, ksize;
62 ok1(tdb_check(tdb, NULL, NULL) == 0);
64 key.dptr = discard_const_p(uint8_t, "hello");
65 data.dsize = strlen("world");
66 data.dptr = discard_const_p(uint8_t, "world");
68 /* Key and data size respectively. */
69 dsize = ksize = 0;
71 /* 5 keys in hash size 2 means we'll have multichains. */
72 for (key.dsize = 1; key.dsize <= 5; key.dsize++) {
73 ksize += key.dsize;
74 dsize += data.dsize;
75 if (tdb_store(tdb, key, data, TDB_INSERT) != 0)
76 abort();
79 /* This is how many bytes we expect to be verifiable. */
80 /* From the file header. */
81 verifiable = strlen(TDB_MAGIC_FOOD) + 1
82 + 2 * sizeof(uint32_t) + 2 * sizeof(tdb_off_t)
83 + 2 * sizeof(uint32_t);
84 /* From the free list chain and hash chains. */
85 verifiable += 3 * sizeof(tdb_off_t);
86 /* From the record headers & tailer */
87 verifiable += 5 * (sizeof(struct tdb_record) + sizeof(uint32_t));
88 /* The free block: we ignore datalen, keylen, full_hash. */
89 verifiable += sizeof(struct tdb_record) - 3*sizeof(uint32_t) +
90 sizeof(uint32_t);
91 /* Our check function verifies the key and data. */
92 verifiable += ksize + dsize;
94 /* Flip one bit at a time, make sure it detects verifiable bytes. */
95 for (i = 0, corrupt = 0; i < tdb->map_size * CHAR_BIT; i++) {
96 tdb_flip_bit(tdb, i);
97 memset(sizes, 0, sizeof(sizes));
98 if (tdb_check(tdb, check, sizes) != 0)
99 corrupt++;
100 else if (sizes[0] != ksize || sizes[1] != dsize)
101 corrupt++;
102 tdb_flip_bit(tdb, i);
104 ok(corrupt == verifiable * CHAR_BIT, "corrupt %u should be %u",
105 corrupt, verifiable * CHAR_BIT);
108 int main(int argc, char *argv[])
110 struct tdb_context *tdb;
112 plan_tests(4);
113 /* This should use mmap. */
114 tdb = tdb_open_ex("run-corrupt.tdb", 2, TDB_CLEAR_IF_FIRST,
115 O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
117 if (!tdb)
118 abort();
119 check_test(tdb);
120 tdb_close(tdb);
122 /* This should not. */
123 tdb = tdb_open_ex("run-corrupt.tdb", 2, TDB_CLEAR_IF_FIRST|TDB_NOMMAP,
124 O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
126 if (!tdb)
127 abort();
128 check_test(tdb);
129 tdb_close(tdb);
131 return exit_status();