• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_STACK_NUMBER = "1024";
33 const std::string STRING_MISSION_NUMBER = "2048";
34 }  // namespace
35 
36 class AaCommandDumpTest : public ::testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp() override;
41     void TearDown() override;
42 
43     void MakeMockObjects() const;
44 
45     std::string cmd_ = "dump";
46 };
47 
SetUpTestCase()48 void AaCommandDumpTest::SetUpTestCase()
49 {}
50 
TearDownTestCase()51 void AaCommandDumpTest::TearDownTestCase()
52 {}
53 
SetUp()54 void AaCommandDumpTest::SetUp()
55 {
56     // reset optind to 0
57     optind = 0;
58 
59     // make mock objects
60     MakeMockObjects();
61 }
62 
TearDown()63 void AaCommandDumpTest::TearDown()
64 {}
65 
MakeMockObjects() const66 void AaCommandDumpTest::MakeMockObjects() const
67 {
68     // mock a stub
69     auto managerStubPtr = sptr<IRemoteObject>(new MockAbilityManagerStub());
70 
71     // set the mock stub
72     auto managerClientPtr = AbilityManagerClient::GetInstance();
73     managerClientPtr->remoteObject_ = managerStubPtr;
74 }
75 
76 /**
77  * @tc.number: Aa_Command_Dump_0100
78  * @tc.name: ExecCommand
79  * @tc.desc: Verify the "aa dump" command.
80  */
81 HWTEST_F(AaCommandDumpTest, Aa_Command_Dump_0100, Function | MediumTest | Level1)
82 {
83     char *argv[] = {
84         (char *)TOOL_NAME.c_str(),
85         (char *)cmd_.c_str(),
86         (char *)"",
87     };
88     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
89 
90     AbilityManagerShellCommand cmd(argc, argv);
91     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_DUMP);
92 }
93 
94 /**
95  * @tc.number: Aa_Command_Dump_0200
96  * @tc.name: ExecCommand
97  * @tc.desc: Verify the "aa dump xxx" command.
98  */
99 HWTEST_F(AaCommandDumpTest, Aa_Command_Dump_0200, Function | MediumTest | Level1)
100 {
101     char *argv[] = {
102         (char *)TOOL_NAME.c_str(),
103         (char *)cmd_.c_str(),
104         (char *)"xxx",
105         (char *)"",
106     };
107     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
108 
109     AbilityManagerShellCommand cmd(argc, argv);
110     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_DUMP);
111 }
112 
113 /**
114  * @tc.number: Aa_Command_Dump_0300
115  * @tc.name: ExecCommand
116  * @tc.desc: Verify the "aa dump -x" command.
117  */
118 HWTEST_F(AaCommandDumpTest, Aa_Command_Dump_0300, Function | MediumTest | Level1)
119 {
120     char *argv[] = {
121         (char *)TOOL_NAME.c_str(),
122         (char *)cmd_.c_str(),
123         (char *)"-x",
124         (char *)"",
125     };
126     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
127 
128     AbilityManagerShellCommand cmd(argc, argv);
129     EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_DUMP);
130 }
131 
132 /**
133  * @tc.number: Aa_Command_Dump_0400
134  * @tc.name: ExecCommand
135  * @tc.desc: Verify the "aa dump -xxx" command.
136  */
137 HWTEST_F(AaCommandDumpTest, Aa_Command_Dump_0400, Function | MediumTest | Level1)
138 {
139     char *argv[] = {
140         (char *)TOOL_NAME.c_str(),
141         (char *)cmd_.c_str(),
142         (char *)"-xxx",
143         (char *)"",
144     };
145     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
146 
147     AbilityManagerShellCommand cmd(argc, argv);
148     EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_DUMP);
149 }
150 
151 /**
152  * @tc.number: Aa_Command_Dump_0500
153  * @tc.name: ExecCommand
154  * @tc.desc: Verify the "aa dump --x" command.
155  */
156 HWTEST_F(AaCommandDumpTest, Aa_Command_Dump_0500, Function | MediumTest | Level1)
157 {
158     char *argv[] = {
159         (char *)TOOL_NAME.c_str(),
160         (char *)cmd_.c_str(),
161         (char *)"--x",
162         (char *)"",
163     };
164     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
165 
166     AbilityManagerShellCommand cmd(argc, argv);
167     EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_DUMP);
168 }
169 
170 /**
171  * @tc.number: Aa_Command_Dump_0600
172  * @tc.name: ExecCommand
173  * @tc.desc: Verify the "aa dump --xxx" command.
174  */
175 HWTEST_F(AaCommandDumpTest, Aa_Command_Dump_0600, Function | MediumTest | Level1)
176 {
177     char *argv[] = {
178         (char *)TOOL_NAME.c_str(),
179         (char *)cmd_.c_str(),
180         (char *)"--xxx",
181         (char *)"",
182     };
183     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
184 
185     AbilityManagerShellCommand cmd(argc, argv);
186     EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_DUMP);
187 }
188 
189 /**
190  * @tc.number: Aa_Command_Dump_0700
191  * @tc.name: ExecCommand
192  * @tc.desc: Verify the "aa dump -h" command.
193  */
194 HWTEST_F(AaCommandDumpTest, Aa_Command_Dump_0700, Function | MediumTest | Level1)
195 {
196     char *argv[] = {
197         (char *)TOOL_NAME.c_str(),
198         (char *)cmd_.c_str(),
199         (char *)"-h",
200         (char *)"",
201     };
202     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
203 
204     AbilityManagerShellCommand cmd(argc, argv);
205     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DUMP);
206 }
207 
208 /**
209  * @tc.number: Aa_Command_Dump_0800
210  * @tc.name: ExecCommand
211  * @tc.desc: Verify the "aa dump --help" command.
212  */
213 HWTEST_F(AaCommandDumpTest, Aa_Command_Dump_0800, Function | MediumTest | Level1)
214 {
215     char *argv[] = {
216         (char *)TOOL_NAME.c_str(),
217         (char *)cmd_.c_str(),
218         (char *)"--help",
219         (char *)"",
220     };
221     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
222 
223     AbilityManagerShellCommand cmd(argc, argv);
224     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DUMP);
225 }
226 
227 /**
228  * @tc.number: Aa_Command_Dump_0900
229  * @tc.name: ExecCommand
230  * @tc.desc: Verify the "aa dump -a" command.
231  */
232 HWTEST_F(AaCommandDumpTest, Aa_Command_Dump_0900, Function | MediumTest | Level1)
233 {
234     char *argv[] = {
235         (char *)TOOL_NAME.c_str(),
236         (char *)cmd_.c_str(),
237         (char *)"-a",
238         (char *)"",
239     };
240     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
241 
242     AbilityManagerShellCommand cmd(argc, argv);
243     EXPECT_EQ(cmd.ExecCommand(), "");
244 }
245 
246 /**
247  * @tc.number: Aa_Command_Dump_1000
248  * @tc.name: ExecCommand
249  * @tc.desc: Verify the "aa dump --all" command.
250  */
251 HWTEST_F(AaCommandDumpTest, Aa_Command_Dump_1000, Function | MediumTest | Level1)
252 {
253     char *argv[] = {
254         (char *)TOOL_NAME.c_str(),
255         (char *)cmd_.c_str(),
256         (char *)"--all",
257         (char *)"",
258     };
259     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
260 
261     AbilityManagerShellCommand cmd(argc, argv);
262     EXPECT_EQ(cmd.ExecCommand(), "");
263 }
264 
265 /**
266  * @tc.number: Aa_Command_Dump_1100
267  * @tc.name: ExecCommand
268  * @tc.desc: Verify the "aa dump -s" command.
269  */
270 HWTEST_F(AaCommandDumpTest, Aa_Command_Dump_1100, Function | MediumTest | Level1)
271 {
272     char *argv[] = {
273         (char *)TOOL_NAME.c_str(),
274         (char *)cmd_.c_str(),
275         (char *)"-s",
276         (char *)"",
277     };
278     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
279 
280     AbilityManagerShellCommand cmd(argc, argv);
281     EXPECT_EQ(cmd.ExecCommand(), "error: option requires a value.\n" + HELP_MSG_DUMP);
282 }
283 
284 /**
285  * @tc.number: Aa_Command_Dump_1200
286  * @tc.name: ExecCommand
287  * @tc.desc: Verify the "aa dump -s <number>" command.
288  */
289 HWTEST_F(AaCommandDumpTest, Aa_Command_Dump_1200, Function | MediumTest | Level1)
290 {
291     char *argv[] = {
292         (char *)TOOL_NAME.c_str(),
293         (char *)cmd_.c_str(),
294         (char *)"-s",
295         (char *)STRING_STACK_NUMBER.c_str(),
296         (char *)"",
297     };
298     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
299 
300     AbilityManagerShellCommand cmd(argc, argv);
301     EXPECT_EQ(cmd.ExecCommand(), STRING_STACK_NUMBER + "\n");
302 }
303 
304 /**
305  * @tc.number: Aa_Command_Dump_1300
306  * @tc.name: ExecCommand
307  * @tc.desc: Verify the "aa dump -m" command.
308  */
309 HWTEST_F(AaCommandDumpTest, Aa_Command_Dump_1300, Function | MediumTest | Level1)
310 {
311     char *argv[] = {
312         (char *)TOOL_NAME.c_str(),
313         (char *)cmd_.c_str(),
314         (char *)"-m",
315         (char *)"",
316     };
317     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
318 
319     AbilityManagerShellCommand cmd(argc, argv);
320     EXPECT_EQ(cmd.ExecCommand(), "error: option requires a value.\n" + HELP_MSG_DUMP);
321 }
322 
323 /**
324  * @tc.number: Aa_Command_Dump_1400
325  * @tc.name: ExecCommand
326  * @tc.desc: Verify the "aa dump -m <number>" command.
327  */
328 HWTEST_F(AaCommandDumpTest, Aa_Command_Dump_1400, Function | MediumTest | Level1)
329 {
330     char *argv[] = {
331         (char *)TOOL_NAME.c_str(),
332         (char *)cmd_.c_str(),
333         (char *)"-m",
334         (char *)STRING_MISSION_NUMBER.c_str(),
335         (char *)"",
336     };
337     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
338 
339     AbilityManagerShellCommand cmd(argc, argv);
340     EXPECT_EQ(cmd.ExecCommand(), STRING_MISSION_NUMBER + "\n");
341 }
342