1 /* $NetBSD: t_fflush.c,v 1.1 2011/09/11 05:15:55 jruoho 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_fflush.c,v 1.1 2011/09/11 05:15:55 jruoho Exp $");
39 static const char *path = "fflush";
41 ATF_TC_WITH_CLEANUP(fflush_err);
42 ATF_TC_HEAD(fflush_err, tc)
44 atf_tc_set_md_var(tc, "descr", "Test errors from fflush(3)");
47 ATF_TC_BODY(fflush_err, tc)
52 atf_tc_expect_fail("the EOF invariant fails on FreeBSD; this is new");
57 ATF_REQUIRE(f != NULL);
58 ATF_REQUIRE(fflush(NULL) == 0);
59 ATF_REQUIRE(fclose(f) == 0);
62 ATF_REQUIRE(f != NULL);
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.
70 ATF_REQUIRE_ERRNO(EBADF, fflush(f) == EOF);
72 ATF_REQUIRE(fclose(f) == 0);
75 ATF_REQUIRE_ERRNO(EBADF, fflush(f) == EOF);
80 ATF_TC_CLEANUP(fflush_err, tc)
85 ATF_TC_WITH_CLEANUP(fflush_seek);
86 ATF_TC_HEAD(fflush_seek, tc)
88 atf_tc_set_md_var(tc, "descr", "Test file offsets with fflush(3)");
91 ATF_TC_BODY(fflush_seek, tc)
98 * IEEE Std 1003.1-2008:
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."
109 f = fopen(path, "w");
110 ATF_REQUIRE(f != NULL);
112 ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
113 ATF_REQUIRE(fclose(f) == 0);
115 f = fopen(path, "r+");
116 ATF_REQUIRE(f != NULL);
119 ATF_REQUIRE(fd != -1);
121 ATF_REQUIRE(fread(buf, 1, 3, f) == 3);
122 ATF_REQUIRE(fflush(f) == 0);
123 ATF_REQUIRE(fseek(f, 0, SEEK_CUR) == 0);
126 * Verify that the offsets are right and that
127 * a read operation resumes at the correct location.
129 ATF_REQUIRE(ftell(f) == 3);
130 ATF_REQUIRE(lseek(fd, 0, SEEK_CUR) == 3);
131 ATF_REQUIRE(fgetc(f) == 'b');
133 ATF_REQUIRE(fclose(f) == 0);
134 ATF_REQUIRE(unlink(path) == 0);
137 ATF_TC_CLEANUP(fflush_seek, tc)
142 ATF_TC_WITH_CLEANUP(fpurge_err);
143 ATF_TC_HEAD(fpurge_err, tc)
145 atf_tc_set_md_var(tc, "descr", "Test errors from fpurge(3)");
148 ATF_TC_BODY(fpurge_err, tc)
152 f = fopen(path, "w");
153 ATF_REQUIRE(f != NULL);
154 ATF_REQUIRE(fclose(f) == 0);
157 ATF_REQUIRE_ERRNO(EBADF, fpurge(f) == EOF);
162 ATF_TC_CLEANUP(fpurge_err, tc)
170 ATF_TP_ADD_TC(tp, fflush_err);
171 ATF_TP_ADD_TC(tp, fflush_seek);
172 ATF_TP_ADD_TC(tp, fpurge_err);
174 return atf_no_error();