Path: blob/main/lib/libc/tests/gen/realpath2_test.c
103830 views
/*1* Copyright (c) 2017 Jan Kokemüller2* All rights reserved.3* Copyright (c) 2025 Klara, Inc.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/param.h>28#include <sys/stat.h>2930#include <errno.h>31#include <fcntl.h>32#include <stdio.h>33#include <stdlib.h>34#include <string.h>35#include <unistd.h>3637#include <atf-c.h>3839ATF_TC(realpath_null);40ATF_TC_HEAD(realpath_null, tc)41{42atf_tc_set_md_var(tc, "descr", "Test null input");43}44ATF_TC_BODY(realpath_null, tc)45{46ATF_REQUIRE_ERRNO(EINVAL, realpath(NULL, NULL) == NULL);47}4849ATF_TC(realpath_empty);50ATF_TC_HEAD(realpath_empty, tc)51{52atf_tc_set_md_var(tc, "descr", "Test empty input");53}54ATF_TC_BODY(realpath_empty, tc)55{56char resb[PATH_MAX] = "";5758ATF_REQUIRE_EQ(0, mkdir("foo", 0755));59ATF_REQUIRE_EQ(0, chdir("foo"));60ATF_REQUIRE_ERRNO(ENOENT, realpath("", resb) == NULL);61ATF_REQUIRE_STREQ("", resb);62}6364ATF_TC(realpath_buffer_overflow);65ATF_TC_HEAD(realpath_buffer_overflow, tc)66{67atf_tc_set_md_var(tc, "descr",68"Test for out of bounds read from 'left' array "69"(compile realpath.c with '-fsanitize=address')");70}7172ATF_TC_BODY(realpath_buffer_overflow, tc)73{74char path[PATH_MAX] = "";75char resb[PATH_MAX] = "";7677memset(path, 'a', sizeof(path) - 1);78path[1] = '/';79ATF_REQUIRE(realpath(path, resb) == NULL);80}8182ATF_TC(realpath_empty_symlink);83ATF_TC_HEAD(realpath_empty_symlink, tc)84{85atf_tc_set_md_var(tc, "descr",86"Test for correct behavior when encountering empty symlinks");87}8889ATF_TC_BODY(realpath_empty_symlink, tc)90{91char path[PATH_MAX] = "";92char slnk[PATH_MAX] = "";93char resb[PATH_MAX] = "";94int fd;9596(void)strlcat(slnk, "empty_symlink", sizeof(slnk));9798ATF_REQUIRE(symlink("", slnk) == 0);99100fd = open("aaa", O_RDONLY | O_CREAT, 0600);101102ATF_REQUIRE(fd >= 0);103ATF_REQUIRE(close(fd) == 0);104105(void)strlcat(path, "empty_symlink", sizeof(path));106(void)strlcat(path, "/aaa", sizeof(path));107108ATF_REQUIRE_ERRNO(ENOENT, realpath(path, resb) == NULL);109110ATF_REQUIRE(unlink("aaa") == 0);111ATF_REQUIRE(unlink(slnk) == 0);112}113114ATF_TC(realpath_partial);115ATF_TC_HEAD(realpath_partial, tc)116{117atf_tc_set_md_var(tc, "descr",118"Test that failure leaves a partial result");119atf_tc_set_md_var(tc, "require.user", "unprivileged");120}121122ATF_TC_BODY(realpath_partial, tc)123{124char resb[PATH_MAX] = "";125size_t len;126127/* scenario 1: missing directory */128ATF_REQUIRE_EQ(0, mkdir("foo", 0755));129ATF_REQUIRE_ERRNO(ENOENT, realpath("foo/bar/baz", resb) == NULL);130len = strnlen(resb, sizeof(resb));131ATF_REQUIRE(len > 8 && len < sizeof(resb));132ATF_REQUIRE_STREQ("/foo/bar", resb + len - 8);133134/* scenario 2: dead link 1 */135ATF_REQUIRE_EQ(0, symlink("nix", "foo/bar"));136ATF_REQUIRE_ERRNO(ENOENT, realpath("foo/bar/baz", resb) == NULL);137len = strnlen(resb, sizeof(resb));138ATF_REQUIRE(len > 8 && len < sizeof(resb));139ATF_REQUIRE_STREQ("/foo/nix", resb + len - 8);140141/* scenario 3: missing file */142ATF_REQUIRE_EQ(0, unlink("foo/bar"));143ATF_REQUIRE_EQ(0, mkdir("foo/bar", 0755));144ATF_REQUIRE_ERRNO(ENOENT, realpath("foo/bar/baz", resb) == NULL);145len = strnlen(resb, sizeof(resb));146ATF_REQUIRE(len > 12 && len < sizeof(resb));147ATF_REQUIRE_STREQ("/foo/bar/baz", resb + len - 12);148149/* scenario 4: dead link 2 */150ATF_REQUIRE_EQ(0, symlink("nix", "foo/bar/baz"));151ATF_REQUIRE_ERRNO(ENOENT, realpath("foo/bar/baz", resb) == NULL);152len = strnlen(resb, sizeof(resb));153ATF_REQUIRE(len > 12 && len < sizeof(resb));154ATF_REQUIRE_STREQ("/foo/bar/nix", resb + len - 12);155156/* scenario 5: unreadable directory */157ATF_REQUIRE_EQ(0, chmod("foo", 000));158ATF_REQUIRE_ERRNO(EACCES, realpath("foo/bar/baz", resb) == NULL);159len = strnlen(resb, sizeof(resb));160ATF_REQUIRE(len > 4 && len < sizeof(resb));161ATF_REQUIRE_STREQ("/foo", resb + len - 4);162163/* scenario 6: not a directory */164ATF_REQUIRE_EQ(0, close(creat("bar", 0644)));165ATF_REQUIRE_ERRNO(ENOTDIR, realpath("bar/baz", resb) == NULL);166len = strnlen(resb, sizeof(resb));167ATF_REQUIRE(len > 4 && len < sizeof(resb));168ATF_REQUIRE_STREQ("/bar", resb + len - 4);169}170171ATF_TP_ADD_TCS(tp)172{173ATF_TP_ADD_TC(tp, realpath_null);174ATF_TP_ADD_TC(tp, realpath_empty);175ATF_TP_ADD_TC(tp, realpath_buffer_overflow);176ATF_TP_ADD_TC(tp, realpath_empty_symlink);177ATF_TP_ADD_TC(tp, realpath_partial);178179return atf_no_error();180}181182183