]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/api-check-callback.c
20c88e7175b78cea08b1809885bab9ef22a4990c
[ccan] / ccan / ntdb / test / api-check-callback.c
1 #include "config.h"
2 #include "ntdb.h"
3 #include "private.h"
4 #include "tap-interface.h"
5 #include "logging.h"
6
7 #define NUM_RECORDS 1000
8
9 static bool store_records(struct ntdb_context *ntdb)
10 {
11         int i;
12         NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
13         NTDB_DATA data = { (unsigned char *)&i, sizeof(i) };
14
15         for (i = 0; i < NUM_RECORDS; i++)
16                 if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
17                         return false;
18         return true;
19 }
20
21 static enum NTDB_ERROR check(NTDB_DATA key,
22                             NTDB_DATA data,
23                             bool *array)
24 {
25         int val;
26
27         if (key.dsize != sizeof(val)) {
28                 diag("Wrong key size: %zu\n", key.dsize);
29                 return NTDB_ERR_CORRUPT;
30         }
31
32         if (key.dsize != data.dsize
33             || memcmp(key.dptr, data.dptr, sizeof(val)) != 0) {
34                 diag("Key and data differ\n");
35                 return NTDB_ERR_CORRUPT;
36         }
37
38         memcpy(&val, key.dptr, sizeof(val));
39         if (val >= NUM_RECORDS || val < 0) {
40                 diag("check value %i\n", val);
41                 return NTDB_ERR_CORRUPT;
42         }
43
44         if (array[val]) {
45                 diag("Value %i already seen\n", val);
46                 return NTDB_ERR_CORRUPT;
47         }
48
49         array[val] = true;
50         return NTDB_SUCCESS;
51 }
52
53 int main(int argc, char *argv[])
54 {
55         unsigned int i, j;
56         struct ntdb_context *ntdb;
57         int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
58                         NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
59                         NTDB_NOMMAP|NTDB_CONVERT };
60         return 0;
61
62         plan_tests(sizeof(flags) / sizeof(flags[0]) * 4 + 1);
63         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
64                 bool array[NUM_RECORDS];
65
66                 ntdb = ntdb_open("run-check-callback.ntdb",
67                                  flags[i]|MAYBE_NOSYNC,
68                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
69                 ok1(ntdb);
70                 if (!ntdb)
71                         continue;
72
73                 ok1(store_records(ntdb));
74                 for (j = 0; j < NUM_RECORDS; j++)
75                         array[j] = false;
76                 ok1(ntdb_check(ntdb, check, array) == NTDB_SUCCESS);
77                 for (j = 0; j < NUM_RECORDS; j++)
78                         if (!array[j])
79                                 break;
80                 ok1(j == NUM_RECORDS);
81                 ntdb_close(ntdb);
82         }
83
84         ok1(tap_log_messages == 0);
85         return exit_status();
86 }