docs: Update man 7 samba.
[Samba/gebeck_regimport.git] / lib / ntdb / test / api-20-alloc-attr.c
blobe4ec89a334f02720906d8e28207adb50e994c1c5
1 #include "config.h"
2 #include "ntdb.h"
3 #include "tap-interface.h"
4 #include <ccan/hash/hash.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <assert.h>
10 #include "logging.h"
12 static const struct ntdb_context *curr_ntdb;
13 static const struct ntdb_file *curr_file;
15 static int owner_null_count,
16 owner_weird_count, alloc_count, free_count, expand_count;
18 static void *test_alloc(const void *owner, size_t len, void *priv_data)
20 void *ret;
22 if (!owner) {
23 owner_null_count++;
24 } else if (owner != curr_ntdb && owner != curr_file) {
25 owner_weird_count++;
28 alloc_count++;
29 ret = malloc(len);
31 /* The first time, this is the current ntdb, next is
32 * for the file struct. */
33 if (!owner) {
34 if (!curr_ntdb) {
35 curr_ntdb = ret;
36 } else if (!curr_file) {
37 curr_file = ret;
40 assert(priv_data == &owner_weird_count);
41 return ret;
44 static void *test_expand(void *old, size_t newlen, void *priv_data)
46 expand_count++;
48 assert(priv_data == &owner_weird_count);
49 return realloc(old, newlen);
52 static void test_free(void *old, void *priv_data)
54 assert(priv_data == &owner_weird_count);
55 if (old) {
56 free_count++;
58 free(old);
61 int main(int argc, char *argv[])
63 unsigned int i, j;
64 union ntdb_attribute alloc_attr;
65 struct ntdb_context *ntdb;
66 int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
67 NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
68 NTDB_NOMMAP|NTDB_CONVERT };
69 NTDB_DATA key = { (unsigned char *)&j, sizeof(j) };
70 NTDB_DATA data = { (unsigned char *)&j, sizeof(j) };
72 alloc_attr.base.next = &tap_log_attr;
73 alloc_attr.base.attr = NTDB_ATTRIBUTE_ALLOCATOR;
75 alloc_attr.alloc.alloc = test_alloc;
76 alloc_attr.alloc.expand = test_expand;
77 alloc_attr.alloc.free = test_free;
78 alloc_attr.alloc.priv_data = &owner_weird_count;
80 plan_tests(sizeof(flags) / sizeof(flags[0]) * (1 + 700 * 3 + 4) + 1);
82 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
83 curr_ntdb = NULL;
84 curr_file = NULL;
85 ntdb = ntdb_open("run-12-store.ntdb", flags[i]|MAYBE_NOSYNC,
86 O_RDWR|O_CREAT|O_TRUNC, 0600, &alloc_attr);
87 ok1(ntdb);
88 if (!ntdb)
89 continue;
91 for (j = 0; j < 700; j++) {
92 NTDB_DATA d = { NULL, 0 }; /* Bogus GCC warning */
93 ok1(ntdb_store(ntdb, key, data, NTDB_REPLACE) == 0);
94 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
95 ok1(ntdb_deq(d, data));
96 test_free(d.dptr, &owner_weird_count);
98 ntdb_close(ntdb);
100 ok1(owner_null_count == 2+i*2);
101 ok1(owner_weird_count == 0);
102 ok1(alloc_count == free_count);
103 ok1(expand_count != 0);
106 ok1(tap_log_messages == 0);
107 return exit_status();