1264737Sjilles/*-
2264737Sjilles * Copyright (c) 2014 Jilles Tjoelker
3264737Sjilles * All rights reserved.
4264737Sjilles *
5264737Sjilles * Redistribution and use in source and binary forms, with or without
6264737Sjilles * modification, are permitted provided that the following conditions
7264737Sjilles * are met:
8264737Sjilles * 1. Redistributions of source code must retain the above copyright
9264737Sjilles *    notice, this list of conditions and the following disclaimer.
10264737Sjilles * 2. Redistributions in binary form must reproduce the above copyright
11264737Sjilles *    notice, this list of conditions and the following disclaimer in the
12264737Sjilles *    documentation and/or other materials provided with the distribution.
13264737Sjilles *
14264737Sjilles * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15264737Sjilles * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16264737Sjilles * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17264737Sjilles * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18264737Sjilles * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19264737Sjilles * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20264737Sjilles * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21264737Sjilles * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22264737Sjilles * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23264737Sjilles * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24264737Sjilles * SUCH DAMAGE.
25264737Sjilles */
26264737Sjilles
27264737Sjilles#include <sys/cdefs.h>
28264737Sjilles__FBSDID("$FreeBSD$");
29264737Sjilles
30290537Sngie#include <errno.h>
31290537Sngie#include <paths.h>
32290537Sngie#include <stdbool.h>
33290537Sngie#include <stdio.h>
34290537Sngie#include <string.h>
35264737Sjilles
36290537Sngie#include <atf-c.h>
37264737Sjilles
38264737Sjillesstatic void
39264737Sjillesruntest(const char *fname1, const char *mode1, const char *fname2,
40264737Sjilles    const char *mode2, bool success)
41264737Sjilles{
42264737Sjilles	FILE *fp1, *fp2;
43264737Sjilles	const char *fname2_print;
44264737Sjilles
45264737Sjilles	fname2_print = fname2 != NULL ? fname2 : "<NULL>";
46264737Sjilles	fp1 = fopen(fname1, mode1);
47290537Sngie	ATF_REQUIRE_MSG(fp1 != NULL,
48290537Sngie	    "fopen(\"%s\", \"%s\") failed; errno=%d", fname1, mode1, errno);
49264737Sjilles	fp2 = freopen(fname2, mode2, fp1);
50264737Sjilles	if (fp2 == NULL) {
51290537Sngie		ATF_REQUIRE_MSG(success == false,
52290537Sngie		    "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) succeeded "
53290537Sngie		    "unexpectedly", fname2_print, mode2, fname1, mode1);
54264737Sjilles		return;
55264737Sjilles	}
56290537Sngie	ATF_REQUIRE_MSG(success == true,
57290537Sngie	    "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) failed: %d",
58290537Sngie	    fname2_print, mode2, fname1, mode1, errno);
59264737Sjilles	fclose(fp2);
60264737Sjilles}
61264737Sjilles
62290537SngieATF_TC_WITHOUT_HEAD(null__r__r__test);
63290537SngieATF_TC_BODY(null__r__r__test, tc)
64264737Sjilles{
65290537Sngie
66290537Sngie	runtest(_PATH_DEVNULL, "r", NULL, "r", true);
67290537Sngie}
68290537Sngie
69290537SngieATF_TC_WITHOUT_HEAD(null__w__r__test);
70290537SngieATF_TC_BODY(null__w__r__test, tc)
71290537Sngie{
72290537Sngie
73290537Sngie	runtest(_PATH_DEVNULL, "w", NULL, "r", false);
74290537Sngie}
75290537Sngie
76290537SngieATF_TC_WITHOUT_HEAD(null__r_append__r__test);
77290537SngieATF_TC_BODY(null__r_append__r__test, tc)
78290537Sngie{
79290537Sngie
80290537Sngie	runtest(_PATH_DEVNULL, "r+", NULL, "r", true);
81290537Sngie}
82290537Sngie
83290537SngieATF_TC_WITHOUT_HEAD(null__r__w__test);
84290537SngieATF_TC_BODY(null__r__w__test, tc)
85290537Sngie{
86290537Sngie
87290537Sngie	runtest(_PATH_DEVNULL, "r", NULL, "w", false);
88290537Sngie}
89290537Sngie
90290537SngieATF_TC_WITHOUT_HEAD(null__w__w__test);
91290537SngieATF_TC_BODY(null__w__w__test, tc)
92290537Sngie{
93290537Sngie
94290537Sngie	runtest(_PATH_DEVNULL, "w", NULL, "w", true);
95290537Sngie}
96290537Sngie
97290537SngieATF_TC_WITHOUT_HEAD(null__r_append__w__test);
98290537SngieATF_TC_BODY(null__r_append__w__test, tc)
99290537Sngie{
100290537Sngie
101290537Sngie	runtest(_PATH_DEVNULL, "r+", NULL, "w", true);
102290537Sngie}
103290537Sngie
104290537SngieATF_TC_WITHOUT_HEAD(null__r__a__test);
105290537SngieATF_TC_BODY(null__r__a__test, tc)
106290537Sngie{
107290537Sngie
108290537Sngie	runtest(_PATH_DEVNULL, "r", NULL, "a", false);
109290537Sngie}
110290537Sngie
111290537SngieATF_TC_WITHOUT_HEAD(null__w__a__test);
112290537SngieATF_TC_BODY(null__w__a__test, tc)
113290537Sngie{
114290537Sngie
115290537Sngie	runtest(_PATH_DEVNULL, "w", NULL, "a", true);
116290537Sngie}
117290537Sngie
118290537SngieATF_TC_WITHOUT_HEAD(null__r_append__a__test);
119290537SngieATF_TC_BODY(null__r_append__a__test, tc)
120290537Sngie{
121290537Sngie
122290537Sngie	runtest(_PATH_DEVNULL, "r+", NULL, "a", true);
123290537Sngie}
124290537Sngie
125290537SngieATF_TC_WITHOUT_HEAD(null__r__r_append__test);
126290537SngieATF_TC_BODY(null__r__r_append__test, tc)
127290537Sngie{
128290537Sngie
129290537Sngie	runtest(_PATH_DEVNULL, "r", NULL, "r+", false);
130290537Sngie}
131290537Sngie
132290537SngieATF_TC_WITHOUT_HEAD(null__w__r_append__test);
133290537SngieATF_TC_BODY(null__w__r_append__test, tc)
134290537Sngie{
135290537Sngie
136290537Sngie	runtest(_PATH_DEVNULL, "w", NULL, "r+", false);
137290537Sngie}
138290537Sngie
139290537SngieATF_TC_WITHOUT_HEAD(null__r_append__r_append__test);
140290537SngieATF_TC_BODY(null__r_append__r_append__test, tc)
141290537Sngie{
142290537Sngie
143290537Sngie	runtest(_PATH_DEVNULL, "r+", NULL, "r+", true);
144290537Sngie}
145290537Sngie
146290537SngieATF_TC_WITHOUT_HEAD(null__r__w_append__test);
147290537SngieATF_TC_BODY(null__r__w_append__test, tc)
148290537Sngie{
149290537Sngie
150290537Sngie	runtest(_PATH_DEVNULL, "r", NULL, "w+", false);
151290537Sngie}
152290537Sngie
153290537SngieATF_TC_WITHOUT_HEAD(null__w__w_append__test);
154290537SngieATF_TC_BODY(null__w__w_append__test, tc)
155290537Sngie{
156290537Sngie
157290537Sngie	runtest(_PATH_DEVNULL, "w", NULL, "w+", false);
158290537Sngie}
159290537Sngie
160290537SngieATF_TC_WITHOUT_HEAD(null__r_append__w_append__test);
161290537SngieATF_TC_BODY(null__r_append__w_append__test, tc)
162290537Sngie{
163290537Sngie
164290537Sngie	runtest(_PATH_DEVNULL, "r+", NULL, "w+", true);
165290537Sngie}
166290537Sngie
167290537SngieATF_TC_WITHOUT_HEAD(sh__r__r__test);
168290537SngieATF_TC_BODY(sh__r__r__test, tc)
169290537Sngie{
170290537Sngie
171264737Sjilles	runtest("/bin/sh", "r", NULL, "r", true);
172290537Sngie}
173290537Sngie
174290537SngieATF_TC_WITHOUT_HEAD(sh__sh__r__r__test);
175290537SngieATF_TC_BODY(sh__sh__r__r__test, tc)
176290537Sngie{
177290537Sngie
178264737Sjilles	runtest("/bin/sh", "r", "/bin/sh", "r", true);
179290537Sngie}
180264737Sjilles
181290537SngieATF_TC_WITHOUT_HEAD(sh__null__r__r__test);
182290537SngieATF_TC_BODY(sh__null__r__r__test, tc)
183290537Sngie{
184290537Sngie
185290537Sngie	runtest("/bin/sh", "r", _PATH_DEVNULL, "r", true);
186264737Sjilles}
187264737Sjilles
188290537SngieATF_TC_WITHOUT_HEAD(sh__null__r__w__test);
189290537SngieATF_TC_BODY(sh__null__r__w__test, tc)
190290537Sngie{
191290537Sngie
192290537Sngie	runtest("/bin/sh", "r", _PATH_DEVNULL, "w", true);
193290537Sngie}
194290537Sngie
195290537SngieATF_TP_ADD_TCS(tp)
196290537Sngie{
197290537Sngie
198290537Sngie	ATF_TP_ADD_TC(tp, null__r__r__test);
199290537Sngie	ATF_TP_ADD_TC(tp, null__w__r__test);
200290537Sngie	ATF_TP_ADD_TC(tp, null__r_append__r__test);
201290537Sngie	ATF_TP_ADD_TC(tp, null__r__w__test);
202290537Sngie	ATF_TP_ADD_TC(tp, null__w__w__test);
203290537Sngie	ATF_TP_ADD_TC(tp, null__r_append__w__test);
204290537Sngie	ATF_TP_ADD_TC(tp, null__r__a__test);
205290537Sngie	ATF_TP_ADD_TC(tp, null__w__a__test);
206290537Sngie	ATF_TP_ADD_TC(tp, null__r_append__a__test);
207290537Sngie	ATF_TP_ADD_TC(tp, null__r__r_append__test);
208290537Sngie	ATF_TP_ADD_TC(tp, null__w__r_append__test);
209290537Sngie	ATF_TP_ADD_TC(tp, null__r_append__r_append__test);
210290537Sngie	ATF_TP_ADD_TC(tp, null__r__w_append__test);
211290537Sngie	ATF_TP_ADD_TC(tp, null__w__w_append__test);
212290537Sngie	ATF_TP_ADD_TC(tp, null__r_append__w_append__test);
213290537Sngie	ATF_TP_ADD_TC(tp, sh__r__r__test);
214290537Sngie	ATF_TP_ADD_TC(tp, sh__sh__r__r__test);
215290537Sngie	ATF_TP_ADD_TC(tp, sh__null__r__r__test);
216290537Sngie	ATF_TP_ADD_TC(tp, sh__null__r__w__test);
217290537Sngie
218290537Sngie	return (atf_no_error());
219290537Sngie}
220290537Sngie
221290537Sngie/*
222290537Sngie vim:ts=8:cin:sw=8
223290537Sngie */
224