1 /*
2 * Copyright (c) 2020-2021 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 #include "FileSystemTest.h"
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <dirent.h>
25 #include <ftw.h>
26 #include <libgen.h>
27
28 #include <gtest/gtest.h>
29
30 #include "utils.h"
31 #include "log.h"
32 #include "KernelConstants.h"
33 #include "libfs.h"
34
35 using namespace testing::ext;
36 /**
37 * @tc.number SUB_KERNEL_FS_STDIO_0700
38 * @tc.name basic function test : Use the rename function to rename files.
39 * @tc.desc [C- SOFTWARE -0200]
40 */
HWTEST_F(FileSystemTest, testRename, Function | MediumTest | Level3)41 HWTEST_F(FileSystemTest, testRename, Function | MediumTest | Level3)
42 {
43 int fd = 0;
44 const char *newFileName = "FILE_NEW";
45 fd = creat(FILE0, 0777);
46 EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
47 EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
48
49 EXPECT_NE(rename(FILE0, newFileName), -1) << "> rename errno = " << errno;
50 EXPECT_NE(unlink(newFileName), -1) << "> unlink errno = " << errno;
51 }
52
53 /**
54 * @tc.number SUB_KERNEL_FS_STDIO_0710
55 * @tc.name basic function test : Use the rename function to rename directories.
56 * @tc.desc [C- SOFTWARE -0200]
57 */
HWTEST_F(FileSystemTest, testRenameDir, Function | MediumTest | Level3)58 HWTEST_F(FileSystemTest, testRenameDir, Function | MediumTest | Level3)
59 {
60 const char *newDirName = "DIR_NEW";
61 EXPECT_NE(mkdir(DIR0, 0777), -1) << "> mkdir errno = " << errno;
62
63 EXPECT_NE(rename(DIR0, newDirName), -1) << "> rename errno = " << errno;
64 EXPECT_NE(rmdir(newDirName), -1) << "> rmdir errno = " << errno;
65 }
66