tdb: enforce hashing, via example hash in old rwlocks entry in header.
[ccan] / ccan / tdb / test / run-nested-transactions.c
1 #define _XOPEN_SOURCE 500
2 #include <ccan/tdb/tdb.h>
3 #include <ccan/tdb/io.c>
4 #include <ccan/tdb/tdb.c>
5 #include <ccan/tdb/lock.c>
6 #include <ccan/tdb/freelist.c>
7 #include <ccan/tdb/traverse.c>
8 #include <ccan/tdb/transaction.c>
9 #include <ccan/tdb/error.c>
10 #include <ccan/tdb/open.c>
11 #include <ccan/tdb/check.c>
12 #include <ccan/tap/tap.h>
13 #include <stdlib.h>
14 #include <stdbool.h>
15 #include <err.h>
16 int main(int argc, char *argv[])
17 {
18         struct tdb_context *tdb;
19         TDB_DATA key, data;
20
21         plan_tests(27);
22         key.dsize = strlen("hi");
23         key.dptr = (void *)"hi";
24
25         tdb = tdb_open("run-nested-transactions.tdb", 1024, TDB_CLEAR_IF_FIRST,
26                        O_CREAT|O_TRUNC|O_RDWR, 0600);
27         ok1(tdb);
28
29         /* No nesting by default. */
30         ok1(tdb_transaction_start(tdb) == 0);
31         data.dptr = (void *)"world";
32         data.dsize = strlen("world");
33         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
34         data = tdb_fetch(tdb, key);
35         ok1(data.dsize == strlen("world"));
36         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
37         free(data.dptr);
38         ok1(tdb_transaction_start(tdb) != 0);
39         ok1(tdb_error(tdb) == TDB_ERR_NESTING);
40
41         data = tdb_fetch(tdb, key);
42         ok1(data.dsize == strlen("world"));
43         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
44         free(data.dptr);
45         ok1(tdb_transaction_commit(tdb) == 0);
46         data = tdb_fetch(tdb, key);
47         ok1(data.dsize == strlen("world"));
48         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
49         free(data.dptr);
50         tdb_close(tdb);
51
52         tdb = tdb_open("run-nested-transactions.tdb", 1024, TDB_ALLOW_NESTING, O_RDWR, 0);
53         ok1(tdb);
54
55         ok1(tdb_transaction_start(tdb) == 0);
56         ok1(tdb_transaction_start(tdb) == 0);
57         ok1(tdb_delete(tdb, key) == 0);
58         ok1(tdb_transaction_commit(tdb) == 0);
59         ok1(!tdb_exists(tdb, key));
60         ok1(tdb_transaction_cancel(tdb) == 0);
61         /* Surprise! Kills inner "committed" transaction. */
62         ok1(tdb_exists(tdb, key));
63
64         ok1(tdb_transaction_start(tdb) == 0);
65         ok1(tdb_transaction_start(tdb) == 0);
66         ok1(tdb_delete(tdb, key) == 0);
67         ok1(tdb_transaction_commit(tdb) == 0);
68         ok1(!tdb_exists(tdb, key));
69         ok1(tdb_transaction_commit(tdb) == 0);
70         ok1(!tdb_exists(tdb, key));
71         tdb_close(tdb);
72
73         return exit_status();
74 }