tdb2: change API to return the error value.
[ccan] / ccan / tdb2 / test / run-55-transaction.c
1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/free.c>
3 #include <ccan/tdb2/lock.c>
4 #include <ccan/tdb2/io.c>
5 #include <ccan/tdb2/hash.c>
6 #include <ccan/tdb2/check.c>
7 #include <ccan/tdb2/transaction.c>
8 #include <ccan/tap/tap.h>
9 #include "logging.h"
10
11 int main(int argc, char *argv[])
12 {
13         unsigned int i;
14         struct tdb_context *tdb;
15         unsigned char *buffer;
16         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
17                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
18         struct tdb_data key = { (unsigned char *)"key", 3 };
19         struct tdb_data data;
20
21         buffer = malloc(1000);
22         for (i = 0; i < 1000; i++)
23                 buffer[i] = i;
24
25         plan_tests(sizeof(flags) / sizeof(flags[0]) * 20 + 1);
26
27         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
28                 tdb = tdb_open("run-55-transaction.tdb", flags[i],
29                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
30                 ok1(tdb);
31                 if (!tdb)
32                         continue;
33
34                 ok1(tdb_transaction_start(tdb) == 0);
35                 data.dptr = buffer;
36                 data.dsize = 1000;
37                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
38                 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
39                 ok1(data.dsize == 1000);
40                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
41                 free(data.dptr);
42
43                 /* Cancelling a transaction means no store */
44                 tdb_transaction_cancel(tdb);
45                 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
46                 ok1(tdb_check(tdb, NULL, NULL) == 0);
47                 ok1(tdb_fetch(tdb, key, &data) == TDB_ERR_NOEXIST);
48
49                 /* Commit the transaction. */
50                 ok1(tdb_transaction_start(tdb) == 0);
51                 data.dptr = buffer;
52                 data.dsize = 1000;
53                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
54                 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
55                 ok1(data.dsize == 1000);
56                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
57                 free(data.dptr);
58                 ok1(tdb_transaction_commit(tdb) == 0);
59                 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
60                 ok1(tdb_check(tdb, NULL, NULL) == 0);
61                 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
62                 ok1(data.dsize == 1000);
63                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
64                 free(data.dptr);
65
66                 tdb_close(tdb);
67         }
68
69         ok1(tap_log_messages == 0);
70         free(buffer);
71         return exit_status();
72 }