1 /* $OpenBSD: tests.c,v 1.1 2017/03/14 01:20:29 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_LONG_EQ(convtime("0"), 0); 30 ASSERT_LONG_EQ(convtime("1"), 1); 31 ASSERT_LONG_EQ(convtime("1S"), 1); 32 /* from the examples in the comment above the function */ 33 ASSERT_LONG_EQ(convtime("90m"), 5400); 34 ASSERT_LONG_EQ(convtime("1h30m"), 5400); 35 ASSERT_LONG_EQ(convtime("2d"), 172800); 36 ASSERT_LONG_EQ(convtime("1w"), 604800); 37 38 /* negative time is not allowed */ 39 ASSERT_LONG_EQ(convtime("-7"), -1); 40 ASSERT_LONG_EQ(convtime("-9d"), -1); 41 42 /* overflow */ 43 snprintf(buf, sizeof buf, "%llu", (unsigned long long)LONG_MAX + 1); 44 ASSERT_LONG_EQ(convtime(buf), -1); 45 46 /* overflow with multiplier */ 47 snprintf(buf, sizeof buf, "%lluM", (unsigned long long)LONG_MAX/60 + 1); 48 ASSERT_LONG_EQ(convtime(buf), -1); 49 ASSERT_LONG_EQ(convtime("1000000000000000000000w"), -1); 50 TEST_DONE(); 51 } 52