1 /* $NetBSD: t_db_hash_seq.c,v 1.2 2015/06/22 22:35:51 christos Exp $ */
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_db_hash_seq.c,v 1.2 2015/06/22 22:35:51 christos Exp $");
34 #include <sys/types.h>
35 #include <sys/socket.h>
43 #include <netinet/in.h>
48 struct sockaddr_storage c_ss;
69 #define DO_ERR(msg, ...) ATF_REQUIRE_MSG(0, msg, __VA_ARGS__)
70 #define DO_WARNX(msg, ...) ATF_REQUIRE_MSG(0, msg, __VA_ARGS__)
74 #define DO_ERR(fmt, ...) err(EXIT_FAILURE, fmt, __VA_ARGS__)
75 #define DO_WARNX(fmt, ...) warnx(fmt, __VA_ARGS__)
78 #define DO_DEBUG(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
80 static HASHINFO openinfo = {
84 8 * 1024 * 1024,/* cachesize */
96 if ((*db->close)(db) == -1)
97 DO_ERR("%s: can't close db", __func__);
102 state_open(const char *dbname, int flags, mode_t perm)
106 db = dbopen(dbname, flags, perm, DB_HASH, &openinfo);
108 if (errno == ENOENT && (flags & O_CREAT) == 0)
110 DO_ERR("%s: can't open `%s'", __func__, dbname);
116 state_sizecheck(const DBT *t)
118 if (sizeof(struct conf) == t->size)
120 DO_WARNX("Key size mismatch %zu != %zu", sizeof(struct conf), t->size);
125 state_del(DB *db, const struct conf *c)
133 k.data = __UNCONST(c);
136 switch (rv = (*db->del)(db, &k, 1)) {
140 DO_DEBUG("%s: returns %d", __func__, rv);
145 DO_ERR("%s: failed", __func__);
152 state_get(DB *db, const struct conf *c, struct dbinfo *dbi)
160 k.data = __UNCONST(c);
163 switch (rv = (*db->get)(db, &k, &v, 0)) {
167 memset(dbi, 0, sizeof(*dbi));
169 memcpy(dbi, v.data, sizeof(*dbi));
171 DO_DEBUG("%s: returns %d", __func__, rv);
174 DO_ERR("%s: failed", __func__);
181 state_put(DB *db, const struct conf *c, const struct dbinfo *dbi)
189 k.data = __UNCONST(c);
191 v.data = __UNCONST(dbi);
192 v.size = sizeof(*dbi);
194 switch (rv = (*db->put)(db, &k, &v, 0)) {
197 DO_DEBUG("%s: returns %d", __func__, rv);
205 DO_ERR("%s: failed", __func__);
210 state_iterate(DB *db, struct conf *c, struct dbinfo *dbi, unsigned int first)
218 first = first ? R_FIRST : R_NEXT;
220 switch (rv = (*db->seq)(db, &k, &v, first)) {
222 if (state_sizecheck(&k) == -1)
224 memcpy(c, k.data, sizeof(*c));
225 memcpy(dbi, v.data, sizeof(*dbi));
227 DO_DEBUG("%s: returns %d", __func__, rv);
231 DO_DEBUG("%s: returns %d", __func__, rv);
234 DO_ERR("%s: failed", __func__);
251 db = state_open(NULL, O_RDWR|O_CREAT|O_TRUNC, 0600);
253 DO_ERR("%s: cannot open `%s'", __func__, "foo");
255 memset(&c, 0, sizeof(c));
256 memset(&d, 0, sizeof(d));
257 memset(flag, 0, sizeof(flag));
259 for (i = 0; i < __arraycount(flag); i++) {
261 state_put(db, &c, &d);
264 for (f = 1, i = 0; state_iterate(db, &c, &d, f) == 1; f = 0, i++) {
266 DO_DEBUG("%zu %d\n", i, c.c_port);
268 DO_WARNX("Already visited %d", c.c_port);
270 if (skip == 0 || c.c_port % skip != 0)
275 for (i = 0; i < __arraycount(flag); i++) {
277 DO_WARNX("Not visited %zu", i);
284 main(int argc, char *argv[])
290 ATF_TC(test_hash_del_none);
291 ATF_TC_HEAD(test_hash_del_none, tc)
293 atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables deleting none");
296 ATF_TC_BODY(test_hash_del_none, tc)
301 ATF_TC(test_hash_del_all);
302 ATF_TC_HEAD(test_hash_del_all, tc)
304 atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables deleting all");
307 ATF_TC_BODY(test_hash_del_all, tc)
312 ATF_TC(test_hash_del_alt);
313 ATF_TC_HEAD(test_hash_del_alt, tc)
315 atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables alternating deletets");
318 ATF_TC_BODY(test_hash_del_alt, tc)
323 ATF_TC(test_hash_del_every_7);
324 ATF_TC_HEAD(test_hash_del_every_7, tc)
326 atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables deleting every 7 elements");
329 ATF_TC_BODY(test_hash_del_every_7, tc)
336 ATF_TP_ADD_TC(tp, test_hash_del_none);
337 ATF_TP_ADD_TC(tp, test_hash_del_all);
338 ATF_TP_ADD_TC(tp, test_hash_del_alt);
339 ATF_TP_ADD_TC(tp, test_hash_del_every_7);