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<IAbilityManager>(new MockAbilityManagerStub());
70 
71     // set the mock stub
72     auto managerClientPtr = AbilityManagerClient::GetInstance();
73     managerClientPtr->proxy_ = 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_DUMPSYS);
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_DUMPSYS);
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(), "fail: unknown option.\n" + HELP_MSG_DUMPSYS);
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(), "fail: unknown option.\n" + HELP_MSG_DUMPSYS);
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(), "fail: unknown option.\n" + HELP_MSG_DUMPSYS);
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(), "fail: unknown option.\n" + HELP_MSG_DUMPSYS);
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_DUMPSYS);
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_DUMPSYS);
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