11.2Snjoly/* $NetBSD: t_realpath.c,v 1.2 2012/03/27 07:54:58 njoly Exp $ */ 21.1Sjruoho 31.1Sjruoho/*- 41.1Sjruoho * Copyright (c) 2012 The NetBSD Foundation, Inc. 51.1Sjruoho * All rights reserved. 61.1Sjruoho * 71.1Sjruoho * This code is derived from software contributed to The NetBSD Foundation 81.1Sjruoho * by Jukka Ruohonen. 91.1Sjruoho * 101.1Sjruoho * Redistribution and use in source and binary forms, with or without 111.1Sjruoho * modification, are permitted provided that the following conditions 121.1Sjruoho * are met: 131.1Sjruoho * 1. Redistributions of source code must retain the above copyright 141.1Sjruoho * notice, this list of conditions and the following disclaimer. 151.1Sjruoho * 2. Redistributions in binary form must reproduce the above copyright 161.1Sjruoho * notice, this list of conditions and the following disclaimer in the 171.1Sjruoho * documentation and/or other materials provided with the distribution. 181.1Sjruoho * 191.1Sjruoho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 201.1Sjruoho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 211.1Sjruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 221.1Sjruoho * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 231.1Sjruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 241.1Sjruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 251.1Sjruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 261.1Sjruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 271.1Sjruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 281.1Sjruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 291.1Sjruoho * POSSIBILITY OF SUCH DAMAGE. 301.1Sjruoho */ 311.1Sjruoho#include <sys/cdefs.h> 321.2Snjoly__RCSID("$NetBSD: t_realpath.c,v 1.2 2012/03/27 07:54:58 njoly Exp $"); 331.1Sjruoho 341.1Sjruoho#include <sys/param.h> 351.1Sjruoho 361.1Sjruoho#include <atf-c.h> 371.1Sjruoho#include <fcntl.h> 381.1Sjruoho#include <stdio.h> 391.1Sjruoho#include <stdlib.h> 401.1Sjruoho#include <string.h> 411.1Sjruoho#include <unistd.h> 421.1Sjruoho 431.1Sjruohostatic const struct { 441.1Sjruoho const char *path; 451.1Sjruoho const char *result; 461.1Sjruoho} paths[] = { 471.1Sjruoho 481.1Sjruoho { "/", "/" }, 491.1Sjruoho { "///////", "/" }, 501.1Sjruoho { "", NULL }, 511.1Sjruoho { " ", NULL }, 521.1Sjruoho { "/ ", NULL }, 531.1Sjruoho { " /", NULL }, 541.1Sjruoho { "/etc///", "/etc" }, 551.1Sjruoho { "///////etc", "/etc" }, 561.1Sjruoho { "/a/b/c/d/e", NULL }, 571.1Sjruoho { " /usr/bin ", NULL }, 581.1Sjruoho { "\\//////usr//bin", NULL }, 591.1Sjruoho { "//usr//bin//", "/usr/bin" }, 601.1Sjruoho { "//////usr//bin//", "/usr/bin" }, 611.1Sjruoho { "/usr/bin//////////", "/usr/bin" }, 621.1Sjruoho}; 631.1Sjruoho 641.1SjruohoATF_TC(realpath_basic); 651.1SjruohoATF_TC_HEAD(realpath_basic, tc) 661.1Sjruoho{ 671.1Sjruoho atf_tc_set_md_var(tc, "descr", "A basic test of realpath(3)"); 681.1Sjruoho} 691.1Sjruoho 701.1SjruohoATF_TC_BODY(realpath_basic, tc) 711.1Sjruoho{ 721.1Sjruoho char buf[MAXPATHLEN]; 731.1Sjruoho char *ptr; 741.1Sjruoho size_t i; 751.1Sjruoho 761.1Sjruoho for (i = 0; i < __arraycount(paths); i++) { 771.1Sjruoho 781.1Sjruoho (void)memset(buf, '\0', sizeof(buf)); 791.1Sjruoho 801.1Sjruoho ptr = realpath(paths[i].path, buf); 811.1Sjruoho 821.1Sjruoho if (ptr == NULL && paths[i].result == NULL) 831.1Sjruoho continue; 841.1Sjruoho 851.1Sjruoho if (ptr == NULL && paths[i].result != NULL) 861.1Sjruoho atf_tc_fail("realpath failed for '%s'", paths[i].path); 871.1Sjruoho 881.1Sjruoho if (strcmp(paths[i].result, buf) != 0) 891.1Sjruoho atf_tc_fail("expected '%s', got '%s'", 901.1Sjruoho paths[i].result, buf); 911.1Sjruoho } 921.1Sjruoho} 931.1Sjruoho 941.1SjruohoATF_TC(realpath_huge); 951.1SjruohoATF_TC_HEAD(realpath_huge, tc) 961.1Sjruoho{ 971.1Sjruoho atf_tc_set_md_var(tc, "descr", "Test huge path with realpath(3)"); 981.1Sjruoho} 991.1Sjruoho 1001.1SjruohoATF_TC_BODY(realpath_huge, tc) 1011.1Sjruoho{ 1021.1Sjruoho char result[MAXPATHLEN] = { 0 }; 1031.1Sjruoho char buffer[MAXPATHLEN] = { 0 }; 1041.1Sjruoho 1051.2Snjoly (void)memset(buffer, '/', sizeof(buffer) - 1); 1061.1Sjruoho 1071.1Sjruoho ATF_CHECK(realpath(buffer, result) != NULL); 1081.1Sjruoho ATF_CHECK(strlen(result) == 1); 1091.1Sjruoho ATF_CHECK(result[0] == '/'); 1101.1Sjruoho} 1111.1Sjruoho 1121.1SjruohoATF_TC(realpath_symlink); 1131.1SjruohoATF_TC_HEAD(realpath_symlink, tc) 1141.1Sjruoho{ 1151.1Sjruoho atf_tc_set_md_var(tc, "descr", "Test symbolic link with realpath(3)"); 1161.1Sjruoho} 1171.1Sjruoho 1181.1SjruohoATF_TC_BODY(realpath_symlink, tc) 1191.1Sjruoho{ 1201.1Sjruoho char path[MAXPATHLEN] = { 0 }; 1211.1Sjruoho char slnk[MAXPATHLEN] = { 0 }; 1221.1Sjruoho char resb[MAXPATHLEN] = { 0 }; 1231.1Sjruoho int fd; 1241.1Sjruoho 1251.1Sjruoho (void)getcwd(path, sizeof(path)); 1261.1Sjruoho (void)getcwd(slnk, sizeof(slnk)); 1271.1Sjruoho 1281.1Sjruoho (void)strlcat(path, "/realpath", sizeof(path)); 1291.1Sjruoho (void)strlcat(slnk, "/symbolic", sizeof(slnk)); 1301.1Sjruoho 1311.1Sjruoho fd = open(path, O_RDONLY | O_CREAT, 0600); 1321.1Sjruoho 1331.1Sjruoho ATF_REQUIRE(fd >= 0); 1341.1Sjruoho ATF_REQUIRE(symlink(path, slnk) == 0); 1351.1Sjruoho ATF_REQUIRE(close(fd) == 0); 1361.1Sjruoho 1371.1Sjruoho ATF_REQUIRE(realpath(slnk, resb) != NULL); 1381.1Sjruoho ATF_REQUIRE(strcmp(resb, path) == 0); 1391.1Sjruoho 1401.1Sjruoho ATF_REQUIRE(unlink(path) == 0); 1411.1Sjruoho ATF_REQUIRE(unlink(slnk) == 0); 1421.1Sjruoho} 1431.1Sjruoho 1441.1SjruohoATF_TP_ADD_TCS(tp) 1451.1Sjruoho{ 1461.1Sjruoho 1471.1Sjruoho ATF_TP_ADD_TC(tp, realpath_basic); 1481.1Sjruoho ATF_TP_ADD_TC(tp, realpath_huge); 1491.1Sjruoho ATF_TP_ADD_TC(tp, realpath_symlink); 1501.1Sjruoho 1511.1Sjruoho return atf_no_error(); 1521.1Sjruoho} 153