Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 17 Oct 2015 04:07:41 +0000 (UTC)
From:      Garrett Cooper <ngie@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-user@freebsd.org
Subject:   svn commit: r289440 - user/ngie/more-tests2/tests/sys/posixshm
Message-ID:  <201510170407.t9H47fMZ097604@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ngie
Date: Sat Oct 17 04:07:41 2015
New Revision: 289440
URL: https://svnweb.freebsd.org/changeset/base/289440

Log:
  - Sprinkle around some errno diagnostics with atf_tc_fail calls
    where needed
  - Remove unreachable statements after atf_tc_fail is called

Modified:
  user/ngie/more-tests2/tests/sys/posixshm/posixshm_test.c

Modified: user/ngie/more-tests2/tests/sys/posixshm/posixshm_test.c
==============================================================================
--- user/ngie/more-tests2/tests/sys/posixshm/posixshm_test.c	Sat Oct 17 04:03:53 2015	(r289439)
+++ user/ngie/more-tests2/tests/sys/posixshm/posixshm_test.c	Sat Oct 17 04:07:41 2015	(r289440)
@@ -110,18 +110,18 @@ scribble_object(void)
 		fd = shm_open(test_path, O_CREAT | O_EXCL | O_RDWR, 0777);
 	}
 	if (fd < 0)
-		atf_tc_fail("shm_open");
+		atf_tc_fail("shm_open failed; errno=%d", errno);
 	if (ftruncate(fd, getpagesize()) < 0)
-		atf_tc_fail("ftruncate");
+		atf_tc_fail("ftruncate failed; errno=%d", errno);
 
 	page = mmap(0, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd,
 	    0);
 	if (page == MAP_FAILED)
-		atf_tc_fail("mmap");
+		atf_tc_fail("mmap failed; errno=%d", errno);
 
 	page[0] = '1';
 	if (munmap(page, getpagesize()) < 0)
-		atf_tc_fail("munmap");
+		atf_tc_fail("munmap failed; errno=%d", errno);
 
 	return (fd);
 }
@@ -136,32 +136,18 @@ ATF_TC_BODY(remap_object, tc)
 
 	page = mmap(0, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd,
 	    0);
-	if (page == MAP_FAILED) {		
-		atf_tc_fail("mmap(2)");
-		close(fd);
-		shm_unlink(test_path);
-		return;
-	}
+	if (page == MAP_FAILED)
+		atf_tc_fail("mmap(2) failed; errno=%d", errno);
 
-	if (page[0] != '1') {		
-		atf_tc_fail("missing data");
-		close(fd);
-		shm_unlink(test_path);
-		return;
-	}
+	if (page[0] != '1')
+		atf_tc_fail("missing data ('%c' != '1')", page[0]);
 
 	close(fd);
-	if (munmap(page, getpagesize()) < 0) {
-		atf_tc_fail("munmap");
-		shm_unlink(test_path);
-		return;
-	}
-
-	if (shm_unlink(test_path) < 0) {
-		atf_tc_fail("shm_unlink");
-		return;
-	}
+	if (munmap(page, getpagesize()) < 0)
+		atf_tc_fail("munmap failed; errno=%d", errno);
 
+	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
+	    "shm_unlink failed; errno=%d", errno);
 }
 
 ATF_TC_WITHOUT_HEAD(reopen_object);
@@ -174,30 +160,20 @@ ATF_TC_BODY(reopen_object, tc)
 	close(fd);
 
 	fd = shm_open(test_path, O_RDONLY, 0777);
-	if (fd < 0) {
-		atf_tc_fail("shm_open(2)");
-		shm_unlink(test_path);
-		return;
-	}
+	if (fd < 0)
+		atf_tc_fail("shm_open(2) failed; errno=%d", errno);
+
 	page = mmap(0, getpagesize(), PROT_READ, MAP_SHARED, fd, 0);
-	if (page == MAP_FAILED) {
-		atf_tc_fail("mmap(2)");
-		close(fd);
-		shm_unlink(test_path);
-		return;
-	}
+	if (page == MAP_FAILED)
+		atf_tc_fail("mmap(2) failed; errno=%d", errno);
 
-	if (page[0] != '1') {
-		atf_tc_fail("missing data");
-		munmap(page, getpagesize());
-		close(fd);
-		shm_unlink(test_path);
-		return;
-	}
+	if (page[0] != '1')
+		atf_tc_fail("missing data ('%c' != '1')", page[0]);
 
 	munmap(page, getpagesize());
 	close(fd);
