VERSION: Bump version number up to 4.1.3...
[Samba.git] / lib / ntdb / test / api-check-callback.c
blobb5ca8a057c13d198bed76cf25588f7b7d57e6405
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"
9 #define NUM_RECORDS 1000
11 static bool store_records(struct ntdb_context *ntdb)
13 int i;
14 NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
15 NTDB_DATA data = { (unsigned char *)&i, sizeof(i) };
17 for (i = 0; i < NUM_RECORDS; i++)
18 if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
19 return false;
20 return true;
23 static enum NTDB_ERROR check(NTDB_DATA key,
24 NTDB_DATA data,
25 bool *array)
27 int val;
29 if (key.dsize != sizeof(val)) {
30 diag("Wrong key size: %zu\n", key.dsize);
31 return NTDB_ERR_CORRUPT;
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;
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;
46 if (array[val]) {
47 diag("Value %i already seen\n", val);
48 return NTDB_ERR_CORRUPT;
51 array[val] = true;
52 return NTDB_SUCCESS;
55 int main(int argc, char *argv[])
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 return 0;
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];
68 ntdb = ntdb_open("run-check-callback.ntdb",
69 flags[i]|MAYBE_NOSYNC,
70 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
71 ok1(ntdb);
72 if (!ntdb)
73 continue;
75 ok1(store_records(ntdb));
76 for (j = 0; j < NUM_RECORDS; j++)
77 array[j] = false;
78 ok1(ntdb_check(ntdb, check, array) == NTDB_SUCCESS);
79 for (j = 0; j < NUM_RECORDS; j++)
80 if (!array[j])
81 break;
82 ok1(j == NUM_RECORDS);
83 ntdb_close(ntdb);
86 ok1(tap_log_messages == 0);
87 return exit_status();