16b22f423SJonathan Anderson /*-
26b22f423SJonathan Anderson * Copyright 2014 Jonathan Anderson.
36b22f423SJonathan Anderson * All rights reserved.
46b22f423SJonathan Anderson *
56b22f423SJonathan Anderson * Redistribution and use in source and binary forms, with or without
66b22f423SJonathan Anderson * modification, are permitted provided that the following conditions
76b22f423SJonathan Anderson * are met:
86b22f423SJonathan Anderson * 1. Redistributions of source code must retain the above copyright
96b22f423SJonathan Anderson * notice, this list of conditions and the following disclaimer.
106b22f423SJonathan Anderson * 2. Redistributions in binary form must reproduce the above copyright
116b22f423SJonathan Anderson * notice, this list of conditions and the following disclaimer in the
126b22f423SJonathan Anderson * documentation and/or other materials provided with the distribution.
136b22f423SJonathan Anderson *
146b22f423SJonathan Anderson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
156b22f423SJonathan Anderson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
166b22f423SJonathan Anderson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
176b22f423SJonathan Anderson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
186b22f423SJonathan Anderson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
196b22f423SJonathan Anderson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
206b22f423SJonathan Anderson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
216b22f423SJonathan Anderson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
226b22f423SJonathan Anderson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
236b22f423SJonathan Anderson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
246b22f423SJonathan Anderson */
256b22f423SJonathan Anderson
266b22f423SJonathan Anderson #include <atf-c.h>
276b22f423SJonathan Anderson #include <fcntl.h>
286b22f423SJonathan Anderson #include <stdio.h>
296b22f423SJonathan Anderson
30f9021888SMariusz Zaborski #include "common.h"
316b22f423SJonathan Anderson
326b22f423SJonathan Anderson struct descriptors {
336b22f423SJonathan Anderson int binary;
346b22f423SJonathan Anderson int testdir;
356b22f423SJonathan Anderson int root;
366b22f423SJonathan Anderson int etc;
376b22f423SJonathan Anderson int usr;
386b22f423SJonathan Anderson };
396b22f423SJonathan Anderson
406b22f423SJonathan Anderson
41f9021888SMariusz Zaborski static void setup(struct descriptors *, const atf_tc_t *);
426b22f423SJonathan Anderson
436b22f423SJonathan Anderson
446b22f423SJonathan Anderson ATF_TC_WITHOUT_HEAD(missing_library);
ATF_TC_BODY(missing_library,tc)456b22f423SJonathan Anderson ATF_TC_BODY(missing_library, tc)
466b22f423SJonathan Anderson {
476b22f423SJonathan Anderson struct descriptors files;
486b22f423SJonathan Anderson
491a49685cSEnji Cooper setup(&files, tc);
506b22f423SJonathan Anderson expect_missing_library(files.binary, NULL);
516b22f423SJonathan Anderson }
526b22f423SJonathan Anderson
536b22f423SJonathan Anderson
546b22f423SJonathan Anderson ATF_TC_WITHOUT_HEAD(wrong_library_directories);
ATF_TC_BODY(wrong_library_directories,tc)556b22f423SJonathan Anderson ATF_TC_BODY(wrong_library_directories, tc)
566b22f423SJonathan Anderson {
576b22f423SJonathan Anderson struct descriptors files;
586b22f423SJonathan Anderson char *pathfds;
596b22f423SJonathan Anderson
601a49685cSEnji Cooper setup(&files, tc);
616b22f423SJonathan Anderson ATF_REQUIRE(
626b22f423SJonathan Anderson asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d", files.etc) > 0);
636b22f423SJonathan Anderson
646b22f423SJonathan Anderson expect_missing_library(files.binary, pathfds);
656b22f423SJonathan Anderson }
666b22f423SJonathan Anderson
676b22f423SJonathan Anderson
686b22f423SJonathan Anderson ATF_TC_WITHOUT_HEAD(bad_library_directories);
ATF_TC_BODY(bad_library_directories,tc)696b22f423SJonathan Anderson ATF_TC_BODY(bad_library_directories, tc)
706b22f423SJonathan Anderson {
716b22f423SJonathan Anderson struct descriptors files;
726b22f423SJonathan Anderson char *pathfds;
736b22f423SJonathan Anderson
741a49685cSEnji Cooper setup(&files, tc);
7535e8923eSEnji Cooper ATF_REQUIRE(asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=::") > 0);
766b22f423SJonathan Anderson
776b22f423SJonathan Anderson expect_missing_library(files.binary, pathfds);
786b22f423SJonathan Anderson }
796b22f423SJonathan Anderson
806b22f423SJonathan Anderson
816b22f423SJonathan Anderson ATF_TC_WITHOUT_HEAD(single_library_directory);
ATF_TC_BODY(single_library_directory,tc)826b22f423SJonathan Anderson ATF_TC_BODY(single_library_directory, tc)
836b22f423SJonathan Anderson {
846b22f423SJonathan Anderson struct descriptors files;
856b22f423SJonathan Anderson char *pathfds;
866b22f423SJonathan Anderson
871a49685cSEnji Cooper setup(&files, tc);
886b22f423SJonathan Anderson ATF_REQUIRE(
896b22f423SJonathan Anderson asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d", files.testdir) > 0);
906b22f423SJonathan Anderson
916b22f423SJonathan Anderson expect_success(files.binary, pathfds);
926b22f423SJonathan Anderson }
936b22f423SJonathan Anderson
946b22f423SJonathan Anderson
956b22f423SJonathan Anderson ATF_TC_WITHOUT_HEAD(first_library_directory);
ATF_TC_BODY(first_library_directory,tc)966b22f423SJonathan Anderson ATF_TC_BODY(first_library_directory, tc)
976b22f423SJonathan Anderson {
986b22f423SJonathan Anderson struct descriptors files;
996b22f423SJonathan Anderson char *pathfds;
1006b22f423SJonathan Anderson
1011a49685cSEnji Cooper setup(&files, tc);
1026b22f423SJonathan Anderson ATF_REQUIRE(
1036b22f423SJonathan Anderson asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d",
1046b22f423SJonathan Anderson files.testdir, files.etc) > 0);
1056b22f423SJonathan Anderson
1066b22f423SJonathan Anderson expect_success(files.binary, pathfds);
1076b22f423SJonathan Anderson }
1086b22f423SJonathan Anderson
1096b22f423SJonathan Anderson
1106b22f423SJonathan Anderson ATF_TC_WITHOUT_HEAD(middle_library_directory);
ATF_TC_BODY(middle_library_directory,tc)1116b22f423SJonathan Anderson ATF_TC_BODY(middle_library_directory, tc)
1126b22f423SJonathan Anderson {
1136b22f423SJonathan Anderson struct descriptors files;
1146b22f423SJonathan Anderson char *pathfds;
1156b22f423SJonathan Anderson
1161a49685cSEnji Cooper setup(&files, tc);
1176b22f423SJonathan Anderson ATF_REQUIRE(
1186b22f423SJonathan Anderson asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d:%d",
1196b22f423SJonathan Anderson files.root, files.testdir, files.usr) > 0);
1206b22f423SJonathan Anderson
1216b22f423SJonathan Anderson expect_success(files.binary, pathfds);
1226b22f423SJonathan Anderson }
1236b22f423SJonathan Anderson
1246b22f423SJonathan Anderson
1256b22f423SJonathan Anderson ATF_TC_WITHOUT_HEAD(last_library_directory);
ATF_TC_BODY(last_library_directory,tc)1266b22f423SJonathan Anderson ATF_TC_BODY(last_library_directory, tc)
1276b22f423SJonathan Anderson {
1286b22f423SJonathan Anderson struct descriptors files;
1296b22f423SJonathan Anderson char *pathfds;
1306b22f423SJonathan Anderson
1311a49685cSEnji Cooper setup(&files, tc);
1326b22f423SJonathan Anderson ATF_REQUIRE(
1336b22f423SJonathan Anderson asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d",
1346b22f423SJonathan Anderson files.root, files.testdir) > 0);
1356b22f423SJonathan Anderson
1366b22f423SJonathan Anderson expect_success(files.binary, pathfds);
1376b22f423SJonathan Anderson }
1386b22f423SJonathan Anderson
1396b22f423SJonathan Anderson
1406b22f423SJonathan Anderson
1416b22f423SJonathan Anderson /* Register test cases with ATF. */
ATF_TP_ADD_TCS(tp)1426b22f423SJonathan Anderson ATF_TP_ADD_TCS(tp)
1436b22f423SJonathan Anderson {
1446b22f423SJonathan Anderson ATF_TP_ADD_TC(tp, missing_library);
1456b22f423SJonathan Anderson ATF_TP_ADD_TC(tp, wrong_library_directories);
1466b22f423SJonathan Anderson ATF_TP_ADD_TC(tp, bad_library_directories);
1476b22f423SJonathan Anderson ATF_TP_ADD_TC(tp, single_library_directory);
1486b22f423SJonathan Anderson ATF_TP_ADD_TC(tp, first_library_directory);
1496b22f423SJonathan Anderson ATF_TP_ADD_TC(tp, middle_library_directory);
1506b22f423SJonathan Anderson ATF_TP_ADD_TC(tp, last_library_directory);
1516b22f423SJonathan Anderson
1526b22f423SJonathan Anderson return atf_no_error();
1536b22f423SJonathan Anderson }
1546b22f423SJonathan Anderson
1556b22f423SJonathan Anderson
1566b22f423SJonathan Anderson static void
setup(struct descriptors * dp,const atf_tc_t * tc)1571a49685cSEnji Cooper setup(struct descriptors *dp, const atf_tc_t *tc)
1586b22f423SJonathan Anderson {
1596b22f423SJonathan Anderson
160*e34c713bSAlex Richardson dp->testdir = opendir_fd(atf_tc_get_config_var(tc, "srcdir"));
1611a49685cSEnji Cooper ATF_REQUIRE(dp->testdir >= 0);
1626b22f423SJonathan Anderson ATF_REQUIRE(
163f9021888SMariusz Zaborski (dp->binary = openat(dp->testdir, TARGET_ELF_NAME, O_RDONLY)) >= 0);
1646b22f423SJonathan Anderson
165*e34c713bSAlex Richardson ATF_REQUIRE((dp->root = opendir_fd("/")) >= 0);
1666b22f423SJonathan Anderson ATF_REQUIRE((dp->etc = opendirat(dp->root, "etc")) >= 0);
1676b22f423SJonathan Anderson ATF_REQUIRE((dp->usr = opendirat(dp->root, "usr")) >= 0);
1686b22f423SJonathan Anderson }
1696b22f423SJonathan Anderson
170