lib:crypto: Reformat source code
[samba.git] / lib / tdb / test / run-traverse-chain.c
blob2a25beca3f0074649b638e7699a2e858fabb3bfe
1 #include "../common/tdb_private.h"
2 #include "../common/io.c"
3 #include "../common/tdb.c"
4 #include "../common/lock.c"
5 #include "../common/freelist.c"
6 #include "../common/traverse.c"
7 #include "../common/transaction.c"
8 #include "../common/error.c"
9 #include "../common/open.c"
10 #include "../common/check.c"
11 #include "../common/hash.c"
12 #include "../common/mutex.c"
13 #include "tap-interface.h"
14 #include <stdlib.h>
15 #include "logging.h"
17 static char keystr0[] = "x";
18 static TDB_DATA key0 = { .dptr = (uint8_t *)keystr0,
19 .dsize = sizeof(keystr0) };
20 static char valuestr0[] = "y";
21 static TDB_DATA value0 = { .dptr = (uint8_t *)valuestr0,
22 .dsize = sizeof(valuestr0) };
24 static char keystr1[] = "aaa";
25 static TDB_DATA key1 = { .dptr = (uint8_t *)keystr1,
26 .dsize = sizeof(keystr1) };
27 static char valuestr1[] = "bbbbb";
28 static TDB_DATA value1 = { .dptr = (uint8_t *)valuestr1,
29 .dsize = sizeof(valuestr1) };
31 static TDB_DATA *keys[] = { &key0, &key1 };
32 static TDB_DATA *values[] = { &value0, &value1 };
34 static bool tdb_data_same(TDB_DATA d1, TDB_DATA d2)
36 if (d1.dsize != d2.dsize) {
37 return false;
39 return (memcmp(d1.dptr, d2.dptr, d1.dsize) == 0);
42 struct traverse_chain_state {
43 size_t idx;
44 bool ok;
47 static int traverse_chain_fn(struct tdb_context *tdb,
48 TDB_DATA key,
49 TDB_DATA data,
50 void *private_data)
52 struct traverse_chain_state *state = private_data;
54 state->ok &= tdb_data_same(key, *keys[state->idx]);
55 state->ok &= tdb_data_same(data, *values[state->idx]);
56 state->idx += 1;
58 return 0;
61 int main(int argc, char *argv[])
63 struct tdb_context *tdb;
64 struct traverse_chain_state state = { .ok = true };
65 int ret;
67 plan_tests(4);
69 tdb = tdb_open_ex(
70 "traverse_chain.tdb",
72 TDB_CLEAR_IF_FIRST,
73 O_RDWR|O_CREAT,
74 0600,
75 &taplogctx,
76 NULL);
77 ok1(tdb);
79 /* add in reverse order, tdb_store adds to the front of the list */
80 ret = tdb_store(tdb, key1, value1, TDB_INSERT);
81 ok1(ret == 0);
82 ret = tdb_store(tdb, key0, value0, TDB_INSERT);
83 ok1(ret == 0);
85 ret = tdb_traverse_key_chain(tdb, key0, traverse_chain_fn, &state);
86 ok1(ret == 2);
87 ok1(state.ok);
89 unlink(tdb_name(tdb));
91 tdb_close(tdb);
93 return exit_status();