s3:registry: do not use regdb functions during db upgrade
[Samba/gebeck_regimport.git] / lib / tdb2 / test / api-93-repack.c
blob74a8b5eda218c852ce80f743229f7b09870a59a0
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_TESTS 1000
10 static bool store_all(struct tdb_context *tdb)
12 unsigned int i;
13 struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
14 struct tdb_data dbuf = { (unsigned char *)&i, sizeof(i) };
16 for (i = 0; i < NUM_TESTS; i++) {
17 if (tdb_store(tdb, key, dbuf, TDB_INSERT) != TDB_SUCCESS)
18 return false;
20 return true;
23 static int mark_entry(struct tdb_context *tdb,
24 TDB_DATA key, TDB_DATA data, bool found[])
26 unsigned int num;
28 if (key.dsize != sizeof(num))
29 return -1;
30 memcpy(&num, key.dptr, key.dsize);
31 if (num >= NUM_TESTS)
32 return -1;
33 if (found[num])
34 return -1;
35 found[num] = true;
36 return 0;
39 static bool is_all_set(bool found[], unsigned int num)
41 unsigned int i;
43 for (i = 0; i < num; i++)
44 if (!found[i])
45 return false;
46 return true;
49 int main(int argc, char *argv[])
51 unsigned int i;
52 bool found[NUM_TESTS];
53 struct tdb_context *tdb;
54 int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
55 TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT,
56 TDB_VERSION1, TDB_VERSION1|TDB_NOMMAP,
57 TDB_VERSION1|TDB_CONVERT,
58 TDB_VERSION1|TDB_NOMMAP|TDB_CONVERT
61 plan_tests(sizeof(flags) / sizeof(flags[0]) * 6 + 1);
63 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
64 tdb = tdb_open("run-93-repack.tdb", flags[i],
65 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
66 ok1(tdb);
67 if (!tdb)
68 break;
70 ok1(store_all(tdb));
72 ok1(tdb_repack(tdb) == TDB_SUCCESS);
73 memset(found, 0, sizeof(found));
74 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
75 ok1(tdb_traverse(tdb, mark_entry, found) == NUM_TESTS);
76 ok1(is_all_set(found, NUM_TESTS));
77 tdb_close(tdb);
80 ok1(tap_log_messages == 0);
81 return exit_status();