1 #include <ccan/tdb2/tdb2.h>
2 #include <ccan/tap/tap.h>
9 int main(int argc
, char *argv
[])
11 unsigned int i
, extra_messages
;
12 struct tdb_context
*tdb
, *tdb2
;
13 struct tdb_data key
= { (unsigned char *)&i
, sizeof(i
) };
14 struct tdb_data data
= { (unsigned char *)&i
, sizeof(i
) };
15 struct tdb_data d
= { NULL
, 0 }; /* Bogus GCC warning */
16 int flags
[] = { TDB_DEFAULT
, TDB_NOMMAP
,
17 TDB_CONVERT
, TDB_NOMMAP
|TDB_CONVERT
,
18 TDB_VERSION1
, TDB_NOMMAP
|TDB_VERSION1
,
19 TDB_CONVERT
|TDB_VERSION1
,
20 TDB_NOMMAP
|TDB_CONVERT
|TDB_VERSION1
};
22 plan_tests(sizeof(flags
) / sizeof(flags
[0]) * 28);
23 for (i
= 0; i
< sizeof(flags
) / sizeof(flags
[0]); i
++) {
24 tdb
= tdb_open("run-open-multiple-times.tdb", flags
[i
],
25 O_RDWR
|O_CREAT
|O_TRUNC
, 0600, &tap_log_attr
);
30 if (flags
[i
] & TDB_VERSION1
) {
35 tdb2
= tdb_open("run-open-multiple-times.tdb", flags
[i
],
36 O_RDWR
|O_CREAT
, 0600, &tap_log_attr
);
37 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
38 ok1(tdb_check(tdb2
, NULL
, NULL
) == 0);
40 /* Store in one, fetch in the other. */
41 ok1(tdb_store(tdb
, key
, data
, TDB_REPLACE
) == 0);
42 ok1(tdb_fetch(tdb2
, key
, &d
) == TDB_SUCCESS
);
43 ok1(tdb_deq(d
, data
));
46 /* Vice versa, with delete. */
47 ok1(tdb_delete(tdb2
, key
) == 0);
48 ok1(tdb_fetch(tdb
, key
, &d
) == TDB_ERR_NOEXIST
);
50 /* OK, now close first one, check second still good. */
51 ok1(tdb_close(tdb
) == 0);
53 ok1(tdb_store(tdb2
, key
, data
, TDB_REPLACE
) == 0);
54 ok1(tdb_fetch(tdb2
, key
, &d
) == TDB_SUCCESS
);
55 ok1(tdb_deq(d
, data
));
59 tdb
= tdb_open("run-open-multiple-times.tdb", flags
[i
],
60 O_RDWR
|O_CREAT
, 0600, &tap_log_attr
);
63 ok1(tdb_transaction_start(tdb2
) == 0);
65 /* Anything in the other one should fail. */
66 ok1(tdb_fetch(tdb
, key
, &d
) == TDB_ERR_LOCK
);
67 tap_log_messages
-= extra_messages
;
68 ok1(tap_log_messages
== 1);
69 ok1(tdb_store(tdb
, key
, data
, TDB_REPLACE
) == TDB_ERR_LOCK
);
70 tap_log_messages
-= extra_messages
;
71 ok1(tap_log_messages
== 2);
72 ok1(tdb_transaction_start(tdb
) == TDB_ERR_LOCK
);
73 ok1(tap_log_messages
== 3);
74 ok1(tdb_chainlock(tdb
, key
) == TDB_ERR_LOCK
);
75 tap_log_messages
-= extra_messages
;
76 ok1(tap_log_messages
== 4);
78 /* Transaciton should work as normal. */
79 ok1(tdb_store(tdb2
, key
, data
, TDB_REPLACE
) == TDB_SUCCESS
);
81 /* Now... try closing with locks held. */
82 ok1(tdb_close(tdb2
) == 0);
84 ok1(tdb_fetch(tdb
, key
, &d
) == TDB_SUCCESS
);
85 ok1(tdb_deq(d
, data
));
87 ok1(tdb_close(tdb
) == 0);
88 ok1(tap_log_messages
== 4);