Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/tests/net/inpcb tests: import in_pcbbind/runtest.c from Open...
details: https://anonhg.NetBSD.org/src/rev/9eb51f83777d
branches: trunk
changeset: 372346:9eb51f83777d
user: ozaki-r <ozaki-r%NetBSD.org@localhost>
date: Thu Nov 17 08:38:08 2022 +0000
description:
tests: import in_pcbbind/runtest.c from OpenBSD as inpcb_bind.c
As of $OpenBSD: runtest.c,v 1.7 2022/04/10 14:08:35 claudio Exp $
diffstat:
tests/net/inpcb/inpcb_bind.c | 455 +++++++++++++++++++++++++++++++++++++++++++
1 files changed, 455 insertions(+), 0 deletions(-)
diffs (truncated from 459 to 300 lines):
diff -r 81f408c4c5db -r 9eb51f83777d tests/net/inpcb/inpcb_bind.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/net/inpcb/inpcb_bind.c Thu Nov 17 08:38:08 2022 +0000
@@ -0,0 +1,455 @@
+/* $OpenBSD: runtest.c,v 1.7 2022/04/10 14:08:35 claudio Exp $ */
+/*
+ * Copyright (c) 2015 Vincent Gross <vincent.gross%kilob.yt@localhost>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <err.h>
+#include <netdb.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <net/if.h>
+#include <ifaddrs.h>
+
+int
+runtest(int *sockp, struct addrinfo *ai, int reuseaddr, int reuseport,
+ void *mreq, int expected)
+{
+ int error, optval;
+ struct ip_mreq imr;
+
+ *sockp = socket(ai->ai_family, ai->ai_socktype, 0);
+ if (*sockp == -1) {
+ warn("%s : socket()", ai->ai_canonname);
+ return (3);
+ }
+
+ if (reuseaddr) {
+ optval = 1;
+ error = setsockopt(*sockp, SOL_SOCKET, SO_REUSEADDR,
+ &optval, sizeof(int));
+ if (error) {
+ warn("%s : setsockopt(SO_REUSEADDR)", ai->ai_canonname);
+ return (2);
+ }
+ }
+
+ if (reuseport) {
+ optval = 1;
+ error = setsockopt(*sockp, SOL_SOCKET, SO_REUSEPORT,
+ &optval, sizeof(int));
+ if (error) {
+ warn("%s : setsockopt(SO_REUSEPORT)", ai->ai_canonname);
+ return (2);
+ }
+ }
+
+ if (mreq) {
+ switch (ai->ai_family) {
+ case AF_INET6:
+ error = setsockopt(*sockp, IPPROTO_IPV6, IPV6_JOIN_GROUP,
+ mreq, sizeof(struct ipv6_mreq));
+ if (error) {
+ warn("%s : setsockopt(IPV6_JOIN_GROUP)",
+ ai->ai_canonname);
+ return (2);
+ }
+ break;
+ case AF_INET:
+ error = setsockopt(*sockp, IPPROTO_IP, IP_ADD_MEMBERSHIP,
+ mreq, sizeof(struct ip_mreq));
+ if (error) {
+ warn("%s : setsockopt(IP_ADD_MEMBERSHIP)",
+ ai->ai_canonname);
+ return (2);
+ }
+ break;
+ default:
+ warnx("%s : trying to join multicast group in unknown AF",
+ ai->ai_canonname);
+ return (2);
+ }
+ }
+
+
+ error = bind(*sockp, ai->ai_addr, ai->ai_addrlen);
+ if (error && (expected == 0 || expected != errno)) {
+ warn("bind(%s,%s,%s)", ai->ai_canonname,
+ reuseaddr ? "REUSEADDR" : "", reuseport ? "REUSEPORT" : "");
+ return (1);
+ }
+ if (error == 0 && expected != 0) {
+ warnx("bind(%s,%s,%s) succeeded, expected : %s", ai->ai_canonname,
+ reuseaddr ? "REUSEADDR" : "", reuseport ? "REUSEPORT" : "",
+ strerror(expected));
+ return (1);
+ }
+
+ return (0);
+}
+
+void
+cleanup(int *fds, int num_fds)
+{
+ while (num_fds-- > 0)
+ if (close(*fds++) && errno != EBADF)
+ err(2, "unable to clean up sockets, aborting");
+}
+
+int
+unicast_testsuite(struct addrinfo *local, struct addrinfo *any)
+{
+ int test_rc, rc, *s;
+ int sockets[4];
+
+ test_rc = 0;
+ rc = 0; s = sockets;
+ rc |= runtest(s++, local, 0, 0, NULL, 0);
+ rc |= runtest(s++, any, 0, 0, NULL, EADDRINUSE);
+ rc |= runtest(s++, any, 1, 0, NULL, 0);
+ cleanup(sockets, 3);
+ test_rc |= rc;
+ if (rc)
+ warnx("%s : test #%d failed", __func__, 1);
+
+ rc = 0; s = sockets;
+ rc |= runtest(s++, any, 0, 0, NULL, 0);
+ rc |= runtest(s++, local, 0, 0, NULL, EADDRINUSE);
+ rc |= runtest(s++, local, 1, 0, NULL, 0);
+ cleanup(sockets, 3);
+ test_rc |= rc;
+ if (rc)
+ warnx("%s : test #%d failed", __func__, 2);
+
+ rc = 0; s = sockets;
+ rc |= runtest(s++, local, 0, 1, NULL, 0);
+ rc |= runtest(s++, local, 0, 1, NULL, 0);
+ rc |= runtest(s++, local, 1, 0, NULL, EADDRINUSE);
+ rc |= runtest(s++, local, 0, 0, NULL, EADDRINUSE);
+ cleanup(sockets, 4);
+ test_rc |= rc;
+ if (rc)
+ warnx("%s : test #%d failed", __func__, 3);
+
+ rc = 0; s = sockets;
+ rc |= runtest(s++, any, 0, 1, NULL, 0);
+ rc |= runtest(s++, any, 0, 1, NULL, 0);
+ rc |= runtest(s++, any, 1, 0, NULL, EADDRINUSE);
+ rc |= runtest(s++, any, 0, 0, NULL, EADDRINUSE);
+ cleanup(sockets, 4);
+ test_rc |= rc;
+ if (rc)
+ warnx("%s : test #%d failed", __func__, 4);
+
+ rc = 0; s = sockets;
+ rc |= runtest(s++, local, 1, 0, NULL, 0);
+ rc |= runtest(s++, local, 1, 0, NULL, EADDRINUSE);
+ rc |= runtest(s++, local, 0, 1, NULL, EADDRINUSE);
+ cleanup(sockets, 3);
+ test_rc |= rc;
+ if (rc)
+ warnx("%s : test #%d failed", __func__, 5);
+
+ rc = 0; s = sockets;
+ rc |= runtest(s++, any, 1, 0, NULL, 0);
+ rc |= runtest(s++, any, 1, 0, NULL, EADDRINUSE);
+ rc |= runtest(s++, any, 0, 1, NULL, EADDRINUSE);
+ cleanup(sockets, 3);
+ test_rc |= rc;
+ if (rc)
+ warnx("%s : test #%d failed", __func__, 6);
+
+ return (test_rc);
+}
+
+int
+mcast_reuse_testsuite(struct addrinfo *local, void *mr)
+{
+ int test_rc, rc, *s;
+ int sockets[6];
+ int testnum = 1;
+
+ test_rc = 0;
+ rc = 0; s = sockets;
+ rc |= runtest(s++, local, 0, 0, mr, 0);
+ rc |= runtest(s++, local, 1, 0, mr, EADDRINUSE);
+ rc |= runtest(s++, local, 0, 1, mr, EADDRINUSE);
+ rc |= runtest(s++, local, 1, 1, mr, EADDRINUSE);
+ cleanup(sockets, 4);
+ test_rc |= rc;
+ if (rc)
+ warnx("%s : test #%d failed", __func__, 1);
+
+ rc = 0; s = sockets;
+ rc |= runtest(s++, local, 0, 1, mr, 0);
+ rc |= runtest(s++, local, 0, 0, mr, EADDRINUSE);
+ rc |= runtest(s++, local, 0, 1, mr, 0);
+ rc |= runtest(s++, local, 1, 0, mr, 0);
+ rc |= runtest(s++, local, 1, 1, mr, 0);
+ cleanup(sockets, 5);
+ test_rc |= rc;
+ if (rc)
+ warnx("%s : test #%d failed", __func__, 2);
+
+ rc = 0; s = sockets;
+ rc |= runtest(s++, local, 1, 0, mr, 0);
+ rc |= runtest(s++, local, 0, 0, mr, EADDRINUSE);
+ rc |= runtest(s++, local, 1, 0, mr, 0);
+ rc |= runtest(s++, local, 0, 1, mr, 0);
+ rc |= runtest(s++, local, 1, 1, mr, 0);
+ cleanup(sockets, 5);
+ test_rc |= rc;
+ if (rc)
+ warnx("%s : test #%d failed", __func__, 3);
+
+ rc = 0; s = sockets;
+ rc |= runtest(s++, local, 1, 1, mr, 0);
+ rc |= runtest(s++, local, 0, 0, mr, EADDRINUSE);
+ rc |= runtest(s++, local, 0, 1, mr, 0);
+ rc |= runtest(s++, local, 1, 0, mr, 0);
+ rc |= runtest(s++, local, 1, 1, mr, 0);
+ cleanup(sockets, 5);
+ test_rc |= rc;
+ if (rc)
+ warnx("%s : test #%d failed", __func__, 4);
+
+#if 0
+ rc = 0; s = sockets;
+ rc |= runtest(s++, local, 1, 1, mr, 0);
+ rc |= runtest(s++, local, 1, 0, mr, 0);
+ rc |= runtest(s++, local, 0, 1, mr, 0);
+ cleanup(sockets, 3);
+ test_rc |= rc;
+ if (rc)
+ warnx("%s : test #%d failed", __func__, 5);
+
+ rc = 0; s = sockets;
+ rc |= runtest(s++, local, 1, 1, mr, 0);
+ rc |= runtest(s++, local, 1, 0, mr, 0);
+ rc |= runtest(s++, local, 1, 0, mr, 0);
+ rc |= runtest(s++, local, 1, 1, mr, 0);
+ rc |= runtest(s++, local, 0, 1, mr, 0);
+ cleanup(sockets, 5);
+ test_rc |= rc;
+ if (rc)
+ warnx("%s : test #%d failed", __func__, 6);
+
+ rc = 0; s = sockets;
+ rc |= runtest(s++, local, 1, 1, mr, 0);
+ rc |= runtest(s++, local, 1, 0, mr, 0);
+ rc |= runtest(s++, local, 1, 1, mr, 0);
+ rc |= runtest(s++, local, 1, 0, mr, 0);
+ rc |= runtest(s++, local, 0, 1, mr, 0);
+ cleanup(sockets, 5);
+ test_rc |= rc;
+ if (rc)
+ warnx("%s : test #%d failed", __func__, 7);
+#endif
+ return (test_rc);
+}
+
+int
+mcast6_testsuite(struct addrinfo *local, struct ipv6_mreq *local_mreq,
+ struct addrinfo *any, struct ipv6_mreq *any_mreq)
+{
+ int test_rc, rc, *s;
+ int sockets[4];
+ int testnum = 1;
+
+ test_rc = 0;
+ rc = 0; s = sockets;
+ rc |= runtest(s++, local, 0, 0, local_mreq, 0);
+ rc |= runtest(s++, any, 0, 0, any_mreq, EADDRINUSE);
+ rc |= runtest(s++, any, 1, 0, any_mreq, 0);
+ cleanup(sockets, 3);
+ test_rc |= rc;
+ if (rc)
+ warnx("%s : test #%d failed", __func__, 1);
+
+ rc = 0; s = sockets;
+ rc |= runtest(s++, any, 0, 0, any_mreq, 0);
+ rc |= runtest(s++, local, 0, 0, local_mreq, EADDRINUSE);
+ rc |= runtest(s++, local, 1, 0, local_mreq, 0);
+ cleanup(sockets, 3);
+ test_rc |= rc;
+ if (rc)
+ warnx("%s : test #%d failed", __func__, 2);
+
+ rc = 0; s = sockets;
Home |
Main Index |
Thread Index |
Old Index