1 /*
2  * Copyright (C) 2024 HiHope Open Source Organization.
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 <cerrno>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <string>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <vector>
23 #include <arpa/inet.h>
24 #include <gtest/gtest.h>
25 #include <netinet/in.h>
26 #include <sys/stat.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
29 #include "securec.h"
30 
31 using namespace testing::ext;
32 
33 static const char *TEST_VALID_PATH = "/data/local/tmp/test_dir";
34 static const char *TEST_VALID_FILE = "/data/local/tmp/test_dir/test.txt";
35 static const char *TEST_RENAME_PATH = "/data/local/tmp/renameat_test_dir";
36 static const char *TEST_RENAME_FILE = "/data/local/tmp/test_dir/renameat_test.txt";
37 static const char *TEST_RENAME_FILE_TMP = "/data/local/tmp/renameat_test_dir/renameat_test.txt";
38 static const char *TEST_INVALID_FILE = "/data/local/abcd/abcd.txt";
39 
40 class HatsRenameatTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp();
45 void TearDown();
46 private:
47 };
SetUp()48 void HatsRenameatTest::SetUp()
49 {
50     int fd = -1;
51     if (access(TEST_VALID_PATH, F_OK) == 0) {
52         (void)remove(TEST_VALID_PATH);
53     }
54     (void)mkdir(TEST_VALID_PATH, S_IWUSR | S_IRUSR | S_IXUSR);
55     fd = open(TEST_VALID_FILE, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
56     close(fd);
57 }
TearDown()58 void HatsRenameatTest::TearDown()
59 {
60     (void)remove(TEST_VALID_FILE);
61     (void)remove(TEST_RENAME_FILE);
62     (void)remove(TEST_RENAME_FILE_TMP);
63     (void)remove(TEST_VALID_PATH);
64     (void)remove(TEST_RENAME_PATH);
65 }
SetUpTestCase()66 void HatsRenameatTest::SetUpTestCase()
67 {
68 }
TearDownTestCase()69 void HatsRenameatTest::TearDownTestCase()
70 {
71 }
72 
73 /*
74  * @tc.number : SUB_KERNEL_SYSCALL_RENAMEAT_0100
75  * @tc.name   : RenameatFileAndPathSuccess_0001
76  * @tc.desc   : renameat an existed file or path success.
77  * @tc.size   : MediumTest
78  * @tc.type   : Function
79  * @tc.level  : Level 1
80  */
HWTEST_F(HatsRenameatTest, RenameatFileAndPathSuccess_0001, Function | MediumTest | Level1)81 HWTEST_F(HatsRenameatTest, RenameatFileAndPathSuccess_0001, Function | MediumTest | Level1)
82 {
83     // renameat exist file success
84     int ret = renameat(AT_FDCWD, TEST_VALID_FILE, AT_FDCWD, TEST_RENAME_FILE);
85     EXPECT_EQ(ret, 0);
86     EXPECT_EQ(access(TEST_VALID_FILE, F_OK), -1);
87     EXPECT_EQ(access(TEST_RENAME_FILE, F_OK), 0);
88 
89     // renameat exist path success
90     ret = renameat(AT_FDCWD, TEST_VALID_PATH, AT_FDCWD, TEST_RENAME_PATH);
91     EXPECT_EQ(ret, 0);
92     EXPECT_EQ(access(TEST_VALID_PATH, F_OK), -1);
93     EXPECT_EQ(access(TEST_RENAME_PATH, F_OK), 0);
94 }
95 
96 /*
97  * @tc.number : SUB_KERNEL_SYSCALL_RENAMEAT_0200
98  * @tc.name   : RenameatNotExistFileFailed_0002
99  * @tc.desc   : renameat a file that does not exist failed, errno ENOENT.
100  * @tc.size   : MediumTest
101  * @tc.type   : Function
102  * @tc.level  : Level 2
103  */
HWTEST_F(HatsRenameatTest, RenameatNotExistFileFailed_0002, Function | MediumTest | Level2)104 HWTEST_F(HatsRenameatTest, RenameatNotExistFileFailed_0002, Function | MediumTest | Level2)
105 {
106     if (access(TEST_INVALID_FILE, F_OK) == 0) {
107         (void)remove(TEST_INVALID_FILE);
108     }
109 
110     errno = 0;
111     int ret = renameat(AT_FDCWD, TEST_INVALID_FILE, AT_FDCWD, TEST_RENAME_FILE);
112     EXPECT_EQ(ret, -1);
113     EXPECT_EQ(errno, ENOENT);
114 }
115 
116 /*
117  * @tc.number : SUB_KERNEL_SYSCALL_RENAMEAT_0300
118  * @tc.name   : Renameat2FileAndPathSuccess_0003
119  * @tc.desc   : renameat2 an existed file or path success.
120  * @tc.size   : MediumTest
121  * @tc.type   : Function
122  * @tc.level  : Level 1
123  */
HWTEST_F(HatsRenameatTest, Renameat2FileAndPathSuccess_0003, Function | MediumTest | Level1)124 HWTEST_F(HatsRenameatTest, Renameat2FileAndPathSuccess_0003, Function | MediumTest | Level1)
125 {
126     // renameat exist file success
127     int ret = renameat2(AT_FDCWD, TEST_VALID_FILE, AT_FDCWD, TEST_RENAME_FILE, 0);
128     EXPECT_EQ(ret, 0);
129     EXPECT_EQ(access(TEST_VALID_FILE, F_OK), -1);
130     EXPECT_EQ(access(TEST_RENAME_FILE, F_OK), 0);
131 
132     // renameat exist path success
133     ret = renameat2(AT_FDCWD, TEST_VALID_PATH, AT_FDCWD, TEST_RENAME_PATH, 0);
134     EXPECT_EQ(ret, 0);
135     EXPECT_EQ(access(TEST_VALID_PATH, F_OK), -1);
136     EXPECT_EQ(access(TEST_RENAME_PATH, F_OK), 0);
137 }