wintest: add option to select the dns backend
[Samba/gebeck_regimport.git] / lib / ntdb / test / api-open-multiple-times.c
blob70bad00568503d6bd63186fe63c9ad76c44ea044
1 #include "config.h"
2 #include "ntdb.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"
10 int main(int argc, char *argv[])
12 unsigned int i;
13 struct ntdb_context *ntdb, *ntdb2;
14 NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
15 NTDB_DATA data = { (unsigned char *)&i, sizeof(i) };
16 NTDB_DATA d = { NULL, 0 }; /* Bogus GCC warning */
17 int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
18 NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
20 plan_tests(sizeof(flags) / sizeof(flags[0]) * 28);
21 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
22 ntdb = ntdb_open("run-open-multiple-times.ntdb", flags[i],
23 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
24 ok1(ntdb);
25 if (!ntdb)
26 continue;
28 ntdb2 = ntdb_open("run-open-multiple-times.ntdb", flags[i],
29 O_RDWR|O_CREAT, 0600, &tap_log_attr);
30 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
31 ok1(ntdb_check(ntdb2, NULL, NULL) == 0);
33 /* Store in one, fetch in the other. */
34 ok1(ntdb_store(ntdb, key, data, NTDB_REPLACE) == 0);
35 ok1(ntdb_fetch(ntdb2, key, &d) == NTDB_SUCCESS);
36 ok1(ntdb_deq(d, data));
37 free(d.dptr);
39 /* Vice versa, with delete. */
40 ok1(ntdb_delete(ntdb2, key) == 0);
41 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_ERR_NOEXIST);
43 /* OK, now close first one, check second still good. */
44 ok1(ntdb_close(ntdb) == 0);
46 ok1(ntdb_store(ntdb2, key, data, NTDB_REPLACE) == 0);
47 ok1(ntdb_fetch(ntdb2, key, &d) == NTDB_SUCCESS);
48 ok1(ntdb_deq(d, data));
49 free(d.dptr);
51 /* Reopen */
52 ntdb = ntdb_open("run-open-multiple-times.ntdb", flags[i],
53 O_RDWR|O_CREAT, 0600, &tap_log_attr);
54 ok1(ntdb);
56 ok1(ntdb_transaction_start(ntdb2) == 0);
58 /* Anything in the other one should fail. */
59 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_ERR_LOCK);
60 ok1(tap_log_messages == 1);
61 ok1(ntdb_store(ntdb, key, data, NTDB_REPLACE) == NTDB_ERR_LOCK);
62 ok1(tap_log_messages == 2);
63 ok1(ntdb_transaction_start(ntdb) == NTDB_ERR_LOCK);
64 ok1(tap_log_messages == 3);
65 ok1(ntdb_chainlock(ntdb, key) == NTDB_ERR_LOCK);
66 ok1(tap_log_messages == 4);
68 /* Transaciton should work as normal. */
69 ok1(ntdb_store(ntdb2, key, data, NTDB_REPLACE) == NTDB_SUCCESS);
71 /* Now... try closing with locks held. */
72 ok1(ntdb_close(ntdb2) == 0);
74 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
75 ok1(ntdb_deq(d, data));
76 free(d.dptr);
77 ok1(ntdb_close(ntdb) == 0);
78 ok1(tap_log_messages == 4);
79 tap_log_messages = 0;
82 return exit_status();