6a146cb1cf7361f2c06d1021fe7a90553721a5ab
[kai/samba-autobuild/.git] / lib / ntdb / test / run-64-bit-tdb.c
1 #include "ntdb-source.h"
2 #include "tap-interface.h"
3 #include "logging.h"
4
5 int main(int argc, char *argv[])
6 {
7         unsigned int i;
8         struct ntdb_context *ntdb;
9         int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
10                         NTDB_CONVERT,
11                         NTDB_NOMMAP|NTDB_CONVERT };
12
13         if (sizeof(off_t) <= 4) {
14                 plan_tests(1);
15                 pass("No 64 bit off_t");
16                 return exit_status();
17         }
18
19         plan_tests(sizeof(flags) / sizeof(flags[0]) * 14);
20         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
21                 off_t old_size;
22                 NTDB_DATA k, d;
23                 struct hash_info h;
24                 struct ntdb_used_record rec;
25                 ntdb_off_t off;
26
27                 ntdb = ntdb_open("run-64-bit-ntdb.ntdb", flags[i],
28                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
29                 ok1(ntdb);
30                 if (!ntdb)
31                         continue;
32
33                 old_size = ntdb->file->map_size;
34
35                 /* This makes a sparse file */
36                 ok1(ftruncate(ntdb->file->fd, 0xFFFFFFF0) == 0);
37                 ok1(add_free_record(ntdb, old_size, 0xFFFFFFF0 - old_size,
38                                     NTDB_LOCK_WAIT, false) == NTDB_SUCCESS);
39
40                 /* Now add a little record past the 4G barrier. */
41                 ok1(ntdb_expand_file(ntdb, 100) == NTDB_SUCCESS);
42                 ok1(add_free_record(ntdb, 0xFFFFFFF0, 100, NTDB_LOCK_WAIT, false)
43                     == NTDB_SUCCESS);
44
45                 ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
46
47                 /* Test allocation path. */
48                 k = ntdb_mkdata("key", 4);
49                 d = ntdb_mkdata("data", 5);
50                 ok1(ntdb_store(ntdb, k, d, NTDB_INSERT) == 0);
51                 ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
52
53                 /* Make sure it put it at end as we expected. */
54                 off = find_and_lock(ntdb, k, F_RDLCK, &h, &rec, NULL);
55                 ok1(off >= 0xFFFFFFF0);
56                 ntdb_unlock_hashes(ntdb, h.hlock_start, h.hlock_range, F_RDLCK);
57
58                 ok1(ntdb_fetch(ntdb, k, &d) == 0);
59                 ok1(d.dsize == 5);
60                 ok1(strcmp((char *)d.dptr, "data") == 0);
61                 free(d.dptr);
62
63                 ok1(ntdb_delete(ntdb, k) == 0);
64                 ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
65
66                 ntdb_close(ntdb);
67         }
68
69         /* We might get messages about mmap failing, so don't test
70          * tap_log_messages */
71         return exit_status();
72 }