1 /*
2 * Copyright (c) 2021-2022 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 <vector>
17
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20
21 #include "dwarf_test.h"
22
23 using namespace testing::ext;
24 using namespace std;
25 // using namespace OHOS::HiviewDFX;
26 namespace OHOS {
27 namespace Developtools {
28 namespace HiPerf {
29 namespace {
30 const unsigned char absptr {0xff};
31 constexpr int data2Size {2};
32 const unsigned char data2[data2Size] {0xff, 0xff};
33 const unsigned char data4[sizeof(int32_t)] {0xff, 0xff, 0xff, 0xff};
34 const unsigned char data8[sizeof(int64_t)] {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
35 constexpr int data128Size {128};
36 unsigned char data128[data128Size] {};
37 #ifdef NOT_USE
38 constexpr int num {9};
39 #else
40 constexpr int num {7};
41 #endif
42 const unsigned char *data[num] {};
43 std::vector<uint64_t> values {
44 65535ULL,
45 4294967295ULL,
46 18446744073709551615ULL,
47 18446744073709551615ULL,
48 18446744073709551615ULL,
49 18446744073709551615ULL,
50 65535ULL,
51 4294967295ULL,
52 1921026034567241472ULL,
53 18446744073709551615ULL,
54 18446744073709551615ULL,
55 1921026034567241472ULL,
56 65535ULL,
57 4294967295ULL,
58 1921010641404477184ULL,
59 18446744073709551615ULL,
60 18446744073709551615ULL,
61 1921010641404477184ULL,
62 65535ULL,
63 447241984ULL,
64 1940452206006808832ULL,
65 18446744073709551615ULL,
66 447241984ULL,
67 1940452206006808832ULL,
68 65535ULL,
69 447273728ULL,
70 1940830438011371264ULL,
71 18446744073709551615ULL,
72 447273728ULL,
73 1940830438011371264ULL,
74 65535ULL,
75 447266560ULL,
76 1940830438011385600ULL,
77 18446744073709551615ULL,
78 447266560ULL,
79 1940830438011385600ULL,
80 };
81 std::vector<dw_encode_t> vfs {
82 DW_EH_PE_absptr,
83 #ifdef NOT_USE
84 DW_EH_PE_uleb128,
85 #endif
86 DW_EH_PE_udata2, DW_EH_PE_udata4, DW_EH_PE_udata8,
87 #ifdef NOT_USE
88 DW_EH_PE_sleb128,
89 #endif
90 DW_EH_PE_sdata2, DW_EH_PE_sdata4, DW_EH_PE_sdata8,
91 };
92 vector<dw_encode_t> ehas {
93 DW_EH_PE_nothing, DW_EH_PE_pcrel, DW_EH_PE_textrel, DW_EH_PE_datarel,
94 DW_EH_PE_funcrel, DW_EH_PE_aligned, DW_EH_PE_omit,
95 };
96 } // namespace
97
98 class DwarfTest : public testing::Test {
99 public:
100 static void SetUpTestCase(void);
101 static void TearDownTestCase(void);
102 void SetUp();
103 void TearDown();
104 };
105
SetUpTestCase(void)106 void DwarfTest::SetUpTestCase(void) {}
107
TearDownTestCase(void)108 void DwarfTest::TearDownTestCase(void) {}
109
SetUp()110 void DwarfTest::SetUp()
111 {
112 for (std::size_t index = 0; index < data128Size; ++index) {
113 data128[index] = 0xff;
114 }
115 std::size_t index {0};
116 data[index++] = &absptr;
117 #ifdef NOT_USE
118 data[index++] = data128;
119 #endif
120 data[index++] = data2;
121 data[index++] = data4;
122 data[index++] = data8;
123 #ifdef NOT_USE
124 data[index++] = data128;
125 #endif
126 data[index++] = data2;
127 data[index++] = data4;
128 data[index++] = data8;
129 }
130
TearDown()131 void DwarfTest::TearDown() {}
132
133 HWTEST_F(DwarfTest, GetEnd, TestSize.Level1)
134 {
135 for (std::size_t i = 0; i < ehas.size(); ++i) {
136 for (std::size_t j = 0; j < num; ++j) {
137 {
138 dw_encode_t dwe = ehas[i] | vfs[j];
139 DwarfEncoding dw {dwe, data[j]};
140 if (!dw.IsOmit()) {
141 if (vfs[j] == DW_EH_PE_absptr) {
142 EXPECT_TRUE(data[j] == dw.GetEnd() - dw.GetSize());
143 } else {
144 EXPECT_TRUE(data[j] == dw.GetEnd());
145 }
146 }
147 }
148 }
149 }
150 }
151
152 HWTEST_F(DwarfTest, GetData, TestSize.Level1)
153 {
154 for (std::size_t i = 0; i < ehas.size(); ++i) {
155 for (std::size_t j = 0; j < num; ++j) {
156 {
157 dw_encode_t dwe = ehas[i] | vfs[j];
158 DwarfEncoding dw {dwe, data[j]};
159 if (!dw.IsOmit()) {
160 if (vfs[j] == DW_EH_PE_absptr) {
161 EXPECT_TRUE(data[j] == dw.GetData());
162 } else {
163 EXPECT_TRUE(data[j] == dw.GetData() + dw.GetSize());
164 }
165 }
166 }
167 }
168 }
169 }
170
171 HWTEST_F(DwarfTest, GetSize, TestSize.Level1)
172 {
173 for (std::size_t i = 0; i < ehas.size(); ++i) {
174 for (std::size_t j = 0; j < num; ++j) {
175 {
176 dw_encode_t dwe = ehas[i] | vfs[j];
177 DwarfEncoding dw {dwe, data[j]};
178 if (!dw.IsOmit()) {
179 EXPECT_TRUE(DWFormatSizeMap.at(vfs[j]) == dw.GetSize());
180 }
181 }
182 }
183 }
184 }
185
186 HWTEST_F(DwarfTest, IsOmit, TestSize.Level1)
187 {
188 for (std::size_t i = 0; i < ehas.size(); ++i) {
189 for (std::size_t j = 0; j < num; ++j) {
190 {
191 dw_encode_t dwe = ehas[i] | vfs[j];
192 DwarfEncoding dw {dwe, data[j]};
193 if (ehas[i] == DW_EH_PE_omit) {
194 EXPECT_TRUE(dw.IsOmit());
195 } else {
196 EXPECT_FALSE(dw.IsOmit());
197 }
198 }
199 }
200 }
201 }
202 } // namespace HiPerf
203 } // namespace Developtools
204 } // namespace OHOS
205