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