lib/tdb2: adapt unit tests to SAMBA environment.
[kai/samba-autobuild/.git] / lib / tdb2 / test / api-open-multiple-times.c
1 #include "config.h"
2 #include "tdb2.h"
3 #include "tap-interface.h"
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <stdlib.h>
8 #include "logging.h"
9
10 int main(int argc, char *argv[])
11 {
12         unsigned int i, extra_messages;
13         struct tdb_context *tdb, *tdb2;
14         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
15         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
16         struct tdb_data d = { NULL, 0 }; /* Bogus GCC warning */
17         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
18                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT,
19                         TDB_VERSION1, TDB_NOMMAP|TDB_VERSION1,
20                         TDB_CONVERT|TDB_VERSION1,
21                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
22
23         plan_tests(sizeof(flags) / sizeof(flags[0]) * 28);
24         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
25                 tdb = tdb_open("run-open-multiple-times.tdb", flags[i],
26                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
27                 ok1(tdb);
28                 if (!tdb)
29                         continue;
30
31                 if (flags[i] & TDB_VERSION1) {
32                         extra_messages = 1;
33                 } else {
34                         extra_messages = 0;
35                 }
36                 tdb2 = tdb_open("run-open-multiple-times.tdb", flags[i],
37                                 O_RDWR|O_CREAT, 0600, &tap_log_attr);
38                 ok1(tdb_check(tdb, NULL, NULL) == 0);
39                 ok1(tdb_check(tdb2, NULL, NULL) == 0);
40
41                 /* Store in one, fetch in the other. */
42                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
43                 ok1(tdb_fetch(tdb2, key, &d) == TDB_SUCCESS);
44                 ok1(tdb_deq(d, data));
45                 free(d.dptr);
46
47                 /* Vice versa, with delete. */
48                 ok1(tdb_delete(tdb2, key) == 0);
49                 ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_NOEXIST);
50
51                 /* OK, now close first one, check second still good. */
52                 ok1(tdb_close(tdb) == 0);
53
54                 ok1(tdb_store(tdb2, key, data, TDB_REPLACE) == 0);
55                 ok1(tdb_fetch(tdb2, key, &d) == TDB_SUCCESS);
56                 ok1(tdb_deq(d, data));
57                 free(d.dptr);
58
59                 /* Reopen */
60                 tdb = tdb_open("run-open-multiple-times.tdb", flags[i],
61                                O_RDWR|O_CREAT, 0600, &tap_log_attr);
62                 ok1(tdb);
63
64                 ok1(tdb_transaction_start(tdb2) == 0);
65
66                 /* Anything in the other one should fail. */
67                 ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_LOCK);
68                 tap_log_messages -= extra_messages;
69                 ok1(tap_log_messages == 1);
70                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == TDB_ERR_LOCK);
71                 tap_log_messages -= extra_messages;
72                 ok1(tap_log_messages == 2);
73                 ok1(tdb_transaction_start(tdb) == TDB_ERR_LOCK);
74                 ok1(tap_log_messages == 3);
75                 ok1(tdb_chainlock(tdb, key) == TDB_ERR_LOCK);
76                 tap_log_messages -= extra_messages;
77                 ok1(tap_log_messages == 4);
78
79                 /* Transaciton should work as normal. */
80                 ok1(tdb_store(tdb2, key, data, TDB_REPLACE) == TDB_SUCCESS);
81
82                 /* Now... try closing with locks held. */
83                 ok1(tdb_close(tdb2) == 0);
84
85                 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
86                 ok1(tdb_deq(d, data));
87                 free(d.dptr);
88                 ok1(tdb_close(tdb) == 0);
89                 ok1(tap_log_messages == 4);
90                 tap_log_messages = 0;
91         }
92
93         return exit_status();
94 }