svn commit: r289437 - user/ngie/more-tests2/tests/sys/posixshm

Garrett Cooper ngie at FreeBSD.org
Sat Oct 17 03:13:23 UTC 2015


Author: ngie
Date: Sat Oct 17 03:13:22 2015
New Revision: 289437
URL: https://svnweb.freebsd.org/changeset/base/289437

Log:
  Unify posixshm.c and shm_test.c
  
  Convert both testcases over to ATF
  
  Don't use hardcoded paths to /tmp; use mkstemp to generate
  temporary paths for non-SHM_ANON created shm objects.

Added:
  user/ngie/more-tests2/tests/sys/posixshm/posixshm_test.c
     - copied, changed from r289236, user/ngie/more-tests2/tests/sys/posixshm/posixshm.c
Deleted:
  user/ngie/more-tests2/tests/sys/posixshm/posixshm.c
  user/ngie/more-tests2/tests/sys/posixshm/shm_test.c
  user/ngie/more-tests2/tests/sys/posixshm/test.c
  user/ngie/more-tests2/tests/sys/posixshm/test.h
Modified:
  user/ngie/more-tests2/tests/sys/posixshm/Makefile

Modified: user/ngie/more-tests2/tests/sys/posixshm/Makefile
==============================================================================
--- user/ngie/more-tests2/tests/sys/posixshm/Makefile	Sat Oct 17 02:49:19 2015	(r289436)
+++ user/ngie/more-tests2/tests/sys/posixshm/Makefile	Sat Oct 17 03:13:22 2015	(r289437)
@@ -2,10 +2,6 @@
 
 TESTSDIR=	${TESTSBASE}/sys/posixshm
 
-TAP_TESTS_C+=	posixshm_test
-PLAIN_TESTS_C+=	posixshm_test2
-
-SRCS.posixshm_test=	posixshm.c test.c
-SRCS.posixshm_test2=	shm_test.c
+ATF_TESTS_C+=	posixshm_test
 
 .include <bsd.test.mk>

Copied and modified: user/ngie/more-tests2/tests/sys/posixshm/posixshm_test.c (from r289236, user/ngie/more-tests2/tests/sys/posixshm/posixshm.c)
==============================================================================
--- user/ngie/more-tests2/tests/sys/posixshm/posixshm.c	Tue Oct 13 17:28:11 2015	(r289236, copy source)
+++ user/ngie/more-tests2/tests/sys/posixshm/posixshm_test.c	Sat Oct 17 03:13:22 2015	(r289437)
@@ -36,14 +36,32 @@ __FBSDID("$FreeBSD$");
 
 #include <errno.h>
 #include <fcntl.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
-#include "test.h"
+#include <atf-c.h>
 
