lib/tdb2: fix -Wcast-qual warnings.
[kai/samba-autobuild/.git] / lib / tdb2 / test / run-tdb1-wronghash-fail.c
1 #include "tdb2-source.h"
2 #include "tap-interface.h"
3 #include <stdlib.h>
4 #include <err.h>
5
6 static void log_fn(struct tdb_context *tdb, enum tdb_log_level level,
7                    enum TDB_ERROR ecode, const char *message, void *priv)
8 {
9         unsigned int *count = priv;
10         if (strstr(message, "hash"))
11                 (*count)++;
12 }
13
14 static uint64_t jenkins_hashfn(const void *key, size_t len, uint64_t seed,
15                                void *unused)
16 {
17         return hashlittle(key, len);
18 }
19
20 /* the tdb1_old_hash function is "magic" as it automatically makes us test the
21  * tdb1_incompatible_hash as well, so use this wrapper. */
22 static uint64_t old_hash(const void *key, size_t len, uint64_t seed,
23                          void *unused)
24 {
25         return tdb1_old_hash(key, len, seed, unused);
26 }
27
28 int main(int argc, char *argv[])
29 {
30         struct tdb_context *tdb;
31         unsigned int log_count;
32         TDB_DATA d;
33         union tdb_attribute log_attr, jhash_attr, ohash_attr,
34                 incompat_hash_attr;
35
36         log_attr.base.attr = TDB_ATTRIBUTE_LOG;
37         log_attr.base.next = NULL;
38         log_attr.log.fn = log_fn;
39         log_attr.log.data = &log_count;
40
41         jhash_attr.base.attr = TDB_ATTRIBUTE_HASH;
42         jhash_attr.base.next = &log_attr;
43         jhash_attr.hash.fn = jenkins_hashfn;
44
45         ohash_attr.base.attr = TDB_ATTRIBUTE_HASH;
46         ohash_attr.base.next = &log_attr;
47         ohash_attr.hash.fn = old_hash;
48
49         incompat_hash_attr.base.attr = TDB_ATTRIBUTE_HASH;
50         incompat_hash_attr.base.next = &log_attr;
51         incompat_hash_attr.hash.fn = tdb1_incompatible_hash;
52
53         plan_tests(28);
54
55         /* Create with default hash. */
56         log_count = 0;
57         tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1,
58                        O_CREAT|O_RDWR|O_TRUNC, 0600, &log_attr);
59         ok1(tdb);
60         ok1(log_count == 0);
61         d = tdb_mkdata("Hello", strlen("Hello"));
62         ok1(tdb_store(tdb, d, d, TDB_INSERT) == TDB_SUCCESS);
63         tdb_close(tdb);
64
65         /* Fail to open with different hash. */
66         tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1, O_RDWR, 0,
67                        &jhash_attr);
68         ok1(!tdb);
69         ok1(log_count == 1);
70
71         /* Create with different hash. */
72         log_count = 0;
73         tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1,
74                        O_CREAT|O_RDWR|O_TRUNC, 0600, &jhash_attr);
75         ok1(tdb);
76         ok1(log_count == 0);
77         tdb_close(tdb);
78
79         /* Endian should be no problem. */
80         log_count = 0;
81         tdb = tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1, O_RDWR, 0,
82                        &ohash_attr);
83         ok1(!tdb);
84         ok1(log_count == 1);
85
86         log_count = 0;
87         tdb = tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1, O_RDWR, 0,
88                        &ohash_attr);
89         ok1(!tdb);
90         ok1(log_count == 1);
91
92         log_count = 0;
93         /* Fail to open with old default hash. */
94         tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1, O_RDWR, 0,
95                        &ohash_attr);
96         ok1(!tdb);
97         ok1(log_count == 1);
98
99         log_count = 0;
100         tdb = tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1, O_RDONLY,
101                        0, &incompat_hash_attr);
102         ok1(tdb);
103         ok1(log_count == 0);
104         ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
105         tdb_close(tdb);
106
107         log_count = 0;
108         tdb = tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1, O_RDONLY,
109                        0, &incompat_hash_attr);
110         ok1(tdb);
111         ok1(log_count == 0);
112         ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
113         tdb_close(tdb);
114
115         /* It should open with jenkins hash if we don't specify. */
116         log_count = 0;
117         tdb = tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1, O_RDWR, 0,
118                        &log_attr);
119         ok1(tdb);
120         ok1(log_count == 0);
121         ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
122         tdb_close(tdb);
123
124         log_count = 0;
125         tdb = tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1, O_RDWR, 0,
126                        &log_attr);
127         ok1(tdb);
128         ok1(log_count == 0);
129         ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
130         tdb_close(tdb);
131
132         log_count = 0;
133         tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1, O_RDONLY,
134                        0, &log_attr);
135         ok1(tdb);
136         ok1(log_count == 0);
137         ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
138         tdb_close(tdb);
139
140
141         return exit_status();
142 }