summaryrefslogtreecommitdiff
path: root/lib/ntdb/test/run-25-hashoverload.c
blob: d82b3edb3d30129d14fec308b10996e4f69c2d2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "ntdb-source.h"
#include "tap-interface.h"
#include "logging.h"

#define OVERLOAD 100

static uint32_t badhash(const void *key, size_t len, uint32_t seed, void *priv)
{
	return 0;
}

static int trav(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *p)
{
	if (p)
		return ntdb_delete(ntdb, key);
	return 0;
}

int main(int argc, char *argv[])
{
	unsigned int i, j;
	struct ntdb_context *ntdb;
	NTDB_DATA key = { (unsigned char *)&j, sizeof(j) };
	NTDB_DATA dbuf = { (unsigned char *)&j, sizeof(j) };
	union ntdb_attribute hattr = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
						.fn = badhash } };
	int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
			NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
			NTDB_NOMMAP|NTDB_CONVERT,
	};

	hattr.base.next = &tap_log_attr;

	plan_tests(sizeof(flags) / sizeof(flags[0]) * (7 * OVERLOAD + 11) + 1);
	for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
		NTDB_DATA d = { NULL, 0 }; /* Bogus GCC warning */

		ntdb = ntdb_open("run-25-hashoverload.ntdb",
				 flags[i]|MAYBE_NOSYNC,
				 O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
		ok1(ntdb);
		if (!ntdb)
			continue;

		/* Overload a bucket. */
		for (j = 0; j < OVERLOAD; j++) {
			ok1(ntdb_store(ntdb, key, dbuf, NTDB_INSERT) == 0);
		}
		ok1(ntdb_check(ntdb, NULL, NULL) == 0);

		/* Check we can find them all. */
		for (j = 0; j < OVERLOAD; j++) {
			ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
			ok1(d.dsize == sizeof(j));
			ok1(d.dptr != NULL);
			ok1(d.dptr && memcmp(d.dptr, &j, d.dsize) == 0);
			free(d.dptr);
		}

		/* Traverse through them. */
		ok1(ntdb_traverse(ntdb, trav, NULL) == OVERLOAD);

		/* Delete the first 99. */
		for (j = 0; j < OVERLOAD-1; j++)
			ok1(ntdb_delete(ntdb, key) == 0);

		ok1(ntdb_check(ntdb, NULL, NULL) == 0);

		ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
		ok1(d.dsize == sizeof(j));
		ok1(d.dptr != NULL);
		ok1(d.dptr && memcmp(d.dptr, &j, d.dsize) == 0);
		free(d.dptr);

		/* Traverse through them. */
		ok1(ntdb_traverse(ntdb, trav, NULL) == 1);

		/* Re-add */
		for (j = 0; j < OVERLOAD-1; j++) {
			ok1(ntdb_store(ntdb, key, dbuf, NTDB_INSERT) == 0);
		}
		ok1(ntdb_check(ntdb, NULL, NULL) == 0);

		/* Now try deleting as we go. */
		ok1(ntdb_traverse(ntdb, trav, trav) == OVERLOAD);
		ok1(ntdb_check(ntdb, NULL, NULL) == 0);
		ok1(ntdb_traverse(ntdb, trav, NULL) == 0);
		ntdb_close(ntdb);
	}

	ok1(tap_log_messages == 0);
	return exit_status();
}