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