• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 AccountCommandSwitchTest : 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_ = "switch";
38 };
39 
SetUpTestCase()40 void AccountCommandSwitchTest::SetUpTestCase()
41 {}
42 
TearDownTestCase()43 void AccountCommandSwitchTest::TearDownTestCase()
44 {}
45 
SetUp()46 void AccountCommandSwitchTest::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 AccountCommandSwitchTest::TearDown()
59 {}
60 
61 /**
62  * @tc.name: Acm_Command_Switch_0100
63  * @tc.desc: Verify the "acm switch" command.
64  * @tc.type: FUNC
65  * @tc.require: SR000GGVFO
66  */
67 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_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_SWITCH);
78 }
79 
80 /**
81  * @tc.name: Acm_Command_Switch_0200
82  * @tc.desc: Verify the "acm switch xxx" command.
83  * @tc.type: FUNC
84  * @tc.require: SR000GGVFO
85  */
86 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_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_SWITCH);
98 }
99 
100 /**
101  * @tc.name: Acm_Command_Switch_0300
102  * @tc.desc: Verify the "acm switch -x" command.
103  * @tc.type: FUNC
104  * @tc.require: SR000GGVFO
105  */
106 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_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_SWITCH);
118 }
119 
120 /**
121  * @tc.name: Acm_Command_Switch_0400
122  * @tc.desc: Verify the "acm switch -xxx" command.
123  * @tc.type: FUNC
124  * @tc.require: SR000GGVFO
125  */
126 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_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_SWITCH);
138 }
139 
140 /**
141  * @tc.name: Acm_Command_Switch_0500
142  * @tc.desc: Verify the "acm switch --x" command.
143  * @tc.type: FUNC
144  * @tc.require: SR000GGVFO
145  */
146 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_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_SWITCH);
158 }
159 
160 /**
161  * @tc.name: Acm_Command_Switch_0600
162  * @tc.desc: Verify the "acm switch --xxx" command.
163  * @tc.type: FUNC
164  * @tc.require: SR000GGVFO
165  */
166 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_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_SWITCH);
178 }
179 
180 /**
181  * @tc.name: Acm_Command_Switch_0700
182  * @tc.desc: Verify the "acm switch -h" command.
183  * @tc.type: FUNC
184  * @tc.require: SR000GGVFO
185  */
186 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_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_SWITCH);
198 }
199 
200 /**
201  * @tc.name: Acm_Command_Switch_0800
202  * @tc.desc: Verify the "acm switch --help" command.
203  * @tc.type: FUNC
204  * @tc.require: SR000GGVFO
205  */
206 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_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_SWITCH);
218 }
219 
220 /**
221  * @tc.name: Acm_Command_Switch_0900
222  * @tc.desc: Verify the "acm switch -i" command.
223  * @tc.type: FUNC
224  * @tc.require: SR000GGVFO
225  */
226 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_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_SWITCH);
238 }
239 
240 /**
241  * @tc.name: Acm_Command_Switch_1000
242  * @tc.desc: Verify the "acm switch -i" command.
243  * @tc.type: FUNC
244  * @tc.require:
245  */
246 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_1000, TestSize.Level1)
247 {
248     OsAccountInfo osAccountInfo;
249     // create an os account
250     EXPECT_EQ(ERR_OK, OsAccount::GetInstance().CreateOsAccount(TOOL_NAME, OsAccountType::NORMAL, osAccountInfo));
251 
252     std::string userId = std::to_string(osAccountInfo.GetLocalId());
253     char *argv[] = {
254         const_cast<char *>(TOOL_NAME.c_str()),
255         const_cast<char *>(cmd_.c_str()),
256         const_cast<char *>("-i"),
257         const_cast<char *>(userId.c_str()),
258         const_cast<char *>(""),
259     };
260     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
261 
262     AccountCommand cmd(argc, argv);
263     EXPECT_EQ(cmd.ExecCommand(), STRING_SWITCH_OS_ACCOUNT_OK + "\n");
264     EXPECT_EQ(ERR_OK, OsAccount::GetInstance().RemoveOsAccount(osAccountInfo.GetLocalId()));
265 }
266