1
0
mirror of synced 2024-09-23 19:18:24 +02:00

tests: Add infrastructure for testing plugins (#1538)

This PR adds a test architecture to be able to test plugins

Main infrastructure done by @WerWolv

---------

Co-authored-by: WerWolv <werwolv98@gmail.com>
This commit is contained in:
iTrooz 2024-02-26 20:51:08 +01:00 committed by GitHub
parent 032ef0722d
commit 47362559ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
28 changed files with 205 additions and 69 deletions

View File

@ -2,9 +2,8 @@ name: "Unit Tests"
on:
push:
branches: [ master ]
branches: ["*"]
pull_request:
branches: [ master ]
workflow_dispatch:
jobs:
@ -49,6 +48,7 @@ jobs:
cd build
CC=gcc-12 CXX=g++-12 cmake \
-DCMAKE_BUILD_TYPE=Debug \
-DIMHEX_ENABLE_UNIT_TESTS=ON \
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
-DCMAKE_C_FLAGS="-fuse-ld=lld -fsanitize=address,leak,undefined -fno-sanitize-recover=all" \

View File

@ -19,6 +19,7 @@ option(IMHEX_ENABLE_UNITY_BUILD "Enables building ImHex as a unity build
option(IMHEX_GENERATE_PDBS "Enable generating PDB files in non-debug builds (Windows only)" OFF)
option(IMHEX_REPLACE_DWARF_WITH_PDB "Remove DWARF information from binaries when generating PDBS (Windows only)" OFF)
option(IMHEX_ENABLE_STD_ASSERTS "Enable debug asserts in the C++ std library. (Breaks Plugin ABI!)" OFF)
option(IMHEX_ENABLE_UNIT_TESTS "Enable building unit tests" OFF)
# Basic compiler and cmake configurations
set(CMAKE_CXX_STANDARD 23)
@ -61,8 +62,10 @@ add_subdirectory(lib/libimhex)
add_subdirectory(main)
# Add unit tests
enable_testing()
add_subdirectory(tests EXCLUDE_FROM_ALL)
if (IMHEX_ENABLE_UNIT_TESTS)
enable_testing()
add_subdirectory(tests EXCLUDE_FROM_ALL)
endif ()
# Configure more resources that will be added to the install package
createPackage()

View File

@ -88,6 +88,12 @@ macro(add_imhex_plugin)
elseif (UNIX)
set_target_properties(${IMHEX_PLUGIN_NAME} PROPERTIES INSTALL_RPATH_USE_ORIGIN ON INSTALL_RPATH "$ORIGIN/")
endif()
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/CMakeLists.txt AND IMHEX_ENABLE_UNIT_TESTS)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tests)
target_link_libraries(${IMHEX_PLUGIN_NAME} PUBLIC ${IMHEX_PLUGIN_NAME}_tests)
target_compile_definitions(${IMHEX_PLUGIN_NAME}_tests PRIVATE IMHEX_PROJECT_NAME="${IMHEX_PLUGIN_NAME}-tests")
endif()
endmacro()
macro(add_romfs_resource input output)

4
dist/rpm/imhex.spec vendored
View File

@ -95,10 +95,6 @@ CXXFLAGS+=" -std=gnu++2b"
%set_build_flags
CXXFLAGS+=" -std=gnu++2b"
%endif
# build binaries required for tests
%cmake_build --target unit_tests
%ctest --exclude-regex '(Helpers/StoreAPI|Helpers/TipsAPI|Helpers/ContentAPI)'
# Helpers/*API exclude tests that require network access
%install

View File

