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