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