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