Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 21 May 2019 19:42:04 +0000 (UTC)
From:      "Bjoern A. Zeeb" <bz@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r348063 - head/tests/sys/netinet
Message-ID:  <201905211942.x4LJg4hZ088781@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: bz
Date: Tue May 21 19:42:04 2019
New Revision: 348063
URL: https://svnweb.freebsd.org/changeset/base/348063

Log:
  Add very basic afinet socket tests which I started to write in order
  to then try to reproduce a kernel panic, which turned out to be a
  race condition and hard to test from here.
  
  Commit the changes anywhere as the "bind zero" case was a surprise
  to me and we should try to maintain this status.
  
  Also it is easy examples someone can build upon.
  
  With help from:	markj
  Event:		Waterloo Hackathon 2019

Added:
  head/tests/sys/netinet/socket_afinet.c   (contents, props changed)
Modified:
  head/tests/sys/netinet/Makefile

Modified: head/tests/sys/netinet/Makefile
==============================================================================
--- head/tests/sys/netinet/Makefile	Tue May 21 19:34:39 2019	(r348062)
+++ head/tests/sys/netinet/Makefile	Tue May 21 19:42:04 2019	(r348063)
@@ -4,7 +4,8 @@ TESTSDIR=	${TESTSBASE}/sys/netinet
 BINDIR=		${TESTSDIR}
 
 ATF_TESTS_C=	ip_reass_test \
-		so_reuseport_lb_test
+		so_reuseport_lb_test \
+		socket_afinet
 
 ATF_TESTS_SH=	fibs_test
 

Added: head/tests/sys/netinet/socket_afinet.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/tests/sys/netinet/socket_afinet.c	Tue May 21 19:42:04 2019	(r348063)
@@ -0,0 +1,97 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2019 Bjoern A. Zeeb
+ *
+ * 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 AUTHOR 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 AUTHOR 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>
+__FBSDID("$FreeBSD$");
+
+#include <sys/errno.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include <atf-c.h>
+
+ATF_TC_WITHOUT_HEAD(socket_afinet);
+ATF_TC_BODY(socket_afinet, tc)
+{
+	int sd;
+
+	sd = socket(PF_INET, SOCK_DGRAM, 0);
+	ATF_CHECK(sd >= 0);
+
+	close(sd);
+}
+
+ATF_TC_WITHOUT_HEAD(socket_afinet_bind_zero);
+ATF_TC_BODY(socket_afinet_bind_zero, tc)
+{
+	int sd, rc;
+	struct sockaddr_in sin;
+
+	sd = socket(PF_INET, SOCK_DGRAM, 0);
+	ATF_CHECK(sd >= 0);
+
+	bzero(&sin, sizeof(sin));
+	/*
+	 * For AF_INET we do not check the family in in_pcbbind_setup(9),
+	 * sa_len gets set from the syscall argument in getsockaddr(9),
+	 * so we bind to 0:0.
+	 */
+	rc = bind(sd, (struct sockaddr *)&sin, sizeof(sin));
+	ATF_CHECK_EQ(0, rc);
+
+	close(sd);
+}
+
+ATF_TC_WITHOUT_HEAD(socket_afinet_bind_ok);
+ATF_TC_BODY(socket_afinet_bind_ok, tc)
+{
+	int sd, rc;
+	struct sockaddr_in sin;
+
+	sd = socket(PF_INET, SOCK_DGRAM, 0);
+	ATF_CHECK(sd >= 0);
+
+	bzero(&sin, sizeof(sin));
+	sin.sin_family = AF_INET;
+	sin.sin_len = sizeof(sin);
+	sin.sin_port = htons(6666);
+	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+	rc = bind(sd, (struct sockaddr *)&sin, sizeof(sin));
+	ATF_CHECK_EQ(0, rc);
+
+	close(sd);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+	ATF_TP_ADD_TC(tp, socket_afinet);
+	ATF_TP_ADD_TC(tp, socket_afinet_bind_zero);
+	ATF_TP_ADD_TC(tp, socket_afinet_bind_ok);
+
+	return atf_no_error();
+}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201905211942.x4LJg4hZ088781>