-#define	TEST_PATH	"/tmp/posixshm_regression_test"
+#define	TEST_PATH_LEN	256
+static char test_path[TEST_PATH_LEN];
+
+static void
+gen_test_path(void)
+{
+	char *tmpdir = getenv("TMPDIR");
+
+	if (tmpdir == NULL)
+		tmpdir = "/tmp";
+
+	snprintf(test_path, sizeof(test_path), "%s/tmp.XXXXXX", tmpdir);
+	test_path[sizeof(test_path) - 1] = '\0';
+	ATF_REQUIRE_MSG(mkstemp(test_path) != -1,
+	    "mkstemp failed; errno=%d", errno);
+	ATF_REQUIRE_MSG(unlink(test_path) == 0,
+	    "unlink failed; errno=%d", errno);
+}
 
 /*
  * Attempt a shm_open() that should fail with an expected error of 'error'.
@@ -54,16 +72,10 @@ shm_open_should_fail(const char *path, i
 	int fd;
 
 	fd = shm_open(path, flags, mode);
-	if (fd >= 0) {
-		fail_err("shm_open() didn't fail");
-		close(fd);
-		return;
-	}
-	if (errno != error) {
-		fail_errno("shm_open");
-		return;
-	}
-	pass();
+	ATF_CHECK_MSG(fd == -1, "shm_open didn't fail");
+	ATF_CHECK_MSG(error == errno,
+	    "shm_open didn't fail with expected errno; errno=%d; expected "
+	    "errno=%d", errno, error);
 }
 
 /*
@@ -73,15 +85,10 @@ static void
 shm_unlink_should_fail(const char *path, int error)
 {
 
-	if (shm_unlink(path) >= 0) {
-		fail_err("shm_unlink() didn't fail");
-		return;
-	}
-	if (errno != error) {
-		fail_errno("shm_unlink");
-		return;
-	}
-	pass();
+	ATF_CHECK_MSG(shm_unlink(path) == -1, "shm_unlink didn't fail");
+	ATF_CHECK_MSG(error == errno,
+	    "shm_unlink didn't fail with expected errno; errno=%d; expected "
+	    "errno=%d", errno, error);
 }
 
 /*
@@ -94,137 +101,116 @@ scribble_object(void)
 	char *page;
 	int fd;
 
-	fd = shm_open(TEST_PATH, O_CREAT | O_EXCL | O_RDWR, 0777);
+	gen_test_path();
+
+	fd = shm_open(test_path, O_CREAT|O_EXCL|O_RDWR, 0777);
 	if (fd < 0 && errno == EEXIST) {
-		if (shm_unlink(TEST_PATH) < 0) {
-			fail_errno("shm_unlink");
-			return (-1);
-		}
-		fd = shm_open(TEST_PATH, O_CREAT | O_EXCL | O_RDWR, 0777);
-	}
-	if (fd < 0) {
-		fail_errno("shm_open");
-		return (-1);
-	}
-	if (ftruncate(fd, getpagesize()) < 0) {
-		fail_errno("ftruncate");
-		close(fd);
-		shm_unlink(TEST_PATH);
-		return (-1);
+		if (shm_unlink(test_path) < 0)
+			atf_tc_fail("shm_unlink");
+		fd = shm_open(test_path, O_CREAT | O_EXCL | O_RDWR, 0777);
 	}
+	if (fd < 0)
+		atf_tc_fail("shm_open");
+	if (ftruncate(fd, getpagesize()) < 0)
+		atf_tc_fail("ftruncate");
 
 	page = mmap(0, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd,
 	    0);
-	if (page == MAP_FAILED) {
-		fail_errno("mmap");
-		close(fd);
-		shm_unlink(TEST_PATH);
-		return (-1);
-	}
+	if (page == MAP_FAILED)
+		atf_tc_fail("mmap");
 
 	page[0] = '1';
-
-	if (munmap(page, getpagesize()) < 0) {
-		fail_errno("munmap");
-		close(fd);
-		shm_unlink(TEST_PATH);
-		return (-1);
-	}
+	if (munmap(page, getpagesize()) < 0)
+		atf_tc_fail("munmap");
 
 	return (fd);
 }
 
-static void
-remap_object(void)
+ATF_TC_WITHOUT_HEAD(remap_object);
+ATF_TC_BODY(remap_object, tc)
 {
 	char *page;
 	int fd;
 
 	fd = scribble_object();
-	if (fd < 0)
-		return;
 
 	page = mmap(0, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd,
 	    0);
 	if (page == MAP_FAILED) {		
-		fail_errno("mmap(2)");
+		atf_tc_fail("mmap(2)");
 		close(fd);
-		shm_unlink(TEST_PATH);
+		shm_unlink(test_path);
 		return;
 	}
 
 	if (page[0] != '1') {		
-		fail_err("missing data");
+		atf_tc_fail("missing data");
 		close(fd);
-		shm_unlink(TEST_PATH);
+		shm_unlink(test_path);
 		return;
 	}
 
 	close(fd);
 	if (munmap(page, getpagesize()) < 0) {
-		fail_errno("munmap");
-		shm_unlink(TEST_PATH);
+		atf_tc_fail("munmap");
+		shm_unlink(test_path);
 		return;
 	}
 
-	if (shm_unlink(TEST_PATH) < 0) {
-		fail_errno("shm_unlink");
+	if (shm_unlink(test_path) < 0) {
+		atf_tc_fail("shm_unlink");
 		return;
 	}
 
-	pass();
 }
-TEST(remap_object, "remap object");
 
-static void
-reopen_object(void)
+ATF_TC_WITHOUT_HEAD(reopen_object);
+ATF_TC_BODY(reopen_object, tc)
 {
 	char *page;
 	int fd;
 
 	fd = scribble_object();
-	if (fd < 0)
-		return;
 	close(fd);
 
-	fd = shm_open(TEST_PATH, O_RDONLY, 0777);
+	fd = shm_open(test_path, O_RDONLY, 0777);
 	if (fd < 0) {
-		fail_errno("shm_open(2)");
-		shm_unlink(TEST_PATH);
+		atf_tc_fail("shm_open(2)");
+		shm_unlink(test_path);
 		return;
 	}
 	page = mmap(0, getpagesize(), PROT_READ, MAP_SHARED, fd, 0);
 	if (page == MAP_FAILED) {
-		fail_errno("mmap(2)");
+		atf_tc_fail("mmap(2)");
 		close(fd);
-		shm_unlink(TEST_PATH);
+		shm_unlink(test_path);
 		return;
 	}
 
 	if (page[0] != '1') {
-		fail_err("missing data");
+		atf_tc_fail("missing data");
 		munmap(page, getpagesize());
 		close(fd);
-		shm_unlink(TEST_PATH);
+		shm_unlink(test_path);
 		return;
 	}
 
 	munmap(page, getpagesize());
 	close(fd);
-	shm_unlink(TEST_PATH);
-	pass();
+	shm_unlink(test_path);
 }
-TEST(reopen_object, "reopen object");
 
-static void
-readonly_mmap_write(void)
+ATF_TC_WITHOUT_HEAD(readonly_mmap_write);
+ATF_TC_BODY(readonly_mmap_write, tc)
 {
 	char *page;
 	int fd;
 
-	fd = shm_open(TEST_PATH, O_RDONLY | O_CREAT, 0777);
+	gen_test_path();
+
+	fd = shm_open(test_path, O_RDONLY | O_CREAT, 0777);
 	if (fd < 0) {
-		fail_errno("shm_open");
+		atf_tc_fail("shm_open");
 		return;
 	}
 
@@ -232,103 +218,99 @@ readonly_mmap_write(void)
 	page = mmap(0, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd,
 	    0);
 	if (page != MAP_FAILED) {		
-		fail_err("mmap(PROT_WRITE) succeeded");
+		atf_tc_fail("mmap(PROT_WRITE) succeeded");
 		munmap(page, getpagesize());
 		close(fd);
-		shm_unlink(TEST_PATH);
+		shm_unlink(test_path);
 		return;
 	}
 	if (errno != EACCES) {
-		fail_errno("mmap");
+		atf_tc_fail("mmap");
 		close(fd);
-		shm_unlink(TEST_PATH);
+		shm_unlink(test_path);
 		return;
 	}
 
 	close(fd);
-	shm_unlink(TEST_PATH);
-	pass();
+	shm_unlink(test_path);
 }
-TEST(readonly_mmap_write, "RDONLY object");
 
-static void
-open_after_unlink(void)
+ATF_TC_WITHOUT_HEAD(open_after_link);
+ATF_TC_BODY(open_after_link, tc)
 {
 	int fd;
 
-	fd = shm_open(TEST_PATH, O_RDONLY | O_CREAT, 0777);
+	gen_test_path();
+
+	fd = shm_open(test_path, O_RDONLY | O_CREAT, 0777);
 	if (fd < 0) {
-		fail_errno("shm_open(1)");
+		atf_tc_fail("shm_open(1)");
 		return;
 	}
 	close(fd);
 
-	if (shm_unlink(TEST_PATH) < 0) {
-		fail_errno("shm_unlink");
+	if (shm_unlink(test_path) < 0) {
+		atf_tc_fail("shm_unlink");
 		return;
 	}
 
-	shm_open_should_fail(TEST_PATH, O_RDONLY, 0777, ENOENT);
+	shm_open_should_fail(test_path, O_RDONLY, 0777, ENOENT);
 }
-TEST(open_after_unlink, "open after unlink");
 
-static void
-open_invalid_path(void)
+ATF_TC_WITHOUT_HEAD(open_invalid_path);
+ATF_TC_BODY(open_invalid_path, tc)
 {
 
 	shm_open_should_fail("blah", O_RDONLY, 0777, EINVAL);
 }
-TEST(open_invalid_path, "open invalid path");
 
-static void
-open_write_only(void)
+ATF_TC_WITHOUT_HEAD(open_write_only);
+ATF_TC_BODY(open_write_only, tc)
 {
 
-	shm_open_should_fail(TEST_PATH, O_WRONLY, 0777, EINVAL);
+	gen_test_path();
+
+	shm_open_should_fail(test_path, O_WRONLY, 0777, EINVAL);
 }
-TEST(open_write_only, "open with O_WRONLY");
 
-static void
-open_extra_flags(void)
+ATF_TC_WITHOUT_HEAD(open_extra_flags);
+ATF_TC_BODY(open_extra_flags, tc)
 {
 
-	shm_open_should_fail(TEST_PATH, O_RDONLY | O_DIRECT, 0777, EINVAL);
+	gen_test_path();
+
+	shm_open_should_fail(test_path, O_RDONLY | O_DIRECT, 0777, EINVAL);
 }
-TEST(open_extra_flags, "open with extra flags");
 
-static void
-open_anon(void)
+ATF_TC_WITHOUT_HEAD(open_anon);
+ATF_TC_BODY(open_anon, tc)
 {
 	int fd;
 
 	fd = shm_open(SHM_ANON, O_RDWR, 0777);
 	if (fd < 0) {
-		fail_errno("shm_open");
+		atf_tc_fail("shm_open");
 		return;
 	}
 	close(fd);
-	pass();
 }
-TEST(open_anon, "open anonymous object");
 
-static void
-open_anon_readonly(void)
+ATF_TC_WITHOUT_HEAD(open_anon_readonly);
+ATF_TC_BODY(open_anon_readonly, tc)
 {
 
 	shm_open_should_fail(SHM_ANON, O_RDONLY, 0777, EINVAL);
 }
-TEST(open_anon_readonly, "open SHM_ANON with O_RDONLY");
 
-static void
-open_bad_path_pointer(void)
+ATF_TC_WITHOUT_HEAD(open_bad_path_pointer);
+ATF_TC_BODY(open_bad_path_pointer, tc)
 {
 
 	shm_open_should_fail((char *)1024, O_RDONLY, 0777, EFAULT);
 }
-TEST(open_bad_path_pointer, "open bad path pointer");
 
-static void
-open_path_too_long(void)
+ATF_TC_WITHOUT_HEAD(open_path_too_long);
+ATF_TC_BODY(open_path_too_long, tc)
 {
 	char *page;
 
@@ -338,99 +320,94 @@ open_path_too_long(void)
 	shm_open_should_fail(page, O_RDONLY, 0777, ENAMETOOLONG);
 	free(page);
 }
-TEST(open_path_too_long, "open pathname too long");
 
-static void
-open_nonexisting_object(void)
+ATF_TC_WITHOUT_HEAD(open_nonexisting_object);
+ATF_TC_BODY(open_nonexisting_object, tc)
 {
 
 	shm_open_should_fail("/notreallythere", O_RDONLY, 0777, ENOENT);
 }
-TEST(open_nonexisting_object, "open nonexistent object");
 
-static void
-exclusive_create_existing_object(void)
+ATF_TC_WITHOUT_HEAD(open_create_existing_object);
+ATF_TC_BODY(open_create_existing_object, tc)
 {
 	int fd;
 
-	fd = shm_open("/tmp/notreallythere", O_RDONLY | O_CREAT, 0777);
-	if (fd < 0) {
-		fail_errno("shm_open(O_CREAT)");
-		return;
-	}
+	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);
 	close(fd);
 
-	shm_open_should_fail("/tmp/notreallythere", O_RDONLY | O_CREAT | O_EXCL,
+	shm_open_should_fail(test_path, O_RDONLY|O_CREAT|O_EXCL,
 	    0777, EEXIST);
 
-	shm_unlink("/tmp/notreallythere");
+	shm_unlink("shm_object");
 }
-TEST(exclusive_create_existing_object, "O_EXCL of existing object");
 
-static void
-trunc_resets_object(void)
+ATF_TC_WITHOUT_HEAD(trunc_resets_object);
+ATF_TC_BODY(trunc_resets_object, tc)
 {
 	struct stat sb;
 	int fd;
 
+	gen_test_path();
+
 	/* Create object and set size to 1024. */
