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