1 /*
2 * Copyright (c) 2024 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 <string>
18 #include <tuple>
19 #include <unordered_map>
20 #include <vector>
21
22 #include <gtest/gtest.h>
23 #include <fcntl.h>
24 #include <file_ex.h>
25
26 #include "b_json/b_report_entity.h"
27 #include "test_manager.h"
28
29 #include "src/b_json/b_report_entity.cpp"
30
31 namespace OHOS::FileManagement::Backup {
32 using namespace std;
33
34 class BReportEntityTest : public testing::Test {
35 public:
SetUpTestCase(void)36 static void SetUpTestCase(void) {};
TearDownTestCase()37 static void TearDownTestCase() {};
SetUp()38 void SetUp() {};
TearDown()39 void TearDown() {};
40 };
41
42 /**
43 * @brief 创建测试文件
44 *
45 * @return tuple<bool, string, string> 创建结果、文件路径、文件内容
46 */
GetTestFile(const TestManager & tm,const string content)47 static tuple<string, string> GetTestFile(const TestManager &tm, const string content)
48 {
49 string path = tm.GetRootDirCurTest();
50 string filePath = path + "temp.txt";
51
52 if (bool contentCreate = SaveStringToFile(filePath, content, true); !contentCreate) {
53 throw system_error(errno, system_category());
54 }
55 return {filePath, content};
56 }
57
58 /**
59 * @tc.number: SUB_backup_b_report_entity_GetReportInfos_0100
60 * @tc.name: b_report_entity_GetReportInfos_0100
61 * @tc.desc: Test function of GetReportInfos interface for SUCCESS.
62 * @tc.size: MEDIUM
63 * @tc.type: FUNC
64 * @tc.level Level 1
65 */
66 HWTEST_F(BReportEntityTest, b_report_entity_GetReportInfos_0100, testing::ext::TestSize.Level0)
67 {
68 GTEST_LOG_(INFO) << "BReportEntityTest-begin b_report_entity_GetReportInfos_0100";
69 try {
70 string fileName = "/a.txt";
71 string mode = "0644";
72 string isDir = "0";
73 string size = "1";
74 string mtime = "1501927260";
75 string hash = "ASDasadSDASDA";
76
77 string content = "version=1.0&attrNum=6\r\npath;mode;dir;size;mtime;hash\r\n";
78 content += fileName + ";" + mode + ";" + isDir + ";" + size + ";" + mtime + ";" + hash;
79 TestManager tm(__func__);
80 const auto [filePath, res] = GetTestFile(tm, content);
81
82 BReportEntity cloudRp(UniqueFd(open(filePath.data(), O_RDONLY, 0)));
83 unordered_map<string, struct ReportFileInfo> cloudFiles = cloudRp.GetReportInfos();
84
85 bool flag = false;
86 fileName = fileName.substr(1, fileName.length() - 1);
87 EXPECT_EQ(cloudFiles.size(), 1);
88 for (auto &item : cloudFiles) {
89 if (item.first == fileName) {
90 EXPECT_EQ(item.first, fileName);
91 EXPECT_EQ(item.second.mode, mode);
92 EXPECT_EQ(to_string(item.second.isDir), isDir);
93 EXPECT_EQ(to_string(item.second.size), size);
94 EXPECT_EQ(to_string(item.second.mtime), mtime);
95 EXPECT_EQ(item.second.hash, hash);
96
97 flag = true;
98 break;
99 }
100 }
101
102 EXPECT_TRUE(flag);
103 } catch (const exception &e) {
104 GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by GetReportInfos.";
105 e.what();
106 }
107 GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_GetReportInfos_0100";
108 }
109
110 /**
111 * @tc.number: SUB_backup_b_report_entity_SplitStringByChar_0100
112 * @tc.name: b_report_entity_SplitStringByChar_0100
113 * @tc.desc: Test function of SplitStringByChar interface for SUCCESS.
114 * @tc.size: MEDIUM
115 * @tc.type: FUNC
116 * @tc.level Level 1
117 */
118 HWTEST_F(BReportEntityTest, b_report_entity_SplitStringByChar_0100, testing::ext::TestSize.Level1)
119 {
120 GTEST_LOG_(INFO) << "BReportEntityTest-begin b_report_entity_SplitStringByChar_0100";
121 try {
122 string str = "";
123 char sep = ATTR_SEP;
124 auto splits = SplitStringByChar(str, sep);
125 EXPECT_EQ(splits.size(), 0);
126
127 str = "test;";
128 splits = SplitStringByChar(str, sep);
129 EXPECT_EQ(splits.size(), 2);
130
131 str = "test";
132 splits = SplitStringByChar(str, sep);
133 EXPECT_EQ(splits.size(), 1);
134 } catch (const exception &e) {
135 GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by SplitStringByChar. " << e.what();
136 EXPECT_TRUE(false);
137 }
138 GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_SplitStringByChar_0100";
139 }
140
141 /**
142 * @tc.number: SUB_backup_b_report_entity_ParseReportInfo_0100
143 * @tc.name: b_report_entity_ParseReportInfo_0100
144 * @tc.desc: Test function of ParseReportInfo interface for SUCCESS.
145 * @tc.size: MEDIUM
146 * @tc.type: FUNC
147 * @tc.level Level 1
148 */
149 HWTEST_F(BReportEntityTest, b_report_entity_ParseReportInfo_0100, testing::ext::TestSize.Level1)
150 {
151 GTEST_LOG_(INFO) << "BReportEntityTest-begin b_report_entity_ParseReportInfo_0100";
152 try {
153 struct ReportFileInfo fileStat;
154 vector<string> splits;
155 unordered_map<string, int> keys;
156 auto err = ParseReportInfo(fileStat, splits, keys);
157 EXPECT_EQ(err, EPERM);
158
159 fileStat = {};
160 splits = {"/test", "0", "0", "0", "0", "0", "0"};
161 keys.emplace(INFO_MODE, 0);
162 keys.emplace(INFO_DIR, 1);
163 keys.emplace(INFO_SIZE, 2);
164 keys.emplace(INFO_MTIME, 3);
165 keys.emplace(INFO_HASH, 4);
166 keys.emplace(INFO_IS_INCREMENTAL, 5);
167 err = ParseReportInfo(fileStat, splits, keys);
168 EXPECT_EQ(err, ERR_OK);
169
170 fileStat = {};
171 splits = {"test", "0", "1", "0", "0", "0", "1"};
172 keys.clear();
173 keys.emplace(INFO_MODE, 0);
174 keys.emplace(INFO_DIR, 1);
175 keys.emplace(INFO_SIZE, 2);
176 keys.emplace(INFO_MTIME, 3);
177 keys.emplace(INFO_HASH, 4);
178 keys.emplace(INFO_IS_INCREMENTAL, 5);
179 err = ParseReportInfo(fileStat, splits, keys);
180 EXPECT_EQ(err, ERR_OK);
181 } catch (const exception &e) {
182 GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by ParseReportInfo." << e.what();
183 EXPECT_TRUE(false);
184 }
185 GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_ParseReportInfo_0100";
186 }
187
188 /**
189 * @tc.number: SUB_backup_b_report_entity_DealLine_0100
190 * @tc.name: b_report_entity_DealLine_0100
191 * @tc.desc: Test function of DealLine interface for SUCCESS.
192 * @tc.size: MEDIUM
193 * @tc.type: FUNC
194 * @tc.level Level 1
195 */
196 HWTEST_F(BReportEntityTest, b_report_entity_DealLine_0100, testing::ext::TestSize.Level1)
197 {
198 GTEST_LOG_(INFO) << "BReportEntityTest-begin b_report_entity_DealLine_0100";
199 try {
200 unordered_map<string, int> keys;
201 int num = 1;
202 string line = "test\r";
203 unordered_map<string, struct ReportFileInfo> infos;
204 DealLine(keys, num, line, infos);
205 EXPECT_EQ(keys.size(), 1);
206
207 num = INFO_ALIGN_NUM;
208 keys.clear();
209 line = "\r";
210 DealLine(keys, num, line, infos);
211 EXPECT_EQ(infos.size(), 0);
212
213 line = "/test;0;0;0;0;0;0\r";
214 keys.clear();
215 keys.emplace(INFO_MODE, 0);
216 keys.emplace(INFO_DIR, 1);
217 keys.emplace(INFO_SIZE, 2);
218 keys.emplace(INFO_MTIME, 3);
219 keys.emplace(INFO_HASH, 4);
220 keys.emplace(INFO_IS_INCREMENTAL, 5);
221 DealLine(keys, num, line, infos);
222 EXPECT_EQ(infos.size(), 1);
223 } catch (const exception &e) {
224 GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by DealLine. " << e.what();
225 EXPECT_TRUE(false);
226 }
227 GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_DealLine_0100";
228 }
229
230 /**
231 * @tc.number: SUB_backup_b_report_entity_DealLine_0101
232 * @tc.name: b_report_entity_DealLine_0101
233 * @tc.desc: Test function of DealLine interface for SUCCESS.
234 * @tc.size: MEDIUM
235 * @tc.type: FUNC
236 * @tc.level Level 1
237 */
238 HWTEST_F(BReportEntityTest, b_report_entity_DealLine_0101, testing::ext::TestSize.Level1)
239 {
240 GTEST_LOG_(INFO) << "BReportEntityTest-begin b_report_entity_DealLine_0101";
241 try {
242 unordered_map<string, int> keys;
243 int num = 1;
244 string line = "";
245 unordered_map<string, struct ReportFileInfo> infos;
246 DealLine(keys, num, line, infos);
247 EXPECT_EQ(infos.size(), 0);
248 } catch (const exception &e) {
249 GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by DealLine. " << e.what();
250 EXPECT_TRUE(false);
251 }
252 GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_DealLine_0101";
253 }
254
255 /**
256 * @tc.number: SUB_backup_b_report_entity_StorageDealLine_0100
257 * @tc.name: b_report_entity_StorageDealLine_0100
258 * @tc.desc: Test function of DealLine interface for SUCCESS.
259 * @tc.size: MEDIUM
260 * @tc.type: FUNC
261 * @tc.level Level 1
262 */
263 HWTEST_F(BReportEntityTest, b_report_entity_StorageDealLine_0100, testing::ext::TestSize.Level1)
264 {
265 GTEST_LOG_(INFO) << "BReportEntityTest-begin b_report_entity_StorageDealLine_0100";
266 try {
267 unordered_map<string, int> keys;
268 int num = 1;
269 string line = "";
270 StorageDealLine(keys, num, line);
271 EXPECT_TRUE(true);
272 } catch (const exception &e) {
273 GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by DealLine. " << e.what();
274 EXPECT_TRUE(false);
275 }
276 GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_StorageDealLine_0100";
277 }
278
279 /**
280 * @tc.number: SUB_backup_b_report_entity_StorageDealLine_0101
281 * @tc.name: b_report_entity_StorageDealLine_0101
282 * @tc.desc: Test function of DealLine interface for SUCCESS.
283 * @tc.size: MEDIUM
284 * @tc.type: FUNC
285 * @tc.level Level 1
286 */
287 HWTEST_F(BReportEntityTest, b_report_entity_StorageDealLine_0101, testing::ext::TestSize.Level1)
288 {
289 GTEST_LOG_(INFO) << "BReportEntityTest-begin b_report_entity_StorageDealLine_0101";
290 try {
291 unordered_map<string, int> keys;
292 int num = 0;
293 string line = "test\r";
294 StorageDealLine(keys, num, line);
295 EXPECT_TRUE(true);
296 } catch (const exception &e) {
297 GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by DealLine. " << e.what();
298 EXPECT_TRUE(false);
299 }
300 GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_StorageDealLine_0101";
301 }
302
303 /**
304 * @tc.number: SUB_backup_b_report_entity_StorageDealLine_0102
305 * @tc.name: b_report_entity_StorageDealLine_0102
306 * @tc.desc: Test function of DealLine interface for SUCCESS.
307 * @tc.size: MEDIUM
308 * @tc.type: FUNC
309 * @tc.level Level 1
310 */
311 HWTEST_F(BReportEntityTest, b_report_entity_StorageDealLine_0102, testing::ext::TestSize.Level1)
312 {
313 GTEST_LOG_(INFO) << "BReportEntityTest-begin b_report_entity_StorageDealLine_0102";
314 try {
315 unordered_map<string, int> keys;
316 int num = 1;
317 string line = "key1;key2;key3";
318 StorageDealLine(keys, num, line);
319 EXPECT_TRUE(true);
320 } catch (const exception &e) {
321 GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by DealLine. " << e.what();
322 EXPECT_TRUE(false);
323 }
324 GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_StorageDealLine_0102";
325 }
326
327 /**
328 * @tc.number: SUB_backup_b_report_entity_StorageDealLine_0103
329 * @tc.name: b_report_entity_StorageDealLine_0103
330 * @tc.desc: Test function of DealLine interface for SUCCESS.
331 * @tc.size: MEDIUM
332 * @tc.type: FUNC
333 * @tc.level Level 1
334 */
335 HWTEST_F(BReportEntityTest, b_report_entity_StorageDealLine_0103, testing::ext::TestSize.Level1)
336 {
337 GTEST_LOG_(INFO) << "BReportEntityTest-begin b_report_entity_StorageDealLine_0103";
338 try {
339 unordered_map<string, int> keys;
340 int num = INFO_ALIGN_NUM;
341 string line = "key1;key2;key3";
342 StorageDealLine(keys, num, line);
343 EXPECT_TRUE(true);
344 } catch (const exception &e) {
345 GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by DealLine. " << e.what();
346 EXPECT_TRUE(false);
347 }
348 GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_StorageDealLine_0103";
349 }
350 } // namespace OHOS::FileManagement::Backup