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