@ -38,6 +38,8 @@ set(LIBIMHEX_SOURCES
source/helpers/tar.cpp
source/helpers/debugging.cpp
source/test/tests.cpp
source/providers/provider.cpp
source/providers/memory_provider.cpp
source/providers/undo/stack.cpp

View File

@ -3,6 +3,7 @@
#include <hex.hpp>
#include <list>
#include <mutex>
#include <map>
#include <string_view>
#include <functional>
@ -308,4 +309,5 @@ namespace hex {
* The 'from' provider should not have any per provider data after this, and should be immediately deleted
*/
EVENT_DEF(MovePerProviderData, prv::Provider *, prv::Provider *);
}

View File

@ -111,6 +111,7 @@ namespace hex {
static void addPlugin(const std::string &name, PluginFunctions functions);
static Plugin* getPlugin(const std::string &name);
static const std::list<Plugin>& getPlugins();
static const std::vector<std::fs::path>& getPluginPaths();
static const std::vector<std::fs::path>& getPluginLoadPaths();

View File

@ -8,6 +8,7 @@
#include <fmt/color.h>
#include <wolv/io/file.hpp>
#include <hex/helpers/fmt.hpp>
namespace hex::log {
@ -88,7 +89,6 @@ namespace hex::log {
hex::log::impl::print(fg(impl::color::fatal()) | fmt::emphasis::bold, "[FATAL]", fmt, args...);
}
[[maybe_unused]] void print(const std::string &fmt, auto && ... args) {
std::scoped_lock lock(impl::getLoggerMutex());

View File

@ -161,7 +161,7 @@ namespace hex::prv {
*/
[[nodiscard]] virtual std::string getName() const = 0;
void resize(u64 newSize);
bool resize(u64 newSize);
void insert(u64 offset, u64 size);
void remove(u64 offset, u64 size);

View File

@ -5,6 +5,7 @@
#include <hex/helpers/utils.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/api/plugin_manager.hpp>
#include <wolv/utils/preproc.hpp>
@ -28,16 +29,20 @@
} \
} while (0)
#define INIT_PLUGIN(name) \
if (!hex::test::initPluginImpl(name)) TEST_FAIL();
namespace hex::test {
using Function = int(*)();
struct Test {
std::function<int()> function;
Function function;
bool shouldFail;
};
class Tests {
public:
static auto addTest(const std::string &name, const std::function<int()> &func, bool shouldFail) noexcept {
static auto addTest(const std::string &name, Function func, bool shouldFail) noexcept {
s_tests.insert({
name, {func, shouldFail}
});
@ -50,7 +55,7 @@ namespace hex::test {
}
private:
static inline std::map<std::string, Test> s_tests;
static std::map<std::string, Test> s_tests;
};
template<class F>
@ -86,4 +91,5 @@ namespace hex::test {
return TestSequence<F>(executor.getName(), std::forward<F>(f), executor.shouldFail());
}
bool initPluginImpl(std::string name);
}

View File

@ -321,6 +321,15 @@ namespace hex {
return plugins;
}
Plugin* PluginManager::getPlugin(const std::string &name) {
for (auto &plugin : getPluginsMutable()) {
if (plugin.getPluginName() == name)
return &plugin;
}
return nullptr;
}
const std::vector<std::fs::path>& PluginManager::getPluginPaths() {
return s_pluginPaths;
}

View File

@ -1,3 +1,4 @@
#include <hex/api/event_manager.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/fmt.hpp>

View File

@ -74,7 +74,11 @@ namespace hex::prv {
}
}
void Provider::resize(u64 newSize) {
bool Provider::resize(u64 newSize) {
if (newSize >> 63) {
log::error("new provider size is very large ({}). Is it a negative number ?", newSize);
return false;
}
i64 difference = newSize - this->getActualSize();
if (difference > 0)
@ -83,6 +87,7 @@ namespace hex::prv {
EventProviderDataRemoved::post(this, this->getActualSize() + difference, -difference);
this->markDirty();
return true;
}
void Provider::insert(u64 offset, u64 size) {

View File

@ -0,0 +1,23 @@
#include <hex/test/tests.hpp>
namespace hex::test {
std::map<std::string, Test> Tests::s_tests;
bool initPluginImpl(std::string name) {
if (name != "Built-in") {
if(!initPluginImpl("Built-in")) return false;
}
hex::Plugin *plugin = hex::PluginManager::getPlugin(name);
if (plugin == nullptr) {
hex::log::fatal("Plugin '{}' was not found !", name);
return false;
}else if (!plugin->initializePlugin()) {
hex::log::fatal("Failed to initialize plugin '{}' !", name);
return false;
}
hex::log::info("Initialized plugin '{}' successfully", name);
return true;
}
}

View File

@ -114,8 +114,6 @@ add_imhex_plugin(
source/content/views/view_achievements.cpp
source/content/views/view_highlight_rules.cpp
source/content/views/view_tutorials.cpp
source/content/helpers/notification.cpp
INCLUDES
include

View File

@ -1,8 +0,0 @@
#pragma once
namespace hex::plugin::builtin {
void showError(const std::string& message);
void showWarning(const std::string& message);
}

View File

@ -1,16 +0,0 @@
#include <hex/helpers/logger.hpp>
#include <toasts/toast_notification.hpp>
namespace hex::plugin::builtin {
void showError(const std::string& message){
ui::ToastError::open(message);
log::error(message);
}
void showWarning(const std::string& message){
ui::ToastWarning::open(message);
log::warn(message);
}
}

View File

@ -13,8 +13,7 @@
#include <hex/providers/provider.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/helpers/logger.hpp>
#include <content/helpers/notification.hpp>
#include <toasts/toast_notification.hpp>
namespace hex::plugin::builtin {
@ -23,26 +22,29 @@ namespace hex::plugin::builtin {
bool load(const std::fs::path &filePath) {
if (!wolv::io::fs::exists(filePath) || !wolv::io::fs::isRegularFile(filePath)) {
showError(hex::format("hex.builtin.popup.error.project.load"_lang,
ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang,
hex::format("hex.builtin.popup.error.project.load.file_not_found"_lang,
wolv::util::toUTF8String(filePath)
)));
return false;
}
Tar tar(filePath, Tar::Mode::Read);
if (!tar.isValid()) {
showError(hex::format("hex.builtin.popup.error.project.load"_lang,
ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang,
hex::format("hex.builtin.popup.error.project.load.invalid_tar"_lang,
tar.getOpenErrorString()
)));
return false;
}
if (!tar.contains(MetadataPath)) {
showError(hex::format("hex.builtin.popup.error.project.load"_lang,
ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang,
hex::format("hex.builtin.popup.error.project.load.invalid_magic"_lang)
));
return false;
}
@ -50,9 +52,10 @@ namespace hex::plugin::builtin {
const auto metadataContent = tar.readVector(MetadataPath);
if (!std::string(metadataContent.begin(), metadataContent.end()).starts_with(MetadataHeaderMagic)) {
showError(hex::format("hex.builtin.popup.error.project.load"_lang,
ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang,
hex::format("hex.builtin.popup.error.project.load.invalid_magic"_lang)
));
return false;
}
}
@ -176,4 +179,4 @@ namespace hex::plugin::builtin {
void registerProjectHandlers() {
hex::ProjectFile::setProjectFunctions(load, store);
}
}
}

View File

@ -11,13 +11,13 @@
#include <content/providers/process_memory_provider.hpp>
#include <content/providers/base64_provider.hpp>
#include <popups/popup_notification.hpp>
#include "content/helpers/notification.hpp"
#include <hex/api/project_file_manager.hpp>
#include <hex/api/task_manager.hpp>
#include <hex/helpers/fmt.hpp>
#include <nlohmann/json.hpp>
#include <toasts/toast_notification.hpp>
#include <wolv/utils/guards.hpp>
@ -70,9 +70,11 @@ namespace hex::plugin::builtin {
if (newProvider == nullptr) {
// If a provider is not created, it will be overwritten when saving the project,
// so we should prevent the project from loading at all
showError(hex::format("hex.builtin.popup.error.project.load"_lang,
hex::format("hex.builtin.popup.error.project.load.create_provider"_lang, providerType)
));
ui::ToastError::open(
hex::format("hex.builtin.popup.error.project.load"_lang,
hex::format("hex.builtin.popup.error.project.load.create_provider"_lang, providerType)
)
);
success = false;
break;
}
@ -94,29 +96,27 @@ namespace hex::plugin::builtin {
}
}
std::string warningMsg;
std::string warningMessage;
for (const auto &warning : providerWarnings){
ImHexApi::Provider::remove(warning.first);
warningMsg.append(
warningMessage.append(
hex::format("\n - {} : {}", warning.first->getName(), warning.second));
}
// If no providers were opened, display an error with
// the warnings that happened when opening them
if (ImHexApi::Provider::getProviders().size() == 0) {
showError(hex::format("hex.builtin.popup.error.project.load"_lang,
hex::format("hex.builtin.popup.error.project.load.no_providers"_lang)) + warningMsg);
if (ImHexApi::Provider::getProviders().empty()) {
ui::ToastError::open(hex::format("{}{}", "hex.builtin.popup.error.project.load"_lang, "hex.builtin.popup.error.project.load.no_providers"_lang, warningMessage));
return false;
} else {
// Else, if are warnings, still display them
if (warningMsg.empty()) {
} else {
// Else, if there are warnings, still display them
if (warningMessage.empty()) {
return true;
} else {
showWarning(
hex::format("hex.builtin.popup.error.project.load.some_providers_failed"_lang, warningMsg));
ui::ToastWarning::open(hex::format("hex.builtin.popup.error.project.load.some_providers_failed"_lang, warningMessage));
}
return success;
}
},

View File

@ -0,0 +1,19 @@
project(${IMHEX_PLUGIN_NAME}_tests)
# Add new tests here #
set(AVAILABLE_TESTS
Providers/ReadWrite
Providers/InvalidResize
)
add_library(${PROJECT_NAME} SHARED
source/main.cpp
)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/plugins/builtin/include)
target_link_libraries(${PROJECT_NAME} PRIVATE libimhex)
foreach (test IN LISTS AVAILABLE_TESTS)
add_test(NAME "Plugin_${IMHEX_PLUGIN_NAME}/${test}" COMMAND $<TARGET_FILE:plugins_test> "${test}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
endforeach ()

View File

@ -0,0 +1,43 @@
#include <iostream>
#include <hex/test/tests.hpp>
#include <hex/api/plugin_manager.hpp>
#include <content/providers/memory_file_provider.hpp>
#include <content/views/view_patches.hpp>
#include <hex/api/task_manager.hpp>
using namespace hex;
using namespace hex::plugin::builtin;
TEST_SEQUENCE("Providers/ReadWrite") {
INIT_PLUGIN("Built-in");
auto &pr = *ImHexApi::Provider::createProvider("hex.builtin.provider.mem_file", true);
TEST_ASSERT(pr.getSize() == 0);
TEST_ASSERT(!pr.isDirty());
pr.resize(50);
TEST_ASSERT(pr.getSize() == 50);
TEST_ASSERT(pr.isDirty());
char buf[] = "\x99\x99"; // temporary value that should be overwriten
pr.read(0, buf, 2);
TEST_ASSERT(std::equal(buf, buf+2, "\x00\x00"));
pr.write(0, "\xFF\xFF", 2);
char buf2[] = "\x99\x99"; // temporary value that should be overwriten
pr.read(0, buf2, 2);
TEST_ASSERT(std::equal(buf2, buf2+2, "\xFF\xFF"));
TEST_SUCCESS();
};
TEST_SEQUENCE("Providers/InvalidResize") {
INIT_PLUGIN("Built-in");
auto &pr = *ImHexApi::Provider::createProvider("hex.builtin.provider.mem_file", true);
TEST_ASSERT(!pr.resize(-1));
TEST_SUCCESS();
};

View File

@ -1,11 +1,11 @@
#pragma once
#include <hex/api/imhex_api.hpp>
#include <hex/ui/imgui_imhex_extensions.h>
#include <hex/ui/toast.hpp>
#include <fonts/codicons_font.h>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/logger.hpp>
#include <popups/popup_notification.hpp>
@ -49,17 +49,23 @@ namespace hex::ui {
struct ToastInfo : impl::ToastNotification<ToastInfo> {
ToastInfo(std::string message)
: ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerInfo), ICON_VS_INFO, "hex.ui.common.info", std::move(message)) {}
: ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerInfo), ICON_VS_INFO, "hex.ui.common.info", std::move(message)) {
log::info("{}", message);
}
};
struct ToastWarning : impl::ToastNotification<ToastWarning> {
ToastWarning(std::string message)
: ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerWarning), ICON_VS_WARNING, "hex.ui.common.warning", std::move(message)) {}
: ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerWarning), ICON_VS_WARNING, "hex.ui.common.warning", std::move(message)) {
log::warn("{}", message);
}
};
struct ToastError : impl::ToastNotification<ToastError> {
ToastError(std::string message)
: ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerError), ICON_VS_ERROR, "hex.ui.common.error", std::move(message)) {}
: ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerError), ICON_VS_ERROR, "hex.ui.common.error", std::move(message)) {
log::error("{}", message);
}
};
}

