Add unit tests for opening non-ELF files.
authorAndrii Nakryiko <andriin@fb.com>
Wed, 29 Nov 2017 00:48:14 +0000 (16:48 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 29 Nov 2017 00:56:35 +0000 (16:56 -0800)
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

folly/experimental/symbolizer/test/ElfTests.cpp

index b81f52e48bc92cba54dcd2474ff51076ac2a4421..f2fb154189a2d8d9dab6a2dafe3d918f6f0de543 100644 (file)
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
  * limitations under the License.
  */
 
+#include <folly/FileUtil.h>
+#include <folly/experimental/TestUtil.h>
 #include <folly/experimental/symbolizer/Elf.h>
 #include <folly/portability/GTest.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);
 }
   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);
+}