]> git.ozlabs.org Git - ccan/blob - ccan/tdb/test/run-corrupt.c
tdb: missing files from checkin.
[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         /* From the free list chain and hash chains. */
78         verifiable += 3 * sizeof(tdb_off_t);
79         /* From the record headers & tailer */
80         verifiable += 5 * (sizeof(struct tdb_record) + sizeof(uint32_t));
81         /* The free block: we ignore datalen, keylen, full_hash. */
82         verifiable += sizeof(struct tdb_record) - 3*sizeof(uint32_t) +
83                 sizeof(uint32_t);
84         /* Our check function verifies the key and data. */
85         verifiable += ksize + dsize;
86
87         /* Flip one bit at a time, make sure it detects verifiable bytes. */
88         for (i = 0, corrupt = 0; i < tdb->map_size * CHAR_BIT; i++) {
89                 tdb_flip_bit(tdb, i);
90                 memset(sizes, 0, sizeof(sizes));
91                 if (tdb_check(tdb, check, sizes) != 0)
92                         corrupt++;
93                 else if (sizes[0] != ksize || sizes[1] != dsize)
94                         corrupt++;
95                 tdb_flip_bit(tdb, i);
96         }
97         ok(corrupt == verifiable * CHAR_BIT, "corrupt %u should be %u",
98            corrupt, verifiable * CHAR_BIT);
99 }
100
101 int main(int argc, char *argv[])
102 {
103         struct tdb_context *tdb;
104
105         plan_tests(4);
106         /* This should use mmap. */
107         tdb = tdb_open_ex("run-corrupt.tdb", 2, TDB_CLEAR_IF_FIRST,
108                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
109
110         if (!tdb)
111                 abort();
112         check_test(tdb);
113         tdb_close(tdb);
114
115         /* This should not. */
116         tdb = tdb_open_ex("run-corrupt.tdb", 2, TDB_CLEAR_IF_FIRST|TDB_NOMMAP,
117                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
118
119         if (!tdb)
120                 abort();
121         check_test(tdb);
122         tdb_close(tdb);
123
124         return exit_status();
125 }