t_closefrom.c revision 1.2
1/* $NetBSD: t_closefrom.c,v 1.2 2011/05/09 10:50:02 jruoho Exp $ */ 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_closefrom.c,v 1.2 2011/05/09 10:50:02 jruoho Exp $"); 33 34#include <atf-c.h> 35#include <errno.h> 36#include <fcntl.h> 37#include <limits.h> 38#include <unistd.h> 39 40static const char path[] = "closefrom"; 41 42ATF_TC_WITH_CLEANUP(closefrom_basic); 43ATF_TC_HEAD(closefrom_basic, tc) 44{ 45 atf_tc_set_md_var(tc, "descr", "A basic test of closefrom(3), #1"); 46} 47 48ATF_TC_BODY(closefrom_basic, tc) 49{ 50 int fd, cur1, cur2; 51 52 fd = open(path, O_RDONLY | O_CREAT, 0400); 53 ATF_REQUIRE(fd >= 0); 54 55 cur1 = fcntl(0, F_MAXFD); 56 57 ATF_REQUIRE(cur1 > 0); 58 ATF_REQUIRE(closefrom(cur1) == 0); 59 60 cur2 = fcntl(0, F_MAXFD); 61 62 ATF_REQUIRE(cur2 >= 0); 63 ATF_REQUIRE(cur1 - 1 == cur2); 64 ATF_REQUIRE(unlink(path) == 0); 65} 66 67ATF_TC_CLEANUP(closefrom_basic, tc) 68{ 69 (void)unlink(path); 70} 71 72ATF_TC_WITH_CLEANUP(closefrom_buffer); 73ATF_TC_HEAD(closefrom_buffer, tc) 74{ 75 atf_tc_set_md_var(tc, "descr", "A basic test of closefrom(3), #2"); 76} 77 78ATF_TC_BODY(closefrom_buffer, tc) 79{ 80 int buf[16], cur, half; 81 size_t i; 82 83 /* 84 * Open a buffer of descriptors, close the half of 85 * these and verify that the result is consistent. 86 */ 87 ATF_REQUIRE(closefrom(STDERR_FILENO + 1) == 0); 88 89 cur = fcntl(0, F_MAXFD); 90 ATF_REQUIRE(cur == STDERR_FILENO); 91 92 for (i = 0; i < __arraycount(buf); i++) { 93 buf[i] = open(path, O_RDWR | O_CREAT, 0600); 94 ATF_REQUIRE(buf[i] >= 0); 95 } 96 97 cur = fcntl(0, F_MAXFD); 98 ATF_REQUIRE(cur == __arraycount(buf) + STDERR_FILENO); 99 100 half = STDERR_FILENO + __arraycount(buf) / 2; 101 ATF_REQUIRE(closefrom(half) == 0); 102 103 cur = fcntl(0, F_MAXFD); 104 ATF_REQUIRE(cur == half - 1); 105 106 for (i = 0; i < __arraycount(buf); i++) 107 (void)close(buf[i]); 108} 109 110ATF_TC_CLEANUP(closefrom_buffer, tc) 111{ 112 (void)unlink(path); 113} 114 115ATF_TC(closefrom_err); 116ATF_TC_HEAD(closefrom_err, tc) 117{ 118 atf_tc_set_md_var(tc, "descr", "Test errors from closefrom(3)"); 119} 120 121ATF_TC_BODY(closefrom_err, tc) 122{ 123 124 errno = 0; 125 ATF_REQUIRE_ERRNO(EBADF, closefrom(-INT_MAX) == -1); 126} 127 128ATF_TP_ADD_TCS(tp) 129{ 130 131 ATF_TP_ADD_TC(tp, closefrom_basic); 132 ATF_TP_ADD_TC(tp, closefrom_buffer); 133 ATF_TP_ADD_TC(tp, closefrom_err); 134 135 return atf_no_error(); 136} 137