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 "functionalext.h"
18
19 /**
20 * @tc.name : rename_0100
21 * @tc.desc : Each parameter is valid, and the file name can be changed.
22 * @tc.level : Level 0
23 */
rename_0100(void)24 void rename_0100(void)
25 {
26 const char *oldname = "oldfile.txt";
27 const char *newname = "newfile.txt";
28 int fd = open(oldname, O_RDONLY | O_CREAT, TEST_MODE);
29 EXPECT_TRUE("rename_0100", fd != -1);
30 close(fd);
31
32 int ret = rename(oldname, newname);
33 EXPECT_EQ("rename_0100", ret, 0);
34 fd = open(oldname, O_RDONLY);
35 EXPECT_EQ("rename_0100", fd, -1);
36 fd = open(newname, O_RDONLY);
37 EXPECT_NE("rename_0100", fd, -1);
38 close(fd);
39 remove(newname);
40 }
41
42 /**
43 * @tc.name : rename_0200
44 * @tc.desc : Parameter 1 is invalid, the file name cannot be changed.
45 * @tc.level : Level 2
46 */
rename_0200(void)47 void rename_0200(void)
48 {
49 const char *oldname = "oldfile.txt";
50 const char *newname = "newfile.txt";
51 int ret = rename(oldname, newname);
52 EXPECT_EQ("rename_0200", ret, -1);
53 }
54
55 /**
56 * @tc.name : rename_0300
57 * @tc.desc : Newpath already exists, the file name can be changed.
58 * @tc.level : Level 1
59 */
rename_0300(void)60 void rename_0300(void)
61 {
62 const char *oldname = "oldfile.txt";
63 const char *newname = "newfile.txt";
64 int fd_old = open(oldname, O_RDWR | O_CREAT, TEST_MODE);
65 EXPECT_TRUE("rename_0300", fd_old != -1);
66
67 char str_old[] = "old";
68 int ret_old = write(fd_old, str_old, sizeof(str_old));
69 EXPECT_TRUE("rename_0300", ret_old > 0);
70 close(fd_old);
71
72 int fd_new = open(newname, O_RDWR | O_CREAT, TEST_MODE);
73 EXPECT_TRUE("rename_0100", fd_new != -1);
74
75 char str_new[] = "new";
76 int ret_new = write(fd_old, str_new, sizeof(str_new));
77 EXPECT_TRUE("rename_0300", ret_new > 0);
78 close(fd_new);
79
80 int result = rename(oldname, newname);
81 EXPECT_EQ("rename_0300", result, 0);
82
83 int fd = open(newname, O_RDONLY);
84 EXPECT_TRUE("rename_0300", fd != -1);
85
86 char buf[10];
87 memset(buf, 0, sizeof(buf));
88 int nread = read(fd, buf, sizeof(buf));
89 EXPECT_TRUE("rename_0300", nread > 0);
90 EXPECT_TRUE("rename_0300", strcmp(buf, "old") == 0);
91
92 remove(newname);
93 }
94
main(int argc,char * argv[])95 int main(int argc, char *argv[])
96 {
97 rename_0100();
98 rename_0200();
99 rename_0300();
100 return t_status;
101 }