summaryrefslogtreecommitdiff
path: root/regress/unittests/misc/tests.c
diff options
context:
space:
mode:
Diffstat (limited to 'regress/unittests/misc/tests.c')
-rw-r--r--regress/unittests/misc/tests.c88
1 files changed, 85 insertions, 3 deletions
diff --git a/regress/unittests/misc/tests.c b/regress/unittests/misc/tests.c
index ed775ebbd..0bd0c84f9 100644
--- a/regress/unittests/misc/tests.c
+++ b/regress/unittests/misc/tests.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tests.c,v 1.1 2019/04/28 22:53:26 dtucker Exp $ */ 1/* $OpenBSD: tests.c,v 1.3 2020/05/29 04:32:26 dtucker Exp $ */
2/* 2/*
3 * Regress test for misc helper functions. 3 * Regress test for misc helper functions.
4 * 4 *
@@ -14,13 +14,14 @@
14 14
15#include "test_helper.h" 15#include "test_helper.h"
16 16
17#include "log.h"
17#include "misc.h" 18#include "misc.h"
18 19
19void 20void
20tests(void) 21tests(void)
21{ 22{
22 int port; 23 int port, parseerr;
23 char *user, *host, *path; 24 char *user, *host, *path, *ret;
24 25
25 TEST_START("misc_parse_user_host_path"); 26 TEST_START("misc_parse_user_host_path");
26 ASSERT_INT_EQ(parse_user_host_path("someuser@some.host:some/path", 27 ASSERT_INT_EQ(parse_user_host_path("someuser@some.host:some/path",
@@ -76,4 +77,85 @@ tests(void)
76 ASSERT_STRING_EQ(path, "some/path"); 77 ASSERT_STRING_EQ(path, "some/path");
77 free(user); free(host); free(path); 78 free(user); free(host); free(path);
78 TEST_DONE(); 79 TEST_DONE();
80
81 TEST_START("misc_convtime");
82 ASSERT_LONG_EQ(convtime("1"), 1);
83 ASSERT_LONG_EQ(convtime("2s"), 2);
84 ASSERT_LONG_EQ(convtime("3m"), 180);
85 ASSERT_LONG_EQ(convtime("1m30"), 90);
86 ASSERT_LONG_EQ(convtime("1m30s"), 90);
87 ASSERT_LONG_EQ(convtime("1h1s"), 3601);
88 ASSERT_LONG_EQ(convtime("1h30m"), 90 * 60);
89 ASSERT_LONG_EQ(convtime("1d"), 24 * 60 * 60);
90 ASSERT_LONG_EQ(convtime("1w"), 7 * 24 * 60 * 60);
91 ASSERT_LONG_EQ(convtime("1w2d3h4m5"), 788645);
92 ASSERT_LONG_EQ(convtime("1w2d3h4m5s"), 788645);
93 /* any negative number or error returns -1 */
94 ASSERT_LONG_EQ(convtime("-1"), -1);
95 ASSERT_LONG_EQ(convtime(""), -1);
96 ASSERT_LONG_EQ(convtime("trout"), -1);
97 ASSERT_LONG_EQ(convtime("-77"), -1);
98 TEST_DONE();
99
100 TEST_START("dollar_expand");
101 if (setenv("FOO", "bar", 1) != 0)
102 abort();
103 if (setenv("BAR", "baz", 1) != 0)
104 abort();
105 if (unsetenv("BAZ") != 0)
106 abort();
107#define ASSERT_DOLLAR_EQ(x, y) do { \
108 char *str = dollar_expand(NULL, (x)); \
109 ASSERT_STRING_EQ(str, (y)); \
110 free(str); \
111} while(0)
112 ASSERT_DOLLAR_EQ("${FOO}", "bar");
113 ASSERT_DOLLAR_EQ(" ${FOO}", " bar");
114 ASSERT_DOLLAR_EQ("${FOO} ", "bar ");
115 ASSERT_DOLLAR_EQ(" ${FOO} ", " bar ");
116 ASSERT_DOLLAR_EQ("${FOO}${BAR}", "barbaz");
117 ASSERT_DOLLAR_EQ(" ${FOO} ${BAR}", " bar baz");
118 ASSERT_DOLLAR_EQ("${FOO}${BAR} ", "barbaz ");
119 ASSERT_DOLLAR_EQ(" ${FOO} ${BAR} ", " bar baz ");
120 ASSERT_DOLLAR_EQ("$", "$");
121 ASSERT_DOLLAR_EQ(" $", " $");
122 ASSERT_DOLLAR_EQ("$ ", "$ ");
123
124 /* suppress error messages for error handing tests */
125 log_init("test_misc", SYSLOG_LEVEL_QUIET, SYSLOG_FACILITY_AUTH, 1);
126 /* error checking, non existent variable */
127 ret = dollar_expand(&parseerr, "a${BAZ}");
128 ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0);
129 ret = dollar_expand(&parseerr, "${BAZ}b");
130 ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0);
131 ret = dollar_expand(&parseerr, "a${BAZ}b");
132 ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0);
133 /* invalid format */
134 ret = dollar_expand(&parseerr, "${");
135 ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1);
136 ret = dollar_expand(&parseerr, "${F");
137 ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1);
138 ret = dollar_expand(&parseerr, "${FO");
139 ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1);
140 /* empty variable name */
141 ret = dollar_expand(&parseerr, "${}");
142 ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1);
143 /* restore loglevel to default */
144 log_init("test_misc", SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 1);
145 TEST_DONE();
146
147 TEST_START("percent_expand");
148 ASSERT_STRING_EQ(percent_expand("%%", "%h", "foo", NULL), "%");
149 ASSERT_STRING_EQ(percent_expand("%h", "h", "foo", NULL), "foo");
150 ASSERT_STRING_EQ(percent_expand("%h ", "h", "foo", NULL), "foo ");
151 ASSERT_STRING_EQ(percent_expand(" %h", "h", "foo", NULL), " foo");
152 ASSERT_STRING_EQ(percent_expand(" %h ", "h", "foo", NULL), " foo ");
153 ASSERT_STRING_EQ(percent_expand(" %a%b ", "a", "foo", "b", "bar", NULL),
154 " foobar ");
155 TEST_DONE();
156
157 TEST_START("percent_dollar_expand");
158 ASSERT_STRING_EQ(percent_dollar_expand("%h${FOO}", "h", "foo", NULL),
159 "foobar");
160 TEST_DONE();
79} 161}