1 /*
2 * Copyright (c) 2022-2023 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 <cstdio>
17 #include <tuple>
18
19 #include <fcntl.h>
20
21 #include <file_ex.h>
22 #include <gtest/gtest.h>
23
24 #include "b_filesystem/b_file.h"
25 #include "test_manager.h"
26
27 namespace OHOS::FileManagement::Backup {
28 using namespace std;
29
30 class BFileTest : public testing::Test {
31 public:
SetUpTestCase(void)32 static void SetUpTestCase(void) {};
TearDownTestCase()33 static void TearDownTestCase() {};
SetUp()34 void SetUp() {};
TearDown()35 void TearDown() {};
36 };
37
38 /**
39 * @brief 创建测试文件
40 *
41 * @return tuple<bool, string, string> 创建结果、文件路径、文件内容
42 */
GetTestFile(const TestManager & tm)43 static tuple<string, string> GetTestFile(const TestManager &tm)
44 {
45 string path = tm.GetRootDirCurTest();
46 string filePath = path + "temp.txt";
47 string content = "backup test";
48 if (bool contentCreate = SaveStringToFile(filePath, content, true); !contentCreate) {
49 throw system_error(errno, system_category());
50 }
51 return {filePath, content};
52 }
53
54 /**
55 * @tc.number: SUB_backup_b_file_ReadFile_0100
56 * @tc.name: b_file_ReadFile_0100
57 * @tc.desc: Test function of ReadFile interface for SUCCESS.
58 * @tc.size: MEDIUM
59 * @tc.type: FUNC
60 * @tc.level Level 1
61 * @tc.require: I6F3GV
62 */
63 HWTEST_F(BFileTest, b_file_ReadFile_0100, testing::ext::TestSize.Level1)
64 {
65 GTEST_LOG_(INFO) << "BFileTest-begin b_file_ReadFile_0100";
66 try {
67 TestManager tm(__func__);
68 const auto [filePath, content] = GetTestFile(tm);
69 BFile bf;
70 unique_ptr<char[]> result = bf.ReadFile(UniqueFd(open(filePath.data(), O_RDWR)));
71 string readContent(result.get());
72 EXPECT_EQ(readContent.compare(content), 0);
73 } catch (const exception &e) {
74 GTEST_LOG_(INFO) << "BFileTest-an exception occurred by ReadFile.";
75 e.what();
76 }
77 GTEST_LOG_(INFO) << "BFileTest-end b_file_ReadFile_0100";
78 }
79
80 /**
81 * @tc.number: SUB_backup_b_file_SendFile_0100
82 * @tc.name: b_file_SendFile_0100
83 * @tc.desc: 测试SendFile接口
84 * @tc.size: MEDIUM
85 * @tc.type: FUNC
86 * @tc.level Level 1
87 * @tc.require: I6F3GV
88 */
89 HWTEST_F(BFileTest, b_file_SendFile_0100, testing::ext::TestSize.Level1)
90 {
91 GTEST_LOG_(INFO) << "BFileTest-begin b_file_SendFile_0100";
92 try {
93 TestManager tm(__func__);
94 const auto [filePath, content] = GetTestFile(tm);
95 TestManager tmInFile("b_file_GetFd_0100");
96 string fileOutPath = tmInFile.GetRootDirCurTest().append("1.tar");
97 BFile::SendFile(UniqueFd(open(fileOutPath.data(), O_RDWR)),
98 UniqueFd(open(filePath.data(), O_RDWR | O_CREAT, S_IRWXU)));
99 string contentTmp = BFile::ReadFile(UniqueFd(open(fileOutPath.c_str(), O_RDONLY))).get();
100 EXPECT_EQ(contentTmp, content);
101 } catch (const exception &e) {
102 GTEST_LOG_(INFO) << "BFileTest-an exception occurred by SendFile.";
103 e.what();
104 }
105 GTEST_LOG_(INFO) << "BFileTest-end b_file_SendFile_0100";
106 }
107
108 /**
109 * @tc.number: SUB_backup_b_file_CopyFile_0100
110 * @tc.name: b_file_CopyFile_0100
111 * @tc.desc: 测试CopyFile接口
112 * @tc.size: MEDIUM
113 * @tc.type: FUNC
114 * @tc.level Level 1
115 * @tc.require: I6F3GV
116 */
117 HWTEST_F(BFileTest, b_file_CopyFile_0100, testing::ext::TestSize.Level1)
118 {
119 GTEST_LOG_(INFO) << "BFileTest-begin b_file_CopyFile_0100";
120 try {
121 TestManager tm(__func__);
122 const auto [filePath, content] = GetTestFile(tm);
123 TestManager tmInFile("b_file_GetFd_0200");
124 string fileInPath = tmInFile.GetRootDirCurTest().append("1.txt");
125 auto ret = BFile::CopyFile(filePath, fileInPath);
126 EXPECT_TRUE(ret);
127 GTEST_LOG_(INFO) << "BFileTest-CopyFile Branches";
128 ret = BFile::CopyFile(filePath, filePath);
129 EXPECT_TRUE(ret);
130 } catch (const exception &e) {
131 GTEST_LOG_(INFO) << "BFileTest-an exception occurred by CopyFile.";
132 e.what();
133 }
134 GTEST_LOG_(INFO) << "BFileTest-end b_file_CopyFile_0100";
135 }
136
137 /**
138 * @tc.number: SUB_backup_b_file_MoveFile_0100
139 * @tc.name: b_file_MoveFile_0100
140 * @tc.desc: 测试MoveFile接口
141 * @tc.size: MEDIUM
142 * @tc.type: FUNC
143 * @tc.level Level 1
144 * @tc.require: I6F3GV
145 */
146 HWTEST_F(BFileTest, b_file_MoveFile_0100, testing::ext::TestSize.Level1)
147 {
148 GTEST_LOG_(INFO) << "BFileTest-begin b_file_MoveFile_0100";
149 try {
150 TestManager tm(__func__);
151 const auto [filePath, content] = GetTestFile(tm);
152 TestManager tmInFile("b_file_GetFd_0200");
153 string fileInPath = tmInFile.GetRootDirCurTest().append("1.txt");
154 auto ret = BFile::MoveFile(filePath, fileInPath);
155 EXPECT_TRUE(ret);
156 GTEST_LOG_(INFO) << "BFileTest-MoveFile Branches";
157 ret = BFile::MoveFile(filePath, filePath);
158 EXPECT_TRUE(ret);
159 } catch (const exception &e) {
160 GTEST_LOG_(INFO) << "BFileTest-an exception occurred by MoveFile.";
161 e.what();
162 }
163 GTEST_LOG_(INFO) << "BFileTest-end b_file_MoveFile_0100";
164 }
165
166 /**
167 * @tc.number: SUB_backup_b_file_Write_0100
168 * @tc.name: b_file_Write_0100
169 * @tc.desc: 测试Write接口
170 * @tc.size: MEDIUM
171 * @tc.type: FUNC
172 * @tc.level Level 1
173 * @tc.require: I6F3GV
174 */
175 HWTEST_F(BFileTest, b_file_Write_0100, testing::ext::TestSize.Level1)
176 {
177 GTEST_LOG_(INFO) << "BFileTest-begin b_file_Write_0100";
178 try {
179 TestManager tm(__func__);
180 const auto [filePath, content] = GetTestFile(tm);
181 TestManager tmInFile("b_file_GetFd_0200");
182 string fileInPath = tmInFile.GetRootDirCurTest().append("1.txt");
183 BFile::Write(UniqueFd(open(filePath.data(), O_RDWR)), fileInPath);
184 string contentTmp = BFile::ReadFile(UniqueFd(open(fileInPath.c_str(), O_RDONLY))).get();
185 EXPECT_EQ(contentTmp, fileInPath);
186 } catch (const exception &e) {
187 GTEST_LOG_(INFO) << "BFileTest-an exception occurred by Write.";
188 e.what();
189 }
190 GTEST_LOG_(INFO) << "BFileTest-end b_file_Write_0100";
191 }
192 } // namespace OHOS::FileManagement::Backup