xref: /freebsd/crypto/openssh/regress/unittests/conversion/tests.c (revision 3332f1b444d4a73238e9f59cca27bfc95fe936bd)
1 /* 	$OpenBSD: tests.c,v 1.3 2021/01/18 11:43:34 dtucker 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 <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 "misc.h"
22 
23 void
24 tests(void)
25 {
26 	char buf[1024];
27 
28 	TEST_START("conversion_convtime");
29 	ASSERT_INT_EQ(convtime("0"), 0);
30 	ASSERT_INT_EQ(convtime("1"), 1);
31 	ASSERT_INT_EQ(convtime("1S"), 1);
32 	/* from the examples in the comment above the function */
33 	ASSERT_INT_EQ(convtime("90m"), 5400);
34 	ASSERT_INT_EQ(convtime("1h30m"), 5400);
35 	ASSERT_INT_EQ(convtime("2d"), 172800);
36 	ASSERT_INT_EQ(convtime("1w"), 604800);
37 
38 	/* negative time is not allowed */
39 	ASSERT_INT_EQ(convtime("-7"), -1);
40 	ASSERT_INT_EQ(convtime("-9d"), -1);
41 
42 	/* overflow */
43 	snprintf(buf, sizeof buf, "%llu", (unsigned long long)INT_MAX);
44 	ASSERT_INT_EQ(convtime(buf), INT_MAX);
45 	snprintf(buf, sizeof buf, "%llu", (unsigned long long)INT_MAX + 1);
46 	ASSERT_INT_EQ(convtime(buf), -1);
47 
48 	/* overflow with multiplier */
49 	snprintf(buf, sizeof buf, "%lluM", (unsigned long long)INT_MAX/60 + 1);
50 	ASSERT_INT_EQ(convtime(buf), -1);
51 	ASSERT_INT_EQ(convtime("1000000000000000000000w"), -1);
52 	TEST_DONE();
53 }
54