| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <check.h> |
| 36 |
#include <global.h> |
| 37 |
|
| 38 |
#include "path.h" |
| 39 |
|
| 40 |
static void setup(void) { |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
static void teardown(void) { |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
static void check_combine(const char *src, const char *dst, const char *exp) { |
| 49 |
char res[HUGE_BUF]; |
| 50 |
|
| 51 |
path_combine(src, dst, res, HUGE_BUF); |
| 52 |
fail_unless(strcmp(res, exp) == 0, "path_combine(%s, %s) = %s but should be %s", src, dst, res, exp); |
| 53 |
} |
| 54 |
|
| 55 |
static void check_normalize(const char *path, const char *exp) { |
| 56 |
char tmp[HUGE_BUF]; |
| 57 |
|
| 58 |
|
| 59 |
strncpy(tmp, path, sizeof(tmp)); |
| 60 |
tmp[HUGE_BUF-1] = '\0'; |
| 61 |
path_normalize(tmp); |
| 62 |
fail_unless(strcmp(tmp, exp) == 0, "path_normalize(%s) = %s but should be %s", path, tmp, exp); |
| 63 |
} |
| 64 |
|
| 65 |
static void check_combine_and_normalize(const char *src, const char *dst, const char *exp) { |
| 66 |
char res[HUGE_BUF]; |
| 67 |
|
| 68 |
path_combine_and_normalize(src, dst, res, sizeof(res)); |
| 69 |
fail_unless(strcmp(res, exp) == 0, "path_combine_and_normalize(%s, %s) = %s but should be %s", src, dst, res, exp); |
| 70 |
} |
| 71 |
|
| 72 |
START_TEST(test_path_combine) { |
| 73 |
check_combine("/path1/file1", "/path2/file2", "/path2/file2"); |
| 74 |
check_combine("path1/file1", "/path2/file2", "/path2/file2"); |
| 75 |
check_combine("/path1/file1", "path2/file2", "/path1/path2/file2"); |
| 76 |
check_combine("path1/file1", "path2/file2", "path1/path2/file2"); |
| 77 |
check_combine("/path1", "/path2", "/path2"); |
| 78 |
check_combine("path1", "/path2", "/path2"); |
| 79 |
check_combine("/path1", "path2", "/path2"); |
| 80 |
check_combine("path1", "path2", "path2"); |
| 81 |
} |
| 82 |
END_TEST |
| 83 |
|
| 84 |
START_TEST(test_path_normalize) { |
| 85 |
check_normalize("", ""); |
| 86 |
check_normalize("/", "/"); |
| 87 |
check_normalize("path1/file1", "path1/file1"); |
| 88 |
check_normalize("/path1/file1", "/path1/file1"); |
| 89 |
check_normalize("/path1//file1", "/path1/file1"); |
| 90 |
check_normalize("//path1/file1", "/path1/file1"); |
| 91 |
check_normalize("///////x////////y///////z////////", "/x/y/z"); |
| 92 |
check_normalize("/a/b/../c/d/../e/../../f/g/../h", "/a/f/h"); |
| 93 |
check_normalize("//a//b//..//c//d//..//e//..//..//f//g//..//h", "/a/f/h"); |
| 94 |
check_normalize("../a", "../a"); |
| 95 |
check_normalize("a/../../b", "../b"); |
| 96 |
check_normalize("/../a", "/a"); |
| 97 |
check_normalize("/a/../../b", "/b"); |
| 98 |
check_normalize("./b/./c/.d/..e/./f", "b/c/.d/..e/f"); |
| 99 |
check_normalize("/b/././././e", "/b/e"); |
| 100 |
check_normalize(".", ""); |
| 101 |
check_normalize("/.", "/"); |
| 102 |
check_normalize("./", ""); |
| 103 |
check_normalize("/a/b/..", "/a"); |
| 104 |
check_normalize("/a/b/../..", "/"); |
| 105 |
check_normalize("/a/b/../../..", "/"); |
| 106 |
check_normalize("a/b/..", "a"); |
| 107 |
check_normalize("a/b/../..", ""); |
| 108 |
check_normalize("a/b/../../..", ".."); |
| 109 |
} |
| 110 |
END_TEST |
| 111 |
|
| 112 |
START_TEST(test_path_combine_and_normalize) { |
| 113 |
check_combine_and_normalize("/path1/file1", "/path2/file2", "/path2/file2"); |
| 114 |
check_combine_and_normalize("path1/file1", "/path2/file2", "/path2/file2"); |
| 115 |
check_combine_and_normalize("/path1/file1", "path2/file2", "/path1/path2/file2"); |
| 116 |
check_combine_and_normalize("/path1", "/path2", "/path2"); |
| 117 |
check_combine_and_normalize("path1", "/path2", "/path2"); |
| 118 |
check_combine_and_normalize("/path1", "path2", "/path2"); |
| 119 |
check_combine_and_normalize("/path1/file1/../u", "path2/x/../y/z/../a/b/..", "/path1/path2/y/a"); |
| 120 |
check_combine_and_normalize("/path1/file1", "/path2//file2", "/path2/file2"); |
| 121 |
check_combine_and_normalize("/path1/file1", "/..", "/"); |
| 122 |
check_combine_and_normalize("/path1/file1", "../x", "/x"); |
| 123 |
check_combine_and_normalize("/path1/file1", "../../../x", "/x"); |
| 124 |
check_combine_and_normalize("/path1/file1", "/.x/..x/...x/x", "/.x/..x/...x/x"); |
| 125 |
} |
| 126 |
END_TEST |
| 127 |
|
| 128 |
static Suite *path_suite(void) { |
| 129 |
Suite *s = suite_create("path"); |
| 130 |
TCase *tc_core = tcase_create("Core"); |
| 131 |
|
| 132 |
|
| 133 |
tcase_add_checked_fixture(tc_core, setup, teardown); |
| 134 |
|
| 135 |
suite_add_tcase(s, tc_core); |
| 136 |
tcase_add_test(tc_core, test_path_combine); |
| 137 |
tcase_add_test(tc_core, test_path_normalize); |
| 138 |
tcase_add_test(tc_core, test_path_combine_and_normalize); |
| 139 |
|
| 140 |
return s; |
| 141 |
} |
| 142 |
|
| 143 |
int main(void) { |
| 144 |
int nf; |
| 145 |
Suite *s = path_suite(); |
| 146 |
SRunner *sr = srunner_create(s); |
| 147 |
|
| 148 |
srunner_set_xml(sr, LOGDIR "/unit/common/path.xml"); |
| 149 |
srunner_set_log(sr, LOGDIR "/unit/common/path.out"); |
| 150 |
srunner_run_all(sr, CK_ENV); |
| 151 |
nf = srunner_ntests_failed(sr); |
| 152 |
srunner_free(sr); |
| 153 |
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 154 |
} |