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