xref: /freebsd/contrib/netbsd-tests/lib/libc/stdio/t_fflush.c (revision 4928135658a9d0eaee37003df6137ab363fcb0b4)
1 /*	$NetBSD: t_fflush.c,v 1.1 2011/09/11 05:15:55 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_fflush.c,v 1.1 2011/09/11 05:15:55 jruoho Exp $");
33 
34 #include <atf-c.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 
39 static const char *path = "fflush";
40 
41 ATF_TC_WITH_CLEANUP(fflush_err);
42 ATF_TC_HEAD(fflush_err, tc)
43 {
44 	atf_tc_set_md_var(tc, "descr", "Test errors from fflush(3)");
45 }
46 
47 ATF_TC_BODY(fflush_err, tc)
48 {
49 	FILE *f;
50 
51 #ifdef __FreeBSD__
52 	atf_tc_expect_fail("the EOF invariant fails on FreeBSD; this is new");
53 #endif
54 
55 	f = fopen(path, "w");
56 
57 	ATF_REQUIRE(f != NULL);
58 	ATF_REQUIRE(fflush(NULL) == 0);
59 	ATF_REQUIRE(fclose(f) == 0);
60 
61 	f = fopen(path, "r");
62 	ATF_REQUIRE(f != NULL);
63 
64 	/*
65 	 * In NetBSD the call should fail if the supplied
66 	 * parameteris not an open stream or the stream is
67 	 * not open for writing.
68 	 */
69 	errno = 0;
70 	ATF_REQUIRE_ERRNO(EBADF, fflush(f) == EOF);
71 
72 	ATF_REQUIRE(fclose(f) == 0);
73 
74 	errno = 0;
75 	ATF_REQUIRE_ERRNO(EBADF, fflush(f) == EOF);
76 
77 	(void)unlink(path);
78 }
79 
80 ATF_TC_CLEANUP(fflush_err, tc)
81 {
82 	(void)unlink(path);
83 }
84 
85 ATF_TC_WITH_CLEANUP(fflush_seek);
86 ATF_TC_HEAD(fflush_seek, tc)
87 {
88 	atf_tc_set_md_var(tc, "descr", "Test file offsets with fflush(3)");
89 }
90 
91 ATF_TC_BODY(fflush_seek, tc)
92 {
93 	char buf[12];
94 	int fd = -1;
95 	FILE *f;
96 
97 	/*
98 	 * IEEE Std 1003.1-2008:
99 	 *
100 	 * "For a stream open for reading, if the file
101 	 *  is not already at EOF, and the file is one
102 	 *  capable of seeking, the file offset of the
103 	 *  underlying open file description shall be
104 	 *  adjusted so that the next operation on the
105 	 *  open file description deals with the byte
106 	 *  after the last one read from or written to
107 	 *  the stream being flushed."
108 	 */
109 	f = fopen(path, "w");
110 	ATF_REQUIRE(f != NULL);
111 
112 	ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
113 	ATF_REQUIRE(fclose(f) == 0);
114 
115 	f = fopen(path, "r+");
116 	ATF_REQUIRE(f != NULL);
117 
118 	fd = fileno(f);
119 	ATF_REQUIRE(fd != -1);
120 
121 	ATF_REQUIRE(fread(buf, 1, 3, f) == 3);
122 	ATF_REQUIRE(fflush(f) == 0);
123 	ATF_REQUIRE(fseek(f, 0, SEEK_CUR) == 0);
124 
125 	/*
126 	 * Verify that the offsets are right and that
127 	 * a read operation resumes at the correct location.
128 	 */
129 	ATF_REQUIRE(ftell(f) == 3);
130 	ATF_REQUIRE(lseek(fd, 0, SEEK_CUR) == 3);
131 	ATF_REQUIRE(fgetc(f) == 'b');
132 
133 	ATF_REQUIRE(fclose(f) == 0);
134 	ATF_REQUIRE(unlink(path) == 0);
135 }
136 
137 ATF_TC_CLEANUP(fflush_seek, tc)
138 {
139 	(void)unlink(path);
140 }
141 
142 ATF_TC_WITH_CLEANUP(fpurge_err);
143 ATF_TC_HEAD(fpurge_err, tc)
144 {
145 	atf_tc_set_md_var(tc, "descr", "Test errors from fpurge(3)");
146 }
147 
148 ATF_TC_BODY(fpurge_err, tc)
149 {
150 	FILE *f;
151 
152 	f = fopen(path, "w");
153 	ATF_REQUIRE(f != NULL);
154 	ATF_REQUIRE(fclose(f) == 0);
155 
156 	errno = 0;
157 	ATF_REQUIRE_ERRNO(EBADF, fpurge(f) == EOF);
158 
159 	(void)unlink(path);
160 }
161 
162 ATF_TC_CLEANUP(fpurge_err, tc)
163 {
164 	(void)unlink(path);
165 }
166 
167 ATF_TP_ADD_TCS(tp)
168 {
169 
170 	ATF_TP_ADD_TC(tp, fflush_err);
171 	ATF_TP_ADD_TC(tp, fflush_seek);
172 	ATF_TP_ADD_TC(tp, fpurge_err);
173 
174 	return atf_no_error();
175 }
176