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