From 4c6781bf95a7e5e0af802e162f4cb3b310203bf4 Mon Sep 17 00:00:00 2001 From: Andrii Nakryiko Date: Tue, 28 Nov 2017 16:48:14 -0800 Subject: [PATCH] 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 --- .../experimental/symbolizer/test/ElfTests.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/folly/experimental/symbolizer/test/ElfTests.cpp b/folly/experimental/symbolizer/test/ElfTests.cpp index b81f52e4..f2fb1541 100644 --- a/folly/experimental/symbolizer/test/ElfTests.cpp +++ b/folly/experimental/symbolizer/test/ElfTests.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include +#include #include #include @@ -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); +} -- 2.34.1