Crossfire Server, Branch 1.12  R12190
check_path.c
Go to the documentation of this file.
00001 /*
00002  * static char *rcsid_check_path_c =
00003  *   "$Id: check_path.c 11578 2009-02-23 22:02:27Z lalo $";
00004  */
00005 
00006 /*
00007  * CrossFire, A Multiplayer game for X-windows
00008  *
00009  * Copyright (C) 2002 Mark Wedel & Crossfire Development Team
00010  * Copyright (C) 1992 Frank Tore Johansen
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00025  *
00026  * The authors can be reached via e-mail at crossfire-devel@real-time.com
00027  */
00028 
00029 /*
00030  * This is the unit tests file for common/path.c
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     /* put any initialisation steps here, they will be run before each testcase */
00042 }
00043 
00044 void teardown(void) {
00045     /* put any cleanup steps here, they will be run after each testcase */
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     /* This is needed as path_normalize modifies in place. */
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(".", ""); /* maybe the result should be "."? */
00101     check_normalize("/.", "/");
00102     check_normalize("./", ""); /* maybe the result should be "."? */
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     /*setup and teardown will be called before each test in testcase 'tc_core' */
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); /*verbosity from env variable*/
00151     nf = srunner_ntests_failed(sr);
00152     srunner_free(sr);
00153     return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
00154 }