1 /*
2 * Copyright (c) 2021-2022 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 #include "account_command.h"
18 #include "account_log_wrapper.h"
19 #include "singleton.h"
20
21 using namespace testing::ext;
22 using namespace OHOS;
23 using namespace OHOS::AAFwk;
24 using namespace OHOS::AccountSA;
25
26 namespace {
27 const std::string HELP_MSG_UNKNOWN_OPTION = "error: unknown option.";
28
29 const std::string STRING_LOCAL_ACCOUNT_NAME = "local_account_name";
30 const std::string STRING_TYPE_NORMAL = "normal";
31 const std::string STRING_TYPE_ADMIN = "admin";
32 const std::string STRING_TYPE_GUEST = "guest";
33 const std::string STRING_TYPE_INVALID = "type_invalid";
34 } // namespace
35
36 class AccountCommandCreateTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp() override;
41 void TearDown() override;
42
43 std::string cmd_ = "create";
44 };
45
SetUpTestCase()46 void AccountCommandCreateTest::SetUpTestCase()
47 {}
48
TearDownTestCase()49 void AccountCommandCreateTest::TearDownTestCase()
50 {}
51
SetUp()52 void AccountCommandCreateTest::SetUp()
53 {
54 // reset optind to 0
55 optind = 0;
56
57 std::vector<OsAccountInfo> osAccountInfos;
58 OsAccount::GetInstance().QueryAllCreatedOsAccounts(osAccountInfos);
59 for (const auto &info : osAccountInfos) {
60 OsAccount::GetInstance().RemoveOsAccount(info.GetLocalId());
61 }
62 }
63
TearDown()64 void AccountCommandCreateTest::TearDown()
65 {}
66
67 /**
68 * @tc.name: Acm_Command_Create_0100
69 * @tc.desc: Verify the "acm create" command.
70 * @tc.type: FUNC
71 * @tc.require: SR000GGVFO
72 */
73 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0100, TestSize.Level1)
74 {
75 ACCOUNT_LOGI("Acm_Command_Create_0100");
76 char *argv[] = {
77 const_cast<char *>(TOOL_NAME.c_str()),
78 const_cast<char *>(cmd_.c_str()),
79 const_cast<char *>(""),
80 };
81 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
82
83 AccountCommand cmd(argc, argv);
84 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_CREATE);
85 }
86
87 /**
88 * @tc.name: Acm_Command_Create_0200
89 * @tc.desc: Verify the "acm create xxx" command.
90 * @tc.type: FUNC
91 * @tc.require: SR000GGVFO
92 */
93 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0200, TestSize.Level1)
94 {
95 ACCOUNT_LOGI("Acm_Command_Create_0200");
96 char *argv[] = {
97 const_cast<char *>(TOOL_NAME.c_str()),
98 const_cast<char *>(cmd_.c_str()),
99 const_cast<char *>("xxx"),
100 const_cast<char *>(""),
101 };
102 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
103
104 AccountCommand cmd(argc, argv);
105 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_CREATE);
106 }
107
108 /**
109 * @tc.name: Acm_Command_Create_0300
110 * @tc.desc: Verify the "acm create -x" command.
111 * @tc.type: FUNC
112 * @tc.require: SR000GGVFO
113 */
114 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0300, TestSize.Level1)
115 {
116 ACCOUNT_LOGI("Acm_Command_Create_0300");
117 char *argv[] = {
118 const_cast<char *>(TOOL_NAME.c_str()),
119 const_cast<char *>(cmd_.c_str()),
120 const_cast<char *>("-x"),
121 const_cast<char *>(""),
122 };
123 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
124
125 AccountCommand cmd(argc, argv);
126 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_CREATE);
127 }
128
129 /**
130 * @tc.name: Acm_Command_Create_0400
131 * @tc.desc: Verify the "acm create -xxx" command.
132 * @tc.type: FUNC
133 * @tc.require: SR000GGVFO
134 */
135 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0400, TestSize.Level1)
136 {
137 ACCOUNT_LOGI("Acm_Command_Create_0400");
138 char *argv[] = {
139 const_cast<char *>(TOOL_NAME.c_str()),
140 const_cast<char *>(cmd_.c_str()),
141 const_cast<char *>("-xxx"),
142 const_cast<char *>(""),
143 };
144 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
145
146 AccountCommand cmd(argc, argv);
147 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_CREATE);
148 }
149
150 /**
151 * @tc.name: Acm_Command_Create_0500
152 * @tc.desc: Verify the "acm create --x" command.
153 * @tc.type: FUNC
154 * @tc.require: SR000GGVFO
155 */
156 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0500, TestSize.Level1)
157 {
158 ACCOUNT_LOGI("Acm_Command_Create_0500");
159 char *argv[] = {
160 const_cast<char *>(TOOL_NAME.c_str()),
161 const_cast<char *>(cmd_.c_str()),
162 const_cast<char *>("--x"),
163 const_cast<char *>(""),
164 };
165 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
166
167 AccountCommand cmd(argc, argv);
168 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_CREATE);
169 }
170
171 /**
172 * @tc.name: Acm_Command_Create_0600
173 * @tc.desc: Verify the "acm create --xxx" command.
174 * @tc.type: FUNC
175 * @tc.require: SR000GGVFO
176 */
177 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0600, TestSize.Level1)
178 {
179 ACCOUNT_LOGI("Acm_Command_Create_0600");
180 char *argv[] = {
181 const_cast<char *>(TOOL_NAME.c_str()),
182 const_cast<char *>(cmd_.c_str()),
183 const_cast<char *>("--xxx"),
184 const_cast<char *>(""),
185 };
186 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
187
188 AccountCommand cmd(argc, argv);
189 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_CREATE);
190 }
191
192 /**
193 * @tc.name: Acm_Command_Create_0700
194 * @tc.desc: Verify the "acm create -h" command.
195 * @tc.type: FUNC
196 * @tc.require: SR000GGVFO
197 */
198 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0700, TestSize.Level1)
199 {
200 ACCOUNT_LOGI("Acm_Command_Create_0700");
201 char *argv[] = {
202 const_cast<char *>(TOOL_NAME.c_str()),
203 const_cast<char *>(cmd_.c_str()),
204 const_cast<char *>("-h"),
205 const_cast<char *>(""),
206 };
207 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
208
209 AccountCommand cmd(argc, argv);
210 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_CREATE);
211 }
212
213 /**
214 * @tc.name: Acm_Command_Create_0800
215 * @tc.desc: Verify the "acm create --help" command.
216 * @tc.type: FUNC
217 * @tc.require: SR000GGVFO
218 */
219 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0800, TestSize.Level1)
220 {
221 ACCOUNT_LOGI("Acm_Command_Create_0800");
222 char *argv[] = {
223 const_cast<char *>(TOOL_NAME.c_str()),
224 const_cast<char *>(cmd_.c_str()),
225 const_cast<char *>("--help"),
226 const_cast<char *>(""),
227 };
228 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
229
230 AccountCommand cmd(argc, argv);
231 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_CREATE);
232 }
233
234 /**
235 * @tc.name: Acm_Command_Create_0900
236 * @tc.desc: Verify the "acm create -n" command.
237 * @tc.type: FUNC
238 * @tc.require: SR000GGVFO
239 */
240 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0900, TestSize.Level1)
241 {
242 ACCOUNT_LOGI("Acm_Command_Create_0900");
243 char *argv[] = {
244 const_cast<char *>(TOOL_NAME.c_str()),
245 const_cast<char *>(cmd_.c_str()),
246 const_cast<char *>("-n"),
247 const_cast<char *>(""),
248 };
249 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
250
251 AccountCommand cmd(argc, argv);
252 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT + "\n" + HELP_MSG_CREATE);
253 }
254
255 /**
256 * @tc.name: Acm_Command_Create_1000
257 * @tc.desc: Verify the "acm create -n <local-account-name>" command.
258 * @tc.type: FUNC
259 * @tc.require: SR000GGVFO
260 */
261 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_1000, TestSize.Level1)
262 {
263 ACCOUNT_LOGI("Acm_Command_Create_1000");
264 char *argv[] = {
265 const_cast<char *>(TOOL_NAME.c_str()),
266 const_cast<char *>(cmd_.c_str()),
267 const_cast<char *>("-n"),
268 const_cast<char *>(STRING_LOCAL_ACCOUNT_NAME.c_str()),
269 const_cast<char *>(""),
270 };
271 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
272
273 AccountCommand cmd(argc, argv);
274 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_TYPE_OPTION + "\n" + HELP_MSG_CREATE);
275 }
276
277 /**
278 * @tc.name: Acm_Command_Create_1100
279 * @tc.desc: Verify the "acm create -t" command.
280 * @tc.type: FUNC
281 * @tc.require: SR000GGVFO
282 */
283 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_1100, TestSize.Level1)
284 {
285 ACCOUNT_LOGI("Acm_Command_Create_1100");
286 char *argv[] = {
287 const_cast<char *>(TOOL_NAME.c_str()),
288 const_cast<char *>(cmd_.c_str()),
289 const_cast<char *>("-t"),
290 const_cast<char *>(""),
291 };
292 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
293
294 AccountCommand cmd(argc, argv);
295 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT + "\n" + HELP_MSG_CREATE);
296 }
297
298 /**
299 * @tc.name: Acm_Command_Create_1200
300 * @tc.desc: Verify the "acm create -t <type>" command.
301 * @tc.type: FUNC
302 * @tc.require: SR000GGVFO
303 */
304 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_1200, TestSize.Level1)
305 {
306 ACCOUNT_LOGI("Acm_Command_Create_1200");
307 char *argv[] = {
308 const_cast<char *>(TOOL_NAME.c_str()),
309 const_cast<char *>(cmd_.c_str()),
310 const_cast<char *>("-t"),
311 const_cast<char *>(STRING_TYPE_INVALID.c_str()),
312 const_cast<char *>(""),
313 };
314 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
315
316 AccountCommand cmd(argc, argv);
317 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_INVALID_TYPE_ARGUMENT + "\n" + HELP_MSG_CREATE);
318 }
319
320 /**
321 * @tc.name: Acm_Command_Create_1300
322 * @tc.desc: Verify the "acm create -t <type>" command.
323 * @tc.type: FUNC
324 * @tc.require: SR000GGVFO
325 */
326 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_1300, TestSize.Level1)
327 {
328 ACCOUNT_LOGI("Acm_Command_Create_1300");
329 char *argv[] = {
330 const_cast<char *>(TOOL_NAME.c_str()),
331 const_cast<char *>(cmd_.c_str()),
332 const_cast<char *>("-t"),
333 const_cast<char *>(STRING_TYPE_NORMAL.c_str()),
334 const_cast<char *>(""),
335 };
336 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
337
338 AccountCommand cmd(argc, argv);
339 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_NAME_OPTION + "\n" + HELP_MSG_CREATE);
340 }
341
342 /**
343 * @tc.name: Acm_Command_Create_1400
344 * @tc.desc: Verify the "acm create -n <local-account-name> -t" command.
345 * @tc.type: FUNC
346 * @tc.require: SR000GGVFO
347 */
348 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_1400, TestSize.Level1)
349 {
350 ACCOUNT_LOGI("Acm_Command_Create_1400");
351 char *argv[] = {
352 const_cast<char *>(TOOL_NAME.c_str()),
353 const_cast<char *>(cmd_.c_str()),
354 const_cast<char *>("-n"),
355 const_cast<char *>(STRING_LOCAL_ACCOUNT_NAME.c_str()),
356 const_cast<char *>("-t"),
357 const_cast<char *>(""),
358 };
359 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
360
361 AccountCommand cmd(argc, argv);
362 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT + "\n" + HELP_MSG_CREATE);
363 }
364
365 /**
366 * @tc.name: Acm_Command_Create_1500
367 * @tc.desc: Verify the "acm create -n <local-account-name> -t <type>" command.
368 * @tc.type: FUNC
369 * @tc.require: SR000GGVFO
370 */
371 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_1500, TestSize.Level1)
372 {
373 ACCOUNT_LOGI("Acm_Command_Create_1500");
374 char *argv[] = {
375 const_cast<char *>(TOOL_NAME.c_str()),
376 const_cast<char *>(cmd_.c_str()),
377 const_cast<char *>("-n"),
378 const_cast<char *>(STRING_LOCAL_ACCOUNT_NAME.c_str()),
379 const_cast<char *>("-t"),
380 const_cast<char *>(STRING_TYPE_INVALID.c_str()),
381 const_cast<char *>(""),
382 };
383 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
384
385 AccountCommand cmd(argc, argv);
386 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_INVALID_TYPE_ARGUMENT + "\n" + HELP_MSG_CREATE);
387 }
388
389 /**
390 * @tc.name: Acm_Command_Create_1600
391 * @tc.desc: Verify the "acm create -n <local-account-name> -t <type>" command with normal type.
392 * @tc.type: FUNC
393 * @tc.require:
394 */
395 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_1600, TestSize.Level1)
396 {
397 ACCOUNT_LOGI("Acm_Command_Create_1600");
398 char *argv[] = {
399 const_cast<char *>(TOOL_NAME.c_str()),
400 const_cast<char *>(cmd_.c_str()),
401 const_cast<char *>("-n"),
402 const_cast<char *>(STRING_LOCAL_ACCOUNT_NAME.c_str()),
403 const_cast<char *>("-t"),
404 const_cast<char *>(STRING_TYPE_NORMAL.c_str()),
405 const_cast<char *>(""),
406 };
407 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
408
409 AccountCommand cmd(argc, argv);
410 EXPECT_EQ(cmd.ExecCommand(), STRING_CREATE_OS_ACCOUNT_OK + "\n");
411 }
412
413 /**
414 * @tc.name: Acm_Command_Create_1700
415 * @tc.desc: Verify the "acm create -n <local-account-name> -t <type>" command with admin type.
416 * @tc.type: FUNC
417 * @tc.require:
418 */
419 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_1700, TestSize.Level1)
420 {
421 ACCOUNT_LOGI("Acm_Command_Create_1700");
422 char *argv[] = {
423 const_cast<char *>(TOOL_NAME.c_str()),
424 const_cast<char *>(cmd_.c_str()),
425 const_cast<char *>("-n"),
426 const_cast<char *>(STRING_LOCAL_ACCOUNT_NAME.c_str()),
427 const_cast<char *>("-t"),
428 const_cast<char *>(STRING_TYPE_ADMIN.c_str()),
429 const_cast<char *>(""),
430 };
431 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
432
433 AccountCommand cmd(argc, argv);
434 EXPECT_EQ(cmd.ExecCommand(), STRING_CREATE_OS_ACCOUNT_OK + "\n");
435 }
436
437 /**
438 * @tc.name: Acm_Command_Create_1800
439 * @tc.desc: Verify the "acm create -n <local-account-name> -t <type>" command with admin type.
440 * @tc.type: FUNC
441 * @tc.require:
442 */
443 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_1800, TestSize.Level1)
444 {
445 ACCOUNT_LOGI("Acm_Command_Create_1800");
446 char *argv[] = {
447 const_cast<char *>(TOOL_NAME.c_str()),
448 const_cast<char *>(cmd_.c_str()),
449 const_cast<char *>("-n"),
450 const_cast<char *>(STRING_LOCAL_ACCOUNT_NAME.c_str()),
451 const_cast<char *>("-t"),
452 const_cast<char *>(STRING_TYPE_GUEST.c_str()),
453 const_cast<char *>(""),
454 };
455 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
456
457 AccountCommand cmd(argc, argv);
458 EXPECT_EQ(cmd.ExecCommand(), STRING_CREATE_OS_ACCOUNT_OK + "\n");
459 }
460
461 /**
462 * @tc.name: Acm_Command_Create_1900
463 * @tc.desc: Verify the "acm create -n <local-account-name> -t <type>" command, local-account-name is over max length.
464 * @tc.type: FUNC
465 * @tc.require:
466 */
467 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_1900, TestSize.Level1)
468 {
469 ACCOUNT_LOGI("Acm_Command_Create_1900");
470 std::string maxAccountName(1025, 's'); // 1025:over max length
471 char *argv[] = {
472 const_cast<char *>(TOOL_NAME.c_str()),
473 const_cast<char *>(cmd_.c_str()),
474 const_cast<char *>("-n"),
475 const_cast<char *>(maxAccountName.c_str()),
476 const_cast<char *>("-t"),
477 const_cast<char *>(STRING_TYPE_NORMAL.c_str()),
478 const_cast<char *>(""),
479 };
480 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
481
482 AccountCommand cmd(argc, argv);
483 EXPECT_EQ(cmd.ExecCommand(), STRING_CREATE_OS_ACCOUNT_NG + "\n");
484 }
485