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 #define private public
19 #include "account_command.h"
20 #undef private
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
28 namespace {
29 const std::string HELP_MSG_UNKNOWN_OPTION = "error: unknown option.";
30 } // namespace
31 class AccountCommandTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp() override;
36 void TearDown() override;
37 std::string cmd_ = "stop";
38 };
39
SetUpTestCase()40 void AccountCommandTest::SetUpTestCase()
41 {}
42
TearDownTestCase()43 void AccountCommandTest::TearDownTestCase()
44 {}
45
SetUp()46 void AccountCommandTest::SetUp()
47 {
48 // reset optind to 0
49 optind = 0;
50 }
51
TearDown()52 void AccountCommandTest::TearDown()
53 {}
54
55 /**
56 * @tc.name: Acm_Command_0100
57 * @tc.desc: Verify the "acm" command.
58 * @tc.type: FUNC
59 * @tc.require: SR000GGVFO
60 */
61 HWTEST_F(AccountCommandTest, Acm_Command_0100, TestSize.Level1)
62 {
63 char *argv[] = {
64 const_cast<char *>(TOOL_NAME.c_str()),
65 const_cast<char *>(""),
66 };
67 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
68
69 AccountCommand cmd(argc, argv);
70 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG);
71 }
72
73 /**
74 * @tc.name: Acm_Command_0200
75 * @tc.desc: Verify the "acm xxx" command.
76 * @tc.type: FUNC
77 * @tc.require: SR000GGVFO
78 */
79 HWTEST_F(AccountCommandTest, Acm_Command_0200, TestSize.Level1)
80 {
81 char *argv[] = {
82 const_cast<char *>(TOOL_NAME.c_str()),
83 const_cast<char *>("xxx"),
84 const_cast<char *>(""),
85 };
86 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
87
88 AccountCommand cmd(argc, argv);
89 EXPECT_EQ(cmd.ExecCommand(), cmd.GetCommandErrorMsg() + HELP_MSG);
90 }
91
92 /**
93 * @tc.name: Acm_Command_0300
94 * @tc.desc: Verify the "acm -xxx" command.
95 * @tc.type: FUNC
96 * @tc.require: SR000GGVFO
97 */
98 HWTEST_F(AccountCommandTest, Acm_Command_0300, TestSize.Level1)
99 {
100 char *argv[] = {
101 const_cast<char *>(TOOL_NAME.c_str()),
102 const_cast<char *>("-xxx"),
103 const_cast<char *>(""),
104 };
105 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
106
107 AccountCommand cmd(argc, argv);
108 EXPECT_EQ(cmd.ExecCommand(), cmd.GetCommandErrorMsg() + HELP_MSG);
109 }
110
111 /**
112 * @tc.name: Acm_Command_0400
113 * @tc.desc: Verify the "acm --xxx" command.
114 * @tc.type: FUNC
115 * @tc.require: SR000GGVFO
116 */
117 HWTEST_F(AccountCommandTest, Acm_Command_0400, TestSize.Level1)
118 {
119 char *argv[] = {
120 const_cast<char *>(TOOL_NAME.c_str()),
121 const_cast<char *>("--xxx"),
122 const_cast<char *>(""),
123 };
124 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
125
126 AccountCommand cmd(argc, argv);
127 EXPECT_EQ(cmd.ExecCommand(), cmd.GetCommandErrorMsg() + HELP_MSG);
128 }
129
130 /**
131 * @tc.name: Acm_Command_0500
132 * @tc.desc: Verify the "acm help" command.
133 * @tc.type: FUNC
134 * @tc.require: SR000GGVFO
135 */
136 HWTEST_F(AccountCommandTest, Acm_Command_0500, TestSize.Level1)
137 {
138 char *argv[] = {
139 const_cast<char *>(TOOL_NAME.c_str()),
140 const_cast<char *>("help"),
141 const_cast<char *>(""),
142 };
143 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
144
145 AccountCommand cmd(argc, argv);
146 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG);
147 }
148
149 #ifdef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
150 /**
151 * @tc.name: Acm_Command_0600
152 * @tc.desc: Verify the "acm stop" command.
153 * @tc.type: FUNC
154 * @tc.require:
155 */
156 HWTEST_F(AccountCommandTest, Acm_Command_0600, TestSize.Level1)
157 {
158 char *argv[] = {
159 const_cast<char *>(TOOL_NAME.c_str()),
160 const_cast<char *>(cmd_.c_str()),
161 const_cast<char *>(""),
162 };
163 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
164
165 AccountCommand cmd(argc, argv);
166 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_STOP);
167 }
168
169 /**
170 * @tc.name: Acm_Command_0700
171 * @tc.desc: Verify the "acm stop xxx" command.
172 * @tc.type: FUNC
173 * @tc.require:
174 */
175 HWTEST_F(AccountCommandTest, Acm_Command_0700, TestSize.Level1)
176 {
177 char *argv[] = {
178 const_cast<char *>(TOOL_NAME.c_str()),
179 const_cast<char *>(cmd_.c_str()),
180 const_cast<char *>("xxx"),
181 const_cast<char *>(""),
182 };
183 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
184
185 AccountCommand cmd(argc, argv);
186 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_STOP);
187 }
188
189 /**
190 * @tc.name: Acm_Command_0800
191 * @tc.desc: Verify the "acm stop -x" command.
192 * @tc.type: FUNC
193 * @tc.require:
194 */
195 HWTEST_F(AccountCommandTest, Acm_Command_0800, TestSize.Level1)
196 {
197 char *argv[] = {
198 const_cast<char *>(TOOL_NAME.c_str()),
199 const_cast<char *>(cmd_.c_str()),
200 const_cast<char *>("-x"),
201 const_cast<char *>(""),
202 };
203 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
204
205 AccountCommand cmd(argc, argv);
206 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_STOP);
207 }
208
209 /**
210 * @tc.name: Acm_Command_0900
211 * @tc.desc: Verify the "acm stop -xxx" command.
212 * @tc.type: FUNC
213 * @tc.require:
214 */
215 HWTEST_F(AccountCommandTest, Acm_Command_0900, TestSize.Level1)
216 {
217 char *argv[] = {
218 const_cast<char *>(TOOL_NAME.c_str()),
219 const_cast<char *>(cmd_.c_str()),
220 const_cast<char *>("-xxx"),
221 const_cast<char *>(""),
222 };
223 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
224
225 AccountCommand cmd(argc, argv);
226 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_STOP);
227 }
228
229 /**
230 * @tc.name: Acm_Command_1000
231 * @tc.desc: Verify the "acm stop --x" command.
232 * @tc.type: FUNC
233 * @tc.require:
234 */
235 HWTEST_F(AccountCommandTest, Acm_Command_1000, TestSize.Level1)
236 {
237 char *argv[] = {
238 const_cast<char *>(TOOL_NAME.c_str()),
239 const_cast<char *>(cmd_.c_str()),
240 const_cast<char *>("--x"),
241 const_cast<char *>(""),
242 };
243 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
244
245 AccountCommand cmd(argc, argv);
246 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_STOP);
247 }
248
249 /**
250 * @tc.name: Acm_Command_1100
251 * @tc.desc: Verify the "acm stop --xxx" command.
252 * @tc.type: FUNC
253 * @tc.require:
254 */
255 HWTEST_F(AccountCommandTest, Acm_Command_1100, TestSize.Level1)
256 {
257 char *argv[] = {
258 const_cast<char *>(TOOL_NAME.c_str()),
259 const_cast<char *>(cmd_.c_str()),
260 const_cast<char *>("--xxx"),
261 const_cast<char *>(""),
262 };
263 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
264
265 AccountCommand cmd(argc, argv);
266 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_STOP);
267 }
268
269 /**
270 * @tc.name: Acm_Command_1200
271 * @tc.desc: Verify the "acm stop -h" command.
272 * @tc.type: FUNC
273 * @tc.require:
274 */
275 HWTEST_F(AccountCommandTest, Acm_Command_1200, TestSize.Level1)
276 {
277 char *argv[] = {
278 const_cast<char *>(TOOL_NAME.c_str()),
279 const_cast<char *>(cmd_.c_str()),
280 const_cast<char *>("-h"),
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_STOP);
287 }
288
289 /**
290 * @tc.name: Acm_Command_1300
291 * @tc.desc: Verify the "acm stop -help" command.
292 * @tc.type: FUNC
293 * @tc.require:
294 */
295 HWTEST_F(AccountCommandTest, Acm_Command_1300, TestSize.Level1)
296 {
297 char *argv[] = {
298 const_cast<char *>(TOOL_NAME.c_str()),
299 const_cast<char *>(cmd_.c_str()),
300 const_cast<char *>("--help"),
301 const_cast<char *>(""),
302 };
303 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
304
305 AccountCommand cmd(argc, argv);
306 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_STOP);
307 }
308
309 /**
310 * @tc.name: Acm_Command_1400
311 * @tc.desc: Verify the "acm stop -i" command.
312 * @tc.type: FUNC
313 * @tc.require:
314 */
315 HWTEST_F(AccountCommandTest, Acm_Command_1400, TestSize.Level1)
316 {
317 char *argv[] = {
318 const_cast<char *>(TOOL_NAME.c_str()),
319 const_cast<char *>(cmd_.c_str()),
320 const_cast<char *>("-i"),
321 const_cast<char *>(""),
322 };
323 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
324
325 AccountCommand cmd(argc, argv);
326 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT + "\n" + HELP_MSG_STOP);
327 }
328
329 /**
330 * @tc.name: Acm_Command_1500
331 * @tc.desc: Verify the "acm stop -i" command.
332 * @tc.type: FUNC
333 * @tc.require:
334 */
335 HWTEST_F(AccountCommandTest, Acm_Command_1500, TestSize.Level1)
336 {
337 OsAccountInfo osAccountInfo;
338 // create an os account
339 EXPECT_EQ(ERR_OK, OsAccount::GetInstance().CreateOsAccount(TOOL_NAME, OsAccountType::NORMAL, osAccountInfo));
340
341 std::string userId = std::to_string(osAccountInfo.GetLocalId());
342 char *argv[] = {
343 const_cast<char *>(TOOL_NAME.c_str()),
344 const_cast<char *>(cmd_.c_str()),
345 const_cast<char *>("-i"),
346 const_cast<char *>(userId.c_str()),
347 const_cast<char *>(""),
348 };
349 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
350
351 AccountCommand cmd(argc, argv);
352 EXPECT_EQ(cmd.ExecCommand(), STRING_STOP_OS_ACCOUNT_OK + "\n");
353 EXPECT_EQ(ERR_OK, OsAccount::GetInstance().RemoveOsAccount(osAccountInfo.GetLocalId()));
354 }
355 #endif