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