xref: /freebsd/lib/libc/tests/stdio/freopen_test.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
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 <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <errno.h>
31 #include <paths.h>
32 #include <stdbool.h>
33 #include <stdio.h>
34 #include <string.h>
35 
36 #include <atf-c.h>
37 
38 static void
39 runtest(const char *fname1, const char *mode1, const char *fname2,
40     const char *mode2, bool success)
41 {
42 	FILE *fp1, *fp2;
43 	const char *fname2_print;
44 
45 	fname2_print = fname2 != NULL ? fname2 : "<NULL>";
46 	fp1 = fopen(fname1, mode1);
47 	ATF_REQUIRE_MSG(fp1 != NULL,
48 	    "fopen(\"%s\", \"%s\") failed; errno=%d", fname1, mode1, errno);
49 	fp2 = freopen(fname2, mode2, fp1);
50 	if (fp2 == NULL) {
51 		fclose(fp1);
52 		ATF_REQUIRE_MSG(success == false,
53 		    "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) succeeded "
54 		    "unexpectedly", fname2_print, mode2, fname1, mode1);
55 		return;
56 	}
57 	ATF_REQUIRE_MSG(success == true,
58 	    "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) failed: %d",
59 	    fname2_print, mode2, fname1, mode1, errno);
60 	fclose(fp2);
61 }
62 
63 ATF_TC_WITHOUT_HEAD(null__r__r__test);
64 ATF_TC_BODY(null__r__r__test, tc)
65 {
66 
67 	runtest(_PATH_DEVNULL, "r", NULL, "r", true);
68 }
69 
70 ATF_TC_WITHOUT_HEAD(null__w__r__test);
71 ATF_TC_BODY(null__w__r__test, tc)
72 {
73 
74 	runtest(_PATH_DEVNULL, "w", NULL, "r", false);
75 }
76 
77 ATF_TC_WITHOUT_HEAD(null__r_append__r__test);
78 ATF_TC_BODY(null__r_append__r__test, tc)
79 {
80 
81 	runtest(_PATH_DEVNULL, "r+", NULL, "r", true);
82 }
83 
84 ATF_TC_WITHOUT_HEAD(null__r__w__test);
85 ATF_TC_BODY(null__r__w__test, tc)
86 {
87 
88 	runtest(_PATH_DEVNULL, "r", NULL, "w", false);
89 }
90 
91 ATF_TC_WITHOUT_HEAD(null__w__w__test);
92 ATF_TC_BODY(null__w__w__test, tc)
93 {
94 
95 	runtest(_PATH_DEVNULL, "w", NULL, "w", true);
96 }
97 
98 ATF_TC_WITHOUT_HEAD(null__r_append__w__test);
99 ATF_TC_BODY(null__r_append__w__test, tc)
100 {
101 
102 	runtest(_PATH_DEVNULL, "r+", NULL, "w", true);
103 }
104 
105 ATF_TC_WITHOUT_HEAD(null__r__a__test);
106 ATF_TC_BODY(null__r__a__test, tc)
107 {
108 
109 	runtest(_PATH_DEVNULL, "r", NULL, "a", false);
110 }
111 
112 ATF_TC_WITHOUT_HEAD(null__w__a__test);
113 ATF_TC_BODY(null__w__a__test, tc)
114 {
115 
116 	runtest(_PATH_DEVNULL, "w", NULL, "a", true);
117 }
118 
119 ATF_TC_WITHOUT_HEAD(null__r_append__a__test);
120 ATF_TC_BODY(null__r_append__a__test, tc)
121 {
122 
123 	runtest(_PATH_DEVNULL, "r+", NULL, "a", true);
124 }
125 
126 ATF_TC_WITHOUT_HEAD(null__r__r_append__test);
127 ATF_TC_BODY(null__r__r_append__test, tc)
128 {
129 
130 	runtest(_PATH_DEVNULL, "r", NULL, "r+", false);
131 }
132 
133 ATF_TC_WITHOUT_HEAD(null__w__r_append__test);
134 ATF_TC_BODY(null__w__r_append__test, tc)
135 {
136 
137 	runtest(_PATH_DEVNULL, "w", NULL, "r+", false);
138 }
139 
140 ATF_TC_WITHOUT_HEAD(null__r_append__r_append__test);
141 ATF_TC_BODY(null__r_append__r_append__test, tc)
142 {
143 
144 	runtest(_PATH_DEVNULL, "r+", NULL, "r+", true);
145 }
146 
147 ATF_TC_WITHOUT_HEAD(null__r__w_append__test);
148 ATF_TC_BODY(null__r__w_append__test, tc)
149 {
150 
151 	runtest(_PATH_DEVNULL, "r", NULL, "w+", false);
152 }
153 
154 ATF_TC_WITHOUT_HEAD(null__w__w_append__test);
155 ATF_TC_BODY(null__w__w_append__test, tc)
156 {
157 
158 	runtest(_PATH_DEVNULL, "w", NULL, "w+", false);
159 }
160 
161 ATF_TC_WITHOUT_HEAD(null__r_append__w_append__test);
162 ATF_TC_BODY(null__r_append__w_append__test, tc)
163 {
164 
165 	runtest(_PATH_DEVNULL, "r+", NULL, "w+", true);
166 }
167 
168 ATF_TC_WITHOUT_HEAD(sh__r__r__test);
169 ATF_TC_BODY(sh__r__r__test, tc)
170 {
171 
172 	runtest("/bin/sh", "r", NULL, "r", true);
173 }
174 
175 ATF_TC_WITHOUT_HEAD(sh__sh__r__r__test);
176 ATF_TC_BODY(sh__sh__r__r__test, tc)
177 {
178 
179 	runtest("/bin/sh", "r", "/bin/sh", "r", true);
180 }
181 
182 ATF_TC_WITHOUT_HEAD(sh__null__r__r__test);
183 ATF_TC_BODY(sh__null__r__r__test, tc)
184 {
185 
186 	runtest("/bin/sh", "r", _PATH_DEVNULL, "r", true);
187 }
188 
189 ATF_TC_WITHOUT_HEAD(sh__null__r__w__test);
190 ATF_TC_BODY(sh__null__r__w__test, tc)
191 {
192 
193 	runtest("/bin/sh", "r", _PATH_DEVNULL, "w", true);
194 }
195 
196 ATF_TP_ADD_TCS(tp)
197 {
198 
199 	ATF_TP_ADD_TC(tp, null__r__r__test);
200 	ATF_TP_ADD_TC(tp, null__w__r__test);
201 	ATF_TP_ADD_TC(tp, null__r_append__r__test);
202 	ATF_TP_ADD_TC(tp, null__r__w__test);
203 	ATF_TP_ADD_TC(tp, null__w__w__test);
204 	ATF_TP_ADD_TC(tp, null__r_append__w__test);
205 	ATF_TP_ADD_TC(tp, null__r__a__test);
206 	ATF_TP_ADD_TC(tp, null__w__a__test);
207 	ATF_TP_ADD_TC(tp, null__r_append__a__test);
208 	ATF_TP_ADD_TC(tp, null__r__r_append__test);
209 	ATF_TP_ADD_TC(tp, null__w__r_append__test);
210 	ATF_TP_ADD_TC(tp, null__r_append__r_append__test);
211 	ATF_TP_ADD_TC(tp, null__r__w_append__test);
212 	ATF_TP_ADD_TC(tp, null__w__w_append__test);
213 	ATF_TP_ADD_TC(tp, null__r_append__w_append__test);
214 	ATF_TP_ADD_TC(tp, sh__r__r__test);
215 	ATF_TP_ADD_TC(tp, sh__sh__r__r__test);
216 	ATF_TP_ADD_TC(tp, sh__null__r__r__test);
217 	ATF_TP_ADD_TC(tp, sh__null__r__w__test);
218 
219 	return (atf_no_error());
220 }
221 
222 /*
223  vim:ts=8:cin:sw=8
224  */
225