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#include "dfx_trace_dlsym.h" 23 24using namespace testing::ext; 25using namespace std; 26 27namespace OHOS { 28namespace HiviewDFX { 29class CommonCutilTest : public testing::Test { 30public: 31 static void SetUpTestCase(void) {} 32 static void TearDownTestCase(void) {} 33 void SetUp() {} 34 void TearDown() {} 35}; 36 37namespace { 38/** 39 * @tc.name: DfxCutilTest001 40 * @tc.desc: test cutil functions 41 * @tc.type: FUNC 42 */ 43HWTEST_F(CommonCutilTest, DfxCutilTest001, TestSize.Level2) 44{ 45 GTEST_LOG_(INFO) << "DfxCutilTest001: start."; 46 char threadName[NAME_BUF_LEN]; 47 char processName[NAME_BUF_LEN]; 48 ASSERT_TRUE(GetThreadName(threadName, sizeof(threadName))); 49 ASSERT_TRUE(GetThreadNameByTid(gettid(), threadName, sizeof(threadName))); 50 ASSERT_TRUE(GetProcessName(processName, sizeof(processName))); 51 ASSERT_GT(GetRealPid(), 0); 52 GTEST_LOG_(INFO) << "DfxCutilTest001: end."; 53} 54 55/** 56 * @tc.name: DfxCutilTest002 57 * @tc.desc: test cutil functions GetThreadName null 58 * @tc.type: FUNC 59 */ 60HWTEST_F(CommonCutilTest, DfxCutilTest002, TestSize.Level2) 61{ 62 GTEST_LOG_(INFO) << "DfxCutilTest002: start."; 63 ASSERT_FALSE(GetThreadName(nullptr, 0)); 64 GTEST_LOG_(INFO) << "DfxCutilTest002: end."; 65} 66 67/** 68 * @tc.name: DfxCutilTest003 69 * @tc.desc: test cutil functions GetTimeMilliseconds 70 * @tc.type: FUNC 71 */ 72HWTEST_F(CommonCutilTest, DfxCutilTest003, TestSize.Level2) 73{ 74 GTEST_LOG_(INFO) << "DfxCutilTest003: start."; 75 uint64_t msNow = GetTimeMilliseconds(); 76 GTEST_LOG_(INFO) << "current time(ms):" << msNow; 77 ASSERT_NE(msNow, 0); 78 GTEST_LOG_(INFO) << "DfxCutilTest003: end."; 79} 80 81/** 82 * @tc.name: DfxCutilTest004 83 * @tc.desc: test cutil functions TrimAndDupStr 84 * @tc.type: FUNC 85 */ 86HWTEST_F(CommonCutilTest, DfxCutilTest004, TestSize.Level2) 87{ 88 GTEST_LOG_(INFO) << "DfxCutilTest004: start."; 89 ASSERT_FALSE(TrimAndDupStr(nullptr, nullptr)); 90 GTEST_LOG_(INFO) << "DfxCutilTest004: end."; 91} 92 93/** 94 * @tc.name: DfxCutilTest005 95 * @tc.desc: test cutil functions TrimAndDupStr 96 * @tc.type: FUNC 97 */ 98HWTEST_F(CommonCutilTest, DfxCutilTest005, TestSize.Level2) 99{ 100 GTEST_LOG_(INFO) << "DfxCutilTest005: start."; 101 const char src[] = "ab cd \n ef"; 102 char dst[11] = {0}; // 11: The length is consistent with the src[] array 103 ASSERT_TRUE(TrimAndDupStr(src, dst)); 104 GTEST_LOG_(INFO) << "dst:" << dst; 105 ASSERT_EQ(strncmp(dst, "abcd", 5), 0); // 5:length of "abcd" 106 GTEST_LOG_(INFO) << "DfxCutilTest005: end."; 107} 108 109/** 110 * @tc.name: TraceTest001 111 * @tc.desc: test Trace functions DfxStartTraceDlsym 112 * @tc.type: FUNC 113 */ 114HWTEST_F(CommonCutilTest, TraceTest001, TestSize.Level2) 115{ 116 GTEST_LOG_(INFO) << "TraceTest001: start."; 117 DfxEnableTraceDlsym(true); 118 char *name = nullptr; 119 DfxStartTraceDlsym(name); 120 FormatTraceName(nullptr, 0, nullptr); 121 const size_t size = 2; 122 FormatTraceName(nullptr, size, nullptr); 123 ASSERT_EQ(name, nullptr); 124 DfxEnableTraceDlsym(false); 125 DfxStartTraceDlsym(name); 126 ASSERT_EQ(name, nullptr); 127 GTEST_LOG_(INFO) << "TraceTest001: end."; 128} 129 130/** 131 * @tc.name: ParseSiValueTest001 132 * @tc.desc: test StartsWith functions 133 * @tc.type: FUNC 134 */ 135HWTEST_F(CommonCutilTest, ParseSiValueTest001, TestSize.Level2) 136{ 137 GTEST_LOG_(INFO) << "ParseSiValueTest001: start."; 138 siginfo_t si = {0}; 139 const int flagOffset = 63; 140 uint64_t timeout; 141 int tid; 142 143 uint64_t data = 100; 144 si.si_value.sival_int = data; 145 ParseSiValue(&si, &timeout, &tid); 146 ASSERT_EQ(tid, 100); 147 ASSERT_EQ(timeout, 0); 148 149 data |= 1ULL << flagOffset; 150 si.si_value.sival_ptr = (void*)(data); 151 ParseSiValue(&si, &timeout, &tid); 152 153 if (sizeof(void *) == sizeof(int)) { 154 ASSERT_EQ(tid, 100); 155 ASSERT_EQ(timeout, 0); 156 GTEST_LOG_(INFO) << "ParseSiValueTest001: end."; 157 return; 158 } 159 160 ASSERT_EQ(tid, 0); 161 ASSERT_EQ(timeout, 100); 162 163 data = 0xFFFFFFFAAAAAAAA; 164 si.si_value.sival_ptr = (void*)(data); 165 ParseSiValue(&si, &timeout, &tid); 166 ASSERT_EQ(tid, 0XAAAAAAAA); 167 ASSERT_EQ(timeout, 0); 168 169 data |= 1ULL << flagOffset; 170 si.si_value.sival_ptr = (void*)(data); 171 ParseSiValue(&si, &timeout, &tid); 172 ASSERT_EQ(tid, 0); 173 ASSERT_EQ(timeout, data & ~(1ULL << flagOffset)); 174} 175} 176} // namespace HiviewDFX 177} // namespace OHOS