proxygen
ElfTests.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2013-present 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 <folly/FileUtil.h>
21 
23 
24 // Add some symbols for testing. Note that we have to be careful with type
25 // signatures here to prevent name mangling
26 uint64_t kIntegerValue = 1234567890UL;
27 const char* kStringValue = "coconuts";
28 
29 class ElfTest : public ::testing::Test {
30  protected:
31  ElfFile elfFile_{"/proc/self/exe"};
32 };
33 
34 TEST_F(ElfTest, IntegerValue) {
35  auto sym = elfFile_.getSymbolByName("kIntegerValue");
36  EXPECT_NE(nullptr, sym.first) << "Failed to look up symbol kIntegerValue";
38 }
39 
40 TEST_F(ElfTest, PointerValue) {
41  auto sym = elfFile_.getSymbolByName("kStringValue");
42  EXPECT_NE(nullptr, sym.first) << "Failed to look up symbol kStringValue";
43  ElfW(Addr) addr = elfFile_.getSymbolValue<ElfW(Addr)>(sym.second);
44  const char* str = &elfFile_.getAddressValue<const char>(addr);
46 }
47 
48 TEST_F(ElfTest, iterateProgramHeaders) {
49  auto phdr = elfFile_.iterateProgramHeaders(
50  [](auto& h) { return h.p_type == PT_LOAD; });
51  EXPECT_NE(nullptr, phdr);
52  EXPECT_GE(phdr->p_filesz, 0);
53 }
54 
55 TEST_F(ElfTest, TinyNonElfFile) {
57  const static folly::StringPiece contents = "!";
58  folly::writeFull(tmpFile.fd(), contents.data(), contents.size());
59 
60  ElfFile elfFile;
61  const char* msg = nullptr;
62  auto res = elfFile.openNoThrow(tmpFile.path().c_str(), true, &msg);
63  EXPECT_EQ(ElfFile::kInvalidElfFile, res);
64  EXPECT_STREQ("not an ELF file (too short)", msg);
65 }
66 
67 TEST_F(ElfTest, NonElfScript) {
69  const static folly::StringPiece contents =
70  "#!/bin/sh\necho I'm small non-ELF executable\n";
71  folly::writeFull(tmpFile.fd(), contents.data(), contents.size());
72 
73  ElfFile elfFile;
74  const char* msg = nullptr;
75  auto res = elfFile.openNoThrow(tmpFile.path().c_str(), true, &msg);
76  EXPECT_EQ(ElfFile::kInvalidElfFile, res);
77  EXPECT_STREQ("invalid ELF magic", msg);
78 }
ElfFile elfFile_
Definition: ElfTests.cpp:31
*than *hazptr_holder h
Definition: Hazptr.h:116
const T & getAddressValue(const ElfAddr addr) const
Definition: Elf.h:218
const ElfPhdr * iterateProgramHeaders(Fn fn) const
Definition: Elf-inl.h:25
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const T & getSymbolValue(const ElfSym *symbol) const
Definition: Elf.h:199
constexpr size_type size() const
Definition: Range.h:431
#define EXPECT_GE(val1, val2)
Definition: gtest.h:1932
const char * kStringValue
Definition: ElfTests.cpp:27
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:1995
uint64_t kIntegerValue
Definition: ElfTests.cpp:26
int openNoThrow(const char *name, bool readOnly=true, const char **msg=nullptr) noexcept
Definition: Elf.cpp:63
constexpr Iter data() const
Definition: Range.h:446
ssize_t writeFull(int fd, const void *buf, size_t count)
Definition: FileUtil.cpp:134
Symbol getSymbolByName(const char *name) const
Definition: Elf.cpp:398
const fs::path & path() const
Definition: TestUtil.cpp:85
TEST_F(ElfTest, IntegerValue)
Definition: ElfTests.cpp:34
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
ThreadPoolListHook * addr