1/*-
2 * Copyright 2014 Jonathan Anderson.
3 * All rights reserved.
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
33struct descriptors {
34	int	binary;
35	int	testdir;
36	int	root;
37	int	etc;
38	int	usr;
39};
40
41static void	setup(struct descriptors *, const atf_tc_t *);
42static void	expect_success(int binary, char *pathfds);
43static void	expect_missing_library(int binary, char *pathfds);
44
45static void	try_to_run(int binary, int expected_exit_status,
46    char * const *env, const char *expected_out, const char *expected_err);
47static int	opendir(const char *name);
48static int	opendirat(int parent, const char *name);
49
50
51ATF_TC_WITHOUT_HEAD(missing_library);
52ATF_TC_BODY(missing_library, tc)
53{
54	struct descriptors files;
55
56	setup(&files, tc);
57	expect_missing_library(files.binary, NULL);
58}
59
60
61ATF_TC_WITHOUT_HEAD(wrong_library_directories);
62ATF_TC_BODY(wrong_library_directories, tc)
63{
64	struct descriptors files;
65	char *pathfds;
66
67	setup(&files, tc);
68	ATF_REQUIRE(
69		asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d", files.etc) > 0);
70
71	expect_missing_library(files.binary, pathfds);
72}
73
74
75ATF_TC_WITHOUT_HEAD(bad_library_directories);
76ATF_TC_BODY(bad_library_directories, tc)
77{
78	struct descriptors files;
79	char *pathfds;
80
81	setup(&files, tc);
82	ATF_REQUIRE(asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=::") > 0);
83
84	expect_missing_library(files.binary, pathfds);
85}
86
87
88ATF_TC_WITHOUT_HEAD(single_library_directory);
89ATF_TC_BODY(single_library_directory, tc)
90{
91	struct descriptors files;
92	char *pathfds;
93
94	setup(&files, tc);
95	ATF_REQUIRE(
96	    asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d", files.testdir) > 0);
97
98	expect_success(files.binary, pathfds);
99}
100
101
102ATF_TC_WITHOUT_HEAD(first_library_directory);
103ATF_TC_BODY(first_library_directory, tc)
104{
105	struct descriptors files;
106	char *pathfds;
107
108	setup(&files, tc);
109	ATF_REQUIRE(
110	    asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d",
111		files.testdir, files.etc) > 0);
112
113	expect_success(files.binary, pathfds);
114}
115
116
117ATF_TC_WITHOUT_HEAD(middle_library_directory);
118ATF_TC_BODY(middle_library_directory, tc)
119{
120	struct descriptors files;
121	char *pathfds;
122
123	setup(&files, tc);
124	ATF_REQUIRE(
125	    asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d:%d",
126		files.root, files.testdir, files.usr) > 0);
127
128	expect_success(files.binary, pathfds);
129}
130
131
132ATF_TC_WITHOUT_HEAD(last_library_directory);
133ATF_TC_BODY(last_library_directory, tc)
134{
135	struct descriptors files;
136	char *pathfds;
137
138	setup(&files, tc);
139	ATF_REQUIRE(
140	    asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d",
141		files.root, files.testdir) > 0);
142
143	expect_success(files.binary, pathfds);
144}
145
146
147
148/* Register test cases with ATF. */
149ATF_TP_ADD_TCS(tp)
150{
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);
158
159	return atf_no_error();
160}
161
162
163static void
164setup(struct descriptors *dp, const atf_tc_t *tc)
165{
166
167	dp->testdir = opendir(atf_tc_get_config_var(tc, "srcdir"));
168	ATF_REQUIRE(dp->testdir >= 0);
169	ATF_REQUIRE(
170	    (dp->binary = openat(dp->testdir, "target", O_RDONLY)) >= 0);
171
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);
175}
176
177static void
178expect_success(int binary, char *pathfds)
179{
180	char * const env[] = { pathfds, NULL };
181	try_to_run(binary, 0, env, "the hypotenuse of 3 and 4 is 5\n", "");
182}
183
184static void
185expect_missing_library(int binary, char *pathfds)
186{
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");
191}
192
193
194static void
195try_to_run(int binary, int exit_status, char * const *env,
196        const char *expected_out, const char *expected_err)
197{
198	pid_t child = atf_utils_fork();
199
200	if (child == 0) {
201		char * const args[] = { "target", NULL };
202
203		fexecve(binary, args, env);
204		atf_tc_fail("fexecve() failed");
205	}
206
207	atf_utils_wait(child, exit_status, expected_out, expected_err);
208}
209
210
211static int
212opendir(const char *name)
213{
214	return open(name, O_RDONLY | O_DIRECTORY);
215}
216
217static int
218opendirat(int parent, const char *name)
219{
220	return openat(parent, name, O_RDONLY | O_DIRECTORY);
221}
222