• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "runtime_extractor.h"
20 
21 using namespace testing;
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 namespace AbilityRuntime {
26 namespace {
27 const std::string TEST_HAP_PATH("/system/app/com.ohos.settings/Settings.hap");
28 const std::string TEST_HAP_SEETINGS_PATH("/system/app/Settings/Settings.hap");
29 const std::string ERROR_HAP_PATH("/system/app/com.ohos.settings/XXX.hap");
30 const std::string TEST_THIRD_HAP_PATH("/data/app/el1/bundle/public/com.ohos.settings/Settings.hap");
31 const std::string MODULE_JSON_PATH("module.json");
32 const std::string CONFIG_JSON_PATH("config.json");
33 const std::string OUT_PATH("/data/module.json");
34 const std::string MAIN_ABILITY_PATH("ets/MainAbility");
35 const std::string FA_MAIN_ABILITY_PATH("assets/js/default");
36 const std::string ERROR_PATH("ets/MainAbilityXXX");
37 const std::string MAIN_ABILITY_FILENAME("ets/MainAbility/MainAbility.abc");
38 const std::string ERROR_FILENAME("ets/MainAbility/XXX.abc");
39 } // namespace
40 class RuntimeExtractorTest : public testing::Test {
41 public:
42     static void SetUpTestCase();
43     static void TearDownTestCase();
44     void SetUp();
45     void TearDown();
46 
47     std::string testPath_;
48 };
49 
SetUpTestCase()50 void RuntimeExtractorTest::SetUpTestCase()
51 {}
52 
TearDownTestCase()53 void RuntimeExtractorTest::TearDownTestCase()
54 {}
55 
SetUp()56 void RuntimeExtractorTest::SetUp()
57 {
58     std::ifstream ifs(TEST_HAP_PATH.c_str());
59     if (ifs.good()) {
60         testPath_ = TEST_HAP_PATH;
61     } else {
62         testPath_ = TEST_HAP_SEETINGS_PATH;
63     }
64 }
65 
TearDown()66 void RuntimeExtractorTest::TearDown()
67 {}
68 
69 /*
70  * Feature: RuntimeExtractor
71  * Function: Init
72  * SubFunction: NA
73  * FunctionPoints:Init runtime extractor
74  * EnvConditions: NA
75  * CaseDescription: Create runtime extractor, call Init function.
76  */
77 HWTEST_F(RuntimeExtractorTest, RuntimeExtractorInit_001, TestSize.Level1)
78 {
79     std::string loadPath;
80     std::shared_ptr<RuntimeExtractor> runtimeExtractor1 = std::make_shared<RuntimeExtractor>(loadPath);
81     EXPECT_FALSE(runtimeExtractor1->Init());
82 
83     std::shared_ptr<RuntimeExtractor> runtimeExtractor2 = std::make_shared<RuntimeExtractor>(testPath_);
84     EXPECT_TRUE(runtimeExtractor2->Init());
85 }
86 
87 /*
88  * Feature: RuntimeExtractor
89  * Function: Create
90  * SubFunction: NA
91  * FunctionPoints:Create runtime extractor
92  * EnvConditions: NA
93  * CaseDescription: Create runtime extractor.
94  */
95 HWTEST_F(RuntimeExtractorTest, RuntimeExtractorCreate_001, TestSize.Level1)
96 {
97     std::string loadPath;
98     std::shared_ptr<RuntimeExtractor> runtimeExtractor1 = RuntimeExtractor::Create(loadPath);
99     EXPECT_TRUE(runtimeExtractor1 == nullptr);
100 
101     loadPath = ERROR_HAP_PATH;
102     std::shared_ptr<RuntimeExtractor> runtimeExtractor2 = RuntimeExtractor::Create(loadPath);
103     EXPECT_TRUE(runtimeExtractor2 == nullptr);
104 
105     std::shared_ptr<RuntimeExtractor> runtimeExtractor3 = RuntimeExtractor::Create(testPath_);
106     EXPECT_TRUE(runtimeExtractor3 != nullptr);
107 
108     loadPath = TEST_THIRD_HAP_PATH;
109     std::shared_ptr<RuntimeExtractor> runtimeExtractor4 = RuntimeExtractor::Create(loadPath);
110     EXPECT_TRUE(runtimeExtractor4 == nullptr);
111 }
112 
113 /*
114  * Feature: RuntimeExtractor
115  * Function: GetFileBuffer
116  * SubFunction: NA
117  * FunctionPoints:Get file buffer
118  * EnvConditions: NA
119  * CaseDescription: Create runtime extractor, call get file buffer function.
120  */
121 HWTEST_F(RuntimeExtractorTest, GetFileBuffer_001, TestSize.Level1)
122 {
123     std::shared_ptr<RuntimeExtractor> runtimeExtractor = std::make_shared<RuntimeExtractor>(testPath_);
124     std::ostringstream outStream;
125     std::string srcPath = MODULE_JSON_PATH;
126     EXPECT_FALSE(runtimeExtractor->GetFileBuffer(srcPath, outStream));
127 
128     runtimeExtractor->Init();
129     EXPECT_FALSE(runtimeExtractor->GetFileBuffer("", outStream));
130     EXPECT_FALSE(runtimeExtractor->GetFileBuffer(CONFIG_JSON_PATH, outStream));
131     EXPECT_TRUE(runtimeExtractor->GetFileBuffer(srcPath, outStream));
132     EXPECT_TRUE(sizeof(outStream) > 0);
133 }
134 
135 /*
136  * Feature: RuntimeExtractor
137  * Function: GetFileList
138  * SubFunction: NA
139  * FunctionPoints:Get file list
140  * EnvConditions: NA
141  * CaseDescription: Create runtime extractor, call get file list function.
142  */
143 HWTEST_F(RuntimeExtractorTest, GetFileList_001, TestSize.Level1)
144 {
145     std::shared_ptr<RuntimeExtractor> runtimeExtractor = std::make_shared<RuntimeExtractor>(testPath_);
146     std::vector<std::string> fileList;
147     std::string srcPath = MAIN_ABILITY_PATH;
148     EXPECT_FALSE(runtimeExtractor->GetFileList(srcPath, fileList));
149 
150     runtimeExtractor->Init();
151     EXPECT_FALSE(runtimeExtractor->GetFileList("", fileList));
152     EXPECT_TRUE(runtimeExtractor->GetFileList(FA_MAIN_ABILITY_PATH, fileList));
153     EXPECT_TRUE(fileList.size() == 0);
154     EXPECT_TRUE(runtimeExtractor->GetFileList(srcPath, fileList));
155     EXPECT_TRUE(fileList.size() > 0);
156 }
157 
158 /*
159  * Feature: RuntimeExtractor
160  * Function: HasEntry
161  * SubFunction: NA
162  * FunctionPoints:Has entry
163  * EnvConditions: NA
164  * CaseDescription: Create runtime extractor, call has entry function.
165  */
166 HWTEST_F(RuntimeExtractorTest, HasEntry_001, TestSize.Level1)
167 {
168     std::shared_ptr<RuntimeExtractor> runtimeExtractor = std::make_shared<RuntimeExtractor>(testPath_);
169     std::string fileName = MAIN_ABILITY_FILENAME;
170     EXPECT_FALSE(runtimeExtractor->HasEntry(fileName));
171 
172     runtimeExtractor->Init();
173     EXPECT_FALSE(runtimeExtractor->HasEntry(""));
174     EXPECT_FALSE(runtimeExtractor->HasEntry(ERROR_FILENAME));
175     EXPECT_TRUE(runtimeExtractor->HasEntry(fileName));
176 }
177 
178 /*
179  * Feature: RuntimeExtractor
180  * Function: IsDirExist
181  * SubFunction: NA
182  * FunctionPoints:Is dir exist
183  * EnvConditions: NA
184  * CaseDescription: Create runtime extractor, call is dir exist function.
185  */
186 HWTEST_F(RuntimeExtractorTest, IsDirExist_001, TestSize.Level1)
187 {
188     std::shared_ptr<RuntimeExtractor> runtimeExtractor = std::make_shared<RuntimeExtractor>(testPath_);
189     std::string srcPath = MAIN_ABILITY_PATH;
190     EXPECT_FALSE(runtimeExtractor->IsDirExist(srcPath));
191 
192     runtimeExtractor->Init();
193     EXPECT_FALSE(runtimeExtractor->IsDirExist(""));
194     EXPECT_FALSE(runtimeExtractor->IsDirExist(ERROR_PATH));
195     EXPECT_TRUE(runtimeExtractor->IsDirExist(srcPath));
196 }
197 
198 /*
199  * Feature: RuntimeExtractor
200  * Function: ExtractByName
201  * SubFunction: NA
202  * FunctionPoints:Extract by name
203  * EnvConditions: NA
204  * CaseDescription: Create runtime extractor, call extract by name function.
205  */
206 HWTEST_F(RuntimeExtractorTest, ExtractByName_001, TestSize.Level1)
207 {
208     std::shared_ptr<RuntimeExtractor> runtimeExtractor = std::make_shared<RuntimeExtractor>(testPath_);
209     std::ostringstream outStream;
210     std::string srcPath = MODULE_JSON_PATH;
211     EXPECT_FALSE(runtimeExtractor->ExtractByName(srcPath, outStream));
212 
213     runtimeExtractor->Init();
214     EXPECT_FALSE(runtimeExtractor->ExtractByName("", outStream));
215     EXPECT_TRUE(runtimeExtractor->ExtractByName(srcPath, outStream));
216     EXPECT_TRUE(sizeof(outStream) > 0);
217 }
218 
219 /*
220  * Feature: RuntimeExtractor
221  * Function: ExtractFile
222  * SubFunction: NA
223  * FunctionPoints:Extract file
224  * EnvConditions: NA
225  * CaseDescription: Create runtime extractor, call extract file function.
226  */
227 HWTEST_F(RuntimeExtractorTest, ExtractFile_001, TestSize.Level1)
228 {
229     std::shared_ptr<RuntimeExtractor> runtimeExtractor = std::make_shared<RuntimeExtractor>(testPath_);
230     std::string outPath = OUT_PATH;
231     std::string srcPath = MODULE_JSON_PATH;
232     EXPECT_FALSE(runtimeExtractor->ExtractFile(srcPath, outPath));
233 
234     runtimeExtractor->Init();
235     EXPECT_FALSE(runtimeExtractor->ExtractFile("", outPath));
236     EXPECT_FALSE(runtimeExtractor->ExtractFile(srcPath, ""));
237     EXPECT_TRUE(runtimeExtractor->ExtractFile(srcPath, outPath));
238     std::ifstream f(outPath.c_str());
239     EXPECT_TRUE(f.good());
240 }
241 
242 /*
243  * Feature: RuntimeExtractor
244  * Function: GetZipFileNames
245  * SubFunction: NA
246  * FunctionPoints:Get zip file names
247  * EnvConditions: NA
248  * CaseDescription: Create runtime extractor, call get zip file names function.
249  */
250 HWTEST_F(RuntimeExtractorTest, GetZipFileNames_001, TestSize.Level1)
251 {
252     std::shared_ptr<RuntimeExtractor> runtimeExtractor = std::make_shared<RuntimeExtractor>(testPath_);
253     std::vector<std::string> fileList;
254     EXPECT_FALSE(runtimeExtractor->GetZipFileNames(fileList));
255     EXPECT_TRUE(fileList.size() == 0);
256 
257     runtimeExtractor->Init();
258     EXPECT_TRUE(runtimeExtractor->GetZipFileNames(fileList));
259     EXPECT_TRUE(fileList.size() > 0);
260 }
261 
262 /*
263  * Feature: RuntimeExtractor
264  * Function: GetSpecifiedTypeFiles
265  * SubFunction: NA
266  * FunctionPoints:Get specified type files
267  * EnvConditions: NA
268  * CaseDescription: Create runtime extractor, call get specified type files function.
269  */
270 HWTEST_F(RuntimeExtractorTest, GetSpecifiedTypeFiles_001, TestSize.Level1)
271 {
272     std::shared_ptr<RuntimeExtractor> runtimeExtractor = std::make_shared<RuntimeExtractor>(testPath_);
273     std::vector<std::string> fileList;
274     runtimeExtractor->GetSpecifiedTypeFiles(fileList, ".abc");
275     EXPECT_TRUE(fileList.size() == 0);
276     runtimeExtractor->Init();
277     runtimeExtractor->GetSpecifiedTypeFiles(fileList, ".abc");
278     EXPECT_TRUE(fileList.size() > 0);
279 }
280 
281 /*
282  * Feature: RuntimeExtractor
283  * Function: IsStageBasedModel
284  * SubFunction: NA
285  * FunctionPoints:Is stage based model
286  * EnvConditions: NA
287  * CaseDescription: Create runtime extractor, call is stage based model function.
288  */
289 HWTEST_F(RuntimeExtractorTest, IsStageBasedModel_001, TestSize.Level1)
290 {
291     std::shared_ptr<RuntimeExtractor> runtimeExtractor = std::make_shared<RuntimeExtractor>(testPath_);
292     std::vector<std::string> fileList;
293     EXPECT_FALSE(runtimeExtractor->IsStageBasedModel("MainAbility"));
294 
295     runtimeExtractor->Init();
296     EXPECT_FALSE(runtimeExtractor->IsStageBasedModel("MainAbility"));
297 }
298 
299 /*
300  * Feature: RuntimeExtractor
301  * Function: IsSameHap
302  * SubFunction: NA
303  * FunctionPoints:Is same hap
304  * EnvConditions: NA
305  * CaseDescription: Create runtime extractor, call is same hap function.
306  */
307 HWTEST_F(RuntimeExtractorTest, IsSameHap_001, TestSize.Level1)
308 {
309     std::string loadPath;
310     std::shared_ptr<RuntimeExtractor> runtimeExtractor = std::make_shared<RuntimeExtractor>(loadPath);
311     EXPECT_FALSE(runtimeExtractor->IsSameHap(""));
312 
313     std::shared_ptr<RuntimeExtractor> runtimeExtractor1 = std::make_shared<RuntimeExtractor>(testPath_);
314     runtimeExtractor1->Init();
315     EXPECT_FALSE(runtimeExtractor1->IsSameHap(""));
316     EXPECT_FALSE(runtimeExtractor1->IsSameHap(ERROR_HAP_PATH));
317     EXPECT_TRUE(runtimeExtractor1->IsSameHap(testPath_));
318 }
319 }  // namespace AbilityRuntime
320 }  // namespace OHOS
321