1 /* 	$OpenBSD: test_convtime.c,v 1.1 2021/03/19 03:25:01 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 <sys/param.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 
24 void test_convtime(void);
25 
26 void
27 test_convtime(void)
28 {
29 	char buf[1024];
30 
31 	TEST_START("misc_convtime");
32 	ASSERT_INT_EQ(convtime("0"), 0);
33 	ASSERT_INT_EQ(convtime("1"), 1);
34 	ASSERT_INT_EQ(convtime("2s"), 2);
35 	ASSERT_INT_EQ(convtime("3m"), 180);
36 	ASSERT_INT_EQ(convtime("1m30"), 90);
37 	ASSERT_INT_EQ(convtime("1m30s"), 90);
38 	ASSERT_INT_EQ(convtime("1h1s"), 3601);
39 	ASSERT_INT_EQ(convtime("1h30m"), 90 * 60);
40 	ASSERT_INT_EQ(convtime("1d"), 24 * 60 * 60);
41 	ASSERT_INT_EQ(convtime("1w"), 7 * 24 * 60 * 60);
42 	ASSERT_INT_EQ(convtime("1w2d3h4m5"), 788645);
43 	ASSERT_INT_EQ(convtime("1w2d3h4m5s"), 788645);
44 	/* any negative number or error returns -1 */
45 	ASSERT_INT_EQ(convtime("-1"),  -1);
46 	ASSERT_INT_EQ(convtime(""),  -1);
47 	ASSERT_INT_EQ(convtime("trout"),  -1);
48 	ASSERT_INT_EQ(convtime("-77"),  -1);
49 	/* boundary conditions */
50 	snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX);
51 	ASSERT_INT_EQ(convtime(buf), INT_MAX);
52 	snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX + 1);
53 	ASSERT_INT_EQ(convtime(buf), -1);
54 	ASSERT_INT_EQ(convtime("3550w5d3h14m7s"), 2147483647);
55 #if INT_MAX == 2147483647
56 	ASSERT_INT_EQ(convtime("3550w5d3h14m8s"), -1);
57 #endif
58 	TEST_DONE();
59 }
60