ntdb: fix up tests.
[ccan] / ccan / ntdb / test / api-55-transaction.c
1 #include "../private.h" // struct ntdb_context
2 #include "../ntdb.h"
3 #include "tap-interface.h"
4 #include <stdlib.h>
5 #include "logging.h"
6 #include "helpapi-external-agent.h"
7
8 int main(int argc, char *argv[])
9 {
10         unsigned int i;
11         struct ntdb_context *ntdb;
12         unsigned char *buffer;
13         int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
14                         NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
15         NTDB_DATA key = ntdb_mkdata("key", 3);
16         NTDB_DATA data;
17
18         buffer = malloc(1000);
19         for (i = 0; i < 1000; i++)
20                 buffer[i] = i;
21
22         plan_tests(sizeof(flags) / sizeof(flags[0]) * 20 + 1);
23
24         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
25                 ntdb = ntdb_open("run-55-transaction.ntdb",
26                                  flags[i]|MAYBE_NOSYNC,
27                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
28                 ok1(ntdb);
29                 if (!ntdb)
30                         continue;
31
32                 ok1(ntdb_transaction_start(ntdb) == 0);
33                 data.dptr = buffer;
34                 data.dsize = 1000;
35                 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
36                 ok1(ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS);
37                 ok1(data.dsize == 1000);
38                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
39                 free(data.dptr);
40
41                 /* Cancelling a transaction means no store */
42                 ntdb_transaction_cancel(ntdb);
43                 ok1(ntdb->file->allrecord_lock.count == 0
44                     && ntdb->file->num_lockrecs == 0);
45                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
46                 ok1(ntdb_fetch(ntdb, key, &data) == NTDB_ERR_NOEXIST);
47
48                 /* Commit the transaction. */
49                 ok1(ntdb_transaction_start(ntdb) == 0);
50                 data.dptr = buffer;
51                 data.dsize = 1000;
52                 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
53                 ok1(ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS);
54                 ok1(data.dsize == 1000);
55                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
56                 free(data.dptr);
57                 ok1(ntdb_transaction_commit(ntdb) == 0);
58                 ok1(ntdb->file->allrecord_lock.count == 0
59                     && ntdb->file->num_lockrecs == 0);
60                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
61                 ok1(ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS);
62                 ok1(data.dsize == 1000);
63                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
64                 free(data.dptr);
65
66                 ntdb_close(ntdb);
67         }
68
69         ok1(tap_log_messages == 0);
70         free(buffer);
71         return exit_status();
72 }