diff options
author | Volker Lendecke <vl@samba.org> | 2018-10-07 22:03:09 +0200 |
---|---|---|
committer | Jeremy Allison <jra@samba.org> | 2018-11-06 18:57:25 +0100 |
commit | 3daf9c85b742ec9365faa123e38e23a0d80a69a0 (patch) | |
tree | f710b8775c6bcb4d85e25fc72c75d6b7771eb12f /lib/tdb | |
parent | 46a87f2cbafdc84199e7e131f1d88961b37a2f78 (diff) | |
download | samba-3daf9c85b742ec9365faa123e38e23a0d80a69a0.tar.gz |
tdb: Add test for tdb_traverse_chain
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
Diffstat (limited to 'lib/tdb')
-rw-r--r-- | lib/tdb/test/run-traverse-chain.c | 94 | ||||
-rw-r--r-- | lib/tdb/wscript | 1 |
2 files changed, 95 insertions, 0 deletions
diff --git a/lib/tdb/test/run-traverse-chain.c b/lib/tdb/test/run-traverse-chain.c new file mode 100644 index 00000000000..2a25beca3f0 --- /dev/null +++ b/lib/tdb/test/run-traverse-chain.c @@ -0,0 +1,94 @@ +#include "../common/tdb_private.h" +#include "../common/io.c" +#include "../common/tdb.c" +#include "../common/lock.c" +#include "../common/freelist.c" +#include "../common/traverse.c" +#include "../common/transaction.c" +#include "../common/error.c" +#include "../common/open.c" +#include "../common/check.c" +#include "../common/hash.c" +#include "../common/mutex.c" +#include "tap-interface.h" +#include <stdlib.h> +#include "logging.h" + +static char keystr0[] = "x"; +static TDB_DATA key0 = { .dptr = (uint8_t *)keystr0, + .dsize = sizeof(keystr0) }; +static char valuestr0[] = "y"; +static TDB_DATA value0 = { .dptr = (uint8_t *)valuestr0, + .dsize = sizeof(valuestr0) }; + +static char keystr1[] = "aaa"; +static TDB_DATA key1 = { .dptr = (uint8_t *)keystr1, + .dsize = sizeof(keystr1) }; +static char valuestr1[] = "bbbbb"; +static TDB_DATA value1 = { .dptr = (uint8_t *)valuestr1, + .dsize = sizeof(valuestr1) }; + +static TDB_DATA *keys[] = { &key0, &key1 }; +static TDB_DATA *values[] = { &value0, &value1 }; + +static bool tdb_data_same(TDB_DATA d1, TDB_DATA d2) +{ + if (d1.dsize != d2.dsize) { + return false; + } + return (memcmp(d1.dptr, d2.dptr, d1.dsize) == 0); +} + +struct traverse_chain_state { + size_t idx; + bool ok; +}; + +static int traverse_chain_fn(struct tdb_context *tdb, + TDB_DATA key, + TDB_DATA data, + void *private_data) +{ + struct traverse_chain_state *state = private_data; + + state->ok &= tdb_data_same(key, *keys[state->idx]); + state->ok &= tdb_data_same(data, *values[state->idx]); + state->idx += 1; + + return 0; +} + +int main(int argc, char *argv[]) +{ + struct tdb_context *tdb; + struct traverse_chain_state state = { .ok = true }; + int ret; + + plan_tests(4); + + tdb = tdb_open_ex( + "traverse_chain.tdb", + 1, + TDB_CLEAR_IF_FIRST, + O_RDWR|O_CREAT, + 0600, + &taplogctx, + NULL); + ok1(tdb); + + /* add in reverse order, tdb_store adds to the front of the list */ + ret = tdb_store(tdb, key1, value1, TDB_INSERT); + ok1(ret == 0); + ret = tdb_store(tdb, key0, value0, TDB_INSERT); + ok1(ret == 0); + + ret = tdb_traverse_key_chain(tdb, key0, traverse_chain_fn, &state); + ok1(ret == 2); + ok1(state.ok); + + unlink(tdb_name(tdb)); + + tdb_close(tdb); + + return exit_status(); +} diff --git a/lib/tdb/wscript b/lib/tdb/wscript index 3ab21fcebc1..483d8e0ebec 100644 --- a/lib/tdb/wscript +++ b/lib/tdb/wscript @@ -57,6 +57,7 @@ tdb1_unit_tests = [ 'run-mutex1', 'run-circular-chain', 'run-circular-freelist', + 'run-traverse-chain', ] def options(opt): |