| 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 | |
| 23 | #include <tap/tap.h> |
| 24 | |
| 25 | #include <src/common/utils.h> |
| 26 | |
| 27 | /* For lttngerr.h */ |
| 28 | int lttng_opt_quiet = 1; |
| 29 | int lttng_opt_verbose = 3; |
| 30 | |
| 31 | struct valid_test_input { |
| 32 | char *input; |
| 33 | char *expected_result; |
| 34 | }; |
| 35 | |
| 36 | /* Valid test cases */ |
| 37 | static struct valid_test_input valid_tests_inputs[] = { |
| 38 | { "/a/b/c/d/./e", "/a/b/c/d/e" }, |
| 39 | { "/a/b/c/d/../e", "/a/b/c/e" }, |
| 40 | { "/a/b/../c/d/../e", "/a/c/e" }, |
| 41 | { "/a/b/../../c/./d/./e", "/c/d/e" }, |
| 42 | { "/a/b/../../c/d/../../e", "/e" }, |
| 43 | { "/a/b/c/d/../../../../e", "/e" }, |
| 44 | { "/./a/b/c/d/./e", "/a/b/c/d/e" }, |
| 45 | { "/", "/" }, |
| 46 | { "", "" }, |
| 47 | }; |
| 48 | static const int num_valid_tests = |
| 49 | sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]); |
| 50 | |
| 51 | /* Invalid test cases */ |
| 52 | static char *invalid_tests_inputs[] = { |
| 53 | NULL, |
| 54 | "/../a/b/c/d/e", |
| 55 | "/a/b/c/d/../../../../../e", |
| 56 | }; |
| 57 | static const int num_invalid_tests = |
| 58 | sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]); |
| 59 | |
| 60 | static void test_utils_resolve_relative(void) |
| 61 | { |
| 62 | char *result; |
| 63 | int i; |
| 64 | |
| 65 | /* Test valid cases */ |
| 66 | for (i = 0; i < num_valid_tests; i++) { |
| 67 | char name[100]; |
| 68 | sprintf(name, "valid test case: %s", valid_tests_inputs[i].input); |
| 69 | |
| 70 | result = utils_resolve_relative(valid_tests_inputs[i].input); |
| 71 | ok(strcmp(result, valid_tests_inputs[i].expected_result) == 0, name); |
| 72 | |
| 73 | free(result); |
| 74 | } |
| 75 | |
| 76 | /* Test invalid cases */ |
| 77 | for (i = 0; i < num_invalid_tests; i++) { |
| 78 | char name[100]; |
| 79 | sprintf(name, "invalid test case: %s", invalid_tests_inputs[i]); |
| 80 | |
| 81 | result = utils_resolve_relative(invalid_tests_inputs[i]); |
| 82 | if (result != NULL) { |
| 83 | free(result); |
| 84 | } |
| 85 | ok(result == NULL, name); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | int main(int argc, char **argv) |
| 90 | { |
| 91 | plan_tests(num_valid_tests + num_invalid_tests); |
| 92 | |
| 93 | diag("utils_resolve_relative tests"); |
| 94 | |
| 95 | test_utils_resolve_relative(); |
| 96 | |
| 97 | return exit_status(); |
| 98 | } |