plugin NAME { path = ... enabled = false key = value } loads the plugin
(shipped one for NAME unless path says otherwise) and hands everything else
to that plugin as its configuration, read with sp_host_plugin_config() or
walked with sp_host_plugin_config_at(). Nested blocks give dotted keys,
lists arrive comma-separated.
The mcp plugin's servers are now server ID { url | cmd | header } blocks
inside plugin mcp { }; the old top-level mcp NAME { } and the flat
plugin = path / plugins = [ ... ] forms keep loading.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
157 lines
4.4 KiB
C
157 lines
4.4 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"
|
|
#include "config.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(®, 0, sizeof(reg));
|
|
memset(&ag, 0, sizeof(ag));
|
|
if (sp_buf_init(&err) != 0)
|
|
return 1;
|
|
|
|
host = sp_host_new(®, &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(®, "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(®, "broken") == NULL);
|
|
}
|
|
|
|
/* a plugin reads its own block: plugin lcm { keep = 40 ... } */
|
|
{
|
|
static struct sp_config conf;
|
|
const char *k = NULL;
|
|
|
|
CHECK(sp_host_plugin_config(host, "lcm", "keep") == NULL);
|
|
sp_config_init(&conf);
|
|
snprintf(conf.plugin_conf[0].plugin, 48, "lcm");
|
|
snprintf(conf.plugin_conf[0].key, 96, "keep");
|
|
snprintf(conf.plugin_conf[0].val, 512, "40");
|
|
snprintf(conf.plugin_conf[1].plugin, 48, "mcp");
|
|
snprintf(conf.plugin_conf[1].key, 96, "x");
|
|
snprintf(conf.plugin_conf[1].val, 512, "y");
|
|
snprintf(conf.plugin_conf[2].plugin, 48, "lcm");
|
|
snprintf(conf.plugin_conf[2].key, 96, "server.a.url");
|
|
snprintf(conf.plugin_conf[2].val, 512, "http://a");
|
|
conf.nplugin_conf = 3;
|
|
sp_host_set_config(host, &conf);
|
|
CHECK(sp_host_plugin_config(host, "lcm", "keep") != NULL &&
|
|
strcmp(sp_host_plugin_config(host, "lcm", "keep"), "40") == 0);
|
|
CHECK(sp_host_plugin_config(host, "lcm", "x") == NULL);
|
|
CHECK(sp_host_plugin_config_at(host, "lcm", 0, &k) != NULL &&
|
|
k != NULL && strcmp(k, "keep") == 0);
|
|
CHECK(sp_host_plugin_config_at(host, "lcm", 1, &k) != NULL &&
|
|
k != NULL && strcmp(k, "server.a.url") == 0);
|
|
CHECK(sp_host_plugin_config_at(host, "lcm", 2, &k) == NULL);
|
|
sp_host_set_config(host, NULL);
|
|
}
|
|
|
|
/* freeing the host stops the threads: no hang, no crash */
|
|
sp_host_free(host);
|
|
|
|
sp_buf_free(&err);
|
|
sp_registry_free(®);
|
|
if (failures == 0)
|
|
printf("test_plugin: ok\n");
|
|
return failures == 0 ? 0 : 1;
|
|
}
|