Test invalid headers and missing files in ELF library

CC #185.
This commit is contained in:
TheAssassin 2021-11-30 00:08:52 +01:00
parent d621d3949d
commit 5685bc575e
2 changed files with 37 additions and 1 deletions

View File

@ -40,7 +40,7 @@ target_include_directories(test_linuxdeploy PRIVATE ${PROJECT_SOURCE_DIR}/src)
ld_add_test(test_linuxdeploy)
ld_core_add_test_executable(test_elf_file test_elf_file.cpp ../../src/core.cpp)
target_link_libraries(test_elf_file PRIVATE gtest_main)
target_link_libraries(test_elf_file PRIVATE gtest_main gmock)
target_include_directories(test_elf_file PRIVATE ${PROJECT_SOURCE_DIR}/src)
# register in CTest
ld_add_test(test_elf_file)

View File

@ -1,4 +1,5 @@
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "linuxdeploy/core/elf_file.h"
@ -9,6 +10,27 @@ namespace bf = boost::filesystem;
using namespace std;
using namespace linuxdeploy::core::elf_file;
namespace {
typedef std::function<void()> callback_t;
template<typename T>
void expectThrowMessageHasSubstr(const callback_t& callback, const char* message) {
EXPECT_THAT(callback, ::testing::ThrowsMessage<ElfFileParseError>(::testing::HasSubstr(message)));
}
void expectElfFileConstructorThrowMessage(const char* path, const char* message) {
return expectThrowMessageHasSubstr<ElfFileParseError>([=]() { ElfFile{path}; }, message);
}
void expectThrowsElfFileErrorInvalidElfHeader(const char* path) {
expectElfFileConstructorThrowMessage(path, "Invalid magic bytes in file header");
}
void expectThrowsElfFileErrorFileNotFound(const char* path) {
expectElfFileConstructorThrowMessage(path, "No such file or directory: ");
}
}
namespace LinuxDeployTest {
class ElfFileTest : public ::testing::Test {};
@ -27,4 +49,18 @@ namespace LinuxDeployTest {
ElfFile staticLibraryFile(SIMPLE_EXECUTABLE_STATIC_PATH);
EXPECT_FALSE(staticLibraryFile.isDynamicallyLinked());
}
TEST_F(ElfFileTest, checkInvalidElfHeaderOnEmptyFile) {
expectThrowsElfFileErrorInvalidElfHeader("/dev/null");
}
TEST_F(ElfFileTest, checkInvalidElfHeaderOnRandomFiles) {
expectThrowsElfFileErrorInvalidElfHeader(SIMPLE_DESKTOP_ENTRY_PATH);
expectThrowsElfFileErrorInvalidElfHeader(SIMPLE_ICON_PATH);
expectThrowsElfFileErrorInvalidElfHeader(SIMPLE_FILE_PATH);
}
TEST_F(ElfFileTest, checkFileNotFound) {
expectThrowsElfFileErrorFileNotFound("/abc/def/ghi/jkl/mno/pqr/stu/vwx/yz");
}
}