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 
18 #include "account_command.h"
19 #include "account_log_wrapper.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 using namespace OHOS::AccountSA::Constants;
27 
28 namespace {
29 const std::string HELP_MSG_UNKNOWN_OPTION = "fail: unknown option.";
30 
31 const std::string STRING_LOCAL_ACCOUNT_ID = "1024";
32 const std::string STRING_CONSTRAINT = "constraint.bluetooth";
33 const std::string STRING_CONSTRAINT1 = "constraint.bluetooth,constraint.bluetooth.set";
34 }  // namespace
35 
36 class AccountCommandSetTest : 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_ = "set";
44 };
45 
SetUpTestCase()46 void AccountCommandSetTest::SetUpTestCase()
47 {}
48 
TearDownTestCase()49 void AccountCommandSetTest::TearDownTestCase()
50 {}
51 
SetUp(void)52 void AccountCommandSetTest::SetUp(void) __attribute__((no_sanitize("cfi")))
53 {
54     testing::UnitTest *test = testing::UnitTest::GetInstance();
55     ASSERT_NE(test, nullptr);
56     const testing::TestInfo *testinfo = test->current_test_info();
57     ASSERT_NE(testinfo, nullptr);
58     string testCaseName = string(testinfo->name());
59     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
60 
61     // reset optind to 0
62     optind = 0;
63 
64     std::vector<OsAccountInfo> osAccountInfos;
65     OsAccount::GetInstance().QueryAllCreatedOsAccounts(osAccountInfos);
66     for (const auto &info : osAccountInfos) {
67         if (info.GetLocalId() == START_USER_ID) {
68             continue;
69         }
70         ACCOUNT_LOGI("[SetUp] remove account %{public}d", info.GetLocalId());
71         OsAccount::GetInstance().RemoveOsAccount(info.GetLocalId());
72     }
73 }
74 
TearDown()75 void AccountCommandSetTest::TearDown()
76 {}
77 
78 /**
79  * @tc.name: Acm_Command_Set_0100
80  * @tc.desc: Verify the "acm set" command.
81  * @tc.type: FUNC
82  * @tc.require: SR000GGVFO
83  */
84 HWTEST_F(AccountCommandSetTest, Acm_Command_Set_0100, TestSize.Level1)
85 {
86     char *argv[] = {
87         const_cast<char *>(TOOL_NAME.c_str()),
88         const_cast<char *>(cmd_.c_str()),
89         const_cast<char *>(""),
90     };
91     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
92 
93     AccountCommand cmd(argc, argv);
94     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_SET);
95 }
96 
97 /**
98  * @tc.name: Acm_Command_Set_0200
99  * @tc.desc: Verify the "acm set xxx" command.
100  * @tc.type: FUNC
101  * @tc.require: SR000GGVFO
102  */
103 HWTEST_F(AccountCommandSetTest, Acm_Command_Set_0200, TestSize.Level1)
104 {
105     char *argv[] = {
106         const_cast<char *>(TOOL_NAME.c_str()),
107         const_cast<char *>(cmd_.c_str()),
108         const_cast<char *>("xxx"),
109         const_cast<char *>(""),
110     };
111     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
112 
113     AccountCommand cmd(argc, argv);
114     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_SET);
115 }
116 
117 /**
118  * @tc.name: Acm_Command_Set_0300
119  * @tc.desc: Verify the "acm set -x" command.
120  * @tc.type: FUNC
121  * @tc.require: SR000GGVFO
122  */
123 HWTEST_F(AccountCommandSetTest, Acm_Command_Set_0300, TestSize.Level1)
124 {
125     char *argv[] = {
126         const_cast<char *>(TOOL_NAME.c_str()),
127         const_cast<char *>(cmd_.c_str()),
128         const_cast<char *>("-x"),
129         const_cast<char *>(""),
130     };
131     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
132 
133     AccountCommand cmd(argc, argv);
134     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_SET);
135 }
136 
137 /**
138  * @tc.name: Acm_Command_Set_0400
139  * @tc.desc: Verify the "acm set -xxx" command.
140  * @tc.type: FUNC
141  * @tc.require: SR000GGVFO
142  */
143 HWTEST_F(AccountCommandSetTest, Acm_Command_Set_0400, TestSize.Level1)
144 {
145     char *argv[] = {
146         const_cast<char *>(TOOL_NAME.c_str()),
147         const_cast<char *>(cmd_.c_str()),
148         const_cast<char *>("-xxx"),
149         const_cast<char *>(""),
150     };
151     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
152 
153     AccountCommand cmd(argc, argv);
154     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_SET);
155 }
156 
157 /**
158  * @tc.name: Acm_Command_Set_0500
159  * @tc.desc: Verify the "acm set --x" command.
160  * @tc.type: FUNC
161  * @tc.require: SR000GGVFO
162  */
163 HWTEST_F(AccountCommandSetTest, Acm_Command_Set_0500, TestSize.Level1)
164 {
165     char *argv[] = {
166         const_cast<char *>(TOOL_NAME.c_str()),
167         const_cast<char *>(cmd_.c_str()),
168         const_cast<char *>("--x"),
169         const_cast<char *>(""),
170     };
171     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
172 
173     AccountCommand cmd(argc, argv);
174     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_SET);
175 }
176 
177 /**
178  * @tc.name: Acm_Command_Set_0600
179  * @tc.desc: Verify the "acm set --xxx" command.
180  * @tc.type: FUNC
181  * @tc.require: SR000GGVFO
182  */
183 HWTEST_F(AccountCommandSetTest, Acm_Command_Set_0600, TestSize.Level1)
184 {
185     char *argv[] = {
186         const_cast<char *>(TOOL_NAME.c_str()),
187         const_cast<char *>(cmd_.c_str()),
188         const_cast<char *>("--xxx"),
189         const_cast<char *>(""),
190     };
191     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
192 
193     AccountCommand cmd(argc, argv);
194     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_SET);
195 }
196 
197 /**
198  * @tc.name: Acm_Command_Set_0700
199  * @tc.desc: Verify the "acm set -h" command.
200  * @tc.type: FUNC
201  * @tc.require: SR000GGVFO
202  */
203 HWTEST_F(AccountCommandSetTest, Acm_Command_Set_0700, TestSize.Level1)
204 {
205     char *argv[] = {
206         const_cast<char *>(TOOL_NAME.c_str()),
207         const_cast<char *>(cmd_.c_str()),
208         const_cast<char *>("-h"),
209         const_cast<char *>(""),
210     };
211     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
212 
213     AccountCommand cmd(argc, argv);
214     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_SET);
215 }
216 
217 /**
218  * @tc.name: Acm_Command_Set_0800
219  * @tc.desc: Verify the "acm set --help" command.
220  * @tc.type: FUNC
221  * @tc.require: SR000GGVFO
222  */
223 HWTEST_F(AccountCommandSetTest, Acm_Command_Set_0800, TestSize.Level1)
224 {
225     char *argv[] = {
226         const_cast<char *>(TOOL_NAME.c_str()),
227         const_cast<char *>(cmd_.c_str()),
228         const_cast<char *>("--help"),
229         const_cast<char *>(""),
230     };
231     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
232 
233     AccountCommand cmd(argc, argv);
234     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_SET);
235 }
236 
237 /**
238  * @tc.name: Acm_Command_Set_0900
239  * @tc.desc: Verify the "acm set -i" command.
240  * @tc.type: FUNC
241  * @tc.require: SR000GGVFO
242  */
243 HWTEST_F(AccountCommandSetTest, Acm_Command_Set_0900, TestSize.Level1)
244 {
245     char *argv[] = {
246         const_cast<char *>(TOOL_NAME.c_str()),
247         const_cast<char *>(cmd_.c_str()),
248         const_cast<char *>("-i"),
249         const_cast<char *>(""),
250     };
251     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
252 
253     AccountCommand cmd(argc, argv);
254     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT + "\n" + HELP_MSG_SET);
255 }
256 
257 /**
258  * @tc.name: Acm_Command_Set_1000
259  * @tc.desc: Verify the "acm set -i <local-account-id>" command.
260  * @tc.type: FUNC
261  * @tc.require: SR000GGVFO
262  */
263 HWTEST_F(AccountCommandSetTest, Acm_Command_Set_1000, TestSize.Level1)
264 {
265     char *argv[] = {
266         const_cast<char *>(TOOL_NAME.c_str()),
267         const_cast<char *>(cmd_.c_str()),
268         const_cast<char *>("-i"),
269         const_cast<char *>(STRING_LOCAL_ACCOUNT_ID.c_str()),
270         const_cast<char *>(""),
271     };
272     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
273 
274     AccountCommand cmd(argc, argv);
275     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_CONSTRAINTS_OPTION + "\n" + HELP_MSG_SET);
276 }
277 
278 /**
279  * @tc.name: Acm_Command_Set_1100
280  * @tc.desc: Verify the "acm set -c" command.
281  * @tc.type: FUNC
282  * @tc.require: SR000GGVFO
283  */
284 HWTEST_F(AccountCommandSetTest, Acm_Command_Set_1100, TestSize.Level1)
285 {
286     char *argv[] = {
287         const_cast<char *>(TOOL_NAME.c_str()),
288         const_cast<char *>(cmd_.c_str()),
289         const_cast<char *>("-c"),
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_SET);
296 }
297 
298 /**
299  * @tc.name: Acm_Command_Set_1200
300  * @tc.desc: Verify the "acm set -c <constraints>" command.
301  * @tc.type: FUNC
302  * @tc.require: SR000GGVFO
303  */
304 HWTEST_F(AccountCommandSetTest, Acm_Command_Set_1200, TestSize.Level1)
305 {
306     char *argv[] = {
307         const_cast<char *>(TOOL_NAME.c_str()),
308         const_cast<char *>(cmd_.c_str()),
309         const_cast<char *>("-c"),
310         const_cast<char *>(STRING_CONSTRAINT.c_str()),
311         const_cast<char *>(""),
312     };
313     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
314 
315     AccountCommand cmd(argc, argv);
316     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_ID_OPTION + "\n" + HELP_MSG_SET);
317 }
318 
319 /**
320  * @tc.name: Acm_Command_Set_1300
321  * @tc.desc: Verify the "acm set -i <local-account-id> -c" command.
322  * @tc.type: FUNC
323  * @tc.require: SR000GGVFO
324  */
325 HWTEST_F(AccountCommandSetTest, Acm_Command_Set_1300, TestSize.Level1)
326 {
327     char *argv[] = {
328         const_cast<char *>(TOOL_NAME.c_str()),
329         const_cast<char *>(cmd_.c_str()),
330         const_cast<char *>("-i"),
331         const_cast<char *>(STRING_LOCAL_ACCOUNT_ID.c_str()),
332         const_cast<char *>("-c"),
333         const_cast<char *>(""),
334     };
335     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
336 
337     AccountCommand cmd(argc, argv);
338     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT + "\n" + HELP_MSG_SET);
339 }
340 
341 /**
342  * @tc.name: Acm_Command_Set_1400
343  * @tc.desc: Verify the "acm set -c <constraints> -i" command.
344  * @tc.type: FUNC
345  * @tc.require: SR000GGVFO
346  */
347 HWTEST_F(AccountCommandSetTest, Acm_Command_Set_1400, TestSize.Level1)
348 {
349     char *argv[] = {
350         const_cast<char *>(TOOL_NAME.c_str()),
351         const_cast<char *>(cmd_.c_str()),
352         const_cast<char *>("-c"),
353         const_cast<char *>(STRING_CONSTRAINT.c_str()),
354         const_cast<char *>("-i"),
355         const_cast<char *>(""),
356     };
357     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
358 
359     AccountCommand cmd(argc, argv);
360     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT + "\n" + HELP_MSG_SET);
361 }
362 
363 /**
364  * @tc.name: Acm_Command_Set_1500
365  * @tc.desc: Verify the "acm set -c <constraints> -i" command.
366  * @tc.type: FUNC
367  * @tc.require:
368  */
369 HWTEST_F(AccountCommandSetTest, Acm_Command_Set_1500, TestSize.Level1)
370 {
371     OsAccountInfo osAccountInfo;
372     // create an os account
373     EXPECT_EQ(ERR_OK, OsAccount::GetInstance().CreateOsAccount(TOOL_NAME, OsAccountType::NORMAL, osAccountInfo));
374 
375     std::string userId = std::to_string(osAccountInfo.GetLocalId());
376     char *argv[] = {
377         const_cast<char *>(TOOL_NAME.c_str()),
378         const_cast<char *>(cmd_.c_str()),
379         const_cast<char *>("-c"),
380         const_cast<char *>(STRING_CONSTRAINT1.c_str()),
381         const_cast<char *>("-i"),
382         const_cast<char *>(userId.c_str()),
383         const_cast<char *>(""),
384     };
385     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
386 
387     AccountCommand cmd(argc, argv);
388     EXPECT_EQ(cmd.ExecCommand(), STRING_SET_OS_ACCOUNT_CONSTRAINTS_OK + "\n");
389     OsAccount::GetInstance().RemoveOsAccount(osAccountInfo.GetLocalId());
390 }
391