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