]> git.ipfire.org Git - thirdparty/openssh-portable.git/commitdiff
upstream: unit tests for misc.c:ptimeout_* API
authordjm@openbsd.org <djm@openbsd.org>
Fri, 6 Jan 2023 02:59:50 +0000 (02:59 +0000)
committerDarren Tucker <dtucker@dtucker.net>
Fri, 6 Jan 2023 10:37:40 +0000 (21:37 +1100)
OpenBSD-Regress-ID: 01f8fb12d08e5aaadd4bd4e71f456b6588be9a94

Makefile.in
regress/unittests/misc/Makefile
regress/unittests/misc/test_ptimeout.c [new file with mode: 0644]
regress/unittests/misc/tests.c

index 5dde2baa079cdd7e036b1700b500917a52b06fb1..cbb6f6b6b8385b7d8ce5e97ec009105f1c0373f9 100644 (file)
@@ -653,7 +653,8 @@ UNITTESTS_TEST_MISC_OBJS=\
        regress/unittests/misc/test_convtime.o \
        regress/unittests/misc/test_argv.o \
        regress/unittests/misc/test_strdelim.o \
-       regress/unittests/misc/test_hpdelim.o
+       regress/unittests/misc/test_hpdelim.o \
+       regress/unittests/misc/test_ptimeout.o
 
 regress/unittests/misc/test_misc$(EXEEXT): \
     ${UNITTESTS_TEST_MISC_OBJS} \
index e8fe1a9531360341b87165ad88b8bea1af988db3..d2be393ad703ed9705df02d2bd3e65dff61674be 100644 (file)
@@ -1,4 +1,4 @@
-#      $OpenBSD: Makefile,v 1.8 2022/02/04 07:53:44 dtucker Exp $
+#      $OpenBSD: Makefile,v 1.9 2023/01/06 02:59:50 djm Exp $
 
 PROG=test_misc
 SRCS=tests.c
@@ -8,6 +8,7 @@ SRCS+=  test_parse.c
 SRCS+= test_argv.c
 SRCS+= test_strdelim.c
 SRCS+= test_hpdelim.c
+SRCS+= test_ptimeout.c
 
 # From usr.bin/ssh/Makefile.inc
 SRCS+= sshbuf.c
diff --git a/regress/unittests/misc/test_ptimeout.c b/regress/unittests/misc/test_ptimeout.c
new file mode 100644 (file)
index 0000000..7adc590
--- /dev/null
@@ -0,0 +1,85 @@
+/*     $OpenBSD: test_ptimeout.c,v 1.1 2023/01/06 02:59:50 djm Exp $ */
+/*
+ * Regress test for misc poll/ppoll timeout helpers.
+ *
+ * Placed in the public domain.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <poll.h>
+#include <time.h>
+
+#include "../test_helper/test_helper.h"
+
+#include "log.h"
+#include "misc.h"
+
+void test_ptimeout(void);
+
+void
+test_ptimeout(void)
+{
+       struct timespec pt, *ts;
+
+       TEST_START("ptimeout_init");
+       ptimeout_init(&pt);
+       ASSERT_PTR_EQ(ptimeout_get_tsp(&pt), NULL);
+       ASSERT_INT_EQ(ptimeout_get_ms(&pt), -1);
+       TEST_DONE();
+
+       TEST_START("ptimeout_deadline_sec");
+       ptimeout_deadline_sec(&pt, 100);
+       ptimeout_deadline_sec(&pt, 200);
+       ASSERT_INT_EQ(ptimeout_get_ms(&pt), 100 * 1000);
+       ts = ptimeout_get_tsp(&pt);
+       ASSERT_PTR_NE(ts, NULL);
+       ASSERT_LONG_EQ(ts->tv_nsec, 0);
+       ASSERT_LONG_EQ(ts->tv_sec, 100);
+       TEST_DONE();
+
+       TEST_START("ptimeout_deadline_ms");
+       ptimeout_deadline_ms(&pt, 50123);
+       ptimeout_deadline_ms(&pt, 50500);
+       ASSERT_INT_EQ(ptimeout_get_ms(&pt), 50123);
+       ts = ptimeout_get_tsp(&pt);
+       ASSERT_PTR_NE(ts, NULL);
+       ASSERT_LONG_EQ(ts->tv_nsec, 123 * 1000000);
+       ASSERT_LONG_EQ(ts->tv_sec, 50);
+       TEST_DONE();
+
+       TEST_START("ptimeout zero");
+       ptimeout_init(&pt);
+       ptimeout_deadline_ms(&pt, 0);
+       ASSERT_INT_EQ(ptimeout_get_ms(&pt), 0);
+       ts = ptimeout_get_tsp(&pt);
+       ASSERT_PTR_NE(ts, NULL);
+       ASSERT_LONG_EQ(ts->tv_nsec, 0);
+       ASSERT_LONG_EQ(ts->tv_sec, 0);
+       TEST_DONE();
+
+       TEST_START("ptimeout_deadline_monotime");
+       ptimeout_init(&pt);
+       ptimeout_deadline_monotime(&pt, monotime() + 100);
+       ASSERT_INT_GT(ptimeout_get_ms(&pt), 50000);
+       ASSERT_INT_LT(ptimeout_get_ms(&pt), 200000);
+       ts = ptimeout_get_tsp(&pt);
+       ASSERT_PTR_NE(ts, NULL);
+       ASSERT_LONG_GT(ts->tv_sec, 50);
+       ASSERT_LONG_LT(ts->tv_sec, 200);
+       TEST_DONE();
+
+       TEST_START("ptimeout_deadline_monotime past");
+       ptimeout_init(&pt);
+       ptimeout_deadline_monotime(&pt, monotime() + 100);
+       ptimeout_deadline_monotime(&pt, monotime() - 100);
+       ASSERT_INT_EQ(ptimeout_get_ms(&pt), 0);
+       ts = ptimeout_get_tsp(&pt);
+       ASSERT_PTR_NE(ts, NULL);
+       ASSERT_LONG_EQ(ts->tv_nsec, 0);
+       ASSERT_LONG_EQ(ts->tv_sec, 0);
+       TEST_DONE();
+}
index d52490e3b3ceb9dda61ff886a37378ed7ad6a80d..32699541413e9e85be198b7e7733594e319ab77e 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: tests.c,v 1.9 2022/02/04 07:53:44 dtucker Exp $ */
+/*     $OpenBSD: tests.c,v 1.10 2023/01/06 02:59:50 djm Exp $ */
 /*
  * Regress test for misc helper functions.
  *
@@ -26,6 +26,7 @@ void test_expand(void);
 void test_argv(void);
 void test_strdelim(void);
 void test_hpdelim(void);
+void test_ptimeout(void);
 
 void
 tests(void)
@@ -36,4 +37,5 @@ tests(void)
        test_argv();
        test_strdelim();
        test_hpdelim();
+       test_ptimeout();
 }