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 * $FreeBSD$ 266b22f423SJonathan Anderson */ 276b22f423SJonathan Anderson 286b22f423SJonathan Anderson #include <atf-c.h> 296b22f423SJonathan Anderson #include <fcntl.h> 306b22f423SJonathan Anderson #include <stdio.h> 316b22f423SJonathan Anderson 32*f9021888SMariusz Zaborski #include "common.h" 336b22f423SJonathan Anderson 346b22f423SJonathan Anderson struct descriptors { 356b22f423SJonathan Anderson int binary; 366b22f423SJonathan Anderson int testdir; 376b22f423SJonathan Anderson int root; 386b22f423SJonathan Anderson int etc; 396b22f423SJonathan Anderson int usr; 406b22f423SJonathan Anderson }; 416b22f423SJonathan Anderson 426b22f423SJonathan Anderson 43*f9021888SMariusz Zaborski static void setup(struct descriptors *, const atf_tc_t *); 446b22f423SJonathan Anderson 456b22f423SJonathan Anderson 466b22f423SJonathan Anderson ATF_TC_WITHOUT_HEAD(missing_library); 476b22f423SJonathan Anderson ATF_TC_BODY(missing_library, tc) 486b22f423SJonathan Anderson { 496b22f423SJonathan Anderson struct descriptors files; 506b22f423SJonathan Anderson 511a49685cSEnji Cooper setup(&files, tc); 526b22f423SJonathan Anderson expect_missing_library(files.binary, NULL); 536b22f423SJonathan Anderson } 546b22f423SJonathan Anderson 556b22f423SJonathan Anderson 566b22f423SJonathan Anderson ATF_TC_WITHOUT_HEAD(wrong_library_directories); 576b22f423SJonathan Anderson ATF_TC_BODY(wrong_library_directories, tc) 586b22f423SJonathan Anderson { 596b22f423SJonathan Anderson struct descriptors files; 606b22f423SJonathan Anderson char *pathfds; 616b22f423SJonathan Anderson 621a49685cSEnji Cooper setup(&files, tc); 636b22f423SJonathan Anderson ATF_REQUIRE( 646b22f423SJonathan Anderson asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d", files.etc) > 0); 656b22f423SJonathan Anderson 666b22f423SJonathan Anderson expect_missing_library(files.binary, pathfds); 676b22f423SJonathan Anderson } 686b22f423SJonathan Anderson 696b22f423SJonathan Anderson 706b22f423SJonathan Anderson ATF_TC_WITHOUT_HEAD(bad_library_directories); 716b22f423SJonathan Anderson ATF_TC_BODY(bad_library_directories, tc) 726b22f423SJonathan Anderson { 736b22f423SJonathan Anderson struct descriptors files; 746b22f423SJonathan Anderson char *pathfds; 756b22f423SJonathan Anderson 761a49685cSEnji Cooper setup(&files, tc); 7735e8923eSEnji Cooper ATF_REQUIRE(asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=::") > 0); 786b22f423SJonathan Anderson 796b22f423SJonathan Anderson expect_missing_library(files.binary, pathfds); 806b22f423SJonathan Anderson } 816b22f423SJonathan Anderson 826b22f423SJonathan Anderson 836b22f423SJonathan Anderson ATF_TC_WITHOUT_HEAD(single_library_directory); 846b22f423SJonathan Anderson ATF_TC_BODY(single_library_directory, tc) 856b22f423SJonathan Anderson { 866b22f423SJonathan Anderson struct descriptors files; 876b22f423SJonathan Anderson char *pathfds; 886b22f423SJonathan Anderson 891a49685cSEnji Cooper setup(&files, tc); 906b22f423SJonathan Anderson ATF_REQUIRE( 916b22f423SJonathan Anderson asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d", files.testdir) > 0); 926b22f423SJonathan Anderson 936b22f423SJonathan Anderson expect_success(files.binary, pathfds); 946b22f423SJonathan Anderson } 956b22f423SJonathan Anderson 966b22f423SJonathan Anderson 976b22f423SJonathan Anderson ATF_TC_WITHOUT_HEAD(first_library_directory); 986b22f423SJonathan Anderson ATF_TC_BODY(first_library_directory, tc) 996b22f423SJonathan Anderson { 1006b22f423SJonathan Anderson struct descriptors files; 1016b22f423SJonathan Anderson char *pathfds; 1026b22f423SJonathan Anderson 1031a49685cSEnji Cooper setup(&files, tc); 1046b22f423SJonathan Anderson ATF_REQUIRE( 1056b22f423SJonathan Anderson asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d", 1066b22f423SJonathan Anderson files.testdir, files.etc) > 0); 1076b22f423SJonathan Anderson 1086b22f423SJonathan Anderson expect_success(files.binary, pathfds); 1096b22f423SJonathan Anderson } 1106b22f423SJonathan Anderson 1116b22f423SJonathan Anderson 1126b22f423SJonathan Anderson ATF_TC_WITHOUT_HEAD(middle_library_directory); 1136b22f423SJonathan Anderson ATF_TC_BODY(middle_library_directory, tc) 1146b22f423SJonathan Anderson { 1156b22f423SJonathan Anderson struct descriptors files; 1166b22f423SJonathan Anderson char *pathfds; 1176b22f423SJonathan Anderson 1181a49685cSEnji Cooper setup(&files, tc); 1196b22f423SJonathan Anderson ATF_REQUIRE( 1206b22f423SJonathan Anderson asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d:%d", 1216b22f423SJonathan Anderson files.root, files.testdir, files.usr) > 0); 1226b22f423SJonathan Anderson 1236b22f423SJonathan Anderson expect_success(files.binary, pathfds); 1246b22f423SJonathan Anderson } 1256b22f423SJonathan Anderson 1266b22f423SJonathan Anderson 1276b22f423SJonathan Anderson ATF_TC_WITHOUT_HEAD(last_library_directory); 1286b22f423SJonathan Anderson ATF_TC_BODY(last_library_directory, tc) 1296b22f423SJonathan Anderson { 1306b22f423SJonathan Anderson struct descriptors files; 1316b22f423SJonathan Anderson char *pathfds; 1326b22f423SJonathan Anderson 1331a49685cSEnji Cooper setup(&files, tc); 1346b22f423SJonathan Anderson ATF_REQUIRE( 1356b22f423SJonathan Anderson asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d", 1366b22f423SJonathan Anderson files.root, files.testdir) > 0); 1376b22f423SJonathan Anderson 1386b22f423SJonathan Anderson expect_success(files.binary, pathfds); 1396b22f423SJonathan Anderson } 1406b22f423SJonathan Anderson 1416b22f423SJonathan Anderson 1426b22f423SJonathan Anderson 1436b22f423SJonathan Anderson /* Register test cases with ATF. */ 1446b22f423SJonathan Anderson ATF_TP_ADD_TCS(tp) 1456b22f423SJonathan Anderson { 1466b22f423SJonathan Anderson ATF_TP_ADD_TC(tp, missing_library); 1476b22f423SJonathan Anderson ATF_TP_ADD_TC(tp, wrong_library_directories); 1486b22f423SJonathan Anderson ATF_TP_ADD_TC(tp, bad_library_directories); 1496b22f423SJonathan Anderson ATF_TP_ADD_TC(tp, single_library_directory); 1506b22f423SJonathan Anderson ATF_TP_ADD_TC(tp, first_library_directory); 1516b22f423SJonathan Anderson ATF_TP_ADD_TC(tp, middle_library_directory); 1526b22f423SJonathan Anderson ATF_TP_ADD_TC(tp, last_library_directory); 1536b22f423SJonathan Anderson 1546b22f423SJonathan Anderson return atf_no_error(); 1556b22f423SJonathan Anderson } 1566b22f423SJonathan Anderson 1576b22f423SJonathan Anderson 1586b22f423SJonathan Anderson static void 1591a49685cSEnji Cooper setup(struct descriptors *dp, const atf_tc_t *tc) 1606b22f423SJonathan Anderson { 1616b22f423SJonathan Anderson 1621a49685cSEnji Cooper dp->testdir = opendir(atf_tc_get_config_var(tc, "srcdir")); 1631a49685cSEnji Cooper ATF_REQUIRE(dp->testdir >= 0); 1646b22f423SJonathan Anderson ATF_REQUIRE( 165*f9021888SMariusz Zaborski (dp->binary = openat(dp->testdir, TARGET_ELF_NAME, O_RDONLY)) >= 0); 1666b22f423SJonathan Anderson 1676b22f423SJonathan Anderson ATF_REQUIRE((dp->root = opendir("/")) >= 0); 1686b22f423SJonathan Anderson ATF_REQUIRE((dp->etc = opendirat(dp->root, "etc")) >= 0); 1696b22f423SJonathan Anderson ATF_REQUIRE((dp->usr = opendirat(dp->root, "usr")) >= 0); 1706b22f423SJonathan Anderson } 1716b22f423SJonathan Anderson 172