xref: /freebsd/lib/libc/tests/stdio/fdopen_test.c (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
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 <fcntl.h>
32 #include <paths.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 #include <atf-c.h>
39 
40 static void
41 runtest(const char *fname, int intmode, const char *strmode, bool success)
42 {
43 	FILE *fp;
44 	int fd;
45 
46 	fd = open(fname, intmode);
47 	ATF_REQUIRE_MSG(fd != -1,
48 	    "open(\"%s\", %#x) failed; errno=%d", fname, intmode, errno);
49 
50 	fp = fdopen(fd, strmode);
51 	if (fp == NULL) {
52 		close(fd);
53 		ATF_REQUIRE_MSG(success == false,
54 		    "fdopen(open(\"%s\", %#x), \"%s\") succeeded unexpectedly",
55 		    fname, intmode, strmode);
56 		return;
57 	}
58 	ATF_REQUIRE_MSG(success == true,
59 	    "fdopen(open(\"%s\", %#x), \"%s\") failed; errno=%d",
60 	    fname, intmode, strmode, errno);
61 	fclose(fp);
62 }
63 
64 ATF_TC_WITHOUT_HEAD(null__O_RDONLY__r_test);
65 ATF_TC_BODY(null__O_RDONLY__r_test, tc)
66 {
67 
68 	runtest(_PATH_DEVNULL, O_RDONLY, "r", true);
69 }
70 
71 ATF_TC_WITHOUT_HEAD(null__O_WRONLY__r_test);
72 ATF_TC_BODY(null__O_WRONLY__r_test, tc)
73 {
74 
75 	runtest(_PATH_DEVNULL, O_WRONLY, "r", false);
76 }
77 
78 ATF_TC_WITHOUT_HEAD(null__O_RDWR__r_test);
79 ATF_TC_BODY(null__O_RDWR__r_test, tc)
80 {
81 
82 	runtest(_PATH_DEVNULL, O_RDWR, "r", true);
83 }
84 
85 ATF_TC_WITHOUT_HEAD(null__O_RDONLY__w_test);
86 ATF_TC_BODY(null__O_RDONLY__w_test, tc)
87 {
88 
89 	runtest(_PATH_DEVNULL, O_RDONLY, "w", false);
90 }
91 
92 ATF_TC_WITHOUT_HEAD(null__O_WRONLY__w_test);
93 ATF_TC_BODY(null__O_WRONLY__w_test, tc)
94 {
95 
96 	runtest(_PATH_DEVNULL, O_WRONLY, "w", true);
97 }
98 
99 ATF_TC_WITHOUT_HEAD(null__O_RDWR__w_test);
100 ATF_TC_BODY(null__O_RDWR__w_test, tc)
101 {
102 
103 	runtest(_PATH_DEVNULL, O_RDWR, "w", true);
104 }
105 
106 ATF_TC_WITHOUT_HEAD(null__O_RDONLY__a_test);
107 ATF_TC_BODY(null__O_RDONLY__a_test, tc)
108 {
109 
110 	runtest(_PATH_DEVNULL, O_RDONLY, "a", false);
111 }
112 
113 ATF_TC_WITHOUT_HEAD(null__O_WRONLY__a_test);
114 ATF_TC_BODY(null__O_WRONLY__a_test, tc)
115 {
116 
117 	runtest(_PATH_DEVNULL, O_WRONLY, "a", true);
118 }
119 
120 ATF_TC_WITHOUT_HEAD(null__O_RDWR__test);
121 ATF_TC_BODY(null__O_RDWR__test, tc)
122 {
123 
124 	runtest(_PATH_DEVNULL, O_RDWR, "a", true);
125 }
126 
127 ATF_TC_WITHOUT_HEAD(null__O_RDONLY__r_append);
128 ATF_TC_BODY(null__O_RDONLY__r_append, tc)
129 {
130 
131 	runtest(_PATH_DEVNULL, O_RDONLY, "r+", false);
132 }
133 
134 ATF_TC_WITHOUT_HEAD(null__O_WRONLY__r_append);
135 ATF_TC_BODY(null__O_WRONLY__r_append, tc)
136 {
137 
138 	runtest(_PATH_DEVNULL, O_WRONLY, "r+", false);
139 }
140 
141 ATF_TC_WITHOUT_HEAD(null__O_RDWR__r_append);
142 ATF_TC_BODY(null__O_RDWR__r_append, tc)
143 {
144 
145 	runtest(_PATH_DEVNULL, O_RDWR, "r+", true);
146 }
147 
148 ATF_TC_WITHOUT_HEAD(null__O_RDONLY__w_append);
149 ATF_TC_BODY(null__O_RDONLY__w_append, tc)
150 {
151 
152 	runtest(_PATH_DEVNULL, O_RDONLY, "w+", false);
153 }
154 
155 ATF_TC_WITHOUT_HEAD(null__O_WRONLY__w_append);
156 ATF_TC_BODY(null__O_WRONLY__w_append, tc)
157 {
158 
159 	runtest(_PATH_DEVNULL, O_WRONLY, "w+", false);
160 }
161 
162 ATF_TC_WITHOUT_HEAD(null__O_RDWR__w_append);
163 ATF_TC_BODY(null__O_RDWR__w_append, tc)
164 {
165 
166 	runtest(_PATH_DEVNULL, O_RDWR, "w+", true);
167 }
168 
169 ATF_TC_WITHOUT_HEAD(sh__O_EXEC__r);
170 ATF_TC_BODY(sh__O_EXEC__r, tc)
171 {
172 
173 	runtest("/bin/sh", O_EXEC, "r", false);
174 }
175 
176 ATF_TC_WITHOUT_HEAD(sh__O_EXEC__w);
177 ATF_TC_BODY(sh__O_EXEC__w, tc)
178 {
179 
180 	runtest("/bin/sh", O_EXEC, "w", false);
181 }
182 
183 ATF_TC_WITHOUT_HEAD(sh__O_EXEC__r_append);
184 ATF_TC_BODY(sh__O_EXEC__r_append, tc)
185 {
186 
187 	runtest("/bin/sh", O_EXEC, "r+", false);
188 }
189 
190 ATF_TC_WITHOUT_HEAD(sh__O_EXEC__w_append);
191 ATF_TC_BODY(sh__O_EXEC__w_append, tc)
192 {
193 
194 	runtest("/bin/sh", O_EXEC, "w+", false);
195 }
196 
197 ATF_TP_ADD_TCS(tp)
198 {
199 
200 	ATF_TP_ADD_TC(tp, null__O_RDONLY__r_test);
201 	ATF_TP_ADD_TC(tp, null__O_WRONLY__r_test);
202 	ATF_TP_ADD_TC(tp, null__O_RDWR__r_test);
203 	ATF_TP_ADD_TC(tp, null__O_RDONLY__w_test);
204 	ATF_TP_ADD_TC(tp, null__O_WRONLY__w_test);
205 	ATF_TP_ADD_TC(tp, null__O_RDWR__w_test);
206 	ATF_TP_ADD_TC(tp, null__O_RDONLY__a_test);
207 	ATF_TP_ADD_TC(tp, null__O_WRONLY__a_test);
208 	ATF_TP_ADD_TC(tp, null__O_RDWR__test);
209 	ATF_TP_ADD_TC(tp, null__O_RDONLY__r_append);
210 	ATF_TP_ADD_TC(tp, null__O_WRONLY__r_append);
211 	ATF_TP_ADD_TC(tp, null__O_RDWR__r_append);
212 	ATF_TP_ADD_TC(tp, null__O_RDONLY__w_append);
213 	ATF_TP_ADD_TC(tp, null__O_WRONLY__w_append);
214 	ATF_TP_ADD_TC(tp, null__O_RDWR__w_append);
215 	ATF_TP_ADD_TC(tp, sh__O_EXEC__r);
216 	ATF_TP_ADD_TC(tp, sh__O_EXEC__w);
217 	ATF_TP_ADD_TC(tp, sh__O_EXEC__r_append);
218 	ATF_TP_ADD_TC(tp, sh__O_EXEC__w_append);
219 
220 	return (atf_no_error());
221 }
222 
223 /*
224  vim:ts=8:cin:sw=8
225  */
226