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_shell_command.h"
17
18 #include <cstdlib>
19
20 #include "ability_manager_client.h"
21 #include "accessibility_element_info.h"
22 #include "accessibility_ui_test_ability.h"
23 #include "appexecfwk_errors.h"
24 #include "bundle_mgr_proxy.h"
25 #include "component_manager.h"
26 #include "iservice_registry.h"
27 #include "launcher_service.h"
28 #include "random_test_flow.h"
29 #include "special_test_flow.h"
30 #include "system_ability_definition.h"
31 #include "test_flow_factory.h"
32
33 namespace OHOS {
34 namespace WuKong {
35 namespace {
36 const std::string WUKONG_TOOL_NAME = "wukong";
37
38 const std::string WUKONG_TOOL_VERSION = "version: 3.2.0.0\n";
39
40 const std::string ACE_ENABLE = "param set persist.ace.testmode.enabled 1";
41
42 const std::string WUKONG_HELP_MSG =
43 "usage: wukong <command> [<arguments>]\n"
44 "These are common wukong command list:\n"
45 " help wukong help information\n"
46 " -v/--version wukong version\n"
47 " exec run random test\n"
48 " special run special test\n"
49 " appinfo show all app information\n";
50 } // namespace
51
WuKongShellCommand(int argc,char * argv[])52 WuKongShellCommand::WuKongShellCommand(int argc, char *argv[]) : ShellCommand(argc, argv, WUKONG_TOOL_NAME)
53 {
54 }
55
init()56 ErrCode WuKongShellCommand::init()
57 {
58 return OHOS::ERR_OK;
59 }
60
CreateCommandMap()61 ErrCode WuKongShellCommand::CreateCommandMap()
62 {
63 TRACK_LOG_STD();
64 commandMap_ = {
65 {"--version", std::bind(&WuKongShellCommand::GetWuKongVersion, this)},
66 {"-v", std::bind(&WuKongShellCommand::GetWuKongVersion, this)},
67 {"help", std::bind(&WuKongShellCommand::RunAsHelpCommand, this)},
68 {"stop", std::bind(&WuKongShellCommand::RunStopCommand, this)},
69 {"exec", std::bind(&WuKongShellCommand::RunTestCommand, this)},
70 {"special", std::bind(&WuKongShellCommand::RunTestCommand, this)},
71 {"appinfo", std::bind(&WuKongShellCommand::ShowAllAppInfo, this)},
72 };
73 TRACK_LOG_END();
74 return OHOS::ERR_OK;
75 }
76
CreateMessageMap()77 ErrCode WuKongShellCommand::CreateMessageMap()
78 {
79 TRACK_LOG_STD();
80 messageMap_ = {
81 {
82 ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR,
83 "error: wukong internal error.",
84 },
85 };
86 TRACK_LOG_END();
87 return OHOS::ERR_OK;
88 }
89
GetWuKongVersion()90 ErrCode WuKongShellCommand::GetWuKongVersion()
91 {
92 resultReceiver_ = WUKONG_TOOL_VERSION;
93 return OHOS::ERR_OK;
94 }
95
RunStopCommand()96 ErrCode WuKongShellCommand::RunStopCommand()
97 {
98 WukongSemaphore sem(SEMPHORE_STOP_NAME, 1);
99 sem.Open();
100 if (sem.GetValue() == 0) {
101 sem.Post();
102 }
103 sem.Close();
104 return OHOS::ERR_OK;
105 }
106
RunTestCommand()107 ErrCode WuKongShellCommand::RunTestCommand()
108 {
109 TRACK_LOG_STD();
110 // get testFlow by cmd_ of ShellCommand
111 std::shared_ptr<TestFlow> testFlow = TestFlowFactory::GetTestFlow(*this, cmd_);
112 if (testFlow == nullptr) {
113 ERROR_LOG_STR("GetTestFlow TestFlow is null command (%s)", cmd_.c_str());
114 return OHOS::ERR_INVALID_VALUE;
115 }
116
117 auto cm = ComponentManager::GetInstance();
118 if (cm == nullptr) {
119 ERROR_LOG("cm is nullptr");
120 return OHOS::ERR_INVALID_VALUE;
121 }
122 uint32_t handle = cm->AddRegisterListener(testFlow);
123
124 // check the command arguments
125 // if argument is not ok, exit wukong command.
126 ErrCode res = testFlow->CheckVaildityCmd();
127 if (res != OHOS::ERR_OK) {
128 DEBUG_LOG("Command arguments is invalid and exit");
129 return res;
130 }
131
132 // connect to accessibility
133 if (!cm->Connect()) {
134 ERROR_LOG("ComponentManager Connect failed");
135 return OHOS::ERR_INVALID_OPERATION;
136 }
137 DEBUG_LOG("connected successfully");
138
139 auto aacPtr = OHOS::Accessibility::AccessibilityUITestAbility::GetInstance();
140 OHOS::Accessibility::AccessibilityElementInfo root;
141 if (aacPtr->GetRoot(root) != Accessibility::RET_OK) {
142 system(ACE_ENABLE.c_str());
143 }
144 // run test flow.
145 res = testFlow->Run();
146 if (res != OHOS::ERR_OK) {
147 DEBUG_LOG("Test flow run failed");
148 }
149 cm->Disconnect();
150 cm->DeleteRegisterListener(handle);
151
152 TRACK_LOG_END();
153 return res;
154 }
155
RunAsHelpCommand()156 ErrCode WuKongShellCommand::RunAsHelpCommand()
157 {
158 TRACK_LOG_STD();
159 resultReceiver_.append(WUKONG_HELP_MSG);
160 TRACK_LOG_END();
161 return OHOS::ERR_OK;
162 }
163
GetArgv()164 char **WuKongShellCommand::GetArgv()
165 {
166 return argv_;
167 }
168
GetArgc()169 int WuKongShellCommand::GetArgc()
170 {
171 return argc_;
172 }
173
ResultReceiverAppend(const std::string receiver)174 void WuKongShellCommand::ResultReceiverAppend(const std::string receiver)
175 {
176 resultReceiver_.append(receiver);
177 }
178
ShowAllAppInfo()179 ErrCode WuKongShellCommand::ShowAllAppInfo()
180 {
181 TRACK_LOG_STD();
182 ErrCode result = WuKongUtil::GetInstance()->GetAllAppInfo();
183 if (result != OHOS::ERR_OK) {
184 return result;
185 }
186 DEBUG_LOG_STR("GetAllAppInfo result: (%u)", result);
187 std::vector<std::string> bundleList;
188 std::vector<std::string> abilityList;
189 std::string iconpath;
190 WuKongUtil::GetInstance()->GetIconPath(iconpath);
191 WuKongUtil::GetInstance()->GetBundleList(bundleList, abilityList);
192
193 std::stringstream appInfo;
194 DEBUG_LOG_STR("bundleList size: (%u)", bundleList.size());
195 for (unsigned index = 0; index < bundleList.size(); index++) {
196 DEBUG_LOG_STR("Bundle Name: (%s), Ability Name: (%s)", bundleList[index].c_str(), abilityList[index].c_str());
197 appInfo << "BundleName: " << bundleList[index] << std::endl;
198 appInfo << "AbilityName: " << abilityList[index] << std::endl;
199 DEBUG_LOG_STR("IconPath: %s", iconpath.c_str());
200 }
201 resultReceiver_.append(appInfo.str());
202 TRACK_LOG_END();
203 return result;
204 }
205 } // namespace WuKong
206 } // namespace OHOS
207