• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdlib>
17 #include <filesystem>
18 
19 #include <gtest/hwext/gtest-ext.h>
20 #include <gtest/hwext/gtest-tag.h>
21 
22 #include "hidebug_base.h"
23 #include "hidebug/hidebug.h"
24 #include "hidebug/hidebug_type.h"
25 #include "securec.h"
26 #include <sys/mman.h>
27 
28 using namespace testing::ext;
29 
30 namespace {
31 class HidebugTest : public ::testing::Test {
32 protected:
SetUp()33     void SetUp() override {}
TearDown()34     void TearDown() override
35     {
36         system("param set hiviewdfx.debugenv.hidebug_test 0");
37         system("param set libc.hook_mode 0");
38     }
39 };
40 
TestCustomMalloc(size_t size)41 static void* TestCustomMalloc(size_t size)
42 {
43     HiDebug_MallocDispatch* original = (HiDebug_MallocDispatch*)OH_HiDebug_GetDefaultMallocDispatchTable();
44     return original->malloc(size);
45 }
DesCustomMalloc(HiDebug_MallocDispatch * current)46 static void DesCustomMalloc(HiDebug_MallocDispatch* current)
47 {
48     HiDebug_MallocDispatch* original = (HiDebug_MallocDispatch*)OH_HiDebug_GetDefaultMallocDispatchTable();
49     original->free(current);
50     OH_HiDebug_RestoreMallocDispatchTable();
51 }
52 
TestCustomFree(void * ptr)53 static void TestCustomFree(void* ptr)
54 {
55     HiDebug_MallocDispatch* original = (HiDebug_MallocDispatch*)OH_HiDebug_GetDefaultMallocDispatchTable();
56     EXPECT_NE(ptr, nullptr);
57     original->free(ptr);
58 }
59 
TestCustomMmap(void * addr,size_t len,int prot,int flags,int fd,off_t offset)60 static void* TestCustomMmap(void* addr, size_t len, int prot, int flags, int fd, off_t offset)
61 {
62     HiDebug_MallocDispatch* original = (HiDebug_MallocDispatch*)OH_HiDebug_GetDefaultMallocDispatchTable();
63     return original->mmap(addr, len, prot, flags, fd, offset);
64 }
65 
TestCustomMunmap(void * addr,size_t len)66 static int TestCustomMunmap(void* addr, size_t len)
67 {
68     HiDebug_MallocDispatch* original = (HiDebug_MallocDispatch*)OH_HiDebug_GetDefaultMallocDispatchTable();
69     EXPECT_NE(addr, nullptr);
70     return original->munmap(addr, len);
71 }
72 
TestCustomCalloc(size_t nmemb,size_t size)73 static void* TestCustomCalloc(size_t nmemb, size_t size)
74 {
75     HiDebug_MallocDispatch* original = (HiDebug_MallocDispatch*)OH_HiDebug_GetDefaultMallocDispatchTable();
76     return original->calloc(nmemb, size);
77 }
78 
TestCustomRealloc(void * ptr,size_t size)79 static void* TestCustomRealloc(void* ptr, size_t size)
80 {
81     HiDebug_MallocDispatch* original = (HiDebug_MallocDispatch*)OH_HiDebug_GetDefaultMallocDispatchTable();
82     return original->realloc(ptr, size);
83 }
84 
85 /**
86  * @tc.name: InitEnvironmentParam
87  * @tc.desc: test InitEnvironmentParam
88  * @tc.type: FUNC
89  */
90 HWTEST_F(HidebugTest, InitEnvironmentParam1, TestSize.Level1)
91 {
92     system("param set hiviewdfx.debugenv.hidebug_test aaa:bbb");
93     EXPECT_TRUE(InitEnvironmentParam("hidebug_test"));
94     EXPECT_FALSE(InitEnvironmentParam("hidebug_test/"));
95     EXPECT_FALSE(InitEnvironmentParam(nullptr));
96 }
97 
98 /**
99  * @tc.name: OH_HiDebug_GetAppCpuUsage1
100  * @tc.desc: test OH_HiDebug_GetAppCpuUsage.get app cpu usage
101  * @tc.type: FUNC
102  */
103 HWTEST_F(HidebugTest, OH_HiDebug_GetAppCpuUsage1, TestSize.Level1)
104 {
105     EXPECT_TRUE(OH_HiDebug_GetAppCpuUsage() >= 0);
106 }
107 
108 /**
109  * @tc.name: OH_HiDebug_GetAppThreadCpuUsage1
110  * @tc.desc: test OH_HiDebug_GetAppThreadCpuUsage.get thread cpu usage of app
111  * @tc.type: FUNC
112  */
113 HWTEST_F(HidebugTest, OH_HiDebug_GetAppThreadCpuUsage1, TestSize.Level1)
114 {
115     HiDebug_ThreadCpuUsagePtr threadCpuUsage = OH_HiDebug_GetAppThreadCpuUsage();
116     HiDebug_ThreadCpuUsagePtr curThreadCpuUsage = threadCpuUsage;
117     while (curThreadCpuUsage != nullptr) {
118         curThreadCpuUsage = curThreadCpuUsage->next;
119     }
120     OH_HiDebug_FreeThreadCpuUsage(&threadCpuUsage);
121     HiDebug_ThreadCpuUsagePtr threadCpuUsage1 = nullptr;
122     OH_HiDebug_FreeThreadCpuUsage(&threadCpuUsage1);
123     OH_HiDebug_FreeThreadCpuUsage(nullptr);
124     EXPECT_TRUE(true);
125 }
126 
127 /**
128  * @tc.name: GetSystemCpuUsage
129  * @tc.desc: test InitEnvironmentParam for libc.hook_mode param set wrong_proc
130  * @tc.type: FUNC
131  */
132 HWTEST_F(HidebugTest, GetSystemCpuUsage, TestSize.Level1)
133 {
134     double systemCpuUsage = OH_HiDebug_GetSystemCpuUsage();
135     ASSERT_GE(systemCpuUsage, 0);
136     ASSERT_LE(systemCpuUsage, 1);
137 }
138 
139 /**
140  * @tc.name: GetAppMemoryLimit1
141  * @tc.desc: test GetAppMemoryLimit1
142  * @tc.type: FUNC
143  */
144 HWTEST_F(HidebugTest, GetAppMemoryLimit1, TestSize.Level1)
145 {
146     OH_HiDebug_GetAppMemoryLimit(nullptr);
147     HiDebug_MemoryLimit memoryLimit;
148     OH_HiDebug_GetAppMemoryLimit(&memoryLimit);
149     ASSERT_GE(memoryLimit.rssLimit, 0);
150     ASSERT_GE(memoryLimit.vssLimit, 0);
151 }
152 
153 /**
154  * @tc.name: OH_HiDebug_GetAppNativeMemInfo1
155  * @tc.desc: test OH_HiDebug_GetAppNativeMemInfo. get application process memory info
156  * @tc.type: FUNC
157  */
158 HWTEST_F(HidebugTest, OH_HiDebug_GetAppNativeMemInfo1, TestSize.Level1)
159 {
160     HiDebug_NativeMemInfo nativeMemInfo;
161     OH_HiDebug_GetAppNativeMemInfo(&nativeMemInfo);
162     ASSERT_TRUE(nativeMemInfo.pss >= 0);
163     ASSERT_TRUE(nativeMemInfo.vss >= 0);
164     ASSERT_TRUE(nativeMemInfo.rss >= 0);
165     ASSERT_TRUE(nativeMemInfo.sharedDirty >= 0);
166     ASSERT_TRUE(nativeMemInfo.privateDirty >= 0);
167     ASSERT_TRUE(nativeMemInfo.sharedClean >= 0);
168     ASSERT_TRUE(nativeMemInfo.privateClean >= 0);
169 }
170 
171 /**
172  * @tc.name: OH_HiDebug_GetAppNativeMemInfoWithCache1
173  * @tc.desc: test OH_HiDebug_GetAppNativeMemInfoWithCache. get application process memory info
174  * @tc.type: FUNC
175  */
176 HWTEST_F(HidebugTest, OH_HiDebug_GetAppNativeMemInfoWithCache1, TestSize.Level1)
177 {
178     HiDebug_NativeMemInfo nativeMemInfo;
179     OH_HiDebug_GetAppNativeMemInfoWithCache(&nativeMemInfo, true);
180     HiDebug_NativeMemInfo cachedNativeMemInfo;
181     OH_HiDebug_GetAppNativeMemInfoWithCache(&cachedNativeMemInfo, false);
182     ASSERT_EQ(nativeMemInfo.pss, cachedNativeMemInfo.pss);
183     ASSERT_EQ(nativeMemInfo.vss, cachedNativeMemInfo.vss);
184     ASSERT_EQ(nativeMemInfo.rss, cachedNativeMemInfo.rss);
185     ASSERT_EQ(nativeMemInfo.sharedDirty, cachedNativeMemInfo.sharedDirty);
186     ASSERT_EQ(nativeMemInfo.privateDirty, cachedNativeMemInfo.privateDirty);
187     ASSERT_EQ(nativeMemInfo.sharedClean, cachedNativeMemInfo.sharedClean);
188     ASSERT_EQ(nativeMemInfo.privateClean, cachedNativeMemInfo.privateClean);
189 }
190 
191 /**
192  * @tc.name: OH_HiDebug_GetSystemMemInfo1
193  * @tc.desc: test OH_HiDebug_GetSystemMemInfo. get system memory info
194  * @tc.type: FUNC
195  */
196 HWTEST_F(HidebugTest, OH_HiDebug_GetSystemMemInfo1, TestSize.Level1)
197 {
198     OH_HiDebug_GetSystemMemInfo(nullptr);
199     HiDebug_SystemMemInfo systemMemInfo;
200     OH_HiDebug_GetSystemMemInfo(&systemMemInfo);
201     ASSERT_GE(systemMemInfo.totalMem, 0);
202     ASSERT_GE(systemMemInfo.freeMem, 0);
203     ASSERT_GE(systemMemInfo.availableMem, 0);
204 }
205 
206 /**
207  * @tc.name: OH_HiDebug_StartAppTraceCapture1
208  * @tc.desc: test OH_HiDebug_StartAppTraceCapture. start app capture trace
209  * @tc.type: FUNC
210  */
211 HWTEST_F(HidebugTest, OH_HiDebug_StartAppTraceCapture1, TestSize.Level1)
212 {
213     uint32_t fileLength = 256;
214     char fileName[256] = {0};
215     HiDebug_TraceFlag flag = HIDEBUG_TRACE_FLAG_MAIN_THREAD;
216     uint64_t tags = HIDEBUG_TRACE_TAG_COMMON_LIBRARY;
217     uint32_t limitSize = 1024 * 1024;
218     const char* targetPath = "/data/storage/el2/log";
219     auto captureResult = OH_HiDebug_StartAppTraceCapture(flag, tags, limitSize, fileName, fileLength);
220     if (std::filesystem::exists(targetPath)) {
221         EXPECT_EQ(captureResult, HIDEBUG_SUCCESS);
222         EXPECT_GT(sizeof(fileName) / sizeof(fileName[0]), 1);
223         EXPECT_EQ(OH_HiDebug_StopAppTraceCapture(), HIDEBUG_SUCCESS);
224     } else {
225         EXPECT_EQ(captureResult, HIDEBUG_NO_PERMISSION);
226         EXPECT_EQ(OH_HiDebug_StopAppTraceCapture(), HIDEBUG_NO_TRACE_RUNNING);
227     }
228 }
229 
230 /**
231  * @tc.name: OH_HiDebug_StartAppTraceCapture2
232  * @tc.desc: test OH_HiDebug_StartAppTraceCapture. repeat start app capture trace
233  * @tc.type: FUNC
234  */
235 HWTEST_F(HidebugTest, OH_HiDebug_StartAppTraceCapture2, TestSize.Level1)
236 {
237     uint32_t fileLength = 256;
238     char fileName[256] = {0};
239     HiDebug_TraceFlag flag = HIDEBUG_TRACE_FLAG_MAIN_THREAD;
240     uint64_t tags = HIDEBUG_TRACE_TAG_COMMON_LIBRARY;
241     uint32_t limitSize = 1024 * 1024;
242     const char* targetPath = "/data/storage/el2/log";
243     auto captureResult = OH_HiDebug_StartAppTraceCapture(flag, tags, limitSize, fileName, fileLength);
244     if (std::filesystem::exists(targetPath)) {
245         EXPECT_EQ(captureResult, HIDEBUG_SUCCESS);
246         auto captureResult2 = OH_HiDebug_StartAppTraceCapture(flag, tags, limitSize, fileName, fileLength);
247         EXPECT_EQ(captureResult2, HIDEBUG_TRACE_CAPTURED_ALREADY);
248         EXPECT_GT(sizeof(fileName) / sizeof(fileName[0]), 1);
249         EXPECT_EQ(OH_HiDebug_StopAppTraceCapture(), HIDEBUG_SUCCESS);
250     } else {
251         EXPECT_EQ(captureResult, HIDEBUG_NO_PERMISSION);
252         EXPECT_EQ(OH_HiDebug_StopAppTraceCapture(), HIDEBUG_NO_TRACE_RUNNING);
253     }
254 }
255 
256 /**
257  * @tc.name: OH_HiDebug_StartAppTraceCapture3
258  * @tc.desc: test OH_HiDebug_StartAppTraceCapture. repeat stop app capture trace
259  * @tc.type: FUNC
260  */
261 HWTEST_F(HidebugTest, OH_HiDebug_StartAppTraceCapture3, TestSize.Level1)
262 {
263     uint32_t fileLength = 256;
264     char fileName[256] = {0};
265     HiDebug_TraceFlag flag = HIDEBUG_TRACE_FLAG_MAIN_THREAD;
266     uint64_t tags = HIDEBUG_TRACE_TAG_COMMON_LIBRARY;
267     uint32_t limitSize = 1024 * 1024;
268     const char* targetPath = "/data/storage/el2/log";
269     auto captureResult = OH_HiDebug_StartAppTraceCapture(flag, tags, limitSize, fileName, fileLength);
270     if (std::filesystem::exists(targetPath)) {
271         EXPECT_EQ(captureResult, HIDEBUG_SUCCESS);
272         EXPECT_GT(sizeof(fileName) / sizeof(fileName[0]), 1);
273         EXPECT_EQ(OH_HiDebug_StopAppTraceCapture(), HIDEBUG_SUCCESS);
274         EXPECT_EQ(OH_HiDebug_StopAppTraceCapture(), HIDEBUG_NO_TRACE_RUNNING);
275     } else {
276         EXPECT_EQ(captureResult, HIDEBUG_NO_PERMISSION);
277         EXPECT_EQ(OH_HiDebug_StopAppTraceCapture(), HIDEBUG_NO_TRACE_RUNNING);
278     }
279 }
280 
281 /**
282  * @tc.name: OH_HiDebug_GetGraphicsMemory
283  * @tc.desc: test OH_HiDebug_GetGraphicsMemory. get graphics memory.
284  * @tc.type: FUNC
285  */
286 HWTEST_F(HidebugTest, OH_HiDebug_GetGraphicsMemory, TestSize.Level1)
287 {
288     uint32_t value = 0;
289     EXPECT_EQ(OH_HiDebug_GetGraphicsMemory(&value), HIDEBUG_SUCCESS);
290     EXPECT_EQ(OH_HiDebug_GetGraphicsMemory(nullptr), HIDEBUG_INVALID_ARGUMENT);
291     EXPECT_GE(value, 0);
292 }
293 
294 /**
295  * @tc.name: OH_HiDebug_SetMallocDispatchTable
296  * @tc.desc: test OH_HiDebug_SetMallocDispatchTable. set malloc dispatch table.
297  * @tc.type: FUNC
298  */
299 HWTEST_F(HidebugTest, OH_HiDebug_SetMallocDispatchTable, TestSize.Level1)
300 {
301     HiDebug_MallocDispatch* original = (HiDebug_MallocDispatch*)OH_HiDebug_GetDefaultMallocDispatchTable();
302     EXPECT_NE(original, nullptr);
303     HiDebug_MallocDispatch* current = (HiDebug_MallocDispatch*)original->malloc(sizeof(HiDebug_MallocDispatch));
304     EXPECT_NE(current, nullptr);
305     memset_s(current, sizeof(HiDebug_MallocDispatch), 0, sizeof(HiDebug_MallocDispatch));
306     current->malloc = TestCustomMalloc;
307     current->free = TestCustomFree;
308     EXPECT_EQ(OH_HiDebug_SetMallocDispatchTable(nullptr), HIDEBUG_INVALID_ARGUMENT);
309     EXPECT_EQ(OH_HiDebug_SetMallocDispatchTable(current), HIDEBUG_SUCCESS);
310     int* temp = (int*)malloc(sizeof(int));
311     *temp = 8;
312     int* temp2 = (int*)malloc(sizeof(int));
313     *temp2 = 10;
314     int ret = *temp2 + *temp;
315     EXPECT_EQ(ret, 18);
316     free(temp);
317     free(temp2);
318     DesCustomMalloc(current);
319 }
320 
321 /**
322  * @tc.name: OH_HiDebug_SetMallocDispatchTableMmap test mmap and munmap
323  * @tc.desc: test OH_HiDebug_SetMallocDispatchTable. set malloc dispatch table.
324  * @tc.type: FUNC
325  */
326 HWTEST_F(HidebugTest, OH_HiDebug_SetMallocDispatchTableMmap, TestSize.Level1)
327 {
328     HiDebug_MallocDispatch* original = (HiDebug_MallocDispatch*)OH_HiDebug_GetDefaultMallocDispatchTable();
329     EXPECT_NE(original, nullptr);
330     HiDebug_MallocDispatch* current = (HiDebug_MallocDispatch*)original->malloc(sizeof(HiDebug_MallocDispatch));
331     EXPECT_NE(current, nullptr);
332     memset_s(current, sizeof(HiDebug_MallocDispatch), 0, sizeof(HiDebug_MallocDispatch));
333     current->mmap = TestCustomMmap;
334     current->munmap = TestCustomMunmap;
335     EXPECT_EQ(OH_HiDebug_SetMallocDispatchTable(current), HIDEBUG_SUCCESS);
336     char* tempMap = nullptr;
337     tempMap = (char*)mmap(NULL, 100, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
338     EXPECT_NE(tempMap, nullptr);
339     if (sprintf_s(tempMap, 100, "%s", "hi, this is test mmap") > 0) {
340         EXPECT_EQ(std::string(tempMap), "hi, this is test mmap");
341     }
342     munmap(tempMap, 100);
343     DesCustomMalloc(current);
344 }
345 
346 /**
347  * @tc.name: OH_HiDebug_SetMallocDispatchTableMmap test mmap and munmap
348  * @tc.desc: test OH_HiDebug_SetMallocDispatchTable. set malloc dispatch table.
349  * @tc.type: FUNC
350  */
351 HWTEST_F(HidebugTest, OH_HiDebug_SetMallocDispatchTableAlloc, TestSize.Level1)
352 {
353     HiDebug_MallocDispatch* original = (HiDebug_MallocDispatch*)OH_HiDebug_GetDefaultMallocDispatchTable();
354     EXPECT_NE(original, nullptr);
355     HiDebug_MallocDispatch* current = (HiDebug_MallocDispatch*)original->malloc(sizeof(HiDebug_MallocDispatch));
356     EXPECT_NE(current, nullptr);
357     memset_s(current, sizeof(HiDebug_MallocDispatch), 0, sizeof(HiDebug_MallocDispatch));
358     current->calloc = TestCustomCalloc;
359     current->realloc = TestCustomRealloc;
360     EXPECT_EQ(OH_HiDebug_SetMallocDispatchTable(current), HIDEBUG_SUCCESS);
361     int* tempCalloc = (int*)calloc(10, sizeof(int));
362     EXPECT_NE(tempCalloc, nullptr);
363     *tempCalloc = 10;
364     EXPECT_EQ(*tempCalloc, 10);
365     int* temp = (int*)realloc(tempCalloc, 20 * sizeof(int));
366     EXPECT_NE(temp, nullptr);
367     *temp = 20;
368     EXPECT_EQ(*temp, 20);
369     free(temp);
370     DesCustomMalloc(current);
371 }
372 
373 } // namespace
374