| 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 | #define _GNU_SOURCE |
| 19 | #include <assert.h> |
| 20 | #include <errno.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | #include <time.h> |
| 26 | #include <sys/types.h> |
| 27 | |
| 28 | #include <common/sessiond-comm/sessiond-comm.h> |
| 29 | #include <common/uri.h> |
| 30 | |
| 31 | #include "utils.h" |
| 32 | |
| 33 | /* This path will NEVER be created in this test */ |
| 34 | #define PATH1 "tmp/.test-junk-lttng" |
| 35 | |
| 36 | #define RANDOM_STRING_LEN 16 |
| 37 | |
| 38 | /* For lttngerr.h */ |
| 39 | int lttng_opt_quiet = 1; |
| 40 | int lttng_opt_verbose = 3; |
| 41 | |
| 42 | /* |
| 43 | * Test string URI and if uri_parse works well. |
| 44 | */ |
| 45 | void test_uri_parsing(void) |
| 46 | { |
| 47 | ssize_t size; |
| 48 | const char *s_uri1; |
| 49 | struct lttng_uri *uri; |
| 50 | |
| 51 | fprintf(stdout, "Testing URIs...\n"); |
| 52 | |
| 53 | s_uri1 = "net://localhost"; |
| 54 | fprintf(stdout, " [+] URI set to %s ", s_uri1); |
| 55 | size = uri_parse(s_uri1, &uri); |
| 56 | assert(size == 2); |
| 57 | assert(uri[0].dtype == LTTNG_DST_IPV4); |
| 58 | assert(uri[0].utype == LTTNG_URI_DST); |
| 59 | assert(uri[0].stype == 0); |
| 60 | assert(uri[0].port == 0); |
| 61 | assert(strlen(uri[0].subdir) == 0); |
| 62 | assert(strcmp(uri[0].dst.ipv4, "127.0.0.1") == 0); |
| 63 | |
| 64 | assert(uri[1].dtype == LTTNG_DST_IPV4); |
| 65 | assert(uri[1].utype == LTTNG_URI_DST); |
| 66 | assert(uri[1].stype == 0); |
| 67 | assert(uri[1].port == 0); |
| 68 | assert(strlen(uri[1].subdir) == 0); |
| 69 | assert(strcmp(uri[1].dst.ipv4, "127.0.0.1") == 0); |
| 70 | PRINT_OK(); |
| 71 | uri_free(uri); |
| 72 | |
| 73 | s_uri1 = "net://localhost:8989:4242/my/test/path"; |
| 74 | fprintf(stdout, " [+] URI set to %s ", s_uri1); |
| 75 | size = uri_parse(s_uri1, &uri); |
| 76 | assert(size == 2); |
| 77 | assert(uri[0].dtype == LTTNG_DST_IPV4); |
| 78 | assert(uri[0].utype == LTTNG_URI_DST); |
| 79 | assert(uri[0].stype == 0); |
| 80 | assert(uri[0].port == 8989); |
| 81 | assert(strcmp(uri[0].subdir, "my/test/path") == 0); |
| 82 | assert(strcmp(uri[0].dst.ipv4, "127.0.0.1") == 0); |
| 83 | |
| 84 | assert(uri[1].dtype == LTTNG_DST_IPV4); |
| 85 | assert(uri[1].utype == LTTNG_URI_DST); |
| 86 | assert(uri[1].stype == 0); |
| 87 | assert(uri[1].port == 4242); |
| 88 | assert(strcmp(uri[0].subdir, "my/test/path") == 0); |
| 89 | assert(strcmp(uri[1].dst.ipv4, "127.0.0.1") == 0); |
| 90 | PRINT_OK(); |
| 91 | uri_free(uri); |
| 92 | |
| 93 | s_uri1 = "net://localhost:8989:4242"; |
| 94 | fprintf(stdout, " [+] URI set to %s ", s_uri1); |
| 95 | size = uri_parse(s_uri1, &uri); |
| 96 | assert(size == 2); |
| 97 | assert(uri[0].dtype == LTTNG_DST_IPV4); |
| 98 | assert(uri[0].utype == LTTNG_URI_DST); |
| 99 | assert(uri[0].stype == 0); |
| 100 | assert(uri[0].port == 8989); |
| 101 | assert(strlen(uri[1].subdir) == 0); |
| 102 | assert(strcmp(uri[0].dst.ipv4, "127.0.0.1") == 0); |
| 103 | |
| 104 | assert(uri[1].dtype == LTTNG_DST_IPV4); |
| 105 | assert(uri[1].utype == LTTNG_URI_DST); |
| 106 | assert(uri[1].stype == 0); |
| 107 | assert(uri[1].port == 4242); |
| 108 | assert(strlen(uri[1].subdir) == 0); |
| 109 | assert(strcmp(uri[1].dst.ipv4, "127.0.0.1") == 0); |
| 110 | PRINT_OK(); |
| 111 | uri_free(uri); |
| 112 | |
| 113 | s_uri1 = "net6://localhost:8989"; |
| 114 | fprintf(stdout, " [+] URI set to %s ", s_uri1); |
| 115 | size = uri_parse(s_uri1, &uri); |
| 116 | assert(size == 2); |
| 117 | assert(uri[0].dtype == LTTNG_DST_IPV6); |
| 118 | assert(uri[0].utype == LTTNG_URI_DST); |
| 119 | assert(uri[0].stype == 0); |
| 120 | assert(uri[0].port == 8989); |
| 121 | assert(strlen(uri[1].subdir) == 0); |
| 122 | assert(strcmp(uri[0].dst.ipv6, "::1") == 0); |
| 123 | |
| 124 | assert(uri[1].dtype == LTTNG_DST_IPV6); |
| 125 | assert(uri[1].utype == LTTNG_URI_DST); |
| 126 | assert(uri[1].stype == 0); |
| 127 | assert(uri[1].port == 0); |
| 128 | assert(strlen(uri[1].subdir) == 0); |
| 129 | assert(strcmp(uri[0].dst.ipv6, "::1") == 0); |
| 130 | PRINT_OK(); |
| 131 | uri_free(uri); |
| 132 | |
| 133 | s_uri1 = "tcp://42.42.42.42/my/test/path"; |
| 134 | fprintf(stdout, " [+] URI set to %s ", s_uri1); |
| 135 | size = uri_parse(s_uri1, &uri); |
| 136 | assert(size == 1); |
| 137 | assert(uri[0].dtype == LTTNG_DST_IPV4); |
| 138 | assert(uri[0].utype == LTTNG_URI_DST); |
| 139 | assert(uri[0].stype == 0); |
| 140 | assert(uri[0].port == 0); |
| 141 | assert(strcmp(uri[0].subdir, "my/test/path") == 0); |
| 142 | assert(strcmp(uri[0].dst.ipv4, "42.42.42.42") == 0); |
| 143 | PRINT_OK(); |
| 144 | uri_free(uri); |
| 145 | |
| 146 | s_uri1 = "tcp6://[fe80::f66d:4ff:fe53:d220]/my/test/path"; |
| 147 | fprintf(stdout, " [+] URI set to %s ", s_uri1); |
| 148 | size = uri_parse(s_uri1, &uri); |
| 149 | assert(size == 1); |
| 150 | assert(uri[0].dtype == LTTNG_DST_IPV6); |
| 151 | assert(uri[0].utype == LTTNG_URI_DST); |
| 152 | assert(uri[0].stype == 0); |
| 153 | assert(uri[0].port == 0); |
| 154 | assert(strcmp(uri[0].subdir, "my/test/path") == 0); |
| 155 | assert(strcmp(uri[0].dst.ipv6, "fe80::f66d:4ff:fe53:d220") == 0); |
| 156 | PRINT_OK(); |
| 157 | uri_free(uri); |
| 158 | |
| 159 | s_uri1 = "file:///my/test/path"; |
| 160 | fprintf(stdout, " [+] URI set to %s ", s_uri1); |
| 161 | size = uri_parse(s_uri1, &uri); |
| 162 | assert(size == 1); |
| 163 | assert(uri[0].dtype == LTTNG_DST_PATH); |
| 164 | assert(uri[0].utype == LTTNG_URI_DST); |
| 165 | assert(uri[0].stype == 0); |
| 166 | assert(uri[0].port == 0); |
| 167 | assert(strlen(uri[0].subdir) == 0); |
| 168 | assert(strcmp(uri[0].dst.path, "/my/test/path") == 0); |
| 169 | PRINT_OK(); |
| 170 | uri_free(uri); |
| 171 | |
| 172 | s_uri1 = "file/my/test/path"; |
| 173 | fprintf(stdout, " [+] Bad URI set to %s ", s_uri1); |
| 174 | size = uri_parse(s_uri1, &uri); |
| 175 | assert(size == -1); |
| 176 | PRINT_OK(); |
| 177 | |
| 178 | s_uri1 = "net://:8999"; |
| 179 | fprintf(stdout, " [+] Bad URI set to %s ", s_uri1); |
| 180 | size = uri_parse(s_uri1, &uri); |
| 181 | assert(size == -1); |
| 182 | PRINT_OK(); |
| 183 | } |
| 184 | |
| 185 | void test_uri_cmp() |
| 186 | { |
| 187 | struct lttng_uri *uri1, *uri2; |
| 188 | const char *s_uri1 = "net://localhost"; |
| 189 | const char *s_uri2 = "net://localhost:8989:4242"; |
| 190 | ssize_t size1, size2; |
| 191 | int res; |
| 192 | |
| 193 | size1 = uri_parse(s_uri1, &uri1); |
| 194 | |
| 195 | /* Sanity checks */ |
| 196 | assert(size1 == 2); |
| 197 | assert(uri1[0].dtype == LTTNG_DST_IPV4); |
| 198 | assert(uri1[0].utype == LTTNG_URI_DST); |
| 199 | assert(uri1[0].stype == 0); |
| 200 | assert(uri1[0].port == 0); |
| 201 | assert(strlen(uri1[0].subdir) == 0); |
| 202 | assert(strcmp(uri1[0].dst.ipv4, "127.0.0.1") == 0); |
| 203 | assert(uri1[1].dtype == LTTNG_DST_IPV4); |
| 204 | assert(uri1[1].utype == LTTNG_URI_DST); |
| 205 | assert(uri1[1].stype == 0); |
| 206 | assert(uri1[1].port == 0); |
| 207 | assert(strlen(uri1[1].subdir) == 0); |
| 208 | assert(strcmp(uri1[1].dst.ipv4, "127.0.0.1") == 0); |
| 209 | |
| 210 | size2 = uri_parse(s_uri2, &uri2); |
| 211 | |
| 212 | assert(size2 == 2); |
| 213 | assert(uri2[0].dtype == LTTNG_DST_IPV4); |
| 214 | assert(uri2[0].utype == LTTNG_URI_DST); |
| 215 | assert(uri2[0].stype == 0); |
| 216 | assert(uri2[0].port == 8989); |
| 217 | assert(strlen(uri2[1].subdir) == 0); |
| 218 | assert(strcmp(uri2[0].dst.ipv4, "127.0.0.1") == 0); |
| 219 | assert(uri2[1].dtype == LTTNG_DST_IPV4); |
| 220 | assert(uri2[1].utype == LTTNG_URI_DST); |
| 221 | assert(uri2[1].stype == 0); |
| 222 | assert(uri2[1].port == 4242); |
| 223 | assert(strlen(uri2[1].subdir) == 0); |
| 224 | assert(strcmp(uri2[1].dst.ipv4, "127.0.0.1") == 0); |
| 225 | |
| 226 | |
| 227 | res = uri_compare(uri1, uri1); |
| 228 | fprintf(stdout, " [+] %s == %s ", s_uri1, s_uri1); |
| 229 | assert(res == 0); |
| 230 | PRINT_OK(); |
| 231 | |
| 232 | res = uri_compare(uri1, uri2); |
| 233 | fprintf(stdout, " [+] %s != %s ", s_uri1, s_uri2); |
| 234 | assert(res != 0); |
| 235 | PRINT_OK(); |
| 236 | |
| 237 | uri_free(uri1); |
| 238 | uri_free(uri2); |
| 239 | } |
| 240 | |
| 241 | int main(int argc, char **argv) |
| 242 | { |
| 243 | srand(time(NULL)); |
| 244 | |
| 245 | printf("\nStreaming unit tests\n-----------\n"); |
| 246 | |
| 247 | /* URI tests */ |
| 248 | test_uri_parsing(); |
| 249 | test_uri_cmp(); |
| 250 | |
| 251 | return 0; |
| 252 | } |