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