84d526c5679d00d548e1acbdcfbb24ca7e4bdd36
[folly.git] / folly / experimental / symbolizer / test / ElfTests.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <gtest/gtest.h>
18
19 #include <folly/experimental/symbolizer/Elf.h>
20
21 using folly::symbolizer::ElfFile;
22
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";
27
28 class ElfTest : public ::testing::Test {
29  public:
30   // Path to the test binary itself; set by main()
31   static std::string binaryPath;
32
33   ElfTest() : elfFile_(binaryPath.c_str()) {
34   }
35   ~ElfTest() override {}
36
37  protected:
38   ElfFile elfFile_;
39 };
40
41 std::string ElfTest::binaryPath;
42
43 TEST_F(ElfTest, IntegerValue) {
44   auto sym = elfFile_.getSymbolByName("kIntegerValue");
45   EXPECT_NE(nullptr, sym.first) <<
46     "Failed to look up symbol kIntegerValue";
47   EXPECT_EQ(kIntegerValue, elfFile_.getSymbolValue<uint64_t>(sym.second));
48 }
49
50 TEST_F(ElfTest, PointerValue) {
51   auto sym = elfFile_.getSymbolByName("kStringValue");
52   EXPECT_NE(nullptr, sym.first) <<
53     "Failed to look up symbol kStringValue";
54   ElfW(Addr) addr = elfFile_.getSymbolValue<ElfW(Addr)>(sym.second);
55   const char *str = &elfFile_.getAddressValue<const char>(addr);
56   EXPECT_STREQ(kStringValue, str);
57 }
58
59 int main(int argc, char** argv) {
60   testing::InitGoogleTest(&argc, argv);
61   gflags::ParseCommandLineFlags(&argc, &argv, true);
62   ElfTest::binaryPath = argv[0];
63   return RUN_ALL_TESTS();
64 }