1 /*-
2 * Copyright (c) 2014 Jilles Tjoelker
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 AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <errno.h>
28 #include <paths.h>
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include <atf-c.h>
34
35 static void
runtest(const char * fname1,const char * mode1,const char * fname2,const char * mode2,bool success)36 runtest(const char *fname1, const char *mode1, const char *fname2,
37 const char *mode2, bool success)
38 {
39 FILE *fp1, *fp2;
40 const char *fname2_print;
41
42 fname2_print = fname2 != NULL ? fname2 : "<NULL>";
43 fp1 = fopen(fname1, mode1);
44 ATF_REQUIRE_MSG(fp1 != NULL,
45 "fopen(\"%s\", \"%s\") failed; errno=%d", fname1, mode1, errno);
46 fp2 = freopen(fname2, mode2, fp1);
47 if (fp2 == NULL) {
48 ATF_REQUIRE_MSG(success == false,
49 "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) succeeded "
50 "unexpectedly", fname2_print, mode2, fname1, mode1);
51 return;
52 }
53 ATF_REQUIRE_MSG(success == true,
54 "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) failed: %d",
55 fname2_print, mode2, fname1, mode1, errno);
56 fclose(fp2);
57 }
58
59 ATF_TC_WITHOUT_HEAD(null__r__r__test);
ATF_TC_BODY(null__r__r__test,tc)60 ATF_TC_BODY(null__r__r__test, tc)
61 {
62
63 runtest(_PATH_DEVNULL, "r", NULL, "r", true);
64 }
65
66 ATF_TC_WITHOUT_HEAD(null__w__r__test);
ATF_TC_BODY(null__w__r__test,tc)67 ATF_TC_BODY(null__w__r__test, tc)
68 {
69
70 runtest(_PATH_DEVNULL, "w", NULL, "r", false);
71 }
72
73 ATF_TC_WITHOUT_HEAD(null__r_append__r__test);
ATF_TC_BODY(null__r_append__r__test,tc)74 ATF_TC_BODY(null__r_append__r__test, tc)
75 {
76
77 runtest(_PATH_DEVNULL, "r+", NULL, "r", true);
78 }
79
80 ATF_TC_WITHOUT_HEAD(null__r__w__test);
ATF_TC_BODY(null__r__w__test,tc)81 ATF_TC_BODY(null__r__w__test, tc)
82 {
83
84 runtest(_PATH_DEVNULL, "r", NULL, "w", false);
85 }
86
87 ATF_TC_WITHOUT_HEAD(null__w__w__test);
ATF_TC_BODY(null__w__w__test,tc)88 ATF_TC_BODY(null__w__w__test, tc)
89 {
90
91 runtest(_PATH_DEVNULL, "w", NULL, "w", true);
92 }
93
94 ATF_TC_WITHOUT_HEAD(null__r_append__w__test);
ATF_TC_BODY(null__r_append__w__test,tc)95 ATF_TC_BODY(null__r_append__w__test, tc)
96 {
97
98 runtest(_PATH_DEVNULL, "r+", NULL, "w", true);
99 }
100
101 ATF_TC_WITHOUT_HEAD(null__r__a__test);
ATF_TC_BODY(null__r__a__test,tc)102 ATF_TC_BODY(null__r__a__test, tc)
103 {
104
105 runtest(_PATH_DEVNULL, "r", NULL, "a", false);
106 }
107
108 ATF_TC_WITHOUT_HEAD(null__w__a__test);
ATF_TC_BODY(null__w__a__test,tc)109 ATF_TC_BODY(null__w__a__test, tc)
110 {
111
112 runtest(_PATH_DEVNULL, "w", NULL, "a", true);
113 }
114
115 ATF_TC_WITHOUT_HEAD(null__r_append__a__test);
ATF_TC_BODY(null__r_append__a__test,tc)116 ATF_TC_BODY(null__r_append__a__test, tc)
117 {
118
119 runtest(_PATH_DEVNULL, "r+", NULL, "a", true);
120 }
121
122 ATF_TC_WITHOUT_HEAD(null__r__r_append__test);
ATF_TC_BODY(null__r__r_append__test,tc)123 ATF_TC_BODY(null__r__r_append__test, tc)
124 {
125
126 runtest(_PATH_DEVNULL, "r", NULL, "r+", false);
127 }
128
129 ATF_TC_WITHOUT_HEAD(null__w__r_append__test);
ATF_TC_BODY(null__w__r_append__test,tc)130 ATF_TC_BODY(null__w__r_append__test, tc)
131 {
132
133 runtest(_PATH_DEVNULL, "w", NULL, "r+", false);
134 }
135
136 ATF_TC_WITHOUT_HEAD(null__r_append__r_append__test);
ATF_TC_BODY(null__r_append__r_append__test,tc)137 ATF_TC_BODY(null__r_append__r_append__test, tc)
138 {
139
140 runtest(_PATH_DEVNULL, "r+", NULL, "r+", true);
141 }
142
143 ATF_TC_WITHOUT_HEAD(null__r__w_append__test);
ATF_TC_BODY(null__r__w_append__test,tc)144 ATF_TC_BODY(null__r__w_append__test, tc)
145 {
146
147 runtest(_PATH_DEVNULL, "r", NULL, "w+", false);
148 }
149
150 ATF_TC_WITHOUT_HEAD(null__w__w_append__test);
ATF_TC_BODY(null__w__w_append__test,tc)151 ATF_TC_BODY(null__w__w_append__test, tc)
152 {
153
154 runtest(_PATH_DEVNULL, "w", NULL, "w+", false);
155 }
156
157 ATF_TC_WITHOUT_HEAD(null__r_append__w_append__test);
ATF_TC_BODY(null__r_append__w_append__test,tc)158 ATF_TC_BODY(null__r_append__w_append__test, tc)
159 {
160
161 runtest(_PATH_DEVNULL, "r+", NULL, "w+", true);
162 }
163
164 ATF_TC_WITHOUT_HEAD(sh__r__r__test);
ATF_TC_BODY(sh__r__r__test,tc)165 ATF_TC_BODY(sh__r__r__test, tc)
166 {
167
168 runtest("/bin/sh", "r", NULL, "r", true);
169 }
170
171 ATF_TC_WITHOUT_HEAD(sh__sh__r__r__test);
ATF_TC_BODY(sh__sh__r__r__test,tc)172 ATF_TC_BODY(sh__sh__r__r__test, tc)
173 {
174
175 runtest("/bin/sh", "r", "/bin/sh", "r", true);
176 }
177
178 ATF_TC_WITHOUT_HEAD(sh__null__r__r__test);
ATF_TC_BODY(sh__null__r__r__test,tc)179 ATF_TC_BODY(sh__null__r__r__test, tc)
180 {
181
182 runtest("/bin/sh", "r", _PATH_DEVNULL, "r", true);
183 }
184
185 ATF_TC_WITHOUT_HEAD(sh__null__r__w__test);
ATF_TC_BODY(sh__null__r__w__test,tc)186 ATF_TC_BODY(sh__null__r__w__test, tc)
187 {
188
189 runtest("/bin/sh", "r", _PATH_DEVNULL, "w", true);
190 }
191
ATF_TP_ADD_TCS(tp)192 ATF_TP_ADD_TCS(tp)
193 {
194
195 ATF_TP_ADD_TC(tp, null__r__r__test);
196 ATF_TP_ADD_TC(tp, null__w__r__test);
197 ATF_TP_ADD_TC(tp, null__r_append__r__test);
198 ATF_TP_ADD_TC(tp, null__r__w__test);
199 ATF_TP_ADD_TC(tp, null__w__w__test);
200 ATF_TP_ADD_TC(tp, null__r_append__w__test);
201 ATF_TP_ADD_TC(tp, null__r__a__test);
202 ATF_TP_ADD_TC(tp, null__w__a__test);
203 ATF_TP_ADD_TC(tp, null__r_append__a__test);
204 ATF_TP_ADD_TC(tp, null__r__r_append__test);
205 ATF_TP_ADD_TC(tp, null__w__r_append__test);
206 ATF_TP_ADD_TC(tp, null__r_append__r_append__test);
207 ATF_TP_ADD_TC(tp, null__r__w_append__test);
208 ATF_TP_ADD_TC(tp, null__w__w_append__test);
209 ATF_TP_ADD_TC(tp, null__r_append__w_append__test);
210 ATF_TP_ADD_TC(tp, sh__r__r__test);
211 ATF_TP_ADD_TC(tp, sh__sh__r__r__test);
212 ATF_TP_ADD_TC(tp, sh__null__r__r__test);
213 ATF_TP_ADD_TC(tp, sh__null__r__w__test);
214
215 return (atf_no_error());
216 }
217
218 /*
219 vim:ts=8:cin:sw=8
220 */
221