/* * splinter -- the cli transport: any program as the model. * * Deterministic: the "model" here is /bin/sh running sed, so a red test means * the transport is wrong, not an endpoint. What is checked: the prompt reaches * the program's stdin, its stdout becomes the answer, a program that prints * nothing is an error, and a non-zero exit carries the program's own words. */ #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) static int one_turn(struct sp_host *host, const char *text, struct sp_reply *out, struct sp_buf *err) { struct sp_transport *tr; struct sp_conv conv; tr = sp_host_transport(host, "cli"); CHECK(tr != NULL); if (tr == NULL) return -1; sp_conv_init(&conv); CHECK(sp_conv_push(&conv, "user", NULL, text) == 0); { int rc = tr->send(tr->ctx, &conv, out, err); sp_conv_free(&conv); return rc; } } int main(void) { struct sp_registry reg; struct sp_agent ag; struct sp_host *host, *host2, *host3; struct sp_buf err; struct sp_reply rep; memset(®, 0, sizeof(reg)); memset(&ag, 0, sizeof(ag)); if (sp_buf_init(&err) != 0) return 1; setenv("SPLINTER_CLI_CMD", "sed 's/^/X: /'", 1); host = sp_host_new(®, &ag, NULL); CHECK(host != NULL); CHECK(sp_plugin_load(host, "plugins/cli/cli.so", &err) == 0); CHECK(sp_host_transport(host, "cli") != NULL); CHECK(sp_host_transport(host, "nope") == NULL); memset(&rep, 0, sizeof(rep)); CHECK(one_turn(host, "hallo", &rep, &err) == 0); CHECK(rep.status == 200); CHECK(rep.content != NULL && strncmp(rep.content, "X: hallo", 8) == 0); sp_reply_free(&rep); /* a program that prints nothing must not look like an empty answer */ /* a fresh host: the transport name is taken once per host, on purpose */ setenv("SPLINTER_CLI_CMD", "true", 1); host2 = sp_host_new(®, &ag, NULL); CHECK(host2 != NULL); CHECK(sp_plugin_load(host2, "plugins/cli/cli.so", &err) == 0); sp_buf_clear(&err); memset(&rep, 0, sizeof(rep)); CHECK(one_turn(host2, "hallo", &rep, &err) == -1); CHECK(strstr(err.p, "no output") != NULL); sp_reply_free(&rep); /* a failing program says so, with its own output attached */ setenv("SPLINTER_CLI_CMD", "sh -c 'echo kaputt >&2; exit 3'", 1); host3 = sp_host_new(®, &ag, NULL); CHECK(host3 != NULL); CHECK(sp_plugin_load(host3, "plugins/cli/cli.so", &err) == 0); sp_buf_clear(&err); memset(&rep, 0, sizeof(rep)); CHECK(one_turn(host3, "hallo", &rep, &err) == -1); CHECK(strstr(err.p, "exit 3") != NULL); CHECK(strstr(err.p, "kaputt") != NULL); sp_reply_free(&rep); sp_host_free(host3); sp_host_free(host2); sp_host_free(host); sp_buf_free(&err); sp_registry_free(®); if (failures == 0) printf("test_cli: ok\n"); return failures == 0 ? 0 : 1; }