1800b99b8Sopenharmony_ci/* 2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License. 5800b99b8Sopenharmony_ci * You may obtain a copy of the License at 6800b99b8Sopenharmony_ci * 7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8800b99b8Sopenharmony_ci * 9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and 13800b99b8Sopenharmony_ci * limitations under the License. 14800b99b8Sopenharmony_ci */ 15800b99b8Sopenharmony_ci 16800b99b8Sopenharmony_ci#include <gtest/gtest.h> 17800b99b8Sopenharmony_ci#include <securec.h> 18800b99b8Sopenharmony_ci#include <string> 19800b99b8Sopenharmony_ci#include <vector> 20800b99b8Sopenharmony_ci#include "dfx_cutil.h" 21800b99b8Sopenharmony_ci#include "dfx_define.h" 22800b99b8Sopenharmony_ci#include "dfx_trace_dlsym.h" 23800b99b8Sopenharmony_ci 24800b99b8Sopenharmony_ciusing namespace testing::ext; 25800b99b8Sopenharmony_ciusing namespace std; 26800b99b8Sopenharmony_ci 27800b99b8Sopenharmony_cinamespace OHOS { 28800b99b8Sopenharmony_cinamespace HiviewDFX { 29800b99b8Sopenharmony_ciclass CommonCutilTest : public testing::Test { 30800b99b8Sopenharmony_cipublic: 31800b99b8Sopenharmony_ci static void SetUpTestCase(void) {} 32800b99b8Sopenharmony_ci static void TearDownTestCase(void) {} 33800b99b8Sopenharmony_ci void SetUp() {} 34800b99b8Sopenharmony_ci void TearDown() {} 35800b99b8Sopenharmony_ci}; 36800b99b8Sopenharmony_ci 37800b99b8Sopenharmony_cinamespace { 38800b99b8Sopenharmony_ci/** 39800b99b8Sopenharmony_ci * @tc.name: DfxCutilTest001 40800b99b8Sopenharmony_ci * @tc.desc: test cutil functions 41800b99b8Sopenharmony_ci * @tc.type: FUNC 42800b99b8Sopenharmony_ci */ 43800b99b8Sopenharmony_ciHWTEST_F(CommonCutilTest, DfxCutilTest001, TestSize.Level2) 44800b99b8Sopenharmony_ci{ 45800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DfxCutilTest001: start."; 46800b99b8Sopenharmony_ci char threadName[NAME_BUF_LEN]; 47800b99b8Sopenharmony_ci char processName[NAME_BUF_LEN]; 48800b99b8Sopenharmony_ci ASSERT_TRUE(GetThreadName(threadName, sizeof(threadName))); 49800b99b8Sopenharmony_ci ASSERT_TRUE(GetThreadNameByTid(gettid(), threadName, sizeof(threadName))); 50800b99b8Sopenharmony_ci ASSERT_TRUE(GetProcessName(processName, sizeof(processName))); 51800b99b8Sopenharmony_ci ASSERT_GT(GetRealPid(), 0); 52800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DfxCutilTest001: end."; 53800b99b8Sopenharmony_ci} 54800b99b8Sopenharmony_ci 55800b99b8Sopenharmony_ci/** 56800b99b8Sopenharmony_ci * @tc.name: DfxCutilTest002 57800b99b8Sopenharmony_ci * @tc.desc: test cutil functions GetThreadName null 58800b99b8Sopenharmony_ci * @tc.type: FUNC 59800b99b8Sopenharmony_ci */ 60800b99b8Sopenharmony_ciHWTEST_F(CommonCutilTest, DfxCutilTest002, TestSize.Level2) 61800b99b8Sopenharmony_ci{ 62800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DfxCutilTest002: start."; 63800b99b8Sopenharmony_ci ASSERT_FALSE(GetThreadName(nullptr, 0)); 64800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DfxCutilTest002: end."; 65800b99b8Sopenharmony_ci} 66800b99b8Sopenharmony_ci 67800b99b8Sopenharmony_ci/** 68800b99b8Sopenharmony_ci * @tc.name: DfxCutilTest003 69800b99b8Sopenharmony_ci * @tc.desc: test cutil functions GetTimeMilliseconds 70800b99b8Sopenharmony_ci * @tc.type: FUNC 71800b99b8Sopenharmony_ci */ 72800b99b8Sopenharmony_ciHWTEST_F(CommonCutilTest, DfxCutilTest003, TestSize.Level2) 73800b99b8Sopenharmony_ci{ 74800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DfxCutilTest003: start."; 75800b99b8Sopenharmony_ci uint64_t msNow = GetTimeMilliseconds(); 76800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "current time(ms):" << msNow; 77800b99b8Sopenharmony_ci ASSERT_NE(msNow, 0); 78800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DfxCutilTest003: end."; 79800b99b8Sopenharmony_ci} 80800b99b8Sopenharmony_ci 81800b99b8Sopenharmony_ci/** 82800b99b8Sopenharmony_ci * @tc.name: DfxCutilTest004 83800b99b8Sopenharmony_ci * @tc.desc: test cutil functions TrimAndDupStr 84800b99b8Sopenharmony_ci * @tc.type: FUNC 85800b99b8Sopenharmony_ci */ 86800b99b8Sopenharmony_ciHWTEST_F(CommonCutilTest, DfxCutilTest004, TestSize.Level2) 87800b99b8Sopenharmony_ci{ 88800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DfxCutilTest004: start."; 89800b99b8Sopenharmony_ci ASSERT_FALSE(TrimAndDupStr(nullptr, nullptr)); 90800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DfxCutilTest004: end."; 91800b99b8Sopenharmony_ci} 92800b99b8Sopenharmony_ci 93800b99b8Sopenharmony_ci/** 94800b99b8Sopenharmony_ci * @tc.name: DfxCutilTest005 95800b99b8Sopenharmony_ci * @tc.desc: test cutil functions TrimAndDupStr 96800b99b8Sopenharmony_ci * @tc.type: FUNC 97800b99b8Sopenharmony_ci */ 98800b99b8Sopenharmony_ciHWTEST_F(CommonCutilTest, DfxCutilTest005, TestSize.Level2) 99800b99b8Sopenharmony_ci{ 100800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DfxCutilTest005: start."; 101800b99b8Sopenharmony_ci const char src[] = "ab cd \n ef"; 102800b99b8Sopenharmony_ci char dst[11] = {0}; // 11: The length is consistent with the src[] array 103800b99b8Sopenharmony_ci ASSERT_TRUE(TrimAndDupStr(src, dst)); 104800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "dst:" << dst; 105800b99b8Sopenharmony_ci ASSERT_EQ(strncmp(dst, "abcd", 5), 0); // 5:length of "abcd" 106800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DfxCutilTest005: end."; 107800b99b8Sopenharmony_ci} 108800b99b8Sopenharmony_ci 109800b99b8Sopenharmony_ci/** 110800b99b8Sopenharmony_ci * @tc.name: TraceTest001 111800b99b8Sopenharmony_ci * @tc.desc: test Trace functions DfxStartTraceDlsym 112800b99b8Sopenharmony_ci * @tc.type: FUNC 113800b99b8Sopenharmony_ci */ 114800b99b8Sopenharmony_ciHWTEST_F(CommonCutilTest, TraceTest001, TestSize.Level2) 115800b99b8Sopenharmony_ci{ 116800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "TraceTest001: start."; 117800b99b8Sopenharmony_ci DfxEnableTraceDlsym(true); 118800b99b8Sopenharmony_ci char *name = nullptr; 119800b99b8Sopenharmony_ci DfxStartTraceDlsym(name); 120800b99b8Sopenharmony_ci FormatTraceName(nullptr, 0, nullptr); 121800b99b8Sopenharmony_ci const size_t size = 2; 122800b99b8Sopenharmony_ci FormatTraceName(nullptr, size, nullptr); 123800b99b8Sopenharmony_ci ASSERT_EQ(name, nullptr); 124800b99b8Sopenharmony_ci DfxEnableTraceDlsym(false); 125800b99b8Sopenharmony_ci DfxStartTraceDlsym(name); 126800b99b8Sopenharmony_ci ASSERT_EQ(name, nullptr); 127800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "TraceTest001: end."; 128800b99b8Sopenharmony_ci} 129800b99b8Sopenharmony_ci 130800b99b8Sopenharmony_ci/** 131800b99b8Sopenharmony_ci * @tc.name: ParseSiValueTest001 132800b99b8Sopenharmony_ci * @tc.desc: test StartsWith functions 133800b99b8Sopenharmony_ci * @tc.type: FUNC 134800b99b8Sopenharmony_ci */ 135800b99b8Sopenharmony_ciHWTEST_F(CommonCutilTest, ParseSiValueTest001, TestSize.Level2) 136800b99b8Sopenharmony_ci{ 137800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ParseSiValueTest001: start."; 138800b99b8Sopenharmony_ci siginfo_t si = {0}; 139800b99b8Sopenharmony_ci const int flagOffset = 63; 140800b99b8Sopenharmony_ci uint64_t timeout; 141800b99b8Sopenharmony_ci int tid; 142800b99b8Sopenharmony_ci 143800b99b8Sopenharmony_ci uint64_t data = 100; 144800b99b8Sopenharmony_ci si.si_value.sival_int = data; 145800b99b8Sopenharmony_ci ParseSiValue(&si, &timeout, &tid); 146800b99b8Sopenharmony_ci ASSERT_EQ(tid, 100); 147800b99b8Sopenharmony_ci ASSERT_EQ(timeout, 0); 148800b99b8Sopenharmony_ci 149800b99b8Sopenharmony_ci data |= 1ULL << flagOffset; 150800b99b8Sopenharmony_ci si.si_value.sival_ptr = (void*)(data); 151800b99b8Sopenharmony_ci ParseSiValue(&si, &timeout, &tid); 152800b99b8Sopenharmony_ci 153800b99b8Sopenharmony_ci if (sizeof(void *) == sizeof(int)) { 154800b99b8Sopenharmony_ci ASSERT_EQ(tid, 100); 155800b99b8Sopenharmony_ci ASSERT_EQ(timeout, 0); 156800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ParseSiValueTest001: end."; 157800b99b8Sopenharmony_ci return; 158800b99b8Sopenharmony_ci } 159800b99b8Sopenharmony_ci 160800b99b8Sopenharmony_ci ASSERT_EQ(tid, 0); 161800b99b8Sopenharmony_ci ASSERT_EQ(timeout, 100); 162800b99b8Sopenharmony_ci 163800b99b8Sopenharmony_ci data = 0xFFFFFFFAAAAAAAA; 164800b99b8Sopenharmony_ci si.si_value.sival_ptr = (void*)(data); 165800b99b8Sopenharmony_ci ParseSiValue(&si, &timeout, &tid); 166800b99b8Sopenharmony_ci ASSERT_EQ(tid, 0XAAAAAAAA); 167800b99b8Sopenharmony_ci ASSERT_EQ(timeout, 0); 168800b99b8Sopenharmony_ci 169800b99b8Sopenharmony_ci data |= 1ULL << flagOffset; 170800b99b8Sopenharmony_ci si.si_value.sival_ptr = (void*)(data); 171800b99b8Sopenharmony_ci ParseSiValue(&si, &timeout, &tid); 172800b99b8Sopenharmony_ci ASSERT_EQ(tid, 0); 173800b99b8Sopenharmony_ci ASSERT_EQ(timeout, data & ~(1ULL << flagOffset)); 174800b99b8Sopenharmony_ci} 175800b99b8Sopenharmony_ci} 176800b99b8Sopenharmony_ci} // namespace HiviewDFX 177800b99b8Sopenharmony_ci} // namespace OHOS