xref: /freebsd/libexec/rtld-elf/tests/ld_library_pathfds.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
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 #include "common.h"
33 
34 struct descriptors {
35 	int	binary;
36 	int	testdir;
37 	int	root;
38 	int	etc;
39 	int	usr;
40 };
41 
42 
43 static void	setup(struct descriptors *, const atf_tc_t *);
44 
45 
46 ATF_TC_WITHOUT_HEAD(missing_library);
47 ATF_TC_BODY(missing_library, tc)
48 {
49 	struct descriptors files;
50 
51 	setup(&files, tc);
52 	expect_missing_library(files.binary, NULL);
53 }
54 
55 
56 ATF_TC_WITHOUT_HEAD(wrong_library_directories);
57 ATF_TC_BODY(wrong_library_directories, tc)
58 {
59 	struct descriptors files;
60 	char *pathfds;
61 
62 	setup(&files, tc);
63 	ATF_REQUIRE(
64 		asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d", files.etc) > 0);
65 
66 	expect_missing_library(files.binary, pathfds);
67 }
68 
69 
70 ATF_TC_WITHOUT_HEAD(bad_library_directories);
71 ATF_TC_BODY(bad_library_directories, tc)
72 {
73 	struct descriptors files;
74 	char *pathfds;
75 
76 	setup(&files, tc);
77 	ATF_REQUIRE(asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=::") > 0);
78 
79 	expect_missing_library(files.binary, pathfds);
80 }
81 
82 
83 ATF_TC_WITHOUT_HEAD(single_library_directory);
84 ATF_TC_BODY(single_library_directory, tc)
85 {
86 	struct descriptors files;
87 	char *pathfds;
88 
89 	setup(&files, tc);
90 	ATF_REQUIRE(
91 	    asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d", files.testdir) > 0);
92 
93 	expect_success(files.binary, pathfds);
94 }
95 
96 
97 ATF_TC_WITHOUT_HEAD(first_library_directory);
98 ATF_TC_BODY(first_library_directory, tc)
99 {
100 	struct descriptors files;
101 	char *pathfds;
102 
103 	setup(&files, tc);
104 	ATF_REQUIRE(
105 	    asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d",
106 		files.testdir, files.etc) > 0);
107 
108 	expect_success(files.binary, pathfds);
109 }
110 
111 
112 ATF_TC_WITHOUT_HEAD(middle_library_directory);
113 ATF_TC_BODY(middle_library_directory, tc)
114 {
115 	struct descriptors files;
116 	char *pathfds;
117 
118 	setup(&files, tc);
119 	ATF_REQUIRE(
120 	    asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d:%d",
121 		files.root, files.testdir, files.usr) > 0);
122 
123 	expect_success(files.binary, pathfds);
124 }
125 
126 
127 ATF_TC_WITHOUT_HEAD(last_library_directory);
128 ATF_TC_BODY(last_library_directory, tc)
129 {
130 	struct descriptors files;
131 	char *pathfds;
132 
133 	setup(&files, tc);
134 	ATF_REQUIRE(
135 	    asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d",
136 		files.root, files.testdir) > 0);
137 
138 	expect_success(files.binary, pathfds);
139 }
140 
141 
142 
143 /* Register test cases with ATF. */
144 ATF_TP_ADD_TCS(tp)
145 {
146 	ATF_TP_ADD_TC(tp, missing_library);
147 	ATF_TP_ADD_TC(tp, wrong_library_directories);
148 	ATF_TP_ADD_TC(tp, bad_library_directories);
149 	ATF_TP_ADD_TC(tp, single_library_directory);
150 	ATF_TP_ADD_TC(tp, first_library_directory);
151 	ATF_TP_ADD_TC(tp, middle_library_directory);
152 	ATF_TP_ADD_TC(tp, last_library_directory);
153 
154 	return atf_no_error();
155 }
156 
157 
158 static void
159 setup(struct descriptors *dp, const atf_tc_t *tc)
160 {
161 
162 	dp->testdir = opendir_fd(atf_tc_get_config_var(tc, "srcdir"));
163 	ATF_REQUIRE(dp->testdir >= 0);
164 	ATF_REQUIRE(
165 	    (dp->binary = openat(dp->testdir, TARGET_ELF_NAME, O_RDONLY)) >= 0);
166 
167 	ATF_REQUIRE((dp->root = opendir_fd("/")) >= 0);
168 	ATF_REQUIRE((dp->etc = opendirat(dp->root, "etc")) >= 0);
169 	ATF_REQUIRE((dp->usr = opendirat(dp->root, "usr")) >= 0);
170 }
171 
172