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