Path: blob/main/libexec/rtld-elf/tests/ld_preload_fds.c
34923 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2* Copyright 2021 Mariusz Zaborski <[email protected]>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"3031int binaryfd;32int libraryfd;3334static void35setup(const atf_tc_t *tc)36{37int testdir;3839testdir = opendir_fd(atf_tc_get_config_var(tc, "srcdir"));40ATF_REQUIRE(testdir >= 0);4142binaryfd = openat(testdir, TARGET_ELF_NAME, O_RDONLY);43ATF_REQUIRE(binaryfd >= 0);44libraryfd = openat(testdir, TARGET_LIBRARY, O_RDONLY);45ATF_REQUIRE(libraryfd >= 0);4647close(testdir);48}4950ATF_TC_WITHOUT_HEAD(missing_library);51ATF_TC_BODY(missing_library, tc)52{5354setup(tc);55expect_missing_library(binaryfd, NULL);56}5758ATF_TC_WITHOUT_HEAD(bad_librarys);59ATF_TC_BODY(bad_librarys, tc)60{61char *senv;6263ATF_REQUIRE(asprintf(&senv, "LD_PRELOAD_FDS=::") > 0);6465setup(tc);66expect_missing_library(binaryfd, senv);67}6869ATF_TC_WITHOUT_HEAD(single_library);70ATF_TC_BODY(single_library, tc)71{72char *senv;7374setup(tc);7576ATF_REQUIRE(77asprintf(&senv, "LD_PRELOAD_FDS=%d", libraryfd) > 0);7879expect_success(binaryfd, senv);80}8182ATF_TC_WITHOUT_HEAD(two_librarys);83ATF_TC_BODY(two_librarys, tc)84{85char *senv;8687setup(tc);8889ATF_REQUIRE(90asprintf(&senv, "LD_PRELOAD_FDS=%d:%d", libraryfd, libraryfd) > 0);9192expect_success(binaryfd, senv);93}9495/* Register test cases with ATF. */96ATF_TP_ADD_TCS(tp)97{9899ATF_TP_ADD_TC(tp, missing_library);100ATF_TP_ADD_TC(tp, bad_librarys);101ATF_TP_ADD_TC(tp, single_library);102ATF_TP_ADD_TC(tp, two_librarys);103104return atf_no_error();105}106107108