1//===-- JSONSymbolTest.cpp ------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Core/Section.h"
10#include "lldb/Symbol/Symbol.h"
11#include "llvm/Testing/Support/Error.h"
12
13#include "gtest/gtest.h"
14
15using namespace lldb;
16using namespace llvm;
17using namespace lldb_private;
18
19static std::string g_error_no_section_list = "no section list provided";
20static std::string g_error_both_value_and_address =
21 "symbol cannot contain both a value and an address";
22static std::string g_error_neither_value_or_address =
23 "symbol must contain either a value or an address";
24
25TEST(JSONSymbolTest, DeserializeCodeAddress) {
26 std::string text = R"(
27{
28 "name": "foo",
29 "type": "code",
30 "size": 32,
31 "address": 4096
32})";
33
34 Expected<json::Value> json = json::parse(JSON: text);
35 ASSERT_TRUE(static_cast<bool>(json));
36
37 json::Path::Root root;
38 JSONSymbol json_symbol;
39 ASSERT_TRUE(fromJSON(*json, json_symbol, root));
40
41 SectionSP sect_sp(new Section(
42 /*module_sp=*/ModuleSP(),
43 /*obj_file=*/nullptr,
44 /*sect_id=*/1,
45 /*name=*/ConstString(".text"),
46 /*sect_type=*/eSectionTypeCode,
47 /*file_vm_addr=*/0x1000,
48 /*vm_size=*/0x1000,
49 /*file_offset=*/0,
50 /*file_size=*/0,
51 /*log2align=*/5,
52 /*flags=*/0x10203040));
53 SectionList sect_list;
54 sect_list.AddSection(section_sp: sect_sp);
55
56 Expected<Symbol> symbol = Symbol::FromJSON(symbol: json_symbol, section_list: &sect_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);
61}
62
63TEST(JSONSymbolTest, DeserializeCodeValue) {
64 std::string text = R"(
65{
66 "name": "foo",
67 "type": "code",
68 "size": 32,
69 "value": 4096
70})";
71
72 Expected<json::Value> json = json::parse(JSON: text);
73 EXPECT_THAT_EXPECTED(json, llvm::Succeeded());
74
75 json::Path::Root root;
76 JSONSymbol json_symbol;
77 ASSERT_TRUE(fromJSON(*json, json_symbol, root));
78
79 SectionList sect_list;
80
81 Expected<Symbol> symbol = Symbol::FromJSON(symbol: json_symbol, section_list: &sect_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);
86}
87
88TEST(JSONSymbolTest, JSONInvalidValueAndAddress) {
89 std::string text = R"(
90{
91 "name": "foo",
92 "type": "code",
93 "size": 32,
94 "value": 4096,
95 "address": 4096
96})";
97
98 Expected<json::Value> json = json::parse(JSON: text);
99 EXPECT_THAT_EXPECTED(json, llvm::Succeeded());
100
101 json::Path::Root root;
102 JSONSymbol json_symbol;
103 ASSERT_FALSE(fromJSON(*json, json_symbol, root));
104}
105
106TEST(JSONSymbolTest, JSONInvalidNoValueOrAddress) {
107 std::string text = R"(
108{
109 "name": "foo",
110 "type": "code",
111 "size": 32
112})";
113
114 Expected<json::Value> json = json::parse(JSON: text);
115 EXPECT_THAT_EXPECTED(json, llvm::Succeeded());
116
117 json::Path::Root root;
118 JSONSymbol json_symbol;
119 ASSERT_FALSE(fromJSON(*json, json_symbol, root));
120}
121
122TEST(JSONSymbolTest, JSONInvalidType) {
123 std::string text = R"(
124{
125 "name": "foo",
126 "type": "bogus",
127 "value": 4096,
128 "size": 32
129})";
130
131 Expected<json::Value> json = json::parse(JSON: text);
132 EXPECT_THAT_EXPECTED(json, llvm::Succeeded());
133
134 json::Path::Root root;
135 JSONSymbol json_symbol;
136 ASSERT_FALSE(fromJSON(*json, json_symbol, root));
137}
138
139TEST(JSONSymbolTest, SymbolInvalidNoSectionList) {
140 JSONSymbol json_symbol;
141 json_symbol.value = 0x1;
142
143 Expected<Symbol> symbol = Symbol::FromJSON(symbol: json_symbol, section_list: nullptr);
144 EXPECT_THAT_EXPECTED(symbol,
145 llvm::FailedWithMessage(g_error_no_section_list));
146}
147
148TEST(JSONSymbolTest, SymbolInvalidValueAndAddress) {
149 JSONSymbol json_symbol;
150 json_symbol.value = 0x1;
151 json_symbol.address = 0x2;
152
153 SectionList sect_list;
154
155 Expected<Symbol> symbol = Symbol::FromJSON(symbol: json_symbol, section_list: &sect_list);
156 EXPECT_THAT_EXPECTED(symbol,
157 llvm::FailedWithMessage(g_error_both_value_and_address));
158}
159
160TEST(JSONSymbolTest, SymbolInvalidNoValueOrAddress) {
161 JSONSymbol json_symbol;
162
163 SectionList sect_list;
164
165 Expected<Symbol> symbol = Symbol::FromJSON(symbol: json_symbol, section_list: &sect_list);
166 EXPECT_THAT_EXPECTED(
167 symbol, llvm::FailedWithMessage(g_error_neither_value_or_address));
168}
169
170TEST(JSONSymbolTest, SymbolInvalidAddressNotInSection) {
171 JSONSymbol json_symbol;
172 json_symbol.address = 0x0fff;
173
174 SectionSP sect_sp(new Section(
175 /*module_sp=*/ModuleSP(),
176 /*obj_file=*/nullptr,
177 /*sect_id=*/1,
178 /*name=*/ConstString(".text"),
179 /*sect_type=*/eSectionTypeCode,
180 /*file_vm_addr=*/0x1000,
181 /*vm_size=*/0x1000,
182 /*file_offset=*/0,
183 /*file_size=*/0,
184 /*log2align=*/5,
185 /*flags=*/0x10203040));
186 SectionList sect_list;
187 sect_list.AddSection(section_sp: sect_sp);
188
189 Expected<Symbol> symbol = Symbol::FromJSON(symbol: json_symbol, section_list: &sect_list);
190 EXPECT_THAT_EXPECTED(
191 symbol, llvm::FailedWithMessage("no section found for address: 0xfff"));
192}
193

source code of lldb/unittests/Symbol/JSONSymbolTest.cpp