157718be8SEnji Cooper /* $NetBSD: t_fopen.c,v 1.3 2011/09/14 14:34:37 martin Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*-
457718be8SEnji Cooper * Copyright (c) 2011 The NetBSD Foundation, Inc.
557718be8SEnji Cooper * All rights reserved.
657718be8SEnji Cooper *
757718be8SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper * by Jukka Ruohonen.
957718be8SEnji Cooper *
1057718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
1157718be8SEnji Cooper * modification, are permitted provided that the following conditions
1257718be8SEnji Cooper * are met:
1357718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
1457718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
1557718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
1657718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
1757718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
1857718be8SEnji Cooper *
1957718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2057718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2157718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2257718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2357718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2457718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2557718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2657718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2757718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2857718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2957718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
3057718be8SEnji Cooper */
3157718be8SEnji Cooper #include <sys/cdefs.h>
3257718be8SEnji Cooper __RCSID("$NetBSD: t_fopen.c,v 1.3 2011/09/14 14:34:37 martin Exp $");
3357718be8SEnji Cooper
3457718be8SEnji Cooper #include <atf-c.h>
3557718be8SEnji Cooper #include <errno.h>
3657718be8SEnji Cooper #include <fcntl.h>
3757718be8SEnji Cooper #include <limits.h>
3857718be8SEnji Cooper #include <paths.h>
3957718be8SEnji Cooper #include <stdio.h>
4057718be8SEnji Cooper #include <string.h>
4157718be8SEnji Cooper #include <unistd.h>
4257718be8SEnji Cooper
4357718be8SEnji Cooper static const char *path = "fopen";
4457718be8SEnji Cooper
4557718be8SEnji Cooper ATF_TC_WITH_CLEANUP(fdopen_close);
ATF_TC_HEAD(fdopen_close,tc)4657718be8SEnji Cooper ATF_TC_HEAD(fdopen_close, tc)
4757718be8SEnji Cooper {
4857718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "See that descriptors are closed");
4957718be8SEnji Cooper }
5057718be8SEnji Cooper
ATF_TC_BODY(fdopen_close,tc)5157718be8SEnji Cooper ATF_TC_BODY(fdopen_close, tc)
5257718be8SEnji Cooper {
5357718be8SEnji Cooper FILE *f;
5457718be8SEnji Cooper int fd;
5557718be8SEnji Cooper
5657718be8SEnji Cooper /*
5757718be8SEnji Cooper * Check that the file descriptor
5857718be8SEnji Cooper * used to fdopen(3) a stream is
5957718be8SEnji Cooper * closed once the stream is closed.
6057718be8SEnji Cooper */
61*1575a795SBrooks Davis fd = open(path, O_RDWR | O_CREAT, 0600);
6257718be8SEnji Cooper
6357718be8SEnji Cooper ATF_REQUIRE(fd >= 0);
6457718be8SEnji Cooper
6557718be8SEnji Cooper f = fdopen(fd, "w+");
6657718be8SEnji Cooper
6757718be8SEnji Cooper ATF_REQUIRE(f != NULL);
6857718be8SEnji Cooper ATF_REQUIRE(fclose(f) == 0);
6957718be8SEnji Cooper ATF_REQUIRE(close(fd) == -1);
7057718be8SEnji Cooper ATF_REQUIRE(unlink(path) == 0);
7157718be8SEnji Cooper }
7257718be8SEnji Cooper
ATF_TC_CLEANUP(fdopen_close,tc)7357718be8SEnji Cooper ATF_TC_CLEANUP(fdopen_close, tc)
7457718be8SEnji Cooper {
7557718be8SEnji Cooper (void)unlink(path);
7657718be8SEnji Cooper }
7757718be8SEnji Cooper
7857718be8SEnji Cooper ATF_TC_WITH_CLEANUP(fdopen_err);
ATF_TC_HEAD(fdopen_err,tc)7957718be8SEnji Cooper ATF_TC_HEAD(fdopen_err, tc)
8057718be8SEnji Cooper {
8157718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test errors from fdopen(3)");
8257718be8SEnji Cooper }
8357718be8SEnji Cooper
ATF_TC_BODY(fdopen_err,tc)8457718be8SEnji Cooper ATF_TC_BODY(fdopen_err, tc)
8557718be8SEnji Cooper {
8657718be8SEnji Cooper int fd;
8757718be8SEnji Cooper
88*1575a795SBrooks Davis fd = open(path, O_RDONLY | O_CREAT, 0600);
8957718be8SEnji Cooper ATF_REQUIRE(fd >= 0);
9057718be8SEnji Cooper
9157718be8SEnji Cooper errno = 0;
9257718be8SEnji Cooper ATF_REQUIRE_ERRNO(EINVAL, fdopen(fd, "w") == NULL);
9357718be8SEnji Cooper
9457718be8SEnji Cooper errno = 0;
9557718be8SEnji Cooper ATF_REQUIRE_ERRNO(EINVAL, fdopen(fd, "a") == NULL);
9657718be8SEnji Cooper
9757718be8SEnji Cooper ATF_REQUIRE(close(fd) == 0);
9857718be8SEnji Cooper
9957718be8SEnji Cooper errno = 0;
10057718be8SEnji Cooper ATF_REQUIRE_ERRNO(EBADF, fdopen(fd, "r") == NULL);
10157718be8SEnji Cooper
10257718be8SEnji Cooper errno = 0;
10357718be8SEnji Cooper ATF_REQUIRE_ERRNO(EBADF, fdopen(-1, "w+") == NULL);
10457718be8SEnji Cooper
10557718be8SEnji Cooper (void)unlink(path);
10657718be8SEnji Cooper }
10757718be8SEnji Cooper
ATF_TC_CLEANUP(fdopen_err,tc)10857718be8SEnji Cooper ATF_TC_CLEANUP(fdopen_err, tc)
10957718be8SEnji Cooper {
11057718be8SEnji Cooper (void)unlink(path);
11157718be8SEnji Cooper }
11257718be8SEnji Cooper
11357718be8SEnji Cooper ATF_TC_WITH_CLEANUP(fdopen_seek);
ATF_TC_HEAD(fdopen_seek,tc)11457718be8SEnji Cooper ATF_TC_HEAD(fdopen_seek, tc)
11557718be8SEnji Cooper {
11657718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test stream position with fdopen(3)");
11757718be8SEnji Cooper }
11857718be8SEnji Cooper
ATF_TC_BODY(fdopen_seek,tc)11957718be8SEnji Cooper ATF_TC_BODY(fdopen_seek, tc)
12057718be8SEnji Cooper {
12157718be8SEnji Cooper FILE *f;
12257718be8SEnji Cooper int fd;
12357718be8SEnji Cooper
12457718be8SEnji Cooper /*
12557718be8SEnji Cooper * Verify that the file position associated
12657718be8SEnji Cooper * with the stream corresponds with the offset
12757718be8SEnji Cooper * set earlier for the file descriptor.
12857718be8SEnji Cooper */
129*1575a795SBrooks Davis fd = open(path, O_RDWR | O_CREAT, 0600);
13057718be8SEnji Cooper
13157718be8SEnji Cooper ATF_REQUIRE(fd >= 0);
13257718be8SEnji Cooper ATF_REQUIRE(write(fd, "garbage", 7) == 7);
13357718be8SEnji Cooper ATF_REQUIRE(lseek(fd, 3, SEEK_SET) == 3);
13457718be8SEnji Cooper
13557718be8SEnji Cooper f = fdopen(fd, "r+");
13657718be8SEnji Cooper
13757718be8SEnji Cooper ATF_REQUIRE(f != NULL);
13857718be8SEnji Cooper ATF_REQUIRE(ftell(f) == 3);
13957718be8SEnji Cooper ATF_REQUIRE(fclose(f) == 0);
14057718be8SEnji Cooper ATF_REQUIRE(unlink(path) == 0);
14157718be8SEnji Cooper }
14257718be8SEnji Cooper
ATF_TC_CLEANUP(fdopen_seek,tc)14357718be8SEnji Cooper ATF_TC_CLEANUP(fdopen_seek, tc)
14457718be8SEnji Cooper {
14557718be8SEnji Cooper (void)unlink(path);
14657718be8SEnji Cooper }
14757718be8SEnji Cooper
14857718be8SEnji Cooper ATF_TC_WITH_CLEANUP(fopen_err);
ATF_TC_HEAD(fopen_err,tc)14957718be8SEnji Cooper ATF_TC_HEAD(fopen_err, tc)
15057718be8SEnji Cooper {
15157718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test errors from fopen(3)");
15257718be8SEnji Cooper }
15357718be8SEnji Cooper
ATF_TC_BODY(fopen_err,tc)15457718be8SEnji Cooper ATF_TC_BODY(fopen_err, tc)
15557718be8SEnji Cooper {
15657718be8SEnji Cooper static const char *mode[] = {
15757718be8SEnji Cooper "x", "xr", "xr", "+r+", "R", "W+", " aXX", "Xr", " r+", "" };
15857718be8SEnji Cooper
15957718be8SEnji Cooper char buf[PATH_MAX + 1];
16057718be8SEnji Cooper size_t i;
16157718be8SEnji Cooper FILE *f;
16257718be8SEnji Cooper
16357718be8SEnji Cooper f = fopen(path, "w+");
16457718be8SEnji Cooper
16557718be8SEnji Cooper ATF_REQUIRE(f != NULL);
16657718be8SEnji Cooper ATF_REQUIRE(fclose(f) == 0);
16757718be8SEnji Cooper
16857718be8SEnji Cooper /*
16957718be8SEnji Cooper * Note that also "invalid" characters
17057718be8SEnji Cooper * may follow the mode-string whenever
17157718be8SEnji Cooper * the first character is valid.
17257718be8SEnji Cooper */
17357718be8SEnji Cooper for (i = 0; i < __arraycount(mode); i++) {
17457718be8SEnji Cooper
17557718be8SEnji Cooper errno = 0;
17657718be8SEnji Cooper f = fopen(path, mode[i]);
17757718be8SEnji Cooper
17857718be8SEnji Cooper if (f == NULL && errno == EINVAL)
17957718be8SEnji Cooper continue;
18057718be8SEnji Cooper
18157718be8SEnji Cooper if (f != NULL)
18257718be8SEnji Cooper (void)fclose(f);
18357718be8SEnji Cooper
18457718be8SEnji Cooper atf_tc_fail_nonfatal("opened file as '%s'", mode[i]);
18557718be8SEnji Cooper }
18657718be8SEnji Cooper
18757718be8SEnji Cooper (void)unlink(path);
18857718be8SEnji Cooper (void)memset(buf, 'x', sizeof(buf));
18957718be8SEnji Cooper
19057718be8SEnji Cooper errno = 0;
19157718be8SEnji Cooper ATF_REQUIRE_ERRNO(EISDIR, fopen("/usr/bin", "w") == NULL);
19257718be8SEnji Cooper
19357718be8SEnji Cooper errno = 0;
19457718be8SEnji Cooper ATF_REQUIRE_ERRNO(ENOENT, fopen("/a/b/c/d/e/f", "r") == NULL);
19557718be8SEnji Cooper
19657718be8SEnji Cooper errno = 0;
19757718be8SEnji Cooper ATF_REQUIRE_ERRNO(ENAMETOOLONG, fopen(buf, "r+") == NULL);
19857718be8SEnji Cooper }
19957718be8SEnji Cooper
ATF_TC_CLEANUP(fopen_err,tc)20057718be8SEnji Cooper ATF_TC_CLEANUP(fopen_err, tc)
20157718be8SEnji Cooper {
20257718be8SEnji Cooper (void)unlink(path);
20357718be8SEnji Cooper }
20457718be8SEnji Cooper
20557718be8SEnji Cooper ATF_TC_WITH_CLEANUP(fopen_append);
ATF_TC_HEAD(fopen_append,tc)20657718be8SEnji Cooper ATF_TC_HEAD(fopen_append, tc)
20757718be8SEnji Cooper {
20857718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test that append-mode works");
20957718be8SEnji Cooper }
21057718be8SEnji Cooper
ATF_TC_BODY(fopen_append,tc)21157718be8SEnji Cooper ATF_TC_BODY(fopen_append, tc)
21257718be8SEnji Cooper {
21357718be8SEnji Cooper char buf[15];
21457718be8SEnji Cooper FILE *f;
21557718be8SEnji Cooper
21657718be8SEnji Cooper (void)memset(buf, 'x', sizeof(buf));
21757718be8SEnji Cooper
21857718be8SEnji Cooper f = fopen(path, "w+");
21957718be8SEnji Cooper
22057718be8SEnji Cooper ATF_REQUIRE(f != NULL);
22157718be8SEnji Cooper ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
22257718be8SEnji Cooper ATF_REQUIRE(fclose(f) == 0);
22357718be8SEnji Cooper
22457718be8SEnji Cooper f = fopen(path, "a");
22557718be8SEnji Cooper
22657718be8SEnji Cooper ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
22757718be8SEnji Cooper ATF_REQUIRE(fclose(f) == 0);
22857718be8SEnji Cooper
22957718be8SEnji Cooper f = fopen(path, "r");
23057718be8SEnji Cooper
23157718be8SEnji Cooper ATF_REQUIRE(fread(buf, 1, sizeof(buf), f) == 14);
23257718be8SEnji Cooper ATF_REQUIRE(strncmp(buf, "garbagegarbage", 14) == 0);
23357718be8SEnji Cooper
23457718be8SEnji Cooper ATF_REQUIRE(fclose(f) == 0);
23557718be8SEnji Cooper ATF_REQUIRE(unlink(path) == 0);
23657718be8SEnji Cooper }
23757718be8SEnji Cooper
ATF_TC_CLEANUP(fopen_append,tc)23857718be8SEnji Cooper ATF_TC_CLEANUP(fopen_append, tc)
23957718be8SEnji Cooper {
24057718be8SEnji Cooper (void)unlink(path);
24157718be8SEnji Cooper }
24257718be8SEnji Cooper
24357718be8SEnji Cooper ATF_TC_WITH_CLEANUP(fopen_mode);
ATF_TC_HEAD(fopen_mode,tc)24457718be8SEnji Cooper ATF_TC_HEAD(fopen_mode, tc)
24557718be8SEnji Cooper {
24657718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test fopen(3) modes");
24757718be8SEnji Cooper }
24857718be8SEnji Cooper
ATF_TC_BODY(fopen_mode,tc)24957718be8SEnji Cooper ATF_TC_BODY(fopen_mode, tc)
25057718be8SEnji Cooper {
25157718be8SEnji Cooper size_t i;
25257718be8SEnji Cooper FILE *f;
25357718be8SEnji Cooper
25457718be8SEnji Cooper static const char *mode[] = {
25557718be8SEnji Cooper "r", "r+", "w", "w+", "a", "a+",
25657718be8SEnji Cooper "rb", "r+b", "wb", "w+b", "ab", "a+b"
25757718be8SEnji Cooper "re", "r+e", "we", "w+e", "ae", "a+e"
25857718be8SEnji Cooper "rf", "r+f", "wf", "w+f", "af", "a+f"
25957718be8SEnji Cooper };
26057718be8SEnji Cooper
26157718be8SEnji Cooper f = fopen(path, "w+");
26257718be8SEnji Cooper
26357718be8SEnji Cooper ATF_REQUIRE(f != NULL);
26457718be8SEnji Cooper ATF_REQUIRE(fclose(f) == 0);
26557718be8SEnji Cooper
26657718be8SEnji Cooper /*
26757718be8SEnji Cooper * Verify that various modes work.
26857718be8SEnji Cooper */
26957718be8SEnji Cooper for (i = 0; i < __arraycount(mode); i++) {
27057718be8SEnji Cooper
27157718be8SEnji Cooper f = fopen(path, mode[i]);
27257718be8SEnji Cooper
27357718be8SEnji Cooper if (f != NULL) {
27457718be8SEnji Cooper ATF_REQUIRE(fclose(f) == 0);
27557718be8SEnji Cooper continue;
27657718be8SEnji Cooper }
27757718be8SEnji Cooper
27857718be8SEnji Cooper atf_tc_fail_nonfatal("failed to open file as %s", mode[i]);
27957718be8SEnji Cooper }
28057718be8SEnji Cooper
28157718be8SEnji Cooper (void)unlink(path);
28257718be8SEnji Cooper }
28357718be8SEnji Cooper
ATF_TC_CLEANUP(fopen_mode,tc)28457718be8SEnji Cooper ATF_TC_CLEANUP(fopen_mode, tc)
28557718be8SEnji Cooper {
28657718be8SEnji Cooper (void)unlink(path);
28757718be8SEnji Cooper }
28857718be8SEnji Cooper
28957718be8SEnji Cooper ATF_TC(fopen_perm);
ATF_TC_HEAD(fopen_perm,tc)29057718be8SEnji Cooper ATF_TC_HEAD(fopen_perm, tc)
29157718be8SEnji Cooper {
29257718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test permissions with fopen(3)");
29357718be8SEnji Cooper atf_tc_set_md_var(tc, "require.user", "unprivileged");
29457718be8SEnji Cooper }
29557718be8SEnji Cooper
ATF_TC_BODY(fopen_perm,tc)29657718be8SEnji Cooper ATF_TC_BODY(fopen_perm, tc)
29757718be8SEnji Cooper {
29857718be8SEnji Cooper
29957718be8SEnji Cooper errno = 0;
30057718be8SEnji Cooper ATF_REQUIRE_ERRNO(EACCES, fopen("/bin/ls", "a+") == NULL);
30157718be8SEnji Cooper
30257718be8SEnji Cooper errno = 0;
30357718be8SEnji Cooper ATF_REQUIRE_ERRNO(EACCES, fopen("/bin/ls", "w+") == NULL);
30457718be8SEnji Cooper }
30557718be8SEnji Cooper
306bb1f0779SBaptiste Daroussin #ifdef __NetBSD__
30757718be8SEnji Cooper ATF_TC(fopen_regular);
ATF_TC_HEAD(fopen_regular,tc)30857718be8SEnji Cooper ATF_TC_HEAD(fopen_regular, tc)
30957718be8SEnji Cooper {
31057718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test fopen(3) with 'f' mode");
31157718be8SEnji Cooper }
31257718be8SEnji Cooper
ATF_TC_BODY(fopen_regular,tc)31357718be8SEnji Cooper ATF_TC_BODY(fopen_regular, tc)
31457718be8SEnji Cooper {
31557718be8SEnji Cooper static const char *mode[] = { "rf", "r+f", "wf", "w+f", "af", "a+f" };
31657718be8SEnji Cooper static const char *devs[] = { _PATH_DEVNULL };
31757718be8SEnji Cooper
31857718be8SEnji Cooper size_t i, j;
31957718be8SEnji Cooper FILE *f;
32057718be8SEnji Cooper
32157718be8SEnji Cooper for (i = 0; i < __arraycount(devs); i++) {
32257718be8SEnji Cooper
32357718be8SEnji Cooper for (j = 0; j < __arraycount(mode); j++) {
32457718be8SEnji Cooper
32557718be8SEnji Cooper errno = 0;
32657718be8SEnji Cooper f = fopen(devs[i], mode[j]);
32757718be8SEnji Cooper
32857718be8SEnji Cooper if (f == NULL && errno == EFTYPE)
32957718be8SEnji Cooper continue;
33057718be8SEnji Cooper
33157718be8SEnji Cooper if (f != NULL)
33257718be8SEnji Cooper (void)fclose(f);
33357718be8SEnji Cooper
33457718be8SEnji Cooper atf_tc_fail_nonfatal("opened %s as %s",
33557718be8SEnji Cooper devs[i], mode[j]);
33657718be8SEnji Cooper }
33757718be8SEnji Cooper }
33857718be8SEnji Cooper }
339bb1f0779SBaptiste Daroussin #endif
34057718be8SEnji Cooper
34157718be8SEnji Cooper ATF_TC_WITH_CLEANUP(fopen_seek);
ATF_TC_HEAD(fopen_seek,tc)34257718be8SEnji Cooper ATF_TC_HEAD(fopen_seek, tc)
34357718be8SEnji Cooper {
34457718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test initial stream position");
34557718be8SEnji Cooper }
34657718be8SEnji Cooper
ATF_TC_BODY(fopen_seek,tc)34757718be8SEnji Cooper ATF_TC_BODY(fopen_seek, tc)
34857718be8SEnji Cooper {
34957718be8SEnji Cooper FILE *f;
35057718be8SEnji Cooper
35157718be8SEnji Cooper f = fopen(path, "w+");
35257718be8SEnji Cooper
35357718be8SEnji Cooper ATF_REQUIRE(f != NULL);
35457718be8SEnji Cooper ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
35557718be8SEnji Cooper ATF_REQUIRE(fclose(f) == 0);
35657718be8SEnji Cooper
35757718be8SEnji Cooper /*
35857718be8SEnji Cooper * The position of the stream should be
35957718be8SEnji Cooper * at the start, except for append-mode.
36057718be8SEnji Cooper */
36157718be8SEnji Cooper f = fopen(path, "r");
36257718be8SEnji Cooper
36357718be8SEnji Cooper ATF_REQUIRE(f != NULL);
36457718be8SEnji Cooper ATF_REQUIRE(ftello(f) == 0);
36557718be8SEnji Cooper ATF_REQUIRE(fclose(f) == 0);
36657718be8SEnji Cooper
36757718be8SEnji Cooper f = fopen(path, "a");
36857718be8SEnji Cooper
36957718be8SEnji Cooper ATF_REQUIRE(f != NULL);
37057718be8SEnji Cooper ATF_REQUIRE(ftello(f) == 7);
37157718be8SEnji Cooper ATF_REQUIRE(fclose(f) == 0);
37257718be8SEnji Cooper ATF_REQUIRE(unlink(path) == 0);
37357718be8SEnji Cooper }
37457718be8SEnji Cooper
ATF_TC_CLEANUP(fopen_seek,tc)37557718be8SEnji Cooper ATF_TC_CLEANUP(fopen_seek, tc)
37657718be8SEnji Cooper {
37757718be8SEnji Cooper (void)unlink(path);
37857718be8SEnji Cooper }
37957718be8SEnji Cooper
38057718be8SEnji Cooper ATF_TC_WITH_CLEANUP(freopen_std);
ATF_TC_HEAD(freopen_std,tc)38157718be8SEnji Cooper ATF_TC_HEAD(freopen_std, tc)
38257718be8SEnji Cooper {
38357718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "A basic test of freopen(3)");
38457718be8SEnji Cooper }
38557718be8SEnji Cooper
ATF_TC_BODY(freopen_std,tc)38657718be8SEnji Cooper ATF_TC_BODY(freopen_std, tc)
38757718be8SEnji Cooper {
38857718be8SEnji Cooper FILE *std[2] = { stdin, stdout };
38957718be8SEnji Cooper char buf[15];
39057718be8SEnji Cooper size_t i;
39157718be8SEnji Cooper FILE *f;
39257718be8SEnji Cooper
39357718be8SEnji Cooper /*
39457718be8SEnji Cooper * Associate a standard stream with a custom stream.
39557718be8SEnji Cooper * Then write to the standard stream and verify that
39657718be8SEnji Cooper * the result now appears in the custom stream.
39757718be8SEnji Cooper */
39857718be8SEnji Cooper for (i = 0; i < __arraycount(std); i++) {
39957718be8SEnji Cooper
40057718be8SEnji Cooper (void)memset(buf, 'x', sizeof(buf));
40157718be8SEnji Cooper
40257718be8SEnji Cooper f = fopen(path, "w+");
40357718be8SEnji Cooper ATF_REQUIRE(f != NULL);
40457718be8SEnji Cooper
40557718be8SEnji Cooper f = freopen(path, "w+", std[i]);
40657718be8SEnji Cooper ATF_REQUIRE(f != NULL);
40757718be8SEnji Cooper
40857718be8SEnji Cooper ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
40957718be8SEnji Cooper ATF_REQUIRE(fprintf(std[i], "garbage") == 7);
41057718be8SEnji Cooper ATF_REQUIRE(fclose(f) == 0);
41157718be8SEnji Cooper
41257718be8SEnji Cooper f = fopen(path, "r");
41357718be8SEnji Cooper
41457718be8SEnji Cooper ATF_REQUIRE(f != NULL);
41557718be8SEnji Cooper ATF_REQUIRE(fread(buf, 1, sizeof(buf), f) == 14);
41657718be8SEnji Cooper ATF_REQUIRE(strncmp(buf, "garbagegarbage", 14) == 0);
41757718be8SEnji Cooper
41857718be8SEnji Cooper ATF_REQUIRE(fclose(f) == 0);
41957718be8SEnji Cooper }
42057718be8SEnji Cooper
42157718be8SEnji Cooper ATF_REQUIRE(unlink(path) == 0);
42257718be8SEnji Cooper }
42357718be8SEnji Cooper
ATF_TC_CLEANUP(freopen_std,tc)42457718be8SEnji Cooper ATF_TC_CLEANUP(freopen_std, tc)
42557718be8SEnji Cooper {
42657718be8SEnji Cooper (void)unlink(path);
42757718be8SEnji Cooper }
42857718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)42957718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
43057718be8SEnji Cooper {
43157718be8SEnji Cooper
43257718be8SEnji Cooper ATF_TP_ADD_TC(tp, fdopen_close);
43357718be8SEnji Cooper ATF_TP_ADD_TC(tp, fdopen_err);
43457718be8SEnji Cooper ATF_TP_ADD_TC(tp, fdopen_seek);
43557718be8SEnji Cooper ATF_TP_ADD_TC(tp, fopen_append);
43657718be8SEnji Cooper ATF_TP_ADD_TC(tp, fopen_err);
43757718be8SEnji Cooper ATF_TP_ADD_TC(tp, fopen_mode);
43857718be8SEnji Cooper ATF_TP_ADD_TC(tp, fopen_perm);
439c069d691SEnji Cooper #ifdef __NetBSD__
44057718be8SEnji Cooper ATF_TP_ADD_TC(tp, fopen_regular);
441c069d691SEnji Cooper #endif
44257718be8SEnji Cooper ATF_TP_ADD_TC(tp, fopen_seek);
44357718be8SEnji Cooper ATF_TP_ADD_TC(tp, freopen_std);
44457718be8SEnji Cooper
44557718be8SEnji Cooper return atf_no_error();
44657718be8SEnji Cooper }
447