1272343Sngie/* $NetBSD: t_realpath.c,v 1.2 2012/03/27 07:54:58 njoly Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2012 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundation 8272343Sngie * by Jukka Ruohonen. 9272343Sngie * 10272343Sngie * Redistribution and use in source and binary forms, with or without 11272343Sngie * modification, are permitted provided that the following conditions 12272343Sngie * are met: 13272343Sngie * 1. Redistributions of source code must retain the above copyright 14272343Sngie * notice, this list of conditions and the following disclaimer. 15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 16272343Sngie * notice, this list of conditions and the following disclaimer in the 17272343Sngie * documentation and/or other materials provided with the distribution. 18272343Sngie * 19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29272343Sngie * POSSIBILITY OF SUCH DAMAGE. 30272343Sngie */ 31272343Sngie#include <sys/cdefs.h> 32272343Sngie__RCSID("$NetBSD: t_realpath.c,v 1.2 2012/03/27 07:54:58 njoly Exp $"); 33272343Sngie 34272343Sngie#include <sys/param.h> 35272343Sngie 36272343Sngie#include <atf-c.h> 37319093Sngie#ifdef __FreeBSD__ 38319093Sngie#include <errno.h> 39319093Sngie#endif 40272343Sngie#include <fcntl.h> 41272343Sngie#include <stdio.h> 42272343Sngie#include <stdlib.h> 43272343Sngie#include <string.h> 44272343Sngie#include <unistd.h> 45272343Sngie 46272343Sngiestatic const struct { 47272343Sngie const char *path; 48272343Sngie const char *result; 49272343Sngie} paths[] = { 50272343Sngie 51272343Sngie { "/", "/" }, 52272343Sngie { "///////", "/" }, 53272343Sngie { "", NULL }, 54272343Sngie { " ", NULL }, 55272343Sngie { "/ ", NULL }, 56272343Sngie { " /", NULL }, 57272343Sngie { "/etc///", "/etc" }, 58272343Sngie { "///////etc", "/etc" }, 59272343Sngie { "/a/b/c/d/e", NULL }, 60272343Sngie { " /usr/bin ", NULL }, 61272343Sngie { "\\//////usr//bin", NULL }, 62272343Sngie { "//usr//bin//", "/usr/bin" }, 63272343Sngie { "//////usr//bin//", "/usr/bin" }, 64272343Sngie { "/usr/bin//////////", "/usr/bin" }, 65272343Sngie}; 66272343Sngie 67272343SngieATF_TC(realpath_basic); 68272343SngieATF_TC_HEAD(realpath_basic, tc) 69272343Sngie{ 70272343Sngie atf_tc_set_md_var(tc, "descr", "A basic test of realpath(3)"); 71272343Sngie} 72272343Sngie 73272343SngieATF_TC_BODY(realpath_basic, tc) 74272343Sngie{ 75272343Sngie char buf[MAXPATHLEN]; 76272343Sngie char *ptr; 77272343Sngie size_t i; 78272343Sngie 79272343Sngie for (i = 0; i < __arraycount(paths); i++) { 80272343Sngie 81272343Sngie (void)memset(buf, '\0', sizeof(buf)); 82272343Sngie 83272343Sngie ptr = realpath(paths[i].path, buf); 84272343Sngie 85272343Sngie if (ptr == NULL && paths[i].result == NULL) 86272343Sngie continue; 87272343Sngie 88272343Sngie if (ptr == NULL && paths[i].result != NULL) 89272343Sngie atf_tc_fail("realpath failed for '%s'", paths[i].path); 90272343Sngie 91272343Sngie if (strcmp(paths[i].result, buf) != 0) 92272343Sngie atf_tc_fail("expected '%s', got '%s'", 93272343Sngie paths[i].result, buf); 94272343Sngie } 95272343Sngie} 96272343Sngie 97272343SngieATF_TC(realpath_huge); 98272343SngieATF_TC_HEAD(realpath_huge, tc) 99272343Sngie{ 100272343Sngie atf_tc_set_md_var(tc, "descr", "Test huge path with realpath(3)"); 101272343Sngie} 102272343Sngie 103272343SngieATF_TC_BODY(realpath_huge, tc) 104272343Sngie{ 105272343Sngie char result[MAXPATHLEN] = { 0 }; 106272343Sngie char buffer[MAXPATHLEN] = { 0 }; 107272343Sngie 108272343Sngie (void)memset(buffer, '/', sizeof(buffer) - 1); 109272343Sngie 110272343Sngie ATF_CHECK(realpath(buffer, result) != NULL); 111272343Sngie ATF_CHECK(strlen(result) == 1); 112272343Sngie ATF_CHECK(result[0] == '/'); 113272343Sngie} 114272343Sngie 115272343SngieATF_TC(realpath_symlink); 116272343SngieATF_TC_HEAD(realpath_symlink, tc) 117272343Sngie{ 118272343Sngie atf_tc_set_md_var(tc, "descr", "Test symbolic link with realpath(3)"); 119272343Sngie} 120272343Sngie 121272343SngieATF_TC_BODY(realpath_symlink, tc) 122272343Sngie{ 123272343Sngie char path[MAXPATHLEN] = { 0 }; 124272343Sngie char slnk[MAXPATHLEN] = { 0 }; 125272343Sngie char resb[MAXPATHLEN] = { 0 }; 126272343Sngie int fd; 127272343Sngie 128319093Sngie#ifdef __FreeBSD__ 129319093Sngie ATF_REQUIRE_MSG(getcwd(path, sizeof(path)) != NULL, 130319093Sngie "getcwd(path) failed: %s", strerror(errno)); 131319093Sngie ATF_REQUIRE_MSG(getcwd(slnk, sizeof(slnk)) != NULL, 132319093Sngie "getcwd(slnk) failed: %s", strerror(errno)); 133319093Sngie#else 134272343Sngie (void)getcwd(path, sizeof(path)); 135272343Sngie (void)getcwd(slnk, sizeof(slnk)); 136319093Sngie#endif 137272343Sngie 138272343Sngie (void)strlcat(path, "/realpath", sizeof(path)); 139272343Sngie (void)strlcat(slnk, "/symbolic", sizeof(slnk)); 140272343Sngie 141272343Sngie fd = open(path, O_RDONLY | O_CREAT, 0600); 142272343Sngie 143272343Sngie ATF_REQUIRE(fd >= 0); 144272343Sngie ATF_REQUIRE(symlink(path, slnk) == 0); 145272343Sngie ATF_REQUIRE(close(fd) == 0); 146272343Sngie 147272343Sngie ATF_REQUIRE(realpath(slnk, resb) != NULL); 148272343Sngie ATF_REQUIRE(strcmp(resb, path) == 0); 149272343Sngie 150272343Sngie ATF_REQUIRE(unlink(path) == 0); 151272343Sngie ATF_REQUIRE(unlink(slnk) == 0); 152272343Sngie} 153272343Sngie 154272343SngieATF_TP_ADD_TCS(tp) 155272343Sngie{ 156272343Sngie 157272343Sngie ATF_TP_ADD_TC(tp, realpath_basic); 158272343Sngie ATF_TP_ADD_TC(tp, realpath_huge); 159272343Sngie ATF_TP_ADD_TC(tp, realpath_symlink); 160272343Sngie 161272343Sngie return atf_no_error(); 162272343Sngie} 163