diff options
author | ngie <ngie@FreeBSD.org> | 2017-02-10 01:13:12 +0000 |
---|---|---|
committer | ngie <ngie@FreeBSD.org> | 2017-02-10 01:13:12 +0000 |
commit | 8864cde3495dbfd6b476c1b77a8bde035e1cf000 (patch) | |
tree | da558c97672e3d32f9e5551cd2daccd8684c5f03 /contrib/netbsd-tests/lib/libc/db/t_db_hash_seq.c | |
parent | 9c6235cf2af8bdadcd0f0a80288683f0c88f3e55 (diff) | |
download | FreeBSD-src-8864cde3495dbfd6b476c1b77a8bde035e1cf000.zip FreeBSD-src-8864cde3495dbfd6b476c1b77a8bde035e1cf000.tar.gz |
MFC r305358,r305449,r305451,r306367,r306397,r309474:
This also contains a merge of ^/projects/netbsd-tests-update-12@r304035 .
This change never hit ^/head because bin/cat's behavior was changed (on ^/head)
to match NetBSD.
PR: 210607
r305358:
Update contrib/netbsd-tests with new content from NetBSD
This updates the snapshot from 09/30/2014 to 08/11/2016
This brings in a number of new testcases from upstream, most
notably:
- bin/cat
- lib/libc
- lib/msun
- lib/libthr
- usr.bin/sort
lib/libc/tests/stdio/open_memstream_test.c was moved to
lib/libc/tests/stdio/open_memstream2_test.c to accomodate
the new open_memstream test from NetBSD.
Tested on: amd64 (VMware fusion VM; various bare metal platforms); i386 (VMware fusion VM); make tinderbox
r305449:
Install h_db to unbreak some of the lib/libc/db testcases after
r305358
r305451:
Fix lib/libc/rpc test assumptions added in r305358
- Require root in the tcp/udp subtests (it's needed on FreeBSD when
registering services).
- Skip the tests if service registration fails.
r306367 (by br):
Allow up to 6 arguments only on MIPS.
r306397 (by br):
Use right piece of code for FreeBSD.
r309474:
Don't build :strvis_locale if VIS_NOLOCALE is undefined
The copy of contrib/libc-vis on ^/stable/10 doesn't contain all of the features
in the ^/stable/11 // ^/head version, including VIS_NOLOCALE. The risk is lower
in conditionally running the test instead of backporting the newer version of
libc-vis
Diffstat (limited to 'contrib/netbsd-tests/lib/libc/db/t_db_hash_seq.c')
-rw-r--r-- | contrib/netbsd-tests/lib/libc/db/t_db_hash_seq.c | 343 |
1 files changed, 343 insertions, 0 deletions
diff --git a/contrib/netbsd-tests/lib/libc/db/t_db_hash_seq.c b/contrib/netbsd-tests/lib/libc/db/t_db_hash_seq.c new file mode 100644 index 0000000..6e19e22 --- /dev/null +++ b/contrib/netbsd-tests/lib/libc/db/t_db_hash_seq.c @@ -0,0 +1,343 @@ +/* $NetBSD: t_db_hash_seq.c,v 1.2 2015/06/22 22:35:51 christos Exp $ */ + +/*- + * Copyright (c) 2015 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include <sys/cdefs.h> +__RCSID("$NetBSD: t_db_hash_seq.c,v 1.2 2015/06/22 22:35:51 christos Exp $"); + +#include <sys/types.h> +#include <sys/socket.h> +#include <db.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <stdlib.h> +#include <fcntl.h> +#include <syslog.h> +#include <netinet/in.h> + +#define ATF + +struct conf { + struct sockaddr_storage c_ss; + int c_lmask; + int c_port; + int c_proto; + int c_family; + int c_uid; + int c_nfail; + char c_name[128]; + int c_rmask; + int c_duration; +}; + +struct dbinfo { + int count; + time_t last; + char id[64]; +}; + +#ifdef ATF +#include <atf-c.h> + +#define DO_ERR(msg, ...) ATF_REQUIRE_MSG(0, msg, __VA_ARGS__) +#define DO_WARNX(msg, ...) ATF_REQUIRE_MSG(0, msg, __VA_ARGS__) +#else +#include <err.h> + +#define DO_ERR(fmt, ...) err(EXIT_FAILURE, fmt, __VA_ARGS__) +#define DO_WARNX(fmt, ...) warnx(fmt, __VA_ARGS__) +#endif + +#define DO_DEBUG(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__) + +static HASHINFO openinfo = { + 4096, /* bsize */ + 32, /* ffactor */ + 256, /* nelem */ + 8 * 1024 * 1024,/* cachesize */ + NULL, /* hash() */ + 0 /* lorder */ +}; + +static int debug = 0; + +static int +state_close(DB *db) +{ + if (db == NULL) + return -1; + if ((*db->close)(db) == -1) + DO_ERR("%s: can't close db", __func__); + return 0; +} + +static DB * +state_open(const char *dbname, int flags, mode_t perm) +{ + DB *db; + + db = dbopen(dbname, flags, perm, DB_HASH, &openinfo); + if (db == NULL) { + if (errno == ENOENT && (flags & O_CREAT) == 0) + return NULL; + DO_ERR("%s: can't open `%s'", __func__, dbname); + } + return db; +} + +static int +state_sizecheck(const DBT *t) +{ + if (sizeof(struct conf) == t->size) + return 0; + DO_WARNX("Key size mismatch %zu != %zu", sizeof(struct conf), t->size); + return 0; +} + +static int +state_del(DB *db, const struct conf *c) +{ + int rv; + DBT k; + + if (db == NULL) + return -1; + + k.data = __UNCONST(c); + k.size = sizeof(*c); + + switch (rv = (*db->del)(db, &k, 1)) { + case 0: + case 1: + if (debug > 1) { + DO_DEBUG("%s: returns %d", __func__, rv); + (*db->sync)(db, 0); + } + return 0; + default: + DO_ERR("%s: failed", __func__); + return -1; + } +} + +#if 0 +static int +state_get(DB *db, const struct conf *c, struct dbinfo *dbi) +{ + int rv; + DBT k, v; + + if (db == NULL) + return -1; + + k.data = __UNCONST(c); + k.size = sizeof(*c); + + switch (rv = (*db->get)(db, &k, &v, 0)) { + case 0: + case 1: + if (rv) + memset(dbi, 0, sizeof(*dbi)); + else + memcpy(dbi, v.data, sizeof(*dbi)); + if (debug > 1) + DO_DEBUG("%s: returns %d", __func__, rv); + return 0; + default: + DO_ERR("%s: failed", __func__); + return -1; + } +} +#endif + +static int +state_put(DB *db, const struct conf *c, const struct dbinfo *dbi) +{ + int rv; + DBT k, v; + + if (db == NULL) + return -1; + + k.data = __UNCONST(c); + k.size = sizeof(*c); + v.data = __UNCONST(dbi); + v.size = sizeof(*dbi); + + switch (rv = (*db->put)(db, &k, &v, 0)) { + case 0: + if (debug > 1) { + DO_DEBUG("%s: returns %d", __func__, rv); + (*db->sync)(db, 0); + } + return 0; + case 1: + errno = EEXIST; + /*FALLTHROUGH*/ + default: + DO_ERR("%s: failed", __func__); + } +} + +static int +state_iterate(DB *db, struct conf *c, struct dbinfo *dbi, unsigned int first) +{ + int rv; + DBT k, v; + + if (db == NULL) + return -1; + + first = first ? R_FIRST : R_NEXT; + + switch (rv = (*db->seq)(db, &k, &v, first)) { + case 0: + if (state_sizecheck(&k) == -1) + return -1; + memcpy(c, k.data, sizeof(*c)); + memcpy(dbi, v.data, sizeof(*dbi)); + if (debug > 1) + DO_DEBUG("%s: returns %d", __func__, rv); + return 1; + case 1: + if (debug > 1) + DO_DEBUG("%s: returns %d", __func__, rv); + return 0; + default: + DO_ERR("%s: failed", __func__); + return -1; + } +} + +#define MAXB 100 + +static int +testdb(int skip) +{ + size_t i; + int f; + char flag[MAXB]; + DB *db; + struct conf c; + struct dbinfo d; + + db = state_open(NULL, O_RDWR|O_CREAT|O_TRUNC, 0600); + if (db == NULL) + DO_ERR("%s: cannot open `%s'", __func__, "foo"); + + memset(&c, 0, sizeof(c)); + memset(&d, 0, sizeof(d)); + memset(flag, 0, sizeof(flag)); + + for (i = 0; i < __arraycount(flag); i++) { + c.c_port = i; + state_put(db, &c, &d); + } + + for (f = 1, i = 0; state_iterate(db, &c, &d, f) == 1; f = 0, i++) { + if (debug > 1) + DO_DEBUG("%zu %d\n", i, c.c_port); + if (flag[c.c_port]) + DO_WARNX("Already visited %d", c.c_port); + flag[c.c_port] = 1; + if (skip == 0 || c.c_port % skip != 0) + continue; + state_del(db, &c); + } + state_close(db); + for (i = 0; i < __arraycount(flag); i++) { + if (flag[i] == 0) + DO_WARNX("Not visited %zu", i); + } + return 0; +} + +#ifndef ATF +int +main(int argc, char *argv[]) +{ + return testdb(6); +} +#else + +ATF_TC(test_hash_del_none); +ATF_TC_HEAD(test_hash_del_none, tc) +{ + atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables deleting none"); +} + +ATF_TC_BODY(test_hash_del_none, tc) +{ + testdb(0); +} + +ATF_TC(test_hash_del_all); +ATF_TC_HEAD(test_hash_del_all, tc) +{ + atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables deleting all"); +} + +ATF_TC_BODY(test_hash_del_all, tc) +{ + testdb(1); +} + +ATF_TC(test_hash_del_alt); +ATF_TC_HEAD(test_hash_del_alt, tc) +{ + atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables alternating deletets"); +} + +ATF_TC_BODY(test_hash_del_alt, tc) +{ + testdb(2); +} + +ATF_TC(test_hash_del_every_7); +ATF_TC_HEAD(test_hash_del_every_7, tc) +{ + atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables deleting every 7 elements"); +} + +ATF_TC_BODY(test_hash_del_every_7, tc) +{ + testdb(7); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, test_hash_del_none); + ATF_TP_ADD_TC(tp, test_hash_del_all); + ATF_TP_ADD_TC(tp, test_hash_del_alt); + ATF_TP_ADD_TC(tp, test_hash_del_every_7); + + return 0; +} +#endif |