lib/tdb2: adapt unit tests to SAMBA environment.
[kai/samba-autobuild/.git] / lib / tdb2 / test / run-tdb1-bad-tdb-header.c
1 #include "tdb2-source.h"
2 #include "tap-interface.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         /* Cannot open fine if complete crap, even with O_CREAT. */
20         fd = open("run-bad-tdb-header.tdb1", O_RDWR|O_CREAT|O_TRUNC, 0600);
21         ok1(fd >= 0);
22         ok1(write(fd, "hello world", 11) == 11);
23         close(fd);
24         tdb = tdb_open("run-bad-tdb-header.tdb1", 0, O_RDWR, 0, &tap_log_attr);
25         ok1(!tdb);
26         tdb = tdb_open("run-bad-tdb-header.tdb1", 0, O_CREAT|O_RDWR,
27                         0600, &hsize);
28         ok1(!tdb);
29
30         /* With truncate, will be fine. */
31         tdb = tdb_open("run-bad-tdb-header.tdb1", TDB_VERSION1,
32                        O_RDWR|O_CREAT|O_TRUNC, 0600, &hsize);
33         ok1(tdb);
34         tdb_close(tdb);
35
36         /* Now, with wrong version it should *not* overwrite. */
37         fd = open("run-bad-tdb-header.tdb1", O_RDWR);
38         ok1(fd >= 0);
39         ok1(read(fd, &hdr, sizeof(hdr)) == sizeof(hdr));
40         ok1(hdr.version == TDB1_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("run-bad-tdb-header.tdb1", TDB_VERSION1, O_RDWR|O_CREAT,
47                        0600, &hsize);
48         ok1(errno == EIO);
49         ok1(!tdb);
50
51         return exit_status();
52 }