]> git.ozlabs.org Git - ccan/blob - ccan/tdb/test/run-corrupt.c
tdb: put example hashes into header, so we notice incorrect hash_fn.
[ccan] / ccan / tdb / test / run-corrupt.c
1 #define _XOPEN_SOURCE 500
2 #include <ccan/tdb/tdb.h>
3 #include <ccan/tdb/io.c>
4 #include <ccan/tdb/tdb.c>
5 #include <ccan/tdb/lock.c>
6 #include <ccan/tdb/freelist.c>
7 #include <ccan/tdb/traverse.c>
8 #include <ccan/tdb/transaction.c>
9 #include <ccan/tdb/error.c>
10 #include <ccan/tdb/open.c>
11 #include <ccan/tdb/check.c>
12 #include <ccan/tap/tap.h>
13 #include <stdlib.h>
14 #include <err.h>
15 #include "logging.h"
16
17 static int check(TDB_DATA key, TDB_DATA data, void *private)
18 {
19         unsigned int *sizes = private;
20
21         if (key.dsize > strlen("hello"))
22                 return -1;
23         if (memcmp(key.dptr, "hello", key.dsize) != 0)
24                 return -1;
25
26         if (data.dsize != strlen("world"))
27                 return -1;
28         if (memcmp(data.dptr, "world", data.dsize) != 0)
29                 return -1;
30
31         sizes[0] += key.dsize;
32         sizes[1] += data.dsize;
33         return 0;
34 }
35
36 static void tdb_flip_bit(struct tdb_context *tdb, unsigned int bit)
37 {
38         unsigned int off = bit / CHAR_BIT;
39         unsigned char mask = (1 << (bit % CHAR_BIT));
40
41         if (tdb->map_ptr)
42                 ((unsigned char *)tdb->map_ptr)[off] ^= mask;
43         else {
44                 unsigned char c;
45                 pread(tdb->fd, &c, 1, off);
46                 c ^= mask;
47                 pwrite(tdb->fd, &c, 1, off);
48         }
49 }
50
51 static void check_test(struct tdb_context *tdb)
52 {
53         TDB_DATA key, data;
54         unsigned int i, verifiable, corrupt, sizes[2], dsize, ksize;
55
56         ok1(tdb_check(tdb, NULL, NULL) == 0);
57         
58         key.dptr = (void *)"hello";
59         data.dsize = strlen("world");
60         data.dptr = (void *)"world";
61
62         /* Key and data size respectively. */
63         dsize = ksize = 0;
64
65         /* 5 keys in hash size 2 means we'll have multichains. */
66         for (key.dsize = 1; key.dsize <= 5; key.dsize++) {
67                 ksize += key.dsize;
68                 dsize += data.dsize;
69                 if (tdb_store(tdb, key, data, TDB_INSERT) != 0)
70                         abort();
71         }
72
73         /* This is how many bytes we expect to be verifiable. */
74         /* From the file header. */
75         verifiable = strlen(TDB_MAGIC_FOOD) + 1
76                 + 2 * sizeof(uint32_t) + 2 * sizeof(tdb_off_t)
77                 + 2 * sizeof(uint32_t);
78         /* From the free list chain and hash chains. */
79         verifiable += 3 * sizeof(tdb_off_t);
80         /* From the record headers & tailer */
81         verifiable += 5 * (sizeof(struct tdb_record) + sizeof(uint32_t));
82         /* The free block: we ignore datalen, keylen, full_hash. */
83         verifiable += sizeof(struct tdb_record) - 3*sizeof(uint32_t) +
84                 sizeof(uint32_t);
85         /* Our check function verifies the key and data. */
86         verifiable += ksize + dsize;
87
88         /* Flip one bit at a time, make sure it detects verifiable bytes. */
89         for (i = 0, corrupt = 0; i < tdb->map_size * CHAR_BIT; i++) {
90                 tdb_flip_bit(tdb, i);
91                 memset(sizes, 0, sizeof(sizes));
92                 if (tdb_check(tdb, check, sizes) != 0)
93                         corrupt++;
94                 else if (sizes[0] != ksize || sizes[1] != dsize)
95                         corrupt++;
96                 tdb_flip_bit(tdb, i);
97         }
98         ok(corrupt == verifiable * CHAR_BIT, "corrupt %u should be %u",
99            corrupt, verifiable * CHAR_BIT);
100 }
101
102 int main(int argc, char *argv[])
103 {
104         struct tdb_context *tdb;
105
106         plan_tests(4);
107         /* This should use mmap. */
108         tdb = tdb_open_ex("run-corrupt.tdb", 2, TDB_CLEAR_IF_FIRST,
109                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
110
111         if (!tdb)
112                 abort();
113         check_test(tdb);
114         tdb_close(tdb);
115
116         /* This should not. */
117         tdb = tdb_open_ex("run-corrupt.tdb", 2, TDB_CLEAR_IF_FIRST|TDB_NOMMAP,
118                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
119
120         if (!tdb)
121                 abort();
122         check_test(tdb);
123         tdb_close(tdb);
124
125         return exit_status();
126 }