summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/sys/t_fsync.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libc/sys/t_fsync.c')
-rw-r--r--src/regress/lib/libc/sys/t_fsync.c125
1 files changed, 125 insertions, 0 deletions
diff --git a/src/regress/lib/libc/sys/t_fsync.c b/src/regress/lib/libc/sys/t_fsync.c
new file mode 100644
index 0000000000..c6f24c0e85
--- /dev/null
+++ b/src/regress/lib/libc/sys/t_fsync.c
@@ -0,0 +1,125 @@
1/* $OpenBSD: t_fsync.c,v 1.1.1.1 2019/11/19 19:57:03 bluhm Exp $ */
2/* $NetBSD: t_fsync.c,v 1.2 2012/03/18 07:00:52 jruoho Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/cdefs.h>
36__RCSID("$NetBSD: t_fsync.c,v 1.2 2012/03/18 07:00:52 jruoho Exp $");
37
38#include <errno.h>
39#include <fcntl.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44
45#include "atf-c.h"
46
47ATF_TC(fsync_err);
48ATF_TC_HEAD(fsync_err, tc)
49{
50 atf_tc_set_md_var(tc, "descr",
51 "Test error conditions of fsync(2) (PR kern/30)");
52}
53
54ATF_TC_BODY(fsync_err, tc)
55{
56 int i, fd[2];
57
58 /*
59 * The fsync(2) call should fail with EBADF
60 * when the 'fd' is not a valid descriptor.
61 */
62 for (i = 1; i < 1024; i = i + 128) {
63
64 errno = 0;
65
66 ATF_REQUIRE(fsync(-i) == -1);
67 ATF_REQUIRE(errno == EBADF);
68 }
69
70 /*
71 * On the other hand, EINVAL should follow
72 * if the operation is not possible with
73 * the file descriptor.
74 */
75 ATF_REQUIRE(pipe(fd) == 0);
76
77 errno = 0;
78
79 ATF_REQUIRE(fsync(fd[0]) == -1);
80 ATF_REQUIRE(errno == EINVAL);
81
82 errno = 0;
83
84 ATF_REQUIRE(fsync(fd[1]) == -1);
85 ATF_REQUIRE(errno == EINVAL);
86
87 ATF_REQUIRE(close(fd[0]) == 0);
88 ATF_REQUIRE(close(fd[1]) == 0);
89}
90
91ATF_TC(fsync_sync);
92ATF_TC_HEAD(fsync_sync, tc)
93{
94 atf_tc_set_md_var(tc, "descr", "A basic test of fsync(2)");
95}
96
97ATF_TC_BODY(fsync_sync, tc)
98{
99 char buf[128];
100 int fd, i;
101
102 for (i = 0; i < 10; i++) {
103
104 (void)snprintf(buf, sizeof(buf), "t_fsync-%d", i);
105
106 /* Adjusted for OpenBSD, initially mkstemp(buf) */
107 fd = open(buf, O_CREAT|O_EXCL|O_RDWR, 0600);
108
109 ATF_REQUIRE(fd != -1);
110 ATF_REQUIRE(write(fd, "0", 1) == 1);
111 ATF_REQUIRE(fsync(fd) == 0);
112
113 ATF_REQUIRE(unlink(buf) == 0);
114 ATF_REQUIRE(close(fd) == 0);
115 }
116}
117
118ATF_TP_ADD_TCS(tp)
119{
120
121 ATF_TP_ADD_TC(tp, fsync_err);
122 ATF_TP_ADD_TC(tp, fsync_sync);
123
124 return atf_no_error();
125}