Files
splinter/tests/test_plugin.c
T
Konrad e4f910cc4e splinter: plugin ABI, widget sidebar, libsplinter
A plugin is a .so against libsplinter.so, exporting sp_plugin_describe().
The layout changes so there is exactly one copy of each symbol in the
process: all logic lives in libsplinter.so and both the binary and every
plugin link against it. Two copies would have meant two registries.

What the ABI offers:
- tools with the authorization classes the loop already enforces, so the
  decision stays in the harness and not in the plugin;
- widgets rendered in the sidebar, one line per widget (name: text);
- a worker thread per widget with tick(), which may block, read files, wait
  on sockets - and which cannot touch curses at all;
- sp_host_widget_text() as the only thread-safe write path: the host copies
  the string under a mutex, so a stack buffer is safe and the UI never holds
  a pointer a worker could free.

Error paths are part of the design: a wrong ABI number is refused with both
numbers in the message, a failed init is reported and skipped, a duplicate
widget name is refused, a tool without a run function is not registered, and
nothing is dlclose()d while a thread might be inside it.

Layout: chat on three quarters, sidebar on one (session, transport, theme,
last-turn numbers, widgets), status bar underneath. The clock example plugin
proves the path end to end - its widget publishes 11:45:23 into the sidebar
from its own thread.

Seven test binaries green; test_plugin loads the real clock.so with dlopen
and checks the worker actually published, plus every refusal above.
2026-11-07 11:47:47 +00:00

127 lines
3.2 KiB
C

/*
* splinter -- plugin host tests.
*
* These run against the real libsplinter.so and the real clock.so built by
* make: the loader is exercised with dlopen, not simulated. What is proven
* here is that a plugin loads, registers a tool and a widget, that the widget's
* worker thread actually runs and publishes, and that the error paths fail
* loudly instead of half-loading.
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "splinter.h"
#include "splinter_plugin.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static void
nap_ms(long ms)
{
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000L;
nanosleep(&ts, NULL);
}
static int
tick_nothing(struct sp_host *h, const char *name, void *ctx)
{
(void)h;
(void)name;
(void)ctx;
return 0;
}
int
main(void)
{
struct sp_registry reg;
struct sp_agent ag;
struct sp_host *host;
struct sp_buf err;
char snap[1024];
struct sp_widget w;
memset(&reg, 0, sizeof(reg));
memset(&ag, 0, sizeof(ag));
if (sp_buf_init(&err) != 0)
return 1;
host = sp_host_new(&reg, &ag, NULL);
CHECK(host != NULL);
CHECK(sp_host_abi(host) == SP_PLUGIN_ABI);
CHECK(sp_host_widget_count(host) == 0);
/* a shared object that is not a plugin fails, and says why */
sp_buf_clear(&err);
CHECK(sp_plugin_load(host, "libsplinter.so", &err) == -1);
CHECK(strstr(err.p, "sp_plugin_describe") != NULL);
/* the real plugin: one widget and one tool */
sp_buf_clear(&err);
CHECK(sp_plugin_load(host, "plugins/clock/clock.so", &err) == 0);
CHECK(sp_host_widget_count(host) == 1);
CHECK(sp_registry_find(&reg, "clock") != NULL);
/* its worker thread publishes within a second (interval is 1000 ms) */
nap_ms(1300);
snap[0] = '\0';
CHECK(sp_host_widget_snapshot(host, snap, sizeof(snap)) > 0);
CHECK(strstr(snap, "clock: ") != NULL);
{
const char *p = strchr(snap, ':');
/* HH:MM:SS, so at least two more colons after the label */
CHECK(p != NULL);
if (p != NULL) {
CHECK(strchr(p + 1, ':') != NULL);
CHECK(strchr(strchr(p + 1, ':') + 1, ':') != NULL);
}
}
/* a second widget, added by hand, and a duplicate name refused */
memset(&w, 0, sizeof(w));
w.name = "manual";
w.tick = tick_nothing;
w.interval_ms = 50;
CHECK(sp_host_add_widget(host, &w) == 0);
CHECK(sp_host_add_widget(host, &w) == -1);
CHECK(sp_host_widget_count(host) == 2);
/* publishing from the main thread works and is visible in the snapshot */
CHECK(sp_host_widget_text(host, "manual", "hello from main") == 0);
CHECK(sp_host_widget_text(host, "nope", "x") == -1);
snap[0] = '\0';
sp_host_widget_snapshot(host, snap, sizeof(snap));
CHECK(strstr(snap, "manual: hello from main") != NULL);
/* a bad tool is refused, not registered half-way */
{
struct sp_tool t;
memset(&t, 0, sizeof(t));
t.name = "broken";
CHECK(sp_host_add_tool(host, &t) == -1);
CHECK(sp_registry_find(&reg, "broken") == NULL);
}
/* freeing the host stops the threads: no hang, no crash */
sp_host_free(host);
sp_buf_free(&err);
sp_registry_free(&reg);
if (failures == 0)
printf("test_plugin: ok\n");
return failures == 0 ? 0 : 1;
}