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
20 using namespace testing::ext;
21 using namespace OHOS;
22 using namespace OHOS::AAFwk;
23 using namespace OHOS::AccountSA;
24
25 namespace {
26 const std::string HELP_MSG_UNKNOWN_OPTION = "error: unknown option.";
27 } // namespace
28
29 class AccountCommandSwitchTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp() override;
34 void TearDown() override;
35
36 std::string cmd_ = "switch";
37 };
38
SetUpTestCase()39 void AccountCommandSwitchTest::SetUpTestCase()
40 {}
41
TearDownTestCase()42 void AccountCommandSwitchTest::TearDownTestCase()
43 {}
44
SetUp()45 void AccountCommandSwitchTest::SetUp()
46 {
47 // reset optind to 0
48 optind = 0;
49 }
50
TearDown()51 void AccountCommandSwitchTest::TearDown()
52 {}
53
54 /**
55 * @tc.name: Acm_Command_Switch_0100
56 * @tc.desc: Verify the "acm switch" command.
57 * @tc.type: FUNC
58 * @tc.require: SR000GGVFO
59 */
60 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_0100, TestSize.Level1)
61 {
62 char *argv[] = {
63 (char *)TOOL_NAME.c_str(),
64 (char *)cmd_.c_str(),
65 (char *)"",
66 };
67 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
68
69 AccountCommand cmd(argc, argv);
70 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_SWITCH);
71 }
72
73 /**
74 * @tc.name: Acm_Command_Switch_0200
75 * @tc.desc: Verify the "acm switch xxx" command.
76 * @tc.type: FUNC
77 * @tc.require: SR000GGVFO
78 */
79 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_0200, TestSize.Level1)
80 {
81 char *argv[] = {
82 (char *)TOOL_NAME.c_str(),
83 (char *)cmd_.c_str(),
84 (char *)"xxx",
85 (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_SWITCH);
91 }
92
93 /**
94 * @tc.name: Acm_Command_Switch_0300
95 * @tc.desc: Verify the "acm switch -x" command.
96 * @tc.type: FUNC
97 * @tc.require: SR000GGVFO
98 */
99 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_0300, TestSize.Level1)
100 {
101 char *argv[] = {
102 (char *)TOOL_NAME.c_str(),
103 (char *)cmd_.c_str(),
104 (char *)"-x",
105 (char *)"",
106 };
107 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
108
109 AccountCommand cmd(argc, argv);
110 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNKNOWN_OPTION + "\n" + HELP_MSG_SWITCH);
111 }
112
113 /**
114 * @tc.name: Acm_Command_Switch_0400
115 * @tc.desc: Verify the "acm switch -xxx" command.
116 * @tc.type: FUNC
117 * @tc.require: SR000GGVFO
118 */
119 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_0400, TestSize.Level1)
120 {
121 char *argv[] = {
122 (char *)TOOL_NAME.c_str(),
123 (char *)cmd_.c_str(),
124 (char *)"-xxx",
125 (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_SWITCH);
131 }
132
133 /**
134 * @tc.name: Acm_Command_Switch_0500
135 * @tc.desc: Verify the "acm switch --x" command.
136 * @tc.type: FUNC
137 * @tc.require: SR000GGVFO
138 */
139 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_0500, TestSize.Level1)
140 {
141 char *argv[] = {
142 (char *)TOOL_NAME.c_str(),
143 (char *)cmd_.c_str(),
144 (char *)"--x",
145 (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_SWITCH);
151 }
152
153 /**
154 * @tc.name: Acm_Command_Switch_0600
155 * @tc.desc: Verify the "acm switch --xxx" command.
156 * @tc.type: FUNC
157 * @tc.require: SR000GGVFO
158 */
159 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_0600, TestSize.Level1)
160 {
161 char *argv[] = {
162 (char *)TOOL_NAME.c_str(),
163 (char *)cmd_.c_str(),
164 (char *)"--xxx",
165 (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_SWITCH);
171 }
172
173 /**
174 * @tc.name: Acm_Command_Switch_0700
175 * @tc.desc: Verify the "acm switch -h" command.
176 * @tc.type: FUNC
177 * @tc.require: SR000GGVFO
178 */
179 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_0700, TestSize.Level1)
180 {
181 char *argv[] = {
182 (char *)TOOL_NAME.c_str(),
183 (char *)cmd_.c_str(),
184 (char *)"-h",
185 (char *)"",
186 };
187 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
188
189 AccountCommand cmd(argc, argv);
190 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_SWITCH);
191 }
192
193 /**
194 * @tc.name: Acm_Command_Switch_0800
195 * @tc.desc: Verify the "acm switch --help" command.
196 * @tc.type: FUNC
197 * @tc.require: SR000GGVFO
198 */
199 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_0800, TestSize.Level1)
200 {
201 char *argv[] = {
202 (char *)TOOL_NAME.c_str(),
203 (char *)cmd_.c_str(),
204 (char *)"--help",
205 (char *)"",
206 };
207 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
208
209 AccountCommand cmd(argc, argv);
210 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_SWITCH);
211 }
212
213 /**
214 * @tc.name: Acm_Command_Switch_0900
215 * @tc.desc: Verify the "acm switch -i" command.
216 * @tc.type: FUNC
217 * @tc.require: SR000GGVFO
218 */
219 HWTEST_F(AccountCommandSwitchTest, Acm_Command_Switch_0900, TestSize.Level1)
220 {
221 char *argv[] = {
222 (char *)TOOL_NAME.c_str(),
223 (char *)cmd_.c_str(),
224 (char *)"-i",
225 (char *)"",
226 };
227 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
228
229 AccountCommand cmd(argc, argv);
230 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT + "\n" + HELP_MSG_SWITCH);
231 }
232