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