1272343Sngie/*	$NetBSD: t_fflush.c,v 1.1 2011/09/11 05:15:55 jruoho Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Jukka Ruohonen.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie *
19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29272343Sngie * POSSIBILITY OF SUCH DAMAGE.
30272343Sngie */
31272343Sngie#include <sys/cdefs.h>
32272343Sngie__RCSID("$NetBSD: t_fflush.c,v 1.1 2011/09/11 05:15:55 jruoho Exp $");
33272343Sngie
34272343Sngie#include <atf-c.h>
35272343Sngie#include <errno.h>
36272343Sngie#include <stdio.h>
37272343Sngie#include <unistd.h>
38272343Sngie
39272343Sngiestatic const char *path = "fflush";
40272343Sngie
41272343SngieATF_TC_WITH_CLEANUP(fflush_err);
42272343SngieATF_TC_HEAD(fflush_err, tc)
43272343Sngie{
44272343Sngie	atf_tc_set_md_var(tc, "descr", "Test errors from fflush(3)");
45272343Sngie}
46272343Sngie
47272343SngieATF_TC_BODY(fflush_err, tc)
48272343Sngie{
49272343Sngie	FILE *f;
50272343Sngie
51276478Sngie#ifdef __FreeBSD__
52276478Sngie	atf_tc_expect_fail("the EOF invariant fails on FreeBSD; this is new");
53276478Sngie#endif
54276478Sngie
55272343Sngie	f = fopen(path, "w");
56272343Sngie
57272343Sngie	ATF_REQUIRE(f != NULL);
58272343Sngie	ATF_REQUIRE(fflush(NULL) == 0);
59272343Sngie	ATF_REQUIRE(fclose(f) == 0);
60272343Sngie
61272343Sngie	f = fopen(path, "r");
62272343Sngie	ATF_REQUIRE(f != NULL);
63272343Sngie
64272343Sngie	/*
65272343Sngie	 * In NetBSD the call should fail if the supplied
66272343Sngie	 * parameteris not an open stream or the stream is
67272343Sngie	 * not open for writing.
68272343Sngie	 */
69272343Sngie	errno = 0;
70272343Sngie	ATF_REQUIRE_ERRNO(EBADF, fflush(f) == EOF);
71272343Sngie
72272343Sngie	ATF_REQUIRE(fclose(f) == 0);
73272343Sngie
74272343Sngie	errno = 0;
75272343Sngie	ATF_REQUIRE_ERRNO(EBADF, fflush(f) == EOF);
76272343Sngie
77272343Sngie	(void)unlink(path);
78272343Sngie}
79272343Sngie
80272343SngieATF_TC_CLEANUP(fflush_err, tc)
81272343Sngie{
82272343Sngie	(void)unlink(path);
83272343Sngie}
84272343Sngie
85272343SngieATF_TC_WITH_CLEANUP(fflush_seek);
86272343SngieATF_TC_HEAD(fflush_seek, tc)
87272343Sngie{
88272343Sngie	atf_tc_set_md_var(tc, "descr", "Test file offsets with fflush(3)");
89272343Sngie}
90272343Sngie
91272343SngieATF_TC_BODY(fflush_seek, tc)
92272343Sngie{
93272343Sngie	char buf[12];
94272343Sngie	int fd = -1;
95272343Sngie	FILE *f;
96272343Sngie
97272343Sngie	/*
98272343Sngie	 * IEEE Std 1003.1-2008:
99272343Sngie	 *
100272343Sngie	 * "For a stream open for reading, if the file
101272343Sngie	 *  is not already at EOF, and the file is one
102272343Sngie	 *  capable of seeking, the file offset of the
103272343Sngie	 *  underlying open file description shall be
104272343Sngie	 *  adjusted so that the next operation on the
105272343Sngie	 *  open file description deals with the byte
106272343Sngie	 *  after the last one read from or written to
107272343Sngie	 *  the stream being flushed."
108272343Sngie	 */
109272343Sngie	f = fopen(path, "w");
110272343Sngie	ATF_REQUIRE(f != NULL);
111272343Sngie
112272343Sngie	ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
113272343Sngie	ATF_REQUIRE(fclose(f) == 0);
114272343Sngie
115272343Sngie	f = fopen(path, "r+");
116272343Sngie	ATF_REQUIRE(f != NULL);
117272343Sngie
118272343Sngie	fd = fileno(f);
119272343Sngie	ATF_REQUIRE(fd != -1);
120272343Sngie
121272343Sngie	ATF_REQUIRE(fread(buf, 1, 3, f) == 3);
122272343Sngie	ATF_REQUIRE(fflush(f) == 0);
123272343Sngie	ATF_REQUIRE(fseek(f, 0, SEEK_CUR) == 0);
124272343Sngie
125272343Sngie	/*
126272343Sngie	 * Verify that the offsets are right and that
127272343Sngie	 * a read operation resumes at the correct location.
128272343Sngie	 */
129272343Sngie	ATF_REQUIRE(ftell(f) == 3);
130272343Sngie	ATF_REQUIRE(lseek(fd, 0, SEEK_CUR) == 3);
131272343Sngie	ATF_REQUIRE(fgetc(f) == 'b');
132272343Sngie
133272343Sngie	ATF_REQUIRE(fclose(f) == 0);
134272343Sngie	ATF_REQUIRE(unlink(path) == 0);
135272343Sngie}
136272343Sngie
137272343SngieATF_TC_CLEANUP(fflush_seek, tc)
138272343Sngie{
139272343Sngie	(void)unlink(path);
140272343Sngie}
141272343Sngie
142272343SngieATF_TC_WITH_CLEANUP(fpurge_err);
143272343SngieATF_TC_HEAD(fpurge_err, tc)
144272343Sngie{
145272343Sngie	atf_tc_set_md_var(tc, "descr", "Test errors from fpurge(3)");
146272343Sngie}
147272343Sngie
148272343SngieATF_TC_BODY(fpurge_err, tc)
149272343Sngie{
150272343Sngie	FILE *f;
151272343Sngie
152272343Sngie	f = fopen(path, "w");
153272343Sngie	ATF_REQUIRE(f != NULL);
154272343Sngie	ATF_REQUIRE(fclose(f) == 0);
155272343Sngie
156272343Sngie	errno = 0;
157272343Sngie	ATF_REQUIRE_ERRNO(EBADF, fpurge(f) == EOF);
158272343Sngie
159272343Sngie	(void)unlink(path);
160272343Sngie}
161272343Sngie
162272343SngieATF_TC_CLEANUP(fpurge_err, tc)
163272343Sngie{
164272343Sngie	(void)unlink(path);
165272343Sngie}
166272343Sngie
167272343SngieATF_TP_ADD_TCS(tp)
168272343Sngie{
169272343Sngie
170272343Sngie	ATF_TP_ADD_TC(tp, fflush_err);
171272343Sngie	ATF_TP_ADD_TC(tp, fflush_seek);
172272343Sngie	ATF_TP_ADD_TC(tp, fpurge_err);
173272343Sngie
174272343Sngie	return atf_no_error();
175272343Sngie}
176