1*1323ec57SEd Maste /* $OpenBSD: test_convtime.c,v 1.2 2021/12/14 21:25:27 deraadt 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> 11*1323ec57SEd 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" 2319261079SEd Maste 2419261079SEd Maste void test_convtime(void); 2519261079SEd Maste 2619261079SEd Maste void 2719261079SEd Maste test_convtime(void) 2819261079SEd Maste { 2919261079SEd Maste char buf[1024]; 3019261079SEd Maste 3119261079SEd Maste TEST_START("misc_convtime"); 3219261079SEd Maste ASSERT_INT_EQ(convtime("0"), 0); 3319261079SEd Maste ASSERT_INT_EQ(convtime("1"), 1); 3419261079SEd Maste ASSERT_INT_EQ(convtime("2s"), 2); 3519261079SEd Maste ASSERT_INT_EQ(convtime("3m"), 180); 3619261079SEd Maste ASSERT_INT_EQ(convtime("1m30"), 90); 3719261079SEd Maste ASSERT_INT_EQ(convtime("1m30s"), 90); 3819261079SEd Maste ASSERT_INT_EQ(convtime("1h1s"), 3601); 3919261079SEd Maste ASSERT_INT_EQ(convtime("1h30m"), 90 * 60); 4019261079SEd Maste ASSERT_INT_EQ(convtime("1d"), 24 * 60 * 60); 4119261079SEd Maste ASSERT_INT_EQ(convtime("1w"), 7 * 24 * 60 * 60); 4219261079SEd Maste ASSERT_INT_EQ(convtime("1w2d3h4m5"), 788645); 4319261079SEd Maste ASSERT_INT_EQ(convtime("1w2d3h4m5s"), 788645); 4419261079SEd Maste /* any negative number or error returns -1 */ 4519261079SEd Maste ASSERT_INT_EQ(convtime("-1"), -1); 4619261079SEd Maste ASSERT_INT_EQ(convtime(""), -1); 4719261079SEd Maste ASSERT_INT_EQ(convtime("trout"), -1); 4819261079SEd Maste ASSERT_INT_EQ(convtime("-77"), -1); 4919261079SEd Maste /* boundary conditions */ 5019261079SEd Maste snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX); 5119261079SEd Maste ASSERT_INT_EQ(convtime(buf), INT_MAX); 5219261079SEd Maste snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX + 1); 5319261079SEd Maste ASSERT_INT_EQ(convtime(buf), -1); 5419261079SEd Maste ASSERT_INT_EQ(convtime("3550w5d3h14m7s"), 2147483647); 5519261079SEd Maste #if INT_MAX == 2147483647 5619261079SEd Maste ASSERT_INT_EQ(convtime("3550w5d3h14m8s"), -1); 5719261079SEd Maste #endif 5819261079SEd Maste TEST_DONE(); 5919261079SEd Maste } 60