• 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 #ifndef TEST_WUKONG_UTIL_H
17 #define TEST_WUKONG_UTIL_H
18 
19 #include <dirent.h>
20 #include <string>
21 #include <unistd.h>
22 #include <vector>
23 
24 #include "bundle_mgr_proxy.h"
25 #include "errors.h"
26 #include "semaphore_ex.h"
27 #include "singleton.h"
28 
29 namespace OHOS {
30 namespace WuKong {
31 class WuKongUtil : public DelayedSingleton<WuKongUtil> {
32 public:
33     /**
34      * @brief Get the All App Info object
35      * @return Return ERR_OK on success, others on failure.
36      */
37     ErrCode GetAllAppInfo();
38 
39     /**
40      * @brief to find  the location of the key bundle in the bundle list.
41      * @param bundleList the bundlelist of test
42      * @param key the key subscript in the bundle list.
43      * @return the location of the key bundle in the bundlelist.
44      */
45     uint32_t FindElement(std::vector<std::string> bundleList, std::string key);
46 
47     /**
48      * @brief Set the Allow List.
49      * @param optarg command string
50      * @return Return ERR_OK on success, others on failure.
51      */
52     ErrCode SetAllowList(const std::string &optarg);
53 
54     /**
55      * @brief Set the Block List.
56      * @param optarg command string.
57      * @return Return ERR_OK on success, others on failure.
58      */
59     ErrCode SetBlockList(const std::string &optarg);
60 
61     /**
62      * @brief Get the Allow List of the test.
63      * @param bundleNameArgs the Allow list.
64      */
65     void GetAllowList(std::vector<std::string> &Allowlist);
66 
67     /**
68      * @brief Get the block List of the test.
69      * @param blockList the block list.
70      */
71     void GetBlockList(std::vector<std::string> &blockList);
72 
73     /**
74      * @brief get the bundlelist and ability list.
75      * @param bundleList the bundle list of the test.
76      * @param abilityList the ability list of the test.
77      */
78     void SetAllAppInfo(std::vector<std::string> &bundleList, std::vector<std::string> &abilityList);
79 
80     /**
81      * @brief Get the Bundle List of the test.
82      * @param bundlelist
83      * @param abilitylist
84      */
85     void GetBundleList(std::vector<std::string> &bundlelist, std::vector<std::string> &abilitylist);
86 
87     /**
88      * @brief Get the list of the bundle list except for blockList.
89      * @param validbundlelist  the list except for blockList
90      */
91     void GetValidBundleList(std::vector<std::string> &validbundlelist);
92 
93     /**
94      * @brief get the size of screen.
95      * @param width the width of screen.
96      * @param height the height of screen.
97      * @return Return ERR_OK on success, others on failure.
98      */
99     ErrCode GetScreenSize(int32_t &width, int32_t &height);
100 
101     /**
102      * @brief get the icon path of app.
103      * @param iconpath icon path of app.
104      */
105     void GetIconPath(std::string &iconpath);
106 
107     /**
108      * @brief get start run time string for report, log etc.
109      * @return start run time string.
110      */
GetStartRunTime()111     std::string GetStartRunTime()
112     {
113         return startRunTime_;
114     }
115 
116     /**
117      * @brief get screenshot.
118      * @return Return ERR_OK on success, others on failure.
119      */
120     ErrCode WukongScreenCap(std::string &screenStorePath);
121 
122     /**
123      * @brief get all abilities by bundleName string.
124      * @param bundleName bundle name.
125      * @param abilities all abilities find by bundle name.
126      */
127     void GetAllAbilitiesByBundleName(std::string bundleName, std::vector<std::string> &abilities);
128 
129     /**
130      * @brief Check if param is valid
131      * @param argumentlist argument list.
132      * @return Return ERR_OK argument is valid, others is invalid.
133      */
134     ErrCode CheckArgumentList(std::vector<std::string> &arguments);
135 
136     /**
137      * @brief get current test wukong base dir.
138      * @return dir string
139      */
140     std::string GetCurrentTestDir();
141 
142     /*
143      * @brief copy file from soruce to dest
144      * @param targetFile
145      * @param sourceFile
146      * @param destFile
147      * @return bool
148      */
149     bool CopyFile(std::string &targetFile, std::string &sourceDir, std::string &destDir);
150 
151     /*
152      * @brief deleted screenshots
153      * @param targetDir
154      * @return bool
155      */
156     bool DeleteFile(std::string targetDir);
157 
158     /*
159      * @brief set the whitelist list
160      * @param tempAllowList
161      */
162     void SetTempAllowList(std::vector<std::string> tempAllowList);
163 
164     /*
165      * @brief get the whitelist list
166      * @return tmpAllowList
167      */
168     std::vector<std::string> GetTempAllowList();
169 
170     /*
171      * @brief set orderFlag
172      * @param orderFlag
173      */
174     void SetOrderFlag(bool orderFlag);
175 
176     /*
177      * @brief get orderFlag
178      * @return bool
179      */
180     bool GetOrderFlag();
181 
182     DECLARE_DELAYED_SINGLETON(WuKongUtil);
183 
184 private:
185     /**
186      * @brief delete repeat argument of argument list.
187      * @param argumentlist argument list.
188      */
189     void DelRepeatArguments(std::vector<std::string> &argumentlist);
190 
191     /**
192      * @brief Check if there is a conflict between the block and Allow lists
193      * @return Return ERR_OK no conflict, others have conflict.
194      */
195     ErrCode CheckBundleNameList();
196 
197     /**
198      * @brief get bundle manager proxy for get abilitys.
199      * @return return bundleManager instance.
200      */
201     sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgrProxy() const;
202 
203     std::string iconPath_;
204     std::vector<std::string> bundleList_;
205     std::vector<std::string> abilityList_;
206     std::vector<std::string> validBundleList_;
207     std::vector<std::string> validAbilityList_;
208     std::vector<std::string> allowList_;
209     std::vector<std::string> blockList_ = {"com.ohos.devicemanagerui", "com.ohos.screenlock",
210                                            "com.ohos.permissionmanager"};
211     int32_t screenWidth_ = -1;
212     int32_t screenHeight_ = -1;
213 
214     std::string startRunTime_;
215     std::string curDir_;
216 
217     bool orderFlag_ = false;
218     std::vector<std::string> tempAllowList_;
219 };
220 
221 class WukongSemaphore {
222 public:
WukongSemaphore(const std::string & name,size_t size)223     WukongSemaphore(const std::string &name, size_t size) : name_(name)
224     {
225         maxCount_ = 0;
226     }
~WukongSemaphore()227     ~WukongSemaphore()
228     {
229     }
230 
Create()231     bool Create()
232     {
233         return true;
234     }
Unlink()235     bool Unlink()
236     {
237         return true;
238     }
239 
Open()240     bool Open()
241     {
242         return true;
243     }
Close()244     bool Close()
245     {
246         return true;
247     }
248 
Wait()249     bool Wait()
250     {
251         return true;
252     }
TryWait()253     bool TryWait()
254     {
255         return true;
256     }
TimedWait(const struct timespec & ts)257     bool TimedWait(const struct timespec &ts)
258     {
259         return true;
260     }
Post()261     bool Post()
262     {
263         return true;
264     }
265 
GetValue()266     int GetValue() const
267     {
268         return maxCount_;
269     }
270 
271 private:
272     std::string name_;
273     int maxCount_;
274 };
275 
276 typedef WukongSemaphore NamedSemaphore;
277 }  // namespace WuKong
278 }  // namespace OHOS
279 #endif  // TEST_WUKONG_UTIL_H
280