xref: /freebsd/lib/libc/tests/stdio/fopen_test.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1 /*-
2  * Copyright (c) 2013 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 <fcntl.h>
31 #include <paths.h>
32 #include <stdio.h>
33 #include <string.h>
34 
35 #include <atf-c.h>
36 
37 /*
38  * O_ACCMODE is currently defined incorrectly. This is what it should be.
39  * Various code depends on the incorrect value.
40  */
41 #define CORRECT_O_ACCMODE (O_ACCMODE | O_EXEC)
42 
43 static void
44 runtest(const char *fname, const char *mode)
45 {
46 	FILE *fp;
47 	int exp_fget_ret, fget_ret, fd, flags, wantedflags;
48 
49 	fp = fopen(fname, mode);
50 	ATF_REQUIRE_MSG(fp != NULL,
51 	    "fopen(\"%s\", \"%s\") failed", fname, mode);
52 	fd = fileno(fp);
53 	ATF_REQUIRE_MSG(fd >= 0, "fileno() failed for fopen");
54 	exp_fget_ret = strchr(mode, 'e') != NULL ? FD_CLOEXEC : 0;
55 	ATF_REQUIRE_MSG((fget_ret = fcntl(fd, F_GETFD)) == exp_fget_ret,
56 	    "fcntl(.., F_GETFD) didn't FD_CLOEXEC as expected %d != %d",
57 	    exp_fget_ret, fget_ret);
58 	flags = fcntl(fd, F_GETFL);
59 	if (strchr(mode, '+'))
60 		wantedflags = O_RDWR | (*mode == 'a' ? O_APPEND : 0);
61 	else if (*mode == 'r')
62 		wantedflags = O_RDONLY;
63 	else if (*mode == 'w')
64 		wantedflags = O_WRONLY;
65 	else if (*mode == 'a')
66 		wantedflags = O_WRONLY | O_APPEND;
67 	else
68 		wantedflags = -1;
69 	fclose(fp);
70 	if (wantedflags == -1)
71 		atf_tc_fail("unrecognized mode: %s", mode);
72 	else if ((flags & (CORRECT_O_ACCMODE | O_APPEND)) != wantedflags)
73 		atf_tc_fail("incorrect access mode: %s", mode);
74 }
75 
76 ATF_TC_WITHOUT_HEAD(fopen_r_test);
77 ATF_TC_BODY(fopen_r_test, tc)
78 {
79 
80 	runtest(_PATH_DEVNULL, "r");
81 }
82 
83 ATF_TC_WITHOUT_HEAD(fopen_r_append_test);
84 ATF_TC_BODY(fopen_r_append_test, tc)
85 {
86 
87 	runtest(_PATH_DEVNULL, "r+");
88 }
89 
90 ATF_TC_WITHOUT_HEAD(fopen_w_test);
91 ATF_TC_BODY(fopen_w_test, tc)
92 {
93 
94 	runtest(_PATH_DEVNULL, "w");
95 }
96 
97 ATF_TC_WITHOUT_HEAD(fopen_w_append_test);
98 ATF_TC_BODY(fopen_w_append_test, tc)
99 {
100 
101 	runtest(_PATH_DEVNULL, "w+");
102 }
103 
104 ATF_TC_WITHOUT_HEAD(fopen_a_test);
105 ATF_TC_BODY(fopen_a_test, tc)
106 {
107 
108 	runtest(_PATH_DEVNULL, "a");
109 }
110 
111 ATF_TC_WITHOUT_HEAD(fopen_a_append_test);
112 ATF_TC_BODY(fopen_a_append_test, tc)
113 {
114 
115 	runtest(_PATH_DEVNULL, "a+");
116 }
117 
118 ATF_TC_WITHOUT_HEAD(fopen_re_test);
119 ATF_TC_BODY(fopen_re_test, tc)
120 {
121 
122 	runtest(_PATH_DEVNULL, "re");
123 }
124 
125 ATF_TC_WITHOUT_HEAD(fopen_r_append_e_test);
126 ATF_TC_BODY(fopen_r_append_e_test, tc)
127 {
128 
129 	runtest(_PATH_DEVNULL, "r+e");
130 }
131 
132 ATF_TC_WITHOUT_HEAD(fopen_we_test);
133 ATF_TC_BODY(fopen_we_test, tc)
134 {
135 
136 	runtest(_PATH_DEVNULL, "we");
137 }
138 
139 ATF_TC_WITHOUT_HEAD(fopen_w_append_e_test);
140 ATF_TC_BODY(fopen_w_append_e_test, tc)
141 {
142 
143 	runtest(_PATH_DEVNULL, "w+e");
144 }
145 
146 ATF_TC_WITHOUT_HEAD(fopen_ae_test);
147 ATF_TC_BODY(fopen_ae_test, tc)
148 {
149 
150 	runtest(_PATH_DEVNULL, "ae");
151 }
152 
153 ATF_TC_WITHOUT_HEAD(fopen_a_append_e_test);
154 ATF_TC_BODY(fopen_a_append_e_test, tc)
155 {
156 
157 	runtest(_PATH_DEVNULL, "a+e");
158 }
159 
160 ATF_TC_WITHOUT_HEAD(fopen_re_append_test);
161 ATF_TC_BODY(fopen_re_append_test, tc)
162 {
163 
164 	runtest(_PATH_DEVNULL, "re+");
165 }
166 
167 ATF_TC_WITHOUT_HEAD(fopen_we_append_test);
168 ATF_TC_BODY(fopen_we_append_test, tc)
169 {
170 
171 	runtest(_PATH_DEVNULL, "we+");
172 }
173 
174 ATF_TC_WITHOUT_HEAD(fopen_ae_append_test);
175 ATF_TC_BODY(fopen_ae_append_test, tc)
176 {
177 
178 	runtest(_PATH_DEVNULL, "ae+");
179 }
180 
181 ATF_TP_ADD_TCS(tp)
182 {
183 
184 	ATF_TP_ADD_TC(tp, fopen_r_test);
185 	ATF_TP_ADD_TC(tp, fopen_r_append_test);
186 	ATF_TP_ADD_TC(tp, fopen_w_test);
187 	ATF_TP_ADD_TC(tp, fopen_w_append_test);
188 	ATF_TP_ADD_TC(tp, fopen_a_test);
189 	ATF_TP_ADD_TC(tp, fopen_a_append_test);
190 	ATF_TP_ADD_TC(tp, fopen_re_test);
191 	ATF_TP_ADD_TC(tp, fopen_r_append_e_test);
192 	ATF_TP_ADD_TC(tp, fopen_we_test);
193 	ATF_TP_ADD_TC(tp, fopen_w_append_e_test);
194 	ATF_TP_ADD_TC(tp, fopen_ae_test);
195 	ATF_TP_ADD_TC(tp, fopen_a_append_e_test);
196 	ATF_TP_ADD_TC(tp, fopen_re_append_test);
197 	ATF_TP_ADD_TC(tp, fopen_we_append_test);
198 	ATF_TP_ADD_TC(tp, fopen_ae_append_test);
199 
200 	return (atf_no_error());
201 }
202 
203 /*
204  vim:ts=8:cin:sw=8
205  */
206