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