-	fd = shm_open(TEST_PATH, O_RDWR | O_CREAT, 0777);
+	fd = shm_open(test_path, O_RDWR | O_CREAT, 0777);
 	if (fd < 0) {
-		fail_errno("shm_open(1)");
+		atf_tc_fail("shm_open(1)");
 		return;
 	}
 	if (ftruncate(fd, 1024) < 0) {
-		fail_errno("ftruncate");
+		atf_tc_fail("ftruncate");
 		close(fd);
 		return;
 	}
 	if (fstat(fd, &sb) < 0) {		
-		fail_errno("fstat(1)");
+		atf_tc_fail("fstat(1)");
 		close(fd);
 		return;
 	}
 	if (sb.st_size != 1024) {
-		fail_err("size %d != 1024", (int)sb.st_size);
+		atf_tc_fail("size %d != 1024", (int)sb.st_size);
 		close(fd);
 		return;
 	}
 	close(fd);
 
 	/* Open with O_TRUNC which should reset size to 0. */
-	fd = shm_open(TEST_PATH, O_RDWR | O_TRUNC, 0777);
+	fd = shm_open(test_path, O_RDWR | O_TRUNC, 0777);
 	if (fd < 0) {
-		fail_errno("shm_open(2)");
+		atf_tc_fail("shm_open(2)");
 		return;
 	}
 	if (fstat(fd, &sb) < 0) {
-		fail_errno("fstat(2)");
+		atf_tc_fail("fstat(2)");
 		close(fd);
 		return;
 	}
 	if (sb.st_size != 0) {
-		fail_err("size after O_TRUNC %d != 0", (int)sb.st_size);
+		atf_tc_fail("size after O_TRUNC %d != 0", (int)sb.st_size);
 		close(fd);
 		return;
 	}
 	close(fd);
