tdb2: test: try (almost) all tests with TDB_VERSION1 flag.
[ccan] / ccan / tdb2 / test / run-firstkey-nextkey.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include "logging.h"
4
5 #define NUM_RECORDS 1000
6
7 static bool store_records(struct tdb_context *tdb)
8 {
9         int i;
10         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
11         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
12
13         for (i = 0; i < NUM_RECORDS; i++)
14                 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
15                         return false;
16         return true;
17 }
18
19 struct trav_data {
20         unsigned int records[NUM_RECORDS];
21         unsigned int calls;
22 };
23
24 static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *p)
25 {
26         struct trav_data *td = p;
27         int val;
28
29         memcpy(&val, dbuf.dptr, dbuf.dsize);
30         td->records[td->calls++] = val;
31         return 0;
32 }
33
34 /* Since tdb_nextkey frees dptr, we need to clone it. */
35 static TDB_DATA dup_key(TDB_DATA key)
36 {
37         void *p = malloc(key.dsize);
38         memcpy(p, key.dptr, key.dsize);
39         key.dptr = p;
40         return key;
41 }
42
43 int main(int argc, char *argv[])
44 {
45         unsigned int i, j;
46         int num;
47         struct trav_data td;
48         TDB_DATA k;
49         struct tdb_context *tdb;
50         union tdb_attribute seed_attr;
51         enum TDB_ERROR ecode;
52         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
53                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
54                         TDB_NOMMAP|TDB_CONVERT,
55                         TDB_INTERNAL|TDB_VERSION1, TDB_VERSION1,
56                         TDB_NOMMAP|TDB_VERSION1,
57                         TDB_INTERNAL|TDB_CONVERT|TDB_VERSION1,
58                         TDB_CONVERT|TDB_VERSION1,
59                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
60
61         seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
62         seed_attr.base.next = &tap_log_attr;
63         seed_attr.seed.seed = 6334326220117065685ULL;
64
65         plan_tests(sizeof(flags) / sizeof(flags[0])
66                    * (NUM_RECORDS*6 + (NUM_RECORDS-1)*3 + 22) + 1);
67         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
68                 tdb = tdb_open("run-traverse.tdb", flags[i],
69                                O_RDWR|O_CREAT|O_TRUNC, 0600,
70                                flags[i] & TDB_VERSION1 ? NULL : &seed_attr);
71                 ok1(tdb);
72                 if (!tdb)
73                         continue;
74
75                 ok1(tdb_firstkey(tdb, &k) == TDB_ERR_NOEXIST);
76
77                 /* One entry... */
78                 k.dptr = (unsigned char *)&num;
79                 k.dsize = sizeof(num);
80                 num = 0;
81                 ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
82                 ok1(tdb_firstkey(tdb, &k) == TDB_SUCCESS);
83                 ok1(k.dsize == sizeof(num));
84                 ok1(memcmp(k.dptr, &num, sizeof(num)) == 0);
85                 ok1(tdb_nextkey(tdb, &k) == TDB_ERR_NOEXIST);
86
87                 /* Two entries. */
88                 k.dptr = (unsigned char *)&num;
89                 k.dsize = sizeof(num);
90                 num = 1;
91                 ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
92                 ok1(tdb_firstkey(tdb, &k) == TDB_SUCCESS);
93                 ok1(k.dsize == sizeof(num));
94                 memcpy(&num, k.dptr, sizeof(num));
95                 ok1(num == 0 || num == 1);
96                 ok1(tdb_nextkey(tdb, &k) == TDB_SUCCESS);
97                 ok1(k.dsize == sizeof(j));
98                 memcpy(&j, k.dptr, sizeof(j));
99                 ok1(j == 0 || j == 1);
100                 ok1(j != num);
101                 ok1(tdb_nextkey(tdb, &k) == TDB_ERR_NOEXIST);
102
103                 /* Clean up. */
104                 k.dptr = (unsigned char *)&num;
105                 k.dsize = sizeof(num);
106                 num = 0;
107                 ok1(tdb_delete(tdb, k) == 0);
108                 num = 1;
109                 ok1(tdb_delete(tdb, k) == 0);
110
111                 /* Now lots of records. */
112                 ok1(store_records(tdb));
113                 td.calls = 0;
114
115                 num = tdb_traverse(tdb, trav, &td);
116                 ok1(num == NUM_RECORDS);
117                 ok1(td.calls == NUM_RECORDS);
118
119                 /* Simple loop should match tdb_traverse */
120                 for (j = 0, ecode = tdb_firstkey(tdb, &k); j < td.calls; j++) {
121                         int val;
122
123                         ok1(ecode == TDB_SUCCESS);
124                         ok1(k.dsize == sizeof(val));
125                         memcpy(&val, k.dptr, k.dsize);
126                         ok1(td.records[j] == val);
127                         ecode = tdb_nextkey(tdb, &k);
128                 }
129
130                 /* But arbitrary orderings should work too. */
131                 for (j = td.calls-1; j > 0; j--) {
132                         k.dptr = (unsigned char *)&td.records[j-1];
133                         k.dsize = sizeof(td.records[j-1]);
134                         k = dup_key(k);
135                         ok1(tdb_nextkey(tdb, &k) == TDB_SUCCESS);
136                         ok1(k.dsize == sizeof(td.records[j]));
137                         ok1(memcmp(k.dptr, &td.records[j], k.dsize) == 0);
138                         free(k.dptr);
139                 }
140
141                 /* Even delete should work. */
142                 for (j = 0, ecode = tdb_firstkey(tdb, &k);
143                      ecode != TDB_ERR_NOEXIST;
144                      j++) {
145                         ok1(ecode == TDB_SUCCESS);
146                         ok1(k.dsize == 4);
147                         ok1(tdb_delete(tdb, k) == 0);
148                         ecode = tdb_nextkey(tdb, &k);
149                 }
150
151                 diag("delete using first/nextkey gave %u of %u records",
152                      j, NUM_RECORDS);
153                 ok1(j == NUM_RECORDS);
154                 tdb_close(tdb);
155         }
156
157         ok1(tap_log_messages == 0);
158         return exit_status();
159 }