]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/api-firstkey-nextkey.c
179cf76c8e2c0fa540872019d164ab9bc593177c
[ccan] / ccan / ntdb / test / api-firstkey-nextkey.c
1 #include "config.h"
2 #include "ntdb.h"
3 #include "private.h"
4 #include "tap-interface.h"
5 #include "logging.h"
6
7 #define NUM_RECORDS 1000
8
9 static bool store_records(struct ntdb_context *ntdb)
10 {
11         int i;
12         NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
13         NTDB_DATA data = { (unsigned char *)&i, sizeof(i) };
14
15         for (i = 0; i < NUM_RECORDS; i++)
16                 if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
17                         return false;
18         return true;
19 }
20
21 struct trav_data {
22         unsigned int records[NUM_RECORDS];
23         unsigned int calls;
24 };
25
26 static int trav(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *p)
27 {
28         struct trav_data *td = p;
29         int val;
30
31         memcpy(&val, dbuf.dptr, dbuf.dsize);
32         td->records[td->calls++] = val;
33         return 0;
34 }
35
36 /* Since ntdb_nextkey frees dptr, we need to clone it. */
37 static NTDB_DATA dup_key(NTDB_DATA key)
38 {
39         void *p = malloc(key.dsize);
40         memcpy(p, key.dptr, key.dsize);
41         key.dptr = p;
42         return key;
43 }
44
45 int main(int argc, char *argv[])
46 {
47         unsigned int i, j;
48         int num;
49         struct trav_data td;
50         NTDB_DATA k;
51         struct ntdb_context *ntdb;
52         union ntdb_attribute seed_attr;
53         enum NTDB_ERROR ecode;
54         int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
55                         NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
56                         NTDB_NOMMAP|NTDB_CONVERT };
57
58         seed_attr.base.attr = NTDB_ATTRIBUTE_SEED;
59         seed_attr.base.next = &tap_log_attr;
60         seed_attr.seed.seed = 6334326220117065685ULL;
61
62         plan_tests(sizeof(flags) / sizeof(flags[0])
63                    * (NUM_RECORDS*6 + (NUM_RECORDS-1)*3 + 22) + 1);
64         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
65                 ntdb = ntdb_open("api-firstkey-nextkey.ntdb",
66                                  flags[i]|MAYBE_NOSYNC,
67                                  O_RDWR|O_CREAT|O_TRUNC, 0600,
68                                  &seed_attr);
69                 ok1(ntdb);
70                 if (!ntdb)
71                         continue;
72
73                 ok1(ntdb_firstkey(ntdb, &k) == NTDB_ERR_NOEXIST);
74
75                 /* One entry... */
76                 k.dptr = (unsigned char *)&num;
77                 k.dsize = sizeof(num);
78                 num = 0;
79                 ok1(ntdb_store(ntdb, k, k, NTDB_INSERT) == 0);
80                 ok1(ntdb_firstkey(ntdb, &k) == NTDB_SUCCESS);
81                 ok1(k.dsize == sizeof(num));
82                 ok1(memcmp(k.dptr, &num, sizeof(num)) == 0);
83                 ok1(ntdb_nextkey(ntdb, &k) == NTDB_ERR_NOEXIST);
84
85                 /* Two entries. */
86                 k.dptr = (unsigned char *)&num;
87                 k.dsize = sizeof(num);
88                 num = 1;
89                 ok1(ntdb_store(ntdb, k, k, NTDB_INSERT) == 0);
90                 ok1(ntdb_firstkey(ntdb, &k) == NTDB_SUCCESS);
91                 ok1(k.dsize == sizeof(num));
92                 memcpy(&num, k.dptr, sizeof(num));
93                 ok1(num == 0 || num == 1);
94                 ok1(ntdb_nextkey(ntdb, &k) == NTDB_SUCCESS);
95                 ok1(k.dsize == sizeof(j));
96                 memcpy(&j, k.dptr, sizeof(j));
97                 ok1(j == 0 || j == 1);
98                 ok1(j != num);
99                 ok1(ntdb_nextkey(ntdb, &k) == NTDB_ERR_NOEXIST);
100
101                 /* Clean up. */
102                 k.dptr = (unsigned char *)&num;
103                 k.dsize = sizeof(num);
104                 num = 0;
105                 ok1(ntdb_delete(ntdb, k) == 0);
106                 num = 1;
107                 ok1(ntdb_delete(ntdb, k) == 0);
108
109                 /* Now lots of records. */
110                 ok1(store_records(ntdb));
111                 td.calls = 0;
112
113                 num = ntdb_traverse(ntdb, trav, &td);
114                 ok1(num == NUM_RECORDS);
115                 ok1(td.calls == NUM_RECORDS);
116
117                 /* Simple loop should match ntdb_traverse */
118                 for (j = 0, ecode = ntdb_firstkey(ntdb, &k); j < td.calls; j++) {
119                         int val;
120
121                         ok1(ecode == NTDB_SUCCESS);
122                         ok1(k.dsize == sizeof(val));
123                         memcpy(&val, k.dptr, k.dsize);
124                         ok1(td.records[j] == val);
125                         ecode = ntdb_nextkey(ntdb, &k);
126                 }
127
128                 /* But arbitrary orderings should work too. */
129                 for (j = td.calls-1; j > 0; j--) {
130                         k.dptr = (unsigned char *)&td.records[j-1];
131                         k.dsize = sizeof(td.records[j-1]);
132                         k = dup_key(k);
133                         ok1(ntdb_nextkey(ntdb, &k) == NTDB_SUCCESS);
134                         ok1(k.dsize == sizeof(td.records[j]));
135                         ok1(memcmp(k.dptr, &td.records[j], k.dsize) == 0);
136                         free(k.dptr);
137                 }
138
139                 /* Even delete should work. */
140                 for (j = 0, ecode = ntdb_firstkey(ntdb, &k);
141                      ecode != NTDB_ERR_NOEXIST;
142                      j++) {
143                         ok1(ecode == NTDB_SUCCESS);
144                         ok1(k.dsize == 4);
145                         ok1(ntdb_delete(ntdb, k) == 0);
146                         ecode = ntdb_nextkey(ntdb, &k);
147                 }
148
149                 diag("delete using first/nextkey gave %u of %u records",
150                      j, NUM_RECORDS);
151                 ok1(j == NUM_RECORDS);
152                 ntdb_close(ntdb);
153         }
154
155         ok1(tap_log_messages == 0);
156         return exit_status();
157 }