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