1 /* $OpenBSD: t_fsync.c,v 1.3 2021/12/13 16:56:48 deraadt 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 <errno.h> 36 #include <fcntl.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.h> 41 42 #include "atf-c.h" 43 44 ATF_TC(fsync_err); 45 ATF_TC_HEAD(fsync_err, tc) 46 { 47 atf_tc_set_md_var(tc, "descr", 48 "Test error conditions of fsync(2) (PR kern/30)"); 49 } 50 51 ATF_TC_BODY(fsync_err, tc) 52 { 53 int i, fd[2]; 54 55 /* 56 * The fsync(2) call should fail with EBADF 57 * when the 'fd' is not a valid descriptor. 58 */ 59 for (i = 1; i < 1024; i = i + 128) { 60 61 errno = 0; 62 63 ATF_REQUIRE(fsync(-i) == -1); 64 ATF_REQUIRE(errno == EBADF); 65 } 66 67 /* 68 * On the other hand, EINVAL should follow 69 * if the operation is not possible with 70 * the file descriptor. 71 */ 72 ATF_REQUIRE(pipe(fd) == 0); 73 74 errno = 0; 75 76 ATF_REQUIRE(fsync(fd[0]) == -1); 77 ATF_REQUIRE(errno == EINVAL); 78 79 errno = 0; 80 81 ATF_REQUIRE(fsync(fd[1]) == -1); 82 ATF_REQUIRE(errno == EINVAL); 83 84 ATF_REQUIRE(close(fd[0]) == 0); 85 ATF_REQUIRE(close(fd[1]) == 0); 86 } 87 88 ATF_TC(fsync_sync); 89 ATF_TC_HEAD(fsync_sync, tc) 90 { 91 atf_tc_set_md_var(tc, "descr", "A basic test of fsync(2)"); 92 } 93 94 ATF_TC_BODY(fsync_sync, tc) 95 { 96 char buf[128]; 97 int fd, i; 98 99 for (i = 0; i < 10; i++) { 100 101 (void)snprintf(buf, sizeof(buf), "t_fsync-%d", i); 102 103 #ifdef __OpenBSD__ 104 fd = open(buf, O_CREAT|O_EXCL|O_RDWR, 0600); 105 #else 106 fd = mkstemp(buf); 107 #endif 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 118 ATF_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 } 126