-	if (shm_unlink(TEST_PATH) < 0) {
-		fail_errno("shm_unlink");
+	if (shm_unlink(test_path) < 0) {
+		atf_tc_fail("shm_unlink");
 		return;
 	}
-	pass();
 }
-TEST(trunc_resets_object, "O_TRUNC resets size");
 
-static void
-unlink_bad_path_pointer(void)
+ATF_TC_WITHOUT_HEAD(unlink_bad_path_pointer);
+ATF_TC_BODY(unlink_bad_path_pointer, tc)
 {
 
 	shm_unlink_should_fail((char *)1024, EFAULT);
 }
-TEST(unlink_bad_path_pointer, "unlink bad path pointer");
 
-static void
-unlink_path_too_long(void)
+ATF_TC_WITHOUT_HEAD(unlink_path_too_long);
+ATF_TC_BODY(unlink_path_too_long, tc)
 {
 	char *page;
 
@@ -440,10 +417,9 @@ unlink_path_too_long(void)
 	shm_unlink_should_fail(page, ENAMETOOLONG);
 	free(page);
 }
-TEST(unlink_path_too_long, "unlink pathname too long");
 
-static void
-test_object_resize(void)
+ATF_TC_WITHOUT_HEAD(object_resize);
+ATF_TC_BODY(object_resize, tc)
 {
 	pid_t pid;
 	struct stat sb;
@@ -453,21 +429,21 @@ test_object_resize(void)
 	/* Start off with a size of a single page. */
 	fd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0777);
 	if (fd < 0) {
-		fail_errno("shm_open");
+		atf_tc_fail("shm_open");
 		return;
 	}
 	if (ftruncate(fd, getpagesize()) < 0) {
-		fail_errno("ftruncate(1)");
+		atf_tc_fail("ftruncate(1)");
 		close(fd);
 		return;
 	}
 	if (fstat(fd, &sb) < 0) {
-		fail_errno("fstat(1)");
+		atf_tc_fail("fstat(1)");
 		close(fd);
 		return;
 	}
 	if (sb.st_size != getpagesize()) {
-		fail_err("first resize failed");
+		atf_tc_fail("first resize failed");
 		close(fd);
 		return;
 	}
