Home | History | Annotate | Line # | Download | only in c063
t_faccessat.c revision 1.1.2.2
      1 /*	$NetBSD: t_faccessat.c,v 1.1.2.2 2012/11/18 17:41:55 manu Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Emmanuel Dreyfus.
      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_faccessat.c,v 1.1.2.2 2012/11/18 17:41:55 manu Exp $");
     33 
     34 #include <atf-c.h>
     35 #include <errno.h>
     36 #include <fcntl.h>
     37 #include <limits.h>
     38 #include <paths.h>
     39 #include <stdio.h>
     40 #include <string.h>
     41 #include <unistd.h>
     42 #include <sys/param.h>
     43 
     44 #define DIR "dir"
     45 #define FILE "dir/faccessat"
     46 #define BASEFILE "faccessat"
     47 #define LINK "dir/symlink"
     48 #define BASELINK "symlink"
     49 #define FILEERR "dir/faccessaterr"
     50 
     51 ATF_TC_WITH_CLEANUP(faccessat_fd);
     52 ATF_TC_HEAD(faccessat_fd, tc)
     53 {
     54 	atf_tc_set_md_var(tc, "descr", "See that faccessat works with fd");
     55 }
     56 
     57 ATF_TC_BODY(faccessat_fd, tc)
     58 {
     59 	int dfd;
     60 	int fd;
     61 
     62 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
     63 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
     64 	ATF_REQUIRE(close(fd) == 0);
     65 
     66 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
     67 	ATF_REQUIRE(faccessat(dfd, BASEFILE, F_OK, 0) == 0);
     68 	ATF_REQUIRE(close(dfd) == 0);
     69 }
     70 
     71 ATF_TC_CLEANUP(faccessat_fd, tc)
     72 {
     73 	(void)unlink(FILE);
     74 	(void)unlink(FILEERR);
     75 	(void)rmdir(DIR);
     76 }
     77 
     78 ATF_TC_WITH_CLEANUP(faccessat_fdcwd);
     79 ATF_TC_HEAD(faccessat_fdcwd, tc)
     80 {
     81 	atf_tc_set_md_var(tc, "descr",
     82 			  "See that faccessat works with fd as AT_FDCWD");
     83 }
     84 
     85 ATF_TC_BODY(faccessat_fdcwd, tc)
     86 {
     87 	int fd;
     88 
     89 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
     90 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
     91 	ATF_REQUIRE(close(fd) == 0);
     92 
     93 	ATF_REQUIRE(chdir(DIR) == 0);
     94 	ATF_REQUIRE(faccessat(AT_FDCWD, BASEFILE, F_OK, 0) == 0);
     95 }
     96 
     97 ATF_TC_CLEANUP(faccessat_fdcwd, tc)
     98 {
     99 	(void)unlink(FILE);
    100 	(void)unlink(FILEERR);
    101 	(void)rmdir(DIR);
    102 }
    103 
    104 ATF_TC_WITH_CLEANUP(faccessat_fdcwderr);
    105 ATF_TC_HEAD(faccessat_fdcwderr, tc)
    106 {
    107 	atf_tc_set_md_var(tc, "descr",
    108 		  "See that faccessat fails with fd as AT_FDCWD and bad path");
    109 }
    110 
    111 ATF_TC_BODY(faccessat_fdcwderr, tc)
    112 {
    113 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
    114 	ATF_REQUIRE(faccessat(AT_FDCWD, FILEERR, F_OK, 0) == -1);
    115 }
    116 
    117 ATF_TC_CLEANUP(faccessat_fdcwderr, tc)
    118 {
    119 	(void)unlink(FILE);
    120 	(void)unlink(FILEERR);
    121 	(void)rmdir(DIR);
    122 }
    123 
    124 ATF_TC_WITH_CLEANUP(faccessat_fderr1);
    125 ATF_TC_HEAD(faccessat_fderr1, tc)
    126 {
    127 	atf_tc_set_md_var(tc, "descr", "See that faccessat fail with bad path");
    128 }
    129 
    130 ATF_TC_BODY(faccessat_fderr1, tc)
    131 {
    132 	int dfd;
    133 
    134 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
    135 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
    136 	ATF_REQUIRE(faccessat(dfd, FILEERR, F_OK, 0) == -1);
    137 	ATF_REQUIRE(close(dfd) == 0);
    138 
    139 }
    140 
    141 ATF_TC_CLEANUP(faccessat_fderr1, tc)
    142 {
    143 	(void)unlink(FILE);
    144 	(void)unlink(FILEERR);
    145 	(void)rmdir(DIR);
    146 }
    147 
    148 ATF_TC_WITH_CLEANUP(faccessat_fderr2);
    149 ATF_TC_HEAD(faccessat_fderr2, tc)
    150 {
    151 	atf_tc_set_md_var(tc, "descr", "See that faccessat fails with bad fdat");
    152 }
    153 
    154 ATF_TC_BODY(faccessat_fderr2, tc)
    155 {
    156 	int dfd;
    157 	int fd;
    158 	char cwd[MAXPATHLEN];
    159 
    160 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
    161 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
    162 	ATF_REQUIRE(close(fd) == 0);
    163 
    164 	ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1);
    165 	ATF_REQUIRE(faccessat(dfd, BASEFILE, F_OK, 0) == -1);
    166 	ATF_REQUIRE(close(dfd) == 0);
    167 }
    168 
    169 ATF_TC_CLEANUP(faccessat_fderr2, tc)
    170 {
    171 	(void)unlink(FILE);
    172 	(void)unlink(FILEERR);
    173 	(void)rmdir(DIR);
    174 }
    175 
    176 ATF_TC_WITH_CLEANUP(faccessat_fderr3);
    177 ATF_TC_HEAD(faccessat_fderr3, tc)
    178 {
    179 	atf_tc_set_md_var(tc, "descr", "See that faccessat fails with fd as -1");
    180 }
    181 
    182 ATF_TC_BODY(faccessat_fderr3, tc)
    183 {
    184 	int fd;
    185 
    186 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
    187 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
    188 	ATF_REQUIRE(close(fd) == 0);
    189 
    190 	ATF_REQUIRE(faccessat(-1, FILE, F_OK, 0) == -1);
    191 }
    192 
    193 ATF_TC_CLEANUP(faccessat_fderr3, tc)
    194 {
    195 	(void)unlink(FILE);
    196 	(void)unlink(FILEERR);
    197 	(void)rmdir(DIR);
    198 }
    199 
    200 ATF_TC_WITH_CLEANUP(faccessat_fdlink);
    201 ATF_TC_HEAD(faccessat_fdlink, tc)
    202 {
    203 	atf_tc_set_md_var(tc, "descr", "See that faccessat works on symlink");
    204 }
    205 
    206 ATF_TC_BODY(faccessat_fdlink, tc)
    207 {
    208 	int dfd;
    209 
    210 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
    211 	ATF_REQUIRE(symlink(FILE, LINK) == 0); /* NB: FILE does not exists */
    212 
    213 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
    214 
    215 	ATF_REQUIRE(faccessat(dfd, BASELINK, F_OK, 0) == -1);
    216 	ATF_REQUIRE(errno == ENOENT);
    217 
    218 	ATF_REQUIRE(faccessat(dfd, BASELINK, F_OK, AT_SYMLINK_NOFOLLOW) == 0);
    219 
    220 	ATF_REQUIRE(close(dfd) == 0);
    221 }
    222 
    223 ATF_TC_CLEANUP(faccessat_fdlink, tc)
    224 {
    225 	(void)unlink(LINK);
    226 	(void)rmdir(DIR);
    227 }
    228 
    229 ATF_TP_ADD_TCS(tp)
    230 {
    231 
    232 	ATF_TP_ADD_TC(tp, faccessat_fd);
    233 	ATF_TP_ADD_TC(tp, faccessat_fdcwd);
    234 	ATF_TP_ADD_TC(tp, faccessat_fdcwderr);
    235 	ATF_TP_ADD_TC(tp, faccessat_fderr1);
    236 	ATF_TP_ADD_TC(tp, faccessat_fderr2);
    237 	ATF_TP_ADD_TC(tp, faccessat_fderr3);
    238 	ATF_TP_ADD_TC(tp, faccessat_fdlink);
    239 
    240 	return atf_no_error();
    241 }
    242