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