128 lines
3.7 KiB
C++
128 lines
3.7 KiB
C++
#include <echoion/api/module/Module.hpp>
|
|
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <array>
|
|
#include <string_view>
|
|
|
|
namespace
|
|
{
|
|
struct ModuleExpectation {
|
|
const char* path;
|
|
std::string_view moduleId;
|
|
std::string_view stableComponentId;
|
|
std::string_view version;
|
|
};
|
|
} // namespace
|
|
|
|
Void::Testing::Test component_module_descriptors_are_loadable()
|
|
{
|
|
const std::array modules{
|
|
ModuleExpectation{
|
|
.path = ECHOION_TEST_HOST,
|
|
.moduleId = "EchoionHost",
|
|
.stableComponentId = "EchoionHost",
|
|
.version = "2.0.0",
|
|
},
|
|
ModuleExpectation{
|
|
.path = ECHOION_TEST_CORE,
|
|
.moduleId = "EchoionCore",
|
|
.stableComponentId = "EchoionCore",
|
|
.version = "2.0.0",
|
|
},
|
|
ModuleExpectation{
|
|
.path = ECHOION_TEST_WEB,
|
|
.moduleId = "EchoionWeb",
|
|
.stableComponentId = "EchoionWeb",
|
|
.version = "2.0.0",
|
|
},
|
|
ModuleExpectation{
|
|
.path = ECHOION_TEST_PLUGIN_INTERNAL,
|
|
.moduleId = "url-shortener-internal",
|
|
.stableComponentId = "EchoionPluginUrlShortenerInternal",
|
|
.version = "2.0.0",
|
|
},
|
|
ModuleExpectation{
|
|
.path = ECHOION_TEST_PLUGIN_EXTERNAL,
|
|
.moduleId = "url-shortener-external",
|
|
.stableComponentId = "EchoionPluginUrlShortenerExternal",
|
|
.version = "2.0.0",
|
|
},
|
|
};
|
|
|
|
for (const auto& expected : modules) {
|
|
void* library = dlopen(expected.path, RTLD_NOW | RTLD_LOCAL);
|
|
Void::Testing::expect(library != nullptr);
|
|
if (library == nullptr) {
|
|
continue;
|
|
}
|
|
|
|
const auto query = reinterpret_cast<Void::Plugin::module_query_fn>(
|
|
dlsym(library, Echoion::Api::Module::querySymbol));
|
|
Void::Testing::expect(query != nullptr);
|
|
if (query == nullptr) {
|
|
dlclose(library);
|
|
continue;
|
|
}
|
|
|
|
const auto* descriptor = query();
|
|
Void::Testing::expect(descriptor != nullptr);
|
|
if (descriptor == nullptr) {
|
|
dlclose(library);
|
|
continue;
|
|
}
|
|
Void::Testing::expect(descriptor->validate().has_value());
|
|
Void::Testing::expect_eq(std::string_view{descriptor->module_id},
|
|
expected.moduleId);
|
|
Void::Testing::expect_eq(descriptor->component_version.to_string(),
|
|
expected.version);
|
|
|
|
auto module = descriptor->create();
|
|
Void::Testing::expect(module != nullptr);
|
|
if (module != nullptr) {
|
|
Void::Testing::expect_eq(module->name(), expected.moduleId);
|
|
}
|
|
module.reset();
|
|
|
|
const auto stableQuery =
|
|
reinterpret_cast<echoion_module_abi_query_fn_v1>(
|
|
dlsym(library, Echoion::Api::Module::stableQuerySymbol));
|
|
Void::Testing::expect(stableQuery != nullptr);
|
|
if (stableQuery != nullptr) {
|
|
const echoion_module_descriptor_v1* stable = stableQuery();
|
|
Void::Testing::expect(stable != nullptr);
|
|
if (stable != nullptr) {
|
|
Void::Testing::expect_eq(stable->struct_size,
|
|
sizeof(echoion_module_descriptor_v1));
|
|
Void::Testing::expect_eq(stable->abi_version,
|
|
ECHOION_MODULE_ABI_VERSION);
|
|
Void::Testing::expect_eq(
|
|
std::string_view{stable->component_id.data,
|
|
stable->component_id.size},
|
|
expected.stableComponentId);
|
|
Void::Testing::expect_eq(stable->version_major, 2U);
|
|
Void::Testing::expect_eq(stable->version_minor, 0U);
|
|
Void::Testing::expect_eq(stable->version_patch, 0U);
|
|
Void::Testing::expect(stable->api != nullptr);
|
|
if (stable->api != nullptr) {
|
|
Void::Testing::expect_eq(stable->api->struct_size,
|
|
sizeof(echoion_module_api_v1));
|
|
Void::Testing::expect(stable->api->create != nullptr);
|
|
Void::Testing::expect(stable->api->destroy != nullptr);
|
|
echoion_module_instance_v1 instance = nullptr;
|
|
const auto status = stable->api->create(nullptr, &instance);
|
|
Void::Testing::expect_eq(status, ECHOION_STATUS_OK_V1);
|
|
Void::Testing::expect(instance != nullptr);
|
|
if (instance != nullptr) {
|
|
stable->api->destroy(instance);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
dlclose(library);
|
|
}
|
|
}
|