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