xref: /freebsd/crypto/openssh/regress/unittests/misc/test_ptimeout.c (revision 4d3fc8b0570b29fb0d6ee9525f104d52176ff0d4)
1f374ba41SEd Maste /* 	$OpenBSD: test_ptimeout.c,v 1.1 2023/01/06 02:59:50 djm Exp $ */
2f374ba41SEd Maste /*
3f374ba41SEd Maste  * Regress test for misc poll/ppoll timeout helpers.
4f374ba41SEd Maste  *
5f374ba41SEd Maste  * Placed in the public domain.
6f374ba41SEd Maste  */
7f374ba41SEd Maste 
8*4d3fc8b0SEd Maste #include "includes.h"
9*4d3fc8b0SEd Maste 
10f374ba41SEd Maste #include <sys/types.h>
11f374ba41SEd Maste #include <stdio.h>
12*4d3fc8b0SEd Maste #ifdef HAVE_STDINT_H
13f374ba41SEd Maste # include <stdint.h>
14*4d3fc8b0SEd Maste #endif
15f374ba41SEd Maste #include <stdlib.h>
16f374ba41SEd Maste #include <string.h>
17f374ba41SEd Maste #include <poll.h>
18f374ba41SEd Maste #include <time.h>
19f374ba41SEd Maste 
20f374ba41SEd Maste #include "../test_helper/test_helper.h"
21f374ba41SEd Maste 
22f374ba41SEd Maste #include "log.h"
23f374ba41SEd Maste #include "misc.h"
24f374ba41SEd Maste 
25f374ba41SEd Maste void test_ptimeout(void);
26f374ba41SEd Maste 
27f374ba41SEd Maste void
28f374ba41SEd Maste test_ptimeout(void)
29f374ba41SEd Maste {
30f374ba41SEd Maste 	struct timespec pt, *ts;
31f374ba41SEd Maste 
32f374ba41SEd Maste 	TEST_START("ptimeout_init");
33f374ba41SEd Maste 	ptimeout_init(&pt);
34f374ba41SEd Maste 	ASSERT_PTR_EQ(ptimeout_get_tsp(&pt), NULL);
35f374ba41SEd Maste 	ASSERT_INT_EQ(ptimeout_get_ms(&pt), -1);
36f374ba41SEd Maste 	TEST_DONE();
37f374ba41SEd Maste 
38f374ba41SEd Maste 	TEST_START("ptimeout_deadline_sec");
39f374ba41SEd Maste 	ptimeout_deadline_sec(&pt, 100);
40f374ba41SEd Maste 	ptimeout_deadline_sec(&pt, 200);
41f374ba41SEd Maste 	ASSERT_INT_EQ(ptimeout_get_ms(&pt), 100 * 1000);
42f374ba41SEd Maste 	ts = ptimeout_get_tsp(&pt);
43f374ba41SEd Maste 	ASSERT_PTR_NE(ts, NULL);
44f374ba41SEd Maste 	ASSERT_LONG_EQ(ts->tv_nsec, 0);
45f374ba41SEd Maste 	ASSERT_LONG_EQ(ts->tv_sec, 100);
46f374ba41SEd Maste 	TEST_DONE();
47f374ba41SEd Maste 
48f374ba41SEd Maste 	TEST_START("ptimeout_deadline_ms");
49f374ba41SEd Maste 	ptimeout_deadline_ms(&pt, 50123);
50f374ba41SEd Maste 	ptimeout_deadline_ms(&pt, 50500);
51f374ba41SEd Maste 	ASSERT_INT_EQ(ptimeout_get_ms(&pt), 50123);
52f374ba41SEd Maste 	ts = ptimeout_get_tsp(&pt);
53f374ba41SEd Maste 	ASSERT_PTR_NE(ts, NULL);
54f374ba41SEd Maste 	ASSERT_LONG_EQ(ts->tv_nsec, 123 * 1000000);
55f374ba41SEd Maste 	ASSERT_LONG_EQ(ts->tv_sec, 50);
56f374ba41SEd Maste 	TEST_DONE();
57f374ba41SEd Maste 
58f374ba41SEd Maste 	TEST_START("ptimeout zero");
59f374ba41SEd Maste 	ptimeout_init(&pt);
60f374ba41SEd Maste 	ptimeout_deadline_ms(&pt, 0);
61f374ba41SEd Maste 	ASSERT_INT_EQ(ptimeout_get_ms(&pt), 0);
62f374ba41SEd Maste 	ts = ptimeout_get_tsp(&pt);
63f374ba41SEd Maste 	ASSERT_PTR_NE(ts, NULL);
64f374ba41SEd Maste 	ASSERT_LONG_EQ(ts->tv_nsec, 0);
65f374ba41SEd Maste 	ASSERT_LONG_EQ(ts->tv_sec, 0);
66f374ba41SEd Maste 	TEST_DONE();
67f374ba41SEd Maste 
68f374ba41SEd Maste 	TEST_START("ptimeout_deadline_monotime");
69f374ba41SEd Maste 	ptimeout_init(&pt);
70f374ba41SEd Maste 	ptimeout_deadline_monotime(&pt, monotime() + 100);
71f374ba41SEd Maste 	ASSERT_INT_GT(ptimeout_get_ms(&pt), 50000);
72f374ba41SEd Maste 	ASSERT_INT_LT(ptimeout_get_ms(&pt), 200000);
73f374ba41SEd Maste 	ts = ptimeout_get_tsp(&pt);
74f374ba41SEd Maste 	ASSERT_PTR_NE(ts, NULL);
75f374ba41SEd Maste 	ASSERT_LONG_GT(ts->tv_sec, 50);
76f374ba41SEd Maste 	ASSERT_LONG_LT(ts->tv_sec, 200);
77f374ba41SEd Maste 	TEST_DONE();
78f374ba41SEd Maste 
79f374ba41SEd Maste 	TEST_START("ptimeout_deadline_monotime past");
80f374ba41SEd Maste 	ptimeout_init(&pt);
81f374ba41SEd Maste 	ptimeout_deadline_monotime(&pt, monotime() + 100);
82f374ba41SEd Maste 	ptimeout_deadline_monotime(&pt, monotime() - 100);
83f374ba41SEd Maste 	ASSERT_INT_EQ(ptimeout_get_ms(&pt), 0);
84f374ba41SEd Maste 	ts = ptimeout_get_tsp(&pt);
85f374ba41SEd Maste 	ASSERT_PTR_NE(ts, NULL);
86f374ba41SEd Maste 	ASSERT_LONG_EQ(ts->tv_nsec, 0);
87f374ba41SEd Maste 	ASSERT_LONG_EQ(ts->tv_sec, 0);
88f374ba41SEd Maste 	TEST_DONE();
89f374ba41SEd Maste }
90