1321987Sdes/* 	$OpenBSD: tests.c,v 1.1 2017/03/14 01:20:29 dtucker Exp $ */
2321987Sdes/*
3321987Sdes * Regress test for conversions
4321987Sdes *
5321987Sdes * Placed in the public domain
6321987Sdes */
7321987Sdes
8321987Sdes#include "includes.h"
9321987Sdes
10321987Sdes#include <sys/types.h>
11321987Sdes#include <sys/param.h>
12321987Sdes#include <stdio.h>
13321987Sdes#ifdef HAVE_STDINT_H
14321987Sdes#include <stdint.h>
15321987Sdes#endif
16321987Sdes#include <stdlib.h>
17321987Sdes#include <string.h>
18321987Sdes
19321987Sdes#include "../test_helper/test_helper.h"
20321987Sdes
21321987Sdes#include "misc.h"
22321987Sdes
23321987Sdesvoid
24321987Sdestests(void)
25321987Sdes{
26321987Sdes	char buf[1024];
27321987Sdes
28321987Sdes	TEST_START("conversion_convtime");
29321987Sdes	ASSERT_LONG_EQ(convtime("0"), 0);
30321987Sdes	ASSERT_LONG_EQ(convtime("1"), 1);
31321987Sdes	ASSERT_LONG_EQ(convtime("1S"), 1);
32321987Sdes	/* from the examples in the comment above the function */
33321987Sdes	ASSERT_LONG_EQ(convtime("90m"), 5400);
34321987Sdes	ASSERT_LONG_EQ(convtime("1h30m"), 5400);
35321987Sdes	ASSERT_LONG_EQ(convtime("2d"), 172800);
36321987Sdes	ASSERT_LONG_EQ(convtime("1w"), 604800);
37321987Sdes
38321987Sdes	/* negative time is not allowed */
39321987Sdes	ASSERT_LONG_EQ(convtime("-7"), -1);
40321987Sdes	ASSERT_LONG_EQ(convtime("-9d"), -1);
41321987Sdes
42321987Sdes	/* overflow */
43321987Sdes	snprintf(buf, sizeof buf, "%llu", (unsigned long long)LONG_MAX + 1);
44321987Sdes	ASSERT_LONG_EQ(convtime(buf), -1);
45321987Sdes
46321987Sdes	/* overflow with multiplier */
47321987Sdes	snprintf(buf, sizeof buf, "%lluM", (unsigned long long)LONG_MAX/60 + 1);
48321987Sdes	ASSERT_LONG_EQ(convtime(buf), -1);
49321987Sdes	ASSERT_LONG_EQ(convtime("1000000000000000000000w"), -1);
50321987Sdes	TEST_DONE();
51321987Sdes}
52