f74f04b59899e6af10d487b0ed737470b9a52917
[ddiss/samba.git] / lib / ntdb / test / api-check-callback.c
1 #include "config.h"
2 #include "ntdb.h"
3 #include "tap-interface.h"
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include "logging.h"
8
9 #define NUM_RECORDS 1000
10
11 static bool store_records(struct ntdb_context *ntdb)
12 {
13         int i;
14         NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
15         NTDB_DATA data = { (unsigned char *)&i, sizeof(i) };
16
17         for (i = 0; i < NUM_RECORDS; i++)
18                 if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
19                         return false;
20         return true;
21 }
22
23 static enum NTDB_ERROR check(NTDB_DATA key,
24                             NTDB_DATA data,
25                             bool *array)
26 {
27         int val;
28
29         if (key.dsize != sizeof(val)) {
30                 diag("Wrong key size: %u\n", key.dsize);
31                 return NTDB_ERR_CORRUPT;
32         }
33
34         if (key.dsize != data.dsize
35             || memcmp(key.dptr, data.dptr, sizeof(val)) != 0) {
36                 diag("Key and data differ\n");
37                 return NTDB_ERR_CORRUPT;
38         }
39
40         memcpy(&val, key.dptr, sizeof(val));
41         if (val >= NUM_RECORDS || val < 0) {
42                 diag("check value %i\n", val);
43                 return NTDB_ERR_CORRUPT;
44         }
45
46         if (array[val]) {
47                 diag("Value %i already seen\n", val);
48                 return NTDB_ERR_CORRUPT;
49         }
50
51         array[val] = true;
52         return NTDB_SUCCESS;
53 }
54
55 int main(int argc, char *argv[])
56 {
57         unsigned int i, j;
58         struct ntdb_context *ntdb;
59         int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
60                         NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
61                         NTDB_NOMMAP|NTDB_CONVERT };
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", flags[i],
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 }