2 * Copyright 2014 Jonathan Anderson.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
41 static void setup(struct descriptors *, const atf_tc_t *);
42 static void expect_success(int binary, char *pathfds);
43 static void expect_missing_library(int binary, char *pathfds);
45 static void try_to_run(int binary, int expected_exit_status,
46 char * const *env, const char *expected_out, const char *expected_err);
47 static int opendir(const char *name);
48 static int opendirat(int parent, const char *name);
51 ATF_TC_WITHOUT_HEAD(missing_library);
52 ATF_TC_BODY(missing_library, tc)
54 struct descriptors files;
57 expect_missing_library(files.binary, NULL);
61 ATF_TC_WITHOUT_HEAD(wrong_library_directories);
62 ATF_TC_BODY(wrong_library_directories, tc)
64 struct descriptors files;
69 asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d", files.etc) > 0);
71 expect_missing_library(files.binary, pathfds);
75 ATF_TC_WITHOUT_HEAD(bad_library_directories);
76 ATF_TC_BODY(bad_library_directories, tc)
78 struct descriptors files;
82 ATF_REQUIRE(asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=::") > 0);
84 expect_missing_library(files.binary, pathfds);
88 ATF_TC_WITHOUT_HEAD(single_library_directory);
89 ATF_TC_BODY(single_library_directory, tc)
91 struct descriptors files;
96 asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d", files.testdir) > 0);
98 expect_success(files.binary, pathfds);
102 ATF_TC_WITHOUT_HEAD(first_library_directory);
103 ATF_TC_BODY(first_library_directory, tc)
105 struct descriptors files;
110 asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d",
111 files.testdir, files.etc) > 0);
113 expect_success(files.binary, pathfds);
117 ATF_TC_WITHOUT_HEAD(middle_library_directory);
118 ATF_TC_BODY(middle_library_directory, tc)
120 struct descriptors files;
125 asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d:%d",
126 files.root, files.testdir, files.usr) > 0);
128 expect_success(files.binary, pathfds);
132 ATF_TC_WITHOUT_HEAD(last_library_directory);
133 ATF_TC_BODY(last_library_directory, tc)
135 struct descriptors files;
140 asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d",
141 files.root, files.testdir) > 0);
143 expect_success(files.binary, pathfds);
148 /* Register test cases with ATF. */
151 ATF_TP_ADD_TC(tp, missing_library);
152 ATF_TP_ADD_TC(tp, wrong_library_directories);
153 ATF_TP_ADD_TC(tp, bad_library_directories);
154 ATF_TP_ADD_TC(tp, single_library_directory);
155 ATF_TP_ADD_TC(tp, first_library_directory);
156 ATF_TP_ADD_TC(tp, middle_library_directory);
157 ATF_TP_ADD_TC(tp, last_library_directory);
159 return atf_no_error();
164 setup(struct descriptors *dp, const atf_tc_t *tc)
167 dp->testdir = opendir(atf_tc_get_config_var(tc, "srcdir"));
168 ATF_REQUIRE(dp->testdir >= 0);
170 (dp->binary = openat(dp->testdir, "target", O_RDONLY)) >= 0);
172 ATF_REQUIRE((dp->root = opendir("/")) >= 0);
173 ATF_REQUIRE((dp->etc = opendirat(dp->root, "etc")) >= 0);
174 ATF_REQUIRE((dp->usr = opendirat(dp->root, "usr")) >= 0);
178 expect_success(int binary, char *pathfds)
180 char * const env[] = { pathfds, NULL };
181 try_to_run(binary, 0, env, "the hypotenuse of 3 and 4 is 5\n", "");
185 expect_missing_library(int binary, char *pathfds)
187 char * const env[] = { pathfds, NULL };
188 try_to_run(binary, 1, env, "",
189 "ld-elf.so.1: Shared object \"libpythagoras.so.0\" not found,"
190 " required by \"target\"\n");
195 try_to_run(int binary, int exit_status, char * const *env,
196 const char *expected_out, const char *expected_err)
198 pid_t child = atf_utils_fork();
201 char * const args[] = { "target", NULL };
203 fexecve(binary, args, env);
204 atf_tc_fail("fexecve() failed");
207 atf_utils_wait(child, exit_status, expected_out, expected_err);
212 opendir(const char *name)
214 return open(name, O_RDONLY | O_DIRECTORY);
218 opendirat(int parent, const char *name)
220 return openat(parent, name, O_RDONLY | O_DIRECTORY);