diff options
Diffstat (limited to 'regress')
-rw-r--r-- | regress/usr.bin/ssh/unittests/misc/Makefile | 5 | ||||
-rw-r--r-- | regress/usr.bin/ssh/unittests/misc/test_convtime.c | 55 | ||||
-rw-r--r-- | regress/usr.bin/ssh/unittests/misc/test_expand.c | 86 | ||||
-rw-r--r-- | regress/usr.bin/ssh/unittests/misc/test_parse.c | 82 | ||||
-rw-r--r-- | regress/usr.bin/ssh/unittests/misc/tests.c | 158 |
5 files changed, 235 insertions, 151 deletions
diff --git a/regress/usr.bin/ssh/unittests/misc/Makefile b/regress/usr.bin/ssh/unittests/misc/Makefile index 04eddcd788a..63d9c4775d9 100644 --- a/regress/usr.bin/ssh/unittests/misc/Makefile +++ b/regress/usr.bin/ssh/unittests/misc/Makefile @@ -1,7 +1,10 @@ -# $OpenBSD: Makefile,v 1.4 2021/01/09 12:24:31 dtucker Exp $ +# $OpenBSD: Makefile,v 1.5 2021/03/19 03:25:01 djm Exp $ PROG=test_misc SRCS=tests.c +SRCS+= test_convtime.c +SRCS+= test_expand.c +SRCS+= test_parse.c # From usr.bin/ssh/Makefile.inc SRCS+= sshbuf.c diff --git a/regress/usr.bin/ssh/unittests/misc/test_convtime.c b/regress/usr.bin/ssh/unittests/misc/test_convtime.c new file mode 100644 index 00000000000..8da05a26181 --- /dev/null +++ b/regress/usr.bin/ssh/unittests/misc/test_convtime.c @@ -0,0 +1,55 @@ +/* $OpenBSD: test_convtime.c,v 1.1 2021/03/19 03:25:01 djm Exp $ */ +/* + * Regress test for misc time conversion functions. + * + * Placed in the public domain. + */ + +#include <sys/types.h> +#include <sys/param.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "test_helper.h" + +#include "log.h" +#include "misc.h" + +void test_convtime(void); + +void +test_convtime(void) +{ + char buf[1024]; + + TEST_START("misc_convtime"); + ASSERT_INT_EQ(convtime("0"), 0); + ASSERT_INT_EQ(convtime("1"), 1); + ASSERT_INT_EQ(convtime("2s"), 2); + ASSERT_INT_EQ(convtime("3m"), 180); + ASSERT_INT_EQ(convtime("1m30"), 90); + ASSERT_INT_EQ(convtime("1m30s"), 90); + ASSERT_INT_EQ(convtime("1h1s"), 3601); + ASSERT_INT_EQ(convtime("1h30m"), 90 * 60); + ASSERT_INT_EQ(convtime("1d"), 24 * 60 * 60); + ASSERT_INT_EQ(convtime("1w"), 7 * 24 * 60 * 60); + ASSERT_INT_EQ(convtime("1w2d3h4m5"), 788645); + ASSERT_INT_EQ(convtime("1w2d3h4m5s"), 788645); + /* any negative number or error returns -1 */ + ASSERT_INT_EQ(convtime("-1"), -1); + ASSERT_INT_EQ(convtime(""), -1); + ASSERT_INT_EQ(convtime("trout"), -1); + ASSERT_INT_EQ(convtime("-77"), -1); + /* boundary conditions */ + snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX); + ASSERT_INT_EQ(convtime(buf), INT_MAX); + snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX + 1); + ASSERT_INT_EQ(convtime(buf), -1); + ASSERT_INT_EQ(convtime("3550w5d3h14m7s"), 2147483647); +#if INT_MAX == 2147483647 + ASSERT_INT_EQ(convtime("3550w5d3h14m8s"), -1); +#endif + TEST_DONE(); +} diff --git a/regress/usr.bin/ssh/unittests/misc/test_expand.c b/regress/usr.bin/ssh/unittests/misc/test_expand.c new file mode 100644 index 00000000000..2a5e1bf1a5d --- /dev/null +++ b/regress/usr.bin/ssh/unittests/misc/test_expand.c @@ -0,0 +1,86 @@ +/* $OpenBSD: test_expand.c,v 1.1 2021/03/19 03:25:01 djm Exp $ */ +/* + * Regress test for misc string expansion functions. + * + * Placed in the public domain. + */ + +#include <sys/types.h> +#include <sys/param.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "test_helper.h" + +#include "log.h" +#include "misc.h" + +void test_expand(void); + +void +test_expand(void) +{ + int parseerr; + char *ret; + + TEST_START("dollar_expand"); + ASSERT_INT_EQ(setenv("FOO", "bar", 1), 0); + ASSERT_INT_EQ(setenv("BAR", "baz", 1), 0); + ASSERT_INT_EQ(unsetenv("BAZ"), 0); +#define ASSERT_DOLLAR_EQ(x, y) do { \ + char *str = dollar_expand(NULL, (x)); \ + ASSERT_STRING_EQ(str, (y)); \ + free(str); \ +} while(0) + ASSERT_DOLLAR_EQ("${FOO}", "bar"); + ASSERT_DOLLAR_EQ(" ${FOO}", " bar"); + ASSERT_DOLLAR_EQ("${FOO} ", "bar "); + ASSERT_DOLLAR_EQ(" ${FOO} ", " bar "); + ASSERT_DOLLAR_EQ("${FOO}${BAR}", "barbaz"); + ASSERT_DOLLAR_EQ(" ${FOO} ${BAR}", " bar baz"); + ASSERT_DOLLAR_EQ("${FOO}${BAR} ", "barbaz "); + ASSERT_DOLLAR_EQ(" ${FOO} ${BAR} ", " bar baz "); + ASSERT_DOLLAR_EQ("$", "$"); + ASSERT_DOLLAR_EQ(" $", " $"); + ASSERT_DOLLAR_EQ("$ ", "$ "); + + /* suppress error messages for error handing tests */ + log_init("test_misc", SYSLOG_LEVEL_QUIET, SYSLOG_FACILITY_AUTH, 1); + /* error checking, non existent variable */ + ret = dollar_expand(&parseerr, "a${BAZ}"); + ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0); + ret = dollar_expand(&parseerr, "${BAZ}b"); + ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0); + ret = dollar_expand(&parseerr, "a${BAZ}b"); + ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0); + /* invalid format */ + ret = dollar_expand(&parseerr, "${"); + ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1); + ret = dollar_expand(&parseerr, "${F"); + ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1); + ret = dollar_expand(&parseerr, "${FO"); + ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1); + /* empty variable name */ + ret = dollar_expand(&parseerr, "${}"); + ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1); + /* restore loglevel to default */ + log_init("test_misc", SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 1); + TEST_DONE(); + + TEST_START("percent_expand"); + ASSERT_STRING_EQ(percent_expand("%%", "%h", "foo", NULL), "%"); + ASSERT_STRING_EQ(percent_expand("%h", "h", "foo", NULL), "foo"); + ASSERT_STRING_EQ(percent_expand("%h ", "h", "foo", NULL), "foo "); + ASSERT_STRING_EQ(percent_expand(" %h", "h", "foo", NULL), " foo"); + ASSERT_STRING_EQ(percent_expand(" %h ", "h", "foo", NULL), " foo "); + ASSERT_STRING_EQ(percent_expand(" %a%b ", "a", "foo", "b", "bar", NULL), + " foobar "); + TEST_DONE(); + + TEST_START("percent_dollar_expand"); + ASSERT_STRING_EQ(percent_dollar_expand("%h${FOO}", "h", "foo", NULL), + "foobar"); + TEST_DONE(); +} diff --git a/regress/usr.bin/ssh/unittests/misc/test_parse.c b/regress/usr.bin/ssh/unittests/misc/test_parse.c new file mode 100644 index 00000000000..6fa4558e22d --- /dev/null +++ b/regress/usr.bin/ssh/unittests/misc/test_parse.c @@ -0,0 +1,82 @@ +/* $OpenBSD: test_parse.c,v 1.1 2021/03/19 03:25:01 djm Exp $ */ +/* + * Regress test for misc user/host/URI parsing functions. + * + * Placed in the public domain. + */ + +#include <sys/types.h> +#include <sys/param.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "test_helper.h" + +#include "log.h" +#include "misc.h" + +void test_parse(void); + +void +test_parse(void) +{ + int port; + char *user, *host, *path; + + TEST_START("misc_parse_user_host_path"); + ASSERT_INT_EQ(parse_user_host_path("someuser@some.host:some/path", + &user, &host, &path), 0); + ASSERT_STRING_EQ(user, "someuser"); + ASSERT_STRING_EQ(host, "some.host"); + ASSERT_STRING_EQ(path, "some/path"); + free(user); free(host); free(path); + TEST_DONE(); + + TEST_START("misc_parse_user_ipv4_path"); + ASSERT_INT_EQ(parse_user_host_path("someuser@1.22.33.144:some/path", + &user, &host, &path), 0); + ASSERT_STRING_EQ(user, "someuser"); + ASSERT_STRING_EQ(host, "1.22.33.144"); + ASSERT_STRING_EQ(path, "some/path"); + free(user); free(host); free(path); + TEST_DONE(); + + TEST_START("misc_parse_user_[ipv4]_path"); + ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:some/path", + &user, &host, &path), 0); + ASSERT_STRING_EQ(user, "someuser"); + ASSERT_STRING_EQ(host, "1.22.33.144"); + ASSERT_STRING_EQ(path, "some/path"); + free(user); free(host); free(path); + TEST_DONE(); + + TEST_START("misc_parse_user_[ipv4]_nopath"); + ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:", + &user, &host, &path), 0); + ASSERT_STRING_EQ(user, "someuser"); + ASSERT_STRING_EQ(host, "1.22.33.144"); + ASSERT_STRING_EQ(path, "."); + free(user); free(host); free(path); + TEST_DONE(); + + TEST_START("misc_parse_user_ipv6_path"); + ASSERT_INT_EQ(parse_user_host_path("someuser@[::1]:some/path", + &user, &host, &path), 0); + ASSERT_STRING_EQ(user, "someuser"); + ASSERT_STRING_EQ(host, "::1"); + ASSERT_STRING_EQ(path, "some/path"); + free(user); free(host); free(path); + TEST_DONE(); + + TEST_START("misc_parse_uri"); + ASSERT_INT_EQ(parse_uri("ssh", "ssh://someuser@some.host:22/some/path", + &user, &host, &port, &path), 0); + ASSERT_STRING_EQ(user, "someuser"); + ASSERT_STRING_EQ(host, "some.host"); + ASSERT_INT_EQ(port, 22); + ASSERT_STRING_EQ(path, "some/path"); + free(user); free(host); free(path); + TEST_DONE(); +} diff --git a/regress/usr.bin/ssh/unittests/misc/tests.c b/regress/usr.bin/ssh/unittests/misc/tests.c index d873dc24154..0e2218f9e87 100644 --- a/regress/usr.bin/ssh/unittests/misc/tests.c +++ b/regress/usr.bin/ssh/unittests/misc/tests.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tests.c,v 1.4 2021/01/15 02:58:11 dtucker Exp $ */ +/* $OpenBSD: tests.c,v 1.5 2021/03/19 03:25:01 djm Exp $ */ /* * Regress test for misc helper functions. * @@ -17,156 +17,14 @@ #include "log.h" #include "misc.h" +void test_parse(void); +void test_convtime(void); +void test_expand(void); + void tests(void) { - int port, parseerr; - char *user, *host, *path, *ret; - char buf[1024]; - - TEST_START("misc_parse_user_host_path"); - ASSERT_INT_EQ(parse_user_host_path("someuser@some.host:some/path", - &user, &host, &path), 0); - ASSERT_STRING_EQ(user, "someuser"); - ASSERT_STRING_EQ(host, "some.host"); - ASSERT_STRING_EQ(path, "some/path"); - free(user); free(host); free(path); - TEST_DONE(); - - TEST_START("misc_parse_user_ipv4_path"); - ASSERT_INT_EQ(parse_user_host_path("someuser@1.22.33.144:some/path", - &user, &host, &path), 0); - ASSERT_STRING_EQ(user, "someuser"); - ASSERT_STRING_EQ(host, "1.22.33.144"); - ASSERT_STRING_EQ(path, "some/path"); - free(user); free(host); free(path); - TEST_DONE(); - - TEST_START("misc_parse_user_[ipv4]_path"); - ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:some/path", - &user, &host, &path), 0); - ASSERT_STRING_EQ(user, "someuser"); - ASSERT_STRING_EQ(host, "1.22.33.144"); - ASSERT_STRING_EQ(path, "some/path"); - free(user); free(host); free(path); - TEST_DONE(); - - TEST_START("misc_parse_user_[ipv4]_nopath"); - ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:", - &user, &host, &path), 0); - ASSERT_STRING_EQ(user, "someuser"); - ASSERT_STRING_EQ(host, "1.22.33.144"); - ASSERT_STRING_EQ(path, "."); - free(user); free(host); free(path); - TEST_DONE(); - - TEST_START("misc_parse_user_ipv6_path"); - ASSERT_INT_EQ(parse_user_host_path("someuser@[::1]:some/path", - &user, &host, &path), 0); - ASSERT_STRING_EQ(user, "someuser"); - ASSERT_STRING_EQ(host, "::1"); - ASSERT_STRING_EQ(path, "some/path"); - free(user); free(host); free(path); - TEST_DONE(); - - TEST_START("misc_parse_uri"); - ASSERT_INT_EQ(parse_uri("ssh", "ssh://someuser@some.host:22/some/path", - &user, &host, &port, &path), 0); - ASSERT_STRING_EQ(user, "someuser"); - ASSERT_STRING_EQ(host, "some.host"); - ASSERT_INT_EQ(port, 22); - ASSERT_STRING_EQ(path, "some/path"); - free(user); free(host); free(path); - TEST_DONE(); - - TEST_START("misc_convtime"); - ASSERT_INT_EQ(convtime("0"), 0); - ASSERT_INT_EQ(convtime("1"), 1); - ASSERT_INT_EQ(convtime("2s"), 2); - ASSERT_INT_EQ(convtime("3m"), 180); - ASSERT_INT_EQ(convtime("1m30"), 90); - ASSERT_INT_EQ(convtime("1m30s"), 90); - ASSERT_INT_EQ(convtime("1h1s"), 3601); - ASSERT_INT_EQ(convtime("1h30m"), 90 * 60); - ASSERT_INT_EQ(convtime("1d"), 24 * 60 * 60); - ASSERT_INT_EQ(convtime("1w"), 7 * 24 * 60 * 60); - ASSERT_INT_EQ(convtime("1w2d3h4m5"), 788645); - ASSERT_INT_EQ(convtime("1w2d3h4m5s"), 788645); - /* any negative number or error returns -1 */ - ASSERT_INT_EQ(convtime("-1"), -1); - ASSERT_INT_EQ(convtime(""), -1); - ASSERT_INT_EQ(convtime("trout"), -1); - ASSERT_INT_EQ(convtime("-77"), -1); - /* boundary conditions */ - snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX); - ASSERT_INT_EQ(convtime(buf), INT_MAX); - snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX + 1); - ASSERT_INT_EQ(convtime(buf), -1); - ASSERT_INT_EQ(convtime("3550w5d3h14m7s"), 2147483647); -#if INT_MAX == 2147483647 - ASSERT_INT_EQ(convtime("3550w5d3h14m8s"), -1); -#endif - TEST_DONE(); - - TEST_START("dollar_expand"); - if (setenv("FOO", "bar", 1) != 0) - abort(); - if (setenv("BAR", "baz", 1) != 0) - abort(); - if (unsetenv("BAZ") != 0) - abort(); -#define ASSERT_DOLLAR_EQ(x, y) do { \ - char *str = dollar_expand(NULL, (x)); \ - ASSERT_STRING_EQ(str, (y)); \ - free(str); \ -} while(0) - ASSERT_DOLLAR_EQ("${FOO}", "bar"); - ASSERT_DOLLAR_EQ(" ${FOO}", " bar"); - ASSERT_DOLLAR_EQ("${FOO} ", "bar "); - ASSERT_DOLLAR_EQ(" ${FOO} ", " bar "); - ASSERT_DOLLAR_EQ("${FOO}${BAR}", "barbaz"); - ASSERT_DOLLAR_EQ(" ${FOO} ${BAR}", " bar baz"); - ASSERT_DOLLAR_EQ("${FOO}${BAR} ", "barbaz "); - ASSERT_DOLLAR_EQ(" ${FOO} ${BAR} ", " bar baz "); - ASSERT_DOLLAR_EQ("$", "$"); - ASSERT_DOLLAR_EQ(" $", " $"); - ASSERT_DOLLAR_EQ("$ ", "$ "); - - /* suppress error messages for error handing tests */ - log_init("test_misc", SYSLOG_LEVEL_QUIET, SYSLOG_FACILITY_AUTH, 1); - /* error checking, non existent variable */ - ret = dollar_expand(&parseerr, "a${BAZ}"); - ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0); - ret = dollar_expand(&parseerr, "${BAZ}b"); - ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0); - ret = dollar_expand(&parseerr, "a${BAZ}b"); - ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0); - /* invalid format */ - ret = dollar_expand(&parseerr, "${"); - ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1); - ret = dollar_expand(&parseerr, "${F"); - ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1); - ret = dollar_expand(&parseerr, "${FO"); - ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1); - /* empty variable name */ - ret = dollar_expand(&parseerr, "${}"); - ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1); - /* restore loglevel to default */ - log_init("test_misc", SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 1); - TEST_DONE(); - - TEST_START("percent_expand"); - ASSERT_STRING_EQ(percent_expand("%%", "%h", "foo", NULL), "%"); - ASSERT_STRING_EQ(percent_expand("%h", "h", "foo", NULL), "foo"); - ASSERT_STRING_EQ(percent_expand("%h ", "h", "foo", NULL), "foo "); - ASSERT_STRING_EQ(percent_expand(" %h", "h", "foo", NULL), " foo"); - ASSERT_STRING_EQ(percent_expand(" %h ", "h", "foo", NULL), " foo "); - ASSERT_STRING_EQ(percent_expand(" %a%b ", "a", "foo", "b", "bar", NULL), - " foobar "); - TEST_DONE(); - - TEST_START("percent_dollar_expand"); - ASSERT_STRING_EQ(percent_dollar_expand("%h${FOO}", "h", "foo", NULL), - "foobar"); - TEST_DONE(); + test_parse(); + test_convtime(); + test_expand(); } |