da1a68043bbf5c5995c4d306987435025f02c8ae
[ddiss/samba.git] / lib / ntdb / test / api-firstkey-nextkey.c
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 <stdlib.h>
8 #include "logging.h"
9
10 #define NUM_RECORDS 1000
11
12 static bool store_records(struct ntdb_context *ntdb)
13 {
14         int i;
15         NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
16         NTDB_DATA data = { (unsigned char *)&i, sizeof(i) };
17
18         for (i = 0; i < NUM_RECORDS; i++)
19                 if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
20                         return false;
21         return true;
22 }
23
24 struct trav_data {
25         unsigned int records[NUM_RECORDS];
26         unsigned int calls;
27 };
28
29 static int trav(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *p)
30 {
31         struct trav_data *td = p;
32         int val;
33
34         memcpy(&val, dbuf.dptr, dbuf.dsize);
35         td->records[td->calls++] = val;
36         return 0;
37 }
38
39 /* Since ntdb_nextkey frees dptr, we need to clone it. */
40 static NTDB_DATA dup_key(NTDB_DATA key)
41 {
42         void *p = malloc(key.dsize);
43         memcpy(p, key.dptr, key.dsize);
44         key.dptr = p;
45         return key;
46 }
47
48 int main(int argc, char *argv[])
49 {
50         unsigned int i, j;
51         int num;
52         struct trav_data td;
53         NTDB_DATA k;
54         struct ntdb_context *ntdb;
55         union ntdb_attribute seed_attr;
56         enum NTDB_ERROR ecode;
57         int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
58                         NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
59                         NTDB_NOMMAP|NTDB_CONVERT };
60
61         seed_attr.base.attr = NTDB_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                 ntdb = ntdb_open("api-firstkey-nextkey.ntdb", flags[i],
69                                O_RDWR|O_CREAT|O_TRUNC, 0600,
70                                &seed_attr);
71                 ok1(ntdb);
72                 if (!ntdb)
73                         continue;
74
75                 ok1(ntdb_firstkey(ntdb, &k) == NTDB_ERR_NOEXIST);
76
77                 /* One entry... */
78                 k.dptr = (unsigned char *)&num;
79                 k.dsize = sizeof(num);
80                 num = 0;
81                 ok1(ntdb_store(ntdb, k, k, NTDB_INSERT) == 0);
82                 ok1(ntdb_firstkey(ntdb, &k) == NTDB_SUCCESS);
83                 ok1(k.dsize == sizeof(num));
84                 ok1(memcmp(k.dptr, &num, sizeof(num)) == 0);
85                 ok1(ntdb_nextkey(ntdb, &k) == NTDB_ERR_NOEXIST);
86
87                 /* Two entries. */
88                 k.dptr = (unsigned char *)&num;
89                 k.dsize = sizeof(num);
90                 num = 1;
91                 ok1(ntdb_store(ntdb, k, k, NTDB_INSERT) == 0);
92                 ok1(ntdb_firstkey(ntdb, &k) == NTDB_SUCCESS);
93                 ok1(k.dsize == sizeof(num));
94                 memcpy(&num, k.dptr, sizeof(num));
95                 ok1(num == 0 || num == 1);
96                 ok1(ntdb_nextkey(ntdb, &k) == NTDB_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(ntdb_nextkey(ntdb, &k) == NTDB_ERR_NOEXIST);
102
103                 /* Clean up. */
104                 k.dptr = (unsigned char *)&num;
105                 k.dsize = sizeof(num);
106                 num = 0;
107                 ok1(ntdb_delete(ntdb, k) == 0);
108                 num = 1;
109                 ok1(ntdb_delete(ntdb, k) == 0);
110
111                 /* Now lots of records. */
112                 ok1(store_records(ntdb));
113                 td.calls = 0;
114
115                 num = ntdb_traverse(ntdb, trav, &td);
116                 ok1(num == NUM_RECORDS);
117                 ok1(td.calls == NUM_RECORDS);
118
119                 /* Simple loop should match ntdb_traverse */
120                 for (j = 0, ecode = ntdb_firstkey(ntdb, &k); j < td.calls; j++) {
121                         int val;
122
123                         ok1(ecode == NTDB_SUCCESS);
124                         ok1(k.dsize == sizeof(val));
125                         memcpy(&val, k.dptr, k.dsize);
126                         ok1(td.records[j] == val);
127                         ecode = ntdb_nextkey(ntdb, &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(ntdb_nextkey(ntdb, &k) == NTDB_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 = ntdb_firstkey(ntdb, &k);
143                      ecode != NTDB_ERR_NOEXIST;
144                      j++) {
145                         ok1(ecode == NTDB_SUCCESS);
146                         ok1(k.dsize == 4);
147                         ok1(ntdb_delete(ntdb, k) == 0);
148                         ecode = ntdb_nextkey(ntdb, &k);
149                 }
150
151                 diag("delete using first/nextkey gave %u of %u records",
152                      j, NUM_RECORDS);
153                 ok1(j == NUM_RECORDS);
154                 ntdb_close(ntdb);
155         }
156
157         ok1(tap_log_messages == 0);
158         return exit_status();
159 }