| 1 | /* |
| 2 | * Copyright (C) - 2013 Raphaƫl Beamonte <raphael.beamonte@gmail.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 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <limits.h> |
| 23 | |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #include <tap/tap.h> |
| 28 | |
| 29 | #include <common/utils.h> |
| 30 | #include <common/common.h> |
| 31 | |
| 32 | /* For error.h */ |
| 33 | int lttng_opt_quiet = 1; |
| 34 | int lttng_opt_verbose = 3; |
| 35 | int lttng_opt_mi; |
| 36 | |
| 37 | struct valid_test_input { |
| 38 | char *input; |
| 39 | char *relative_part; |
| 40 | char *absolute_part; |
| 41 | }; |
| 42 | |
| 43 | struct tree_symlink { |
| 44 | char *orig; |
| 45 | char *dest; |
| 46 | }; |
| 47 | |
| 48 | struct symlink_test_input { |
| 49 | char *input; |
| 50 | char *expected_result; |
| 51 | }; |
| 52 | |
| 53 | /* Valid test cases */ |
| 54 | static struct valid_test_input valid_tests_inputs[] = { |
| 55 | { "/a/b/c/d/e", "", "/a/b/c/d/e" }, |
| 56 | { "./a/b/c/d/e", ".", "/a/b/c/d/e" }, |
| 57 | { "../a/b/c/d/../e", "..", "/a/b/c/e" }, |
| 58 | { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" }, |
| 59 | { "../../a/b/c/d/e", "../..", "/a/b/c/d/e" }, |
| 60 | { "./a/b/../c/d/../e", ".", "/a/c/e" }, |
| 61 | { "../a/b/../../c/./d/./e", "..", "/c/d/e" }, |
| 62 | { "../../a/b/../c/d/../../e", "../..", "/a/e" }, |
| 63 | { "./a/b/c/d/../../../../e", ".", "/e" }, |
| 64 | { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" }, |
| 65 | { "a/", ".", "/a/" }, |
| 66 | { "a", ".", "/a" }, |
| 67 | { "../../", "../..", "/" }, |
| 68 | { "../..", "../..", "" }, |
| 69 | { "../", "..", "/" }, |
| 70 | { "..", "..", "" }, |
| 71 | { "./", ".", "/" }, |
| 72 | { ".", ".", "" }, |
| 73 | { "/../a/b/c/d/e", "", "/a/b/c/d/e" }, |
| 74 | { "/a/b/c/d/../../../../../e", "", "/e" }, |
| 75 | { "/..", "", "/" }, |
| 76 | { "/a/..", "", "/" }, |
| 77 | }; |
| 78 | char **valid_tests_expected_results; |
| 79 | static const int num_valid_tests = |
| 80 | sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]); |
| 81 | |
| 82 | /* Symlinks test cases */ |
| 83 | char tree_origin[] = "/tmp/test_utils_expand_path.XXXXXX"; |
| 84 | |
| 85 | static const char * const tree_dirs[] = { |
| 86 | "a", |
| 87 | "a/b", |
| 88 | "a/b/c", |
| 89 | "a/e", |
| 90 | }; |
| 91 | static const int num_tree_dirs = |
| 92 | sizeof(tree_dirs) / sizeof(tree_dirs[0]); |
| 93 | |
| 94 | static struct tree_symlink tree_symlinks[] = { |
| 95 | { "a/d", "b/c/" }, |
| 96 | { "a/g", "d/" }, |
| 97 | { "a/b/f", "../e/" }, |
| 98 | { "a/b/h", "../g/" }, |
| 99 | { "a/b/k", "c/g/" }, |
| 100 | { "a/b/c/g", "../../../" }, |
| 101 | }; |
| 102 | static const int num_tree_symlinks = |
| 103 | sizeof(tree_symlinks) / sizeof(tree_symlinks[0]); |
| 104 | |
| 105 | static struct symlink_test_input symlink_tests_inputs[] = { |
| 106 | { "a/g/../l/.", "a/b/l" }, |
| 107 | { "a/g/../l/./", "a/b/l/" }, |
| 108 | { "a/g/../l/..", "a/b" }, |
| 109 | { "a/g/../l/../", "a/b/" }, |
| 110 | { "a/b/h/g/", "" }, |
| 111 | }; |
| 112 | static const int num_symlink_tests = |
| 113 | sizeof(symlink_tests_inputs) / sizeof(symlink_tests_inputs[0]); |
| 114 | |
| 115 | /* Invalid test cases */ |
| 116 | static char *invalid_tests_inputs[] = { |
| 117 | NULL, |
| 118 | }; |
| 119 | static const int num_invalid_tests = |
| 120 | sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]); |
| 121 | |
| 122 | #define ERRSIZE 100 |
| 123 | char errmsg[ERRSIZE]; |
| 124 | static void printerr(char *msg) |
| 125 | { |
| 126 | fprintf(stderr, "test_utils_expand_path: error: %s\n", msg); |
| 127 | } |
| 128 | |
| 129 | int prepare_valid_results(void) |
| 130 | { |
| 131 | int i; |
| 132 | char *relative, *cur_path = NULL, *prev_path = NULL, |
| 133 | *pprev_path = NULL, *empty = NULL; |
| 134 | int ret = 0; |
| 135 | |
| 136 | /* Prepare the relative paths */ |
| 137 | cur_path = realpath(".", NULL); |
| 138 | prev_path = realpath("..", NULL); |
| 139 | pprev_path = realpath("../..", NULL); |
| 140 | empty = strdup(""); |
| 141 | if (!cur_path || !prev_path || !pprev_path || !empty) { |
| 142 | printerr("strdup out of memory"); |
| 143 | ret = -1; |
| 144 | goto end; |
| 145 | } |
| 146 | |
| 147 | /* allocate memory for the expected results */ |
| 148 | valid_tests_expected_results = zmalloc(sizeof(char *) * num_valid_tests); |
| 149 | if (!valid_tests_expected_results) { |
| 150 | printerr("out of memory"); |
| 151 | ret = -1; |
| 152 | goto end; |
| 153 | } |
| 154 | for (i = 0; i < num_valid_tests; i++) { |
| 155 | valid_tests_expected_results[i] = malloc(PATH_MAX); |
| 156 | if (valid_tests_expected_results[i] == NULL) { |
| 157 | printerr("malloc expected results"); |
| 158 | ret = -1; |
| 159 | goto end; |
| 160 | } |
| 161 | |
| 162 | if (strcmp(valid_tests_inputs[i].relative_part, ".") == 0) { |
| 163 | relative = cur_path; |
| 164 | } else if (strcmp(valid_tests_inputs[i].relative_part, "..") == 0) { |
| 165 | relative = prev_path; |
| 166 | } else if (strcmp(valid_tests_inputs[i].relative_part, "../..") == 0) { |
| 167 | relative = pprev_path; |
| 168 | } else { |
| 169 | relative = empty; |
| 170 | } |
| 171 | |
| 172 | snprintf(valid_tests_expected_results[i], PATH_MAX, |
| 173 | "%s%s", relative, valid_tests_inputs[i].absolute_part); |
| 174 | } |
| 175 | |
| 176 | end: |
| 177 | free(cur_path); |
| 178 | free(prev_path); |
| 179 | free(pprev_path); |
| 180 | free(empty); |
| 181 | |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | int free_valid_results(void) |
| 186 | { |
| 187 | int i; |
| 188 | |
| 189 | for (i = 0; i < num_valid_tests; i++) { |
| 190 | free(valid_tests_expected_results[i]); |
| 191 | } |
| 192 | |
| 193 | free(valid_tests_expected_results); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | int prepare_symlink_tree(void) |
| 199 | { |
| 200 | int i; |
| 201 | char tmppath[PATH_MAX]; |
| 202 | |
| 203 | /* Create the temporary directory */ |
| 204 | if (mkdtemp(tree_origin) == NULL) { |
| 205 | printerr("mkdtemp"); |
| 206 | goto error; |
| 207 | } |
| 208 | |
| 209 | /* Create the directories of the test tree */ |
| 210 | for (i = 0; i < num_tree_dirs; i++) { |
| 211 | snprintf(tmppath, PATH_MAX, "%s/%s", tree_origin, tree_dirs[i]); |
| 212 | |
| 213 | if (mkdir(tmppath, 0755) != 0) { |
| 214 | snprintf(errmsg, ERRSIZE, "mkdir %s", tmppath); |
| 215 | printerr(errmsg); |
| 216 | goto error; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /* Create the symlinks of the test tree */ |
| 221 | for (i = 0; i < num_tree_symlinks; i++) { |
| 222 | snprintf(tmppath, PATH_MAX, "%s/%s", |
| 223 | tree_origin, tree_symlinks[i].orig); |
| 224 | |
| 225 | if (symlink(tree_symlinks[i].dest, tmppath) != 0) { |
| 226 | snprintf(errmsg, ERRSIZE, "symlink %s to %s", |
| 227 | tmppath, tree_symlinks[i].dest); |
| 228 | printerr(errmsg); |
| 229 | goto error; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | return 0; |
| 234 | |
| 235 | error: |
| 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | int free_symlink_tree(void) |
| 240 | { |
| 241 | int i; |
| 242 | char tmppath[PATH_MAX]; |
| 243 | |
| 244 | /* Remove the symlinks from the test tree */ |
| 245 | for (i = num_tree_symlinks - 1; i > -1; i--) { |
| 246 | snprintf(tmppath, PATH_MAX, "%s/%s", |
| 247 | tree_origin, tree_symlinks[i].orig); |
| 248 | |
| 249 | if (unlink(tmppath) != 0) { |
| 250 | snprintf(errmsg, ERRSIZE, "unlink %s", tmppath); |
| 251 | printerr(errmsg); |
| 252 | goto error; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /* Remove the directories from the test tree */ |
| 257 | for (i = num_tree_dirs - 1; i > -1; i--) { |
| 258 | snprintf(tmppath, PATH_MAX, "%s/%s", tree_origin, tree_dirs[i]); |
| 259 | |
| 260 | if (rmdir(tmppath) != 0) { |
| 261 | snprintf(errmsg, ERRSIZE, "rmdir %s", tmppath); |
| 262 | printerr(errmsg); |
| 263 | goto error; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /* Remove the temporary directory */ |
| 268 | if (rmdir(tree_origin) != 0) { |
| 269 | snprintf(errmsg, ERRSIZE, "rmdir %s", tree_origin); |
| 270 | printerr(errmsg); |
| 271 | goto error; |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | |
| 276 | error: |
| 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | static void test_utils_expand_path(void) |
| 281 | { |
| 282 | char *result; |
| 283 | char name[100], tmppath[PATH_MAX]; |
| 284 | int i; |
| 285 | |
| 286 | /* Test valid cases */ |
| 287 | for (i = 0; i < num_valid_tests; i++) { |
| 288 | sprintf(name, "valid test case: %s", valid_tests_inputs[i].input); |
| 289 | |
| 290 | result = utils_expand_path(valid_tests_inputs[i].input); |
| 291 | ok(result != NULL && |
| 292 | strcmp(result, valid_tests_expected_results[i]) == 0, name); |
| 293 | |
| 294 | free(result); |
| 295 | } |
| 296 | |
| 297 | /* Test symlink tree cases */ |
| 298 | int treelen = strlen(tree_origin) + 1; |
| 299 | for (i = 0; i < num_symlink_tests; i++) { |
| 300 | sprintf(name, "symlink tree test case: [tmppath/]%s", |
| 301 | symlink_tests_inputs[i].input); |
| 302 | |
| 303 | snprintf(tmppath, PATH_MAX, "%s/%s", |
| 304 | tree_origin, symlink_tests_inputs[i].input); |
| 305 | result = utils_expand_path(tmppath); |
| 306 | ok(result != NULL && strcmp(result + treelen, |
| 307 | symlink_tests_inputs[i].expected_result) == 0, name); |
| 308 | |
| 309 | free(result); |
| 310 | } |
| 311 | |
| 312 | /* Test invalid cases */ |
| 313 | for (i = 0; i < num_invalid_tests; i++) { |
| 314 | sprintf(name, "invalid test case: %s", invalid_tests_inputs[i]); |
| 315 | |
| 316 | result = utils_expand_path(invalid_tests_inputs[i]); |
| 317 | if (result != NULL) { |
| 318 | free(result); |
| 319 | } |
| 320 | ok(result == NULL, name); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | int main(int argc, char **argv) |
| 325 | { |
| 326 | if (prepare_symlink_tree() != 0) { |
| 327 | goto error_mkdir; |
| 328 | } |
| 329 | |
| 330 | if (prepare_valid_results() != 0) { |
| 331 | goto error_malloc; |
| 332 | } |
| 333 | |
| 334 | plan_tests(num_valid_tests + num_invalid_tests + num_symlink_tests); |
| 335 | |
| 336 | diag("utils_expand_path tests"); |
| 337 | |
| 338 | test_utils_expand_path(); |
| 339 | |
| 340 | free_valid_results(); |
| 341 | free_symlink_tree(); |
| 342 | |
| 343 | return exit_status(); |
| 344 | |
| 345 | error_malloc: |
| 346 | free_valid_results(); |
| 347 | |
| 348 | error_mkdir: |
| 349 | free_symlink_tree(); |
| 350 | |
| 351 | return 1; |
| 352 | } |