-	shm_unlink(test_path);
+	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
+	    "shm_unlink failed; errno=%d", errno);
 }
 
 ATF_TC_WITHOUT_HEAD(readonly_mmap_write);
@@ -209,30 +185,21 @@ ATF_TC_BODY(readonly_mmap_write, tc)
 	gen_test_path();
 
 	fd = shm_open(test_path, O_RDONLY | O_CREAT, 0777);
-	if (fd < 0) {
-		atf_tc_fail("shm_open");
-		return;
-	}
+	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
 
 	/* PROT_WRITE should fail with EACCES. */
 	page = mmap(0, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd,
 	    0);
-	if (page != MAP_FAILED) {		
-		atf_tc_fail("mmap(PROT_WRITE) succeeded");
-		munmap(page, getpagesize());
-		close(fd);
-		shm_unlink(test_path);
-		return;
-	}
-	if (errno != EACCES) {
-		atf_tc_fail("mmap");
-		close(fd);
-		shm_unlink(test_path);
-		return;
-	}
+	if (page != MAP_FAILED)
+		atf_tc_fail("mmap(PROT_WRITE) succeeded unexpectedly");
+
+	if (errno != EACCES)
+		atf_tc_fail("mmap(PROT_WRITE) didn't fail with EACCES; "
+		    "errno=%d", errno);
 
 	close(fd);
-	shm_unlink(test_path);
+	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
+	    "shm_unlink failed; errno=%d", errno);
 }
 
 ATF_TC_WITHOUT_HEAD(open_after_link);
@@ -243,16 +210,11 @@ ATF_TC_BODY(open_after_link, tc)
 	gen_test_path();
 
 	fd = shm_open(test_path, O_RDONLY | O_CREAT, 0777);
-	if (fd < 0) {
-		atf_tc_fail("shm_open(1)");
-		return;
-	}
+	ATF_REQUIRE_MSG(fd >= 0, "shm_open(1) failed; errno=%d", errno);
 	close(fd);
 
-	if (shm_unlink(test_path) < 0) {
-		atf_tc_fail("shm_unlink");
-		return;
-	}
+	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1, "shm_unlink failed: %d",
+	    errno);
 
 	shm_open_should_fail(test_path, O_RDONLY, 0777, ENOENT);
 }
@@ -288,10 +250,7 @@ ATF_TC_BODY(open_anon, tc)
 	int fd;
 
 	fd = shm_open(SHM_ANON, O_RDWR, 0777);
-	if (fd < 0) {
-		atf_tc_fail("shm_open");
-		return;
-	}
+	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
 	close(fd);
 }
 
@@ -336,13 +295,14 @@ ATF_TC_BODY(open_create_existing_object,
 	gen_test_path();
 
 	fd = shm_open(test_path, O_RDONLY|O_CREAT, 0777);
-	ATF_REQUIRE_MSG(fd != -1, "shm_open(O_CREAT) failed; errno=%d", errno);
+	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
 	close(fd);
 
 	shm_open_should_fail(test_path, O_RDONLY|O_CREAT|O_EXCL,
 	    0777, EEXIST);
 
-	shm_unlink("shm_object");
+	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
+	    "shm_unlink failed; errno=%d", errno);
 }
 
 ATF_TC_WITHOUT_HEAD(trunc_resets_object);
@@ -355,48 +315,24 @@ ATF_TC_BODY(trunc_resets_object, tc)
 
 	/* Create object and set size to 1024. */
 	fd = shm_open(test_path, O_RDWR | O_CREAT, 0777);
-	if (fd < 0) {
-		atf_tc_fail("shm_open(1)");
-		return;
-	}
-	if (ftruncate(fd, 1024) < 0) {
-		atf_tc_fail("ftruncate");
-		close(fd);
-		return;
-	}
-	if (fstat(fd, &sb) < 0) {		
-		atf_tc_fail("fstat(1)");
-		close(fd);
-		return;
-	}
-	if (sb.st_size != 1024) {
-		atf_tc_fail("size %d != 1024", (int)sb.st_size);
-		close(fd);
-		return;
-	}
+	ATF_REQUIRE_MSG(fd >= 0, "shm_open(1) failed; errno=%d", errno);
+	ATF_REQUIRE_MSG(ftruncate(fd, 1024) != -1,
+	    "ftruncate failed; errno=%d", errno);
+	ATF_REQUIRE_MSG(fstat(fd, &sb) != -1,
+	    "fstat(1) failed; errno=%d", errno);
+	ATF_REQUIRE_MSG(sb.st_size == 1024, "size %d != 1024", (int)sb.st_size);
 	close(fd);
 
 	/* Open with O_TRUNC which should reset size to 0. */
 	fd = shm_open(test_path, O_RDWR | O_TRUNC, 0777);
