svn commit: r289223 - in user/ngie/more-tests2: etc/mtree tests/sys tests/sys/posixshm tools/regression/posixshm tools/test/posixshm
John Baldwin
jhb at freebsd.org
Wed Oct 14 22:22:22 UTC 2015
On Tuesday, October 13, 2015 04:50:12 PM Garrett Cooper wrote:
> Author: ngie
> Date: Tue Oct 13 16:50:12 2015
> New Revision: 289223
> URL: https://svnweb.freebsd.org/changeset/base/289223
>
> Log:
> Integrate tools/test/posixshm and tools/regression/posixshm into the FreeBSD
> test suite as tests/sys/posixshm
>
> Copied: user/ngie/more-tests2/tests/sys/posixshm/posixshm.c (from r289197, user/ngie/more-tests2/tools/regression/posixshm/posixshm.c)
> ==============================================================================
> --- /dev/null 00:00:00 1970 (empty, because file is newly added)
> +++ user/ngie/more-tests2/tests/sys/posixshm/posixshm.c Tue Oct 13 16:50:12 2015 (r289223, copy of r289197, user/ngie/more-tests2/tools/regression/posixshm/posixshm.c)
> @@ -0,0 +1,627 @@
> +/*-
> + * Copyright (c) 2006 Robert N. M. Watson
> + * All rights reserved.
> + *
> + * 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/param.h>
> +#include <sys/mman.h>
> +#include <sys/resource.h>
> +#include <sys/stat.h>
> +#include <sys/syscall.h>
> +#include <sys/wait.h>
> +
> +#include <errno.h>
> +#include <fcntl.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <unistd.h>
> +
> +#include "test.h"
> +
> +#define TEST_PATH "/tmp/posixshm_regression_test"
> +
> +/*
> + * Attempt a shm_open() that should fail with an expected error of 'error'.
> + */
> +static void
> +shm_open_should_fail(const char *path, int flags, mode_t mode, int error)
> +{
> + 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();
> +}
> +
> +/*
> + * Attempt a shm_unlink() that should fail with an expected error of 'error'.
> + */
> +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();
> +}
> +
> +/*
> + * Open the test object and write '1' to the first byte. Returns valid fd
> + * on success and -1 on failure.
> + */
> +static int
> +scribble_object(void)
> +{
> + char *page;
> + int fd;
> +
> + 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);
> + }
> +
> + 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);
> + }
> +
> + page[0] = '1';
> +
> + if (munmap(page, getpagesize()) < 0) {
> + fail_errno("munmap");
> + close(fd);
> + shm_unlink(TEST_PATH);
> + return (-1);
> + }
> +
> + return (fd);
> +}
> +
> +static void
> +remap_object(void)
> +{
> + 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)");
> + close(fd);
> + shm_unlink(TEST_PATH);
> + return;
> + }
> +
> + if (page[0] != '1') {
> + fail_err("missing data");
> + close(fd);
> + shm_unlink(TEST_PATH);
> + return;
> + }
> +
> + close(fd);
> + if (munmap(page, getpagesize()) < 0) {
> + fail_errno("munmap");
> + shm_unlink(TEST_PATH);
> + return;
> + }
> +
> + if (shm_unlink(TEST_PATH) < 0) {
> + fail_errno("shm_unlink");
> + return;
> + }
> +
> + pass();
> +}
> +TEST(remap_object, "remap object");
This should be rather straight foward to convert to true ATF. It was
already using a home-grown test frame work.
> Modified: user/ngie/more-tests2/tests/sys/posixshm/shm_test.c
> ==============================================================================
> --- user/ngie/more-tests2/tools/test/posixshm/shm_test.c Mon Oct 12 18:33:36 2015 (r289197)
> +++ user/ngie/more-tests2/tests/sys/posixshm/shm_test.c Tue Oct 13 16:50:12 2015 (r289223)
> @@ -79,7 +79,12 @@ main(int argc, char **argv)
> /*
> * Can't use mkstemp for obvious reasons...
> */
> - strcpy(buf, "/tmp/shmtest.XXXXXXXXXXXX");
> + char *tmpdir = getenv("TMPDIR");
> + if (tmpdir == NULL)
> + tmpdir = "/tmp";
> + snprintf(buf, sizeof(buf) - 1,
> + "%s/shmtest.XXXXXXXXXXXX", tmpdir);
> + buf[sizeof(buf) - 1] = '\0';
> mktemp(buf);
> desc = shm_open(buf, O_EXCL | O_CREAT | O_RDWR, 0600);
This should probably be revisited. It may be that this can just use
SHM_ANON instead. This probably predates the in-kernel shm_open().
--
John Baldwin
More information about the svn-src-user
mailing list