• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         string isIncremental = "1";
77 
78         string content = "version=1.0&attrNum=6\r\npath;mode;dir;size;mtime;hash;isIncremetal\r\n";
79         content += fileName + ";" + mode + ";" + isDir + ";" + size + ";" + mtime + ";" + hash + ";" + isIncremental;
80         TestManager tm(__func__);
81         const auto [filePath, res] = GetTestFile(tm, content);
82 
83         BReportEntity cloudRp(UniqueFd(open(filePath.data(), O_RDONLY, 0)));
84         unordered_map<string, struct ReportFileInfo> cloudFiles;
85         cloudRp.GetReportInfos(cloudFiles);
86 
87         bool flag = false;
88         fileName = fileName.substr(1, fileName.length() - 1);
89         EXPECT_EQ(cloudFiles.size(), 1);
90         for (auto &item : cloudFiles) {
91             if (item.first == fileName) {
92                 EXPECT_EQ(item.first, fileName);
93                 EXPECT_EQ(item.second.mode, mode);
94                 EXPECT_EQ(to_string(item.second.isDir), isDir);
95                 EXPECT_EQ(to_string(item.second.size), size);
96                 EXPECT_EQ(to_string(item.second.mtime), mtime);
97                 EXPECT_EQ(item.second.hash, hash);
98                 EXPECT_EQ(to_string(item.second.isIncremental), isIncremental);
99 
100                 flag = true;
101                 break;
102             }
103         }
104 
105         EXPECT_TRUE(flag);
106     } catch (const exception &e) {
107         GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by GetReportInfos.";
108         e.what();
109     }
110     GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_GetReportInfos_0100";
111 }
112 
113 /**
114  * @tc.number: SUB_backup_b_report_entity_SplitStringByChar_0100
115  * @tc.name: b_report_entity_SplitStringByChar_0100
116  * @tc.desc: Test function of SplitStringByChar interface for SUCCESS.
117  * @tc.size: MEDIUM
118  * @tc.type: FUNC
119  * @tc.level Level 1
120  */
121 HWTEST_F(BReportEntityTest, b_report_entity_SplitStringByChar_0100, testing::ext::TestSize.Level1)
122 {
123     GTEST_LOG_(INFO) << "BReportEntityTest-begin b_report_entity_SplitStringByChar_0100";
124     try {
125         string str = "";
126         char sep = ATTR_SEP;
127         vector<string> splits;
128         SplitStringByChar(str, sep, splits);
129         EXPECT_EQ(splits.size(), 0);
130 
131         str = "test;";
132         SplitStringByChar(str, sep, splits);
133         EXPECT_EQ(splits.size(), 2);
134 
135         str = "test";
136         splits.clear();
137         SplitStringByChar(str, sep, splits);
138         EXPECT_EQ(splits.size(), 1);
139     } catch (const exception &e) {
140         GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by SplitStringByChar. " << e.what();
141         EXPECT_TRUE(false);
142     }
143     GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_SplitStringByChar_0100";
144 }
145 
146 /**
147  * @tc.number: SUB_backup_b_report_entity_ParseReportInfo_0100
148  * @tc.name: b_report_entity_ParseReportInfo_0100
149  * @tc.desc: Test function of ParseReportInfo interface for SUCCESS.
150  * @tc.size: MEDIUM
151  * @tc.type: FUNC
152  * @tc.level Level 1
153  */
154 HWTEST_F(BReportEntityTest, b_report_entity_ParseReportInfo_0100, testing::ext::TestSize.Level1)
155 {
156     GTEST_LOG_(INFO) << "BReportEntityTest-begin b_report_entity_ParseReportInfo_0100";
157     try {
158         struct ReportFileInfo fileStat;
159         vector<string> splits;
160         std::vector<std::string> keys;
161         int testLen = 1;
162         auto err = ParseReportInfo(fileStat, splits, testLen);
163         EXPECT_EQ(err, EPERM);
164 
165         splits.emplace_back("test");
166         testLen = 1;
167         err = ParseReportInfo(fileStat, splits, testLen);
168         EXPECT_EQ(err, EPERM);
169 
170         fileStat = {};
171         splits = {"/test", "0", "0", "0", "0", "0", "0"};
172         err = ParseReportInfo(fileStat, splits, splits.size());
173         EXPECT_EQ(err, ERR_OK);
174 
175         splits = {"test", "0", "1", "0", "0", "0", "1", "1"};
176         err = ParseReportInfo(fileStat, splits, splits.size());
177         EXPECT_EQ(err, ERR_OK);
178 
179         splits = {"test", "0", "1", "test", "test", "0", "1", "1"};
180         err = ParseReportInfo(fileStat, splits, splits.size());
181         EXPECT_EQ(err, ERR_OK);
182     } catch (const exception &e) {
183         GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by ParseReportInfo." << e.what();
184         EXPECT_TRUE(false);
185     }
186     GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_ParseReportInfo_0100";
187 }
188 
189 /**
190  * @tc.number: SUB_backup_b_report_entity_DealLine_0100
191  * @tc.name: b_report_entity_DealLine_0100
192  * @tc.desc: Test function of DealLine interface for SUCCESS.
193  * @tc.size: MEDIUM
194  * @tc.type: FUNC
195  * @tc.level Level 1
196  */
197 HWTEST_F(BReportEntityTest, b_report_entity_DealLine_0100, testing::ext::TestSize.Level1)
198 {
199     GTEST_LOG_(INFO) << "BReportEntityTest-begin b_report_entity_DealLine_0100";
200     try {
201         std::vector<string> keys;
202         int num = 1;
203         string line = "test\r";
204         unordered_map<string, struct ReportFileInfo> infos;
205         DealLine(keys, num, line, infos);
206         EXPECT_EQ(keys.size(), 1);
207 
208         num = INFO_ALIGN_NUM;
209         keys.clear();
210         line = "\r";
211         DealLine(keys, num, line, infos);
212         EXPECT_EQ(infos.size(), 0);
213 
214         line = "/test;0;0;0;0;0;0;0\r";
215         keys = Data_Header;
216         DealLine(keys, num, line, infos);
217         EXPECT_EQ(infos.size(), 1);
218 
219         line = "/test;0;0;0;0;0;0\r";
220         keys.resize(keys.size() - 1);
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         std::vector<string> keys;
243         int num = 0;
244         string line = "";
245         unordered_map<string, struct ReportFileInfo> infos;
246         DealLine(keys, num, line, infos);
247         EXPECT_EQ(infos.size(), 0);
248         EXPECT_EQ(keys.size(), 0);
249 
250         num = 1;
251         DealLine(keys, num, line, infos);
252         EXPECT_EQ(infos.size(), 0);
253         EXPECT_EQ(keys.size(), 0);
254 
255         num = 2;
256         line = "";
257         DealLine(keys, num, line, infos);
258         EXPECT_EQ(infos.size(), 0);
259         EXPECT_EQ(keys.size(), 0);
260 
261         num = 2;
262         line = "test";
263         DealLine(keys, num, line, infos);
264         EXPECT_EQ(infos.size(), 0);
265         EXPECT_EQ(keys.size(), 0);
266     } catch (const exception &e) {
267         GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by DealLine. " << e.what();
268         EXPECT_TRUE(false);
269     }
270     GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_DealLine_0101";
271 }
272 
273 /**
274  * @tc.number: SUB_backup_b_report_entity_GetStorageReportInfos_0100
275  * @tc.name: b_report_entity_GetStorageReportInfos_0100
276  * @tc.desc: Test function of GetStorageReportInfos interface for SUCCESS.
277  * @tc.size: MEDIUM
278  * @tc.type: FUNC
279  * @tc.level Level 1
280  */
281 HWTEST_F(BReportEntityTest, b_report_entity_GetStorageReportInfos_0100, testing::ext::TestSize.Level1)
282 {
283     GTEST_LOG_(INFO) << "BReportEntityTest-begin b_report_entity_GetStorageReportInfos_0100";
284     try {
285         string fileName = "/a.txt";
286         string mode = "0644";
287         string isDir = "0";
288         string size = "1";
289         string mtime = "1501927260";
290         string hash = "ASDasadSDASDA";
291         string isIncremental = "1";
292 
293         string content = "version=1.0&attrNum=6\r\npath;mode;dir;size;mtime;hash;isIncremental\r\n";
294         content += fileName + ";" + mode + ";" + isDir + ";" + size + ";" + mtime + ";" + hash + ";" + isIncremental;
295         TestManager tm(__func__);
296         const auto [filePath, res] = GetTestFile(tm, content);
297         BReportEntity cloudRp(UniqueFd(open(filePath.data(), O_RDONLY, 0)));
298 
299         unordered_map<string, struct ReportFileInfo> localFilesInfo;
300         bool ret = cloudRp.GetStorageReportInfos(localFilesInfo);
301         EXPECT_TRUE(ret);
302     } catch (const exception &e) {
303         GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by GetStorageReportInfos.";
304         EXPECT_TRUE(false);
305     }
306     GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_GetStorageReportInfos_0100";
307 }
308 
309 /**
310  * @tc.number: SUB_backup_b_report_entity_CheckAndUpdateIfReportLineEncoded_0100
311  * @tc.name: b_report_entity_CheckAndUpdateIfReportLineEncoded_0100
312  * @tc.desc: Test function of CheckAndUpdateIfReportLineEncoded interface for SUCCESS.
313  * @tc.size: MEDIUM
314  * @tc.type: FUNC
315  * @tc.level Level 1
316  */
317 HWTEST_F(BReportEntityTest, b_report_entity_CheckAndUpdateIfReportLineEncoded_0100, testing::ext::TestSize.Level1)
318 {
319     GTEST_LOG_(INFO) << "BReportEntityTest-begin b_report_entity_CheckAndUpdateIfReportLineEncoded_0100";
320     try {
321         string fileName = "/a.txt";
322         string mode = "0644";
323         string isDir = "0";
324         string size = "1";
325         string mtime = "1501927260";
326         string hash = "ASDasadSDASDA";
327 
328         string content = "version=1.0&attrNum=6\r\npath;mode;dir;size;mtime;hash\r\n";
329         content += fileName + ";" + mode + ";" + isDir + ";" + size + ";" + mtime + ";" + hash;
330         TestManager tm(__func__);
331         const auto [filePath, res] = GetTestFile(tm, content);
332         BReportEntity cloudRp(UniqueFd(open(filePath.data(), O_RDONLY, 0)));
333 
334         std::string path;
335         cloudRp.CheckAndUpdateIfReportLineEncoded(path);
336         path = filePath;
337         cloudRp.CheckAndUpdateIfReportLineEncoded(path);
338         EXPECT_TRUE(true);
339     } catch (const exception &e) {
340         GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by CheckAndUpdateIfReportLineEncoded.";
341         EXPECT_TRUE(false);
342     }
343     GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_CheckAndUpdateIfReportLineEncoded_0100";
344 }
345 
346 /**
347  * @tc.number: SUB_backup_b_report_entity_EncodeReportItem_0100
348  * @tc.name: b_report_entity_EncodeReportItem_0100
349  * @tc.desc: Test function of EncodeReportItem interface for SUCCESS.
350  * @tc.size: MEDIUM
351  * @tc.type: FUNC
352  * @tc.level Level 1
353  */
354 HWTEST_F(BReportEntityTest, b_report_entity_EncodeReportItem_0100, testing::ext::TestSize.Level1)
355 {
356     GTEST_LOG_(INFO) << "BReportEntityTest-begin b_report_entity_EncodeReportItem_0100";
357     try {
358         string fileName = "/a.txt";
359         string mode = "0644";
360         string isDir = "0";
361         string size = "1";
362         string mtime = "1501927260";
363         string hash = "ASDasadSDASDA";
364 
365         string content = "version=1.0&attrNum=6\r\npath;mode;dir;size;mtime;hash\r\n";
366         content += fileName + ";" + mode + ";" + isDir + ";" + size + ";" + mtime + ";" + hash;
367         TestManager tm(__func__);
368         const auto [filePath, res] = GetTestFile(tm, content);
369         BReportEntity cloudRp(UniqueFd(open(filePath.data(), O_RDONLY, 0)));
370 
371         const std::string reportItem = "test";
372         bool enableEncode = false;
373         std::string ret = cloudRp.EncodeReportItem(reportItem, enableEncode);
374         EXPECT_EQ(ret, reportItem);
375         ret = cloudRp.DecodeReportItem(reportItem, enableEncode);
376         EXPECT_EQ(ret, reportItem);
377 
378         enableEncode = true;
379         ret = cloudRp.EncodeReportItem(reportItem, enableEncode);
380         EXPECT_EQ(ret, reportItem);
381         ret = cloudRp.DecodeReportItem(reportItem, enableEncode);
382         EXPECT_EQ(ret, reportItem);
383     } catch (const exception &e) {
384         GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by EncodeReportItem. " << e.what();
385         EXPECT_TRUE(false);
386     }
387     GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_EncodeReportItem_0100";
388 }
389 } // namespace OHOS::FileManagement::Backup