xref: /freebsd/contrib/netbsd-tests/lib/libc/c063/t_unlinkat.c (revision 8a6fe8ce60ab99778558c4951d23615a0141daf0)
1*ddba0402SEnji Cooper /*	$NetBSD: t_unlinkat.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>
32*ddba0402SEnji Cooper __RCSID("$NetBSD: t_unlinkat.c,v 1.3 2017/01/10 15:13:56 christos Exp $");
3357718be8SEnji Cooper 
34*ddba0402SEnji Cooper #include <sys/param.h>
35*ddba0402SEnji 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>
4357718be8SEnji Cooper #include <unistd.h>
4457718be8SEnji Cooper 
4557718be8SEnji Cooper #define DIR "dir"
4657718be8SEnji Cooper #define FILE "dir/unlinkat"
4757718be8SEnji Cooper #define BASEFILE "unlinkat"
4857718be8SEnji Cooper #define FILEERR "dir/unlinkaterr"
4957718be8SEnji Cooper 
5057718be8SEnji Cooper ATF_TC(unlinkat_fd);
ATF_TC_HEAD(unlinkat_fd,tc)5157718be8SEnji Cooper ATF_TC_HEAD(unlinkat_fd, tc)
5257718be8SEnji Cooper {
5357718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "See that unlinkat works with fd");
5457718be8SEnji Cooper }
ATF_TC_BODY(unlinkat_fd,tc)5557718be8SEnji Cooper ATF_TC_BODY(unlinkat_fd, tc)
5657718be8SEnji Cooper {
5757718be8SEnji Cooper 	int dfd;
5857718be8SEnji Cooper 	int fd;
5957718be8SEnji Cooper 
6057718be8SEnji Cooper 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
6157718be8SEnji Cooper 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
6257718be8SEnji Cooper 	ATF_REQUIRE(close(fd) == 0);
6357718be8SEnji Cooper 
6457718be8SEnji Cooper 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
6557718be8SEnji Cooper 	ATF_REQUIRE(unlinkat(dfd, BASEFILE, 0) == 0);
6657718be8SEnji Cooper 	ATF_REQUIRE(close(dfd) == 0);
6757718be8SEnji Cooper }
6857718be8SEnji Cooper 
6957718be8SEnji Cooper ATF_TC(unlinkat_fdcwd);
ATF_TC_HEAD(unlinkat_fdcwd,tc)7057718be8SEnji Cooper ATF_TC_HEAD(unlinkat_fdcwd, tc)
7157718be8SEnji Cooper {
7257718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
7357718be8SEnji Cooper 			  "See that unlinkat works with fd as AT_FDCWD");
7457718be8SEnji Cooper }
ATF_TC_BODY(unlinkat_fdcwd,tc)7557718be8SEnji Cooper ATF_TC_BODY(unlinkat_fdcwd, tc)
7657718be8SEnji Cooper {
7757718be8SEnji Cooper 	int fd;
7857718be8SEnji Cooper 
7957718be8SEnji Cooper 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
8057718be8SEnji Cooper 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
8157718be8SEnji Cooper 	ATF_REQUIRE(close(fd) == 0);
8257718be8SEnji Cooper 
8357718be8SEnji Cooper 	ATF_REQUIRE(chdir(DIR) == 0);
8457718be8SEnji Cooper 	ATF_REQUIRE(unlinkat(AT_FDCWD, BASEFILE, 0) == 0);
8557718be8SEnji Cooper }
8657718be8SEnji Cooper 
8757718be8SEnji Cooper ATF_TC(unlinkat_fdcwderr);
ATF_TC_HEAD(unlinkat_fdcwderr,tc)8857718be8SEnji Cooper ATF_TC_HEAD(unlinkat_fdcwderr, tc)
8957718be8SEnji Cooper {
9057718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
9157718be8SEnji Cooper 		  "See that unlinkat fails with fd as AT_FDCWD and bad path");
9257718be8SEnji Cooper }
ATF_TC_BODY(unlinkat_fdcwderr,tc)9357718be8SEnji Cooper ATF_TC_BODY(unlinkat_fdcwderr, tc)
9457718be8SEnji Cooper {
9557718be8SEnji Cooper 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
9657718be8SEnji Cooper 	ATF_REQUIRE(unlinkat(AT_FDCWD, FILEERR, 0) == -1);
9757718be8SEnji Cooper }
9857718be8SEnji Cooper 
9957718be8SEnji Cooper ATF_TC(unlinkat_fderr1);
ATF_TC_HEAD(unlinkat_fderr1,tc)10057718be8SEnji Cooper ATF_TC_HEAD(unlinkat_fderr1, tc)
10157718be8SEnji Cooper {
10257718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "See that unlinkat fail with bad path");
10357718be8SEnji Cooper }
ATF_TC_BODY(unlinkat_fderr1,tc)10457718be8SEnji Cooper ATF_TC_BODY(unlinkat_fderr1, tc)
10557718be8SEnji Cooper {
10657718be8SEnji Cooper 	int dfd;
10757718be8SEnji Cooper 
10857718be8SEnji Cooper 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
10957718be8SEnji Cooper 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
11057718be8SEnji Cooper 	ATF_REQUIRE(unlinkat(dfd, FILEERR, 0) == -1);
11157718be8SEnji Cooper 	ATF_REQUIRE(close(dfd) == 0);
11257718be8SEnji Cooper }
11357718be8SEnji Cooper 
11457718be8SEnji Cooper ATF_TC(unlinkat_fderr2);
ATF_TC_HEAD(unlinkat_fderr2,tc)11557718be8SEnji Cooper ATF_TC_HEAD(unlinkat_fderr2, tc)
11657718be8SEnji Cooper {
11757718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "See that unlinkat fails with bad fdat");
11857718be8SEnji Cooper }
ATF_TC_BODY(unlinkat_fderr2,tc)11957718be8SEnji Cooper ATF_TC_BODY(unlinkat_fderr2, tc)
12057718be8SEnji Cooper {
12157718be8SEnji Cooper 	int dfd;
12257718be8SEnji Cooper 	int fd;
12357718be8SEnji Cooper 	char cwd[MAXPATHLEN];
12457718be8SEnji Cooper 
12557718be8SEnji Cooper 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
12657718be8SEnji Cooper 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
12757718be8SEnji Cooper 	ATF_REQUIRE(close(fd) == 0);
12857718be8SEnji Cooper 
12957718be8SEnji Cooper 	ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1);
13057718be8SEnji Cooper 	ATF_REQUIRE(unlinkat(dfd, BASEFILE, 0) == -1);
13157718be8SEnji Cooper 	ATF_REQUIRE(close(dfd) == 0);
13257718be8SEnji Cooper }
13357718be8SEnji Cooper 
13457718be8SEnji Cooper ATF_TC(unlinkat_fderr3);
ATF_TC_HEAD(unlinkat_fderr3,tc)13557718be8SEnji Cooper ATF_TC_HEAD(unlinkat_fderr3, tc)
13657718be8SEnji Cooper {
13757718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "See that unlinkat fails with fd as -1");
13857718be8SEnji Cooper }
ATF_TC_BODY(unlinkat_fderr3,tc)13957718be8SEnji Cooper ATF_TC_BODY(unlinkat_fderr3, tc)
14057718be8SEnji Cooper {
14157718be8SEnji Cooper 	int fd;
14257718be8SEnji Cooper 
14357718be8SEnji Cooper 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
14457718be8SEnji Cooper 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
14557718be8SEnji Cooper 	ATF_REQUIRE(close(fd) == 0);
14657718be8SEnji Cooper 
14757718be8SEnji Cooper 	ATF_REQUIRE(unlinkat(-1, FILE, 0) == -1);
14857718be8SEnji Cooper }
14957718be8SEnji Cooper 
15057718be8SEnji Cooper ATF_TC(unlinkat_dir);
ATF_TC_HEAD(unlinkat_dir,tc)15157718be8SEnji Cooper ATF_TC_HEAD(unlinkat_dir, tc)
15257718be8SEnji Cooper {
15357718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
15457718be8SEnji Cooper 			  "See that unlinkat can remove directories");
15557718be8SEnji Cooper }
ATF_TC_BODY(unlinkat_dir,tc)15657718be8SEnji Cooper ATF_TC_BODY(unlinkat_dir, tc)
15757718be8SEnji Cooper {
15857718be8SEnji Cooper 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
15957718be8SEnji Cooper 
16057718be8SEnji Cooper 	ATF_REQUIRE(unlinkat(AT_FDCWD, DIR, 0) == -1);
16157718be8SEnji Cooper 	ATF_REQUIRE(errno == EPERM);
16257718be8SEnji Cooper 	ATF_REQUIRE(unlinkat(AT_FDCWD, DIR, AT_REMOVEDIR) == 0);
16357718be8SEnji Cooper }
16457718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)16557718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
16657718be8SEnji Cooper {
16757718be8SEnji Cooper 
16857718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, unlinkat_fd);
16957718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, unlinkat_fdcwd);
17057718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, unlinkat_fdcwderr);
17157718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, unlinkat_fderr1);
17257718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, unlinkat_fderr2);
17357718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, unlinkat_fderr3);
17457718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, unlinkat_dir);
17557718be8SEnji Cooper 
17657718be8SEnji Cooper 	return atf_no_error();
17757718be8SEnji Cooper }
178