1 /*
2 * Copyright (c) 2021-2025 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 const int32_t START_USER_ID = 100;
32 } // namespace
33
34 class AccountCommandDeleteTest : 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_ = "delete";
42 };
43
SetUpTestCase()44 void AccountCommandDeleteTest::SetUpTestCase()
45 {
46 ASSERT_NE(GetAllAccountPermission(), 0);
47 }
48
TearDownTestCase()49 void AccountCommandDeleteTest::TearDownTestCase()
50 {}
51
SetUp(void)52 void AccountCommandDeleteTest::SetUp(void) __attribute__((no_sanitize("cfi")))
53 {
54 testing::UnitTest *test = testing::UnitTest::GetInstance();
55 ASSERT_NE(test, nullptr);
56 const testing::TestInfo *testinfo = test->current_test_info();
57 ASSERT_NE(testinfo, nullptr);
58 string testCaseName = string(testinfo->name());
59 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
60
61 // reset optind to 0
62 optind = 0;
63
64 std::vector<OsAccountInfo> osAccountInfos;
65 OsAccount::GetInstance().QueryAllCreatedOsAccounts(osAccountInfos);
66 for (const auto &info : osAccountInfos) {
67 if (info.GetLocalId() == START_USER_ID) {
68 continue;
69 }
70 ACCOUNT_LOGI("[SetUp] remove account %{public}d", info.GetLocalId());
71 OsAccount::GetInstance().RemoveOsAccount(info.GetLocalId());
72 }
73 }
74
TearDown()75 void AccountCommandDeleteTest::TearDown()
76 {}
77
78 /**
79 * @tc.name: Acm_Command_Delete_0100
80 * @tc.desc: Verify the "acm delete" command.
81 * @tc.type: FUNC
82 * @tc.require: SR000GGVFO
83 */
84 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0100, TestSize.Level1)
85 {
86 char *argv[] = {
87 const_cast<char *>(TOOL_NAME.c_str()),
88 const_cast<char *>(cmd_.c_str()),
89 const_cast<char *>(""),
90 };
91 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
92
93 AccountCommand cmd(argc, argv);
94 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_DELETE);
95 }
96
97 /**
98 * @tc.name: Acm_Command_Delete_0200
99 * @tc.desc: Verify the "acm delete xxx" command.
100 * @tc.type: FUNC
101 * @tc.require: SR000GGVFO
102 */
103 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0200, TestSize.Level1)
104 {
105 char *argv[] = {
106 const_cast<char *>(TOOL_NAME.c_str()),
107 const_cast<char *>(cmd_.c_str()),
108 const_cast<char *>("xxx"),
109 const_cast<char *>(""),
110 };
111 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
112
113 AccountCommand cmd(argc, argv);
114 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_DELETE);
115 }
116
117 /**
118 * @tc.name: Acm_Command_Delete_0300
119 * @tc.desc: Verify the "acm delete -x" command.
120 * @tc.type: FUNC
121 * @tc.require: SR000GGVFO
122 */
123 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0300, TestSize.Level1)
124 {
125 char *argv[] = {
126 const_cast<char *>(TOOL_NAME.c_str()),
127 const_cast<char *>(cmd_.c_str()),
128 const_cast<char *>("-x"),
129 const_cast<char *>(""),
130 };
131 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
132
133 AccountCommand cmd(argc, argv);
134 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_DELETE);
135 }
136
137 /**
138 * @tc.name: Acm_Command_Delete_0400
139 * @tc.desc: Verify the "acm delete -xxx" command.
140 * @tc.type: FUNC
141 * @tc.require: SR000GGVFO
142 */
143 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0400, TestSize.Level1)
144 {
145 char *argv[] = {
146 const_cast<char *>(TOOL_NAME.c_str()),
147 const_cast<char *>(cmd_.c_str()),
148 const_cast<char *>("-xxx"),
149 const_cast<char *>(""),
150 };
151 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
152
153 AccountCommand cmd(argc, argv);
154 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_DELETE);
155 }
156
157 /**
158 * @tc.name: Acm_Command_Delete_0500
159 * @tc.desc: Verify the "acm delete --x" command.
160 * @tc.type: FUNC
161 * @tc.require: SR000GGVFO
162 */
163 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0500, TestSize.Level1)
164 {
165 char *argv[] = {
166 const_cast<char *>(TOOL_NAME.c_str()),
167 const_cast<char *>(cmd_.c_str()),
168 const_cast<char *>("--x"),
169 const_cast<char *>(""),
170 };
171 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
172
173 AccountCommand cmd(argc, argv);
174 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_DELETE);
175 }
176
177 /**
178 * @tc.name: Acm_Command_Delete_0600
179 * @tc.desc: Verify the "acm delete --xxx" command.
180 * @tc.type: FUNC
181 * @tc.require: SR000GGVFO
182 */
183 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0600, TestSize.Level1)
184 {
185 char *argv[] = {
186 const_cast<char *>(TOOL_NAME.c_str()),
187 const_cast<char *>(cmd_.c_str()),
188 const_cast<char *>("--xxx"),
189 const_cast<char *>(""),
190 };
191 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
192
193 AccountCommand cmd(argc, argv);
194 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_DELETE);
195 }
196
197 /**
198 * @tc.name: Acm_Command_Delete_0700
199 * @tc.desc: Verify the "acm delete -h" command.
200 * @tc.type: FUNC
201 * @tc.require: SR000GGVFO
202 */
203 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0700, TestSize.Level1)
204 {
205 char *argv[] = {
206 const_cast<char *>(TOOL_NAME.c_str()),
207 const_cast<char *>(cmd_.c_str()),
208 const_cast<char *>("-h"),
209 const_cast<char *>(""),
210 };
211 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
212
213 AccountCommand cmd(argc, argv);
214 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DELETE);
215 }
216
217 /**
218 * @tc.name: Acm_Command_Delete_0800
219 * @tc.desc: Verify the "acm delete --help" command.
220 * @tc.type: FUNC
221 * @tc.require: SR000GGVFO
222 */
223 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0800, TestSize.Level1)
224 {
225 char *argv[] = {
226 const_cast<char *>(TOOL_NAME.c_str()),
227 const_cast<char *>(cmd_.c_str()),
228 const_cast<char *>("--help"),
229 const_cast<char *>(""),
230 };
231 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
232
233 AccountCommand cmd(argc, argv);
234 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DELETE);
235 }
236
237 /**
238 * @tc.name: Acm_Command_Delete_0900
239 * @tc.desc: Verify the "acm delete -i" command.
240 * @tc.type: FUNC
241 * @tc.require: SR000GGVFO
242 */
243 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_0900, TestSize.Level1)
244 {
245 char *argv[] = {
246 const_cast<char *>(TOOL_NAME.c_str()),
247 const_cast<char *>(cmd_.c_str()),
248 const_cast<char *>("-i"),
249 const_cast<char *>(""),
250 };
251 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
252
253 AccountCommand cmd(argc, argv);
254 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT + "\n" + HELP_MSG_DELETE);
255 }
256
257 /**
258 * @tc.name: Acm_Command_Delete_1000
259 * @tc.desc: Verify the "acm delete -i" command with invalid userId.
260 * @tc.type: FUNC
261 * @tc.require:
262 */
263 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_1000, TestSize.Level1)
264 {
265 std::string userId = "88";
266 char *argv[] = {
267 const_cast<char *>(TOOL_NAME.c_str()),
268 const_cast<char *>(cmd_.c_str()),
269 const_cast<char *>("-i"),
270 const_cast<char *>(userId.c_str()),
271 const_cast<char *>(""),
272 };
273 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
274
275 AccountCommand cmd(argc, argv);
276 EXPECT_EQ(cmd.ExecCommand(), STRING_DELETE_OS_ACCOUNT_NG + "\n");
277 }
278
279 /**
280 * @tc.name: Acm_Command_Delete_1100
281 * @tc.desc: Verify the "acm delete -i" command with valid userId.
282 * @tc.type: FUNC
283 * @tc.require:
284 */
285 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_1100, TestSize.Level1)
286 {
287 OsAccountInfo osAccountInfo;
288 // create an os account
289 EXPECT_EQ(ERR_OK, OsAccount::GetInstance().CreateOsAccount(TOOL_NAME, OsAccountType::NORMAL, osAccountInfo));
290
291 std::string userId = std::to_string(osAccountInfo.GetLocalId());
292 char *argv[] = {
293 const_cast<char *>(TOOL_NAME.c_str()),
294 const_cast<char *>(cmd_.c_str()),
295 const_cast<char *>("-i"),
296 const_cast<char *>(userId.c_str()),
297 const_cast<char *>(""),
298 };
299 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
300
301 AccountCommand cmd(argc, argv);
302 EXPECT_EQ(cmd.ExecCommand(), STRING_DELETE_OS_ACCOUNT_OK + "\n");
303 }
304
305 /**
306 * @tc.name: Acm_Command_Delete_1200
307 * @tc.desc: Verify the "acm delete -i 0" command with valid userId.
308 * @tc.type: FUNC
309 * @tc.require:
310 */
311 HWTEST_F(AccountCommandDeleteTest, Acm_Command_Delete_1200, TestSize.Level1)
312 {
313 char *argv[] = {
314 const_cast<char *>(TOOL_NAME.c_str()),
315 const_cast<char *>(cmd_.c_str()),
316 const_cast<char *>("-i"),
317 const_cast<char *>("0"),
318 const_cast<char *>(""),
319 };
320 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
321
322 AccountCommand cmd(argc, argv);
323 EXPECT_EQ(cmd.ExecCommand(), STRING_DELETE_OS_ACCOUNT_NG + "\n");
324 }