]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-bad-tdb-header.c
tdb2: add TDB_ATTRIBUTE_TDB1_HASHSIZE
[ccan] / ccan / tdb2 / test / run-tdb1-bad-tdb-header.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include <stdlib.h>
4 #include <err.h>
5 #include "logging.h"
6
7 int main(int argc, char *argv[])
8 {
9         struct tdb_context *tdb;
10         struct tdb1_header hdr;
11         int fd;
12         union tdb_attribute hsize;
13
14         hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
15         hsize.base.next = &tap_log_attr;
16         hsize.tdb1_hashsize.hsize = 1024;
17
18         plan_tests(11);
19         /* Can open fine if complete crap, as long as O_CREAT. */
20         fd = open("run-bad-tdb-header.tdb", O_RDWR|O_CREAT|O_TRUNC, 0600);
21         ok1(fd >= 0);
22         ok1(write(fd, "hello world", 11) == 11);
23         close(fd);
24         tdb = tdb1_open("run-bad-tdb-header.tdb", 0, O_RDWR, 0, &tap_log_attr);
25         ok1(!tdb);
26         tdb = tdb1_open("run-bad-tdb-header.tdb", 0, O_CREAT|O_RDWR,
27                         0600, &hsize);
28         ok1(tdb);
29         tdb1_close(tdb);
30
31         /* Now, with wrong version it should *not* overwrite. */
32         fd = open("run-bad-tdb-header.tdb", O_RDWR);
33         ok1(fd >= 0);
34         ok1(read(fd, &hdr, sizeof(hdr)) == sizeof(hdr));
35         ok1(hdr.version == TDB1_VERSION);
36         hdr.version++;
37         lseek(fd, 0, SEEK_SET);
38         ok1(write(fd, &hdr, sizeof(hdr)) == sizeof(hdr));
39         close(fd);
40
41         tdb = tdb1_open("run-bad-tdb-header.tdb", 0, O_RDWR|O_CREAT,
42                         0600, &hsize);
43         ok1(errno == EIO);
44         ok1(!tdb);
45
46         /* With truncate, will be fine. */
47         tdb = tdb1_open("run-bad-tdb-header.tdb", 0,
48                         O_RDWR|O_CREAT|O_TRUNC, 0600, &hsize);
49         ok1(tdb);
50         tdb1_close(tdb);
51
52         return exit_status();
53 }