1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 * Copyright 2021 Mariusz Zaborski <oshogbo@FreeBSD.org>
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
32int binaryfd;
33int libraryfd;
34
35static void
36setup(const atf_tc_t *tc)
37{
38	int testdir;
39
40	testdir = opendir_fd(atf_tc_get_config_var(tc, "srcdir"));
41	ATF_REQUIRE(testdir >= 0);
42
43	binaryfd = openat(testdir, TARGET_ELF_NAME, O_RDONLY);
44	ATF_REQUIRE(binaryfd >= 0);
45	libraryfd = openat(testdir, TARGET_LIBRARY, O_RDONLY);
46	ATF_REQUIRE(libraryfd >= 0);
47
48	close(testdir);
49}
50
51ATF_TC_WITHOUT_HEAD(missing_library);
52ATF_TC_BODY(missing_library, tc)
53{
54
55	setup(tc);
56	expect_missing_library(binaryfd, NULL);
57}
58
59ATF_TC_WITHOUT_HEAD(bad_librarys);
60ATF_TC_BODY(bad_librarys, tc)
61{
62	char *senv;
63
64	ATF_REQUIRE(asprintf(&senv, "LD_PRELOAD_FDS=::") > 0);
65
66	setup(tc);
67	expect_missing_library(binaryfd, senv);
68}
69
70ATF_TC_WITHOUT_HEAD(single_library);
71ATF_TC_BODY(single_library, tc)
72{
73	char *senv;
74
75	setup(tc);
76
77	ATF_REQUIRE(
78	    asprintf(&senv, "LD_PRELOAD_FDS=%d", libraryfd) > 0);
79
80	expect_success(binaryfd, senv);
81}
82
83ATF_TC_WITHOUT_HEAD(two_librarys);
84ATF_TC_BODY(two_librarys, tc)
85{
86	char *senv;
87
88	setup(tc);
89
90	ATF_REQUIRE(
91	    asprintf(&senv, "LD_PRELOAD_FDS=%d:%d", libraryfd, libraryfd) > 0);
92
93	expect_success(binaryfd, senv);
94}
95
96/* Register test cases with ATF. */
97ATF_TP_ADD_TCS(tp)
98{
99
100	ATF_TP_ADD_TC(tp, missing_library);
101	ATF_TP_ADD_TC(tp, bad_librarys);
102	ATF_TP_ADD_TC(tp, single_library);
103	ATF_TP_ADD_TC(tp, two_librarys);
104
105	return atf_no_error();
106}
107