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 "os_account_constants.h"
22 #include "singleton.h"
23
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::AAFwk;
27 using namespace OHOS::AccountSA;
28 using namespace OHOS::AccountSA::Constants;
29
30 namespace {
31 const std::string HELP_MSG_UNKNOWN_OPTION = "fail: unknown option.";
32 } // namespace
33
34 class AccountCommandDumpTest : 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_ = "dump";
42 };
43
SetUpTestCase()44 void AccountCommandDumpTest::SetUpTestCase()
45 {
46 ASSERT_NE(GetAllAccountPermission(), 0);
47 }
48
TearDownTestCase()49 void AccountCommandDumpTest::TearDownTestCase()
50 {}
51
SetUp(void)52 void AccountCommandDumpTest::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 AccountCommandDumpTest::TearDown()
76 {}
77
78 /**
79 * @tc.name: Acm_Command_Dump_0100
80 * @tc.desc: Verify the "acm dump" command.
81 * @tc.type: FUNC
82 * @tc.require: SR000GGVFO
83 */
84 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_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_DUMP);
95 }
96
97 /**
98 * @tc.name: Acm_Command_Dump_0200
99 * @tc.desc: Verify the "acm dump xxx" command.
100 * @tc.type: FUNC
101 * @tc.require: SR000GGVFO
102 */
103 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_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_DUMP);
115 }
116
117 /**
118 * @tc.name: Acm_Command_Dump_0300
119 * @tc.desc: Verify the "acm dump -x" command.
120 * @tc.type: FUNC
121 * @tc.require: SR000GGVFO
122 */
123 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_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_DUMP);
135 }
136
137 /**
138 * @tc.name: Acm_Command_Dump_0400
139 * @tc.desc: Verify the "acm dump -xxx" command.
140 * @tc.type: FUNC
141 * @tc.require: SR000GGVFO
142 */
143 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_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_DUMP);
155 }
156
157 /**
158 * @tc.name: Acm_Command_Dump_0500
159 * @tc.desc: Verify the "acm dump --x" command.
160 * @tc.type: FUNC
161 * @tc.require: SR000GGVFO
162 */
163 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_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_DUMP);
175 }
176
177 /**
178 * @tc.name: Acm_Command_Dump_0600
179 * @tc.desc: Verify the "acm dump --xxx" command.
180 * @tc.type: FUNC
181 * @tc.require: SR000GGVFO
182 */
183 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_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_DUMP);
195 }
196
197 /**
198 * @tc.name: Acm_Command_Dump_0700
199 * @tc.desc: Verify the "acm dump -h" command.
200 * @tc.type: FUNC
201 * @tc.require: SR000GGVFO
202 */
203 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_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_DUMP);
215 }
216
217 /**
218 * @tc.name: Acm_Command_Dump_0800
219 * @tc.desc: Verify the "acm dump --help" command.
220 * @tc.type: FUNC
221 * @tc.require: SR000GGVFO
222 */
223 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_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_DUMP);
235 }
236
237 /**
238 * @tc.name: Acm_Command_Dump_0900
239 * @tc.desc: Verify the "acm dump -i" command.
240 * @tc.type: FUNC
241 * @tc.require: SR000GGVFO
242 */
243 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_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_DUMP);
255 }
256
257 /**
258 * @tc.name: Acm_Command_Dump_1000
259 * @tc.desc: Verify the "acm dump -i" command.
260 * @tc.type: FUNC
261 * @tc.require:
262 */
263 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_1000, TestSize.Level1)
264 {
265 char *argv[] = {
266 const_cast<char *>(TOOL_NAME.c_str()),
267 const_cast<char *>(cmd_.c_str()),
268 const_cast<char *>("-i"),
269 const_cast<char *>("0"),
270 };
271 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
272
273 AccountCommand cmd(argc, argv);
274 EXPECT_FALSE(cmd.ExecCommand().empty());
275 }
276
277 /**
278 * @tc.name: Acm_Command_Dump_1100
279 * @tc.desc: Verify the "acm dump -i" command.
280 * @tc.type: FUNC
281 * @tc.require:
282 */
283 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_1100, TestSize.Level1)
284 {
285 OsAccountInfo osAccountInfo;
286 // create an os account
287 EXPECT_EQ(ERR_OK, OsAccount::GetInstance().CreateOsAccount(TOOL_NAME, OsAccountType::NORMAL, osAccountInfo));
288
289 std::string userId = std::to_string(osAccountInfo.GetLocalId());
290 char *argv[] = {
291 const_cast<char *>(TOOL_NAME.c_str()),
292 const_cast<char *>(cmd_.c_str()),
293 const_cast<char *>("-i"),
294 const_cast<char *>(userId.c_str()),
295 };
296 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
297
298 AccountCommand cmd(argc, argv);
299 EXPECT_FALSE(cmd.ExecCommand().empty());
300 OsAccount::GetInstance().RemoveOsAccount(osAccountInfo.GetLocalId());
301 }
302