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