2 * Copyright 2014 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <gtest/gtest.h>
19 #include "folly/experimental/symbolizer/Elf.h"
21 using folly::symbolizer::ElfFile;
23 // Add some symbols for testing. Note that we have to be careful with type
24 // signatures here to prevent name mangling
25 uint64_t kIntegerValue = 1234567890UL;
26 const char* kStringValue = "coconuts";
28 class ElfTest : public ::testing::Test {
30 // Path to the test binary itself; set by main()
31 static std::string binaryPath;
33 ElfTest() : elfFile_(binaryPath.c_str()) {
42 std::string ElfTest::binaryPath;
44 TEST_F(ElfTest, IntegerValue) {
45 auto sym = elfFile_.getSymbolByName("kIntegerValue");
46 EXPECT_NE(nullptr, sym.first) <<
47 "Failed to look up symbol kIntegerValue";
48 EXPECT_EQ(kIntegerValue, elfFile_.getSymbolValue<uint64_t>(sym.second));
51 TEST_F(ElfTest, PointerValue) {
52 auto sym = elfFile_.getSymbolByName("kStringValue");
53 EXPECT_NE(nullptr, sym.first) <<
54 "Failed to look up symbol kStringValue";
55 ElfW(Addr) addr = elfFile_.getSymbolValue<ElfW(Addr)>(sym.second);
56 const char *str = &elfFile_.getAddressValue<const char>(addr);
57 EXPECT_STREQ(kStringValue, str);
60 int main(int argc, char** argv) {
61 testing::InitGoogleTest(&argc, argv);
62 google::ParseCommandLineFlags(&argc, &argv, true);
63 ElfTest::binaryPath = argv[0];
64 return RUN_ALL_TESTS();