]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - libexec/rtld-elf/tests/ld_preload_fds.c
bsddialog: import version 0.0.1
[FreeBSD/FreeBSD.git] / libexec / rtld-elf / tests / ld_preload_fds.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  * Copyright 2021 Mariusz Zaborski <oshogbo@FreeBSD.org>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27
28 #include <atf-c.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31
32 #include "common.h"
33
34 int binaryfd;
35 int libraryfd;
36
37 static void
38 setup(const atf_tc_t *tc)
39 {
40         int testdir;
41
42         testdir = opendir_fd(atf_tc_get_config_var(tc, "srcdir"));
43         ATF_REQUIRE(testdir >= 0);
44
45         binaryfd = openat(testdir, TARGET_ELF_NAME, O_RDONLY);
46         ATF_REQUIRE(binaryfd >= 0);
47         libraryfd = openat(testdir, TARGET_LIBRARY, O_RDONLY);
48         ATF_REQUIRE(libraryfd >= 0);
49
50         close(testdir);
51 }
52
53 ATF_TC_WITHOUT_HEAD(missing_library);
54 ATF_TC_BODY(missing_library, tc)
55 {
56
57         setup(tc);
58         expect_missing_library(binaryfd, NULL);
59 }
60
61 ATF_TC_WITHOUT_HEAD(bad_librarys);
62 ATF_TC_BODY(bad_librarys, tc)
63 {
64         char *senv;
65
66         ATF_REQUIRE(asprintf(&senv, "LD_PRELOAD_FDS=::") > 0);
67
68         setup(tc);
69         expect_missing_library(binaryfd, senv);
70 }
71
72 ATF_TC_WITHOUT_HEAD(single_library);
73 ATF_TC_BODY(single_library, tc)
74 {
75         char *senv;
76
77         setup(tc);
78
79         ATF_REQUIRE(
80             asprintf(&senv, "LD_PRELOAD_FDS=%d", libraryfd) > 0);
81
82         expect_success(binaryfd, senv);
83 }
84
85 ATF_TC_WITHOUT_HEAD(two_librarys);
86 ATF_TC_BODY(two_librarys, tc)
87 {
88         char *senv;
89
90         setup(tc);
91
92         ATF_REQUIRE(
93             asprintf(&senv, "LD_PRELOAD_FDS=%d:%d", libraryfd, libraryfd) > 0);
94
95         expect_success(binaryfd, senv);
96 }
97
98 /* Register test cases with ATF. */
99 ATF_TP_ADD_TCS(tp)
100 {
101
102         ATF_TP_ADD_TC(tp, missing_library);
103         ATF_TP_ADD_TC(tp, bad_librarys);
104         ATF_TP_ADD_TC(tp, single_library);
105         ATF_TP_ADD_TC(tp, two_librarys);
106
107         return atf_no_error();
108 }