View File

@ -1,9 +1,10 @@
project(unit_tests)
add_custom_target(unit_tests DEPENDS helpers algorithms)
add_custom_target(unit_tests DEPENDS imhex_all helpers algorithms plugins)
add_subdirectory(common)
target_compile_definitions(tests_common PUBLIC IMHEX_PROJECT_NAME="${PROJECT_NAME}")
add_subdirectory(helpers)
add_subdirectory(algorithms)
add_subdirectory(plugins)

View File

@ -5,5 +5,4 @@ project(tests_common)
add_library(tests_common STATIC
source/main.cpp
)
target_include_directories(tests_common PUBLIC include)
target_link_libraries(tests_common PUBLIC libimhex ${FMT_LIBRARIES} libwolv)

View File

@ -3,8 +3,10 @@
#include <hex/api/event_manager.hpp>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/test/tests.hpp>
#include <hex/api/plugin_manager.hpp>
#include <hex/api/task_manager.hpp>
#include <hex/api/event_manager.hpp>
#include <cstdlib>
@ -48,7 +50,10 @@ int main(int argc, char **argv) {
else
hex::log::info("Failed!");
hex::TaskManager::exit();
hex::EventImHexClosing::post();
hex::EventManager::clear();
hex::PluginManager::unload();
return result;
}

View File

@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.16)
project(plugins_test)
add_executable(${PROJECT_NAME}
source/plugins.cpp
)
# ---- No need to change anything from here downwards unless you know what you're doing ---- #
target_include_directories(${PROJECT_NAME} PRIVATE include)
target_link_libraries(${PROJECT_NAME} PRIVATE libimhex tests_common ${FMT_LIBRARIES})
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
add_dependencies(unit_tests ${PROJECT_NAME})

View File

@ -0,0 +1,16 @@
#include <hex/api/plugin_manager.hpp>
#include <hex/helpers/utils.hpp>
using namespace hex;
class PluginLoader {
public:
PluginLoader() {
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Plugins)) {
PluginManager::addLoadPath(dir);
}
PluginManager::load();
}
};
static PluginLoader pluginLoader;