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