| 1 | /* |
| 2 | * Copyright (C) - 2012 David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by as |
| 6 | * published by the Free Software Foundation; only version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #include <assert.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include <tap/tap.h> |
| 22 | |
| 23 | #include <common/uri.h> |
| 24 | |
| 25 | /* For error.h */ |
| 26 | int lttng_opt_quiet = 1; |
| 27 | int lttng_opt_verbose = 3; |
| 28 | int lttng_opt_mi; |
| 29 | |
| 30 | /* Number of TAP tests in this file */ |
| 31 | #define NUM_TESTS 11 |
| 32 | |
| 33 | void test_uri_parsing(void) |
| 34 | { |
| 35 | ssize_t size; |
| 36 | const char *s_uri1; |
| 37 | struct lttng_uri *uri = NULL; |
| 38 | |
| 39 | s_uri1 = "net://localhost"; |
| 40 | |
| 41 | size = uri_parse(s_uri1, &uri); |
| 42 | |
| 43 | ok(size == 2 && |
| 44 | uri[0].dtype == LTTNG_DST_IPV4 && |
| 45 | uri[0].utype == LTTNG_URI_DST && |
| 46 | uri[0].stype == 0 && |
| 47 | uri[0].port == 0 && |
| 48 | strlen(uri[0].subdir) == 0 && |
| 49 | strcmp(uri[0].dst.ipv4, "127.0.0.1") == 0 && |
| 50 | uri[1].dtype == LTTNG_DST_IPV4 && |
| 51 | uri[1].utype == LTTNG_URI_DST && |
| 52 | uri[1].stype == 0 && |
| 53 | uri[1].port == 0 && |
| 54 | strlen(uri[1].subdir) == 0 && |
| 55 | strcmp(uri[1].dst.ipv4, "127.0.0.1") == 0, |
| 56 | "URI set to net://localhost"); |
| 57 | |
| 58 | if (uri) { |
| 59 | uri_free(uri); |
| 60 | uri = NULL; |
| 61 | } |
| 62 | |
| 63 | s_uri1 = "net://localhost:8989:4242/my/test/path"; |
| 64 | |
| 65 | size = uri_parse(s_uri1, &uri); |
| 66 | |
| 67 | ok(size == 2 && |
| 68 | uri[0].dtype == LTTNG_DST_IPV4 && |
| 69 | uri[0].utype == LTTNG_URI_DST && |
| 70 | uri[0].stype == 0 && |
| 71 | uri[0].port == 8989 && |
| 72 | strcmp(uri[0].subdir, "my/test/path") == 0 && |
| 73 | strcmp(uri[0].dst.ipv4, "127.0.0.1") == 0 && |
| 74 | uri[1].dtype == LTTNG_DST_IPV4 && |
| 75 | uri[1].utype == LTTNG_URI_DST && |
| 76 | uri[1].stype == 0 && |
| 77 | uri[1].port == 4242 && |
| 78 | strlen(uri[1].subdir) == 0 && |
| 79 | strcmp(uri[1].dst.ipv4, "127.0.0.1") == 0, |
| 80 | "URI set to net://localhost:8989:4242/my/test/path"); |
| 81 | |
| 82 | if (uri) { |
| 83 | uri_free(uri); |
| 84 | uri = NULL; |
| 85 | } |
| 86 | |
| 87 | s_uri1 = "net://localhost:8989:4242"; |
| 88 | |
| 89 | size = uri_parse(s_uri1, &uri); |
| 90 | |
| 91 | ok(size == 2 && |
| 92 | uri[0].dtype == LTTNG_DST_IPV4 && |
| 93 | uri[0].utype == LTTNG_URI_DST && |
| 94 | uri[0].stype == 0 && |
| 95 | uri[0].port == 8989 && |
| 96 | strlen(uri[0].subdir) == 0 && |
| 97 | strcmp(uri[0].dst.ipv4, "127.0.0.1") == 0 && |
| 98 | uri[1].dtype == LTTNG_DST_IPV4 && |
| 99 | uri[1].utype == LTTNG_URI_DST && |
| 100 | uri[1].stype == 0 && |
| 101 | uri[1].port == 4242 && |
| 102 | strlen(uri[1].subdir) == 0 && |
| 103 | strcmp(uri[1].dst.ipv4, "127.0.0.1") == 0, |
| 104 | "URI set to net://localhost:8989:4242"); |
| 105 | |
| 106 | if (uri) { |
| 107 | uri_free(uri); |
| 108 | uri = NULL; |
| 109 | } |
| 110 | |
| 111 | s_uri1 = "net6://[::1]:8989"; |
| 112 | |
| 113 | size = uri_parse(s_uri1, &uri); |
| 114 | |
| 115 | ok(size == 2 && |
| 116 | uri[0].dtype == LTTNG_DST_IPV6 && |
| 117 | uri[0].utype == LTTNG_URI_DST && |
| 118 | uri[0].stype == 0 && |
| 119 | uri[0].port == 8989 && |
| 120 | strlen(uri[0].subdir) == 0 && |
| 121 | strcmp(uri[0].dst.ipv6, "::1") == 0 && |
| 122 | uri[1].dtype == LTTNG_DST_IPV6 && |
| 123 | uri[1].utype == LTTNG_URI_DST && |
| 124 | uri[1].stype == 0 && |
| 125 | uri[1].port == 0 && |
| 126 | strlen(uri[1].subdir) == 0 && |
| 127 | strcmp(uri[1].dst.ipv6, "::1") == 0, |
| 128 | "URI set to net6://[::1]:8989"); |
| 129 | |
| 130 | if (uri) { |
| 131 | uri_free(uri); |
| 132 | uri = NULL; |
| 133 | } |
| 134 | |
| 135 | s_uri1 = "tcp://42.42.42.42/my/test/path"; |
| 136 | |
| 137 | size = uri_parse(s_uri1, &uri); |
| 138 | |
| 139 | ok(size == 1 && |
| 140 | uri[0].dtype == LTTNG_DST_IPV4 && |
| 141 | uri[0].utype == LTTNG_URI_DST && |
| 142 | uri[0].stype == 0 && |
| 143 | uri[0].port == 0 && |
| 144 | strcmp(uri[0].subdir, "my/test/path") == 0 && |
| 145 | strcmp(uri[0].dst.ipv4, "42.42.42.42") == 0, |
| 146 | "URI set to tcp://42.42.42.42/my/test/path"); |
| 147 | |
| 148 | if (uri) { |
| 149 | uri_free(uri); |
| 150 | uri = NULL; |
| 151 | } |
| 152 | |
| 153 | s_uri1 = "tcp6://[fe80::f66d:4ff:fe53:d220]/my/test/path"; |
| 154 | |
| 155 | size = uri_parse(s_uri1, &uri); |
| 156 | |
| 157 | ok(size == 1 && |
| 158 | uri[0].dtype == LTTNG_DST_IPV6 && |
| 159 | uri[0].utype == LTTNG_URI_DST && |
| 160 | uri[0].stype == 0 && |
| 161 | uri[0].port == 0 && |
| 162 | strcmp(uri[0].subdir, "my/test/path") == 0 && |
| 163 | strcmp(uri[0].dst.ipv6, "fe80::f66d:4ff:fe53:d220") == 0, |
| 164 | "URI set to tcp6://[fe80::f66d:4ff:fe53:d220]/my/test/path"); |
| 165 | |
| 166 | if (uri) { |
| 167 | uri_free(uri); |
| 168 | uri = NULL; |
| 169 | } |
| 170 | |
| 171 | s_uri1 = "file:///my/test/path"; |
| 172 | |
| 173 | size = uri_parse(s_uri1, &uri); |
| 174 | |
| 175 | ok(size == 1 && |
| 176 | uri[0].dtype == LTTNG_DST_PATH && |
| 177 | uri[0].utype == LTTNG_URI_DST && |
| 178 | uri[0].stype == 0 && |
| 179 | uri[0].port == 0 && |
| 180 | strlen(uri[0].subdir) == 0 && |
| 181 | strcmp(uri[0].dst.path, "/my/test/path") == 0, |
| 182 | "URI set to file:///my/test/path"); |
| 183 | |
| 184 | if (uri) { |
| 185 | uri_free(uri); |
| 186 | uri = NULL; |
| 187 | } |
| 188 | |
| 189 | /* FIXME: Noisy on stdout */ |
| 190 | s_uri1 = "file/my/test/path"; |
| 191 | size = uri_parse(s_uri1, &uri); |
| 192 | ok(size == -1, "Bad URI set to file/my/test/path"); |
| 193 | assert(!uri); |
| 194 | |
| 195 | s_uri1 = "net://:8999"; |
| 196 | size = uri_parse(s_uri1, &uri); |
| 197 | ok(size == -1, "Bad URI set to net://:8999"); |
| 198 | assert(!uri); |
| 199 | } |
| 200 | |
| 201 | void test_uri_cmp() |
| 202 | { |
| 203 | struct lttng_uri *uri1, *uri2; |
| 204 | const char *s_uri1 = "net://localhost"; |
| 205 | const char *s_uri2 = "net://localhost:8989:4242"; |
| 206 | ssize_t size1, size2; |
| 207 | int res; |
| 208 | |
| 209 | size1 = uri_parse(s_uri1, &uri1); |
| 210 | |
| 211 | /* Sanity checks */ |
| 212 | assert(size1 == 2); |
| 213 | assert(uri1[0].dtype == LTTNG_DST_IPV4); |
| 214 | assert(uri1[0].utype == LTTNG_URI_DST); |
| 215 | assert(uri1[0].stype == 0); |
| 216 | assert(uri1[0].port == 0); |
| 217 | assert(strlen(uri1[0].subdir) == 0); |
| 218 | assert(strcmp(uri1[0].dst.ipv4, "127.0.0.1") == 0); |
| 219 | assert(uri1[1].dtype == LTTNG_DST_IPV4); |
| 220 | assert(uri1[1].utype == LTTNG_URI_DST); |
| 221 | assert(uri1[1].stype == 0); |
| 222 | assert(uri1[1].port == 0); |
| 223 | assert(strlen(uri1[1].subdir) == 0); |
| 224 | assert(strcmp(uri1[1].dst.ipv4, "127.0.0.1") == 0); |
| 225 | |
| 226 | size2 = uri_parse(s_uri2, &uri2); |
| 227 | |
| 228 | assert(size2 == 2); |
| 229 | assert(uri2[0].dtype == LTTNG_DST_IPV4); |
| 230 | assert(uri2[0].utype == LTTNG_URI_DST); |
| 231 | assert(uri2[0].stype == 0); |
| 232 | assert(uri2[0].port == 8989); |
| 233 | assert(strlen(uri2[0].subdir) == 0); |
| 234 | assert(strcmp(uri2[0].dst.ipv4, "127.0.0.1") == 0); |
| 235 | assert(uri2[1].dtype == LTTNG_DST_IPV4); |
| 236 | assert(uri2[1].utype == LTTNG_URI_DST); |
| 237 | assert(uri2[1].stype == 0); |
| 238 | assert(uri2[1].port == 4242); |
| 239 | assert(strlen(uri2[1].subdir) == 0); |
| 240 | assert(strcmp(uri2[1].dst.ipv4, "127.0.0.1") == 0); |
| 241 | |
| 242 | res = uri_compare(uri1, uri1); |
| 243 | |
| 244 | ok(res == 0, |
| 245 | "URI compare net://localhost == net://localhost"); |
| 246 | |
| 247 | res = uri_compare(uri1, uri2); |
| 248 | |
| 249 | ok(res != 0, |
| 250 | "URI compare net://localhost != net://localhost:8989:4242"); |
| 251 | |
| 252 | uri_free(uri1); |
| 253 | uri_free(uri2); |
| 254 | } |
| 255 | |
| 256 | int main(int argc, char **argv) |
| 257 | { |
| 258 | plan_tests(NUM_TESTS); |
| 259 | |
| 260 | diag("URI unit tests"); |
| 261 | |
| 262 | test_uri_parsing(); |
| 263 | |
| 264 | test_uri_cmp(); |
| 265 | |
| 266 | return exit_status(); |
| 267 | } |