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 
24 using namespace testing::ext;
25 using namespace std;
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 class CommonCutilTest : public testing::Test {
30 public:
SetUpTestCase(void)31     static void SetUpTestCase(void) {}
TearDownTestCase(void)32     static void TearDownTestCase(void) {}
SetUp()33     void SetUp() {}
TearDown()34     void TearDown() {}
35 };
36 
37 namespace {
38 /**
39  * @tc.name: DfxCutilTest001
40  * @tc.desc: test cutil functions
41  * @tc.type: FUNC
42  */
43 HWTEST_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  */
60 HWTEST_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  */
72 HWTEST_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  */
86 HWTEST_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  */
98 HWTEST_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: ParseSiValueTest001
111  * @tc.desc: test StartsWith functions
112  * @tc.type: FUNC
113  */
114 HWTEST_F(CommonCutilTest, ParseSiValueTest001, TestSize.Level2)
115 {
116     GTEST_LOG_(INFO) << "ParseSiValueTest001: start.";
117     siginfo_t si = {0};
118     const int flagOffset = 63;
119     uint64_t timeout;
120     int tid;
121 
122     uint64_t data = 100;
123     si.si_value.sival_int = data;
124     ParseSiValue(&si, &timeout, &tid);
125     ASSERT_EQ(tid, 100);
126     ASSERT_EQ(timeout, 0);
127 
128     data |= 1ULL << flagOffset;
129     si.si_value.sival_ptr = (void*)(data);
130     ParseSiValue(&si, &timeout, &tid);
131 
132     if (sizeof(void *) == sizeof(int)) {
133         ASSERT_EQ(tid, 100);
134         ASSERT_EQ(timeout, 0);
135         GTEST_LOG_(INFO) << "ParseSiValueTest001: end.";
136         return;
137     }
138 
139     ASSERT_EQ(tid, 0);
140     ASSERT_EQ(timeout, 100);
141 
142     data = 0xFFFFFFFAAAAAAAA;
143     si.si_value.sival_ptr = (void*)(data);
144     ParseSiValue(&si, &timeout, &tid);
145     ASSERT_EQ(tid, 0XAAAAAAAA);
146     ASSERT_EQ(timeout, 0);
147 
148     data |= 1ULL << flagOffset;
149     si.si_value.sival_ptr = (void*)(data);
150     ParseSiValue(&si, &timeout, &tid);
151     ASSERT_EQ(tid, 0);
152     ASSERT_EQ(timeout, data & ~(1ULL << flagOffset));
153 }
154 
155 /**
156  * @tc.name: TraceTest001
157  * @tc.desc: test Trace functions DfxStartTraceDlsym
158  * @tc.type: FUNC
159  */
160 HWTEST_F(CommonCutilTest, TraceTest001, TestSize.Level2)
161 {
162     GTEST_LOG_(INFO) << "TraceTest001: start.";
163     DfxEnableTraceDlsym(true);
164     char *name = nullptr;
165     DfxStartTraceDlsym(name);
166     FormatTraceName(nullptr, 0, nullptr);
167     const size_t size = 2;
168     FormatTraceName(nullptr, size, nullptr);
169     ASSERT_EQ(name, nullptr);
170     DfxEnableTraceDlsym(false);
171     DfxStartTraceDlsym(name);
172     ASSERT_EQ(name, nullptr);
173     GTEST_LOG_(INFO) << "TraceTest001: end.";
174 }
175 }
176 } // namespace HiviewDFX
177 } // namespace OHOS