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