Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/thorpej-futex]: src/tests/lib/libc/sys Unit tests for timerfd.



details:   https://anonhg.NetBSD.org/src/rev/dfca453a2179
branches:  thorpej-futex
changeset: 1024922:dfca453a2179
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Mon Dec 14 16:55:51 2020 +0000

description:
Unit tests for timerfd.

diffstat:

 tests/lib/libc/sys/Makefile    |    4 +-
 tests/lib/libc/sys/t_timerfd.c |  625 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 628 insertions(+), 1 deletions(-)

diffs (truncated from 654 to 300 lines):

diff -r 932c644e28e7 -r dfca453a2179 tests/lib/libc/sys/Makefile
--- a/tests/lib/libc/sys/Makefile       Mon Dec 14 16:54:04 2020 +0000
+++ b/tests/lib/libc/sys/Makefile       Mon Dec 14 16:55:51 2020 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.68.2.1 2020/12/14 16:01:38 thorpej Exp $
+# $NetBSD: Makefile,v 1.68.2.2 2020/12/14 16:55:51 thorpej Exp $
 
 MKMAN= no
 
@@ -82,6 +82,7 @@
 TESTS_C+=              t_stat
 TESTS_C+=              t_syscall
 TESTS_C+=              t_timer_create
+TESTS_C+=              t_timerfd
 TESTS_C+=              t_truncate
 TESTS_C+=              t_ucontext
 TESTS_C+=              t_umask
@@ -96,6 +97,7 @@
 
 LDADD.t_eventfd+=      -lpthread
 LDADD.t_getpid+=        -lpthread
+LDADD.t_timerfd+=      -lpthread
 
 LDADD.t_ptrace_sigchld+=       -pthread -lm
 
