xref: /netbsd-src/tests/lib/libc/db/t_db_hash_seq.c (revision 45092b058235086dd1d353a370dd21259a52ec61)
1*45092b05Smrg /*	$NetBSD: t_db_hash_seq.c,v 1.4 2020/09/07 00:28:44 mrg Exp $	*/
288fc5969Schristos 
388fc5969Schristos /*-
488fc5969Schristos  * Copyright (c) 2015 The NetBSD Foundation, Inc.
588fc5969Schristos  * All rights reserved.
688fc5969Schristos  *
788fc5969Schristos  * This code is derived from software contributed to The NetBSD Foundation
888fc5969Schristos  * by Christos Zoulas.
988fc5969Schristos  *
1088fc5969Schristos  * Redistribution and use in source and binary forms, with or without
1188fc5969Schristos  * modification, are permitted provided that the following conditions
1288fc5969Schristos  * are met:
1388fc5969Schristos  * 1. Redistributions of source code must retain the above copyright
1488fc5969Schristos  *    notice, this list of conditions and the following disclaimer.
1588fc5969Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1688fc5969Schristos  *    notice, this list of conditions and the following disclaimer in the
1788fc5969Schristos  *    documentation and/or other materials provided with the distribution.
1888fc5969Schristos  *
1988fc5969Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2088fc5969Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2188fc5969Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2288fc5969Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2388fc5969Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2488fc5969Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2588fc5969Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2688fc5969Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2788fc5969Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2888fc5969Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2988fc5969Schristos  * POSSIBILITY OF SUCH DAMAGE.
3088fc5969Schristos  */
3188fc5969Schristos #include <sys/cdefs.h>
32*45092b05Smrg __RCSID("$NetBSD: t_db_hash_seq.c,v 1.4 2020/09/07 00:28:44 mrg Exp $");
3388fc5969Schristos 
3488fc5969Schristos #include <sys/types.h>
3588fc5969Schristos #include <sys/socket.h>
3688fc5969Schristos #include <db.h>
3788fc5969Schristos #include <stdio.h>
3888fc5969Schristos #include <string.h>
3988fc5969Schristos #include <errno.h>
4088fc5969Schristos #include <stdlib.h>
4188fc5969Schristos #include <fcntl.h>
4288fc5969Schristos #include <syslog.h>
4388fc5969Schristos #include <netinet/in.h>
4488fc5969Schristos 
4588fc5969Schristos #define ATF
4688fc5969Schristos 
4788fc5969Schristos struct conf {
4888fc5969Schristos 	struct sockaddr_storage	c_ss;
4988fc5969Schristos 	int			c_lmask;
5088fc5969Schristos 	int			c_port;
5188fc5969Schristos 	int			c_proto;
5288fc5969Schristos 	int			c_family;
5388fc5969Schristos 	int			c_uid;
5488fc5969Schristos 	int			c_nfail;
5588fc5969Schristos 	char			c_name[128];
5688fc5969Schristos 	int			c_rmask;
5788fc5969Schristos 	int			c_duration;
5888fc5969Schristos };
5988fc5969Schristos 
6088fc5969Schristos struct dbinfo {
6188fc5969Schristos 	int count;
6288fc5969Schristos 	time_t last;
6388fc5969Schristos 	char id[64];
6488fc5969Schristos };
6588fc5969Schristos 
6688fc5969Schristos #ifdef ATF
6788fc5969Schristos #include <atf-c.h>
6888fc5969Schristos 
6988fc5969Schristos #define DO_ERR(msg, ...)	ATF_REQUIRE_MSG(0, msg, __VA_ARGS__)
7088fc5969Schristos #define DO_WARNX(msg, ...)	ATF_REQUIRE_MSG(0, msg, __VA_ARGS__)
7188fc5969Schristos #else
7288fc5969Schristos #include <err.h>
7388fc5969Schristos 
7488fc5969Schristos #define DO_ERR(fmt, ...)	err(EXIT_FAILURE, fmt, __VA_ARGS__)
7588fc5969Schristos #define DO_WARNX(fmt, ...)	warnx(fmt, __VA_ARGS__)
7688fc5969Schristos #endif
7788fc5969Schristos 
7888fc5969Schristos #define DO_DEBUG(fmt, ...)	fprintf(stderr, fmt, __VA_ARGS__)
7988fc5969Schristos 
8088fc5969Schristos static HASHINFO openinfo = {
8188fc5969Schristos 	4096,		/* bsize */
8288fc5969Schristos 	32,		/* ffactor */
8388fc5969Schristos 	256,		/* nelem */
8488fc5969Schristos 	8 * 1024 * 1024,/* cachesize */
8588fc5969Schristos 	NULL,		/* hash() */
8688fc5969Schristos 	0		/* lorder */
8788fc5969Schristos };
8888fc5969Schristos 
8988fc5969Schristos static int debug = 0;
9088fc5969Schristos 
9188fc5969Schristos static int
state_close(DB * db)9288fc5969Schristos state_close(DB *db)
9388fc5969Schristos {
9488fc5969Schristos 	if (db == NULL)
9588fc5969Schristos 		return -1;
9688fc5969Schristos 	if ((*db->close)(db) == -1)
9788fc5969Schristos 		DO_ERR("%s: can't close db", __func__);
9888fc5969Schristos 	return 0;
9988fc5969Schristos }
10088fc5969Schristos 
10188fc5969Schristos static DB *
state_open(const char * dbname,int flags,mode_t perm)10288fc5969Schristos state_open(const char *dbname, int flags, mode_t perm)
10388fc5969Schristos {
10488fc5969Schristos 	DB *db;
10588fc5969Schristos 
10688fc5969Schristos 	db = dbopen(dbname, flags, perm, DB_HASH, &openinfo);
10788fc5969Schristos 	if (db == NULL) {
10888fc5969Schristos 		if (errno == ENOENT && (flags & O_CREAT) == 0)
10988fc5969Schristos 			return NULL;
110*45092b05Smrg 		DO_ERR("%s: can't open `%s'", __func__,
111*45092b05Smrg 		    dbname ? dbname : "<memory>");
11288fc5969Schristos 	}
11388fc5969Schristos 	return db;
11488fc5969Schristos }
11588fc5969Schristos 
11688fc5969Schristos static int
state_sizecheck(const DBT * t)11788fc5969Schristos state_sizecheck(const DBT *t)
11888fc5969Schristos {
11988fc5969Schristos 	if (sizeof(struct conf) == t->size)
12088fc5969Schristos 		return 0;
12188fc5969Schristos 	DO_WARNX("Key size mismatch %zu != %zu", sizeof(struct conf), t->size);
12288fc5969Schristos 	return 0;
12388fc5969Schristos }
12488fc5969Schristos 
12588fc5969Schristos static int
state_del(DB * db,const struct conf * c)12688fc5969Schristos state_del(DB *db, const struct conf *c)
12788fc5969Schristos {
12888fc5969Schristos 	int rv;
12988fc5969Schristos 	DBT k;
13088fc5969Schristos 
13188fc5969Schristos 	if (db == NULL)
13288fc5969Schristos 		return -1;
13388fc5969Schristos 
13488fc5969Schristos 	k.data = __UNCONST(c);
13588fc5969Schristos 	k.size = sizeof(*c);
13688fc5969Schristos 
13788fc5969Schristos 	switch (rv = (*db->del)(db, &k, 1)) {
13888fc5969Schristos 	case 0:
13988fc5969Schristos 	case 1:
14088fc5969Schristos 		if (debug > 1) {
14188fc5969Schristos 			DO_DEBUG("%s: returns %d", __func__, rv);
14288fc5969Schristos 			(*db->sync)(db, 0);
14388fc5969Schristos 		}
14488fc5969Schristos 		return 0;
14588fc5969Schristos 	default:
14688fc5969Schristos 		DO_ERR("%s: failed", __func__);
14788fc5969Schristos 		return -1;
14888fc5969Schristos 	}
14988fc5969Schristos }
15088fc5969Schristos 
15188fc5969Schristos #if 0
15288fc5969Schristos static int
15388fc5969Schristos state_get(DB *db, const struct conf *c, struct dbinfo *dbi)
15488fc5969Schristos {
15588fc5969Schristos 	int rv;
15688fc5969Schristos 	DBT k, v;
15788fc5969Schristos 
15888fc5969Schristos 	if (db == NULL)
15988fc5969Schristos 		return -1;
16088fc5969Schristos 
16188fc5969Schristos 	k.data = __UNCONST(c);
16288fc5969Schristos 	k.size = sizeof(*c);
16388fc5969Schristos 
16488fc5969Schristos 	switch (rv = (*db->get)(db, &k, &v, 0)) {
16588fc5969Schristos 	case 0:
16688fc5969Schristos 	case 1:
16788fc5969Schristos 		if (rv)
16888fc5969Schristos 			memset(dbi, 0, sizeof(*dbi));
16988fc5969Schristos 		else
17088fc5969Schristos 			memcpy(dbi, v.data, sizeof(*dbi));
17188fc5969Schristos 		if (debug > 1)
17288fc5969Schristos 			DO_DEBUG("%s: returns %d", __func__, rv);
17388fc5969Schristos 		return 0;
17488fc5969Schristos 	default:
17588fc5969Schristos 		DO_ERR("%s: failed", __func__);
17688fc5969Schristos 		return -1;
17788fc5969Schristos 	}
17888fc5969Schristos }
17988fc5969Schristos #endif
18088fc5969Schristos 
18188fc5969Schristos static int
state_put(DB * db,const struct conf * c,const struct dbinfo * dbi)18288fc5969Schristos state_put(DB *db, const struct conf *c, const struct dbinfo *dbi)
18388fc5969Schristos {
18488fc5969Schristos 	int rv;
18588fc5969Schristos 	DBT k, v;
18688fc5969Schristos 
18788fc5969Schristos 	if (db == NULL)
18888fc5969Schristos 		return -1;
18988fc5969Schristos 
19088fc5969Schristos 	k.data = __UNCONST(c);
19188fc5969Schristos 	k.size = sizeof(*c);
19288fc5969Schristos 	v.data = __UNCONST(dbi);
19388fc5969Schristos 	v.size = sizeof(*dbi);
19488fc5969Schristos 
19588fc5969Schristos 	switch (rv = (*db->put)(db, &k, &v, 0)) {
19688fc5969Schristos 	case 0:
19788fc5969Schristos 		if (debug > 1) {
19888fc5969Schristos 			DO_DEBUG("%s: returns %d", __func__, rv);
19988fc5969Schristos 			(*db->sync)(db, 0);
20088fc5969Schristos 		}
20188fc5969Schristos 		return 0;
20288fc5969Schristos 	case 1:
20388fc5969Schristos 		errno = EEXIST;
20488fc5969Schristos 		/*FALLTHROUGH*/
20588fc5969Schristos 	default:
20688fc5969Schristos 		DO_ERR("%s: failed", __func__);
20788fc5969Schristos 	}
20888fc5969Schristos }
20988fc5969Schristos 
21088fc5969Schristos static int
state_iterate(DB * db,struct conf * c,struct dbinfo * dbi,unsigned int first)21188fc5969Schristos state_iterate(DB *db, struct conf *c, struct dbinfo *dbi, unsigned int first)
21288fc5969Schristos {
21388fc5969Schristos 	int rv;
21488fc5969Schristos 	DBT k, v;
21588fc5969Schristos 
21688fc5969Schristos 	if (db == NULL)
21788fc5969Schristos 		return -1;
21888fc5969Schristos 
21988fc5969Schristos 	first = first ? R_FIRST : R_NEXT;
22088fc5969Schristos 
22188fc5969Schristos 	switch (rv = (*db->seq)(db, &k, &v, first)) {
22288fc5969Schristos 	case 0:
22388fc5969Schristos 		if (state_sizecheck(&k) == -1)
22488fc5969Schristos 			return -1;
22588fc5969Schristos 		memcpy(c, k.data, sizeof(*c));
22688fc5969Schristos 		memcpy(dbi, v.data, sizeof(*dbi));
22788fc5969Schristos 		if (debug > 1)
22888fc5969Schristos 			DO_DEBUG("%s: returns %d", __func__, rv);
22988fc5969Schristos 		return 1;
23088fc5969Schristos 	case 1:
23188fc5969Schristos 		if (debug > 1)
23288fc5969Schristos 			DO_DEBUG("%s: returns %d", __func__, rv);
23388fc5969Schristos 		return 0;
23488fc5969Schristos 	default:
23588fc5969Schristos 		DO_ERR("%s: failed", __func__);
23688fc5969Schristos 		return -1;
23788fc5969Schristos 	}
23888fc5969Schristos }
23988fc5969Schristos 
24088fc5969Schristos #define MAXB 100
24188fc5969Schristos 
24288fc5969Schristos static int
testdb(int skip)24388fc5969Schristos testdb(int skip)
24488fc5969Schristos {
24588fc5969Schristos 	size_t i;
24688fc5969Schristos 	int f;
24788fc5969Schristos 	char flag[MAXB];
24888fc5969Schristos 	DB *db;
24988fc5969Schristos 	struct conf c;
25088fc5969Schristos 	struct dbinfo d;
25188fc5969Schristos 
252df322057Schristos 	db = state_open(NULL, O_RDWR|O_CREAT|O_TRUNC, 0600);
25388fc5969Schristos 	if (db == NULL)
25488fc5969Schristos 		DO_ERR("%s: cannot open `%s'", __func__, "foo");
25588fc5969Schristos 
25688fc5969Schristos 	memset(&c, 0, sizeof(c));
25788fc5969Schristos 	memset(&d, 0, sizeof(d));
25888fc5969Schristos 	memset(flag, 0, sizeof(flag));
25988fc5969Schristos 
26088fc5969Schristos 	for (i = 0; i < __arraycount(flag); i++) {
26188fc5969Schristos 		c.c_port = i;
26288fc5969Schristos 		state_put(db, &c, &d);
26388fc5969Schristos 	}
26488fc5969Schristos 
26588fc5969Schristos 	for (f = 1, i = 0; state_iterate(db, &c, &d, f) == 1; f = 0, i++) {
26688fc5969Schristos 		if (debug > 1)
26788fc5969Schristos 			DO_DEBUG("%zu %d\n", i, c.c_port);
26888fc5969Schristos 		if (flag[c.c_port])
26988fc5969Schristos 			DO_WARNX("Already visited %d", c.c_port);
27088fc5969Schristos 		flag[c.c_port] = 1;
271df322057Schristos 		if (skip == 0 || c.c_port % skip != 0)
27288fc5969Schristos 			continue;
27388fc5969Schristos 		state_del(db, &c);
27488fc5969Schristos 	}
27588fc5969Schristos 	state_close(db);
27688fc5969Schristos 	for (i = 0; i < __arraycount(flag); i++) {
27788fc5969Schristos 		if (flag[i] == 0)
27888fc5969Schristos 			DO_WARNX("Not visited %zu", i);
27988fc5969Schristos 	}
28088fc5969Schristos 	return 0;
28188fc5969Schristos }
28288fc5969Schristos 
28388fc5969Schristos #ifndef ATF
28488fc5969Schristos int
main(int argc,char * argv[])28588fc5969Schristos main(int argc, char *argv[])
28688fc5969Schristos {
28788fc5969Schristos 	return testdb(6);
28888fc5969Schristos }
28988fc5969Schristos #else
29088fc5969Schristos 
291df322057Schristos ATF_TC(test_hash_del_none);
ATF_TC_HEAD(test_hash_del_none,tc)292df322057Schristos ATF_TC_HEAD(test_hash_del_none, tc)
29388fc5969Schristos {
294df322057Schristos 	atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables deleting none");
29588fc5969Schristos }
29688fc5969Schristos 
ATF_TC_BODY(test_hash_del_none,tc)297df322057Schristos ATF_TC_BODY(test_hash_del_none, tc)
29888fc5969Schristos {
299df322057Schristos 	testdb(0);
300df322057Schristos }
301df322057Schristos 
302df322057Schristos ATF_TC(test_hash_del_all);
ATF_TC_HEAD(test_hash_del_all,tc)303df322057Schristos ATF_TC_HEAD(test_hash_del_all, tc)
304df322057Schristos {
305df322057Schristos 	atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables deleting all");
306df322057Schristos }
307df322057Schristos 
ATF_TC_BODY(test_hash_del_all,tc)308df322057Schristos ATF_TC_BODY(test_hash_del_all, tc)
309df322057Schristos {
310df322057Schristos 	testdb(1);
311df322057Schristos }
312df322057Schristos 
313df322057Schristos ATF_TC(test_hash_del_alt);
ATF_TC_HEAD(test_hash_del_alt,tc)314df322057Schristos ATF_TC_HEAD(test_hash_del_alt, tc)
315df322057Schristos {
316df322057Schristos 	atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables alternating deletets");
317df322057Schristos }
318df322057Schristos 
ATF_TC_BODY(test_hash_del_alt,tc)319df322057Schristos ATF_TC_BODY(test_hash_del_alt, tc)
320df322057Schristos {
321df322057Schristos 	testdb(2);
322df322057Schristos }
323df322057Schristos 
324df322057Schristos ATF_TC(test_hash_del_every_7);
ATF_TC_HEAD(test_hash_del_every_7,tc)325df322057Schristos ATF_TC_HEAD(test_hash_del_every_7, tc)
326df322057Schristos {
327df322057Schristos 	atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables deleting every 7 elements");
328df322057Schristos }
329df322057Schristos 
ATF_TC_BODY(test_hash_del_every_7,tc)330df322057Schristos ATF_TC_BODY(test_hash_del_every_7, tc)
331df322057Schristos {
332df322057Schristos 	testdb(7);
33388fc5969Schristos }
33488fc5969Schristos 
ATF_TP_ADD_TCS(tp)33588fc5969Schristos ATF_TP_ADD_TCS(tp)
33688fc5969Schristos {
337df322057Schristos 	ATF_TP_ADD_TC(tp, test_hash_del_none);
338df322057Schristos 	ATF_TP_ADD_TC(tp, test_hash_del_all);
339df322057Schristos 	ATF_TP_ADD_TC(tp, test_hash_del_alt);
340df322057Schristos 	ATF_TP_ADD_TC(tp, test_hash_del_every_7);
34188fc5969Schristos 
34236592171Smaya 	return atf_no_error();
34388fc5969Schristos }
34488fc5969Schristos #endif
345