s3:registry: do not use regdb functions during db upgrade
[Samba/gebeck_regimport.git] / lib / tdb2 / test / api-check-callback.c
blobfb980f2276bd50ec111056d9c34286d88ee63d4b
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"
8 #define NUM_RECORDS 1000
10 static bool store_records(struct tdb_context *tdb)
12 int i;
13 struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
14 struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
16 for (i = 0; i < NUM_RECORDS; i++)
17 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
18 return false;
19 return true;
22 static enum TDB_ERROR check(struct tdb_data key,
23 struct tdb_data data,
24 bool *array)
26 int val;
28 if (key.dsize != sizeof(val)) {
29 diag("Wrong key size: %u\n", key.dsize);
30 return TDB_ERR_CORRUPT;
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;
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;
45 if (array[val]) {
46 diag("Value %i already seen\n", val);
47 return TDB_ERR_CORRUPT;
50 array[val] = true;
51 return TDB_SUCCESS;
54 int main(int argc, char *argv[])
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 };
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];
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;
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);
88 ok1(tap_log_messages == 0);
89 return exit_status();