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 "account_log_wrapper.h"
20 #include "singleton.h"
21
22 using namespace testing::ext;
23 using namespace OHOS;
24 using namespace OHOS::AAFwk;
25 using namespace OHOS::AccountSA;
26 using namespace OHOS::AccountSA::Constants;
27
28 namespace {
29 const std::string HELP_MSG_UNKNOWN_OPTION = "fail: unknown option.";
30 } // namespace
31
32 class AccountCommandDumpTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp() override;
37 void TearDown() override;
38
39 std::string cmd_ = "dump";
40 };
41
SetUpTestCase()42 void AccountCommandDumpTest::SetUpTestCase()
43 {}
44
TearDownTestCase()45 void AccountCommandDumpTest::TearDownTestCase()
46 {}
47
SetUp(void)48 void AccountCommandDumpTest::SetUp(void) __attribute__((no_sanitize("cfi")))
49 {
50 testing::UnitTest *test = testing::UnitTest::GetInstance();
51 ASSERT_NE(test, nullptr);
52 const testing::TestInfo *testinfo = test->current_test_info();
53 ASSERT_NE(testinfo, nullptr);
54 string testCaseName = string(testinfo->name());
55 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
56
57 // reset optind to 0
58 optind = 0;
59
60 std::vector<OsAccountInfo> osAccountInfos;
61 OsAccount::GetInstance().QueryAllCreatedOsAccounts(osAccountInfos);
62 for (const auto &info : osAccountInfos) {
63 if (info.GetLocalId() == START_USER_ID) {
64 continue;
65 }
66 ACCOUNT_LOGI("[SetUp] remove account %{public}d", info.GetLocalId());
67 OsAccount::GetInstance().RemoveOsAccount(info.GetLocalId());
68 }
69 }
70
TearDown()71 void AccountCommandDumpTest::TearDown()
72 {}
73
74 /**
75 * @tc.name: Acm_Command_Dump_0100
76 * @tc.desc: Verify the "acm dump" command.
77 * @tc.type: FUNC
78 * @tc.require: SR000GGVFO
79 */
80 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_0100, TestSize.Level1)
81 {
82 char *argv[] = {
83 const_cast<char *>(TOOL_NAME.c_str()),
84 const_cast<char *>(cmd_.c_str()),
85 const_cast<char *>(""),
86 };
87 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
88
89 AccountCommand cmd(argc, argv);
90 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_DUMP);
91 }
92
93 /**
94 * @tc.name: Acm_Command_Dump_0200
95 * @tc.desc: Verify the "acm dump xxx" command.
96 * @tc.type: FUNC
97 * @tc.require: SR000GGVFO
98 */
99 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_0200, TestSize.Level1)
100 {
101 char *argv[] = {
102 const_cast<char *>(TOOL_NAME.c_str()),
103 const_cast<char *>(cmd_.c_str()),
104 const_cast<char *>("xxx"),
105 const_cast<char *>(""),
106 };
107 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
108
109 AccountCommand cmd(argc, argv);
110 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_DUMP);
111 }
112
113 /**
114 * @tc.name: Acm_Command_Dump_0300
115 * @tc.desc: Verify the "acm dump -x" command.
116 * @tc.type: FUNC
117 * @tc.require: SR000GGVFO
118 */
119 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_0300, TestSize.Level1)
120 {
121 char *argv[] = {
122 const_cast<char *>(TOOL_NAME.c_str()),
123 const_cast<char *>(cmd_.c_str()),
124 const_cast<char *>("-x"),
125 const_cast<char *>(""),
126 };
127 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
128
129 AccountCommand cmd(argc, argv);
130 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_DUMP);
131 }
132
133 /**
134 * @tc.name: Acm_Command_Dump_0400
135 * @tc.desc: Verify the "acm dump -xxx" command.
136 * @tc.type: FUNC
137 * @tc.require: SR000GGVFO
138 */
139 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_0400, TestSize.Level1)
140 {
141 char *argv[] = {
142 const_cast<char *>(TOOL_NAME.c_str()),
143 const_cast<char *>(cmd_.c_str()),
144 const_cast<char *>("-xxx"),
145 const_cast<char *>(""),
146 };
147 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
148
149 AccountCommand cmd(argc, argv);
150 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_DUMP);
151 }
152
153 /**
154 * @tc.name: Acm_Command_Dump_0500
155 * @tc.desc: Verify the "acm dump --x" command.
156 * @tc.type: FUNC
157 * @tc.require: SR000GGVFO
158 */
159 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_0500, TestSize.Level1)
160 {
161 char *argv[] = {
162 const_cast<char *>(TOOL_NAME.c_str()),
163 const_cast<char *>(cmd_.c_str()),
164 const_cast<char *>("--x"),
165 const_cast<char *>(""),
166 };
167 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
168
169 AccountCommand cmd(argc, argv);
170 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_DUMP);
171 }
172
173 /**
174 * @tc.name: Acm_Command_Dump_0600
175 * @tc.desc: Verify the "acm dump --xxx" command.
176 * @tc.type: FUNC
177 * @tc.require: SR000GGVFO
178 */
179 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_0600, TestSize.Level1)
180 {
181 char *argv[] = {
182 const_cast<char *>(TOOL_NAME.c_str()),
183 const_cast<char *>(cmd_.c_str()),
184 const_cast<char *>("--xxx"),
185 const_cast<char *>(""),
186 };
187 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
188
189 AccountCommand cmd(argc, argv);
190 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_DUMP);
191 }
192
193 /**
194 * @tc.name: Acm_Command_Dump_0700
195 * @tc.desc: Verify the "acm dump -h" command.
196 * @tc.type: FUNC
197 * @tc.require: SR000GGVFO
198 */
199 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_0700, TestSize.Level1)
200 {
201 char *argv[] = {
202 const_cast<char *>(TOOL_NAME.c_str()),
203 const_cast<char *>(cmd_.c_str()),
204 const_cast<char *>("-h"),
205 const_cast<char *>(""),
206 };
207 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
208
209 AccountCommand cmd(argc, argv);
210 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DUMP);
211 }
212
213 /**
214 * @tc.name: Acm_Command_Dump_0800
215 * @tc.desc: Verify the "acm dump --help" command.
216 * @tc.type: FUNC
217 * @tc.require: SR000GGVFO
218 */
219 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_0800, TestSize.Level1)
220 {
221 char *argv[] = {
222 const_cast<char *>(TOOL_NAME.c_str()),
223 const_cast<char *>(cmd_.c_str()),
224 const_cast<char *>("--help"),
225 const_cast<char *>(""),
226 };
227 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
228
229 AccountCommand cmd(argc, argv);
230 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DUMP);
231 }
232
233 /**
234 * @tc.name: Acm_Command_Dump_0900
235 * @tc.desc: Verify the "acm dump -i" command.
236 * @tc.type: FUNC
237 * @tc.require: SR000GGVFO
238 */
239 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_0900, TestSize.Level1)
240 {
241 char *argv[] = {
242 const_cast<char *>(TOOL_NAME.c_str()),
243 const_cast<char *>(cmd_.c_str()),
244 const_cast<char *>("-i"),
245 const_cast<char *>(""),
246 };
247 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
248
249 AccountCommand cmd(argc, argv);
250 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT + "\n" + HELP_MSG_DUMP);
251 }
252
253 /**
254 * @tc.name: Acm_Command_Dump_1000
255 * @tc.desc: Verify the "acm dump -i" command.
256 * @tc.type: FUNC
257 * @tc.require:
258 */
259 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_1000, TestSize.Level1)
260 {
261 char *argv[] = {
262 const_cast<char *>(TOOL_NAME.c_str()),
263 const_cast<char *>(cmd_.c_str()),
264 const_cast<char *>("-i"),
265 const_cast<char *>("0"),
266 };
267 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
268
269 AccountCommand cmd(argc, argv);
270 EXPECT_FALSE(cmd.ExecCommand().empty());
271 }
272
273 /**
274 * @tc.name: Acm_Command_Dump_1100
275 * @tc.desc: Verify the "acm dump -i" command.
276 * @tc.type: FUNC
277 * @tc.require:
278 */
279 HWTEST_F(AccountCommandDumpTest, Acm_Command_Dump_1100, TestSize.Level1)
280 {
281 OsAccountInfo osAccountInfo;
282 // create an os account
283 EXPECT_EQ(ERR_OK, OsAccount::GetInstance().CreateOsAccount(TOOL_NAME, OsAccountType::NORMAL, osAccountInfo));
284
285 std::string userId = std::to_string(osAccountInfo.GetLocalId());
286 char *argv[] = {
287 const_cast<char *>(TOOL_NAME.c_str()),
288 const_cast<char *>(cmd_.c_str()),
289 const_cast<char *>("-i"),
290 const_cast<char *>(userId.c_str()),
291 };
292 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
293
294 AccountCommand cmd(argc, argv);
295 EXPECT_FALSE(cmd.ExecCommand().empty());
296 OsAccount::GetInstance().RemoveOsAccount(osAccountInfo.GetLocalId());
297 }
298