1*d9741277Sdjm /* $OpenBSD: test_convtime.c,v 1.3 2022/08/11 01:57:50 djm Exp $ */
208adf9a7Sdjm /*
308adf9a7Sdjm * Regress test for misc time conversion functions.
408adf9a7Sdjm *
508adf9a7Sdjm * Placed in the public domain.
608adf9a7Sdjm */
708adf9a7Sdjm
808adf9a7Sdjm #include <sys/types.h>
908adf9a7Sdjm #include <stdio.h>
1008adf9a7Sdjm #include <stdint.h>
1108adf9a7Sdjm #include <stdlib.h>
12a8c975d2Sderaadt #include <limits.h>
1308adf9a7Sdjm #include <string.h>
1408adf9a7Sdjm
1508adf9a7Sdjm #include "test_helper.h"
1608adf9a7Sdjm
1708adf9a7Sdjm #include "log.h"
1808adf9a7Sdjm #include "misc.h"
19*d9741277Sdjm #include "ssherr.h"
2008adf9a7Sdjm
2108adf9a7Sdjm void test_convtime(void);
2208adf9a7Sdjm
2308adf9a7Sdjm void
test_convtime(void)2408adf9a7Sdjm test_convtime(void)
2508adf9a7Sdjm {
2608adf9a7Sdjm char buf[1024];
27*d9741277Sdjm uint64_t t;
2808adf9a7Sdjm
2908adf9a7Sdjm TEST_START("misc_convtime");
3008adf9a7Sdjm ASSERT_INT_EQ(convtime("0"), 0);
3108adf9a7Sdjm ASSERT_INT_EQ(convtime("1"), 1);
3208adf9a7Sdjm ASSERT_INT_EQ(convtime("2s"), 2);
3308adf9a7Sdjm ASSERT_INT_EQ(convtime("3m"), 180);
3408adf9a7Sdjm ASSERT_INT_EQ(convtime("1m30"), 90);
3508adf9a7Sdjm ASSERT_INT_EQ(convtime("1m30s"), 90);
3608adf9a7Sdjm ASSERT_INT_EQ(convtime("1h1s"), 3601);
3708adf9a7Sdjm ASSERT_INT_EQ(convtime("1h30m"), 90 * 60);
3808adf9a7Sdjm ASSERT_INT_EQ(convtime("1d"), 24 * 60 * 60);
3908adf9a7Sdjm ASSERT_INT_EQ(convtime("1w"), 7 * 24 * 60 * 60);
4008adf9a7Sdjm ASSERT_INT_EQ(convtime("1w2d3h4m5"), 788645);
4108adf9a7Sdjm ASSERT_INT_EQ(convtime("1w2d3h4m5s"), 788645);
4208adf9a7Sdjm /* any negative number or error returns -1 */
4308adf9a7Sdjm ASSERT_INT_EQ(convtime("-1"), -1);
4408adf9a7Sdjm ASSERT_INT_EQ(convtime(""), -1);
4508adf9a7Sdjm ASSERT_INT_EQ(convtime("trout"), -1);
4608adf9a7Sdjm ASSERT_INT_EQ(convtime("-77"), -1);
4708adf9a7Sdjm /* boundary conditions */
4808adf9a7Sdjm snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX);
4908adf9a7Sdjm ASSERT_INT_EQ(convtime(buf), INT_MAX);
5008adf9a7Sdjm snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX + 1);
5108adf9a7Sdjm ASSERT_INT_EQ(convtime(buf), -1);
5208adf9a7Sdjm ASSERT_INT_EQ(convtime("3550w5d3h14m7s"), 2147483647);
5308adf9a7Sdjm #if INT_MAX == 2147483647
5408adf9a7Sdjm ASSERT_INT_EQ(convtime("3550w5d3h14m8s"), -1);
5508adf9a7Sdjm #endif
5608adf9a7Sdjm TEST_DONE();
57*d9741277Sdjm
58*d9741277Sdjm /* XXX timezones/DST make verification of this tricky */
59*d9741277Sdjm /* XXX maybe setenv TZ and tzset() to make it unambiguous? */
60*d9741277Sdjm TEST_START("misc_parse_absolute_time");
61*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("20000101", &t), 0);
62*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("200001011223", &t), 0);
63*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("20000101122345", &t), 0);
64*d9741277Sdjm
65*d9741277Sdjm /* forced UTC TZ */
66*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("20000101Z", &t), 0);
67*d9741277Sdjm ASSERT_U64_EQ(t, 946684800);
68*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("200001011223Z", &t), 0);
69*d9741277Sdjm ASSERT_U64_EQ(t, 946729380);
70*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("20000101122345Z", &t), 0);
71*d9741277Sdjm ASSERT_U64_EQ(t, 946729425);
72*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("20000101UTC", &t), 0);
73*d9741277Sdjm ASSERT_U64_EQ(t, 946684800);
74*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("200001011223UTC", &t), 0);
75*d9741277Sdjm ASSERT_U64_EQ(t, 946729380);
76*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("20000101122345UTC", &t), 0);
77*d9741277Sdjm ASSERT_U64_EQ(t, 946729425);
78*d9741277Sdjm
79*d9741277Sdjm /* Bad month */
80*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("20001301", &t),
81*d9741277Sdjm SSH_ERR_INVALID_FORMAT);
82*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("20000001", &t),
83*d9741277Sdjm SSH_ERR_INVALID_FORMAT);
84*d9741277Sdjm /* Incomplete */
85*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("2", &t),
86*d9741277Sdjm SSH_ERR_INVALID_FORMAT);
87*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("2000", &t),
88*d9741277Sdjm SSH_ERR_INVALID_FORMAT);
89*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("20000", &t),
90*d9741277Sdjm SSH_ERR_INVALID_FORMAT);
91*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("200001", &t),
92*d9741277Sdjm SSH_ERR_INVALID_FORMAT);
93*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("2000010", &t),
94*d9741277Sdjm SSH_ERR_INVALID_FORMAT);
95*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("200001010", &t),
96*d9741277Sdjm SSH_ERR_INVALID_FORMAT);
97*d9741277Sdjm /* Bad day, hour, minute, second */
98*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("20000199", &t),
99*d9741277Sdjm SSH_ERR_INVALID_FORMAT);
100*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("200001019900", &t),
101*d9741277Sdjm SSH_ERR_INVALID_FORMAT);
102*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("200001010099", &t),
103*d9741277Sdjm SSH_ERR_INVALID_FORMAT);
104*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("20000101000099", &t),
105*d9741277Sdjm SSH_ERR_INVALID_FORMAT);
106*d9741277Sdjm /* Invalid TZ specifier */
107*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("20000101ZZ", &t),
108*d9741277Sdjm SSH_ERR_INVALID_FORMAT);
109*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("20000101PDT", &t),
110*d9741277Sdjm SSH_ERR_INVALID_FORMAT);
111*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("20000101U", &t),
112*d9741277Sdjm SSH_ERR_INVALID_FORMAT);
113*d9741277Sdjm ASSERT_INT_EQ(parse_absolute_time("20000101UTCUTC", &t),
114*d9741277Sdjm SSH_ERR_INVALID_FORMAT);
115*d9741277Sdjm
116*d9741277Sdjm TEST_DONE();
11708adf9a7Sdjm }
118