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#ifndef TEST_WUKONG_UTIL_H
17#define TEST_WUKONG_UTIL_H
18
19#include <cstdio>
20#include <dirent.h>
21#include <string>
22#include <unistd.h>
23#include <vector>
24
25#include "bundle_mgr_proxy.h"
26#include "errors.h"
27#include "semaphore_ex.h"
28#include "singleton.h"
29
30namespace OHOS {
31namespace WuKong {
32class WuKongUtil : public DelayedSingleton<WuKongUtil> {
33public:
34    /**
35     * @brief Get the All App Info object
36     * @return Return ERR_OK on success, others on failure.
37     */
38    ErrCode GetAllAppInfo();
39
40    /**
41     * @brief to find  the location of the key bundle in the bundle list.
42     * @param bundleList the bundlelist of test
43     * @param key the key subscript in the bundle list.
44     * @return the location of the key bundle in the bundlelist.
45     */
46    uint32_t FindElement(std::vector<std::string> &bundleList, std::string key);
47
48    /**
49     * @brief to find if the key bundle is in the bundle list.
50     * @param bundleList the bundlelist of test
51     * @param key the key subscript in the bundle list.
52     * @return if the key bundle in the bundlelist.
53     */
54    bool ContainsElement(std::vector<std::string> &bundleList, std::string key);
55
56    /**
57     * @brief Set the Allow List.
58     * @param optarg command string
59     * @return Return ERR_OK on success, others on failure.
60     */
61    ErrCode SetAllowList(const std::string &optarg);
62
63    /**
64     * @brief Set the Block List.
65     * @param optarg command string.
66     * @return Return ERR_OK on success, others on failure.
67     */
68    ErrCode SetBlockList(const std::string &optarg);
69
70    /**
71     * @brief Set the BlockPage List.
72     * @param optarg command string.
73     * @return Return ERR_OK on success, others on failure.
74     */
75    ErrCode SetBlockPageList(const std::string &optarg);
76
77    /**
78     * @brief Set the BlockAbility List.
79     * @param optarg command string.
80     * @return Return ERR_OK on success, others on failure.
81     */
82    ErrCode SetBlockAbilityList(const std::string &optarg);
83
84    /**
85     * @brief Check if AbilityName is valid
86     * @return Return ERR_OK argument is valid, others is invalid.
87     */
88    ErrCode CheckAbilityNameList();
89
90    /**
91     * @brief Get the Allow Ability List of the test.
92     * @param bundleNameArgs the Allow Ability list.
93     */
94    void GetAllowAbilityList(std::vector<std::string> &allowAbilityList);
95
96    /**
97     * @brief Check the list of allowed abilities to cannibalize
98     * @return Return ERR_OK is ok, others have not ok.
99     */
100    ErrCode CheckAbilityArgumentList(std::vector<std::string> &allowAbilityList);
101
102    /**
103     * @brief Set Allow Ability List.
104     * @param optarg the Ability list of the test.
105     */
106    ErrCode SetAllowAbilityList(const std::string &optarg);
107
108    /**
109     * @brief Get the Block Ability List of the test.
110     * @param blockAbilityList the block Ability List.
111     */
112    void GetBlockAbilityList(std::vector<std::string> &blockAbilityList);
113
114    /**
115     * @brief Get the Allow List of the test.
116     * @param bundleNameArgs the Allow list.
117     */
118    void GetAllowList(std::vector<std::string> &Allowlist);
119
120    /**
121     * @brief Get the block List of the test.
122     * @param blockList the block list.
123     */
124    void GetBlockList(std::vector<std::string> &blockList);
125
126    /**
127     * @brief Get the block page List of the test.
128     * @param blockList the block page list.
129     */
130    void GetBlockPageList(std::vector<std::string> &blockPageList);
131
132    /**
133     * @brief get the bundlelist and ability list.
134     * @param bundleList the bundle list of the test.
135     * @param abilityList the ability list of the test.
136     */
137    void SetAllAppInfo(std::vector<std::string> &bundleList, std::vector<std::string> &abilityList);
138
139    /**
140     * @brief Get the Bundle List of the test.
141     * @param bundlelist
142     * @param abilitylist
143     */
144    void GetBundleList(std::vector<std::string> &bundlelist, std::vector<std::string> &abilitylist);
145
146    /**
147     * @brief Get the list of the bundle list except for blockList.
148     * @param validbundlelist  the list except for blockList
149     */
150    void GetValidBundleList(std::vector<std::string> &validbundlelist);
151
152    /**
153     * @brief get the size of screen.
154     * @param width the width of screen.
155     * @param height the height of screen.
156     * @return Return ERR_OK on success, others on failure.
157     */
158    ErrCode GetScreenSize(int32_t &width, int32_t &height);
159
160    /**
161     * @brief get the icon path of app.
162     * @param iconpath icon path of app.
163     */
164    void GetIconPath(std::string &iconpath);
165
166    /**
167     * @brief get start run time string for report, log etc.
168     * @return start run time string.
169     */
170    std::string GetStartRunTime()
171    {
172        return startRunTime_;
173    }
174
175    /**
176     * @brief get screenshot.
177     * @return Return ERR_OK on success, others on failure.
178     */
179    ErrCode WukongScreenCap(std::string &screenStorePath, bool gCommandUitest, bool g_commandCheckBWScreen = false);
180
181    /**
182     * @brief get all abilities by bundleName string.
183     * @param bundleName bundle name.
184     * @param abilities all abilities find by bundle name.
185     */
186    void GetAllAbilitiesByBundleName(std::string bundleName, std::vector<std::string> &abilities);
187
188    /**
189     * @brief Check if param is valid
190     * @param argumentlist argument list.
191     * @param isAddToList should add check item to bundleList.
192     * @return Return ERR_OK argument is valid, others is invalid.
193     */
194    ErrCode CheckArgumentList(std::vector<std::string> &arguments, bool isAddToList);
195
196    /**
197     * @brief get current test wukong base dir.
198     * @return dir string
199     */
200    std::string GetCurrentTestDir();
201
202    /*
203     * @brief copy file from soruce to dest
204     * @param targetFile
205     * @param sourceFile
206     * @param destFile
207     * @return bool
208     */
209    bool CopyFile(std::string &targetFile, std::string &sourceDir, std::string &destDir);
210
211    /*
212     * @brief deleted screenshots
213     * @param targetDir
214     * @return bool
215     */
216    bool DeleteFile(std::string targetDir);
217
218    /*
219     * @brief set the whitelist list
220     * @param tempAllowList
221     */
222    void SetTempAllowList(std::vector<std::string> tempAllowList);
223
224    /*
225     * @brief get the whitelist list
226     * @return tmpAllowList
227     */
228    std::vector<std::string> GetTempAllowList();
229
230    /*
231     * @brief set orderFlag
232     * @param orderFlag
233     */
234    void SetOrderFlag(bool orderFlag);
235
236    /*
237     * @brief get orderFlag
238     * @return bool
239     */
240    bool GetOrderFlag();
241
242    /*
243     * @brief set ComponentUri
244     * @param optarg
245     */
246    ErrCode SetComponentUri(const std::string &optarg);
247
248    /*
249     * @brief get ComponentUri
250     * @return string
251     */
252    std::string GetComponentUri();
253
254    /*
255     * @brief set ComponentUriType
256     * @param optarg
257     */
258    ErrCode SetComponentUriType(const std::string &optarg);
259
260    /*
261     * @brief get ComponentUriType
262     * @return string
263     */
264    std::string GetComponentUriType();
265
266    /**
267     * @brief get all ability infos
268     * @return Return ERR_OK if get sth, others not.
269     */
270    ErrCode GetAllAbilities();
271
272    /*
273     * @brief set the component id block list
274     * @param compIdBlockList
275     */
276    void SetCompIdBlockList(const std::string &optarg);
277
278    /*
279     * @brief get the component id block list
280     * @return compIdBlockList
281     */
282    std::vector<std::string> GetCompIdBlockList();
283
284    /*
285     * @brief set the component Type block list
286     * @param compTypeBlockList
287     */
288    void SetCompTypeBlockList(const std::string &optarg);
289
290    /*
291     * @brief get the component Type block list
292     * @return compTypeBlockList
293     */
294    std::vector<std::string> GetCompTypeBlockList();
295
296    /*
297     * @brief cmd calls
298     * @param cmd
299     * @return cmd result
300     */
301    std::string runProcess(std::string cmd);
302
303    /*
304     * @brief set IsFirstStartAppFlag
305     * @param isFirstStartApp
306     */
307    void SetIsFirstStartAppFlag(bool isFirstStartApp);
308
309    /*
310     * @brief get isFirstStartApp
311     * @return bool
312     */
313    bool GetIsFirstStartAppFlag();
314
315    /*
316     * @brief get Bundle Pid
317     * @return string
318     */
319    std::string GetBundlePid();
320
321    /*
322     * @brief get Bundle PssTotal
323     * @return uint64_t
324     */
325    uint64_t GetBundlePssTotal();
326
327    DECLARE_DELAYED_SINGLETON(WuKongUtil);
328
329private:
330    /**
331     * @brief delete repeat argument of argument list.
332     * @param argumentlist argument list.
333     */
334    void DelRepeatArguments(std::vector<std::string> &argumentlist);
335
336    /**
337     * @brief Check if there is a conflict between the block and Allow lists
338     * @return Return ERR_OK no conflict, others have conflict.
339     */
340    ErrCode CheckBundleNameList();
341
342    /**
343     * @brief get bundle manager proxy for get abilitys.
344     * @return return bundleManager instance.
345     */
346    sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgrProxy() const;
347
348    std::string iconPath_;
349    std::vector<std::string> bundleList_;
350    std::vector<std::string> abilityList_;
351    std::vector<std::string> allowAbilityList_;
352    std::vector<std::string> validBundleList_;
353    std::vector<std::string> validAbilityList_;
354    std::vector<std::string> allowList_;
355    std::vector<std::string> blockList_ = {"com.ohos.devicemanagerui", "com.ohos.screenlock",
356                                           "com.ohos.permissionmanager"};
357    std::vector<std::string> blockPageList_ = {"pages/system"};
358    std::vector<std::string> blockAbilityList_;
359    std::vector<std::string> unLaunchedBundleList_;
360    std::vector<std::string> unLaunchedAbilityList_;
361    std::vector<std::string> allBundleList_;
362    std::vector<std::string> allAbilityList_;
363    std::string uri_;
364    std::string uriType_;
365    int32_t screenWidth_ = -1;
366    int32_t screenHeight_ = -1;
367
368    std::string startRunTime_;
369    std::string curDir_;
370
371    bool orderFlag_ = false;
372    bool isFirstStartApp_ = true;
373    std::vector<std::string> tempAllowList_;
374    std::vector<std::string> compIdBlockList_ = {};
375    std::vector<std::string> compTypeBlockList_ = {"Divider"};
376};
377
378class WukongSemaphore {
379public:
380    WukongSemaphore(const std::string &name, size_t size) : name_(name)
381    {
382        maxCount_ = 0;
383    }
384    ~WukongSemaphore()
385    {
386    }
387
388    bool Create()
389    {
390        return true;
391    }
392    bool Unlink()
393    {
394        return true;
395    }
396
397    bool Open()
398    {
399        return true;
400    }
401    bool Close()
402    {
403        return true;
404    }
405
406    bool Wait()
407    {
408        return true;
409    }
410    bool TryWait()
411    {
412        return true;
413    }
414    bool TimedWait(const struct timespec &ts)
415    {
416        return true;
417    }
418    bool Post()
419    {
420        return true;
421    }
422
423    int GetValue() const
424    {
425        return maxCount_;
426    }
427
428private:
429    std::string name_;
430    int maxCount_;
431};
432
433typedef WukongSemaphore NamedSemaphore;
434}  // namespace WuKong
435}  // namespace OHOS
436#endif  // TEST_WUKONG_UTIL_H
437