Commit 4c6781bf authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Facebook Github Bot

Add unit tests for opening non-ELF files.

Summary:
Just testing that ElfFile returns expected error code and message for
non-ELF files.

Depends on D6410210

Reviewed By: yfeldblum

Differential Revision: D6418365

fbshipit-source-id: aaab3b9f3ca1a12d384ae98a1772b7f640115192
parent ecb501b7
......@@ -14,6 +14,8 @@
* limitations under the License.
*/
#include <folly/FileUtil.h>
#include <folly/experimental/TestUtil.h>
#include <folly/experimental/symbolizer/Elf.h>
#include <folly/portability/GTest.h>
......@@ -49,3 +51,28 @@ TEST_F(ElfTest, iterateProgramHeaders) {
EXPECT_NE(nullptr, phdr);
EXPECT_GE(phdr->p_filesz, 0);
}
TEST_F(ElfTest, TinyNonElfFile) {
folly::test::TemporaryFile tmpFile;
const static folly::StringPiece contents = "!";
folly::writeFull(tmpFile.fd(), contents.data(), contents.size());
ElfFile elfFile;
const char* msg = nullptr;
auto res = elfFile.openNoThrow(tmpFile.path().c_str(), true, &msg);
EXPECT_EQ(ElfFile::kInvalidElfFile, res);
EXPECT_STREQ("not an ELF file (too short)", msg);
}
TEST_F(ElfTest, NonElfScript) {
folly::test::TemporaryFile tmpFile;
const static folly::StringPiece contents =
"#!/bin/sh\necho I'm small non-ELF executable\n";
folly::writeFull(tmpFile.fd(), contents.data(), contents.size());
ElfFile elfFile;
const char* msg = nullptr;
auto res = elfFile.openNoThrow(tmpFile.path().c_str(), true, &msg);
EXPECT_EQ(ElfFile::kInvalidElfFile, res);
EXPECT_STREQ("invalid ELF magic", msg);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment