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