diff -r 932c644e28e7 -r dfca453a2179 tests/lib/libc/sys/t_timerfd.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/sys/t_timerfd.c    Mon Dec 14 16:55:51 2020 +0000
@@ -0,0 +1,625 @@
+/* $NetBSD: t_timerfd.c,v 1.1.2.1 2020/12/14 16:55:51 thorpej Exp $ */
+
+/*-
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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>
+__COPYRIGHT("@(#) Copyright (c) 2020\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_timerfd.c,v 1.1.2.1 2020/12/14 16:55:51 thorpej Exp $");
+
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/select.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/timerfd.h>
+#include <errno.h>
+#include <poll.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+struct helper_context {
+       int     fd;
+
+       pthread_barrier_t barrier;
+};
+
+static void
+init_helper_context(struct helper_context * const ctx)
+{
+
+       memset(ctx, 0, sizeof(*ctx));
+
+       ATF_REQUIRE(pthread_barrier_init(&ctx->barrier, NULL, 2) == 0);
+}
+
+static bool
+wait_barrier(struct helper_context * const ctx)
+{
+       int rv = pthread_barrier_wait(&ctx->barrier);
+
+       return rv == 0 || rv == PTHREAD_BARRIER_SERIAL_THREAD;
+}
+
+static int
+my_timerfd_create(clockid_t clock_id, int flags)
+{
+       return syscall(SYS_timerfd_create, clock_id, flags);
+}
+
+static int
+my_timerfd_gettime(int fd, struct itimerspec *curr_value)
+{
+       return syscall(SYS_timerfd_gettime, fd, curr_value);
+}
+
+static int
+my_timerfd_settime(int fd, int flags, const struct itimerspec *new_value,
+    struct itimerspec *old_value)
+{
+       return syscall(SYS_timerfd_settime, fd, flags, new_value, old_value);
+}
+
+/*****************************************************************************/
+
+static int
+timerfd_read(int fd, uint64_t *valp)
+{
+       uint64_t val;
+
+       switch (read(fd, &val, sizeof(val))) {
+       case -1:
+               return -1;
+
+       case sizeof(val):
+               *valp = val;
+               return 0;
+
+       default:
+               /* ?? Should never happen. */
+               errno = EIO;
+               return -1;
+       }
+}
+
+/*****************************************************************************/
+
+ATF_TC(timerfd_create);
+ATF_TC_HEAD(timerfd_create, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "validates timerfd_create()");
+}
+ATF_TC_BODY(timerfd_create, tc)
+{
+       int fd;
+
+       ATF_REQUIRE((fd = my_timerfd_create(CLOCK_REALTIME, 0)) >= 0);
+       (void) close(fd);
+
+       ATF_REQUIRE((fd = my_timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
+       (void) close(fd);
+
+       ATF_REQUIRE_ERRNO(EINVAL,
+           (fd = my_timerfd_create(CLOCK_VIRTUAL, 0)) == -1);
+
+       ATF_REQUIRE_ERRNO(EINVAL,
+           (fd = my_timerfd_create(CLOCK_PROF, 0)) == -1);
+
+       ATF_REQUIRE_ERRNO(EINVAL,
+           (fd = my_timerfd_create(CLOCK_REALTIME,
+                                   ~(TFD_CLOEXEC | TFD_NONBLOCK))) == -1);
+}
+
+/*****************************************************************************/
+
+ATF_TC(timerfd_bogusfd);
+ATF_TC_HEAD(timerfd_bogusfd, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "validates rejection of bogus fds by timerfd_{get,set}time()");
+}
+ATF_TC_BODY(timerfd_bogusfd, tc)
+{
+       struct itimerspec its = { 0 };
+       int fd;
+
+       ATF_REQUIRE((fd = kqueue()) >= 0);      /* arbitrary fd type */
+
+       ATF_REQUIRE_ERRNO(EINVAL,
+           my_timerfd_gettime(fd, &its) == -1);
+
+       its.it_value.tv_sec = 5;
+       ATF_REQUIRE_ERRNO(EINVAL,
+           my_timerfd_settime(fd, 0, &its, NULL) == -1);
+
+       (void) close(fd);
+}
+
+/*****************************************************************************/
+
+ATF_TC(timerfd_block);
+ATF_TC_HEAD(timerfd_block, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "validates blocking behavior");
+}
+ATF_TC_BODY(timerfd_block, tc)
+{
+       struct timespec then, now, delta;
+       uint64_t val;
+       int fd;
+
+       ATF_REQUIRE((fd = my_timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
+
+       const struct itimerspec its = {
+               .it_value = { .tv_sec = 1, .tv_nsec = 0 },
+               .it_interval = { .tv_sec = 0, .tv_nsec = 0 },
+       };
+
+       ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
+       ATF_REQUIRE(my_timerfd_settime(fd, 0, &its, NULL) == 0);
+       ATF_REQUIRE(timerfd_read(fd, &val) == 0);
+       ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
+       ATF_REQUIRE(val == 1);
+
+       timespecsub(&now, &then, &delta);
+       ATF_REQUIRE(delta.tv_sec == 1);
+
+       (void) close(fd);
+}
+
+/*****************************************************************************/
+
+ATF_TC(timerfd_repeating);
+ATF_TC_HEAD(timerfd_repeating, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "validates repeating timer behavior");
+}
+ATF_TC_BODY(timerfd_repeating, tc)
+{
+       struct timespec then, now, delta;
+       uint64_t val;
+       int fd;
+
+       ATF_REQUIRE((fd = my_timerfd_create(CLOCK_MONOTONIC,
+                                           TFD_NONBLOCK)) >= 0);
+
+       const struct itimerspec its = {
+               .it_value = { .tv_sec = 0, .tv_nsec = 200000000 },
+               .it_interval = { .tv_sec = 0, .tv_nsec = 200000000 },
+       };
+
+       ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
+       ATF_REQUIRE(my_timerfd_settime(fd, 0, &its, NULL) == 0);
+       ATF_REQUIRE(sleep(1) == 0);
+       ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
+       ATF_REQUIRE(timerfd_read(fd, &val) == 0);
+       ATF_REQUIRE(val >= 3 && val <= 5);      /* allow some slop */
+
+       timespecsub(&now, &then, &delta);
+       ATF_REQUIRE(delta.tv_sec == 1);
+
+       (void) close(fd);
+}
+
+/*****************************************************************************/
+
+ATF_TC(timerfd_abstime);
+ATF_TC_HEAD(timerfd_abstime, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "validates specifying abstime");
+}
+ATF_TC_BODY(timerfd_abstime, tc)
+{
+       struct timespec then, now, delta;
+       uint64_t val;
+       int fd;
+
+       ATF_REQUIRE((fd = my_timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
+
+       struct itimerspec its = {
+               .it_value = { .tv_sec = 0, .tv_nsec = 0 },
+               .it_interval = { .tv_sec = 0, .tv_nsec = 0 },
+       };
+
+       ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
+       its.it_value = then;
+       its.it_value.tv_sec += 1;
+       ATF_REQUIRE(my_timerfd_settime(fd, TFD_TIMER_ABSTIME, &its, NULL) == 0);
+       ATF_REQUIRE(timerfd_read(fd, &val) == 0);
+       ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
+       ATF_REQUIRE(val == 1);
+
+       timespecsub(&now, &then, &delta);
+       ATF_REQUIRE(delta.tv_sec == 1);
+
+       (void) close(fd);
+}
+
+/*****************************************************************************/
+
+ATF_TC(timerfd_cancel_on_set_immed);
+ATF_TC_HEAD(timerfd_cancel_on_set_immed, tc)
+{



Home | Main Index | Thread Index | Old Index