Path: blob/main/libexec/rtld-elf/tests/ld_library_pathfds.c
34923 views
/*-1* Copyright 2014 Jonathan Anderson.2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR14* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES15* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,17* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT18* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,19* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY20* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT21* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF22* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.23*/2425#include <atf-c.h>26#include <fcntl.h>27#include <stdio.h>2829#include "common.h"3031struct descriptors {32int binary;33int testdir;34int root;35int etc;36int usr;37};383940static void setup(struct descriptors *, const atf_tc_t *);414243ATF_TC_WITHOUT_HEAD(missing_library);44ATF_TC_BODY(missing_library, tc)45{46struct descriptors files;4748setup(&files, tc);49expect_missing_library(files.binary, NULL);50}515253ATF_TC_WITHOUT_HEAD(wrong_library_directories);54ATF_TC_BODY(wrong_library_directories, tc)55{56struct descriptors files;57char *pathfds;5859setup(&files, tc);60ATF_REQUIRE(61asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d", files.etc) > 0);6263expect_missing_library(files.binary, pathfds);64}656667ATF_TC_WITHOUT_HEAD(bad_library_directories);68ATF_TC_BODY(bad_library_directories, tc)69{70struct descriptors files;71char *pathfds;7273setup(&files, tc);74ATF_REQUIRE(asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=::") > 0);7576expect_missing_library(files.binary, pathfds);77}787980ATF_TC_WITHOUT_HEAD(single_library_directory);81ATF_TC_BODY(single_library_directory, tc)82{83struct descriptors files;84char *pathfds;8586setup(&files, tc);87ATF_REQUIRE(88asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d", files.testdir) > 0);8990expect_success(files.binary, pathfds);91}929394ATF_TC_WITHOUT_HEAD(first_library_directory);95ATF_TC_BODY(first_library_directory, tc)96{97struct descriptors files;98char *pathfds;99100setup(&files, tc);101ATF_REQUIRE(102asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d",103files.testdir, files.etc) > 0);104105expect_success(files.binary, pathfds);106}107108109ATF_TC_WITHOUT_HEAD(middle_library_directory);110ATF_TC_BODY(middle_library_directory, tc)111{112struct descriptors files;113char *pathfds;114115setup(&files, tc);116ATF_REQUIRE(117asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d:%d",118files.root, files.testdir, files.usr) > 0);119120expect_success(files.binary, pathfds);121}122123124ATF_TC_WITHOUT_HEAD(last_library_directory);125ATF_TC_BODY(last_library_directory, tc)126{127struct descriptors files;128char *pathfds;129130setup(&files, tc);131ATF_REQUIRE(132asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d",133files.root, files.testdir) > 0);134135expect_success(files.binary, pathfds);136}137138139140/* Register test cases with ATF. */141ATF_TP_ADD_TCS(tp)142{143ATF_TP_ADD_TC(tp, missing_library);144ATF_TP_ADD_TC(tp, wrong_library_directories);145ATF_TP_ADD_TC(tp, bad_library_directories);146ATF_TP_ADD_TC(tp, single_library_directory);147ATF_TP_ADD_TC(tp, first_library_directory);148ATF_TP_ADD_TC(tp, middle_library_directory);149ATF_TP_ADD_TC(tp, last_library_directory);150151return atf_no_error();152}153154155static void156setup(struct descriptors *dp, const atf_tc_t *tc)157{158159dp->testdir = opendir_fd(atf_tc_get_config_var(tc, "srcdir"));160ATF_REQUIRE(dp->testdir >= 0);161ATF_REQUIRE(162(dp->binary = openat(dp->testdir, TARGET_ELF_NAME, O_RDONLY)) >= 0);163164ATF_REQUIRE((dp->root = opendir_fd("/")) >= 0);165ATF_REQUIRE((dp->etc = opendirat(dp->root, "etc")) >= 0);166ATF_REQUIRE((dp->usr = opendirat(dp->root, "usr")) >= 0);167}168169170171