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 #include <securec.h> 18 #include <string> 19 #include <vector> 20 #include "dfx_cutil.h" 21 #include "dfx_define.h" 22 23 using namespace testing::ext; 24 using namespace std; 25 26 namespace OHOS { 27 namespace HiviewDFX { 28 class CommonCutilTest : public testing::Test { 29 public: SetUpTestCase(void)30 static void SetUpTestCase(void) {} TearDownTestCase(void)31 static void TearDownTestCase(void) {} SetUp()32 void SetUp() {} TearDown()33 void TearDown() {} 34 }; 35 36 namespace { 37 /** 38 * @tc.name: DfxCutilTest001 39 * @tc.desc: test cutil functions 40 * @tc.type: FUNC 41 */ 42 HWTEST_F(CommonCutilTest, DfxCutilTest001, TestSize.Level2) 43 { 44 GTEST_LOG_(INFO) << "DfxCutilTest001: start."; 45 char threadName[NAME_BUF_LEN]; 46 char processName[NAME_BUF_LEN]; 47 ASSERT_TRUE(GetThreadName(threadName, sizeof(threadName))); 48 ASSERT_TRUE(GetThreadNameByTid(gettid(), threadName, sizeof(threadName))); 49 ASSERT_TRUE(GetProcessName(processName, sizeof(processName))); 50 ASSERT_GT(GetRealPid(), 0); 51 GTEST_LOG_(INFO) << "DfxCutilTest001: end."; 52 } 53 54 /** 55 * @tc.name: DfxCutilTest002 56 * @tc.desc: test cutil functions GetThreadName null 57 * @tc.type: FUNC 58 */ 59 HWTEST_F(CommonCutilTest, DfxCutilTest002, TestSize.Level2) 60 { 61 GTEST_LOG_(INFO) << "DfxCutilTest002: start."; 62 ASSERT_FALSE(GetThreadName(nullptr, 0)); 63 GTEST_LOG_(INFO) << "DfxCutilTest002: end."; 64 } 65 66 /** 67 * @tc.name: DfxCutilTest003 68 * @tc.desc: test cutil functions GetTimeMilliseconds 69 * @tc.type: FUNC 70 */ 71 HWTEST_F(CommonCutilTest, DfxCutilTest003, TestSize.Level2) 72 { 73 GTEST_LOG_(INFO) << "DfxCutilTest003: start."; 74 uint64_t msNow = GetTimeMilliseconds(); 75 GTEST_LOG_(INFO) << "current time(ms):" << msNow; 76 ASSERT_NE(msNow, 0); 77 GTEST_LOG_(INFO) << "DfxCutilTest003: end."; 78 } 79 80 /** 81 * @tc.name: DfxCutilTest004 82 * @tc.desc: test cutil functions TrimAndDupStr 83 * @tc.type: FUNC 84 */ 85 HWTEST_F(CommonCutilTest, DfxCutilTest004, TestSize.Level2) 86 { 87 GTEST_LOG_(INFO) << "DfxCutilTest004: start."; 88 ASSERT_FALSE(TrimAndDupStr(nullptr, nullptr)); 89 GTEST_LOG_(INFO) << "DfxCutilTest004: end."; 90 } 91 92 /** 93 * @tc.name: DfxCutilTest005 94 * @tc.desc: test cutil functions TrimAndDupStr 95 * @tc.type: FUNC 96 */ 97 HWTEST_F(CommonCutilTest, DfxCutilTest005, TestSize.Level2) 98 { 99 GTEST_LOG_(INFO) << "DfxCutilTest005: start."; 100 const char src[] = "ab cd \n ef"; 101 char dst[11] = {0}; // 11: The length is consistent with the src[] array 102 ASSERT_TRUE(TrimAndDupStr(src, dst)); 103 GTEST_LOG_(INFO) << "dst:" << dst; 104 ASSERT_EQ(strncmp(dst, "abcd", 5), 0); // 5:length of "abcd" 105 GTEST_LOG_(INFO) << "DfxCutilTest005: end."; 106 } 107 } 108 } // namespace HiviewDFX 109 } // namespace OHOS