1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
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 "hidebug_base.h"
17 #include "hidebug/hidebug.h"
18 #include "hidebug/hidebug_type.h"
19
20 #include <filesystem>
21 #include <hwext/gtest-ext.h>
22 #include <hwext/gtest-tag.h>
23
24 #include <cstdlib>
25
26 using namespace testing::ext;
27
28 namespace {
29 class HidebugTest : public ::testing::Test {
30 protected:
31 void SetUp() override {}
32 void TearDown() override
33 {
34 system("param set hiviewdfx.debugenv.hidebug_test 0");
35 system("param set libc.hook_mode 0");
36 }
37 };
38
39 /**
40 * @tc.name: InitEnvironmentParam
41 * @tc.desc: test InitEnvironmentParam
42 * @tc.type: FUNC
43 */
HWTEST_F(HidebugTest, InitEnvironmentParam1, TestSize.Level1)44 HWTEST_F(HidebugTest, InitEnvironmentParam1, TestSize.Level1)
45 {
46 system("param set hiviewdfx.debugenv.hidebug_test aaa:bbb");
47 const char* inputName = "hidebug_test";
48 EXPECT_TRUE(InitEnvironmentParam(inputName));
49 }
50
51 /**
52 * @tc.name: InitEnvironmentParam
53 * @tc.desc: test InitEnvironmentParam for input is nullptr
54 * @tc.type: FUNC
55 */
HWTEST_F(HidebugTest, InitEnvironmentParam2, TestSize.Level1)56 HWTEST_F(HidebugTest, InitEnvironmentParam2, TestSize.Level1)
57 {
58 system("param set hiviewdfx.debugenv.hidebug_test aaa:bbb");
59 const char* inputName = nullptr;
60 EXPECT_FALSE(InitEnvironmentParam(inputName));
61 }
62
63 /**
64 * @tc.name: InitEnvironmentParam
65 * @tc.desc: test InitEnvironmentParam for input is wrong
66 * @tc.type: FUNC
67 */
HWTEST_F(HidebugTest, InitEnvironmentParam3, TestSize.Level1)68 HWTEST_F(HidebugTest, InitEnvironmentParam3, TestSize.Level1)
69 {
70 system("param set hiviewdfx.debugenv.hidebug_test aaa:bbb");
71 const char* inputName = "hidebug_test/";
72 EXPECT_FALSE(InitEnvironmentParam(inputName));
73 }
74
75 /**
76 * @tc.name: InitEnvironmentParam
77 * @tc.desc: test InitEnvironmentParam for param set wrong
78 * @tc.type: FUNC
79 */
HWTEST_F(HidebugTest, InitEnvironmentParam4, TestSize.Level1)80 HWTEST_F(HidebugTest, InitEnvironmentParam4, TestSize.Level1)
81 {
82 system("param set hiviewdfx.debugenv.hidebug_test error_input");
83 const char* inputName = "hidebug_test";
84 EXPECT_FALSE(InitEnvironmentParam(inputName));
85 }
86
87 /**
88 * @tc.name: InitEnvironmentParam
89 * @tc.desc: test InitEnvironmentParam for libc.hook_mode
90 * @tc.type: FUNC
91 */
HWTEST_F(HidebugTest, InitEnvironmentParam5, TestSize.Level1)92 HWTEST_F(HidebugTest, InitEnvironmentParam5, TestSize.Level1)
93 {
94 system("param set hiviewdfx.debugenv.hidebug_test aaa:bbb");
95 system("param set libc.hook_mode startup:hidebug_test");
96 const char* inputName = "hidebug_test";
97 EXPECT_TRUE(InitEnvironmentParam(inputName));
98 }
99
100 /**
101 * @tc.name: InitEnvironmentParam
102 * @tc.desc: test InitEnvironmentParam for libc.hook_mode param set fail
103 * @tc.type: FUNC
104 */
HWTEST_F(HidebugTest, InitEnvironmentParam6, TestSize.Level1)105 HWTEST_F(HidebugTest, InitEnvironmentParam6, TestSize.Level1)
106 {
107 system("param set hiviewdfx.debugenv.hidebug_test error_input");
108 system("param set libc.hook_mode error_set:hidebug_test");
109 const char* inputName = "hidebug_test";
110 EXPECT_FALSE(InitEnvironmentParam(inputName));
111 }
112
113 /**
114 * @tc.name: InitEnvironmentParam
115 * @tc.desc: test InitEnvironmentParam for libc.hook_mode fail
116 * @tc.type: FUNC
117 */
HWTEST_F(HidebugTest, InitEnvironmentParam7, TestSize.Level1)118 HWTEST_F(HidebugTest, InitEnvironmentParam7, TestSize.Level1)
119 {
120 system("param set hiviewdfx.debugenv.hidebug_test error_input");
121 system("param set libc.hook_mode error_set:hidebug_test");
122 const char* inputName = "hidebug_test";
123 EXPECT_FALSE(InitEnvironmentParam(inputName));
124 }
125
126 /**
127 * @tc.name: InitEnvironmentParam
128 * @tc.desc: test InitEnvironmentParam for libc.hook_mode input error
129 * @tc.type: FUNC
130 */
HWTEST_F(HidebugTest, InitEnvironmentParam8, TestSize.Level1)131 HWTEST_F(HidebugTest, InitEnvironmentParam8, TestSize.Level1)
132 {
133 system("param set hiviewdfx.debugenv.hidebug_test aaa:bbb");
134 system("param set libc.hook_mode error_set:hidebug_test");
135 const char* inputName = "error_input";
136 EXPECT_FALSE(InitEnvironmentParam(inputName));
137 }
138
139 /**
140 * @tc.name: InitEnvironmentParam
141 * @tc.desc: test InitEnvironmentParam for libc.hook_mode param set wrong_proc
142 * @tc.type: FUNC
143 */
HWTEST_F(HidebugTest, InitEnvironmentParam9, TestSize.Level1)144 HWTEST_F(HidebugTest, InitEnvironmentParam9, TestSize.Level1)
145 {
146 system("param set hiviewdfx.debugenv.hidebug_test error_input");
147 system("param set libc.hook_mode start_up:wrong_proc");
148 const char* inputName = "hidebug";
149 EXPECT_FALSE(InitEnvironmentParam(inputName));
150 }
151
152 /**
153 * @tc.name: OH_HiDebug_GetAppCpuUsage1
154 * @tc.desc: test OH_HiDebug_GetAppCpuUsage.get app cpu usage
155 * @tc.type: FUNC
156 */
HWTEST_F(HidebugTest, OH_HiDebug_GetAppCpuUsage1, TestSize.Level1)157 HWTEST_F(HidebugTest, OH_HiDebug_GetAppCpuUsage1, TestSize.Level1)
158 {
159 EXPECT_TRUE(OH_HiDebug_GetAppCpuUsage() >= 0);
160 }
161
162 /**
163 * @tc.name: OH_HiDebug_GetAppThreadCpuUsage1
164 * @tc.desc: test OH_HiDebug_GetAppThreadCpuUsage.get thread cpu usage of app
165 * @tc.type: FUNC
166 */
HWTEST_F(HidebugTest, OH_HiDebug_GetAppThreadCpuUsage1, TestSize.Level1)167 HWTEST_F(HidebugTest, OH_HiDebug_GetAppThreadCpuUsage1, TestSize.Level1)
168 {
169 HiDebug_ThreadCpuUsagePtr threadCpuUsage = OH_HiDebug_GetAppThreadCpuUsage();
170 HiDebug_ThreadCpuUsagePtr curThreadCpuUsage = threadCpuUsage;
171 while (curThreadCpuUsage != nullptr) {
172 curThreadCpuUsage = curThreadCpuUsage->next;
173 }
174 OH_HiDebug_FreeThreadCpuUsage(&threadCpuUsage);
175 EXPECT_TRUE(true);
176 }
177
178 /**
179 * @tc.name: GetSystemCpuUsage
180 * @tc.desc: test InitEnvironmentParam for libc.hook_mode param set wrong_proc
181 * @tc.type: FUNC
182 */
HWTEST_F(HidebugTest, GetSystemCpuUsage, TestSize.Level1)183 HWTEST_F(HidebugTest, GetSystemCpuUsage, TestSize.Level1)
184 {
185 double systemCpuUsage = OH_HiDebug_GetSystemCpuUsage();
186 EXPECT_TRUE(systemCpuUsage >= 0 && systemCpuUsage <= 1);
187 }
188
189 /**
190 * @tc.name: GetAppMemoryLimit1
191 * @tc.desc: test GetAppMemoryLimit1
192 * @tc.type: FUNC
193 */
HWTEST_F(HidebugTest, GetAppMemoryLimit1, TestSize.Level1)194 HWTEST_F(HidebugTest, GetAppMemoryLimit1, TestSize.Level1)
195 {
196 HiDebug_MemoryLimit memoryLimit;
197 OH_HiDebug_GetAppMemoryLimit(&memoryLimit);
198 ASSERT_TRUE(memoryLimit.rssLimit >= 0);
199 ASSERT_TRUE(memoryLimit.vssLimit >= 0);
200 }
201
202 /**
203 * @tc.name: OH_HiDebug_GetAppNativeMemInfo1
204 * @tc.desc: test OH_HiDebug_GetAppNativeMemInfo. get application process memory info
205 * @tc.type: FUNC
206 */
HWTEST_F(HidebugTest, OH_HiDebug_GetAppNativeMemInfo1, TestSize.Level1)207 HWTEST_F(HidebugTest, OH_HiDebug_GetAppNativeMemInfo1, TestSize.Level1)
208 {
209 HiDebug_NativeMemInfo nativeMemInfo;
210 OH_HiDebug_GetAppNativeMemInfo(&nativeMemInfo);
211 ASSERT_TRUE(nativeMemInfo.pss >= 0);
212 ASSERT_TRUE(nativeMemInfo.vss >= 0);
213 ASSERT_TRUE(nativeMemInfo.rss >= 0);
214 ASSERT_TRUE(nativeMemInfo.sharedDirty >= 0);
215 ASSERT_TRUE(nativeMemInfo.privateDirty >= 0);
216 ASSERT_TRUE(nativeMemInfo.sharedClean >= 0);
217 ASSERT_TRUE(nativeMemInfo.privateClean >= 0);
218 }
219
220 /**
221 * @tc.name: OH_HiDebug_GetSystemMemInfo1
222 * @tc.desc: test OH_HiDebug_GetSystemMemInfo. get system memory info
223 * @tc.type: FUNC
224 */
HWTEST_F(HidebugTest, OH_HiDebug_GetSystemMemInfo1, TestSize.Level1)225 HWTEST_F(HidebugTest, OH_HiDebug_GetSystemMemInfo1, TestSize.Level1)
226 {
227 HiDebug_SystemMemInfo systemMemInfo;
228 OH_HiDebug_GetSystemMemInfo(&systemMemInfo);
229 ASSERT_TRUE(systemMemInfo.totalMem >= 0);
230 ASSERT_TRUE(systemMemInfo.freeMem >= 0);
231 ASSERT_TRUE(systemMemInfo.availableMem >= 0);
232 }
233
234 /**
235 * @tc.name: OH_HiDebug_StartAppTraceCapture1
236 * @tc.desc: test OH_HiDebug_StartAppTraceCapture. start app capture trace
237 * @tc.type: FUNC
238 */
HWTEST_F(HidebugTest, OH_HiDebug_StartAppTraceCapture1, TestSize.Level1)239 HWTEST_F(HidebugTest, OH_HiDebug_StartAppTraceCapture1, TestSize.Level1)
240 {
241 uint32_t fileLength = 256;
242 char fileName[256] = {0};
243 HiDebug_TraceFlag flag = HIDEBUG_TRACE_FLAG_MAIN_THREAD;
244 uint64_t tags = HIDEBUG_TRACE_TAG_COMMON_LIBRARY;
245 uint32_t limitSize = 1024 * 1024;
246 const char* targetPath = "/data/storage/el2/log";
247 auto captureResult = OH_HiDebug_StartAppTraceCapture(flag, tags, limitSize, fileName, fileLength);
248 if (std::filesystem::exists(targetPath)) {
249 EXPECT_EQ(captureResult, HIDEBUG_SUCCESS);
250 EXPECT_GT(sizeof(fileName) / sizeof(fileName[0]), 1);
251 EXPECT_EQ(OH_HiDebug_StopAppTraceCapture(), HIDEBUG_SUCCESS);
252 } else {
253 EXPECT_EQ(captureResult, HIDEBUG_NO_PERMISSION);
254 EXPECT_EQ(OH_HiDebug_StopAppTraceCapture(), HIDEBUG_NO_TRACE_RUNNING);
255 }
256 }
257
258 /**
259 * @tc.name: OH_HiDebug_StartAppTraceCapture2
260 * @tc.desc: test OH_HiDebug_StartAppTraceCapture. repeat start app capture trace
261 * @tc.type: FUNC
262 */
HWTEST_F(HidebugTest, OH_HiDebug_StartAppTraceCapture2, TestSize.Level1)263 HWTEST_F(HidebugTest, OH_HiDebug_StartAppTraceCapture2, TestSize.Level1)
264 {
265 uint32_t fileLength = 256;
266 char fileName[256] = {0};
267 HiDebug_TraceFlag flag = HIDEBUG_TRACE_FLAG_MAIN_THREAD;
268 uint64_t tags = HIDEBUG_TRACE_TAG_COMMON_LIBRARY;
269 uint32_t limitSize = 1024 * 1024;
270 const char* targetPath = "/data/storage/el2/log";
271 auto captureResult = OH_HiDebug_StartAppTraceCapture(flag, tags, limitSize, fileName, fileLength);
272 if (std::filesystem::exists(targetPath)) {
273 EXPECT_EQ(captureResult, HIDEBUG_SUCCESS);
274 auto captureResult2 = OH_HiDebug_StartAppTraceCapture(flag, tags, limitSize, fileName, fileLength);
275 EXPECT_EQ(captureResult2, HIDEBUG_TRACE_CAPTURED_ALREADY);
276 EXPECT_GT(sizeof(fileName) / sizeof(fileName[0]), 1);
277 EXPECT_EQ(OH_HiDebug_StopAppTraceCapture(), HIDEBUG_SUCCESS);
278 } else {
279 EXPECT_EQ(captureResult, HIDEBUG_NO_PERMISSION);
280 EXPECT_EQ(OH_HiDebug_StopAppTraceCapture(), HIDEBUG_NO_TRACE_RUNNING);
281 }
282 }
283
284 /**
285 * @tc.name: OH_HiDebug_StartAppTraceCapture3
286 * @tc.desc: test OH_HiDebug_StartAppTraceCapture. repeat stop app capture trace
287 * @tc.type: FUNC
288 */
HWTEST_F(HidebugTest, OH_HiDebug_StartAppTraceCapture3, TestSize.Level1)289 HWTEST_F(HidebugTest, OH_HiDebug_StartAppTraceCapture3, TestSize.Level1)
290 {
291 uint32_t fileLength = 256;
292 char fileName[256] = {0};
293 HiDebug_TraceFlag flag = HIDEBUG_TRACE_FLAG_MAIN_THREAD;
294 uint64_t tags = HIDEBUG_TRACE_TAG_COMMON_LIBRARY;
295 uint32_t limitSize = 1024 * 1024;
296 const char* targetPath = "/data/storage/el2/log";
297 auto captureResult = OH_HiDebug_StartAppTraceCapture(flag, tags, limitSize, fileName, fileLength);
298 if (std::filesystem::exists(targetPath)) {
299 EXPECT_EQ(captureResult, HIDEBUG_SUCCESS);
300 EXPECT_GT(sizeof(fileName) / sizeof(fileName[0]), 1);
301 EXPECT_EQ(OH_HiDebug_StopAppTraceCapture(), HIDEBUG_SUCCESS);
302 EXPECT_EQ(OH_HiDebug_StopAppTraceCapture(), HIDEBUG_NO_TRACE_RUNNING);
303 } else {
304 EXPECT_EQ(captureResult, HIDEBUG_NO_PERMISSION);
305 EXPECT_EQ(OH_HiDebug_StopAppTraceCapture(), HIDEBUG_NO_TRACE_RUNNING);
306 }
307 }
308
309 /**
310 * @tc.name: OH_HiDebug_GetGraphicsMemory
311 * @tc.desc: test OH_HiDebug_GetGraphicsMemory. get graphics memory.
312 * @tc.type: FUNC
313 */
HWTEST_F(HidebugTest, OH_HiDebug_GetGraphicsMemory, TestSize.Level1)314 HWTEST_F(HidebugTest, OH_HiDebug_GetGraphicsMemory, TestSize.Level1)
315 {
316 uint32_t value = 0;
317 EXPECT_EQ(OH_HiDebug_GetGraphicsMemory(&value), HIDEBUG_SUCCESS);
318 EXPECT_GE(value, 0);
319 }
320 } // namespace
321