1 //===-- JSONSymbolTest.cpp ------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Core/Section.h"
10 #include "lldb/Symbol/Symbol.h"
11 #include "llvm/Testing/Support/Error.h"
13 #include "gtest/gtest.h"
17 using namespace lldb_private
;
19 static std::string g_error_no_section_list
= "no section list provided";
20 static std::string g_error_both_value_and_address
=
21 "symbol cannot contain both a value and an address";
22 static std::string g_error_neither_value_or_address
=
23 "symbol must contain either a value or an address";
25 TEST(JSONSymbolTest
, DeserializeCodeAddress
) {
26 std::string text
= R
"(
34 Expected
<json::Value
> json
= json::parse(text
);
35 ASSERT_TRUE(static_cast<bool>(json
));
37 json::Path::Root root
;
38 JSONSymbol json_symbol
;
39 ASSERT_TRUE(fromJSON(*json
, json_symbol
, root
));
41 SectionSP
sect_sp(new Section(
42 /*module_sp=*/ModuleSP(),
45 /*name=*/ConstString(".text"),
46 /*sect_type=*/eSectionTypeCode
,
47 /*file_vm_addr=*/0x1000,
52 /*flags=*/0x10203040));
53 SectionList sect_list
;
54 sect_list
.AddSection(sect_sp
);
56 Expected
<Symbol
> symbol
= Symbol::FromJSON(json_symbol
, §_list
);
57 EXPECT_THAT_EXPECTED(symbol
, llvm::Succeeded());
58 EXPECT_EQ(symbol
->GetName(), ConstString("foo"));
59 EXPECT_EQ(symbol
->GetFileAddress(), static_cast<lldb::addr_t
>(0x1000));
60 EXPECT_EQ(symbol
->GetType(), eSymbolTypeCode
);
63 TEST(JSONSymbolTest
, DeserializeCodeValue
) {
64 std::string text
= R
"(
72 Expected
<json::Value
> json
= json::parse(text
);
73 EXPECT_THAT_EXPECTED(json
, llvm::Succeeded());
75 json::Path::Root root
;
76 JSONSymbol json_symbol
;
77 ASSERT_TRUE(fromJSON(*json
, json_symbol
, root
));
79 SectionList sect_list
;
81 Expected
<Symbol
> symbol
= Symbol::FromJSON(json_symbol
, §_list
);
82 EXPECT_THAT_EXPECTED(symbol
, llvm::Succeeded());
83 EXPECT_EQ(symbol
->GetName(), ConstString("foo"));
84 EXPECT_EQ(symbol
->GetRawValue(), static_cast<lldb::addr_t
>(0x1000));
85 EXPECT_EQ(symbol
->GetType(), eSymbolTypeCode
);
88 TEST(JSONSymbolTest
, JSONInvalidValueAndAddress
) {
89 std::string text
= R
"(
98 Expected
<json::Value
> json
= json::parse(text
);
99 EXPECT_THAT_EXPECTED(json
, llvm::Succeeded());
101 json::Path::Root root
;
102 JSONSymbol json_symbol
;
103 ASSERT_FALSE(fromJSON(*json
, json_symbol
, root
));
106 TEST(JSONSymbolTest
, JSONInvalidNoValueOrAddress
) {
107 std::string text
= R
"(
114 Expected
<json::Value
> json
= json::parse(text
);
115 EXPECT_THAT_EXPECTED(json
, llvm::Succeeded());
117 json::Path::Root root
;
118 JSONSymbol json_symbol
;
119 ASSERT_FALSE(fromJSON(*json
, json_symbol
, root
));
122 TEST(JSONSymbolTest
, JSONInvalidType
) {
123 std::string text
= R
"(
131 Expected
<json::Value
> json
= json::parse(text
);
132 EXPECT_THAT_EXPECTED(json
, llvm::Succeeded());
134 json::Path::Root root
;
135 JSONSymbol json_symbol
;
136 ASSERT_FALSE(fromJSON(*json
, json_symbol
, root
));
139 TEST(JSONSymbolTest
, SymbolInvalidNoSectionList
) {
140 JSONSymbol json_symbol
;
141 json_symbol
.value
= 0x1;
143 Expected
<Symbol
> symbol
= Symbol::FromJSON(json_symbol
, nullptr);
144 EXPECT_THAT_EXPECTED(symbol
,
145 llvm::FailedWithMessage(g_error_no_section_list
));
148 TEST(JSONSymbolTest
, SymbolInvalidValueAndAddress
) {
149 JSONSymbol json_symbol
;
150 json_symbol
.value
= 0x1;
151 json_symbol
.address
= 0x2;
153 SectionList sect_list
;
155 Expected
<Symbol
> symbol
= Symbol::FromJSON(json_symbol
, §_list
);
156 EXPECT_THAT_EXPECTED(symbol
,
157 llvm::FailedWithMessage(g_error_both_value_and_address
));
160 TEST(JSONSymbolTest
, SymbolInvalidNoValueOrAddress
) {
161 JSONSymbol json_symbol
;
163 SectionList sect_list
;
165 Expected
<Symbol
> symbol
= Symbol::FromJSON(json_symbol
, §_list
);
166 EXPECT_THAT_EXPECTED(
167 symbol
, llvm::FailedWithMessage(g_error_neither_value_or_address
));
170 TEST(JSONSymbolTest
, SymbolInvalidAddressNotInSection
) {
171 JSONSymbol json_symbol
;
172 json_symbol
.address
= 0x0fff;
174 SectionSP
sect_sp(new Section(
175 /*module_sp=*/ModuleSP(),
176 /*obj_file=*/nullptr,
178 /*name=*/ConstString(".text"),
179 /*sect_type=*/eSectionTypeCode
,
180 /*file_vm_addr=*/0x1000,
185 /*flags=*/0x10203040));
186 SectionList sect_list
;
187 sect_list
.AddSection(sect_sp
);
189 Expected
<Symbol
> symbol
= Symbol::FromJSON(json_symbol
, §_list
);
190 EXPECT_THAT_EXPECTED(
191 symbol
, llvm::FailedWithMessage("no section found for address: 0xfff"));