summaryrefslogtreecommitdiff
path: root/regress/unittests
diff options
context:
space:
mode:
authordtucker@openbsd.org <dtucker@openbsd.org>2017-03-14 01:20:29 +0000
committerDarren Tucker <dtucker@zip.com.au>2017-03-14 13:45:14 +1100
commitf57783f1ddfb4cdfbd612c6beb5ec01cb5b9a6b9 (patch)
tree0e2a81bcbccd1ba92315ff5b2599a488155abcc5 /regress/unittests
parent8884b7247d094cd11ff9e39c325ba928c5bdbc6c (diff)
upstream commit
Add unit test for convtime(). Upstream-Regress-ID: 8717bc0ca4c21120f6dd3a1d3b7a363f707c31e1
Diffstat (limited to 'regress/unittests')
-rw-r--r--regress/unittests/Makefile7
-rw-r--r--regress/unittests/conversion/Makefile10
-rw-r--r--regress/unittests/conversion/tests.c47
3 files changed, 61 insertions, 3 deletions
diff --git a/regress/unittests/Makefile b/regress/unittests/Makefile
index e70b16644..e975f6ca4 100644
--- a/regress/unittests/Makefile
+++ b/regress/unittests/Makefile
@@ -1,5 +1,6 @@
1# $OpenBSD: Makefile,v 1.7 2016/08/19 06:44:13 djm Exp $ 1# $OpenBSD: Makefile,v 1.9 2017/03/14 01:20:29 dtucker Exp $
2REGRESS_FAIL_EARLY= yes 2
3SUBDIR= test_helper sshbuf sshkey bitmap kex hostkeys utf8 match 3REGRESS_FAIL_EARLY?= yes
4SUBDIR= test_helper sshbuf sshkey bitmap kex hostkeys utf8 match conversion
4 5
5.include <bsd.subdir.mk> 6.include <bsd.subdir.mk>
diff --git a/regress/unittests/conversion/Makefile b/regress/unittests/conversion/Makefile
new file mode 100644
index 000000000..cde97dc28
--- /dev/null
+++ b/regress/unittests/conversion/Makefile
@@ -0,0 +1,10 @@
1# $OpenBSD: Makefile,v 1.1 2017/03/14 01:20:29 dtucker Exp $
2
3PROG=test_conversion
4SRCS=tests.c
5REGRESS_TARGETS=run-regress-${PROG}
6
7run-regress-${PROG}: ${PROG}
8 env ${TEST_ENV} ./${PROG}
9
10.include <bsd.regress.mk>
diff --git a/regress/unittests/conversion/tests.c b/regress/unittests/conversion/tests.c
new file mode 100644
index 000000000..ebd7bde00
--- /dev/null
+++ b/regress/unittests/conversion/tests.c
@@ -0,0 +1,47 @@
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 <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
19void
20tests(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 + 1);
40 ASSERT_LONG_EQ(convtime(buf), -1);
41
42 /* overflow with multiplier */
43 snprintf(buf, sizeof buf, "%lluM", (unsigned long long)LONG_MAX/60 + 1);
44 ASSERT_LONG_EQ(convtime(buf), -1);
45 ASSERT_LONG_EQ(convtime("1000000000000000000000w"), -1);
46 TEST_DONE();
47}