1 /*
2  * Copyright (c) 2022-2024 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 private public
19 #include "ability_command.h"
20 #include "ability_manager_client.h"
21 #undef private
22 #include "ability_manager_interface.h"
23 #include "hilog_tag_wrapper.h"
24 #include "mock_ability_manager_stub.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::AAFwk;
29 
30 namespace {
31 const std::string STRING_CLASS_NAME = "ohos.acts.aafwk.ability.test.ConstructorTest#testDataAbilityOtherFunction0010";
32 const std::string STRING_USER_TEST_RUNNER = "JSUserTestRunner";
33 const std::string STRING_BUNDLE_NAME = "com.example.myapplication";
34 const std::string STRING_BUNDLE_NAME1 = "com.example.myapplication1";
35 const std::string CLASS = "class";
36 const std::string UNITTEST = "unittest";
37 const std::string TIME = "50";
38 }  // namespace
39 
40 class AbilityCommandModuleTest : public ::testing::Test {
41 public:
42     static void SetUpTestCase();
43     static void TearDownTestCase();
44     void SetUp() override;
45     void TearDown() override;
46 
47     void MakeMockObjects() const;
48 
49     std::string cmd_ = "test";
50 };
51 
SetUpTestCase()52 void AbilityCommandModuleTest::SetUpTestCase()
53 {}
54 
TearDownTestCase()55 void AbilityCommandModuleTest::TearDownTestCase()
56 {}
57 
SetUp()58 void AbilityCommandModuleTest::SetUp()
59 {
60     // reset optind to 0
61     optind = 0;
62 
63     // make mock objects
64     MakeMockObjects();
65 }
66 
TearDown()67 void AbilityCommandModuleTest::TearDown()
68 {}
69 
MakeMockObjects() const70 void AbilityCommandModuleTest::MakeMockObjects() const
71 {
72     // mock a stub
73     auto managerStubPtr = sptr<IAbilityManager>(new MockAbilityManagerStub());
74 
75     // set the mock stub
76     auto managerClientPtr = AbilityManagerClient::GetInstance();
77     managerClientPtr->proxy_ = managerStubPtr;
78 }
79 
80 /**
81  * @tc.number: Ability_Command_Module_Test_0100
82  * @tc.name: ExecCommand
83  * @tc.desc: Verify the "aa test -h -hhhhhhhhhhhhhhhhhhhhhh" command.
84  */
85 HWTEST_F(AbilityCommandModuleTest, Ability_Command_Module_Test_0100, Function | MediumTest | Level1)
86 {
87     TAG_LOGI(AAFwkTag::TEST, "Ability_Command_Module_Test_0100 is called");
88     char* argv[] = {
89         (char*)TOOL_NAME.c_str(),
90         (char*)cmd_.c_str(),
91         (char*)"-h",
92         (char*)"-hhhhhhhhhhhhhhhhhhhhhh",
93     };
94     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
95 
96     AbilityManagerShellCommand cmd(argc, argv);
97     EXPECT_EQ(cmd.RunAsTestCommand(), OHOS::ERR_OK);
98 }
99 
100 /**
101  * @tc.number: Ability_Command_Module_Test_0200
102  * @tc.name: ExecCommand
103  * @tc.desc: Verify the "aa test -hhhhhhhhhhhhhhhhhhhhhh -h" command.
104  */
105 HWTEST_F(AbilityCommandModuleTest, Ability_Command_Module_Test_0200, Function | MediumTest | Level1)
106 {
107     TAG_LOGI(AAFwkTag::TEST, "Ability_Command_Module_Test_0200 is called");
108     char* argv[] = {
109         (char*)TOOL_NAME.c_str(),
110         (char*)cmd_.c_str(),
111         (char*)"-hhhhhhhhhhhhhhhhhhhhhh",
112         (char*)"-h",
113     };
114     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
115 
116     AbilityManagerShellCommand cmd(argc, argv);
117     EXPECT_EQ(cmd.RunAsTestCommand(), OHOS::ERR_INVALID_VALUE);
118 }
119 
120 /**
121  * @tc.number: Ability_Command_Module_Test_0300
122  * @tc.name: ExecCommand
123  * @tc.desc: Verify the "aa test -hhhhhhhhhhhhhhhhhhhhhh" command.
124  */
125 HWTEST_F(AbilityCommandModuleTest, Ability_Command_Module_Test_0300, Function | MediumTest | Level1)
126 {
127     TAG_LOGI(AAFwkTag::TEST, "Ability_Command_Module_Test_0300 is called");
128     char* argv[] = {
129         (char*)TOOL_NAME.c_str(),
130         (char*)cmd_.c_str(),
131         (char*)"-hhhhhhhhhhhhhhhhhhhhhh",
132     };
133     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
134 
135     AbilityManagerShellCommand cmd(argc, argv);
136     EXPECT_EQ(cmd.RunAsTestCommand(), OHOS::ERR_INVALID_VALUE);
137 }
138 
139 /**
140  * @tc.number: Ability_Command_Module_Test_0400
141  * @tc.name: ExecCommand
142  * @tc.desc: Verify the "aa test -help -hhhhhhhhhhhhhhhhhhhhhh" command.
143  */
144 HWTEST_F(AbilityCommandModuleTest, Ability_Command_Module_Test_0400, Function | MediumTest | Level1)
145 {
146     TAG_LOGI(AAFwkTag::TEST, "Ability_Command_Module_Test_0400 is called");
147     char* argv[] = {
148         (char*)TOOL_NAME.c_str(),
149         (char*)cmd_.c_str(),
150         (char*)"--help",
151         (char*)"-hhhhhhhhhhhhhhhhhhhhhh",
152     };
153     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
154 
155     AbilityManagerShellCommand cmd(argc, argv);
156     EXPECT_EQ(cmd.RunAsTestCommand(), OHOS::ERR_OK);
157 }
158 
159 /**
160  * @tc.number: Ability_Command_Module_Test_0500
161  * @tc.name: ExecCommand
162  * @tc.desc: Verify the "aa test -hhhhhhhhhhhhhhhhhhhhhh -help" command.
163  */
164 HWTEST_F(AbilityCommandModuleTest, Ability_Command_Module_Test_0500, Function | MediumTest | Level1)
165 {
166     TAG_LOGI(AAFwkTag::TEST, "Ability_Command_Module_Test_0500 is called");
167     char* argv[] = {
168         (char*)TOOL_NAME.c_str(),
169         (char*)cmd_.c_str(),
170         (char*)"-hhhhhhhhhhhhhhhhhhhhhh",
171         (char*)"-help",
172     };
173     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
174 
175     AbilityManagerShellCommand cmd(argc, argv);
176     EXPECT_EQ(cmd.RunAsTestCommand(), OHOS::ERR_INVALID_VALUE);
177 }
178 
179 /**
180  * @tc.number: Ability_Command_Module_Test_0600
181  * @tc.name: ExecCommand
182  * @tc.desc: Verify the "aa test -p com.example.myapplication -s unittst JSUserTestRunner -s class
183  *           ohos.acts.aafwk.ability.test.ConstructorTest#testDataAbilityOtherFunction0010 -h" command.
184  */
185 HWTEST_F(AbilityCommandModuleTest, Ability_Command_Module_Test_0600, Function | MediumTest | Level1)
186 {
187     TAG_LOGI(AAFwkTag::TEST, "Ability_Command_Module_Test_0600 is called");
188     char* argv[] = {
189         (char*)TOOL_NAME.c_str(),
190         (char*)cmd_.c_str(),
191         (char*)"-p",
192         (char*)STRING_BUNDLE_NAME.c_str(),
193         (char*)"-s",
194         (char*)UNITTEST.c_str(),
195         (char*)STRING_USER_TEST_RUNNER.c_str(),
196         (char*)"-s",
197         (char*)CLASS.c_str(),
198         (char*)STRING_CLASS_NAME.c_str(),
199         (char*)"-h",
200     };
201     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
202 
203     AbilityManagerShellCommand cmd(argc, argv);
204     EXPECT_EQ(cmd.RunAsTestCommand(), OHOS::ERR_INVALID_VALUE);
205 }
206 
207 /**
208  * @tc.number: Ability_Command_Module_Test_0700
209  * @tc.name: ExecCommand
210  * @tc.desc: Verify the "aa test -w 50" command.
211  */
212 HWTEST_F(AbilityCommandModuleTest, Ability_Command_Module_Test_0700, Function | MediumTest | Level1)
213 {
214     TAG_LOGI(AAFwkTag::TEST, "Ability_Command_Module_Test_0700 is called");
215     char* argv[] = {
216         (char*)TOOL_NAME.c_str(),
217         (char*)cmd_.c_str(),
218         (char*)"-w",
219         (char*)TIME.c_str(),
220     };
221     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
222 
223     AbilityManagerShellCommand cmd(argc, argv);
224     EXPECT_EQ(cmd.RunAsTestCommand(), OHOS::ERR_INVALID_VALUE);
225 }
226 
227 /**
228  * @tc.number: Ability_Command_Module_Test_0800
229  * @tc.name: ExecCommand
230  * @tc.desc: Verify the "aa test -p com.example.myapplication -s unittst JSUserTestRunner -s class
231  *           ohos.acts.aafwk.ability.test.ConstructorTest#testDataAbilityOtherFunction0010 -w 50 -z 50" command.
232  */
233 HWTEST_F(AbilityCommandModuleTest, Ability_Command_Module_Test_0800, Function | MediumTest | Level1)
234 {
235     TAG_LOGI(AAFwkTag::TEST, "Ability_Command_Module_Test_0800 is called");
236     char* argv[] = {
237         (char*)TOOL_NAME.c_str(),
238         (char*)cmd_.c_str(),
239         (char*)"-p",
240         (char*)STRING_BUNDLE_NAME.c_str(),
241         (char*)"-s",
242         (char*)UNITTEST.c_str(),
243         (char*)STRING_USER_TEST_RUNNER.c_str(),
244         (char*)"-s",
245         (char*)CLASS.c_str(),
246         (char*)STRING_CLASS_NAME.c_str(),
247         (char*)"-w",
248         (char*)TIME.c_str(),
249         (char*)"-z",
250         (char*)TIME.c_str(),
251     };
252     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
253 
254     AbilityManagerShellCommand cmd(argc, argv);
255     EXPECT_EQ(cmd.RunAsTestCommand(), OHOS::ERR_INVALID_VALUE);
256 }
257 
258 /**
259  * @tc.number: Ability_Command_Module_Test_0900
260  * @tc.name: ExecCommand
261  * @tc.desc: Verify the "aa test -p com.example.myapplication -s unittst JSUserTestRunner  -z 50 -s class
262  *           ohos.acts.aafwk.ability.test.ConstructorTest#testDataAbilityOtherFunction0010 -w 50" command.
263  */
264 HWTEST_F(AbilityCommandModuleTest, Ability_Command_Module_Test_0900, Function | MediumTest | Level1)
265 {
266     TAG_LOGI(AAFwkTag::TEST, "Ability_Command_Module_Test_0900 is called");
267     char* argv[] = {
268         (char*)TOOL_NAME.c_str(),
269         (char*)cmd_.c_str(),
270         (char*)"-p",
271         (char*)STRING_BUNDLE_NAME.c_str(),
272         (char*)"-s",
273         (char*)UNITTEST.c_str(),
274         (char*)STRING_USER_TEST_RUNNER.c_str(),
275         (char*)"-z",
276         (char*)TIME.c_str(),
277         (char*)"-s",
278         (char*)CLASS.c_str(),
279         (char*)STRING_CLASS_NAME.c_str(),
280         (char*)"-w",
281         (char*)TIME.c_str(),
282     };
283     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
284 
285     AbilityManagerShellCommand cmd(argc, argv);
286     EXPECT_EQ(cmd.RunAsTestCommand(), OHOS::ERR_INVALID_VALUE);
287 }
288