/* * splinter -- a plugin's life: a failed init leaves nothing behind, and * shutdown() is called when the host goes. * * Both were promised by the header and neither happened: an init that failed * half-way kept its tools and its widget thread, and shutdown() was never * called by anybody. */ #include #include #include #include #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) int main(void) { struct sp_registry reg; struct sp_agent ag; struct sp_host *host; struct sp_buf err; char mark[] = "/var/tmp/splinter-lifecycle-XXXXXX"; int fd; fd = mkstemp(mark); CHECK(fd >= 0); if (fd >= 0) close(fd); (void)unlink(mark); (void)setenv("SP_TEST_SHUTDOWN_FILE", mark, 1); CHECK(sp_buf_init(&err) == 0); /* ---- init fails after registering: nothing of it stays ---- */ memset(®, 0, sizeof(reg)); memset(&ag, 0, sizeof(ag)); host = sp_host_new(®, &ag, NULL); CHECK(host != NULL); (void)setenv("SP_TEST_INIT_FAIL", "1", 1); CHECK(sp_plugin_load(host, "tests/plugin_lifecycle.so", &err) != 0); CHECK(sp_registry_find(®, "lifecycle_tool") == NULL); CHECK(sp_host_widget_count(host) == 0); sp_host_free(host); /* joins nothing, calls no shutdown */ CHECK(access(mark, F_OK) != 0); sp_registry_free(®); /* ---- init succeeds: the plugin is used, then shut down ---- */ memset(®, 0, sizeof(reg)); host = sp_host_new(®, &ag, NULL); CHECK(host != NULL); (void)unsetenv("SP_TEST_INIT_FAIL"); sp_buf_clear(&err); CHECK(sp_plugin_load(host, "tests/plugin_lifecycle.so", &err) == 0); CHECK(sp_registry_find(®, "lifecycle_tool") != NULL); CHECK(sp_host_widget_count(host) == 1); sp_host_free(host); CHECK(access(mark, F_OK) == 0); (void)unlink(mark); sp_registry_free(®); sp_buf_free(&err); if (failures == 0) printf("test_lifecycle: ok\n"); return failures == 0 ? 0 : 1; }