99f5578db72d1dd189157046e11771e4651d84a0
[ddiss/samba.git] / lib / tdb2 / test / run-tdb1-corrupt.c
1 #include "tdb2-source.h"
2 #include "tap-interface.h"
3 #include <stdlib.h>
4 #include "logging.h"
5
6 static int check(TDB_DATA key, TDB_DATA data, void *private)
7 {
8         unsigned int *sizes = private;
9
10         if (key.dsize > strlen("hello"))
11                 return -1;
12         if (memcmp(key.dptr, "hello", key.dsize) != 0)
13                 return -1;
14
15         if (data.dsize != strlen("world"))
16                 return -1;
17         if (memcmp(data.dptr, "world", data.dsize) != 0)
18                 return -1;
19
20         sizes[0] += key.dsize;
21         sizes[1] += data.dsize;
22         return 0;
23 }
24
25 static void tdb1_flip_bit(struct tdb_context *tdb, unsigned int bit)
26 {
27         unsigned int off = bit / CHAR_BIT;
28         unsigned char mask = (1 << (bit % CHAR_BIT));
29
30         if (tdb->file->map_ptr)
31                 ((unsigned char *)tdb->file->map_ptr)[off] ^= mask;
32         else {
33                 unsigned char c;
34                 if (pread(tdb->file->fd, &c, 1, off) != 1)
35                         err(1, "pread");
36                 c ^= mask;
37                 if (pwrite(tdb->file->fd, &c, 1, off) != 1)
38                         err(1, "pwrite");
39         }
40 }
41
42 static void check_test(struct tdb_context *tdb)
43 {
44         TDB_DATA key, data;
45         unsigned int i, verifiable, corrupt, sizes[2], dsize, ksize;
46
47         ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
48
49         key = tdb_mkdata("hello", strlen("hello"));
50         data = tdb_mkdata("world", strlen("world"));
51
52         /* Key and data size respectively. */
53         dsize = ksize = 0;
54
55         /* 5 keys in hash size 2 means we'll have multichains. */
56         for (key.dsize = 1; key.dsize <= 5; key.dsize++) {
57                 ksize += key.dsize;
58                 dsize += data.dsize;
59                 if (tdb_store(tdb, key, data, TDB_INSERT) != TDB_SUCCESS)
60                         abort();
61         }
62
63         /* This is how many bytes we expect to be verifiable. */
64         /* From the file header. */
65         verifiable = strlen(TDB_MAGIC_FOOD) + 1
66                 + 2 * sizeof(uint32_t) + 2 * sizeof(tdb1_off_t)
67                 + 2 * sizeof(uint32_t);
68         /* From the free list chain and hash chains. */
69         verifiable += 3 * sizeof(tdb1_off_t);
70         /* From the record headers & tailer */
71         verifiable += 5 * (sizeof(struct tdb1_record) + sizeof(uint32_t));
72         /* The free block: we ignore datalen, keylen, full_hash. */
73         verifiable += sizeof(struct tdb1_record) - 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->file->map_size * CHAR_BIT; i++) {
80                 tdb1_flip_bit(tdb, i);
81                 memset(sizes, 0, sizeof(sizes));
82                 if (tdb_check(tdb, check, sizes) == TDB_ERR_CORRUPT)
83                         corrupt++;
84                 else if (sizes[0] != ksize || sizes[1] != dsize)
85                         corrupt++;
86                 tdb1_flip_bit(tdb, i);
87         }
88         ok(corrupt == verifiable * CHAR_BIT, "corrupt %u should be %u",
89            corrupt, verifiable * CHAR_BIT);
90 }
91
92 int main(int argc, char *argv[])
93 {
94         struct tdb_context *tdb;
95         union tdb_attribute hsize;
96
97         hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
98         hsize.base.next = &tap_log_attr;
99         hsize.tdb1_hashsize.hsize = 2;
100
101         plan_tests(4);
102         /* This should use mmap. */
103         tdb = tdb_open("run-corrupt.tdb1", TDB_VERSION1,
104                        O_CREAT|O_TRUNC|O_RDWR, 0600, &hsize);
105
106         if (!tdb)
107                 abort();
108         check_test(tdb);
109         tdb_close(tdb);
110
111         /* This should not. */
112         tdb = tdb_open("run-corrupt.tdb1", TDB_VERSION1|TDB_NOMMAP,
113                        O_CREAT|O_TRUNC|O_RDWR, 0600, &hsize);
114
115         if (!tdb)
116                 abort();
117         check_test(tdb);
118         tdb_close(tdb);
119
120         return exit_status();
121 }