Files
dpm-core-ng/tests/test_modules.cpp
Christopher M. Punches 55c852586b Dispatch returns a result envelope
dpm_execute fills a dpm_result on every call: status and error_code
from the module's return value, and a payload the module hands back
through dpm_set_result. The payload's layout belongs to the module and
is documented per command; its release function is carried in the
envelope and invoked by dpm_result_release. The context keeps a stack
of the envelopes in progress, so a module calling a peer receives the
peer's payload in its own envelope and its caller sees only what the
module sets itself.

The good fixture returns a payload and counts its releases, and the
modules test covers the envelope fields, the payload round trip, single
release, discard on a NULL envelope, and a payload set outside any
dispatch. The prose documents describe the envelope and the payload
header a module ships.
2026-09-07 14:53:24 -04:00

143 lines
5.4 KiB
C++

/**
* @file test_modules.cpp
* @brief Load-time validation, module acquisition, and dispatch
*
* @copyright Copyright (c) 2026 SILO GROUP LLC
* @author Chris Punches <chris.punches@silogroup.org>
*
* Part of the Dark Horse Linux Package Manager (DPM)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "harness.hpp"
int main(void) {
dpm_open_overrides overrides = {TEST_FIXTURE_CONF, TEST_FIXTURE_MODULES,
nullptr, -1, TEST_FIXTURE_METADATA};
dpm_ctx* ctx = dpm_open(&overrides);
CHECK(ctx != nullptr);
if (!ctx) {
std::fprintf(stderr, "cannot continue without a context\n");
return 1;
}
/* ---- every broken fixture is refused, and the reason says why ---- */
{
CHECK(dpm_require(ctx, "missing_symbols") == nullptr);
CHECK(error_contains(ctx, "missing required contract symbols"));
CHECK(error_contains(ctx, "dpm_module_execute"));
CHECK(error_contains(ctx, "dpm_module_aliases"));
CHECK(dpm_require(ctx, "bad_version") == nullptr);
CHECK(error_contains(ctx, "malformed version"));
CHECK(dpm_require(ctx, "nonexistent") == nullptr);
CHECK(error_contains(ctx, "not found"));
}
/* ---- a bad argument is refused without touching the module path ---- */
{
CHECK(dpm_require(nullptr, "good") == nullptr);
CHECK(dpm_require(ctx, nullptr) == nullptr);
CHECK(dpm_require(ctx, "") == nullptr);
}
/* ---- the known-good fixture loads and reports what it declares ---- */
{
dpm_module* good = dpm_require(ctx, "good");
CHECK(good != nullptr);
if (!good) {
dpm_close(ctx);
return harness_report("modules");
}
/* a module is opened at most once per context */
CHECK(dpm_require(ctx, "good") == good);
dpm_module_info seen;
CHECK(dpm_get_module_info(ctx, good, &seen) == 0);
CHECK(std::strcmp(seen.name, "good") == 0);
CHECK(std::strcmp(seen.version, "1.2.3") == 0);
CHECK(seen.description != nullptr && *seen.description);
CHECK(dpm_get_module_info(ctx, nullptr, &seen) != 0);
CHECK(dpm_get_module_info(ctx, good, nullptr) != 0);
CHECK(dpm_get_module_info(nullptr, good, &seen) != 0);
/* dispatch returns the module's own value, so a round trip is
observable with no compile-time knowledge of the module */
dpm_result result;
CHECK(dpm_execute(ctx, good, "ping", 0, nullptr, &result) == 42);
CHECK(result.status == DPM_RESULT_ERROR);
CHECK(result.error_code == 42);
CHECK(result.data == nullptr);
CHECK(result.release == nullptr);
CHECK(dpm_execute(ctx, good, "anything_else", 0, nullptr, &result) == 0);
CHECK(result.status == DPM_RESULT_OK);
CHECK(result.error_code == 0);
CHECK(result.data == nullptr);
CHECK(dpm_execute(ctx, good, nullptr, 0, nullptr, &result) == 0);
CHECK(result.status == DPM_RESULT_OK);
CHECK(dpm_execute(nullptr, good, "ping", 0, nullptr, &result) == 1);
CHECK(result.status == DPM_RESULT_ERROR);
CHECK(dpm_execute(ctx, nullptr, "ping", 0, nullptr, &result) == 1);
CHECK(result.status == DPM_RESULT_ERROR);
}
/* ---- a payload reaches the caller and is released once ---- */
{
dpm_module* good = dpm_require(ctx, "good");
CHECK(good != nullptr);
if (!good) {
dpm_close(ctx);
return harness_report("modules");
}
dpm_result result;
CHECK(dpm_execute(ctx, good, "payload", 0, nullptr, &result) == 0);
CHECK(result.status == DPM_RESULT_OK);
CHECK(result.data != nullptr);
CHECK(result.release != nullptr);
CHECK(result.data != nullptr &&
std::strcmp(static_cast<const char*>(result.data), "pong") == 0);
/* nothing has been released yet */
CHECK(dpm_execute(ctx, good, "releases", 0, nullptr, nullptr) == 0);
dpm_result_release(&result);
CHECK(result.data == nullptr);
CHECK(result.release == nullptr);
CHECK(result.status == DPM_RESULT_OK);
CHECK(dpm_execute(ctx, good, "releases", 0, nullptr, nullptr) == 1);
/* releasing again calls nothing */
dpm_result_release(&result);
CHECK(dpm_execute(ctx, good, "releases", 0, nullptr, nullptr) == 1);
/* a NULL envelope discards the payload before dispatch returns */
CHECK(dpm_execute(ctx, good, "payload", 0, nullptr, nullptr) == 0);
CHECK(dpm_execute(ctx, good, "releases", 0, nullptr, nullptr) == 2);
/* setting a payload outside any dispatch releases it at once */
dpm_set_result(ctx, &result, nullptr);
dpm_result_release(nullptr);
}
dpm_close(ctx);
return harness_report("modules");
}