• 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 "wukong_util.h"
17 
18 #include <climits>
19 #include <dirent.h>
20 #include <fstream>
21 #include <iostream>
22 #include <memory.h>
23 #include <sstream>
24 #include <sys/stat.h>
25 
26 #include "display_manager.h"
27 #include "if_system_ability_manager.h"
28 #include "iservice_registry.h"
29 #include "launcher_service.h"
30 #include "png.h"
31 #include "string_ex.h"
32 #include "system_ability_definition.h"
33 #include "wukong_define.h"
34 #include "bundle_mgr_proxy.h"
35 #include "common.h"
36 #include "accessibility_ui_test_ability.h"
37 #include "component_manager.h"
38 
39 namespace OHOS {
40 namespace WuKong {
41 namespace {
42 const std::string DEFAULT_DIR = "/data/local/tmp/wukong/report/";
TakeWuKongScreenCap(const std::string & wkScreenPath)43 bool TakeWuKongScreenCap(const std::string &wkScreenPath)
44 {
45     // get PixelMap from DisplayManager API
46     Rosen::DisplayManager &displayMgr = Rosen::DisplayManager::GetInstance();
47     std::shared_ptr<Media::PixelMap> pixelMap = displayMgr.GetScreenshot(displayMgr.GetDefaultDisplayId());
48     static constexpr int bitmapDepth = 8;
49     if (pixelMap == nullptr) {
50         DEBUG_LOG("Failed to get display pixelMap");
51         return false;
52     }
53     auto width = static_cast<uint32_t>(pixelMap->GetWidth());
54     auto height = static_cast<uint32_t>(pixelMap->GetHeight());
55     auto data = pixelMap->GetPixels();
56     auto stride = static_cast<uint32_t>(pixelMap->GetRowBytes());
57     png_structp pngStruct = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
58     if (pngStruct == nullptr) {
59         DEBUG_LOG("error: png_create_write_struct nullptr!");
60         return false;
61     }
62     png_infop pngInfo = png_create_info_struct(pngStruct);
63     if (pngInfo == nullptr) {
64         DEBUG_LOG("error: png_create_info_struct error nullptr!");
65         png_destroy_write_struct(&pngStruct, nullptr);
66         return false;
67     }
68     FILE *fp = fopen(wkScreenPath.c_str(), "wb");
69     if (fp == nullptr) {
70         ERROR_LOG("error: open file error!");
71         png_destroy_write_struct(&pngStruct, &pngInfo);
72         return false;
73     }
74     png_init_io(pngStruct, fp);
75     png_set_IHDR(pngStruct, pngInfo, width, height, bitmapDepth, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE,
76                  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
77     png_set_packing(pngStruct);          // set packing info
78     png_write_info(pngStruct, pngInfo);  // write to header
79     for (uint32_t i = 0; i < height; i++) {
80         png_write_row(pngStruct, data + (i * stride));
81     }
82     png_write_end(pngStruct, pngInfo);
83     // free
84     png_destroy_write_struct(&pngStruct, &pngInfo);
85     (void)fclose(fp);
86     return true;
87 }
88 }  // namespace
89 using namespace std;
90 using namespace OHOS::AppExecFwk;
91 const int USE_ID = 100;
WuKongUtil()92 WuKongUtil::WuKongUtil()
93 {
94     TRACK_LOG_STD();
95     const int timeBufsize = 32;
96     char fileNameBuf[timeBufsize] = {0};
97     time_t currentTime = time(0);
98     uint32_t res = 0;
99 
100     if (currentTime > 0) {
101         tm *timePtr = localtime(&currentTime);
102         if (timePtr == nullptr) {
103             ERROR_LOG("timePtr is nullptr");
104             return;
105         }
106         res = strftime(fileNameBuf, timeBufsize, "%Y%m%d_%H%M%S", timePtr);
107     }
108     if (res > 0) {
109         startRunTime_ = std::string(fileNameBuf);
110     } else {
111         startRunTime_ = "unvalid_time";
112     }
113     curDir_ = DEFAULT_DIR + startRunTime_ + "/";
114     DEBUG_LOG_STR("common dir{%s}", curDir_.c_str());
115     DIR *rootDir = nullptr;
116     std::string dirStr = "/";
117     std::vector<std::string> strs;
118     OHOS::SplitStr(curDir_, "/", strs);
119     for (auto str : strs) {
120         dirStr.append(str);
121         dirStr.append("/");
122         if ((rootDir = opendir(dirStr.c_str())) == nullptr) {
123             int ret = mkdir(dirStr.c_str(), S_IROTH | S_IRWXU | S_IRWXG);
124             if (ret != 0 && dirStr != "/data/" && dirStr != "/data/local/") {
125                 std::cerr << "failed to create dir: " << dirStr << std::endl;
126                 break;
127             }
128         } else {
129             closedir(rootDir);
130         }
131     }
132     DEBUG_LOG_STR("%s", startRunTime_.c_str());
133     TRACK_LOG_END();
134 }
135 
136 /**
137  * @brief: release util
138  */
~WuKongUtil()139 WuKongUtil::~WuKongUtil()
140 {
141     TRACK_LOG_STD();
142 }
143 
GetAllAppInfo()144 ErrCode WuKongUtil::GetAllAppInfo()
145 {
146     AppExecFwk::LauncherService launcherservice;
147     std::vector<AppExecFwk::LauncherAbilityInfo> launcherAbilityInfos(0);
148 
149     bool result = launcherservice.GetAllLauncherAbilityInfos(USE_ID, launcherAbilityInfos);
150     DEBUG_LOG_STR("GetAllLauncherAbilityInfos: size (%u), result (%d)", launcherAbilityInfos.size(), result);
151     if (launcherAbilityInfos.size() == 0) {
152         ERROR_LOG("GetAllLauncherAbilityInfos size is 0");
153         return OHOS::ERR_INVALID_VALUE;
154     }
155     for (auto item : launcherAbilityInfos) {
156         iconPath_ = item.applicationInfo.iconPath;
157         DEBUG_LOG_STR("iconPath: %s", item.applicationInfo.iconPath.c_str());
158         DEBUG_LOG_STR("codePath: %s", item.applicationInfo.codePath.c_str());
159         DEBUG_LOG_STR("dataDir: %s", item.applicationInfo.dataDir.c_str());
160         DEBUG_LOG_STR("dataBaseDir: %s", item.applicationInfo.dataBaseDir.c_str());
161         DEBUG_LOG_STR("cacheDir: %s", item.applicationInfo.cacheDir.c_str());
162         DEBUG_LOG_STR("entryDir: %s", item.applicationInfo.entryDir.c_str());
163         std::string bundleName = item.elementName.GetBundleName();
164         // store the list of all bundle names
165         bundleList_.push_back(bundleName);
166         abilityList_.push_back(item.elementName.GetAbilityName());
167         uint32_t isInBlockList = FindElement(blockList_, bundleName);
168         if (isInBlockList != INVALIDVALUE) {
169             continue;
170         }
171         // store the list of bundle names except for block list
172         validBundleList_.push_back(bundleName);
173         validAbilityList_.push_back(item.elementName.GetAbilityName());
174     }
175     GetAllAbilities();
176     return OHOS::ERR_OK;
177 }
178 
GetBundleList(std::vector<std::string> & bundlelist,std::vector<std::string> & abilitylist)179 void WuKongUtil::GetBundleList(std::vector<std::string> &bundlelist, std::vector<std::string> &abilitylist)
180 {
181     if (bundleList_.size() == 0) {
182         GetAllAppInfo();
183     }
184     bundlelist = bundleList_;
185     abilitylist = abilityList_;
186 }
187 
FindElement(std::vector<std::string> & bundleList,std::string key)188 uint32_t WuKongUtil::FindElement(std::vector<std::string> &bundleList, std::string key)
189 {
190     auto it = find(bundleList.begin(), bundleList.end(), key);
191     if (it != bundleList.end()) {
192         return distance(bundleList.begin(), it);
193     }
194     return INVALIDVALUE;
195 }
196 
ContainsElement(std::vector<std::string> & bundleList,std::string key)197 bool WuKongUtil::ContainsElement(std::vector<std::string> &bundleList, std::string key)
198 {
199     return INVALIDVALUE != FindElement(bundleList, key);
200 }
201 
CheckBundleNameList()202 ErrCode WuKongUtil::CheckBundleNameList()
203 {
204     std::set<std::string> m(allowList_.begin(), allowList_.end());
205 
206     for (auto it = blockList_.begin(); it != blockList_.end(); it++) {
207         if (m.find(*it) != m.end()) {
208             ERROR_LOG("invalid param:please check params of '-p' and '-b'");
209             return OHOS::ERR_INVALID_VALUE;
210         }
211     }
212     return OHOS::ERR_OK;
213 }
214 
CheckArgumentList(std::vector<std::string> & arguments,bool isAddToList)215 ErrCode WuKongUtil::CheckArgumentList(std::vector<std::string> &arguments, bool isAddToList)
216 {
217     ErrCode result = OHOS::ERR_OK;
218     GetAllAppInfo();
219     for (uint32_t i = 0; i < arguments.size(); i++) {
220         uint32_t index = FindElement(bundleList_, arguments[i]);
221         if (index == INVALIDVALUE) {
222             uint32_t unLaunchedIndex = FindElement(unLaunchedBundleList_, arguments[i]);
223             if (unLaunchedIndex == INVALIDVALUE) {
224                 ERROR_LOG_STR("bundle name '%s' is not be included in all bundles", arguments[i].c_str());
225                 result = OHOS::ERR_INVALID_VALUE;
226             } else if (isAddToList) {
227                 bundleList_.push_back(arguments[i]);
228                 abilityList_.push_back(unLaunchedAbilityList_[unLaunchedIndex]);
229             }
230         }
231     }
232     return result;
233 }
234 
SetAllowList(const std::string & optarg)235 ErrCode WuKongUtil::SetAllowList(const std::string &optarg)
236 {
237     SplitStr(optarg, ",", allowList_);
238     ErrCode result = CheckArgumentList(allowList_, true);
239     if (result == OHOS::ERR_OK) {
240         // delete repeat argument
241         DelRepeatArguments(allowList_);
242         if (allowList_.size() > 0) {
243             result = CheckBundleNameList();
244         }
245     }
246     return result;
247 }
248 
CheckAbilityArgumentList(std::vector<std::string> & arguments)249 ErrCode WuKongUtil::CheckAbilityArgumentList(std::vector<std::string> &arguments)
250 {
251     ErrCode result = OHOS::ERR_OK;
252     GetAllAppInfo();
253     for (uint32_t i = 0; i < arguments.size(); i++) {
254         uint32_t index = FindElement(allAbilityList_, arguments[i]);
255         if (index == INVALIDVALUE) {
256             ERROR_LOG_STR("Ability name '%s' is not be included in all abilities", arguments[i].c_str());
257             result = OHOS::ERR_INVALID_VALUE;
258         }
259     }
260     return result;
261 }
262 
SetAllowAbilityList(const std::string & optarg)263 ErrCode WuKongUtil::SetAllowAbilityList(const std::string &optarg)
264 {
265     SplitStr(optarg, ",", allowAbilityList_);
266     ErrCode result = CheckAbilityArgumentList(allowAbilityList_);
267     if (result == OHOS::ERR_OK) {
268         // delete repeat argument
269         DelRepeatArguments(allowAbilityList_);
270         if (allowAbilityList_.size() > 0) {
271             result = CheckAbilityNameList();
272         }
273     }
274     return result;
275 }
276 
SetBlockAbilityList(const std::string & optarg)277 ErrCode WuKongUtil::SetBlockAbilityList(const std::string &optarg)
278 {
279     SplitStr(optarg, ",", blockAbilityList_);
280     ErrCode result = CheckAbilityArgumentList(blockAbilityList_);
281     if (result == OHOS::ERR_OK) {
282         // delete repeat argument
283         DelRepeatArguments(blockAbilityList_);
284         if (blockAbilityList_.size() > 0) {
285             result = CheckAbilityNameList();
286         }
287     }
288     return result;
289 }
290 
CheckAbilityNameList()291 ErrCode WuKongUtil::CheckAbilityNameList()
292 {
293     std::set<std::string> m(allowAbilityList_.begin(), allowAbilityList_.end());
294 
295     for (auto it = blockAbilityList_.begin(); it != blockAbilityList_.end(); it++) {
296         if (m.find(*it) != m.end()) {
297             ERROR_LOG("invalid param:please check params of '-e' and '-E'");
298             return OHOS::ERR_INVALID_VALUE;
299         }
300     }
301     return OHOS::ERR_OK;
302 }
303 
SetBlockList(const std::string & optarg)304 ErrCode WuKongUtil::SetBlockList(const std::string &optarg)
305 {
306     SplitStr(optarg, ",", blockList_);
307     ErrCode result = CheckArgumentList(blockList_, false);
308     if (result == OHOS::ERR_OK) {
309         // delete repeat argument
310         DelRepeatArguments(blockList_);
311         if (blockList_.size() > 0) {
312             result = CheckBundleNameList();
313         }
314     }
315     return result;
316 }
317 
SetBlockPageList(const std::string & optarg)318 ErrCode WuKongUtil::SetBlockPageList(const std::string &optarg)
319 {
320     ErrCode result = OHOS::ERR_OK;
321     std::vector<std::string> temp;
322     SplitStr(optarg, ",", temp);
323     blockPageList_.insert(blockPageList_.end(), temp.begin(), temp.end());
324     // delete repeat argument
325     DelRepeatArguments(blockPageList_);
326     if (blockPageList_.size() > 0) {
327         stringstream ss;
328         for (const auto& str:blockPageList_) {
329         ss << str << " ";
330         }
331         INFO_LOG_STR("Please confirm that the blocked page is %s", ss.str().c_str());
332         result = OHOS::ERR_OK;
333     }
334     return result;
335 }
336 
DelRepeatArguments(std::vector<std::string> & argumentlist)337 void WuKongUtil::DelRepeatArguments(std::vector<std::string> &argumentlist)
338 {
339     std::set<std::string> s(argumentlist.begin(), argumentlist.end());
340     argumentlist.assign(s.begin(), s.end());
341 }
342 
GetAllowList(std::vector<std::string> & allowList)343 void WuKongUtil::GetAllowList(std::vector<std::string> &allowList)
344 {
345     allowList = allowList_;
346 }
347 
GetAllowAbilityList(std::vector<std::string> & allowAbilityList)348 void WuKongUtil::GetAllowAbilityList(std::vector<std::string> &allowAbilityList)
349 {
350     allowAbilityList = allowAbilityList_;
351 }
352 
GetBlockList(std::vector<std::string> & blockList)353 void WuKongUtil::GetBlockList(std::vector<std::string> &blockList)
354 {
355     blockList = blockList_;
356 }
357 
GetBlockAbilityList(std::vector<std::string> & blockAbilityList)358 void WuKongUtil::GetBlockAbilityList(std::vector<std::string> &blockAbilityList)
359 {
360     blockAbilityList = blockAbilityList_;
361 }
362 
GetBlockPageList(std::vector<std::string> & blockPageList)363 void WuKongUtil::GetBlockPageList(std::vector<std::string> &blockPageList)
364 {
365     blockPageList = blockPageList_;
366 }
367 
GetValidBundleList(std::vector<std::string> & validbundlelist)368 void WuKongUtil::GetValidBundleList(std::vector<std::string> &validbundlelist)
369 {
370     validbundlelist = validBundleList_;
371 }
372 
SetAllAppInfo(std::vector<std::string> & bundleList,std::vector<std::string> & abilityList)373 void WuKongUtil::SetAllAppInfo(std::vector<std::string> &bundleList, std::vector<std::string> &abilityList)
374 {
375     bundleList_ = bundleList;
376     abilityList_ = abilityList;
377 }
378 
SetTempAllowList(std::vector<std::string> tempAllowList)379 void WuKongUtil::SetTempAllowList(std::vector<std::string> tempAllowList)
380 {
381     tempAllowList_ = tempAllowList;
382 }
383 
GetTempAllowList()384 std::vector<std::string> WuKongUtil::GetTempAllowList()
385 {
386     return tempAllowList_;
387 }
388 
SetOrderFlag(bool orderFlag)389 void WuKongUtil::SetOrderFlag(bool orderFlag)
390 {
391     orderFlag_ = orderFlag;
392 }
393 
GetOrderFlag()394 bool WuKongUtil::GetOrderFlag()
395 {
396     return orderFlag_;
397 }
398 
GetScreenSize(int32_t & width,int32_t & height)399 ErrCode WuKongUtil::GetScreenSize(int32_t &width, int32_t &height)
400 {
401     ErrCode result = OHOS::ERR_OK;
402     if (screenWidth_ == -1 || screenHeight_ == -1) {
403         OHOS::Rosen::DisplayManager &displayMgr = OHOS::Rosen::DisplayManager::GetInstance();
404         sptr<OHOS::Rosen::Display> display = displayMgr.GetDefaultDisplay();
405         if (display == nullptr) {
406             ERROR_LOG("get screen size failed");
407             return OHOS::ERR_NO_INIT;
408         }
409         screenWidth_ = display->GetWidth();
410         screenHeight_ = display->GetHeight();
411     }
412     width = screenWidth_;
413     height = screenHeight_;
414     return result;
415 }
416 
GetIconPath(std::string & iconpath)417 void WuKongUtil::GetIconPath(std::string &iconpath)
418 {
419     iconpath = iconPath_;
420 }
421 
WukongScreenCap(std::string & screenStorePath,bool gCommandUitest)422 ErrCode WuKongUtil::WukongScreenCap(std::string &screenStorePath, bool gCommandUitest)
423 {
424     using namespace std::chrono;
425     ErrCode result = ERR_OK;
426     auto wukongts = to_string(time_point_cast<milliseconds>(system_clock::now()).time_since_epoch().count());
427     int fileExist_ = access((curDir_ + "screenshot").c_str(), F_OK);
428     if (fileExist_ == 0) {
429         DEBUG_LOG("File exist.");
430     } else {
431         const int wukongScreenShot = mkdir((curDir_ + "screenshot").c_str(), 0777);
432         DEBUG_LOG("File create.");
433         if (wukongScreenShot == -1) {
434             DEBUG_LOG("Error creating directory!");
435             result = ERR_NO_INIT;
436         }
437     }
438     auto wkScreenPath = curDir_ + "screenshot/" + "/" + wukongts + ".png";
439     DEBUG_LOG_STR("WukongScreenCap store path is  {%s}", wkScreenPath.c_str());
440     if (gCommandUitest) {
441         auto cm = ComponentManager::GetInstance();
442         auto auita = Accessibility::AccessibilityUITestAbility::GetInstance();
443         auita->Disconnect();
444         std::string uitestCmd = "uitest dumpLayout -i -p " + curDir_ + "screenshot" + "/" + wukongts + ".json";
445         std::string res = Common::runProcess(uitestCmd);
446         DEBUG_LOG_STR("unitestCmd : %s", res.c_str());
447         cm->Disconnect();
448         if (!cm->Connect()) {
449             ERROR_LOG("ComponentManager Connect failed");
450             return OHOS::ERR_INVALID_OPERATION;
451         }
452     }
453     bool isTakeScreen = TakeWuKongScreenCap(wkScreenPath);
454     if (isTakeScreen == true) {
455         screenStorePath = wkScreenPath;
456         DEBUG_LOG("The snapshot has been created.");
457     } else {
458         DEBUG_LOG("This snapshot can not be created.");
459     }
460     return result;
461 }
462 
GetBundleMgrProxy() const463 sptr<IBundleMgr> WuKongUtil::GetBundleMgrProxy() const
464 {
465     sptr<ISystemAbilityManager> systemAbilityManager =
466         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
467     if (!systemAbilityManager) {
468         ERROR_LOG("failed to get system ability mgr.");
469         return nullptr;
470     }
471 
472     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
473     if (!remoteObject) {
474         ERROR_LOG("failed to get bundle manager proxy.");
475         return nullptr;
476     }
477 
478     return iface_cast<IBundleMgr>(remoteObject);
479 }
480 
GetAllAbilitiesByBundleName(std::string bundleName,std::vector<std::string> & abilities)481 void WuKongUtil::GetAllAbilitiesByBundleName(std::string bundleName, std::vector<std::string> &abilities)
482 {
483     TRACK_LOG_STD();
484     sptr<IBundleMgr> bundleMgrProxy = GetBundleMgrProxy();
485     std::vector<BundleInfo> bundleInfos;
486     if (!bundleMgrProxy) {
487         ERROR_LOG("bundleMgrProxy is nullptr");
488         return;
489     }
490     bool getInfoResult = bundleMgrProxy->GetBundleInfos(BundleFlag::GET_BUNDLE_DEFAULT, bundleInfos, USE_ID);
491     if (!getInfoResult) {
492         ERROR_LOG("GetBundleInfos ERR");
493         return;
494     }
495     DEBUG_LOG_STR("bundles length{%d}", bundleInfos.size());
496     for (const auto &bundleIter : bundleInfos) {
497         TRACK_LOG_STR("bundleIter.name{%s}", bundleIter.name.c_str());
498         BundleInfo bundleInfo;
499         if (bundleIter.name == bundleName) {
500             TRACK_LOG_STR("map bundleName{%s}", bundleIter.name.c_str());
501             bool result =
502                 bundleMgrProxy->GetBundleInfo(bundleIter.name, BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo, 100);
503             if (!result) {
504                 ERROR_LOG_STR("WriteBundleInfo getBundleInfo result %d, bundleName: %s", result,
505                     bundleIter.name.c_str());
506                 break;
507             }
508             for (auto &abilityIter : bundleInfo.abilityInfos) {
509                 TRACK_LOG_STR("bundleName{%s} container abilities item{%s}", bundleIter.name.c_str(),
510                               (abilityIter.name).c_str());
511                 abilities.push_back(abilityIter.name);
512             }
513         }
514     }
515     TRACK_LOG_END();
516 }
517 
GetAllAbilities()518 ErrCode WuKongUtil::GetAllAbilities()
519 {
520     TRACK_LOG_STD();
521     ErrCode result = OHOS::ERR_INVALID_VALUE;
522     sptr<IBundleMgr> bundleMgrProxy = GetBundleMgrProxy();
523     std::vector<BundleInfo> bundleInfos;
524     if (!bundleMgrProxy) {
525         ERROR_LOG("bundleMgrProxy is nullptr");
526         return result;
527     }
528     bool getInfoResult = bundleMgrProxy->GetBundleInfos(BundleFlag::GET_BUNDLE_DEFAULT, bundleInfos, USE_ID);
529     if (!getInfoResult) {
530         ERROR_LOG("GetBundleInfos ERR");
531         return result;
532     }
533     DEBUG_LOG_STR("bundles length{%d}", bundleInfos.size());
534     for (const auto &bundleIter : bundleInfos) {
535         std::string bundleName = bundleIter.name;
536         uint32_t bundleListIndex = FindElement(bundleList_, bundleName);
537         BundleInfo bundleInfo;
538         bool getBundleResult =
539             bundleMgrProxy->GetBundleInfo(bundleName, BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo, 100);
540         if (!getBundleResult) {
541             ERROR_LOG_STR("WriteWuKongBundleInfo getBundleInfo result %d", getBundleResult);
542             continue;
543         }
544         if (bundleListIndex != INVALIDVALUE) {
545             for (auto &abilityIter : bundleInfo.abilityInfos) {
546                 allBundleList_.push_back(bundleName);
547                 allAbilityList_.push_back(abilityIter.name);
548                 DEBUG_LOG_STR("bundleName: %s, abilityName: %s", bundleName.c_str(), abilityIter.name.c_str());
549             }
550         } else {
551             for (auto &abilityIter : bundleInfo.abilityInfos) {
552                 unLaunchedBundleList_.push_back(bundleName);
553                 unLaunchedAbilityList_.push_back(abilityIter.name);
554                 allBundleList_.push_back(bundleName);
555                 allAbilityList_.push_back(abilityIter.name);
556                 DEBUG_LOG_STR("bundleName: %s, abilityName: %s", bundleName.c_str(), abilityIter.name.c_str());
557             }
558         }
559     }
560     if (unLaunchedAbilityList_.size() > 0) {
561         result = OHOS::ERR_OK;
562     }
563     TRACK_LOG_END();
564     return result;
565 }
566 
GetCurrentTestDir()567 std::string WuKongUtil::GetCurrentTestDir()
568 {
569     return curDir_;
570 }
571 
CopyFile(std::string & targetFile,std::string & sourceDir,std::string & destDir)572 bool WuKongUtil::CopyFile(std::string &targetFile, std::string &sourceDir, std::string &destDir)
573 {
574     std::ifstream in;
575     std::ofstream out;
576     DEBUG_LOG_STR("targetFile{%s} sourceDir{%s} destDir{%s}", targetFile.c_str(), sourceDir.c_str(), destDir.c_str());
577     char filepathSource[PATH_MAX] = {'\0'};
578     std::string sourceFile = sourceDir + targetFile;
579     char *realPathSource = realpath(sourceFile.c_str(), filepathSource);
580     if (realPathSource == nullptr) {
581         ERROR_LOG_STR("failed to get source file path (%s), errno: (%d)", sourceFile.c_str(), errno);
582         return false;
583     }
584     in.open(filepathSource, std::ios::binary);
585     if (in.fail()) {
586         std::cout << "Error 1: Fail to open the source file." << std::endl;
587         in.close();
588         out.close();
589         return false;
590     }
591 
592     char filepathDest[PATH_MAX] = {'\0'};
593     char *realPathDest = realpath(destDir.c_str(), filepathDest);
594     if (realPathDest == nullptr) {
595         ERROR_LOG_STR("failed to get dest dir path (%s), errno: (%d)", destDir.c_str(), errno);
596         return false;
597     }
598     DEBUG_LOG_STR("destDir{%s}", filepathDest);
599     std::string destFile = destDir + targetFile;
600     out.open(destFile.c_str(), std::ios::binary);
601     if (out.fail()) {
602         std::cout << "Error 2: Fail to create the new file." << std::endl;
603         out.close();
604         in.close();
605         return false;
606     }
607     out << in.rdbuf();
608     out.close();
609     in.close();
610     return true;
611 }
612 
DeleteFile(std::string targetDir)613 bool WuKongUtil::DeleteFile(std::string targetDir)
614 {
615     DIR *dirdp = nullptr;
616     char filepathSource[PATH_MAX] = {'\0'};
617     char *realPathSource = realpath(targetDir.c_str(), filepathSource);
618     if (realPathSource != nullptr) {
619         struct dirent *dp;
620         dirdp = opendir(targetDir.c_str());
621         while ((dp = readdir(dirdp)) != NULL) {
622             std::string currentFileName(dp->d_name);
623             std::string sourceFile = targetDir + currentFileName;
624             char *realFileSource = realpath(sourceFile.c_str(), filepathSource);
625             if (realFileSource != nullptr) {
626                 remove(sourceFile.c_str());
627             }
628         }
629     } else {
630         return false;
631     }
632     (void)closedir(dirdp);
633     return true;
634 }
635 
SetCompIdBlockList(const std::string & optarg)636 void WuKongUtil::SetCompIdBlockList(const std::string &optarg)
637 {
638     SplitStr(optarg, ",", compIdBlockList_);
639 }
640 
GetCompIdBlockList()641 std::vector<std::string> WuKongUtil::GetCompIdBlockList()
642 {
643     return compIdBlockList_;
644 }
645 
SetCompTypeBlockList(const std::string & optarg)646 void WuKongUtil::SetCompTypeBlockList(const std::string &optarg)
647 {
648     SplitStr(optarg, ",", compTypeBlockList_);
649 }
650 
GetCompTypeBlockList()651 std::vector<std::string> WuKongUtil::GetCompTypeBlockList()
652 {
653     return compTypeBlockList_;
654 }
655 
runProcess(std::string cmd)656 std::string WuKongUtil::runProcess(std::string cmd)
657 {
658     const unsigned int NUMBER_SIZE = 1024;
659     if (FILE* fp = popen(cmd.c_str(), "r")) {
660         std::ostringstream ostrStream;
661         char line[NUMBER_SIZE];
662         while (fgets(line, NUMBER_SIZE, fp)) {
663             ostrStream << line;
664         }
665         pclose(fp);
666         return ostrStream.str();
667     } else {
668         ERROR_LOG("popen function failed");
669     }
670     return "";
671 }
SetIsFirstStartAppFlag(bool isFirstStartApp)672 void WuKongUtil::SetIsFirstStartAppFlag(bool isFirstStartApp)
673 {
674     isFirstStartApp_ = isFirstStartApp;
675 }
GetIsFirstStartAppFlag()676 bool WuKongUtil::GetIsFirstStartAppFlag()
677 {
678     return isFirstStartApp_;
679 }
680 
681 }  // namespace WuKong
682 }  // namespace OHOS
683