• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: TraceTest001
83  * @tc.desc: test Trace functions DfxStartTraceDlsym
84  * @tc.type: FUNC
85  */
86 HWTEST_F(CommonCutilTest, TraceTest001, TestSize.Level2)
87 {
88     GTEST_LOG_(INFO) << "TraceTest001: start.";
89     DfxEnableTraceDlsym(true);
90     char *name = nullptr;
91     DfxStartTraceDlsym(name);
92     FormatTraceName(nullptr, 0, nullptr);
93     const size_t size = 2;
94     FormatTraceName(nullptr, size, nullptr);
95     ASSERT_EQ(name, nullptr);
96     DfxEnableTraceDlsym(false);
97     DfxStartTraceDlsym(name);
98     ASSERT_EQ(name, nullptr);
99     GTEST_LOG_(INFO) << "TraceTest001: end.";
100 }
101 
102 /**
103  * @tc.name: ParseSiValueTest001
104  * @tc.desc: test StartsWith functions
105  * @tc.type: FUNC
106  */
107 HWTEST_F(CommonCutilTest, ParseSiValueTest001, TestSize.Level0)
108 {
109     GTEST_LOG_(INFO) << "ParseSiValueTest001: start.";
110     siginfo_t si = {0};
111     const int flagOffset = 63;
112     uint64_t timeout;
113     int tid;
114 
115     uint64_t data = 100;
116     si.si_value.sival_int = data;
117     ParseSiValue(&si, &timeout, &tid);
118     ASSERT_EQ(tid, 100);
119     ASSERT_EQ(timeout, 0);
120 
121     data |= 1ULL << flagOffset;
122     si.si_value.sival_ptr = (void*)(data);
123     ParseSiValue(&si, &timeout, &tid);
124 
125     if (sizeof(void *) == sizeof(int)) {
126         ASSERT_EQ(tid, 100);
127         ASSERT_EQ(timeout, 0);
128         GTEST_LOG_(INFO) << "ParseSiValueTest001: end.";
129     } else {
130         ASSERT_EQ(tid, 0);
131         ASSERT_EQ(timeout, 100);
132 
133         data = 0xFFFFFFFAAAAAAAA;
134         si.si_value.sival_ptr = (void*)(data);
135         ParseSiValue(&si, &timeout, &tid);
136         ASSERT_EQ(tid, 0XAAAAAAAA);
137         ASSERT_EQ(timeout, 0);
138 
139         data |= 1ULL << flagOffset;
140         si.si_value.sival_ptr = (void*)(data);
141         ParseSiValue(&si, &timeout, &tid);
142         ASSERT_EQ(tid, 0);
143         ASSERT_EQ(timeout, data & ~(1ULL << flagOffset));
144     }
145 }
146 }
147 } // namespace HiviewDFX
148 } // namespace OHOS