]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-check-callback.c
tdb2: test: try (almost) all tests with TDB_VERSION1 flag.
[ccan] / ccan / tdb2 / test / run-check-callback.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include "logging.h"
4
5 #define NUM_RECORDS 1000
6
7 static bool store_records(struct tdb_context *tdb)
8 {
9         int i;
10         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
11         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
12
13         for (i = 0; i < NUM_RECORDS; i++)
14                 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
15                         return false;
16         return true;
17 }
18
19 static enum TDB_ERROR check(struct tdb_data key,
20                             struct tdb_data data,
21                             bool *array)
22 {
23         int val;
24
25         if (key.dsize != sizeof(val)) {
26                 diag("Wrong key size: %u\n", key.dsize);
27                 return TDB_ERR_CORRUPT;
28         }
29
30         if (key.dsize != data.dsize
31             || memcmp(key.dptr, data.dptr, sizeof(val)) != 0) {
32                 diag("Key and data differ\n");
33                 return TDB_ERR_CORRUPT;
34         }
35
36         memcpy(&val, key.dptr, sizeof(val));
37         if (val >= NUM_RECORDS || val < 0) {
38                 diag("check value %i\n", val);
39                 return TDB_ERR_CORRUPT;
40         }
41
42         if (array[val]) {
43                 diag("Value %i already seen\n", val);
44                 return TDB_ERR_CORRUPT;
45         }
46
47         array[val] = true;
48         return TDB_SUCCESS;
49 }
50
51 int main(int argc, char *argv[])
52 {
53         unsigned int i, j;
54         struct tdb_context *tdb;
55         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
56                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
57                         TDB_NOMMAP|TDB_CONVERT,
58                         TDB_INTERNAL|TDB_VERSION1, TDB_VERSION1,
59                         TDB_NOMMAP|TDB_VERSION1,
60                         TDB_INTERNAL|TDB_CONVERT|TDB_VERSION1,
61                         TDB_CONVERT|TDB_VERSION1,
62                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
63
64         plan_tests(sizeof(flags) / sizeof(flags[0]) * 4 + 1);
65         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
66                 bool array[NUM_RECORDS];
67
68                 tdb = tdb_open("run-check-callback.tdb", flags[i],
69                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
70                 ok1(tdb);
71                 if (!tdb)
72                         continue;
73
74                 ok1(store_records(tdb));
75                 for (j = 0; j < NUM_RECORDS; j++)
76                         array[j] = false;
77                 ok1(tdb_check(tdb, check, array) == TDB_SUCCESS);
78                 for (j = 0; j < NUM_RECORDS; j++)
79                         if (!array[j])
80                                 break;
81                 ok1(j == NUM_RECORDS);
82                 tdb_close(tdb);
83         }
84
85         ok1(tap_log_messages == 0);
86         return exit_status();
87 }