@@ -476,7 +452,7 @@ test_object_resize(void)
 	page = mmap(0, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd,
 	    0);
 	if (page == MAP_FAILED) {
-		fail_errno("mmap(1)");
+		atf_tc_fail("mmap(1)");
 		close(fd);
 		return;
 	}
@@ -484,24 +460,24 @@ test_object_resize(void)
 	page[0] = '1';
 
 	if (munmap(page, getpagesize()) < 0) {
-		fail_errno("munmap(1)");
+		atf_tc_fail("munmap(1)");
 		close(fd);
 		return;
 	}
 
 	/* Grow the object to 2 pages. */
 	if (ftruncate(fd, getpagesize() * 2) < 0) {
-		fail_errno("ftruncate(2)");
+		atf_tc_fail("ftruncate(2)");
 		close(fd);
 		return;
 	}
 	if (fstat(fd, &sb) < 0) {
-		fail_errno("fstat(2)");
+		atf_tc_fail("fstat(2)");
 		close(fd);
 		return;
 	}
 	if (sb.st_size != getpagesize() * 2) {
-		fail_err("second resize failed");
+		atf_tc_fail("second resize failed");
 		close(fd);
 		return;
 	}
@@ -510,13 +486,13 @@ test_object_resize(void)
 	page = mmap(0, getpagesize() * 2, PROT_READ | PROT_WRITE, MAP_SHARED,
 	    fd, 0);
 	if (page == MAP_FAILED) {
-		fail_errno("mmap(2)");
+		atf_tc_fail("mmap(2)");
 		close(fd);
 		return;
 	}
 
 	if (page[0] != '1') {
-		fail_err("missing data at 0");
+		atf_tc_fail("missing data at 0");
 		close(fd);
 		return;
 	}
