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