1 /* $NetBSD: t_fchownat.c,v 1.3 2013/03/17 04:46:06 jmmv Exp $ */
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_fchownat.c,v 1.3 2013/03/17 04:46:06 jmmv Exp $");
43 #include <sys/param.h>
46 #define FILE "dir/fchownat"
47 #define BASEFILE "fchownat"
48 #define LINK "dir/symlink"
49 #define BASELINK "symlink"
50 #define FILEERR "dir/fchownaterr"
53 static int getuser(uid_t
*, gid_t
*);
55 static int getuser(uid_t
*uid
, gid_t
*gid
)
59 if ((pw
= getpwnam(USER
)) == NULL
)
69 ATF_TC_HEAD(fchownat_fd
, tc
)
71 atf_tc_set_md_var(tc
, "descr", "See that fchownat works with fd");
72 atf_tc_set_md_var(tc
, "require.user", "root");
74 ATF_TC_BODY(fchownat_fd
, tc
)
82 ATF_REQUIRE(getuser(&uid
, &gid
) == 0);
83 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
84 ATF_REQUIRE((fd
= open(FILE, O_CREAT
|O_RDWR
, 0644)) != -1);
85 ATF_REQUIRE(close(fd
) == 0);
87 ATF_REQUIRE((dfd
= open(DIR, O_RDONLY
, 0)) != -1);
88 ATF_REQUIRE(fchownat(dfd
, BASEFILE
, uid
, gid
, 0) == 0);
89 ATF_REQUIRE(close(dfd
) == 0);
91 ATF_REQUIRE(stat(FILE, &st
) == 0);
92 ATF_REQUIRE(st
.st_uid
== uid
);
93 ATF_REQUIRE(st
.st_gid
== gid
);
96 ATF_TC(fchownat_fdcwd
);
97 ATF_TC_HEAD(fchownat_fdcwd
, tc
)
99 atf_tc_set_md_var(tc
, "descr",
100 "See that fchownat works with fd as AT_FDCWD");
101 atf_tc_set_md_var(tc
, "require.user", "root");
103 ATF_TC_BODY(fchownat_fdcwd
, tc
)
110 ATF_REQUIRE(getuser(&uid
, &gid
) == 0);
111 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
112 ATF_REQUIRE((fd
= open(FILE, O_CREAT
|O_RDWR
, 0644)) != -1);
113 ATF_REQUIRE(close(fd
) == 0);
115 ATF_REQUIRE(chdir(DIR) == 0);
116 ATF_REQUIRE(fchownat(AT_FDCWD
, BASEFILE
, uid
, gid
, 0) == 0);
118 ATF_REQUIRE(stat(BASEFILE
, &st
) == 0);
119 ATF_REQUIRE(st
.st_uid
== uid
);
120 ATF_REQUIRE(st
.st_gid
== gid
);
123 ATF_TC(fchownat_fdcwderr
);
124 ATF_TC_HEAD(fchownat_fdcwderr
, tc
)
126 atf_tc_set_md_var(tc
, "descr",
127 "See that fchownat fails with fd as AT_FDCWD and bad path");
128 atf_tc_set_md_var(tc
, "require.user", "root");
130 ATF_TC_BODY(fchownat_fdcwderr
, tc
)
135 ATF_REQUIRE(getuser(&uid
, &gid
) == 0);
136 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
137 ATF_REQUIRE(fchownat(AT_FDCWD
, FILEERR
, uid
, gid
, 0) == -1);
140 ATF_TC(fchownat_fderr1
);
141 ATF_TC_HEAD(fchownat_fderr1
, tc
)
143 atf_tc_set_md_var(tc
, "descr", "See that fchownat fail with bad path");
144 atf_tc_set_md_var(tc
, "require.user", "root");
146 ATF_TC_BODY(fchownat_fderr1
, tc
)
152 ATF_REQUIRE(getuser(&uid
, &gid
) == 0);
153 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
154 ATF_REQUIRE((dfd
= open(DIR, O_RDONLY
, 0)) != -1);
155 ATF_REQUIRE(fchownat(dfd
, FILEERR
, uid
, gid
, 0) == -1);
156 ATF_REQUIRE(close(dfd
) == 0);
159 ATF_TC(fchownat_fderr2
);
160 ATF_TC_HEAD(fchownat_fderr2
, tc
)
162 atf_tc_set_md_var(tc
, "descr", "See that fchownat fails with bad fdat");
163 atf_tc_set_md_var(tc
, "require.user", "root");
165 ATF_TC_BODY(fchownat_fderr2
, tc
)
169 char cwd
[MAXPATHLEN
];
173 ATF_REQUIRE(getuser(&uid
, &gid
) == 0);
174 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
175 ATF_REQUIRE((fd
= open(FILE, O_CREAT
|O_RDWR
, 0644)) != -1);
176 ATF_REQUIRE(close(fd
) == 0);
178 ATF_REQUIRE((dfd
= open(getcwd(cwd
, MAXPATHLEN
), O_RDONLY
, 0)) != -1);
179 ATF_REQUIRE(fchownat(dfd
, BASEFILE
, uid
, gid
, 0) == -1);
180 ATF_REQUIRE(close(dfd
) == 0);
183 ATF_TC(fchownat_fderr3
);
184 ATF_TC_HEAD(fchownat_fderr3
, tc
)
186 atf_tc_set_md_var(tc
, "descr", "See that fchownat fails with fd as -1");
187 atf_tc_set_md_var(tc
, "require.user", "root");
189 ATF_TC_BODY(fchownat_fderr3
, tc
)
195 ATF_REQUIRE(getuser(&uid
, &gid
) == 0);
196 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
197 ATF_REQUIRE((fd
= open(FILE, O_CREAT
|O_RDWR
, 0644)) != -1);
198 ATF_REQUIRE(close(fd
) == 0);
200 ATF_REQUIRE(fchownat(-1, FILE, uid
, gid
, 0) == -1);
203 ATF_TC(fchownat_fdlink
);
204 ATF_TC_HEAD(fchownat_fdlink
, tc
)
206 atf_tc_set_md_var(tc
, "descr", "See that fchownat works on symlink");
207 atf_tc_set_md_var(tc
, "require.user", "root");
209 ATF_TC_BODY(fchownat_fdlink
, tc
)
216 ATF_REQUIRE(getuser(&uid
, &gid
) == 0);
217 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
218 ATF_REQUIRE(symlink(FILE, LINK
) == 0); /* Target does not exists */
220 ATF_REQUIRE((dfd
= open(DIR, O_RDONLY
, 0)) != -1);
222 ATF_REQUIRE(fchownat(dfd
, BASELINK
, uid
, gid
, 0) == -1);
223 ATF_REQUIRE(errno
== ENOENT
);
225 ATF_REQUIRE(fchownat(dfd
, BASELINK
, uid
, gid
,
226 AT_SYMLINK_NOFOLLOW
) == 0);
228 ATF_REQUIRE(close(dfd
) == 0);
230 ATF_REQUIRE(lstat(LINK
, &st
) == 0);
231 ATF_REQUIRE(st
.st_uid
== uid
);
232 ATF_REQUIRE(st
.st_gid
== gid
);
238 ATF_TP_ADD_TC(tp
, fchownat_fd
);
239 ATF_TP_ADD_TC(tp
, fchownat_fdcwd
);
240 ATF_TP_ADD_TC(tp
, fchownat_fdcwderr
);
241 ATF_TP_ADD_TC(tp
, fchownat_fderr1
);
242 ATF_TP_ADD_TC(tp
, fchownat_fderr2
);
243 ATF_TP_ADD_TC(tp
, fchownat_fderr3
);
244 ATF_TP_ADD_TC(tp
, fchownat_fdlink
);
246 return atf_no_error();