1 /* $NetBSD: t_fopen.c,v 1.3 2011/09/14 14:34:37 martin Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_fopen.c,v 1.3 2011/09/14 14:34:37 martin Exp $");
43 static const char *path
= "fopen";
45 ATF_TC_WITH_CLEANUP(fdopen_close
);
46 ATF_TC_HEAD(fdopen_close
, tc
)
48 atf_tc_set_md_var(tc
, "descr", "See that descriptors are closed");
51 ATF_TC_BODY(fdopen_close
, tc
)
57 * Check that the file descriptor
58 * used to fdopen(3) a stream is
59 * closed once the stream is closed.
61 fd
= open(path
, O_RDWR
| O_CREAT
);
67 ATF_REQUIRE(f
!= NULL
);
68 ATF_REQUIRE(fclose(f
) == 0);
69 ATF_REQUIRE(close(fd
) == -1);
70 ATF_REQUIRE(unlink(path
) == 0);
73 ATF_TC_CLEANUP(fdopen_close
, tc
)
78 ATF_TC_WITH_CLEANUP(fdopen_err
);
79 ATF_TC_HEAD(fdopen_err
, tc
)
81 atf_tc_set_md_var(tc
, "descr", "Test errors from fdopen(3)");
84 ATF_TC_BODY(fdopen_err
, tc
)
88 fd
= open(path
, O_RDONLY
| O_CREAT
);
92 ATF_REQUIRE_ERRNO(EINVAL
, fdopen(fd
, "w") == NULL
);
95 ATF_REQUIRE_ERRNO(EINVAL
, fdopen(fd
, "a") == NULL
);
97 ATF_REQUIRE(close(fd
) == 0);
100 ATF_REQUIRE_ERRNO(EBADF
, fdopen(fd
, "r") == NULL
);
103 ATF_REQUIRE_ERRNO(EBADF
, fdopen(-1, "w+") == NULL
);
108 ATF_TC_CLEANUP(fdopen_err
, tc
)
113 ATF_TC_WITH_CLEANUP(fdopen_seek
);
114 ATF_TC_HEAD(fdopen_seek
, tc
)
116 atf_tc_set_md_var(tc
, "descr", "Test stream position with fdopen(3)");
119 ATF_TC_BODY(fdopen_seek
, tc
)
125 * Verify that the file position associated
126 * with the stream corresponds with the offset
127 * set earlier for the file descriptor.
129 fd
= open(path
, O_RDWR
| O_CREAT
);
131 ATF_REQUIRE(fd
>= 0);
132 ATF_REQUIRE(write(fd
, "garbage", 7) == 7);
133 ATF_REQUIRE(lseek(fd
, 3, SEEK_SET
) == 3);
135 f
= fdopen(fd
, "r+");
137 ATF_REQUIRE(f
!= NULL
);
138 ATF_REQUIRE(ftell(f
) == 3);
139 ATF_REQUIRE(fclose(f
) == 0);
140 ATF_REQUIRE(unlink(path
) == 0);
143 ATF_TC_CLEANUP(fdopen_seek
, tc
)
148 ATF_TC_WITH_CLEANUP(fopen_err
);
149 ATF_TC_HEAD(fopen_err
, tc
)
151 atf_tc_set_md_var(tc
, "descr", "Test errors from fopen(3)");
154 ATF_TC_BODY(fopen_err
, tc
)
156 static const char *mode
[] = {
157 "x", "xr", "xr", "+r+", "R", "W+", " aXX", "Xr", " r+", "" };
159 char buf
[PATH_MAX
+ 1];
163 f
= fopen(path
, "w+");
165 ATF_REQUIRE(f
!= NULL
);
166 ATF_REQUIRE(fclose(f
) == 0);
169 * Note that also "invalid" characters
170 * may follow the mode-string whenever
171 * the first character is valid.
173 for (i
= 0; i
< __arraycount(mode
); i
++) {
176 f
= fopen(path
, mode
[i
]);
178 if (f
== NULL
&& errno
== EINVAL
)
184 atf_tc_fail_nonfatal("opened file as '%s'", mode
[i
]);
188 (void)memset(buf
, 'x', sizeof(buf
));
191 ATF_REQUIRE_ERRNO(EISDIR
, fopen("/usr/bin", "w") == NULL
);
194 ATF_REQUIRE_ERRNO(ENOENT
, fopen("/a/b/c/d/e/f", "r") == NULL
);
197 ATF_REQUIRE_ERRNO(ENAMETOOLONG
, fopen(buf
, "r+") == NULL
);
200 ATF_TC_CLEANUP(fopen_err
, tc
)
205 ATF_TC_WITH_CLEANUP(fopen_append
);
206 ATF_TC_HEAD(fopen_append
, tc
)
208 atf_tc_set_md_var(tc
, "descr", "Test that append-mode works");
211 ATF_TC_BODY(fopen_append
, tc
)
216 (void)memset(buf
, 'x', sizeof(buf
));
218 f
= fopen(path
, "w+");
220 ATF_REQUIRE(f
!= NULL
);
221 ATF_REQUIRE(fwrite("garbage", 1, 7, f
) == 7);
222 ATF_REQUIRE(fclose(f
) == 0);
224 f
= fopen(path
, "a");
226 ATF_REQUIRE(fwrite("garbage", 1, 7, f
) == 7);
227 ATF_REQUIRE(fclose(f
) == 0);
229 f
= fopen(path
, "r");
231 ATF_REQUIRE(fread(buf
, 1, sizeof(buf
), f
) == 14);
232 ATF_REQUIRE(strncmp(buf
, "garbagegarbage", 14) == 0);
234 ATF_REQUIRE(fclose(f
) == 0);
235 ATF_REQUIRE(unlink(path
) == 0);
238 ATF_TC_CLEANUP(fopen_append
, tc
)
243 ATF_TC_WITH_CLEANUP(fopen_mode
);
244 ATF_TC_HEAD(fopen_mode
, tc
)
246 atf_tc_set_md_var(tc
, "descr", "Test fopen(3) modes");
249 ATF_TC_BODY(fopen_mode
, tc
)
254 static const char *mode
[] = {
255 "r", "r+", "w", "w+", "a", "a+",
256 "rb", "r+b", "wb", "w+b", "ab", "a+b"
257 "re", "r+e", "we", "w+e", "ae", "a+e"
258 "rf", "r+f", "wf", "w+f", "af", "a+f"
261 f
= fopen(path
, "w+");
263 ATF_REQUIRE(f
!= NULL
);
264 ATF_REQUIRE(fclose(f
) == 0);
267 * Verify that various modes work.
269 for (i
= 0; i
< __arraycount(mode
); i
++) {
271 f
= fopen(path
, mode
[i
]);
274 ATF_REQUIRE(fclose(f
) == 0);
278 atf_tc_fail_nonfatal("failed to open file as %s", mode
[i
]);
284 ATF_TC_CLEANUP(fopen_mode
, tc
)
290 ATF_TC_HEAD(fopen_perm
, tc
)
292 atf_tc_set_md_var(tc
, "descr", "Test permissions with fopen(3)");
293 atf_tc_set_md_var(tc
, "require.user", "unprivileged");
296 ATF_TC_BODY(fopen_perm
, tc
)
300 ATF_REQUIRE_ERRNO(EACCES
, fopen("/bin/ls", "a+") == NULL
);
303 ATF_REQUIRE_ERRNO(EACCES
, fopen("/bin/ls", "w+") == NULL
);
306 ATF_TC(fopen_regular
);
307 ATF_TC_HEAD(fopen_regular
, tc
)
309 atf_tc_set_md_var(tc
, "descr", "Test fopen(3) with 'f' mode");
312 ATF_TC_BODY(fopen_regular
, tc
)
314 static const char *mode
[] = { "rf", "r+f", "wf", "w+f", "af", "a+f" };
315 static const char *devs
[] = { _PATH_DEVNULL
};
320 for (i
= 0; i
< __arraycount(devs
); i
++) {
322 for (j
= 0; j
< __arraycount(mode
); j
++) {
325 f
= fopen(devs
[i
], mode
[j
]);
327 if (f
== NULL
&& errno
== EFTYPE
)
333 atf_tc_fail_nonfatal("opened %s as %s",
339 ATF_TC_WITH_CLEANUP(fopen_seek
);
340 ATF_TC_HEAD(fopen_seek
, tc
)
342 atf_tc_set_md_var(tc
, "descr", "Test initial stream position");
345 ATF_TC_BODY(fopen_seek
, tc
)
349 f
= fopen(path
, "w+");
351 ATF_REQUIRE(f
!= NULL
);
352 ATF_REQUIRE(fwrite("garbage", 1, 7, f
) == 7);
353 ATF_REQUIRE(fclose(f
) == 0);
356 * The position of the stream should be
357 * at the start, except for append-mode.
359 f
= fopen(path
, "r");
361 ATF_REQUIRE(f
!= NULL
);
362 ATF_REQUIRE(ftello(f
) == 0);
363 ATF_REQUIRE(fclose(f
) == 0);
365 f
= fopen(path
, "a");
367 ATF_REQUIRE(f
!= NULL
);
368 ATF_REQUIRE(ftello(f
) == 7);
369 ATF_REQUIRE(fclose(f
) == 0);
370 ATF_REQUIRE(unlink(path
) == 0);
373 ATF_TC_CLEANUP(fopen_seek
, tc
)
378 ATF_TC_WITH_CLEANUP(freopen_std
);
379 ATF_TC_HEAD(freopen_std
, tc
)
381 atf_tc_set_md_var(tc
, "descr", "A basic test of freopen(3)");
384 ATF_TC_BODY(freopen_std
, tc
)
386 FILE *std
[2] = { stdin
, stdout
};
392 * Associate a standard stream with a custom stream.
393 * Then write to the standard stream and verify that
394 * the result now appears in the custom stream.
396 for (i
= 0; i
< __arraycount(std
); i
++) {
398 (void)memset(buf
, 'x', sizeof(buf
));
400 f
= fopen(path
, "w+");
401 ATF_REQUIRE(f
!= NULL
);
403 f
= freopen(path
, "w+", std
[i
]);
404 ATF_REQUIRE(f
!= NULL
);
406 ATF_REQUIRE(fwrite("garbage", 1, 7, f
) == 7);
407 ATF_REQUIRE(fprintf(std
[i
], "garbage") == 7);
408 ATF_REQUIRE(fclose(f
) == 0);
410 f
= fopen(path
, "r");
412 ATF_REQUIRE(f
!= NULL
);
413 ATF_REQUIRE(fread(buf
, 1, sizeof(buf
), f
) == 14);
414 ATF_REQUIRE(strncmp(buf
, "garbagegarbage", 14) == 0);
416 ATF_REQUIRE(fclose(f
) == 0);
419 ATF_REQUIRE(unlink(path
) == 0);
422 ATF_TC_CLEANUP(freopen_std
, tc
)
430 ATF_TP_ADD_TC(tp
, fdopen_close
);
431 ATF_TP_ADD_TC(tp
, fdopen_err
);
432 ATF_TP_ADD_TC(tp
, fdopen_seek
);
433 ATF_TP_ADD_TC(tp
, fopen_append
);
434 ATF_TP_ADD_TC(tp
, fopen_err
);
435 ATF_TP_ADD_TC(tp
, fopen_mode
);
436 ATF_TP_ADD_TC(tp
, fopen_perm
);
437 ATF_TP_ADD_TC(tp
, fopen_regular
);
438 ATF_TP_ADD_TC(tp
, fopen_seek
);
439 ATF_TP_ADD_TC(tp
, freopen_std
);
441 return atf_no_error();