s3:registry: do not use regdb functions during db upgrade
[Samba/gebeck_regimport.git] / lib / tdb2 / test / run-tdb1-wronghash-fail.c
blob63c1bdf1e46b637ded5ee05bb2fc5742d01ea79f
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include <stdlib.h>
4 #include <err.h>
6 static void log_fn(struct tdb_context *tdb, enum tdb_log_level level,
7 enum TDB_ERROR ecode, const char *message, void *priv)
9 unsigned int *count = priv;
10 if (strstr(message, "hash"))
11 (*count)++;
14 static uint64_t jenkins_hashfn(const void *key, size_t len, uint64_t seed,
15 void *unused)
17 return hashlittle(key, len);
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)
25 return tdb1_old_hash(key, len, seed, unused);
28 int main(int argc, char *argv[])
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;
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;
41 jhash_attr.base.attr = TDB_ATTRIBUTE_HASH;
42 jhash_attr.base.next = &log_attr;
43 jhash_attr.hash.fn = jenkins_hashfn;
45 ohash_attr.base.attr = TDB_ATTRIBUTE_HASH;
46 ohash_attr.base.next = &log_attr;
47 ohash_attr.hash.fn = old_hash;
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;
53 plan_tests(28);
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.dptr = (void *)"Hello";
62 d.dsize = 5;
63 ok1(tdb_store(tdb, d, d, TDB_INSERT) == TDB_SUCCESS);
64 tdb_close(tdb);
66 /* Fail to open with different hash. */
67 tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1, O_RDWR, 0,
68 &jhash_attr);
69 ok1(!tdb);
70 ok1(log_count == 1);
72 /* Create with different hash. */
73 log_count = 0;
74 tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1,
75 O_CREAT|O_RDWR|O_TRUNC, 0600, &jhash_attr);
76 ok1(tdb);
77 ok1(log_count == 0);
78 tdb_close(tdb);
80 /* Endian should be no problem. */
81 log_count = 0;
82 tdb = tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1, O_RDWR, 0,
83 &ohash_attr);
84 ok1(!tdb);
85 ok1(log_count == 1);
87 log_count = 0;
88 tdb = tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1, O_RDWR, 0,
89 &ohash_attr);
90 ok1(!tdb);
91 ok1(log_count == 1);
93 log_count = 0;
94 /* Fail to open with old default hash. */
95 tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1, O_RDWR, 0,
96 &ohash_attr);
97 ok1(!tdb);
98 ok1(log_count == 1);
100 log_count = 0;
101 tdb = tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1, O_RDONLY,
102 0, &incompat_hash_attr);
103 ok1(tdb);
104 ok1(log_count == 0);
105 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
106 tdb_close(tdb);
108 log_count = 0;
109 tdb = tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1, O_RDONLY,
110 0, &incompat_hash_attr);
111 ok1(tdb);
112 ok1(log_count == 0);
113 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
114 tdb_close(tdb);
116 /* It should open with jenkins hash if we don't specify. */
117 log_count = 0;
118 tdb = tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1, O_RDWR, 0,
119 &log_attr);
120 ok1(tdb);
121 ok1(log_count == 0);
122 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
123 tdb_close(tdb);
125 log_count = 0;
126 tdb = tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1, O_RDWR, 0,
127 &log_attr);
128 ok1(tdb);
129 ok1(log_count == 0);
130 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
131 tdb_close(tdb);
133 log_count = 0;
134 tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1, O_RDONLY,
135 0, &log_attr);
136 ok1(tdb);
137 ok1(log_count == 0);
138 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
139 tdb_close(tdb);
142 return exit_status();