Fix missing va_end()s
[ccan] / ccan / ntdb / test / api-open-multiple-times.c
1 #include "config.h"
2 #include "../ntdb.h"
3 #include "tap-interface.h"
4 #include <stdlib.h>
5 #include "logging.h"
6 #include "../private.h"
7 #include "helpapi-external-agent.h"
8
9 int main(int argc, char *argv[])
10 {
11         unsigned int i;
12         struct ntdb_context *ntdb, *ntdb2;
13         NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
14         NTDB_DATA data = { (unsigned char *)&i, sizeof(i) };
15         NTDB_DATA d = { NULL, 0 }; /* Bogus GCC warning */
16         int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
17                         NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
18
19         plan_tests(sizeof(flags) / sizeof(flags[0]) * 30);
20         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
21                 ntdb = ntdb_open("run-open-multiple-times.ntdb",
22                                  flags[i]|MAYBE_NOSYNC,
23                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
24                 ok1(ntdb);
25                 if (!ntdb)
26                         continue;
27
28                 ntdb2 = ntdb_open("run-open-multiple-times.ntdb",
29                                   flags[i]|MAYBE_NOSYNC,
30                                   O_RDWR|O_CREAT, 0600, &tap_log_attr);
31                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
32                 ok1(ntdb_check(ntdb2, NULL, NULL) == 0);
33                 ok1((flags[i] & NTDB_NOMMAP) || ntdb2->file->map_ptr);
34
35                 /* Store in one, fetch in the other. */
36                 ok1(ntdb_store(ntdb, key, data, NTDB_REPLACE) == 0);
37                 ok1(ntdb_fetch(ntdb2, key, &d) == NTDB_SUCCESS);
38                 ok1(ntdb_deq(d, data));
39                 free(d.dptr);
40
41                 /* Vice versa, with delete. */
42                 ok1(ntdb_delete(ntdb2, key) == 0);
43                 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_ERR_NOEXIST);
44
45                 /* OK, now close first one, check second still good. */
46                 ok1(ntdb_close(ntdb) == 0);
47
48                 ok1((flags[i] & NTDB_NOMMAP) || ntdb2->file->map_ptr);
49                 ok1(ntdb_store(ntdb2, key, data, NTDB_REPLACE) == 0);
50                 ok1(ntdb_fetch(ntdb2, key, &d) == NTDB_SUCCESS);
51                 ok1(ntdb_deq(d, data));
52                 free(d.dptr);
53
54                 /* Reopen */
55                 ntdb = ntdb_open("run-open-multiple-times.ntdb",
56                                  flags[i]|MAYBE_NOSYNC,
57                                  O_RDWR|O_CREAT, 0600, &tap_log_attr);
58                 ok1(ntdb);
59
60                 ok1(ntdb_transaction_start(ntdb2) == 0);
61
62                 /* Anything in the other one should fail. */
63                 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_ERR_LOCK);
64                 ok1(tap_log_messages == 1);
65                 ok1(ntdb_store(ntdb, key, data, NTDB_REPLACE) == NTDB_ERR_LOCK);
66                 ok1(tap_log_messages == 2);
67                 ok1(ntdb_transaction_start(ntdb) == NTDB_ERR_LOCK);
68                 ok1(tap_log_messages == 3);
69                 ok1(ntdb_chainlock(ntdb, key) == NTDB_ERR_LOCK);
70                 ok1(tap_log_messages == 4);
71
72                 /* Transaciton should work as normal. */
73                 ok1(ntdb_store(ntdb2, key, data, NTDB_REPLACE) == NTDB_SUCCESS);
74
75                 /* Now... try closing with locks held. */
76                 ok1(ntdb_close(ntdb2) == 0);
77
78                 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
79                 ok1(ntdb_deq(d, data));
80                 free(d.dptr);
81                 ok1(ntdb_close(ntdb) == 0);
82                 ok1(tap_log_messages == 4);
83                 tap_log_messages = 0;
84         }
85
86         return exit_status();
87 }