xref: /freebsd/crypto/openssh/regress/unittests/misc/test_convtime.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /* 	$OpenBSD: test_convtime.c,v 1.3 2022/08/11 01:57:50 djm Exp $ */
2 /*
3  * Regress test for misc time conversion functions.
4  *
5  * Placed in the public domain.
6  */
7 
8 #include "includes.h"
9 
10 #include <sys/types.h>
11 #include <limits.h>
12 #include <stdio.h>
13 #ifdef HAVE_STDINT_H
14 #include <stdint.h>
15 #endif
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include "../test_helper/test_helper.h"
20 
21 #include "log.h"
22 #include "misc.h"
23 #include "ssherr.h"
24 
25 void test_convtime(void);
26 
27 void
28 test_convtime(void)
29 {
30 	char buf[1024];
31 	uint64_t t;
32 
33 	TEST_START("misc_convtime");
34 	ASSERT_INT_EQ(convtime("0"), 0);
35 	ASSERT_INT_EQ(convtime("1"), 1);
36 	ASSERT_INT_EQ(convtime("2s"), 2);
37 	ASSERT_INT_EQ(convtime("3m"), 180);
38 	ASSERT_INT_EQ(convtime("1m30"), 90);
39 	ASSERT_INT_EQ(convtime("1m30s"), 90);
40 	ASSERT_INT_EQ(convtime("1h1s"), 3601);
41 	ASSERT_INT_EQ(convtime("1h30m"), 90 * 60);
42 	ASSERT_INT_EQ(convtime("1d"), 24 * 60 * 60);
43 	ASSERT_INT_EQ(convtime("1w"), 7 * 24 * 60 * 60);
44 	ASSERT_INT_EQ(convtime("1w2d3h4m5"), 788645);
45 	ASSERT_INT_EQ(convtime("1w2d3h4m5s"), 788645);
46 	/* any negative number or error returns -1 */
47 	ASSERT_INT_EQ(convtime("-1"),  -1);
48 	ASSERT_INT_EQ(convtime(""),  -1);
49 	ASSERT_INT_EQ(convtime("trout"),  -1);
50 	ASSERT_INT_EQ(convtime("-77"),  -1);
51 	/* boundary conditions */
52 	snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX);
53 	ASSERT_INT_EQ(convtime(buf), INT_MAX);
54 	snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX + 1);
55 	ASSERT_INT_EQ(convtime(buf), -1);
56 	ASSERT_INT_EQ(convtime("3550w5d3h14m7s"), 2147483647);
57 #if INT_MAX == 2147483647
58 	ASSERT_INT_EQ(convtime("3550w5d3h14m8s"), -1);
59 #endif
60 	TEST_DONE();
61 
62 	/* XXX timezones/DST make verification of this tricky */
63 	/* XXX maybe setenv TZ and tzset() to make it unambiguous? */
64 	TEST_START("misc_parse_absolute_time");
65 	ASSERT_INT_EQ(parse_absolute_time("20000101", &t), 0);
66 	ASSERT_INT_EQ(parse_absolute_time("200001011223", &t), 0);
67 	ASSERT_INT_EQ(parse_absolute_time("20000101122345", &t), 0);
68 
69 	/* forced UTC TZ */
70 	ASSERT_INT_EQ(parse_absolute_time("20000101Z", &t), 0);
71 	ASSERT_U64_EQ(t, 946684800);
72 	ASSERT_INT_EQ(parse_absolute_time("200001011223Z", &t), 0);
73 	ASSERT_U64_EQ(t, 946729380);
74 	ASSERT_INT_EQ(parse_absolute_time("20000101122345Z", &t), 0);
75 	ASSERT_U64_EQ(t, 946729425);
76 	ASSERT_INT_EQ(parse_absolute_time("20000101UTC", &t), 0);
77 	ASSERT_U64_EQ(t, 946684800);
78 	ASSERT_INT_EQ(parse_absolute_time("200001011223UTC", &t), 0);
79 	ASSERT_U64_EQ(t, 946729380);
80 	ASSERT_INT_EQ(parse_absolute_time("20000101122345UTC", &t), 0);
81 	ASSERT_U64_EQ(t, 946729425);
82 
83 	/* Bad month */
84 	ASSERT_INT_EQ(parse_absolute_time("20001301", &t),
85 	    SSH_ERR_INVALID_FORMAT);
86 	ASSERT_INT_EQ(parse_absolute_time("20000001", &t),
87 	    SSH_ERR_INVALID_FORMAT);
88 	/* Incomplete */
89 	ASSERT_INT_EQ(parse_absolute_time("2", &t),
90 	    SSH_ERR_INVALID_FORMAT);
91 	ASSERT_INT_EQ(parse_absolute_time("2000", &t),
92 	    SSH_ERR_INVALID_FORMAT);
93 	ASSERT_INT_EQ(parse_absolute_time("20000", &t),
94 	    SSH_ERR_INVALID_FORMAT);
95 	ASSERT_INT_EQ(parse_absolute_time("200001", &t),
96 	    SSH_ERR_INVALID_FORMAT);
97 	ASSERT_INT_EQ(parse_absolute_time("2000010", &t),
98 	    SSH_ERR_INVALID_FORMAT);
99 	ASSERT_INT_EQ(parse_absolute_time("200001010", &t),
100 	    SSH_ERR_INVALID_FORMAT);
101 	/* Bad day, hour, minute, second */
102 	ASSERT_INT_EQ(parse_absolute_time("20000199", &t),
103 	    SSH_ERR_INVALID_FORMAT);
104 	ASSERT_INT_EQ(parse_absolute_time("200001019900", &t),
105 	    SSH_ERR_INVALID_FORMAT);
106 	ASSERT_INT_EQ(parse_absolute_time("200001010099", &t),
107 	    SSH_ERR_INVALID_FORMAT);
108 	ASSERT_INT_EQ(parse_absolute_time("20000101000099", &t),
109 	    SSH_ERR_INVALID_FORMAT);
110 	/* Invalid TZ specifier */
111 	ASSERT_INT_EQ(parse_absolute_time("20000101ZZ", &t),
112 	    SSH_ERR_INVALID_FORMAT);
113 	ASSERT_INT_EQ(parse_absolute_time("20000101PDT", &t),
114 	    SSH_ERR_INVALID_FORMAT);
115 	ASSERT_INT_EQ(parse_absolute_time("20000101U", &t),
116 	    SSH_ERR_INVALID_FORMAT);
117 	ASSERT_INT_EQ(parse_absolute_time("20000101UTCUTC", &t),
118 	    SSH_ERR_INVALID_FORMAT);
119 
120 	TEST_DONE();
121 }
122