]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-wronghash-fail.c
tdb2: make TDB1 code use tdb2's TDB_ERROR and tdb_logerr()
[ccan] / ccan / tdb2 / test / run-tdb1-wronghash-fail.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include <stdlib.h>
4 #include <err.h>
5
6 static void log_fn(struct tdb1_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 int main(int argc, char *argv[])
15 {
16         struct tdb1_context *tdb;
17         unsigned int log_count;
18         TDB1_DATA d;
19         struct tdb1_logging_context log_ctx = { log_fn, &log_count };
20
21         plan_tests(28);
22
23         /* Create with default hash. */
24         log_count = 0;
25         tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0,
26                           O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx, NULL);
27         ok1(tdb);
28         ok1(log_count == 0);
29         d.dptr = (void *)"Hello";
30         d.dsize = 5;
31         ok1(tdb1_store(tdb, d, d, TDB1_INSERT) == 0);
32         tdb1_close(tdb);
33
34         /* Fail to open with different hash. */
35         tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
36                           &log_ctx, tdb1_jenkins_hash);
37         ok1(!tdb);
38         ok1(log_count == 1);
39
40         /* Create with different hash. */
41         log_count = 0;
42         tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0,
43                           O_CREAT|O_RDWR|O_TRUNC,
44                           0600, &log_ctx, tdb1_jenkins_hash);
45         ok1(tdb);
46         ok1(log_count == 0);
47         tdb1_close(tdb);
48
49         /* Endian should be no problem. */
50         log_count = 0;
51         tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDWR, 0,
52                           &log_ctx, tdb1_old_hash);
53         ok1(!tdb);
54         ok1(log_count == 1);
55
56         log_count = 0;
57         tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDWR, 0,
58                           &log_ctx, tdb1_old_hash);
59         ok1(!tdb);
60         ok1(log_count == 1);
61
62         log_count = 0;
63         /* Fail to open with old default hash. */
64         tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
65                           &log_ctx, tdb1_old_hash);
66         ok1(!tdb);
67         ok1(log_count == 1);
68
69         log_count = 0;
70         tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDONLY,
71                           0, &log_ctx, tdb1_jenkins_hash);
72         ok1(tdb);
73         ok1(log_count == 0);
74         ok1(tdb1_check(tdb, NULL, NULL) == 0);
75         tdb1_close(tdb);
76
77         log_count = 0;
78         tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDONLY,
79                           0, &log_ctx, tdb1_jenkins_hash);
80         ok1(tdb);
81         ok1(log_count == 0);
82         ok1(tdb1_check(tdb, NULL, NULL) == 0);
83         tdb1_close(tdb);
84
85         /* It should open with jenkins hash if we don't specify. */
86         log_count = 0;
87         tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDWR, 0,
88                           &log_ctx, NULL);
89         ok1(tdb);
90         ok1(log_count == 0);
91         ok1(tdb1_check(tdb, NULL, NULL) == 0);
92         tdb1_close(tdb);
93
94         log_count = 0;
95         tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDWR, 0,
96                           &log_ctx, NULL);
97         ok1(tdb);
98         ok1(log_count == 0);
99         ok1(tdb1_check(tdb, NULL, NULL) == 0);
100         tdb1_close(tdb);
101
102         log_count = 0;
103         tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDONLY,
104                           0, &log_ctx, NULL);
105         ok1(tdb);
106         ok1(log_count == 0);
107         ok1(tdb1_check(tdb, NULL, NULL) == 0);
108         tdb1_close(tdb);
109
110
111         return exit_status();
112 }