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