-	if (fd < 0) {
-		atf_tc_fail("shm_open(2)");
-		return;
-	}
-	if (fstat(fd, &sb) < 0) {
-		atf_tc_fail("fstat(2)");
-		close(fd);
-		return;
-	}
-	if (sb.st_size != 0) {
-		atf_tc_fail("size after O_TRUNC %d != 0", (int)sb.st_size);
-		close(fd);
-		return;
-	}
+	ATF_REQUIRE_MSG(fd >= 0, "shm_open(2) failed; errno=%d", errno);
+	ATF_REQUIRE_MSG(fstat(fd, &sb) != -1,
+	    "fstat(2) failed; errno=%d", errno);
+	ATF_REQUIRE_MSG(sb.st_size == 0,
+	    "size was not 0 after truncation: %d", (int)sb.st_size);
 	close(fd);
-	if (shm_unlink(test_path) < 0) {
-		atf_tc_fail("shm_unlink");
-		return;
-	}
+	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
+	    "shm_unlink failed; errno=%d", errno);
 }
 
 ATF_TC_WITHOUT_HEAD(unlink_bad_path_pointer);
@@ -423,110 +359,76 @@ ATF_TC_BODY(object_resize, tc)
 {
 	pid_t pid;
 	struct stat sb;
-	char *page;
+	char err_buf[1024], *page;
 	int fd, status;
 
 	/* Start off with a size of a single page. */
-	fd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0777);
-	if (fd < 0) {
-		atf_tc_fail("shm_open");
-		return;
-	}
-	if (ftruncate(fd, getpagesize()) < 0) {
-		atf_tc_fail("ftruncate(1)");
-		close(fd);
-		return;
-	}
-	if (fstat(fd, &sb) < 0) {
-		atf_tc_fail("fstat(1)");
-		close(fd);
-		return;
-	}
-	if (sb.st_size != getpagesize()) {
-		atf_tc_fail("first resize failed");
-		close(fd);
-		return;
-	}
+	fd = shm_open(SHM_ANON, O_CREAT|O_RDWR, 0777);
+	if (fd < 0)
+		atf_tc_fail("shm_open failed; errno=%d", errno);
+
+	if (ftruncate(fd, getpagesize()) < 0)
+		atf_tc_fail("ftruncate(1) failed; errno=%d", errno);
+
+	if (fstat(fd, &sb) < 0)
+		atf_tc_fail("fstat(1) failed; errno=%d", errno);
+
+	if (sb.st_size != getpagesize())
+		atf_tc_fail("first resize failed (%d != %d)",
+		    (int)sb.st_size, getpagesize());
 
 	/* Write a '1' to the first byte. */
-	page = mmap(0, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd,
+	page = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd,
 	    0);
-	if (page == MAP_FAILED) {
+	if (page == MAP_FAILED)
 		atf_tc_fail("mmap(1)");
-		close(fd);
-		return;
-	}
 
 	page[0] = '1';
 
-	if (munmap(page, getpagesize()) < 0) {
-		atf_tc_fail("munmap(1)");
-		close(fd);
-		return;
-	}
+	if (munmap(page, getpagesize()) < 0)
+		atf_tc_fail("munmap(1) failed; errno=%d", errno);
 
 	/* Grow the object to 2 pages. */
-	if (ftruncate(fd, getpagesize() * 2) < 0) {
-		atf_tc_fail("ftruncate(2)");
-		close(fd);
-		return;
-	}
-	if (fstat(fd, &sb) < 0) {
-		atf_tc_fail("fstat(2)");
-		close(fd);
-		return;
-	}
-	if (sb.st_size != getpagesize() * 2) {
-		atf_tc_fail("second resize failed");
-		close(fd);
-		return;
-	}
+	if (ftruncate(fd, getpagesize() * 2) < 0)
+		atf_tc_fail("ftruncate(2) failed; errno=%d", errno);
+
+	if (fstat(fd, &sb) < 0)
+		atf_tc_fail("fstat(2) failed; errno=%d", errno);
+
+	if (sb.st_size != getpagesize() * 2)
+		atf_tc_fail("second resize failed (%d != %d)",
+		    (int)sb.st_size, getpagesize() * 2);
 
 	/* Check for '1' at the first byte. */
