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 } // namespace
29
30 class AccountCommandDeleteTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp() override;
35 void TearDown() override;
36
37 std::string cmd_ = "delete";
38 };
39
SetUpTestCase()40 void AccountCommandDeleteTest::SetUpTestCase()
41 {}
42
TearDownTestCase()43 void AccountCommandDeleteTest::TearDownTestCase()
44 {}
45
SetUp()46 void AccountCommandDeleteTest::SetUp()
47 {
48 // reset optind to 0
49 optind = 0;
50
51 std::vector<OsAccountInfo> osAccountInfos;
52 OsAccount::GetInstance().QueryAllCreatedOsAccounts(osAccountInfos);
53 for (const auto &info : osAccountInfos) {
54 OsAccount::GetInstance().RemoveOsAccount(info.GetLocalId());
55 }
56 }
57
TearDown()58 void AccountCommandDeleteTest::TearDown()
59 {}
60
61 /**
62 * @tc.name: Acm_Command_Delete_0100
63 * @tc.desc: Verify the "acm delete" command.
64 * @tc.type: FUNC
65 * @tc.require: SR000GGVFO
66 */
67 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0100, TestSize.Level1)
68 {
69 char *argv[] = {
70 const_cast<char *>(TOOL_NAME.c_str()),
71 const_cast<char *>(cmd_.c_str()),
72 const_cast<char *>(""),
73 };
74 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
75
76 AccountCommand cmd(argc, argv);
77 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_DELETE);
78 }
79
80 /**
81 * @tc.name: Acm_Command_Delete_0200
82 * @tc.desc: Verify the "acm delete xxx" command.
83 * @tc.type: FUNC
84 * @tc.require: SR000GGVFO
85 */
86 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0200, TestSize.Level1)
87 {
88 char *argv[] = {
89 const_cast<char *>(TOOL_NAME.c_str()),
90 const_cast<char *>(cmd_.c_str()),
91 const_cast<char *>("xxx"),
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_DELETE);
98 }
99
100 /**
101 * @tc.name: Acm_Command_Delete_0300
102 * @tc.desc: Verify the "acm delete -x" command.
103 * @tc.type: FUNC
104 * @tc.require: SR000GGVFO
105 */
106 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0300, 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 *>("-x"),
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_UNKNOWN_OPTION + "\n" + HELP_MSG_DELETE);
118 }
119
120 /**
121 * @tc.name: Acm_Command_Delete_0400
122 * @tc.desc: Verify the "acm delete -xxx" command.
123 * @tc.type: FUNC
124 * @tc.require: SR000GGVFO
125 */
126 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0400, 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 *>("-xxx"),
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_DELETE);
138 }
139
140 /**
141 * @tc.name: Acm_Command_Delete_0500
142 * @tc.desc: Verify the "acm delete --x" command.
143 * @tc.type: FUNC
144 * @tc.require: SR000GGVFO
145 */
146 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0500, 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 *>("--x"),
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_DELETE);
158 }
159
160 /**
161 * @tc.name: Acm_Command_Delete_0600
162 * @tc.desc: Verify the "acm delete --xxx" command.
163 * @tc.type: FUNC
164 * @tc.require: SR000GGVFO
165 */
166 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0600, 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 *>("--xxx"),
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_DELETE);
178 }
179
180 /**
181 * @tc.name: Acm_Command_Delete_0700
182 * @tc.desc: Verify the "acm delete -h" command.
183 * @tc.type: FUNC
184 * @tc.require: SR000GGVFO
185 */
186 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0700, 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 *>("-h"),
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_DELETE);
198 }
199
200 /**
201 * @tc.name: Acm_Command_Delete_0800
202 * @tc.desc: Verify the "acm delete --help" command.
203 * @tc.type: FUNC
204 * @tc.require: SR000GGVFO
205 */
206 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0800, 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 *>("--help"),
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_DELETE);
218 }
219
220 /**
221 * @tc.name: Acm_Command_Delete_0900
222 * @tc.desc: Verify the "acm delete -i" command.
223 * @tc.type: FUNC
224 * @tc.require: SR000GGVFO
225 */
226 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0900, 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 *>("-i"),
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_OPTION_REQUIRES_AN_ARGUMENT + "\n" + HELP_MSG_DELETE);
238 }
239
240 /**
241 * @tc.name: Acm_Command_Delete_1000
242 * @tc.desc: Verify the "acm delete -i" command with invalid userId.
243 * @tc.type: FUNC
244 * @tc.require:
245 */
246 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_1000, TestSize.Level1)
247 {
248 std::string userId = "88";
249 char *argv[] = {
250 const_cast<char *>(TOOL_NAME.c_str()),
251 const_cast<char *>(cmd_.c_str()),
252 const_cast<char *>("-i"),
253 const_cast<char *>(userId.c_str()),
254 const_cast<char *>(""),
255 };
256 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
257
258 AccountCommand cmd(argc, argv);
259 EXPECT_EQ(cmd.ExecCommand(), STRING_DELETE_OS_ACCOUNT_NG + "\n");
260 }
261
262 /**
263 * @tc.name: Acm_Command_Delete_1100
264 * @tc.desc: Verify the "acm delete -i" command with valid userId.
265 * @tc.type: FUNC
266 * @tc.require:
267 */
268 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_1100, TestSize.Level1)
269 {
270 OsAccountInfo osAccountInfo;
271 // create an os account
272 EXPECT_EQ(ERR_OK, OsAccount::GetInstance().CreateOsAccount(TOOL_NAME, OsAccountType::NORMAL, osAccountInfo));
273
274 std::string userId = std::to_string(osAccountInfo.GetLocalId());
275 char *argv[] = {
276 const_cast<char *>(TOOL_NAME.c_str()),
277 const_cast<char *>(cmd_.c_str()),
278 const_cast<char *>("-i"),
279 const_cast<char *>(userId.c_str()),
280 const_cast<char *>(""),
281 };
282 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
283
284 AccountCommand cmd(argc, argv);
285 EXPECT_EQ(cmd.ExecCommand(), STRING_DELETE_OS_ACCOUNT_OK + "\n");
286 }
287
288 /**
289 * @tc.name: Acm_Command_Delete_1200
290 * @tc.desc: Verify the "acm delete -i 0" command with valid userId.
291 * @tc.type: FUNC
292 * @tc.require:
293 */
294 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_1200, TestSize.Level1)
295 {
296 char *argv[] = {
297 const_cast<char *>(TOOL_NAME.c_str()),
298 const_cast<char *>(cmd_.c_str()),
299 const_cast<char *>("-i"),
300 const_cast<char *>("0"),
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(), STRING_DELETE_OS_ACCOUNT_NG + "\n");
307 }