1 /* $OpenBSD: tests.c,v 1.3 2020/05/29 04:32:26 dtucker Exp $ */ 2 /* 3 * Regress test for misc helper functions. 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 "log.h" 18 #include "misc.h" 19 20 void 21 tests(void) 22 { 23 int port, parseerr; 24 char *user, *host, *path, *ret; 25 26 TEST_START("misc_parse_user_host_path"); 27 ASSERT_INT_EQ(parse_user_host_path("someuser@some.host:some/path", 28 &user, &host, &path), 0); 29 ASSERT_STRING_EQ(user, "someuser"); 30 ASSERT_STRING_EQ(host, "some.host"); 31 ASSERT_STRING_EQ(path, "some/path"); 32 free(user); free(host); free(path); 33 TEST_DONE(); 34 35 TEST_START("misc_parse_user_ipv4_path"); 36 ASSERT_INT_EQ(parse_user_host_path("someuser@1.22.33.144:some/path", 37 &user, &host, &path), 0); 38 ASSERT_STRING_EQ(user, "someuser"); 39 ASSERT_STRING_EQ(host, "1.22.33.144"); 40 ASSERT_STRING_EQ(path, "some/path"); 41 free(user); free(host); free(path); 42 TEST_DONE(); 43 44 TEST_START("misc_parse_user_[ipv4]_path"); 45 ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:some/path", 46 &user, &host, &path), 0); 47 ASSERT_STRING_EQ(user, "someuser"); 48 ASSERT_STRING_EQ(host, "1.22.33.144"); 49 ASSERT_STRING_EQ(path, "some/path"); 50 free(user); free(host); free(path); 51 TEST_DONE(); 52 53 TEST_START("misc_parse_user_[ipv4]_nopath"); 54 ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:", 55 &user, &host, &path), 0); 56 ASSERT_STRING_EQ(user, "someuser"); 57 ASSERT_STRING_EQ(host, "1.22.33.144"); 58 ASSERT_STRING_EQ(path, "."); 59 free(user); free(host); free(path); 60 TEST_DONE(); 61 62 TEST_START("misc_parse_user_ipv6_path"); 63 ASSERT_INT_EQ(parse_user_host_path("someuser@[::1]:some/path", 64 &user, &host, &path), 0); 65 ASSERT_STRING_EQ(user, "someuser"); 66 ASSERT_STRING_EQ(host, "::1"); 67 ASSERT_STRING_EQ(path, "some/path"); 68 free(user); free(host); free(path); 69 TEST_DONE(); 70 71 TEST_START("misc_parse_uri"); 72 ASSERT_INT_EQ(parse_uri("ssh", "ssh://someuser@some.host:22/some/path", 73 &user, &host, &port, &path), 0); 74 ASSERT_STRING_EQ(user, "someuser"); 75 ASSERT_STRING_EQ(host, "some.host"); 76 ASSERT_INT_EQ(port, 22); 77 ASSERT_STRING_EQ(path, "some/path"); 78 free(user); free(host); free(path); 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(); 161 } 162