-	page = mmap(0, getpagesize() * 2, PROT_READ | PROT_WRITE, MAP_SHARED,
+	page = mmap(0, getpagesize() * 2, PROT_READ|PROT_WRITE, MAP_SHARED,
 	    fd, 0);
-	if (page == MAP_FAILED) {
-		atf_tc_fail("mmap(2)");
-		close(fd);
-		return;
-	}
+	if (page == MAP_FAILED)
+		atf_tc_fail("mmap(2) failed; errno=%d", errno);
 
-	if (page[0] != '1') {
-		atf_tc_fail("missing data at 0");
-		close(fd);
-		return;
-	}
+	if (page[0] != '1')
+		atf_tc_fail("'%c' != '1'", page[0]);
 
 	/* Write a '2' at the start of the second page. */
 	page[getpagesize()] = '2';
 
 	/* Shrink the object back to 1 page. */
-	if (ftruncate(fd, getpagesize()) < 0) {
-		atf_tc_fail("ftruncate(3)");
-		close(fd);
-		return;
-	}
-	if (fstat(fd, &sb) < 0) {
-		atf_tc_fail("fstat(3)");
-		close(fd);
-		return;
-	}
-	if (sb.st_size != getpagesize()) {
-		atf_tc_fail("third resize failed");
-		close(fd);
-		return;
-	}
+	if (ftruncate(fd, getpagesize()) < 0)
+		atf_tc_fail("ftruncate(3) failed; errno=%d", errno);
+
+	if (fstat(fd, &sb) < 0)
+		atf_tc_fail("fstat(3) failed; errno=%d", errno);
+
+	if (sb.st_size != getpagesize())
+		atf_tc_fail("third resize failed (%d != %d)",
+		    (int)sb.st_size, getpagesize());
 
 	/*
 	 * Fork a child process to make sure the second page is no
 	 * longer valid.
 	 */
 	pid = fork();
-	if (pid < 0) {
-		atf_tc_fail("fork");
-		close(fd);
-		return;
-	}
+	if (pid == -1)
+		atf_tc_fail("fork failed; errno=%d", errno);
 
 	if (pid == 0) {
 		struct rlimit lim;
@@ -546,33 +448,23 @@ ATF_TC_BODY(object_resize, tc)
 		fprintf(stderr, "child: page 1: '%c'\n", c);
 		exit(0);
 	}
-	if (wait(&status) < 0) {
-		atf_tc_fail("wait");
-		close(fd);
-		return;
-	}
-	if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGSEGV) {
+
+	if (wait(&status) < 0)
+		atf_tc_fail("wait failed; errno=%d", errno);
+
+	if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGSEGV)
 		atf_tc_fail("child terminated with status %x", status);
-		close(fd);
-		return;
-	}
 
 	/* Grow the object back to 2 pages. */
-	if (ftruncate(fd, getpagesize() * 2) < 0) {
-		atf_tc_fail("ftruncate(4)");
-		close(fd);
-		return;
-	}
-	if (fstat(fd, &sb) < 0) {
-		atf_tc_fail("fstat(4)");
-		close(fd);
-		return;
-	}
-	if (sb.st_size != getpagesize() * 2) {
-		atf_tc_fail("second resize failed");
-		close(fd);
-		return;
-	}
+	if (ftruncate(fd, getpagesize() * 2) < 0)
+		atf_tc_fail("ftruncate(2) failed; errno=%d", errno);
+
+	if (fstat(fd, &sb) < 0)
+		atf_tc_fail("fstat(2) failed; errno=%d", errno);
+
+	if (sb.st_size != getpagesize() * 2)
+		atf_tc_fail("fourth resize failed (%d != %d)",
+		    (int)sb.st_size, getpagesize());
 
 	/*
 	 * Note that the mapping at 'page' for the second page is
@@ -583,11 +475,9 @@ ATF_TC_BODY(object_resize, tc)
 	 * object was shrunk and the new pages when an object are
 	 * grown are zero-filled.
 	 */
-	if (page[getpagesize()] != 0) {
-		atf_tc_fail("invalid data at %d", getpagesize());
-		close(fd);
-		return;
-	}
+	if (page[getpagesize()] != 0)
+		atf_tc_fail("invalid data at %d: %x != 0",
+		    getpagesize(), (int)page[getpagesize()]);
 
 	close(fd);
 }



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