tdb2: remove double-open detection for TDB1 databases.
[ccan] / ccan / tdb2 / test / run-90-get-set-attributes.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include "logging.h"
4
5 static int mylock(int fd, int rw, off_t off, off_t len, bool waitflag,
6                   void *unused)
7 {
8         return 0;
9 }
10
11 static int myunlock(int fd, int rw, off_t off, off_t len, void *unused)
12 {
13         return 0;
14 }
15
16 static uint64_t hash_fn(const void *key, size_t len, uint64_t seed,
17                         void *priv)
18 {
19         return 0;
20 }
21
22 int main(int argc, char *argv[])
23 {
24         unsigned int i;
25         struct tdb_context *tdb;
26         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
27                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
28         union tdb_attribute seed_attr;
29         union tdb_attribute hash_attr;
30         union tdb_attribute lock_attr;
31
32         hash_attr.base.attr = TDB_ATTRIBUTE_HASH;
33         hash_attr.base.next = &seed_attr;
34         hash_attr.hash.fn = hash_fn;
35         hash_attr.hash.data = &hash_attr;
36
37         seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
38         seed_attr.base.next = &lock_attr;
39         seed_attr.seed.seed = 100;
40
41         lock_attr.base.attr = TDB_ATTRIBUTE_FLOCK;
42         lock_attr.base.next = &tap_log_attr;
43         lock_attr.flock.lock = mylock;
44         lock_attr.flock.unlock = myunlock;
45         lock_attr.flock.data = &lock_attr;
46
47         plan_tests(sizeof(flags) / sizeof(flags[0]) * 50);
48
49         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
50                 union tdb_attribute attr;
51
52                 /* First open with no attributes. */
53                 tdb = tdb_open("run-90-get-set-attributes.tdb", flags[i],
54                                O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
55                 ok1(tdb);
56
57                 /* Get log on no attributes will fail */
58                 attr.base.attr = TDB_ATTRIBUTE_LOG;
59                 ok1(tdb_get_attribute(tdb, &attr) == TDB_ERR_NOEXIST);
60                 /* These always work. */
61                 attr.base.attr = TDB_ATTRIBUTE_HASH;
62                 ok1(tdb_get_attribute(tdb, &attr) == 0);
63                 ok1(attr.base.attr == TDB_ATTRIBUTE_HASH);
64                 ok1(attr.hash.fn == jenkins_hash);
65                 attr.base.attr = TDB_ATTRIBUTE_FLOCK;
66                 ok1(tdb_get_attribute(tdb, &attr) == 0);
67                 ok1(attr.base.attr == TDB_ATTRIBUTE_FLOCK);
68                 ok1(attr.flock.lock == tdb_fcntl_lock);
69                 ok1(attr.flock.unlock == tdb_fcntl_unlock);
70                 attr.base.attr = TDB_ATTRIBUTE_SEED;
71                 ok1(tdb_get_attribute(tdb, &attr) == 0);
72                 ok1(attr.base.attr == TDB_ATTRIBUTE_SEED);
73                 /* This is possible, just astronomically unlikely. */
74                 ok1(attr.seed.seed != 0);
75
76                 /* Unset attributes. */
77                 tdb_unset_attribute(tdb, TDB_ATTRIBUTE_LOG);
78                 tdb_unset_attribute(tdb, TDB_ATTRIBUTE_FLOCK);
79
80                 /* Set them. */
81                 ok1(tdb_set_attribute(tdb, &tap_log_attr) == 0);
82                 ok1(tdb_set_attribute(tdb, &lock_attr) == 0);
83                 /* These should fail. */
84                 ok1(tdb_set_attribute(tdb, &seed_attr) == TDB_ERR_EINVAL);
85                 ok1(tap_log_messages == 1);
86                 ok1(tdb_set_attribute(tdb, &hash_attr) == TDB_ERR_EINVAL);
87                 ok1(tap_log_messages == 2);
88                 tap_log_messages = 0;
89
90                 /* Getting them should work as expected. */
91                 attr.base.attr = TDB_ATTRIBUTE_LOG;
92                 ok1(tdb_get_attribute(tdb, &attr) == 0);
93                 ok1(attr.base.attr == TDB_ATTRIBUTE_LOG);
94                 ok1(attr.log.fn == tap_log_attr.log.fn);
95                 ok1(attr.log.data == tap_log_attr.log.data);
96
97                 attr.base.attr = TDB_ATTRIBUTE_FLOCK;
98                 ok1(tdb_get_attribute(tdb, &attr) == 0);
99                 ok1(attr.base.attr == TDB_ATTRIBUTE_FLOCK);
100                 ok1(attr.flock.lock == mylock);
101                 ok1(attr.flock.unlock == myunlock);
102                 ok1(attr.flock.data == &lock_attr);
103
104                 /* Unset them again. */
105                 tdb_unset_attribute(tdb, TDB_ATTRIBUTE_FLOCK);
106                 ok1(tap_log_messages == 0);
107                 tdb_unset_attribute(tdb, TDB_ATTRIBUTE_LOG);
108                 ok1(tap_log_messages == 0);
109
110                 tdb_close(tdb);
111                 ok1(tap_log_messages == 0);
112
113                 /* Now open with all attributes. */
114                 tdb = tdb_open("run-90-get-set-attributes.tdb", flags[i],
115                                O_RDWR|O_CREAT|O_TRUNC, 0600, &hash_attr);
116                 ok1(tdb);
117
118                 /* Get will succeed */
119                 attr.base.attr = TDB_ATTRIBUTE_LOG;
120                 ok1(tdb_get_attribute(tdb, &attr) == 0);
121                 ok1(attr.base.attr == TDB_ATTRIBUTE_LOG);
122                 ok1(attr.log.fn == tap_log_attr.log.fn);
123                 ok1(attr.log.data == tap_log_attr.log.data);
124
125                 attr.base.attr = TDB_ATTRIBUTE_HASH;
126                 ok1(tdb_get_attribute(tdb, &attr) == 0);
127                 ok1(attr.base.attr == TDB_ATTRIBUTE_HASH);
128                 ok1(attr.hash.fn == hash_fn);
129                 ok1(attr.hash.data == &hash_attr);
130
131                 attr.base.attr = TDB_ATTRIBUTE_FLOCK;
132                 ok1(tdb_get_attribute(tdb, &attr) == 0);
133                 ok1(attr.base.attr == TDB_ATTRIBUTE_FLOCK);
134                 ok1(attr.flock.lock == mylock);
135                 ok1(attr.flock.unlock == myunlock);
136                 ok1(attr.flock.data == &lock_attr);
137
138                 attr.base.attr = TDB_ATTRIBUTE_SEED;
139                 ok1(tdb_get_attribute(tdb, &attr) == 0);
140                 ok1(attr.base.attr == TDB_ATTRIBUTE_SEED);
141                 ok1(attr.seed.seed == seed_attr.seed.seed);
142
143                 /* Unset attributes. */
144                 tdb_unset_attribute(tdb, TDB_ATTRIBUTE_HASH);
145                 ok1(tap_log_messages == 1);
146                 tdb_unset_attribute(tdb, TDB_ATTRIBUTE_SEED);
147                 ok1(tap_log_messages == 2);
148                 tdb_unset_attribute(tdb, TDB_ATTRIBUTE_FLOCK);
149                 tdb_unset_attribute(tdb, TDB_ATTRIBUTE_LOG);
150                 ok1(tap_log_messages == 2);
151                 tap_log_messages = 0;
152
153                 tdb_close(tdb);
154
155         }
156         return exit_status();
157 }