@@ -526,17 +502,17 @@ test_object_resize(void)
 
 	/* Shrink the object back to 1 page. */
 	if (ftruncate(fd, getpagesize()) < 0) {
-		fail_errno("ftruncate(3)");
+		atf_tc_fail("ftruncate(3)");
 		close(fd);
 		return;
 	}
 	if (fstat(fd, &sb) < 0) {
-		fail_errno("fstat(3)");
+		atf_tc_fail("fstat(3)");
 		close(fd);
 		return;
 	}
 	if (sb.st_size != getpagesize()) {
-		fail_err("third resize failed");
+		atf_tc_fail("third resize failed");
 		close(fd);
 		return;
 	}
@@ -547,7 +523,7 @@ test_object_resize(void)
 	 */
 	pid = fork();
 	if (pid < 0) {
-		fail_errno("fork");
+		atf_tc_fail("fork");
 		close(fd);
 		return;
 	}
@@ -571,29 +547,29 @@ test_object_resize(void)
 		exit(0);
 	}
 	if (wait(&status) < 0) {
-		fail_errno("wait");
+		atf_tc_fail("wait");
 		close(fd);
 		return;
 	}
 	if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGSEGV) {
-		fail_err("child terminated with status %x", status);
+		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) {
-		fail_errno("ftruncate(4)");
+		atf_tc_fail("ftruncate(4)");
 		close(fd);
 		return;
 	}
 	if (fstat(fd, &sb) < 0) {
-		fail_errno("fstat(4)");
+		atf_tc_fail("fstat(4)");
 		close(fd);
 		return;
 	}
 	if (sb.st_size != getpagesize() * 2) {
-		fail_err("second resize failed");
+		atf_tc_fail("second resize failed");
 		close(fd);
 		return;
 	}
