Fix bug 10025 - Lack of Sanity Checking in calls to malloc()/calloc().
[Samba.git] / lib / tdb / test / run-transaction-expand.c
blobd62c76a88cfcc321d597f0e063e9ff4948c4fb91
1 #include "../common/tdb_private.h"
3 /* Speed up the tests, but do the actual sync tests. */
4 static unsigned int sync_counts = 0;
5 static inline int fake_fsync(int fd)
7 sync_counts++;
8 return 0;
10 #define fsync fake_fsync
12 #ifdef MS_SYNC
13 static inline int fake_msync(void *addr, size_t length, int flags)
15 sync_counts++;
16 return 0;
18 #define msync fake_msync
19 #endif
21 #ifdef HAVE_FDATASYNC
22 static inline int fake_fdatasync(int fd)
24 sync_counts++;
25 return 0;
27 #define fdatasync fake_fdatasync
28 #endif
30 #include "../common/io.c"
31 #include "../common/tdb.c"
32 #include "../common/lock.c"
33 #include "../common/freelist.c"
34 #include "../common/traverse.c"
35 #include "../common/transaction.c"
36 #include "../common/error.c"
37 #include "../common/open.c"
38 #include "../common/check.c"
39 #include "../common/hash.c"
40 #include "tap-interface.h"
41 #include <stdlib.h>
42 #include "logging.h"
44 static void write_record(struct tdb_context *tdb, size_t extra_len,
45 TDB_DATA *data)
47 TDB_DATA key;
48 key.dsize = strlen("hi");
49 key.dptr = (void *)"hi";
51 data->dsize += extra_len;
52 tdb_transaction_start(tdb);
53 tdb_store(tdb, key, *data, TDB_REPLACE);
54 tdb_transaction_commit(tdb);
57 int main(int argc, char *argv[])
59 struct tdb_context *tdb;
60 size_t i;
61 TDB_DATA data;
62 struct tdb_record rec;
63 tdb_off_t off;
65 /* Do *not* suppress sync for this test; we do it ourselves. */
66 unsetenv("TDB_NO_FSYNC");
68 plan_tests(5);
69 tdb = tdb_open_ex("run-transaction-expand.tdb",
70 1024, TDB_CLEAR_IF_FIRST,
71 O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
72 ok1(tdb);
74 data.dsize = 0;
75 data.dptr = calloc(1000, getpagesize());
76 if (data.dptr == NULL) {
77 diag("Unable to allocate memory for data.dptr");
78 tdb_close(tdb);
79 exit(1);
82 /* Simulate a slowly growing record. */
83 for (i = 0; i < 1000; i++)
84 write_record(tdb, getpagesize(), &data);
86 tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &off);
87 tdb_read(tdb, off, &rec, sizeof(rec), DOCONV());
88 diag("TDB size = %zu, recovery = %llu-%llu",
89 (size_t)tdb->map_size, (unsigned long long)off, (unsigned long long)(off + sizeof(rec) + rec.rec_len));
91 /* We should only be about 5 times larger than largest record. */
92 ok1(tdb->map_size < 6 * i * getpagesize());
93 tdb_close(tdb);
95 tdb = tdb_open_ex("run-transaction-expand.tdb",
96 1024, TDB_CLEAR_IF_FIRST,
97 O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
98 ok1(tdb);
100 data.dsize = 0;
102 /* Simulate a slowly growing record, repacking to keep
103 * recovery area at end. */
104 for (i = 0; i < 1000; i++) {
105 write_record(tdb, getpagesize(), &data);
106 if (i % 10 == 0)
107 tdb_repack(tdb);
110 tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &off);
111 tdb_read(tdb, off, &rec, sizeof(rec), DOCONV());
112 diag("TDB size = %zu, recovery = %llu-%llu",
113 (size_t)tdb->map_size, (unsigned long long)off, (unsigned long long)(off + sizeof(rec) + rec.rec_len));
115 /* We should only be about 4 times larger than largest record. */
116 ok1(tdb->map_size < 5 * i * getpagesize());
118 /* We should have synchronized multiple times. */
119 ok1(sync_counts);
120 tdb_close(tdb);
121 free(data.dptr);
123 return exit_status();