• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #define protected public
19 #define private public
20 #include "ability_command.h"
21 #undef protected
22 #undef private
23 
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::AAFwk;
27 
28 namespace {
29 const std::string STRING_VALID_PID  = "520";
30 const std::string STRING_INVALID_PID = "-1";
31 const std::string STRING_VALID_LEVEL  = "1";
32 const std::string STRING_INVALID_LEVEL = "3";
33 }  // namespace
34 
35 class AaCommandSendMemoryLevelTest : public ::testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp() override;
40     void TearDown() override;
41     std::string sendMemoryLevelCmd_ = "send-memory-level";
42 };
43 
SetUpTestCase()44 void AaCommandSendMemoryLevelTest::SetUpTestCase()
45 {}
46 
TearDownTestCase()47 void AaCommandSendMemoryLevelTest::TearDownTestCase()
48 {}
49 
SetUp()50 void AaCommandSendMemoryLevelTest::SetUp()
51 {
52     // reset optind to 0
53     optind = 0;
54 }
55 
TearDown()56 void AaCommandSendMemoryLevelTest::TearDown()
57 {}
58 
59 /**
60  * @tc.number: Aa_Command_ParsePidMemoryLevel_0100
61  * @tc.name: Parse Pid and Level from argv[]
62  * @tc.desc: Verify that send-memory-level command parse Pid and Level normally.
63  */
64 HWTEST_F(AaCommandSendMemoryLevelTest, ParsePidMemoryLevel_0100, TestSize.Level1)
65 {
66     GTEST_LOG_(INFO) << "Aa_Command_ParsePidMemoryLevel_0100";
67 
68     char* argv[] = {
69         (char*)TOOL_NAME.c_str(),
70         (char*)sendMemoryLevelCmd_.c_str(),
71         (char*)"-p",
72         (char*)STRING_VALID_PID.c_str(),
73         (char*)"-l",
74         (char*)STRING_VALID_LEVEL.c_str(),
75         (char*)"",
76     };
77     int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
78 
79     AbilityManagerShellCommand cmd(argc, argv);
80     std::string pid = "";
81     std::string level = "";
82     cmd.ParsePidMemoryLevel(pid, level);
83     EXPECT_EQ(pid, STRING_VALID_PID);
84     EXPECT_EQ(level, STRING_VALID_LEVEL);
85 }
86 
87 /**
88  * @tc.number: Aa_Command_ParsePidMemoryLevel_0200
89  * @tc.name: Parse Pid and Level from argv[]
90  * @tc.desc: Verify that send-memory-level command parse Pid and Level unormally.
91  */
92 HWTEST_F(AaCommandSendMemoryLevelTest, ParsePidMemoryLevel_0200, TestSize.Level1)
93 {
94     GTEST_LOG_(INFO) << "Aa_Command_ParsePidMemoryLevel_0200";
95 
96     char* argv[] = {
97         (char*)TOOL_NAME.c_str(),
98         (char*)sendMemoryLevelCmd_.c_str(),
99         (char*)"-p",
100         (char*)STRING_INVALID_PID.c_str(),
101         (char*)"-l",
102         (char*)STRING_INVALID_LEVEL.c_str(),
103         (char*)"",
104     };
105     int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
106 
107     AbilityManagerShellCommand cmd(argc, argv);
108     std::string pid = STRING_VALID_PID;
109     std::string level = STRING_VALID_LEVEL;
110     cmd.ParsePidMemoryLevel(pid, level); // 解析后pid的值:STRING_VALID_PID ——> STRING_INVALID_PID
111     EXPECT_EQ(pid, STRING_INVALID_PID);
112     EXPECT_EQ(level, STRING_INVALID_LEVEL);
113 }
114 
115 /**
116  * @tc.number: Aa_Command_SendMemoryLevel_0100
117  * @tc.name: ExecCommand
118  * @tc.desc: Verify the "aa send-memory-level" command.
119  */
120 HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0100, TestSize.Level1)
121 {
122     GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0100";
123 
124     char* argv[] = {
125         (char*)TOOL_NAME.c_str(),
126         (char*)sendMemoryLevelCmd_.c_str(),
127         (char*)"",
128     };
129     int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
130 
131     AbilityManagerShellCommand cmd(argc, argv);
132     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_SEND_MEMORY_LEVEL + "\n");
133 }
134 
135 /**
136  * @tc.number: Aa_Command_SendMemoryLevel_0200
137  * @tc.name: ExecCommand
138  * @tc.desc: Verify the "aa send-memory-level xxx" command.
139  */
140 HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0200, TestSize.Level1)
141 {
142     GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0200";
143 
144     char* argv[] = {
145         (char*)TOOL_NAME.c_str(),
146         (char*)sendMemoryLevelCmd_.c_str(),
147         (char*)"xxx",
148         (char*)"",
149     };
150     int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
151 
152     AbilityManagerShellCommand cmd(argc, argv);
153     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_SEND_MEMORY_LEVEL + "\n");
154 }
155 
156 /**
157  * @tc.number: Aa_Command_SendMemoryLevel_0300
158  * @tc.name: ExecCommand
159  * @tc.desc: Verify the "aa send-memory-level -x" command.
160  */
161 HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0300, TestSize.Level1)
162 {
163     GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0300";
164 
165     char* argv[] = {
166         (char*)TOOL_NAME.c_str(),
167         (char*)sendMemoryLevelCmd_.c_str(),
168         (char*)"-x",
169         (char*)"",
170     };
171     int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
172 
173     AbilityManagerShellCommand cmd(argc, argv);
174     EXPECT_EQ(cmd.ExecCommand(), "fail: unknown option.\n" + HELP_MSG_SEND_MEMORY_LEVEL + "\n");
175 }
176 
177 /**
178  * @tc.number: Aa_Command_SendMemoryLevel_0400
179  * @tc.name: ExecCommand
180  * @tc.desc: Verify the "aa send-memory-level -h" command.
181  */
182 HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0400, TestSize.Level1)
183 {
184     GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0400";
185 
186     char* argv[] = {
187         (char*)TOOL_NAME.c_str(),
188         (char*)sendMemoryLevelCmd_.c_str(),
189         (char*)"-h",
190         (char*)"",
191     };
192     int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
193 
194     AbilityManagerShellCommand cmd(argc, argv);
195     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_SEND_MEMORY_LEVEL + "\n");
196 }
197 
198 /**
199  * @tc.number: Aa_Command_SendMemoryLevel_0500
200  * @tc.name: ExecCommand
201  * @tc.desc: Verify the "aa send-memory-level --help" command.
202  */
203 HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0500, TestSize.Level1)
204 {
205     GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0500";
206 
207     char* argv[] = {
208         (char*)TOOL_NAME.c_str(),
209         (char*)sendMemoryLevelCmd_.c_str(),
210         (char*)"--help",
211         (char*)"",
212     };
213     int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
214 
215     AbilityManagerShellCommand cmd(argc, argv);
216     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_SEND_MEMORY_LEVEL + "\n");
217 }
218 
219 /**
220  * @tc.number: Aa_Command_SendMemoryLevel_0600
221  * @tc.name: ExecCommand
222  * @tc.desc: Verify the "aa send-memory-level -p -l" command.
223  */
224 HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0600, TestSize.Level1)
225 {
226     GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0600";
227 
228     char* argv[] = {
229         (char*)TOOL_NAME.c_str(),
230         (char*)sendMemoryLevelCmd_.c_str(),
231         (char*)"-p",
232         (char*)"-l",
233         (char*)"",
234     };
235     int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
236 
237     AbilityManagerShellCommand cmd(argc, argv);
238     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_SEND_MEMORY_LEVEL + "\n");
239 }
240 
241 /**
242  * @tc.number: Aa_Command_SendMemoryLevel_0700
243  * @tc.name: ExecCommand
244  * @tc.desc: Verify the "aa send-memory-level -p -1 -l 3" command.
245  */
246 HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0700, TestSize.Level1)
247 {
248     GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0700";
249 
250     char* argv[] = {
251         (char*)TOOL_NAME.c_str(),
252         (char*)sendMemoryLevelCmd_.c_str(),
253         (char*)"-p",
254         (char*)STRING_INVALID_PID.c_str(),
255         (char*)"-l",
256         (char*)STRING_INVALID_LEVEL.c_str(),
257         (char*)"",
258     };
259     int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
260 
261     AbilityManagerShellCommand cmd(argc, argv);
262     EXPECT_TRUE(cmd.ExecCommand().find(STRING_SEND_MEMORY_LEVEL_NG) != string::npos);
263 }
264 
265 /**
266  * @tc.number: Aa_Command_SendMemoryLevel_0800
267  * @tc.name: ExecCommand
268  * @tc.desc: Verify the "aa send-memory-level -p -1 -l 1" command.
269  */
270 HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0800, TestSize.Level1)
271 {
272     GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0800";
273 
274     char* argv[] = {
275         (char*)TOOL_NAME.c_str(),
276         (char*)sendMemoryLevelCmd_.c_str(),
277         (char*)"-p",
278         (char*)STRING_INVALID_PID.c_str(),
279         (char*)"-l",
280         (char*)STRING_VALID_LEVEL.c_str(),
281         (char*)"",
282     };
283     int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
284 
285     AbilityManagerShellCommand cmd(argc, argv);
286     EXPECT_TRUE(cmd.ExecCommand().find(STRING_SEND_MEMORY_LEVEL_NG) != string::npos);
287 }
288 
289 /**
290  * @tc.number: Aa_Command_SendMemoryLevel_0900
291  * @tc.name: ExecCommand
292  * @tc.desc: Verify the "aa send-memory-level -p -1 -l 3" command.
293  */
294 HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0900, TestSize.Level1)
295 {
296     GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0900";
297 
298     char* argv[] = {
299         (char*)TOOL_NAME.c_str(),
300         (char*)sendMemoryLevelCmd_.c_str(),
301         (char*)"-p",
302         (char*)STRING_INVALID_PID.c_str(),
303         (char*)"-l",
304         (char*)STRING_INVALID_LEVEL.c_str(),
305         (char*)"",
306     };
307     int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1;
308 
309     AbilityManagerShellCommand cmd(argc, argv);
310     EXPECT_TRUE(cmd.ExecCommand().find(STRING_SEND_MEMORY_LEVEL_NG) != string::npos);
311 }