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