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