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 #include "account_command.h"
18 #include "account_log_wrapper.h"
19
20 using namespace testing::ext;
21 using namespace OHOS;
22 using namespace OHOS::AAFwk;
23 using namespace OHOS::AccountSA;
24
25 namespace {
26 const std::string HELP_MSG_UNKNOWN_OPTION = "error: unknown option.";
27
28 const std::string STRING_LOCAL_ACCOUNT_NAME = "local_account_name";
29 const std::string STRING_TYPE = "normal";
30 const std::string STRING_TYPE_INVALID = "type_invalid";
31 } // namespace
32
33 class AccountCommandCreateTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp() override;
38 void TearDown() override;
39
40 std::string cmd_ = "create";
41 };
42
SetUpTestCase()43 void AccountCommandCreateTest::SetUpTestCase()
44 {}
45
TearDownTestCase()46 void AccountCommandCreateTest::TearDownTestCase()
47 {}
48
SetUp()49 void AccountCommandCreateTest::SetUp()
50 {
51 // reset optind to 0
52 optind = 0;
53 }
54
TearDown()55 void AccountCommandCreateTest::TearDown()
56 {}
57
58 /**
59 * @tc.name: Acm_Command_Create_0100
60 * @tc.desc: Verify the "acm create" command.
61 * @tc.type: FUNC
62 * @tc.require: SR000GGVFO
63 */
64 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0100, TestSize.Level1)
65 {
66 ACCOUNT_LOGI("Acm_Command_Create_0100");
67 char *argv[] = {
68 (char *)TOOL_NAME.c_str(),
69 (char *)cmd_.c_str(),
70 (char *)"",
71 };
72 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
73
74 AccountCommand cmd(argc, argv);
75 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_CREATE);
76 }
77
78 /**
79 * @tc.name: Acm_Command_Create_0200
80 * @tc.desc: Verify the "acm create xxx" command.
81 * @tc.type: FUNC
82 * @tc.require: SR000GGVFO
83 */
84 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0200, TestSize.Level1)
85 {
86 ACCOUNT_LOGI("Acm_Command_Create_0200");
87 char *argv[] = {
88 (char *)TOOL_NAME.c_str(),
89 (char *)cmd_.c_str(),
90 (char *)"xxx",
91 (char *)"",
92 };
93 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
94
95 AccountCommand cmd(argc, argv);
96 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_CREATE);
97 }
98
99 /**
100 * @tc.name: Acm_Command_Create_0300
101 * @tc.desc: Verify the "acm create -x" command.
102 * @tc.type: FUNC
103 * @tc.require: SR000GGVFO
104 */
105 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0300, TestSize.Level1)
106 {
107 ACCOUNT_LOGI("Acm_Command_Create_0300");
108 char *argv[] = {
109 (char *)TOOL_NAME.c_str(),
110 (char *)cmd_.c_str(),
111 (char *)"-x",
112 (char *)"",
113 };
114 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
115
116 AccountCommand cmd(argc, argv);
117 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_CREATE);
118 }
119
120 /**
121 * @tc.name: Acm_Command_Create_0400
122 * @tc.desc: Verify the "acm create -xxx" command.
123 * @tc.type: FUNC
124 * @tc.require: SR000GGVFO
125 */
126 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0400, TestSize.Level1)
127 {
128 ACCOUNT_LOGI("Acm_Command_Create_0400");
129 char *argv[] = {
130 (char *)TOOL_NAME.c_str(),
131 (char *)cmd_.c_str(),
132 (char *)"-xxx",
133 (char *)"",
134 };
135 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
136
137 AccountCommand cmd(argc, argv);
138 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_CREATE);
139 }
140
141 /**
142 * @tc.name: Acm_Command_Create_0500
143 * @tc.desc: Verify the "acm create --x" command.
144 * @tc.type: FUNC
145 * @tc.require: SR000GGVFO
146 */
147 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0500, TestSize.Level1)
148 {
149 ACCOUNT_LOGI("Acm_Command_Create_0500");
150 char *argv[] = {
151 (char *)TOOL_NAME.c_str(),
152 (char *)cmd_.c_str(),
153 (char *)"--x",
154 (char *)"",
155 };
156 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
157
158 AccountCommand cmd(argc, argv);
159 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_CREATE);
160 }
161
162 /**
163 * @tc.name: Acm_Command_Create_0600
164 * @tc.desc: Verify the "acm create --xxx" command.
165 * @tc.type: FUNC
166 * @tc.require: SR000GGVFO
167 */
168 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0600, TestSize.Level1)
169 {
170 ACCOUNT_LOGI("Acm_Command_Create_0600");
171 char *argv[] = {
172 (char *)TOOL_NAME.c_str(),
173 (char *)cmd_.c_str(),
174 (char *)"--xxx",
175 (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_CREATE);
181 }
182
183 /**
184 * @tc.name: Acm_Command_Create_0700
185 * @tc.desc: Verify the "acm create -h" command.
186 * @tc.type: FUNC
187 * @tc.require: SR000GGVFO
188 */
189 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0700, TestSize.Level1)
190 {
191 ACCOUNT_LOGI("Acm_Command_Create_0700");
192 char *argv[] = {
193 (char *)TOOL_NAME.c_str(),
194 (char *)cmd_.c_str(),
195 (char *)"-h",
196 (char *)"",
197 };
198 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
199
200 AccountCommand cmd(argc, argv);
201 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_CREATE);
202 }
203
204 /**
205 * @tc.name: Acm_Command_Create_0800
206 * @tc.desc: Verify the "acm create --help" command.
207 * @tc.type: FUNC
208 * @tc.require: SR000GGVFO
209 */
210 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0800, TestSize.Level1)
211 {
212 ACCOUNT_LOGI("Acm_Command_Create_0800");
213 char *argv[] = {
214 (char *)TOOL_NAME.c_str(),
215 (char *)cmd_.c_str(),
216 (char *)"--help",
217 (char *)"",
218 };
219 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
220
221 AccountCommand cmd(argc, argv);
222 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_CREATE);
223 }
224
225 /**
226 * @tc.name: Acm_Command_Create_0900
227 * @tc.desc: Verify the "acm create -n" command.
228 * @tc.type: FUNC
229 * @tc.require: SR000GGVFO
230 */
231 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_0900, TestSize.Level1)
232 {
233 ACCOUNT_LOGI("Acm_Command_Create_0900");
234 char *argv[] = {
235 (char *)TOOL_NAME.c_str(),
236 (char *)cmd_.c_str(),
237 (char *)"-n",
238 (char *)"",
239 };
240 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
241
242 AccountCommand cmd(argc, argv);
243 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT + "\n" + HELP_MSG_CREATE);
244 }
245
246 /**
247 * @tc.name: Acm_Command_Create_1000
248 * @tc.desc: Verify the "acm create -n <local-account-name>" command.
249 * @tc.type: FUNC
250 * @tc.require: SR000GGVFO
251 */
252 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_1000, TestSize.Level1)
253 {
254 ACCOUNT_LOGI("Acm_Command_Create_1000");
255 char *argv[] = {
256 (char *)TOOL_NAME.c_str(),
257 (char *)cmd_.c_str(),
258 (char *)"-n",
259 (char *)STRING_LOCAL_ACCOUNT_NAME.c_str(),
260 (char *)"",
261 };
262 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
263
264 AccountCommand cmd(argc, argv);
265 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_TYPE_OPTION + "\n" + HELP_MSG_CREATE);
266 }
267
268 /**
269 * @tc.name: Acm_Command_Create_1100
270 * @tc.desc: Verify the "acm create -t" command.
271 * @tc.type: FUNC
272 * @tc.require: SR000GGVFO
273 */
274 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_1100, TestSize.Level1)
275 {
276 ACCOUNT_LOGI("Acm_Command_Create_1100");
277 char *argv[] = {
278 (char *)TOOL_NAME.c_str(),
279 (char *)cmd_.c_str(),
280 (char *)"-t",
281 (char *)"",
282 };
283 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
284
285 AccountCommand cmd(argc, argv);
286 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT + "\n" + HELP_MSG_CREATE);
287 }
288
289 /**
290 * @tc.name: Acm_Command_Create_1200
291 * @tc.desc: Verify the "acm create -t <type>" command.
292 * @tc.type: FUNC
293 * @tc.require: SR000GGVFO
294 */
295 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_1200, TestSize.Level1)
296 {
297 ACCOUNT_LOGI("Acm_Command_Create_1200");
298 char *argv[] = {
299 (char *)TOOL_NAME.c_str(),
300 (char *)cmd_.c_str(),
301 (char *)"-t",
302 (char *)STRING_TYPE_INVALID.c_str(),
303 (char *)"",
304 };
305 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
306
307 AccountCommand cmd(argc, argv);
308 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_INVALID_TYPE_ARGUMENT + "\n" + HELP_MSG_CREATE);
309 }
310
311 /**
312 * @tc.name: Acm_Command_Create_1300
313 * @tc.desc: Verify the "acm create -t <type>" command.
314 * @tc.type: FUNC
315 * @tc.require: SR000GGVFO
316 */
317 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_1300, TestSize.Level1)
318 {
319 ACCOUNT_LOGI("Acm_Command_Create_1300");
320 char *argv[] = {
321 (char *)TOOL_NAME.c_str(),
322 (char *)cmd_.c_str(),
323 (char *)"-t",
324 (char *)STRING_TYPE.c_str(),
325 (char *)"",
326 };
327 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
328
329 AccountCommand cmd(argc, argv);
330 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_NAME_OPTION + "\n" + HELP_MSG_CREATE);
331 }
332
333 /**
334 * @tc.name: Acm_Command_Create_1400
335 * @tc.desc: Verify the "acm create -n <local-account-name> -t" command.
336 * @tc.type: FUNC
337 * @tc.require: SR000GGVFO
338 */
339 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_1400, TestSize.Level1)
340 {
341 ACCOUNT_LOGI("Acm_Command_Create_1400");
342 char *argv[] = {
343 (char *)TOOL_NAME.c_str(),
344 (char *)cmd_.c_str(),
345 (char *)"-n",
346 (char *)STRING_LOCAL_ACCOUNT_NAME.c_str(),
347 (char *)"-t",
348 (char *)"",
349 };
350 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
351
352 AccountCommand cmd(argc, argv);
353 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT + "\n" + HELP_MSG_CREATE);
354 }
355
356 /**
357 * @tc.name: Acm_Command_Create_1500
358 * @tc.desc: Verify the "acm create -n <local-account-name> -t <type>" command.
359 * @tc.type: FUNC
360 * @tc.require: SR000GGVFO
361 */
362 HWTEST_F(AccountCommandCreateTest, Acm_Command_Create_1500, TestSize.Level1)
363 {
364 ACCOUNT_LOGI("Acm_Command_Create_1500");
365 char *argv[] = {
366 (char *)TOOL_NAME.c_str(),
367 (char *)cmd_.c_str(),
368 (char *)"-n",
369 (char *)STRING_LOCAL_ACCOUNT_NAME.c_str(),
370 (char *)"-t",
371 (char *)STRING_TYPE_INVALID.c_str(),
372 (char *)"",
373 };
374 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
375
376 AccountCommand cmd(argc, argv);
377 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_INVALID_TYPE_ARGUMENT + "\n" + HELP_MSG_CREATE);
378 }
379