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