]> git.ozlabs.org Git - ccan/blob - ccan/tdb/test/run-corrupt.c
6d190c2aff976547272dd1f6326155c4f6c90a5b
[ccan] / ccan / tdb / test / run-corrupt.c
1 #define _XOPEN_SOURCE 500
2 #include "tdb/tdb.h"
3 #include "tdb/io.c"
4 #include "tdb/tdb.c"
5 #include "tdb/lock.c"
6 #include "tdb/freelist.c"
7 #include "tdb/traverse.c"
8 #include "tdb/transaction.c"
9 #include "tdb/error.c"
10 #include "tdb/open.c"
11 #include "tdb/check.c"
12 #include "tap/tap.h"
13 #include <stdlib.h>
14 #include <err.h>
15
16 static int check(TDB_DATA key, TDB_DATA data, void *private)
17 {
18         unsigned int *sizes = private;
19
20         if (key.dsize > strlen("hello"))
21                 return -1;
22         if (memcmp(key.dptr, "hello", key.dsize) != 0)
23                 return -1;
24
25         if (data.dsize != strlen("world"))
26                 return -1;
27         if (memcmp(data.dptr, "world", data.dsize) != 0)
28                 return -1;
29
30         sizes[0] += key.dsize;
31         sizes[1] += data.dsize;
32         return 0;
33 }
34
35 int main(int argc, char *argv[])
36 {
37         struct tdb_context *tdb;
38         TDB_DATA key, data;
39         unsigned int i, verifiable, corrupt, sizes[2], dsize, ksize;
40
41         plan_tests(2);
42         tdb = tdb_open("/tmp/test6.tdb", 2, TDB_CLEAR_IF_FIRST,
43                        O_CREAT|O_TRUNC|O_RDWR, 0600);
44
45         if (!tdb)
46                 abort();
47         ok1(tdb_check(tdb, NULL, NULL) == 0);
48         
49         key.dptr = (void *)"hello";
50         data.dsize = strlen("world");
51         data.dptr = (void *)"world";
52
53         /* Key and data size respectively. */
54         dsize = ksize = 0;
55
56         /* 5 keys in hash size 2 means we'll have multichains. */
57         for (key.dsize = 1; key.dsize <= 5; key.dsize++) {
58                 ksize += key.dsize;
59                 dsize += data.dsize;
60                 if (tdb_store(tdb, key, data, TDB_INSERT) != 0)
61                         abort();
62         }
63
64         /* This is how many bytes we expect to be verifiable. */
65         /* From the file header. */
66         verifiable = strlen(TDB_MAGIC_FOOD) + 1
67                 + 2 * sizeof(uint32_t) + 2 * sizeof(tdb_off_t);
68         /* From the free list chain and hash chains. */
69         verifiable += 3 * sizeof(tdb_off_t);
70         /* From the record headers & tailer */
71         verifiable += 5 * (sizeof(struct list_struct) + sizeof(uint32_t));
72         /* The free block: we ignore datalen, keylen, full_hash. */
73         verifiable += sizeof(struct list_struct) - 3*sizeof(uint32_t) +
74                 sizeof(uint32_t);
75         /* Our check function verifies the key and data. */
76         verifiable += ksize + dsize;
77
78         /* Flip one bit at a time, make sure it detects verifiable bytes. */
79         for (i = 0, corrupt = 0; i < tdb->map_size * CHAR_BIT; i++) {
80                 bit_flip(tdb->map_ptr, i);
81                 memset(sizes, 0, sizeof(sizes));
82                 if (tdb_check(tdb, check, sizes) != 0)
83                         corrupt++;
84                 else if (sizes[0] != ksize || sizes[1] != dsize)
85                         corrupt++;
86                 bit_flip(tdb->map_ptr, i);
87         }
88         ok(corrupt == verifiable * CHAR_BIT, "corrupt %u should be %u",
89            corrupt, verifiable * CHAR_BIT);
90         tdb_close(tdb);
91
92         return exit_status();
93 }