1 /* $OpenBSD: tests.c,v 1.4 2021/01/15 02:58:11 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 char buf[1024]; 26 27 TEST_START("misc_parse_user_host_path"); 28 ASSERT_INT_EQ(parse_user_host_path("someuser@some.host:some/path", 29 &user, &host, &path), 0); 30 ASSERT_STRING_EQ(user, "someuser"); 31 ASSERT_STRING_EQ(host, "some.host"); 32 ASSERT_STRING_EQ(path, "some/path"); 33 free(user); free(host); free(path); 34 TEST_DONE(); 35 36 TEST_START("misc_parse_user_ipv4_path"); 37 ASSERT_INT_EQ(parse_user_host_path("someuser@1.22.33.144:some/path", 38 &user, &host, &path), 0); 39 ASSERT_STRING_EQ(user, "someuser"); 40 ASSERT_STRING_EQ(host, "1.22.33.144"); 41 ASSERT_STRING_EQ(path, "some/path"); 42 free(user); free(host); free(path); 43 TEST_DONE(); 44 45 TEST_START("misc_parse_user_[ipv4]_path"); 46 ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:some/path", 47 &user, &host, &path), 0); 48 ASSERT_STRING_EQ(user, "someuser"); 49 ASSERT_STRING_EQ(host, "1.22.33.144"); 50 ASSERT_STRING_EQ(path, "some/path"); 51 free(user); free(host); free(path); 52 TEST_DONE(); 53 54 TEST_START("misc_parse_user_[ipv4]_nopath"); 55 ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:", 56 &user, &host, &path), 0); 57 ASSERT_STRING_EQ(user, "someuser"); 58 ASSERT_STRING_EQ(host, "1.22.33.144"); 59 ASSERT_STRING_EQ(path, "."); 60 free(user); free(host); free(path); 61 TEST_DONE(); 62 63 TEST_START("misc_parse_user_ipv6_path"); 64 ASSERT_INT_EQ(parse_user_host_path("someuser@[::1]:some/path", 65 &user, &host, &path), 0); 66 ASSERT_STRING_EQ(user, "someuser"); 67 ASSERT_STRING_EQ(host, "::1"); 68 ASSERT_STRING_EQ(path, "some/path"); 69 free(user); free(host); free(path); 70 TEST_DONE(); 71 72 TEST_START("misc_parse_uri"); 73 ASSERT_INT_EQ(parse_uri("ssh", "ssh://someuser@some.host:22/some/path", 74 &user, &host, &port, &path), 0); 75 ASSERT_STRING_EQ(user, "someuser"); 76 ASSERT_STRING_EQ(host, "some.host"); 77 ASSERT_INT_EQ(port, 22); 78 ASSERT_STRING_EQ(path, "some/path"); 79 free(user); free(host); free(path); 80 TEST_DONE(); 81 82 TEST_START("misc_convtime"); 83 ASSERT_INT_EQ(convtime("0"), 0); 84 ASSERT_INT_EQ(convtime("1"), 1); 85 ASSERT_INT_EQ(convtime("2s"), 2); 86 ASSERT_INT_EQ(convtime("3m"), 180); 87 ASSERT_INT_EQ(convtime("1m30"), 90); 88 ASSERT_INT_EQ(convtime("1m30s"), 90); 89 ASSERT_INT_EQ(convtime("1h1s"), 3601); 90 ASSERT_INT_EQ(convtime("1h30m"), 90 * 60); 91 ASSERT_INT_EQ(convtime("1d"), 24 * 60 * 60); 92 ASSERT_INT_EQ(convtime("1w"), 7 * 24 * 60 * 60); 93 ASSERT_INT_EQ(convtime("1w2d3h4m5"), 788645); 94 ASSERT_INT_EQ(convtime("1w2d3h4m5s"), 788645); 95 /* any negative number or error returns -1 */ 96 ASSERT_INT_EQ(convtime("-1"), -1); 97 ASSERT_INT_EQ(convtime(""), -1); 98 ASSERT_INT_EQ(convtime("trout"), -1); 99 ASSERT_INT_EQ(convtime("-77"), -1); 100 /* boundary conditions */ 101 snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX); 102 ASSERT_INT_EQ(convtime(buf), INT_MAX); 103 snprintf(buf, sizeof buf, "%llu", (long long unsigned)INT_MAX + 1); 104 ASSERT_INT_EQ(convtime(buf), -1); 105 ASSERT_INT_EQ(convtime("3550w5d3h14m7s"), 2147483647); 106 #if INT_MAX == 2147483647 107 ASSERT_INT_EQ(convtime("3550w5d3h14m8s"), -1); 108 #endif 109 TEST_DONE(); 110 111 TEST_START("dollar_expand"); 112 if (setenv("FOO", "bar", 1) != 0) 113 abort(); 114 if (setenv("BAR", "baz", 1) != 0) 115 abort(); 116 if (unsetenv("BAZ") != 0) 117 abort(); 118 #define ASSERT_DOLLAR_EQ(x, y) do { \ 119 char *str = dollar_expand(NULL, (x)); \ 120 ASSERT_STRING_EQ(str, (y)); \ 121 free(str); \ 122 } while(0) 123 ASSERT_DOLLAR_EQ("${FOO}", "bar"); 124 ASSERT_DOLLAR_EQ(" ${FOO}", " bar"); 125 ASSERT_DOLLAR_EQ("${FOO} ", "bar "); 126 ASSERT_DOLLAR_EQ(" ${FOO} ", " bar "); 127 ASSERT_DOLLAR_EQ("${FOO}${BAR}", "barbaz"); 128 ASSERT_DOLLAR_EQ(" ${FOO} ${BAR}", " bar baz"); 129 ASSERT_DOLLAR_EQ("${FOO}${BAR} ", "barbaz "); 130 ASSERT_DOLLAR_EQ(" ${FOO} ${BAR} ", " bar baz "); 131 ASSERT_DOLLAR_EQ("$", "$"); 132 ASSERT_DOLLAR_EQ(" $", " $"); 133 ASSERT_DOLLAR_EQ("$ ", "$ "); 134 135 /* suppress error messages for error handing tests */ 136 log_init("test_misc", SYSLOG_LEVEL_QUIET, SYSLOG_FACILITY_AUTH, 1); 137 /* error checking, non existent variable */ 138 ret = dollar_expand(&parseerr, "a${BAZ}"); 139 ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0); 140 ret = dollar_expand(&parseerr, "${BAZ}b"); 141 ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0); 142 ret = dollar_expand(&parseerr, "a${BAZ}b"); 143 ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0); 144 /* invalid format */ 145 ret = dollar_expand(&parseerr, "${"); 146 ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1); 147 ret = dollar_expand(&parseerr, "${F"); 148 ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1); 149 ret = dollar_expand(&parseerr, "${FO"); 150 ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1); 151 /* empty variable name */ 152 ret = dollar_expand(&parseerr, "${}"); 153 ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1); 154 /* restore loglevel to default */ 155 log_init("test_misc", SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 1); 156 TEST_DONE(); 157 158 TEST_START("percent_expand"); 159 ASSERT_STRING_EQ(percent_expand("%%", "%h", "foo", NULL), "%"); 160 ASSERT_STRING_EQ(percent_expand("%h", "h", "foo", NULL), "foo"); 161 ASSERT_STRING_EQ(percent_expand("%h ", "h", "foo", NULL), "foo "); 162 ASSERT_STRING_EQ(percent_expand(" %h", "h", "foo", NULL), " foo"); 163 ASSERT_STRING_EQ(percent_expand(" %h ", "h", "foo", NULL), " foo "); 164 ASSERT_STRING_EQ(percent_expand(" %a%b ", "a", "foo", "b", "bar", NULL), 165 " foobar "); 166 TEST_DONE(); 167 168 TEST_START("percent_dollar_expand"); 169 ASSERT_STRING_EQ(percent_dollar_expand("%h${FOO}", "h", "foo", NULL), 170 "foobar"); 171 TEST_DONE(); 172 } 173