1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <sys/stat.h>
19 #include "functionalext.h"
20 #include "filepath_util.h"
21
22 /**
23 * @tc.name : fchownat_0100
24 * @tc.desc : Parameter fd is not equal to AT_FDCWD, the user and group that can change the owner of the file.
25 * @tc.level : Level 0
26 */
fchownat_0100(void)27 void fchownat_0100(void)
28 {
29 char ptr[PATH_MAX] = {0};
30 FILE_ABSOLUTE_PATH(STR_FCHOWNAT_TEST_TXT, ptr);
31 int fd = open(ptr, O_RDWR | O_CREAT, TEST_MODE);
32 EXPECT_TRUE("fchownat_0100", fd >= 0);
33 int ret = fchownat(fd, ptr, 0, 0, AT_SYMLINK_NOFOLLOW);
34 EXPECT_EQ("fchownat_0100", ret, 0);
35 struct stat buf;
36 int st = stat(ptr, &buf);
37 EXPECT_EQ("fchownat_0100", st, 0);
38 EXPECT_EQ("fchownat_0100", buf.st_uid, 0);
39 EXPECT_EQ("fchownat_0100", buf.st_gid, 0);
40 close(fd);
41 remove(ptr);
42 }
43
44 /**
45 * @tc.name : fchownat_0200
46 * @tc.desc : Parameter fd equal to AT_FDCWD, the user and group that can change the owner of the file.
47 * @tc.level : Level 1
48 */
fchownat_0200(void)49 void fchownat_0200(void)
50 {
51 char ptr[PATH_MAX] = {0};
52 FILE_ABSOLUTE_PATH(STR_FCHOWNAT_TEST_TXT, ptr);
53 int fd = open(ptr, O_RDWR | O_CREAT, TEST_MODE);
54 EXPECT_TRUE("fchownat_0200", fd >= 0);
55 int ret = fchownat(AT_FDCWD, ptr, 0, 0, AT_SYMLINK_NOFOLLOW);
56 EXPECT_EQ("fchownat_0200", ret, 0);
57 struct stat buf;
58 int st = stat(ptr, &buf);
59 EXPECT_EQ("fchownat_0200", st, 0);
60 EXPECT_EQ("fchownat_0200", buf.st_uid, 0);
61 EXPECT_EQ("fchownat_0200", buf.st_gid, 0);
62 close(fd);
63 remove(ptr);
64 }
65
main(int argc,char * argv[])66 int main(int argc, char *argv[])
67 {
68 fchownat_0100();
69 fchownat_0200();
70 return t_status;
71 }