1 /* 	$OpenBSD: tests.c,v 1.4 2021/12/14 21:25:27 deraadt Exp $ */
2 /*
3  * Regress test for conversions
4  *
5  * Placed in the public domain
6  */
7 
8 #include "includes.h"
9 
10 #include <sys/types.h>
11 #include <stdio.h>
12 #ifdef HAVE_STDINT_H
13 #include <stdint.h>
14 #endif
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #include "../test_helper/test_helper.h"
19 
20 #include "misc.h"
21 
22 void
23 tests(void)
24 {
25 	char buf[1024];
26 
27 	TEST_START("conversion_convtime");
28 	ASSERT_INT_EQ(convtime("0"), 0);
29 	ASSERT_INT_EQ(convtime("1"), 1);
30 	ASSERT_INT_EQ(convtime("1S"), 1);
31 	/* from the examples in the comment above the function */
32 	ASSERT_INT_EQ(convtime("90m"), 5400);
33 	ASSERT_INT_EQ(convtime("1h30m"), 5400);
34 	ASSERT_INT_EQ(convtime("2d"), 172800);
35 	ASSERT_INT_EQ(convtime("1w"), 604800);
36 
37 	/* negative time is not allowed */
38 	ASSERT_INT_EQ(convtime("-7"), -1);
39 	ASSERT_INT_EQ(convtime("-9d"), -1);
40 
41 	/* overflow */
42 	snprintf(buf, sizeof buf, "%llu", (unsigned long long)INT_MAX);
43 	ASSERT_INT_EQ(convtime(buf), INT_MAX);
44 	snprintf(buf, sizeof buf, "%llu", (unsigned long long)INT_MAX + 1);
45 	ASSERT_INT_EQ(convtime(buf), -1);
46 
47 	/* overflow with multiplier */
48 	snprintf(buf, sizeof buf, "%lluM", (unsigned long long)INT_MAX/60 + 1);
49 	ASSERT_INT_EQ(convtime(buf), -1);
50 	ASSERT_INT_EQ(convtime("1000000000000000000000w"), -1);
51 	TEST_DONE();
52 }
53