1 /*
2 * Copyright (c) 2023 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 #define private public
20 #include "ability_command.h"
21 #undef protected
22 #undef private
23
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::AAFwk;
27
28 namespace {
29 const std::string STRING_BUNDLE_NAME = "bundleName";
30 const std::string EMPTY_BUNDLE_NAME = "";
31 } // namespace
32
33 class AaCommandAttachTest : public ::testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp() override;
38 void TearDown() override;
39 std::string attachCmd_ = "attach";
40 std::string detachCmd_ = "detach";
41 };
42
SetUpTestCase()43 void AaCommandAttachTest::SetUpTestCase()
44 {}
45
TearDownTestCase()46 void AaCommandAttachTest::TearDownTestCase()
47 {}
48
SetUp()49 void AaCommandAttachTest::SetUp()
50 {
51 // reset optind to 0
52 optind = 0;
53 }
54
TearDown()55 void AaCommandAttachTest::TearDown()
56 {}
57
58 /**
59 * @tc.number: Aa_Command_ParseBundleName_0100
60 * @tc.name: Parse bundleName from argv[]
61 * @tc.desc: Verify that attach command parse bundleName normally.
62 */
63 HWTEST_F(AaCommandAttachTest, ParseBundleName_0100, TestSize.Level1)
64 {
65 GTEST_LOG_(INFO) << "Aa_Command_ParseBundleName_0100";
66
67 char* argv[] = {
68 (char*)TOOL_NAME.c_str(),
69 (char*)detachCmd_.c_str(),
70 (char*)"-b",
71 (char*)STRING_BUNDLE_NAME.c_str(),
72 (char*)"",
73 };
74 int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
75
76 AbilityManagerShellCommand cmd(argc, argv);
77 std::string bundleName = "";
78 cmd.ParseBundleName(bundleName);
79 EXPECT_EQ(bundleName, STRING_BUNDLE_NAME);
80 }
81
82 /**
83 * @tc.number: Aa_Command_ParseBundleName_0200
84 * @tc.name: Parse bundleName from argv[]
85 * @tc.desc: Verify attach command parse bundleName normally.
86 */
87 HWTEST_F(AaCommandAttachTest, ParseBundleName_0200, TestSize.Level1)
88 {
89 GTEST_LOG_(INFO) << "Aa_Command_ParseBundleName_0100";
90
91 char* argv[] = {
92 (char*)TOOL_NAME.c_str(),
93 (char*)detachCmd_.c_str(),
94 (char*)"-b",
95 (char*)EMPTY_BUNDLE_NAME.c_str(),
96 (char*)"",
97 };
98 int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
99
100 AbilityManagerShellCommand cmd(argc, argv);
101 std::string bundleName = STRING_BUNDLE_NAME;
102 cmd.ParseBundleName(bundleName);
103 EXPECT_EQ(bundleName, EMPTY_BUNDLE_NAME);
104 }
105
106 /**
107 * @tc.number: Aa_Command_Attach_0100
108 * @tc.name: ExecCommand
109 * @tc.desc: Verify the "aa attach" command.
110 */
111 HWTEST_F(AaCommandAttachTest, Aa_Command_Attach_0100, TestSize.Level1)
112 {
113 GTEST_LOG_(INFO) << "Aa_Command_Attach_0100";
114
115 char* argv[] = {
116 (char*)TOOL_NAME.c_str(),
117 (char*)attachCmd_.c_str(),
118 (char*)"",
119 };
120 int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
121
122 AbilityManagerShellCommand cmd(argc, argv);
123 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_ATTACH_APP_DEBUG + "\n");
124 }
125
126 /**
127 * @tc.number: Aa_Command_Attach_0200
128 * @tc.name: ExecCommand
129 * @tc.desc: Verify the "aa attach xxx" command.
130 */
131 HWTEST_F(AaCommandAttachTest, Aa_Command_Attach_0200, TestSize.Level1)
132 {
133 GTEST_LOG_(INFO) << "Aa_Command_Attach_0200";
134
135 char* argv[] = {
136 (char*)TOOL_NAME.c_str(),
137 (char*)attachCmd_.c_str(),
138 (char*)"xxx",
139 (char*)"",
140 };
141 int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
142
143 AbilityManagerShellCommand cmd(argc, argv);
144 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_ATTACH_APP_DEBUG + "\n");
145 }
146
147 /**
148 * @tc.number: Aa_Command_Attach_0300
149 * @tc.name: ExecCommand
150 * @tc.desc: Verify the "aa attach -x" command.
151 */
152 HWTEST_F(AaCommandAttachTest, Aa_Command_Attach_0300, TestSize.Level1)
153 {
154 GTEST_LOG_(INFO) << "Aa_Command_Attach_0300";
155
156 char* argv[] = {
157 (char*)TOOL_NAME.c_str(),
158 (char*)attachCmd_.c_str(),
159 (char*)"-x",
160 (char*)"",
161 };
162 int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
163
164 AbilityManagerShellCommand cmd(argc, argv);
165 EXPECT_EQ(cmd.ExecCommand(), "fail: unknown option.\n" + HELP_MSG_ATTACH_APP_DEBUG + "\n");
166 }
167
168 /**
169 * @tc.number: Aa_Command_Attach_0400
170 * @tc.name: ExecCommand
171 * @tc.desc: Verify the "aa attach -h" command.
172 */
173 HWTEST_F(AaCommandAttachTest, Aa_Command_Attach_0400, TestSize.Level1)
174 {
175 GTEST_LOG_(INFO) << "Aa_Command_Attach_0400";
176
177 char* argv[] = {
178 (char*)TOOL_NAME.c_str(),
179 (char*)attachCmd_.c_str(),
180 (char*)"-h",
181 (char*)"",
182 };
183 int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
184
185 AbilityManagerShellCommand cmd(argc, argv);
186 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_ATTACH_APP_DEBUG + "\n");
187 }
188
189 /**
190 * @tc.number: Aa_Command_Attach_0500
191 * @tc.name: ExecCommand
192 * @tc.desc: Verify the "aa attach --help" command.
193 */
194 HWTEST_F(AaCommandAttachTest, Aa_Command_Attach_0500, TestSize.Level1)
195 {
196 GTEST_LOG_(INFO) << "Aa_Command_Attach_0500";
197
198 char* argv[] = {
199 (char*)TOOL_NAME.c_str(),
200 (char*)attachCmd_.c_str(),
201 (char*)"--help",
202 (char*)"",
203 };
204 int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
205
206 AbilityManagerShellCommand cmd(argc, argv);
207 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_ATTACH_APP_DEBUG + "\n");
208 }
209
210 /**
211 * @tc.number: Aa_Command_Attach_0600
212 * @tc.name: ExecCommand
213 * @tc.desc: Verify the "aa attach -b" command.
214 */
215 HWTEST_F(AaCommandAttachTest, Aa_Command_Attach_0600, TestSize.Level1)
216 {
217 GTEST_LOG_(INFO) << "Aa_Command_Attach_0600";
218
219 char* argv[] = {
220 (char*)TOOL_NAME.c_str(),
221 (char*)attachCmd_.c_str(),
222 (char*)"-b",
223 (char*)"",
224 };
225 int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
226
227 AbilityManagerShellCommand cmd(argc, argv);
228 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_ATTACH_APP_DEBUG + "\n");
229 }
230
231 /**
232 * @tc.number: Aa_Command_Attach_0700
233 * @tc.name: ExecCommand
234 * @tc.desc: Verify the "aa attach -b <bundle-name>" command.
235 */
236 HWTEST_F(AaCommandAttachTest, Aa_Command_Attach_0700, TestSize.Level1)
237 {
238 GTEST_LOG_(INFO) << "Aa_Command_Attach_0700";
239
240 char* argv[] = {
241 (char*)TOOL_NAME.c_str(),
242 (char*)attachCmd_.c_str(),
243 (char*)"-b",
244 (char*)STRING_BUNDLE_NAME.c_str(),
245 (char*)"",
246 };
247 int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
248
249 AbilityManagerShellCommand cmd(argc, argv);
250 EXPECT_EQ(cmd.ExecCommand(), STRING_ATTACH_APP_DEBUG_OK + "\n");
251 }
252
253 /**
254 * @tc.number: Aa_Command_Detach_0100
255 * @tc.name: ExecCommand
256 * @tc.desc: Verify the "aa detach" command.
257 */
258 HWTEST_F(AaCommandAttachTest, Aa_Command_Detach_0100, TestSize.Level1)
259 {
260 GTEST_LOG_(INFO) << "Aa_Command_Detach_0100";
261
262 char* argv[] = {
263 (char*)TOOL_NAME.c_str(),
264 (char*)detachCmd_.c_str(),
265 (char*)"",
266 };
267 int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
268
269 AbilityManagerShellCommand cmd(argc, argv);
270 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_DETACH_APP_DEBUG + "\n");
271 }
272
273 /**
274 * @tc.number: Aa_Command_Detach_0200
275 * @tc.name: ExecCommand
276 * @tc.desc: Verify the "aa detach xxx" command.
277 */
278 HWTEST_F(AaCommandAttachTest, Aa_Command_Detach_0200, TestSize.Level1)
279 {
280 GTEST_LOG_(INFO) << "Aa_Command_Detach_0200";
281
282 char* argv[] = {
283 (char*)TOOL_NAME.c_str(),
284 (char*)detachCmd_.c_str(),
285 (char*)"xxx",
286 (char*)"",
287 };
288 int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
289
290 AbilityManagerShellCommand cmd(argc, argv);
291 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_DETACH_APP_DEBUG + "\n");
292 }
293
294 /**
295 * @tc.number: Aa_Command_Detach_0300
296 * @tc.name: ExecCommand
297 * @tc.desc: Verify the "aa detach -x" command.
298 */
299 HWTEST_F(AaCommandAttachTest, Aa_Command_Detach_0300, TestSize.Level1)
300 {
301 GTEST_LOG_(INFO) << "Aa_Command_Detach_0300";
302
303 char* argv[] = {
304 (char*)TOOL_NAME.c_str(),
305 (char*)detachCmd_.c_str(),
306 (char*)"-x",
307 (char*)"",
308 };
309 int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
310
311 AbilityManagerShellCommand cmd(argc, argv);
312 EXPECT_EQ(cmd.ExecCommand(), "fail: unknown option.\n" + HELP_MSG_DETACH_APP_DEBUG + "\n");
313 }
314
315 /**
316 * @tc.number: Aa_Command_Detach_0400
317 * @tc.name: ExecCommand
318 * @tc.desc: Verify the "aa detach -h" command.
319 */
320 HWTEST_F(AaCommandAttachTest, Aa_Command_Detach_0400, TestSize.Level1)
321 {
322 GTEST_LOG_(INFO) << "Aa_Command_Detach_0400";
323
324 char* argv[] = {
325 (char*)TOOL_NAME.c_str(),
326 (char*)detachCmd_.c_str(),
327 (char*)"-h",
328 (char*)"",
329 };
330 int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
331
332 AbilityManagerShellCommand cmd(argc, argv);
333 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DETACH_APP_DEBUG + "\n");
334 }
335
336 /**
337 * @tc.number: Aa_Command_Detach_0500
338 * @tc.name: ExecCommand
339 * @tc.desc: Verify the "aa detach --help" command.
340 */
341 HWTEST_F(AaCommandAttachTest, Aa_Command_Detach_0500, TestSize.Level1)
342 {
343 GTEST_LOG_(INFO) << "Aa_Command_Detach_0500";
344
345 char* argv[] = {
346 (char*)TOOL_NAME.c_str(),
347 (char*)detachCmd_.c_str(),
348 (char*)"--help",
349 (char*)"",
350 };
351 int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
352
353 AbilityManagerShellCommand cmd(argc, argv);
354 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DETACH_APP_DEBUG + "\n");
355 }
356
357 /**
358 * @tc.number: Aa_Command_Detach_0600
359 * @tc.name: ExecCommand
360 * @tc.desc: Verify the "aa detach -b" command.
361 */
362 HWTEST_F(AaCommandAttachTest, Aa_Command_Detach_0600, TestSize.Level1)
363 {
364 GTEST_LOG_(INFO) << "Aa_Command_Detach_0600";
365
366 char* argv[] = {
367 (char*)TOOL_NAME.c_str(),
368 (char*)detachCmd_.c_str(),
369 (char*)"-b",
370 (char*)"",
371 };
372 int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
373
374 AbilityManagerShellCommand cmd(argc, argv);
375 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DETACH_APP_DEBUG + "\n");
376 }
377
378 /**
379 * @tc.number: Aa_Command_Detach_0700
380 * @tc.name: ExecCommand
381 * @tc.desc: Verify the "aa detach -b <bundle-name>" command.
382 */
383 HWTEST_F(AaCommandAttachTest, Aa_Command_Detach_0700, TestSize.Level1)
384 {
385 GTEST_LOG_(INFO) << "Aa_Command_Detach_0700";
386
387 char* argv[] = {
388 (char*)TOOL_NAME.c_str(),
389 (char*)detachCmd_.c_str(),
390 (char*)"-b",
391 (char*)STRING_BUNDLE_NAME.c_str(),
392 (char*)"",
393 };
394 int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
395
396 AbilityManagerShellCommand cmd(argc, argv);
397 EXPECT_EQ(cmd.ExecCommand(), STRING_DETACH_APP_DEBUG_OK + "\n");
398 }
399