Path: blob/main/crypto/krb5/src/util/support/t_path.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* util/support/t_path.c - Path manipulation tests */2/*3* Copyright (C) 2011 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526#include <k5-platform.h>2728/* For testing purposes, use a different symbol for Windows path semantics. */29#ifdef _WIN3230#define WINDOWS_PATHS31#endif3233/*34* The ultimate arbiter of these tests is the dirname, basename, and isabs35* methods of the Python posixpath and ntpath modules.36*/3738struct {39const char *path;40const char *posix_dirname;41const char *posix_basename;42const char *win_dirname;43const char *win_basename;44} split_tests[] = {45{ "", "", "", "", "" },46{ "a/b/c", "a/b", "c", "a/b", "c" },47{ "a/b/", "a/b", "", "a/b", "" },48{ "a\\b\\c", "", "a\\b\\c", "a\\b", "c" },49{ "a\\b\\", "", "a\\b\\", "a\\b", "" },50{ "a/b\\c", "a", "b\\c", "a/b", "c" },51{ "a//b", "a", "b", "a", "b" },52{ "a/\\/b", "a/\\", "b", "a", "b" },53{ "a//b/c", "a//b", "c", "a//b", "c" },5455{ "/", "/", "", "/", "" },56{ "\\", "", "\\", "\\", "" },57{ "/a/b/c", "/a/b", "c", "/a/b", "c" },58{ "\\a/b/c", "\\a/b", "c", "\\a/b", "c" },59{ "/a", "/", "a", "/", "a" },60{ "//a", "//", "a", "//", "a" },61{ "\\//\\a", "\\", "\\a", "\\//\\", "a" },6263{ "/:", "/", ":", "/:", "" },64{ "c:\\", "", "c:\\", "c:\\", "" },65{ "c:/", "c:", "", "c:/", "" },66{ "c:/\\a", "c:", "\\a", "c:/\\", "a" },67{ "c:a", "", "c:a", "c:", "a" },68};6970struct {71const char *path1;72const char *path2;73const char *posix_result;74const char *win_result;75} join_tests[] = {76{ "", "", "", "" },77{ "", "a", "a", "a" },78{ "", "/a", "/a", "/a" },79{ "", "c:", "c:", "c:" },8081{ "a", "", "a/", "a\\" },82{ "a/", "", "a/", "a/" },83{ "a\\", "", "a\\/", "a\\" },84{ "a/\\", "", "a/\\/", "a/\\" },8586{ "a", "b", "a/b", "a\\b" },87{ "a", "/b", "/b", "/b" },88{ "a", "c:", "a/c:", "a\\c:" },89{ "a", "c:/", "a/c:/", "c:/" },90{ "a", "c:/a", "a/c:/a", "c:/a" },91{ "a", "/:", "/:", "a/:" },92{ "a/", "b", "a/b", "a/b" },93{ "a/", "", "a/", "a/" },94{ "a\\", "b", "a\\/b", "a\\b" },9596{ "a//", "b", "a//b", "a//b" },97{ "a/\\", "b", "a/\\/b", "a/\\b" },98};99100struct {101const char *path;102int posix_result;103int win_result;104} isabs_tests[] = {105{ "", 0, 0 },106{ "/", 1, 1 },107{ "/a", 1, 1 },108{ "a/b", 0, 0 },109{ "\\", 0, 1 },110{ "\\a", 0, 1 },111{ "c:", 0, 0 },112{ "/:", 1, 0 },113{ "\\:", 0, 0 },114{ "c:/a", 0, 1 },115{ "c:\\a", 0, 1 },116{ "c:a", 0, 0 },117{ "c:a/b", 0, 0 },118{ "/:a/b", 1, 0 },119};120121int122main(void)123{124char *dirname, *basename, *joined;125const char *edirname, *ebasename, *ejoined, *ipath, *path1, *path2;126int result, eresult, status = 0;127size_t i;128129for (i = 0; i < sizeof(split_tests) / sizeof(*split_tests); i++) {130ipath = split_tests[i].path;131#ifdef WINDOWS_PATHS132edirname = split_tests[i].win_dirname;133ebasename = split_tests[i].win_basename;134#else135edirname = split_tests[i].posix_dirname;136ebasename = split_tests[i].posix_basename;137#endif138if (k5_path_split(ipath, NULL, NULL) != 0)139abort();140if (k5_path_split(ipath, &dirname, NULL) != 0)141abort();142free(dirname);143if (k5_path_split(ipath, NULL, &basename) != 0)144abort();145free(basename);146if (k5_path_split(ipath, &dirname, &basename) != 0)147abort();148if (strcmp(dirname, edirname) != 0) {149fprintf(stderr, "Split test %d: dirname %s != expected %s\n",150(int)i, dirname, edirname);151status = 1;152}153if (strcmp(basename, ebasename) != 0) {154fprintf(stderr, "Split test %d: basename %s != expected %s\n",155(int)i, basename, ebasename);156status = 1;157}158free(dirname);159free(basename);160}161162for (i = 0; i < sizeof(join_tests) / sizeof(*join_tests); i++) {163path1 = join_tests[i].path1;164path2 = join_tests[i].path2;165#ifdef WINDOWS_PATHS166ejoined = join_tests[i].win_result;167#else168ejoined = join_tests[i].posix_result;169#endif170if (k5_path_join(path1, path2, &joined) != 0)171abort();172if (strcmp(joined, ejoined) != 0) {173fprintf(stderr, "Join test %d: %s != expected %s\n",174(int)i, joined, ejoined);175status = 1;176}177free(joined);178}179180for (i = 0; i < sizeof(isabs_tests) / sizeof(*isabs_tests); i++) {181#ifdef WINDOWS_PATHS182eresult = isabs_tests[i].win_result;183#else184eresult = isabs_tests[i].posix_result;185#endif186result = k5_path_isabs(isabs_tests[i].path);187if (result != eresult) {188fprintf(stderr, "isabs test %d: %d != expected %d\n",189(int)i, result, eresult);190status = 1;191}192}193194return status;195}196197198