00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <check.h>
00036 #include <global.h>
00037
00038 #include "path.h"
00039
00040 void setup(void) {
00041
00042 }
00043
00044 void teardown(void) {
00045
00046 }
00047
00048 static void check_combine(const char *src, const char *dst, const char *exp) {
00049 char res[HUGE_BUF];
00050
00051 path_combine(src, dst, res, HUGE_BUF);
00052 fail_unless(strcmp(res, exp) == 0, "path_combine(%s, %s) = %s but should be %s", src, dst, res, exp);
00053 }
00054
00055 static void check_normalize(const char *path, const char *exp) {
00056 char tmp[HUGE_BUF];
00057
00058
00059 strncpy(tmp, path, sizeof(tmp));
00060 tmp[HUGE_BUF-1] = '\0';
00061 path_normalize(tmp);
00062 fail_unless(strcmp(tmp, exp) == 0, "path_normalize(%s) = %s but should be %s", path, tmp, exp);
00063 }
00064
00065 static void check_combine_and_normalize(const char *src, const char *dst, const char *exp) {
00066 char res[HUGE_BUF];
00067
00068 path_combine_and_normalize(src, dst, res, sizeof(res));
00069 fail_unless(strcmp(res, exp) == 0, "path_combine_and_normalize(%s, %s) = %s but should be %s", src, dst, res, exp);
00070 }
00071
00072 START_TEST(test_path_combine) {
00073 check_combine("/path1/file1", "/path2/file2", "/path2/file2");
00074 check_combine("path1/file1", "/path2/file2", "/path2/file2");
00075 check_combine("/path1/file1", "path2/file2", "/path1/path2/file2");
00076 check_combine("path1/file1", "path2/file2", "path1/path2/file2");
00077 check_combine("/path1", "/path2", "/path2");
00078 check_combine("path1", "/path2", "/path2");
00079 check_combine("/path1", "path2", "/path2");
00080 check_combine("path1", "path2", "path2");
00081 }
00082 END_TEST
00083
00084 START_TEST(test_path_normalize) {
00085 check_normalize("", "");
00086 check_normalize("/", "/");
00087 check_normalize("path1/file1", "path1/file1");
00088 check_normalize("/path1/file1", "/path1/file1");
00089 check_normalize("/path1//file1", "/path1/file1");
00090 check_normalize("//path1/file1", "/path1/file1");
00091 check_normalize("///////x////////y///////z////////", "/x/y/z");
00092 check_normalize("/a/b/../c/d/../e/../../f/g/../h", "/a/f/h");
00093 check_normalize("//a//b//..//c//d//..//e//..//..//f//g//..//h", "/a/f/h");
00094 check_normalize("../a", "../a");
00095 check_normalize("a/../../b", "../b");
00096 check_normalize("/../a", "/a");
00097 check_normalize("/a/../../b", "/b");
00098 check_normalize("./b/./c/.d/..e/./f", "b/c/.d/..e/f");
00099 check_normalize("/b/././././e", "/b/e");
00100 check_normalize(".", "");
00101 check_normalize("/.", "/");
00102 check_normalize("./", "");
00103 check_normalize("/a/b/..", "/a");
00104 check_normalize("/a/b/../..", "/");
00105 check_normalize("/a/b/../../..", "/");
00106 check_normalize("a/b/..", "a");
00107 check_normalize("a/b/../..", "");
00108 check_normalize("a/b/../../..", "..");
00109 }
00110 END_TEST
00111
00112 START_TEST(test_path_combine_and_normalize) {
00113 check_combine_and_normalize("/path1/file1", "/path2/file2", "/path2/file2");
00114 check_combine_and_normalize("path1/file1", "/path2/file2", "/path2/file2");
00115 check_combine_and_normalize("/path1/file1", "path2/file2", "/path1/path2/file2");
00116 check_combine_and_normalize("/path1", "/path2", "/path2");
00117 check_combine_and_normalize("path1", "/path2", "/path2");
00118 check_combine_and_normalize("/path1", "path2", "/path2");
00119 check_combine_and_normalize("/path1/file1/../u", "path2/x/../y/z/../a/b/..", "/path1/path2/y/a");
00120 check_combine_and_normalize("/path1/file1", "/path2//file2", "/path2/file2");
00121 check_combine_and_normalize("/path1/file1", "/..", "/");
00122 check_combine_and_normalize("/path1/file1", "../x", "/x");
00123 check_combine_and_normalize("/path1/file1", "../../../x", "/x");
00124 check_combine_and_normalize("/path1/file1", "/.x/..x/...x/x", "/.x/..x/...x/x");
00125 }
00126 END_TEST
00127
00128 Suite *path_suite(void) {
00129 Suite *s = suite_create("path");
00130 TCase *tc_core = tcase_create("Core");
00131
00132
00133 tcase_add_checked_fixture(tc_core, setup, teardown);
00134
00135 suite_add_tcase(s, tc_core);
00136 tcase_add_test(tc_core, test_path_combine);
00137 tcase_add_test(tc_core, test_path_normalize);
00138 tcase_add_test(tc_core, test_path_combine_and_normalize);
00139
00140 return s;
00141 }
00142
00143 int main(void) {
00144 int nf;
00145 Suite *s = path_suite();
00146 SRunner *sr = srunner_create(s);
00147
00148 srunner_set_xml(sr, LOGDIR "/unit/common/path.xml");
00149 srunner_set_log(sr, LOGDIR "/unit/common/path.out");
00150 srunner_run_all(sr, CK_ENV);
00151 nf = srunner_ntests_failed(sr);
00152 srunner_free(sr);
00153 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
00154 }