xref: /freebsd-src/crypto/openssh/regress/unittests/misc/test_convtime.c (revision 38a52bd3b5cac3da6f7f6eef3dd050e6aa08ebb3)
1*38a52bd3SEd Maste /* 	$OpenBSD: test_convtime.c,v 1.3 2022/08/11 01:57:50 djm Exp $ */
219261079SEd Maste /*
319261079SEd Maste  * Regress test for misc time conversion functions.
419261079SEd Maste  *
519261079SEd Maste  * Placed in the public domain.
619261079SEd Maste  */
719261079SEd Maste 
819261079SEd Maste #include "includes.h"
919261079SEd Maste 
1019261079SEd Maste #include <sys/types.h>
111323ec57SEd Maste #include <limits.h>
1219261079SEd Maste #include <stdio.h>
1319261079SEd Maste #ifdef HAVE_STDINT_H
1419261079SEd Maste #include <stdint.h>
1519261079SEd Maste #endif
1619261079SEd Maste #include <stdlib.h>
1719261079SEd Maste #include <string.h>
1819261079SEd Maste 
1919261079SEd Maste #include "../test_helper/test_helper.h"
2019261079SEd Maste 
2119261079SEd Maste #include "log.h"
2219261079SEd Maste #include "misc.h"
23*38a52bd3SEd Maste #include "ssherr.h"
2419261079SEd Maste 
2519261079SEd Maste void test_convtime(void);
2619261079SEd Maste 
2719261079SEd Maste void
test_convtime(void)2819261079SEd Maste test_convtime(void)
2919261079SEd Maste {
3019261079SEd Maste 	char buf[1024];
31*38a52bd3SEd Maste 	uint64_t t;
3219261079SEd Maste 
3319261079SEd Maste 	TEST_START("misc_convtime");
3419261079SEd Maste 	ASSERT_INT_EQ(convtime("0"), 0);
3519261079SEd Maste 	ASSERT_INT_EQ(convtime("1"), 1);
3619261079SEd Maste 	ASSERT_INT_EQ(convtime("2s"), 2);
3719261079SEd Maste 	ASSERT_INT_EQ(convtime("3m"), 180);
3819261079SEd Maste 	ASSERT_INT_EQ(convtime("1m30"), 90);
3919261079SEd Maste 	ASSERT_INT_EQ(convtime("1m30s"), 90);
4019261079SEd Maste 	ASSERT_INT_EQ(convtime("1h1s"), 3601);
4119261079SEd Maste 	ASSERT_INT_EQ(convtime("1h30m"), 90 * 60);
4219261079SEd Maste 	ASSERT_INT_EQ(convtime("1d"), 24 * 60 * 60);
4319261079SEd Maste 	ASSERT_INT_EQ(convtime("1w"), 7 * 24 * 60 * 60);
4419261079SEd Maste 	ASSERT_INT_EQ(convtime("1w2d3h4m5"), 788645);
4519261079SEd Maste 	ASSERT_INT_EQ(convtime("1w2d3h4m5s"), 788645);
4619261079SEd Maste 	/* any negative number or error returns -1 */
4719261079SEd Maste 	ASSERT_INT_EQ(convtime("-1"),  -1);
4819261079SEd Maste 	ASSERT_INT_EQ(convtime(""),  -1);
4919261079SEd Maste 	ASSERT_INT_EQ(convtime("trout"),  -1);
5019261079SEd Maste 	ASSERT_INT_EQ(convtime("-77"),  -1);
5119261079SEd Maste 	/* boundary conditions */
5219261079SEd Maste 	snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX);
5319261079SEd Maste 	ASSERT_INT_EQ(convtime(buf), INT_MAX);
5419261079SEd Maste 	snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX + 1);
5519261079SEd Maste 	ASSERT_INT_EQ(convtime(buf), -1);
5619261079SEd Maste 	ASSERT_INT_EQ(convtime("3550w5d3h14m7s"), 2147483647);
5719261079SEd Maste #if INT_MAX == 2147483647
5819261079SEd Maste 	ASSERT_INT_EQ(convtime("3550w5d3h14m8s"), -1);
5919261079SEd Maste #endif
6019261079SEd Maste 	TEST_DONE();
61*38a52bd3SEd Maste 
62*38a52bd3SEd Maste 	/* XXX timezones/DST make verification of this tricky */
63*38a52bd3SEd Maste 	/* XXX maybe setenv TZ and tzset() to make it unambiguous? */
64*38a52bd3SEd Maste 	TEST_START("misc_parse_absolute_time");
65*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("20000101", &t), 0);
66*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("200001011223", &t), 0);
67*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("20000101122345", &t), 0);
68*38a52bd3SEd Maste 
69*38a52bd3SEd Maste 	/* forced UTC TZ */
70*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("20000101Z", &t), 0);
71*38a52bd3SEd Maste 	ASSERT_U64_EQ(t, 946684800);
72*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("200001011223Z", &t), 0);
73*38a52bd3SEd Maste 	ASSERT_U64_EQ(t, 946729380);
74*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("20000101122345Z", &t), 0);
75*38a52bd3SEd Maste 	ASSERT_U64_EQ(t, 946729425);
76*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("20000101UTC", &t), 0);
77*38a52bd3SEd Maste 	ASSERT_U64_EQ(t, 946684800);
78*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("200001011223UTC", &t), 0);
79*38a52bd3SEd Maste 	ASSERT_U64_EQ(t, 946729380);
80*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("20000101122345UTC", &t), 0);
81*38a52bd3SEd Maste 	ASSERT_U64_EQ(t, 946729425);
82*38a52bd3SEd Maste 
83*38a52bd3SEd Maste 	/* Bad month */
84*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("20001301", &t),
85*38a52bd3SEd Maste 	    SSH_ERR_INVALID_FORMAT);
86*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("20000001", &t),
87*38a52bd3SEd Maste 	    SSH_ERR_INVALID_FORMAT);
88*38a52bd3SEd Maste 	/* Incomplete */
89*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("2", &t),
90*38a52bd3SEd Maste 	    SSH_ERR_INVALID_FORMAT);
91*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("2000", &t),
92*38a52bd3SEd Maste 	    SSH_ERR_INVALID_FORMAT);
93*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("20000", &t),
94*38a52bd3SEd Maste 	    SSH_ERR_INVALID_FORMAT);
95*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("200001", &t),
96*38a52bd3SEd Maste 	    SSH_ERR_INVALID_FORMAT);
97*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("2000010", &t),
98*38a52bd3SEd Maste 	    SSH_ERR_INVALID_FORMAT);
99*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("200001010", &t),
100*38a52bd3SEd Maste 	    SSH_ERR_INVALID_FORMAT);
101*38a52bd3SEd Maste 	/* Bad day, hour, minute, second */
102*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("20000199", &t),
103*38a52bd3SEd Maste 	    SSH_ERR_INVALID_FORMAT);
104*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("200001019900", &t),
105*38a52bd3SEd Maste 	    SSH_ERR_INVALID_FORMAT);
106*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("200001010099", &t),
107*38a52bd3SEd Maste 	    SSH_ERR_INVALID_FORMAT);
108*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("20000101000099", &t),
109*38a52bd3SEd Maste 	    SSH_ERR_INVALID_FORMAT);
110*38a52bd3SEd Maste 	/* Invalid TZ specifier */
111*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("20000101ZZ", &t),
112*38a52bd3SEd Maste 	    SSH_ERR_INVALID_FORMAT);
113*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("20000101PDT", &t),
114*38a52bd3SEd Maste 	    SSH_ERR_INVALID_FORMAT);
115*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("20000101U", &t),
116*38a52bd3SEd Maste 	    SSH_ERR_INVALID_FORMAT);
117*38a52bd3SEd Maste 	ASSERT_INT_EQ(parse_absolute_time("20000101UTCUTC", &t),
118*38a52bd3SEd Maste 	    SSH_ERR_INVALID_FORMAT);
119*38a52bd3SEd Maste 
120*38a52bd3SEd Maste 	TEST_DONE();
12119261079SEd Maste }
122