Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 8 Jun 2017 05:12:11 +0000 (UTC)
From:      Gleb Smirnoff <glebius@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r319683 - head/tools/regression/sockets/accf_data_attach
Message-ID:  <201706080512.v585CBRb093161@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: glebius
Date: Thu Jun  8 05:12:11 2017
New Revision: 319683
URL: https://svnweb.freebsd.org/changeset/base/319683

Log:
  Improve this unit test: make sure that the accept filter actually works.
  
  Before this test just checked scenario of setting and removing the accept
  filter at different states of the socket.  Now it also checks that accept
  filter works: we connect to the server, and then check that we can't accept,
  then we send 1 byte of data and check again.

Modified:
  head/tools/regression/sockets/accf_data_attach/accf_data_attach.c

Modified: head/tools/regression/sockets/accf_data_attach/accf_data_attach.c
==============================================================================
--- head/tools/regression/sockets/accf_data_attach/accf_data_attach.c	Thu Jun  8 04:54:15 2017	(r319682)
+++ head/tools/regression/sockets/accf_data_attach/accf_data_attach.c	Thu Jun  8 05:12:11 2017	(r319683)
@@ -34,6 +34,7 @@
 
 #include <err.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -64,7 +65,7 @@ main(void)
 	struct accept_filter_arg afa;
 	struct sockaddr_in sin;
 	socklen_t len;
-	int lso, ret;
+	int lso, so, i, ret;
 
 	/* XXX: PLAIN_TEST_REQUIRE_MODULE "backport" for stable/9 */
 	const char *_mod_name = "accf_data";
@@ -76,7 +77,7 @@ main(void)
 	}
 	/* XXX: PLAIN_TEST_REQUIRE_MODULE for stable/9 */
 
-	printf("1..11\n");
+	printf("1..12\n");
 
 	/*
 	 * Step 0. Open socket().
@@ -103,6 +104,7 @@ main(void)
 	/*
 	 * Step 2. Bind().  Ideally this will succeed.
 	 */
+	setsockopt(lso, SOL_SOCKET, SO_REUSEADDR, &lso, sizeof(lso));
 	bzero(&sin, sizeof(sin));
 	sin.sin_len = sizeof(sin);
 	sin.sin_family = AF_INET;
@@ -203,24 +205,53 @@ main(void)
 	printf("ok 10 - getsockopt\n");
 
 	/*
-	 * Step 10: Remove accept filter.  After removing the accept filter
+	 * Step 10: Set listening socket to non blocking mode.  Open
+	 * connection to our listening socket and try to accept.  Should
+	 * no succeed.  Write a byte of data and try again.  Should accept.
+	 */
+	i = fcntl(lso, F_GETFL);
+	if (i < 0)
+		errx(-1, "not ok 11 - ioctl(F_GETFL): %s", strerror(errno));
+	i |= O_NONBLOCK;
+	if (fcntl(lso, F_SETFL, i) != 0)
+		errx(-1, "not ok 11 - ioctl(F_SETFL): %s", strerror(errno));
+	so = socket(PF_INET, SOCK_STREAM, 0);
+	if (so == -1)
+		errx(-1, "not ok 11 - socket: %s", strerror(errno));
+	if (connect(so, (struct sockaddr *)&sin, sizeof(sin)) < 0)
+		errx(-1, "not ok 11 - connect %s", strerror(errno));
+	if (accept(lso, NULL, 0) != -1 && errno != EWOULDBLOCK)
+		errx(-1, "not ok 11 - accept #1 %s", strerror(errno));
+	if (write(so, "0", 1) != 1)
+		errx(-1, "not ok 11 - write %s", strerror(errno));
+	/*
+	 * XXXGL: ugly, but we need to make sure that our write reaches
+	 * remote side of the socket.
+	 */
+	usleep(10000);
+	if (accept(lso, NULL, 0) < 1)
+		errx(-1, "not ok 11 - accept #2 %s", strerror(errno));
+	printf("ok 11 - accept\n");
+
+	/*
+	 * Step 11: Remove accept filter.  After removing the accept filter
 	 * getsockopt() should fail with EINVAL.
 	 */
 	ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, NULL, 0);
 	if (ret != 0)
-		errx(-1, "not ok 11 - setsockopt() after listen() "
+		errx(-1, "not ok 12 - setsockopt() after listen() "
 		    "failed with %d (%s)", errno, strerror(errno));
 	bzero(&afa, sizeof(afa));
 	len = sizeof(afa);
 	ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
 	if (ret == 0)
-		errx(-1, "not ok 11 - getsockopt() after removing "
+		errx(-1, "not ok 12 - getsockopt() after removing "
 		    "the accept filter returns valid accept filter %s",
 		    afa.af_name);
 	if (errno != EINVAL)
-		errx(-1, "not ok 11 - getsockopt() after removing the accept"
+		errx(-1, "not ok 12 - getsockopt() after removing the accept"
 		    "filter failed with %d (%s)", errno, strerror(errno));
-	printf("ok 11 - setsockopt\n");
+	printf("ok 12 - setsockopt\n");
 
 	close(lso);
 	return (0);



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