1 /*
2  * Copyright (c) 2021 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 <gtest/gtest.h>
17 
18 #define protected public
19 #include "ability_command.h"
20 #undef protected
21 #include "mock_ability_manager_stub.h"
22 #define private public
23 #include "ability_manager_client.h"
24 #undef private
25 #include "ability_manager_interface.h"
26 
27 using namespace testing::ext;
28 using namespace OHOS;
29 using namespace OHOS::AAFwk;
30 
31 namespace {
32 const std::string STRING_DEVICE = "device";
33 const std::string STRING_ABILITY_NAME = "ability";
34 const std::string STRING_ABILITY_NAME_INVALID = "invalid_ability";
35 const std::string STRING_BUNDLE_NAME = "bundle";
36 const std::string STRING_BUNDLE_NAME_INVALID = "invalid_bundle";
37 const std::string STRING_RECORD_ID = "1024";
38 const std::string STRING_RECORD_ID_INVALID = "2048";
39 const std::string STRING_STATE_ON = "on";
40 const std::string STRING_STATE_ON_INVALID = "invalid_on";
41 const std::string STRING_STATE_OFF = "off";
42 const std::string STRING_STATE_OFF_INVALID = "invalid_off";
43 }  // namespace
44 
45 class AaCommandStartModuleTest : public ::testing::Test {
46 public:
47     static void SetUpTestCase();
48     static void TearDownTestCase();
49     void SetUp() override;
50     void TearDown() override;
51 
52     void MakeMockObjects() const;
53 
54     std::string cmd_ = "start";
55 };
56 
SetUpTestCase()57 void AaCommandStartModuleTest::SetUpTestCase()
58 {}
59 
TearDownTestCase()60 void AaCommandStartModuleTest::TearDownTestCase()
61 {}
62 
SetUp()63 void AaCommandStartModuleTest::SetUp()
64 {
65     // reset optind to 0
66     optind = 0;
67 
68     // make mock objects
69     MakeMockObjects();
70 }
71 
TearDown()72 void AaCommandStartModuleTest::TearDown()
73 {}
74 
MakeMockObjects() const75 void AaCommandStartModuleTest::MakeMockObjects() const
76 {
77     // mock a stub
78     auto managerStubPtr = sptr<IAbilityManager>(new MockAbilityManagerStub());
79 
80     // set the mock stub
81     auto managerClientPtr = AbilityManagerClient::GetInstance();
82     managerClientPtr->proxy_ = managerStubPtr;
83 }
84 
85 /**
86  * @tc.number: Aa_Command_Start_ModuleTest_0100
87  * @tc.name: ExecCommand
88  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name>" command.
89  */
90 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0100, Function | MediumTest | Level1)
91 {
92     char* argv[] = {
93         (char*)TOOL_NAME.c_str(),
94         (char*)cmd_.c_str(),
95         (char*)"-d",
96         (char*)STRING_DEVICE.c_str(),
97         (char*)"-a",
98         (char*)STRING_ABILITY_NAME.c_str(),
99         (char*)"-b",
100         (char*)STRING_BUNDLE_NAME.c_str(),
101         (char*)"",
102     };
103     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
104 
105     AbilityManagerShellCommand cmd(argc, argv);
106     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
107 }
108 
109 /**
110  * @tc.number: Aa_Command_Start_ModuleTest_0200
111  * @tc.name: ExecCommand
112  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name>" command.
113  */
114 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0200, Function | MediumTest | Level1)
115 {
116     char* argv[] = {
117         (char*)TOOL_NAME.c_str(),
118         (char*)cmd_.c_str(),
119         (char*)"-d",
120         (char*)STRING_DEVICE.c_str(),
121         (char*)"-a",
122         (char*)STRING_ABILITY_NAME_INVALID.c_str(),
123         (char*)"-b",
124         (char*)STRING_BUNDLE_NAME.c_str(),
125         (char*)"",
126     };
127     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
128 
129     AbilityManagerShellCommand cmd(argc, argv);
130     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_NG + "\n" + cmd.messageMap_.at(RESOLVE_ABILITY_ERR) + "\n");
131 }
132 
133 /**
134  * @tc.number: Aa_Command_Start_ModuleTest_0300
135  * @tc.name: ExecCommand
136  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name>" command.
137  */
138 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0300, Function | MediumTest | Level1)
139 {
140     char* argv[] = {
141         (char*)TOOL_NAME.c_str(),
142         (char*)cmd_.c_str(),
143         (char*)"-d",
144         (char*)STRING_DEVICE.c_str(),
145         (char*)"-a",
146         (char*)STRING_ABILITY_NAME.c_str(),
147         (char*)"-b",
148         (char*)STRING_BUNDLE_NAME_INVALID.c_str(),
149         (char*)"",
150     };
151     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
152 
153     AbilityManagerShellCommand cmd(argc, argv);
154     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_NG + "\n" + cmd.messageMap_.at(RESOLVE_APP_ERR) + "\n");
155 }
156 
157 /**
158  * @tc.number: Aa_Command_Start_ModuleTest_0400
159  * @tc.name: ExecCommand
160  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name> -D" command.
161  * @tc.type: FUNC
162  */
163 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0400, Function | MediumTest | Level1)
164 {
165     char* argv[] = {
166         (char*)TOOL_NAME.c_str(),
167         (char*)cmd_.c_str(),
168         (char*)"-d",
169         (char*)STRING_DEVICE.c_str(),
170         (char*)"-a",
171         (char*)STRING_ABILITY_NAME.c_str(),
172         (char*)"-b",
173         (char*)STRING_BUNDLE_NAME.c_str(),
174         (char*)"-D",
175         (char*)"",
176     };
177     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
178 
179     AbilityManagerShellCommand cmd(argc, argv);
180     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
181 }
182 
183 /**
184  * @tc.number: Aa_Command_Start_ModuleTest_0500
185  * @tc.name: ExecCommand
186  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name> --pi <key> <integer-value>" command.
187  * @tc.type: FUNC
188  */
189 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0500, Function | MediumTest | Level1)
190 {
191     char* argv[] = {
192         (char*)TOOL_NAME.c_str(),
193         (char*)cmd_.c_str(),
194         (char*)"-d",
195         (char*)STRING_DEVICE.c_str(),
196         (char*)"-a",
197         (char*)STRING_ABILITY_NAME.c_str(),
198         (char*)"-b",
199         (char*)STRING_BUNDLE_NAME.c_str(),
200         (char*)"--pi",
201         (char*)"kinteger",
202         (char*)"900",
203         (char*)"",
204     };
205     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
206 
207     AbilityManagerShellCommand cmd(argc, argv);
208     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
209 }
210 
211 /**
212  * @tc.number: Aa_Command_Start_ModuleTest_0600
213  * @tc.name: ExecCommand
214  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name> --pb <key> <bool-value>" command.
215  * @tc.type: FUNC
216  */
217 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0600, Function | MediumTest | Level1)
218 {
219     char* argv[] = {
220         (char*)TOOL_NAME.c_str(),
221         (char*)cmd_.c_str(),
222         (char*)"-d",
223         (char*)STRING_DEVICE.c_str(),
224         (char*)"-a",
225         (char*)STRING_ABILITY_NAME.c_str(),
226         (char*)"-b",
227         (char*)STRING_BUNDLE_NAME.c_str(),
228         (char*)"--pb",
229         (char*)"kbool",
230         (char*)"true",
231         (char*)"",
232     };
233     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
234 
235     AbilityManagerShellCommand cmd(argc, argv);
236     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
237 }
238 
239 /**
240  * @tc.number: Aa_Command_Start_ModuleTest_0700
241  * @tc.name: ExecCommand
242  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name> --ps <key> <value>" command.
243  * @tc.type: FUNC
244  */
245 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0700, Function | MediumTest | Level1)
246 {
247     char* argv[] = {
248         (char*)TOOL_NAME.c_str(),
249         (char*)cmd_.c_str(),
250         (char*)"-d",
251         (char*)STRING_DEVICE.c_str(),
252         (char*)"-a",
253         (char*)STRING_ABILITY_NAME.c_str(),
254         (char*)"-b",
255         (char*)STRING_BUNDLE_NAME.c_str(),
256         (char*)"--ps",
257         (char*)"kstring",
258         (char*)"string-value",
259         (char*)"",
260     };
261     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
262 
263     AbilityManagerShellCommand cmd(argc, argv);
264     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
265 }
266 
267 /**
268  * @tc.number: Aa_Command_Start_ModuleTest_0800
269  * @tc.name: ExecCommand
270  * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name> \
271  * --psn <key> <integer-value>" command.
272  * @tc.type: FUNC
273  */
274 HWTEST_F(AaCommandStartModuleTest, Aa_Command_Start_ModuleTest_0800, Function | MediumTest | Level1)
275 {
276     char* argv[] = {
277         (char*)TOOL_NAME.c_str(),
278         (char*)cmd_.c_str(),
279         (char*)"-d",
280         (char*)STRING_DEVICE.c_str(),
281         (char*)"-a",
282         (char*)STRING_ABILITY_NAME.c_str(),
283         (char*)"-b",
284         (char*)STRING_BUNDLE_NAME.c_str(),
285         (char*)"--psn",
286         (char*)"knullstring",
287         (char*)"",
288     };
289     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
290 
291     AbilityManagerShellCommand cmd(argc, argv);
292     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
293 }