]> git.ozlabs.org Git - ccan/blob - ccan/tdb/test/run-bad-tdb-header.c
More header fixups: now it all compiles again.
[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/tap/tap.h>
12 #include <stdlib.h>
13 #include <err.h>
14
15 int main(int argc, char *argv[])
16 {
17         struct tdb_context *tdb;
18         struct tdb_header hdr;
19         int fd;
20
21         plan_tests(11);
22         /* Can open fine if complete crap, as long as O_CREAT. */
23         fd = open("/tmp/test.tdb", O_RDWR|O_CREAT|O_TRUNC, 0600);
24         ok1(fd >= 0);
25         ok1(write(fd, "hello world", 11) == 11);
26         close(fd);
27         tdb = tdb_open("/tmp/test.tdb", 1024, 0, O_RDWR, 0);
28         ok1(!tdb);
29         tdb = tdb_open("/tmp/test.tdb", 1024, 0, O_CREAT|O_RDWR, 0600);
30         ok1(tdb);
31         tdb_close(tdb);
32
33         /* Now, with wrong version it should *not* overwrite. */
34         fd = open("/tmp/test.tdb", O_RDWR);
35         ok1(fd >= 0);
36         ok1(read(fd, &hdr, sizeof(hdr)) == sizeof(hdr));
37         ok1(hdr.version == TDB_VERSION);
38         hdr.version++;
39         lseek(fd, 0, SEEK_SET);
40         ok1(write(fd, &hdr, sizeof(hdr)) == sizeof(hdr));
41         close(fd);
42
43         tdb = tdb_open("/tmp/test.tdb", 1024, 0, O_RDWR|O_CREAT, 0600);
44         ok1(errno == EIO);
45         ok1(!tdb);
46
47         /* With truncate, will be fine. */
48         tdb = tdb_open("/tmp/test.tdb", 1024, 0, O_RDWR|O_CREAT|O_TRUNC, 0600);
49         ok1(tdb);
50         tdb_close(tdb);
51         unlink("/tmp/test.tdb");
52
53         return exit_status();
54 }