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