strsplit: remove nump argument
[ccan] / ccan / tdb / test / run-bad-tdb-header.c
1 #define _XOPEN_SOURCE 500
2 #include <ccan/tdb/tdb.h>
3 #include <ccan/tdb/io.c>
4 #include <ccan/tdb/tdb.c>
5 #include <ccan/tdb/lock.c>
6 #include <ccan/tdb/freelist.c>
7 #include <ccan/tdb/traverse.c>
8 #include <ccan/tdb/transaction.c>
9 #include <ccan/tdb/error.c>
10 #include <ccan/tdb/open.c>
11 #include <ccan/tdb/check.c>
12 #include <ccan/tdb/hash.c>
13 #include <ccan/tap/tap.h>
14 #include <stdlib.h>
15 #include <err.h>
16 #include "logging.h"
17
18 int main(int argc, char *argv[])
19 {
20         struct tdb_context *tdb;
21         struct tdb_header hdr;
22         int fd;
23
24         plan_tests(11);
25         /* Can open fine if complete crap, as long as O_CREAT. */
26         fd = open("run-bad-tdb-header.tdb", O_RDWR|O_CREAT|O_TRUNC, 0600);
27         ok1(fd >= 0);
28         ok1(write(fd, "hello world", 11) == 11);
29         close(fd);
30         tdb = tdb_open_ex("run-bad-tdb-header.tdb", 1024, 0, O_RDWR, 0,
31                           &taplogctx, NULL);
32         ok1(!tdb);
33         tdb = tdb_open_ex("run-bad-tdb-header.tdb", 1024, 0, O_CREAT|O_RDWR,
34                           0600, &taplogctx, NULL);
35         ok1(tdb);
36         tdb_close(tdb);
37
38         /* Now, with wrong version it should *not* overwrite. */
39         fd = open("run-bad-tdb-header.tdb", O_RDWR);
40         ok1(fd >= 0);
41         ok1(read(fd, &hdr, sizeof(hdr)) == sizeof(hdr));
42         ok1(hdr.version == TDB_VERSION);
43         hdr.version++;
44         lseek(fd, 0, SEEK_SET);
45         ok1(write(fd, &hdr, sizeof(hdr)) == sizeof(hdr));
46         close(fd);
47
48         tdb = tdb_open_ex("run-bad-tdb-header.tdb", 1024, 0, O_RDWR|O_CREAT,
49                           0600, &taplogctx, NULL);
50         ok1(errno == EIO);
51         ok1(!tdb);
52
53         /* With truncate, will be fine. */
54         tdb = tdb_open_ex("run-bad-tdb-header.tdb", 1024, 0,
55                           O_RDWR|O_CREAT|O_TRUNC, 0600, &taplogctx, NULL);
56         ok1(tdb);
57         tdb_close(tdb);
58
59         return exit_status();
60 }