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 <fstream>
17 #include <gtest/gtest.h>
18 #include <iostream>
19 #define private public
20 #define protected public
21 #include "extract_resource_manager.h"
22 #include "zip_file.h"
23 #include "extractor.h"
24 #undef private
25 #undef protected
26 
27 using namespace testing;
28 using namespace testing::ext;
29 
30 namespace OHOS {
31 namespace AbilityBase {
32 namespace {
33 const std::string TEST_HAP_PATH("/system/app/com.ohos.settings/Settings.hap");
34 const std::string TEST_HAP_SEETINGS_PATH("/system/app/Settings/Settings.hap");
35 const std::string ERROR_HAP_PATH("/system/app/com.ohos.settings/XXX.hap");
36 const std::string TEST_THIRD_HAP_PATH("/data/app/el1/bundle/public/com.ohos.settings/Settings.hap");
37 const std::string MODULE_JSON_PATH("module.json");
38 const std::string CONFIG_JSON_PATH("config.json");
39 const std::string OUT_PATH("/data/module.json");
40 const std::string MAIN_ABILITY_PATH("ets/MainAbility");
41 const std::string FA_MAIN_ABILITY_PATH("assets/js/default");
42 const std::string ERROR_PATH("ets/MainAbilityXXX");
43 const std::string MAIN_ABILITY_FILENAME("ets/MainAbility/MainAbility.abc");
44 const std::string ERROR_FILENAME("ets/MainAbility/XXX.abc");
45 } // namespace
46 class ExtractorTest : public testing::Test {
47 public:
48     static void SetUpTestCase();
49     static void TearDownTestCase();
50     void SetUp();
51     void TearDown();
52 
53     std::string testPath_;
54     static bool addToMap_;
55 };
56 
57 bool ExtractorTest::addToMap_ = false;
58 
SetUpTestCase()59 void ExtractorTest::SetUpTestCase()
60 {}
61 
TearDownTestCase()62 void ExtractorTest::TearDownTestCase()
63 {}
64 
SetUp()65 void ExtractorTest::SetUp()
66 {
67     std::ifstream ifs(TEST_HAP_PATH.c_str());
68     if (ifs.good()) {
69         testPath_ = TEST_HAP_PATH;
70     } else {
71         testPath_ = TEST_HAP_SEETINGS_PATH;
72     }
73 }
74 
TearDown()75 void ExtractorTest::TearDown()
76 {}
77 
78 /*
79  * Feature: Extractor
80  * Function: Init
81  * SubFunction: NA
82  * FunctionPoints:Init extractor
83  * EnvConditions: NA
84  * CaseDescription: Create extractor, call Init function.
85  */
86 HWTEST_F(ExtractorTest, ExtractorInit_001, TestSize.Level1)
87 {
88     std::string loadPath;
89     std::shared_ptr<Extractor> extractor1 = std::make_shared<Extractor>(loadPath);
90     EXPECT_FALSE(extractor1->Init());
91 
92     std::shared_ptr<Extractor> extractor2 = std::make_shared<Extractor>(testPath_);
93     EXPECT_TRUE(extractor2->Init());
94 }
95 
96 /*
97  * Feature: Extractor
98  * Function: GetExtractor
99  * SubFunction: NA
100  * FunctionPoints:Create extractor
101  * EnvConditions: NA
102  * CaseDescription: Create extractor.
103  */
104 HWTEST_F(ExtractorTest, ExtractorCreate_001, TestSize.Level1)
105 {
106     bool newCreate = false;
107     std::string loadPath;
108     std::shared_ptr<Extractor> extractor1 = ExtractorUtil::GetExtractor(loadPath, newCreate);
109     EXPECT_TRUE(extractor1 == nullptr);
110     EXPECT_FALSE(newCreate);
111 
112     loadPath = ERROR_HAP_PATH;
113     std::shared_ptr<Extractor> extractor2 = ExtractorUtil::GetExtractor(loadPath, newCreate);
114     EXPECT_TRUE(extractor2 == nullptr);
115     EXPECT_FALSE(newCreate);
116 
117     std::shared_ptr<Extractor> extractor3 = ExtractorUtil::GetExtractor(testPath_, newCreate);
118     EXPECT_TRUE(extractor3 != nullptr);
119 
120     loadPath = TEST_THIRD_HAP_PATH;
121     std::shared_ptr<Extractor> extractor4 = ExtractorUtil::GetExtractor(loadPath, newCreate);
122     EXPECT_TRUE(extractor4 == nullptr);
123     EXPECT_FALSE(newCreate);
124 }
125 
126 /*
127  * Feature: Extractor
128  * Function: GetLoadFilePath
129  * SubFunction: NA
130  * FunctionPoints:Get load file path
131  * EnvConditions: NA
132  * CaseDescription: Get load file path.
133  */
134 HWTEST_F(ExtractorTest, GetLoadFilePath_001, TestSize.Level1)
135 {
136     std::string loadPath;
137     std::string loadFilePath = ExtractorUtil::GetLoadFilePath(loadPath);
138     EXPECT_TRUE(loadPath == loadFilePath);
139 
140     loadPath = ERROR_HAP_PATH;
141     loadFilePath = ExtractorUtil::GetLoadFilePath(loadPath);
142     EXPECT_TRUE(loadPath == loadFilePath);
143 
144     loadPath = testPath_;
145     loadFilePath = ExtractorUtil::GetLoadFilePath(loadPath);
146     EXPECT_TRUE(loadPath == loadFilePath);
147 
148     loadPath = TEST_THIRD_HAP_PATH;
149     loadFilePath = ExtractorUtil::GetLoadFilePath(loadPath);
150     ExtractorUtil::DeleteExtractor(loadPath);
151     EXPECT_TRUE(loadPath != loadFilePath);
152 }
153 
154 /*
155  * Feature: Extractor
156  * Function: GetFileBuffer
157  * SubFunction: NA
158  * FunctionPoints:Get file buffer
159  * EnvConditions: NA
160  * CaseDescription: Create extractor, call get file buffer function.
161  */
162 HWTEST_F(ExtractorTest, GetFileBuffer_001, TestSize.Level1)
163 {
164     std::shared_ptr<Extractor> extractor = std::make_shared<Extractor>(testPath_);
165     std::ostringstream outStream;
166     std::string srcPath = MODULE_JSON_PATH;
167     EXPECT_FALSE(extractor->GetFileBuffer(srcPath, outStream));
168 
169     extractor->Init();
170     EXPECT_FALSE(extractor->GetFileBuffer("", outStream));
171     EXPECT_FALSE(extractor->GetFileBuffer(CONFIG_JSON_PATH, outStream));
172     EXPECT_TRUE(extractor->GetFileBuffer(srcPath, outStream));
173     EXPECT_TRUE(sizeof(outStream) > 0);
174 }
175 
176 /*
177  * Feature: Extractor
178  * Function: GetFileList
179  * SubFunction: NA
180  * FunctionPoints:Get file list
181  * EnvConditions: NA
182  * CaseDescription: Create extractor, call get file list function.
183  */
184 HWTEST_F(ExtractorTest, GetFileList_001, TestSize.Level1)
185 {
186     std::shared_ptr<Extractor> extractor = std::make_shared<Extractor>(testPath_);
187     std::vector<std::string> fileList;
188     std::string srcPath = MAIN_ABILITY_PATH;
189     EXPECT_FALSE(extractor->GetFileList(srcPath, fileList));
190 
191     extractor->Init();
192     EXPECT_FALSE(extractor->GetFileList("", fileList));
193     EXPECT_TRUE(extractor->GetFileList(FA_MAIN_ABILITY_PATH, fileList));
194     EXPECT_TRUE(fileList.size() == 0);
195 }
196 
197 /*
198  * Feature: Extractor
199  * Function: GetFileList
200  * SubFunction: NA
201  * FunctionPoints:Get file list
202  * EnvConditions: NA
203  * CaseDescription: Create extractor, call get file list function.
204  */
205 HWTEST_F(ExtractorTest, GetFileList_002, TestSize.Level1)
206 {
207     std::shared_ptr<Extractor> extractor = std::make_shared<Extractor>(testPath_);
208 
209     extractor->initial_ = true;
210     extractor->zipFile_.entriesMap_.emplace("a/b/c.txt", ZipEntry());
211     extractor->zipFile_.entriesMap_.emplace("a/c.txt", ZipEntry());
212     extractor->zipFile_.entriesMap_.emplace("a/b.txt", ZipEntry());
213     extractor->zipFile_.entriesMap_.emplace("a.txt", ZipEntry());
214     extractor->zipFile_.entriesMap_.emplace("b.txt", ZipEntry());
215     extractor->zipFile_.entriesMap_.emplace("b/c.txt", ZipEntry());
216     extractor->zipFile_.entriesMap_.emplace("b/c/", ZipEntry());
217     extractor->zipFile_.entriesMap_.emplace("c", ZipEntry());
218     extractor->zipFile_.isOpen_ = true;
219 
220     std::vector<std::string> fileList;
221     extractor->GetFileList("b", fileList);
222     EXPECT_TRUE(fileList.size() == 1);
223     EXPECT_TRUE(fileList[0] == "b/c.txt");
224 
225     fileList.clear();
226     extractor->GetFileList("a/", fileList);
227     EXPECT_TRUE(fileList.size() == 3);
228     std::set<std::string> firstSet(fileList.begin(), fileList.end());
229     EXPECT_TRUE(firstSet.size() == 3);
230 
231     fileList.clear();
232     extractor->GetFileList("/", fileList);
233     EXPECT_TRUE(fileList.size() == 7);
234 
235     extractor->SetCacheMode(CacheMode::CACHE_ALL);
236 
237     fileList.clear();
238     extractor->GetFileList("b", fileList);
239     EXPECT_TRUE(fileList.size() == 1);
240     EXPECT_TRUE(fileList[0] == "b/c.txt");
241 
242     fileList.clear();
243     extractor->GetFileList("a/", fileList);
244     EXPECT_TRUE(fileList.size() == 3);
245     for (const auto &name : fileList) {
246         EXPECT_TRUE(firstSet.count(name) > 0);
247     }
248 
249     fileList.clear();
250     extractor->GetFileList("/", fileList);
251     EXPECT_TRUE(fileList.size() == 7);
252 }
253 
254 /*
255  * Feature: Extractor
256  * Function: GetFileList
257  * SubFunction: NA
258  * FunctionPoints:Get file list
259  * EnvConditions: NA
260  * CaseDescription: Create extractor, call get file list function.
261  */
262 HWTEST_F(ExtractorTest, GetFileList_003, TestSize.Level1)
263 {
264     std::shared_ptr<Extractor> extractor = std::make_shared<Extractor>(testPath_);
265 
266     extractor->initial_ = true;
267     extractor->zipFile_.entriesMap_.emplace("a/b/c.txt", ZipEntry());
268     extractor->zipFile_.entriesMap_.emplace("a/c.txt", ZipEntry());
269     extractor->zipFile_.entriesMap_.emplace("a/b.txt", ZipEntry());
270     extractor->zipFile_.entriesMap_.emplace("a.txt", ZipEntry());
271     extractor->zipFile_.entriesMap_.emplace("b.txt", ZipEntry());
272     extractor->zipFile_.entriesMap_.emplace("b/c.txt", ZipEntry());
273     extractor->zipFile_.entriesMap_.emplace("b/c/", ZipEntry());
274     extractor->zipFile_.entriesMap_.emplace("c", ZipEntry());
275     extractor->zipFile_.isOpen_ = true;
276 
277     std::set<std::string> fileList;
278     extractor->GetFileList("b", fileList);
279     EXPECT_TRUE(fileList.size() == 2);
280     EXPECT_TRUE(fileList.count("c.txt") > 0);
281     EXPECT_TRUE(fileList.count("c") > 0);
282 
283     fileList.clear();
284     extractor->GetFileList("/", fileList);
285     EXPECT_TRUE(fileList.size() == 5);
286     EXPECT_TRUE(fileList.count("a") > 0);
287     EXPECT_TRUE(fileList.count("a.txt") > 0);
288     EXPECT_TRUE(fileList.count("b.txt") > 0);
289     EXPECT_TRUE(fileList.count("b") > 0);
290     EXPECT_TRUE(fileList.count("c") > 0);
291 
292     fileList.clear();
293     extractor->GetFileList("a/", fileList);
294     EXPECT_TRUE(fileList.size() == 3);
295     EXPECT_TRUE(fileList.count("c.txt") > 0);
296     EXPECT_TRUE(fileList.count("b") > 0);
297 
298     extractor->SetCacheMode(CacheMode::CACHE_ALL);
299 
300     fileList.clear();
301     extractor->GetFileList("b", fileList);
302     EXPECT_TRUE(fileList.size() == 2);
303     EXPECT_TRUE(fileList.count("c.txt") > 0);
304     EXPECT_TRUE(fileList.count("c") > 0);
305 
306     fileList.clear();
307     extractor->GetFileList("/", fileList);
308     EXPECT_TRUE(fileList.size() == 5);
309     EXPECT_TRUE(fileList.count("a") > 0);
310     EXPECT_TRUE(fileList.count("a.txt") > 0);
311     EXPECT_TRUE(fileList.count("b.txt") > 0);
312     EXPECT_TRUE(fileList.count("b") > 0);
313     EXPECT_TRUE(fileList.count("c") > 0);
314 
315     fileList.clear();
316     extractor->GetFileList("a/", fileList);
317     EXPECT_TRUE(fileList.size() == 3);
318     EXPECT_TRUE(fileList.count("c.txt") > 0);
319     EXPECT_TRUE(fileList.count("b") > 0);
320 }
321 
322 /*
323  * Feature: Extractor
324  * Function: HasEntry
325  * SubFunction: NA
326  * FunctionPoints:Has entry
327  * EnvConditions: NA
328  * CaseDescription: Create extractor, call has entry function.
329  */
330 HWTEST_F(ExtractorTest, HasEntry_001, TestSize.Level1)
331 {
332     std::shared_ptr<Extractor> extractor = std::make_shared<Extractor>(testPath_);
333     std::string fileName = MAIN_ABILITY_FILENAME;
334     EXPECT_FALSE(extractor->HasEntry(fileName));
335 
336     extractor->Init();
337     EXPECT_FALSE(extractor->HasEntry(""));
338     EXPECT_FALSE(extractor->HasEntry(ERROR_FILENAME));
339 }
340 
341 /*
342  * Feature: Extractor
343  * Function: IsDirExist
344  * SubFunction: NA
345  * FunctionPoints:Is dir exist
346  * EnvConditions: NA
347  * CaseDescription: Create extractor, call is dir exist function.
348  */
349 HWTEST_F(ExtractorTest, IsDirExist_001, TestSize.Level1)
350 {
351     std::shared_ptr<Extractor> extractor = std::make_shared<Extractor>(testPath_);
352     std::string srcPath = MAIN_ABILITY_PATH;
353     EXPECT_FALSE(extractor->IsDirExist(srcPath));
354 
355     extractor->Init();
356     EXPECT_FALSE(extractor->IsDirExist(""));
357     EXPECT_FALSE(extractor->IsDirExist(ERROR_PATH));
358 }
359 
360 /*
361  * Feature: Extractor
362  * Function: IsDirExist
363  * SubFunction: NA
364  * FunctionPoints:Is dir exist
365  * EnvConditions: NA
366  * CaseDescription: Create extractor, call is dir exist function.
367  */
368 HWTEST_F(ExtractorTest, IsDirExist_002, TestSize.Level1)
369 {
370     std::shared_ptr<Extractor> extractor = std::make_shared<Extractor>(testPath_);
371 
372     extractor->initial_ = true;
373     extractor->zipFile_.entriesMap_.emplace("a/b/c.txt", ZipEntry());
374     extractor->zipFile_.entriesMap_.emplace("a/c.txt", ZipEntry());
375     extractor->zipFile_.entriesMap_.emplace("a/b.txt", ZipEntry());
376     extractor->zipFile_.entriesMap_.emplace("a.txt", ZipEntry());
377     extractor->zipFile_.entriesMap_.emplace("b.txt", ZipEntry());
378     extractor->zipFile_.entriesMap_.emplace("b/c.txt", ZipEntry());
379     extractor->zipFile_.entriesMap_.emplace("b/c/", ZipEntry());
380     extractor->zipFile_.entriesMap_.emplace("c", ZipEntry());
381     extractor->zipFile_.isOpen_ = true;
382 
383     EXPECT_FALSE(extractor->IsDirExist(""));
384     EXPECT_TRUE(extractor->IsDirExist("/"));
385     EXPECT_TRUE(extractor->IsDirExist("a"));
386     EXPECT_TRUE(extractor->IsDirExist("b/c"));
387     EXPECT_TRUE(extractor->IsDirExist("b/c/"));
388     EXPECT_FALSE(extractor->IsDirExist("a.txt"));
389     EXPECT_FALSE(extractor->IsDirExist("a.txt/"));
390     EXPECT_FALSE(extractor->IsDirExist("d"));
391     EXPECT_FALSE(extractor->IsDirExist("d/"));
392     EXPECT_FALSE(extractor->IsDirExist("a/b/c.txt"));
393 
394     extractor->SetCacheMode(CacheMode::CACHE_ALL);
395 
396     EXPECT_FALSE(extractor->IsDirExist(""));
397     EXPECT_TRUE(extractor->IsDirExist("/"));
398     EXPECT_TRUE(extractor->IsDirExist("a"));
399     EXPECT_TRUE(extractor->IsDirExist("b/c"));
400     EXPECT_TRUE(extractor->IsDirExist("b/c/"));
401     EXPECT_FALSE(extractor->IsDirExist("a.txt"));
402     EXPECT_FALSE(extractor->IsDirExist("a.txt/"));
403     EXPECT_FALSE(extractor->IsDirExist("d"));
404     EXPECT_FALSE(extractor->IsDirExist("a/b/c.txt"));
405 }
406 
407 /*
408  * Feature: Extractor
409  * Function: ExtractByName
410  * SubFunction: NA
411  * FunctionPoints:Extract by name
412  * EnvConditions: NA
413  * CaseDescription: Create extractor, call extract by name function.
414  */
415 HWTEST_F(ExtractorTest, ExtractByName_001, TestSize.Level1)
416 {
417     std::shared_ptr<Extractor> extractor = std::make_shared<Extractor>(testPath_);
418     std::ostringstream outStream;
419     std::string srcPath = MODULE_JSON_PATH;
420     EXPECT_FALSE(extractor->ExtractByName(srcPath, outStream));
421 
422     extractor->Init();
423     EXPECT_FALSE(extractor->ExtractByName("", outStream));
424     EXPECT_TRUE(extractor->ExtractByName(srcPath, outStream));
425     EXPECT_TRUE(sizeof(outStream) > 0);
426 }
427 
428 /*
429  * Feature: Extractor
430  * Function: GetSpecifiedTypeFiles
431  * SubFunction: NA
432  * FunctionPoints:Get specified type files
433  * EnvConditions: NA
434  * CaseDescription: Create extractor, call get specified type files function.
435  */
436 HWTEST_F(ExtractorTest, GetSpecifiedTypeFiles_001, TestSize.Level1)
437 {
438     std::shared_ptr<Extractor> extractor = std::make_shared<Extractor>(testPath_);
439     std::vector<std::string> fileList;
440     extractor->GetSpecifiedTypeFiles(fileList, ".abc");
441     EXPECT_TRUE(fileList.size() == 0);
442     extractor->Init();
443     extractor->GetSpecifiedTypeFiles(fileList, ".abc");
444     EXPECT_TRUE(fileList.size() > 0);
445 }
446 
447 /*
448  * Feature: Extractor
449  * Function: IsStageBasedModel
450  * SubFunction: NA
451  * FunctionPoints:Is stage based model
452  * EnvConditions: NA
453  * CaseDescription: Create extractor, call is stage based model function.
454  */
455 HWTEST_F(ExtractorTest, IsStageBasedModel_001, TestSize.Level1)
456 {
457     std::shared_ptr<Extractor> extractor = std::make_shared<Extractor>(testPath_);
458     std::vector<std::string> fileList;
459     EXPECT_FALSE(extractor->IsStageBasedModel("MainAbility"));
460 
461     extractor->Init();
462     EXPECT_FALSE(extractor->IsStageBasedModel("MainAbility"));
463 }
464 
465 /*
466  * Feature: Extractor
467  * Function: IsSameHap
468  * SubFunction: NA
469  * FunctionPoints:Is same hap
470  * EnvConditions: NA
471  * CaseDescription: Create extractor, call is same hap function.
472  */
473 HWTEST_F(ExtractorTest, IsSameHap_001, TestSize.Level1)
474 {
475     std::string loadPath;
476     std::shared_ptr<Extractor> extractor = std::make_shared<Extractor>(loadPath);
477     EXPECT_FALSE(extractor->IsSameHap(""));
478 
479     std::shared_ptr<Extractor> extractor1 = std::make_shared<Extractor>(testPath_);
480     extractor1->Init();
481     EXPECT_FALSE(extractor1->IsSameHap(""));
482     EXPECT_FALSE(extractor1->IsSameHap(ERROR_HAP_PATH));
483     EXPECT_TRUE(extractor1->IsSameHap(testPath_));
484 }
485 
486 /*
487  * Feature: Extractor
488  * Function: ExtractToBufByName
489  * SubFunction: NA
490  * EnvConditions: NA
491  * CaseDescription: Create extractor, call ExtractToBufByName function.
492  */
493 HWTEST_F(ExtractorTest, ExtractToBufByName_001, TestSize.Level1)
494 {
495     std::shared_ptr<Extractor> extractor1 = std::make_shared<Extractor>(testPath_);
496     extractor1->Init();
497     std::unique_ptr<uint8_t[]> data;
498     size_t len = 0;
499     EXPECT_FALSE(extractor1->ExtractToBufByName("", data, len));
500 }
501 
502 /*
503  * Feature: Extractor
504  * Function: GetData
505  * SubFunction: NA
506  * EnvConditions: NA
507  * CaseDescription: Create extractor, call GetData function.
508  */
509 HWTEST_F(ExtractorTest, GetData_001, TestSize.Level1)
510 {
511     std::shared_ptr<Extractor> extractor1 = std::make_shared<Extractor>(testPath_);
512     std::string fileName = "www";
513     extractor1->GetData(fileName, false);
514     extractor1->GetSafeData(fileName);
515     bool res = extractor1->IsHapCompress(fileName);
516     EXPECT_EQ(res, false);
517 }
518 
519 /*
520  * Feature: ExtractResourceManager
521  * Function: SetGlobalObject
522  * SubFunction: NA
523  * EnvConditions: NA
524  * CaseDescription: Create ExtractResourceManager, call SetGlobalObject function.
525  */
526 HWTEST_F(ExtractorTest, SetGlobalObject_001, TestSize.Level1)
527 {
528     std::shared_ptr<Global::Resource::ResourceManager> resourceManager = nullptr;
529     std::shared_ptr<ExtractResourceManager> ers = std::make_shared<ExtractResourceManager>();
530     ers->SetGlobalObject(resourceManager);
531     EXPECT_EQ(ers->GetGlobalObject(), nullptr);
532 }
533 
534 /*
535  * Feature: ZipFile
536  * Function: SetContentLocation
537  * SubFunction: NA
538  * EnvConditions: NA
539  * CaseDescription: Create ZipFile, call SetContentLocation function.
540  */
541 HWTEST_F(ExtractorTest, SetContentLocation_001, TestSize.Level1)
542 {
543     std::shared_ptr<ZipFile> zipf = std::make_shared<ZipFile>(TEST_HAP_PATH);
544     ZipPos start = 0;
545     size_t length = 1;
546     zipf->SetContentLocation(start, length);
547     std::string srcPath = "srcPath";
548     std::set<std::string> fileSet;
549     zipf->GetChildNames(srcPath, fileSet);
550     std::string fileName = "www";
551     std::unique_ptr<uint8_t[]> dataPtr = std::make_unique<uint8_t[]>(length);
552     bool ret = zipf->ExtractFileFromMMap(fileName, nullptr, dataPtr, length);
553     EXPECT_EQ(ret, false);
554 }
555 }  // namespace AbilityBase
556 }  // namespace OHOS
557