@@ -608,20 +584,156 @@ test_object_resize(void)
 	 * grown are zero-filled.
 	 */
 	if (page[getpagesize()] != 0) {
-		fail_err("invalid data at %d", getpagesize());
+		atf_tc_fail("invalid data at %d", getpagesize());
 		close(fd);
 		return;
 	}
 
 	close(fd);
-	pass();
 }
-TEST(test_object_resize, "object resize");
 
-int
-main(int argc, char *argv[])
+/* Signal handler which does nothing. */
+static void
+ignoreit(int sig __unused)
+{
+	;
+}
+
+ATF_TC_WITHOUT_HEAD(shm_functionality_across_fork);
+ATF_TC_BODY(shm_functionality_across_fork, tc)
+{
+	char *cp, c;
+	int error, desc, rv;
+	long scval;
+	sigset_t ss;
+	struct sigaction sa;
+	void *region;
+	size_t i, psize;
+
+#ifndef _POSIX_SHARED_MEMORY_OBJECTS
+	printf("_POSIX_SHARED_MEMORY_OBJECTS is undefined\n");
+#else
+	printf("_POSIX_SHARED_MEMORY_OBJECTS is defined as %ld\n", 
+	       (long)_POSIX_SHARED_MEMORY_OBJECTS - 0);
+	if (_POSIX_SHARED_MEMORY_OBJECTS - 0 == -1)
+		printf("***Indicates this feature may be unsupported!\n");
+#endif
+	errno = 0;
+	scval = sysconf(_SC_SHARED_MEMORY_OBJECTS);
+	if (scval == -1 && errno != 0) {
+		atf_tc_fail("sysconf(_SC_SHARED_MEMORY_OBJECTS) failed; "
+		    "errno=%d", errno);
+	} else {
+		printf("sysconf(_SC_SHARED_MEMORY_OBJECTS) returns %ld\n",
+		       scval);
+		if (scval == -1)
+			printf("***Indicates this feature is unsupported!\n");
+	}
+
+	errno = 0;
+	scval = sysconf(_SC_PAGESIZE);
+	if (scval == -1 && errno != 0) {
+		atf_tc_fail("sysconf(_SC_PAGESIZE) failed; errno=%d", errno);
+	} else if (scval <= 0 || (size_t)psize != psize) {
+		fprintf(stderr, "bogus return from sysconf(_SC_PAGESIZE): %ld",
+		    scval);
+		psize = 4096;
+	} else {
+		printf("sysconf(_SC_PAGESIZE) returns %ld\n", scval);
+		psize = scval;
+	}
+
+	gen_test_path();
+	desc = shm_open(test_path, O_EXCL | O_CREAT | O_RDWR, 0600);
+
+	ATF_REQUIRE_MSG(desc >= 0, "shm_open failed; errno=%d", errno);
+	ATF_REQUIRE_MSG(shm_unlink(test_path) == 0,
+	    "shm_unlink failed; errno=%d", errno);
+	ATF_REQUIRE_MSG(ftruncate(desc, (off_t)psize) != -1,
+	    "ftruncate failed; errno=%d", errno);
+
+	region = mmap((void *)0, psize, PROT_READ | PROT_WRITE, MAP_SHARED,
+		      desc, (off_t)0);
+	ATF_REQUIRE_MSG(region != MAP_FAILED, "mmap failed; errno=%d", errno);
+	memset(region, '\377', psize);
+
+	sa.sa_flags = 0;
+	sa.sa_handler = ignoreit;
+	sigemptyset(&sa.sa_mask);
+	ATF_REQUIRE_MSG(sigaction(SIGUSR1, &sa, (struct sigaction *)0) == 0,
+	    "sigaction failed; errno=%d", errno);
+
+	sigemptyset(&ss);
+	sigaddset(&ss, SIGUSR1);
+	ATF_REQUIRE_MSG(sigprocmask(SIG_BLOCK, &ss, (sigset_t *)0) == 0,
+	    "sigprocmask failed; errno=%d", errno);
+
+	rv = fork();
+	ATF_REQUIRE_MSG(rv != -1, "fork failed; errno=%d", errno);
+	if (rv == 0) {
+		sigemptyset(&ss);
+		sigsuspend(&ss);
+
+		for (cp = region; cp < (char *)region + psize; cp++) {
+			if (*cp != '\151')
+				_exit(1);
+		}
+		if (lseek(desc, 0, SEEK_SET) == -1)
+			_exit(1);
+		for (i = 0; i < psize; i++) {
+			error = read(desc, &c, 1);
+			if (c != '\151')
+				_exit(1);
+		}
+		_exit(0);
+	} else {
+		int status;
+
+		memset(region, '\151', psize - 2);
+		error = pwrite(desc, region, 2, psize - 2);
+		if (error != 2) {
+			if (error >= 0)
+				atf_tc_fail("short write; %d bytes written",
+				    error);
+			else
+				atf_tc_fail("shmfd write");
+		}
+		kill(rv, SIGUSR1);
+		waitpid(rv, &status, 0);
+
+		if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
+			printf("Functionality test successful\n");
+		} else if (WIFEXITED(status)) {
+			atf_tc_fail("Child process exited with status %d",
+			    WEXITSTATUS(status));
+		} else {
+			atf_tc_fail("Child process terminated with %s",
+			    strsignal(WTERMSIG(status)));
+		}
+	}
+}
+
+ATF_TP_ADD_TCS(tp)
 {
 
-	run_tests();
-	return (0);
+	ATF_TP_ADD_TC(tp, remap_object);
+	ATF_TP_ADD_TC(tp, reopen_object);
+	ATF_TP_ADD_TC(tp, readonly_mmap_write);
+	ATF_TP_ADD_TC(tp, open_after_link);
+	ATF_TP_ADD_TC(tp, open_invalid_path);
+	ATF_TP_ADD_TC(tp, open_write_only);
+	ATF_TP_ADD_TC(tp, open_extra_flags);
+	ATF_TP_ADD_TC(tp, open_anon);
+	ATF_TP_ADD_TC(tp, open_anon_readonly);
+	ATF_TP_ADD_TC(tp, open_bad_path_pointer);
+	ATF_TP_ADD_TC(tp, open_path_too_long);
+	ATF_TP_ADD_TC(tp, open_nonexisting_object);
+	ATF_TP_ADD_TC(tp, open_create_existing_object);
+	ATF_TP_ADD_TC(tp, shm_functionality_across_fork);
+	ATF_TP_ADD_TC(tp, trunc_resets_object);
+	ATF_TP_ADD_TC(tp, unlink_bad_path_pointer);
+	ATF_TP_ADD_TC(tp, unlink_path_too_long);
+	ATF_TP_ADD_TC(tp, object_resize);
+
+	return (atf_no_error());
 }


More information about the svn-src-user mailing list