157718be8SEnji Cooper /*	$NetBSD: t_faccessat.c,v 1.3 2017/01/10 15:13:56 christos Exp $ */
257718be8SEnji Cooper 
357718be8SEnji Cooper /*-
457718be8SEnji Cooper  * Copyright (c) 2012 The NetBSD Foundation, Inc.
557718be8SEnji Cooper  * All rights reserved.
657718be8SEnji Cooper  *
757718be8SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper  * by Emmanuel Dreyfus.
957718be8SEnji Cooper  *
1057718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
1157718be8SEnji Cooper  * modification, are permitted provided that the following conditions
1257718be8SEnji Cooper  * are met:
1357718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
1457718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
1557718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
1657718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
1757718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
1857718be8SEnji Cooper  *
1957718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2057718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2157718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2257718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2357718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2457718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2557718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2657718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2757718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2857718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2957718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
3057718be8SEnji Cooper  */
3157718be8SEnji Cooper #include <sys/cdefs.h>
3257718be8SEnji Cooper __RCSID("$NetBSD: t_faccessat.c,v 1.3 2017/01/10 15:13:56 christos Exp $");
3357718be8SEnji Cooper 
3457718be8SEnji Cooper #include <sys/param.h>
3557718be8SEnji Cooper #include <sys/stat.h>
3657718be8SEnji Cooper #include <atf-c.h>
3757718be8SEnji Cooper #include <errno.h>
3857718be8SEnji Cooper #include <fcntl.h>
3957718be8SEnji Cooper #include <limits.h>
4057718be8SEnji Cooper #include <paths.h>
4157718be8SEnji Cooper #include <stdio.h>
4257718be8SEnji Cooper #include <string.h>
433287272dSMarcelo Araujo #include <unistd.h>
443287272dSMarcelo Araujo 
453287272dSMarcelo Araujo #define DIR "dir"
4657718be8SEnji Cooper #define FILE "dir/faccessat"
4757718be8SEnji Cooper #define BASEFILE "faccessat"
4857718be8SEnji Cooper #define LINK "dir/symlink"
4957718be8SEnji Cooper #define BASELINK "symlink"
5057718be8SEnji Cooper #define FILEERR "dir/faccessaterr"
5157718be8SEnji Cooper 
5257718be8SEnji Cooper ATF_TC(faccessat_fd);
ATF_TC_HEAD(faccessat_fd,tc)5357718be8SEnji Cooper ATF_TC_HEAD(faccessat_fd, tc)
5457718be8SEnji Cooper {
5557718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "See that faccessat works with fd");
5657718be8SEnji Cooper }
ATF_TC_BODY(faccessat_fd,tc)5757718be8SEnji Cooper ATF_TC_BODY(faccessat_fd, tc)
5857718be8SEnji Cooper {
5957718be8SEnji Cooper 	int dfd;
6057718be8SEnji Cooper 	int fd;
6157718be8SEnji Cooper 
6257718be8SEnji Cooper 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
6357718be8SEnji Cooper 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
6457718be8SEnji Cooper 	ATF_REQUIRE(close(fd) == 0);
6557718be8SEnji Cooper 
6657718be8SEnji Cooper 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
6757718be8SEnji Cooper 	ATF_REQUIRE(faccessat(dfd, BASEFILE, F_OK, 0) == 0);
6857718be8SEnji Cooper 	ATF_REQUIRE(close(dfd) == 0);
6957718be8SEnji Cooper }
7057718be8SEnji Cooper 
7157718be8SEnji Cooper ATF_TC(faccessat_fdcwd);
ATF_TC_HEAD(faccessat_fdcwd,tc)7257718be8SEnji Cooper ATF_TC_HEAD(faccessat_fdcwd, tc)
7357718be8SEnji Cooper {
7457718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
7557718be8SEnji Cooper 			  "See that faccessat works with fd as AT_FDCWD");
7657718be8SEnji Cooper }
ATF_TC_BODY(faccessat_fdcwd,tc)7757718be8SEnji Cooper ATF_TC_BODY(faccessat_fdcwd, tc)
7857718be8SEnji Cooper {
7957718be8SEnji Cooper 	int fd;
8057718be8SEnji Cooper 
8157718be8SEnji Cooper 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
8257718be8SEnji Cooper 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
8357718be8SEnji Cooper 	ATF_REQUIRE(close(fd) == 0);
8457718be8SEnji Cooper 
8557718be8SEnji Cooper 	ATF_REQUIRE(chdir(DIR) == 0);
8657718be8SEnji Cooper 	ATF_REQUIRE(faccessat(AT_FDCWD, BASEFILE, F_OK, 0) == 0);
8757718be8SEnji Cooper }
8857718be8SEnji Cooper 
8957718be8SEnji Cooper ATF_TC(faccessat_fdcwderr);
ATF_TC_HEAD(faccessat_fdcwderr,tc)9057718be8SEnji Cooper ATF_TC_HEAD(faccessat_fdcwderr, tc)
9157718be8SEnji Cooper {
9257718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
9357718be8SEnji Cooper 		  "See that faccessat fails with fd as AT_FDCWD and bad path");
9457718be8SEnji Cooper }
ATF_TC_BODY(faccessat_fdcwderr,tc)9557718be8SEnji Cooper ATF_TC_BODY(faccessat_fdcwderr, tc)
9657718be8SEnji Cooper {
9757718be8SEnji Cooper 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
9857718be8SEnji Cooper 	ATF_REQUIRE(faccessat(AT_FDCWD, FILEERR, F_OK, 0) == -1);
9957718be8SEnji Cooper }
10057718be8SEnji Cooper 
10157718be8SEnji Cooper ATF_TC(faccessat_fderr1);
ATF_TC_HEAD(faccessat_fderr1,tc)10257718be8SEnji Cooper ATF_TC_HEAD(faccessat_fderr1, tc)
10357718be8SEnji Cooper {
10457718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "See that faccessat fail with bad path");
10557718be8SEnji Cooper }
ATF_TC_BODY(faccessat_fderr1,tc)10657718be8SEnji Cooper ATF_TC_BODY(faccessat_fderr1, tc)
10757718be8SEnji Cooper {
10857718be8SEnji Cooper 	int dfd;
10957718be8SEnji Cooper 
11057718be8SEnji Cooper 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
11157718be8SEnji Cooper 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
11257718be8SEnji Cooper 	ATF_REQUIRE(faccessat(dfd, FILEERR, F_OK, 0) == -1);
11357718be8SEnji Cooper 	ATF_REQUIRE(close(dfd) == 0);
11457718be8SEnji Cooper }
11557718be8SEnji Cooper 
11657718be8SEnji Cooper ATF_TC(faccessat_fderr2);
ATF_TC_HEAD(faccessat_fderr2,tc)11757718be8SEnji Cooper ATF_TC_HEAD(faccessat_fderr2, tc)
11857718be8SEnji Cooper {
11957718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "See that faccessat fails with bad fdat");
12057718be8SEnji Cooper }
ATF_TC_BODY(faccessat_fderr2,tc)12157718be8SEnji Cooper ATF_TC_BODY(faccessat_fderr2, tc)
12257718be8SEnji Cooper {
12357718be8SEnji Cooper 	int dfd;
12457718be8SEnji Cooper 	int fd;
12557718be8SEnji Cooper 	char cwd[MAXPATHLEN];
12657718be8SEnji Cooper 
12757718be8SEnji Cooper 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
12857718be8SEnji Cooper 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
12957718be8SEnji Cooper 	ATF_REQUIRE(close(fd) == 0);
13057718be8SEnji Cooper 
13157718be8SEnji Cooper 	ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1);
13257718be8SEnji Cooper 	ATF_REQUIRE(faccessat(dfd, BASEFILE, F_OK, 0) == -1);
13357718be8SEnji Cooper 	ATF_REQUIRE(close(dfd) == 0);
13457718be8SEnji Cooper }
13557718be8SEnji Cooper 
13657718be8SEnji Cooper ATF_TC(faccessat_fderr3);
ATF_TC_HEAD(faccessat_fderr3,tc)13757718be8SEnji Cooper ATF_TC_HEAD(faccessat_fderr3, tc)
13857718be8SEnji Cooper {
13957718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "See that faccessat fails with fd as -1");
14057718be8SEnji Cooper }
ATF_TC_BODY(faccessat_fderr3,tc)14157718be8SEnji Cooper ATF_TC_BODY(faccessat_fderr3, tc)
14257718be8SEnji Cooper {
14357718be8SEnji Cooper 	int fd;
14457718be8SEnji Cooper 
14557718be8SEnji Cooper 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
14657718be8SEnji Cooper 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
14757718be8SEnji Cooper 	ATF_REQUIRE(close(fd) == 0);
14857718be8SEnji Cooper 
14957718be8SEnji Cooper 	ATF_REQUIRE(faccessat(-1, FILE, F_OK, 0) == -1);
15057718be8SEnji Cooper }
15157718be8SEnji Cooper 
15257718be8SEnji Cooper ATF_TC(faccessat_fdlink);
ATF_TC_HEAD(faccessat_fdlink,tc)15357718be8SEnji Cooper ATF_TC_HEAD(faccessat_fdlink, tc)
15457718be8SEnji Cooper {
15557718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "See that faccessat works on symlink");
15657718be8SEnji Cooper }
ATF_TC_BODY(faccessat_fdlink,tc)15757718be8SEnji Cooper ATF_TC_BODY(faccessat_fdlink, tc)
15857718be8SEnji Cooper {
15957718be8SEnji Cooper 	int dfd;
16057718be8SEnji Cooper 
16157718be8SEnji Cooper 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
16257718be8SEnji Cooper 	ATF_REQUIRE(symlink(FILE, LINK) == 0); /* NB: FILE does not exists */
16357718be8SEnji Cooper 
16457718be8SEnji Cooper 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
16557718be8SEnji Cooper 
16657718be8SEnji Cooper 	ATF_REQUIRE(faccessat(dfd, BASELINK, F_OK, 0) == -1);
16757718be8SEnji Cooper 	ATF_REQUIRE(errno == ENOENT);
16857718be8SEnji Cooper 
16957718be8SEnji Cooper #ifdef __FreeBSD__
17057718be8SEnji Cooper 	atf_tc_expect_fail("Depends on non-standard behavior not mentioned in POSIX.1-2008");
171d79f904aSJilles Tjoelker #endif
172d79f904aSJilles Tjoelker 	ATF_REQUIRE(faccessat(dfd, BASELINK, F_OK, AT_SYMLINK_NOFOLLOW) == 0);
173d79f904aSJilles Tjoelker 
17457718be8SEnji Cooper 	ATF_REQUIRE(close(dfd) == 0);
17557718be8SEnji Cooper }
17657718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)17757718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
17857718be8SEnji Cooper {
17957718be8SEnji Cooper 
18057718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, faccessat_fd);
18157718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, faccessat_fdcwd);
18257718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, faccessat_fdcwderr);
18357718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, faccessat_fderr1);
18457718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, faccessat_fderr2);
18557718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, faccessat_fderr3);
18657718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, faccessat_fdlink);
18757718be8SEnji Cooper 
18857718be8SEnji Cooper 	return atf_no_error();
18957718be8SEnji Cooper }
19057718be8SEnji Cooper