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