t_closefrom.c revision 1.3
11.3Sjruoho/* $NetBSD: t_closefrom.c,v 1.3 2011/05/09 11:05:36 jruoho Exp $ */ 21.1Sjruoho 31.1Sjruoho/*- 41.1Sjruoho * Copyright (c) 2011 The NetBSD Foundation, Inc. 51.1Sjruoho * All rights reserved. 61.1Sjruoho * 71.1Sjruoho * This code is derived from software contributed to The NetBSD Foundation 81.1Sjruoho * by Jukka Ruohonen. 91.1Sjruoho * 101.1Sjruoho * Redistribution and use in source and binary forms, with or without 111.1Sjruoho * modification, are permitted provided that the following conditions 121.1Sjruoho * are met: 131.1Sjruoho * 1. Redistributions of source code must retain the above copyright 141.1Sjruoho * notice, this list of conditions and the following disclaimer. 151.1Sjruoho * 2. Redistributions in binary form must reproduce the above copyright 161.1Sjruoho * notice, this list of conditions and the following disclaimer in the 171.1Sjruoho * documentation and/or other materials provided with the distribution. 181.1Sjruoho * 191.1Sjruoho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 201.1Sjruoho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 211.1Sjruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 221.1Sjruoho * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 231.1Sjruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 241.1Sjruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 251.1Sjruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 261.1Sjruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 271.1Sjruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 281.1Sjruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 291.1Sjruoho * POSSIBILITY OF SUCH DAMAGE. 301.1Sjruoho */ 311.1Sjruoho#include <sys/cdefs.h> 321.3Sjruoho__RCSID("$NetBSD: t_closefrom.c,v 1.3 2011/05/09 11:05:36 jruoho Exp $"); 331.1Sjruoho 341.1Sjruoho#include <atf-c.h> 351.1Sjruoho#include <errno.h> 361.1Sjruoho#include <fcntl.h> 371.1Sjruoho#include <limits.h> 381.1Sjruoho#include <unistd.h> 391.1Sjruoho 401.1Sjruohostatic const char path[] = "closefrom"; 411.1Sjruoho 421.1SjruohoATF_TC_WITH_CLEANUP(closefrom_basic); 431.1SjruohoATF_TC_HEAD(closefrom_basic, tc) 441.1Sjruoho{ 451.1Sjruoho atf_tc_set_md_var(tc, "descr", "A basic test of closefrom(3), #1"); 461.1Sjruoho} 471.1Sjruoho 481.1SjruohoATF_TC_BODY(closefrom_basic, tc) 491.1Sjruoho{ 501.1Sjruoho int fd, cur1, cur2; 511.1Sjruoho 521.1Sjruoho fd = open(path, O_RDONLY | O_CREAT, 0400); 531.1Sjruoho ATF_REQUIRE(fd >= 0); 541.1Sjruoho 551.1Sjruoho cur1 = fcntl(0, F_MAXFD); 561.1Sjruoho 571.1Sjruoho ATF_REQUIRE(cur1 > 0); 581.1Sjruoho ATF_REQUIRE(closefrom(cur1) == 0); 591.1Sjruoho 601.1Sjruoho cur2 = fcntl(0, F_MAXFD); 611.1Sjruoho 621.1Sjruoho ATF_REQUIRE(cur2 >= 0); 631.1Sjruoho ATF_REQUIRE(cur1 - 1 == cur2); 641.3Sjruoho 651.3Sjruoho ATF_REQUIRE(close(fd) == -1); 661.1Sjruoho ATF_REQUIRE(unlink(path) == 0); 671.1Sjruoho} 681.1Sjruoho 691.1SjruohoATF_TC_CLEANUP(closefrom_basic, tc) 701.1Sjruoho{ 711.1Sjruoho (void)unlink(path); 721.1Sjruoho} 731.1Sjruoho 741.1SjruohoATF_TC_WITH_CLEANUP(closefrom_buffer); 751.1SjruohoATF_TC_HEAD(closefrom_buffer, tc) 761.1Sjruoho{ 771.1Sjruoho atf_tc_set_md_var(tc, "descr", "A basic test of closefrom(3), #2"); 781.1Sjruoho} 791.1Sjruoho 801.1SjruohoATF_TC_BODY(closefrom_buffer, tc) 811.1Sjruoho{ 821.1Sjruoho int buf[16], cur, half; 831.1Sjruoho size_t i; 841.1Sjruoho 851.1Sjruoho /* 861.1Sjruoho * Open a buffer of descriptors, close the half of 871.1Sjruoho * these and verify that the result is consistent. 881.1Sjruoho */ 891.1Sjruoho ATF_REQUIRE(closefrom(STDERR_FILENO + 1) == 0); 901.1Sjruoho 911.1Sjruoho cur = fcntl(0, F_MAXFD); 921.1Sjruoho ATF_REQUIRE(cur == STDERR_FILENO); 931.1Sjruoho 941.1Sjruoho for (i = 0; i < __arraycount(buf); i++) { 951.1Sjruoho buf[i] = open(path, O_RDWR | O_CREAT, 0600); 961.1Sjruoho ATF_REQUIRE(buf[i] >= 0); 971.1Sjruoho } 981.1Sjruoho 991.1Sjruoho cur = fcntl(0, F_MAXFD); 1001.1Sjruoho ATF_REQUIRE(cur == __arraycount(buf) + STDERR_FILENO); 1011.1Sjruoho 1021.1Sjruoho half = STDERR_FILENO + __arraycount(buf) / 2; 1031.1Sjruoho ATF_REQUIRE(closefrom(half) == 0); 1041.1Sjruoho 1051.1Sjruoho cur = fcntl(0, F_MAXFD); 1061.1Sjruoho ATF_REQUIRE(cur == half - 1); 1071.1Sjruoho 1081.1Sjruoho for (i = 0; i < __arraycount(buf); i++) 1091.1Sjruoho (void)close(buf[i]); 1101.1Sjruoho} 1111.1Sjruoho 1121.1SjruohoATF_TC_CLEANUP(closefrom_buffer, tc) 1131.1Sjruoho{ 1141.1Sjruoho (void)unlink(path); 1151.1Sjruoho} 1161.1Sjruoho 1171.1SjruohoATF_TC(closefrom_err); 1181.1SjruohoATF_TC_HEAD(closefrom_err, tc) 1191.1Sjruoho{ 1201.1Sjruoho atf_tc_set_md_var(tc, "descr", "Test errors from closefrom(3)"); 1211.1Sjruoho} 1221.1Sjruoho 1231.1SjruohoATF_TC_BODY(closefrom_err, tc) 1241.1Sjruoho{ 1251.1Sjruoho 1261.1Sjruoho errno = 0; 1271.1Sjruoho ATF_REQUIRE_ERRNO(EBADF, closefrom(-INT_MAX) == -1); 1281.1Sjruoho} 1291.1Sjruoho 1301.1SjruohoATF_TP_ADD_TCS(tp) 1311.1Sjruoho{ 1321.1Sjruoho 1331.1Sjruoho ATF_TP_ADD_TC(tp, closefrom_basic); 1341.1Sjruoho ATF_TP_ADD_TC(tp, closefrom_buffer); 1351.1Sjruoho ATF_TP_ADD_TC(tp, closefrom_err); 1361.1Sjruoho 1371.1Sjruoho return atf_no_error(); 1381.1Sjruoho} 139