test_ack used one fixed /var/tmp path; a leftover or a second run could delete a request under another. It now mkdtemps a private directory (and one for the world-writable case) and waits 10 s instead of 5. test_gateway and test_ui check that the fake Bot API really started (readiness plus kill -0) and print its log instead of racing a dead server; the gateway waits 60 s for a turn under load. test_ui lets the input process the typed keys before Ctrl-L instead of assuming one delivery order. Full make check green.
178 lines
5.4 KiB
C
178 lines
5.4 KiB
C
/*
|
|
* The ack mechanism: a request is a file, an answer is a file, and any process
|
|
* may write it. Silence is not consent.
|
|
*/
|
|
#include <pthread.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#include "ack.h"
|
|
#include "splinter.h"
|
|
|
|
static int checks, fails;
|
|
|
|
#define CHECK(cond, what) do { \
|
|
checks++; \
|
|
if (!(cond)) { \
|
|
fails++; \
|
|
printf("FAIL %s (line %d)\n", what, __LINE__); \
|
|
} else { \
|
|
printf("ok %s\n", what); \
|
|
} \
|
|
} while (0)
|
|
|
|
static int answer_id;
|
|
static int answer_yes;
|
|
|
|
static void *
|
|
answerer(void *arg)
|
|
{
|
|
(void)arg;
|
|
{ struct timespec ts = { 0, 300000000L }; (void)nanosleep(&ts, NULL); }
|
|
(void)sp_ack_answer(answer_id, answer_yes, "test-phone");
|
|
return NULL;
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
char out[2048], who[64];
|
|
char dir[] = "/var/tmp/splinter-ack-XXXXXX";
|
|
char open_dir[] = "/var/tmp/splinter-ack-open-XXXXXX";
|
|
struct sp_buf log;
|
|
int id, yes;
|
|
pthread_t th;
|
|
|
|
CHECK(mkdtemp(dir) != NULL, "a private ack directory");
|
|
(void)setenv("SPLINTER_ACK_DIR", dir, 1);
|
|
|
|
id = sp_ack_request("run", "{\"command\":\"ls\"}", "mutate", 5);
|
|
CHECK(id > 0, "request is written and returns an id");
|
|
|
|
/* the answer comes from another thread, like another process would */
|
|
answer_id = id;
|
|
answer_yes = 1;
|
|
(void)pthread_create(&th, NULL, answerer, NULL);
|
|
yes = sp_ack_wait(id, 10, who, sizeof(who));
|
|
(void)pthread_join(th, NULL);
|
|
CHECK(yes == 1, "an answer of yes is read back");
|
|
CHECK(strcmp(who, "test-phone") == 0, "the answer says who gave it");
|
|
|
|
id = sp_ack_request("run", "{\"command\":\"rm -rf /\"}", "destructive", 1);
|
|
answer_id = id;
|
|
answer_yes = 0;
|
|
(void)pthread_create(&th, NULL, answerer, NULL);
|
|
yes = sp_ack_wait(id, 10, who, sizeof(who));
|
|
(void)pthread_join(th, NULL);
|
|
CHECK(yes == 0, "an answer of no is read back as no");
|
|
|
|
id = sp_ack_request("run", "slow", "mutate", 1);
|
|
CHECK(sp_ack_wait(id, 1, who, sizeof(who)) == 0,
|
|
"a timeout is a no, not a yes");
|
|
|
|
CHECK(sp_ack_answer(999999, 1, "nobody") != 0,
|
|
"answering a request that does not exist fails");
|
|
|
|
id = sp_ack_request("edit", "{\"path\":\"/etc/passwd\"}", "mutate", 30);
|
|
if (sp_buf_init(&log) == 0) {
|
|
(void)sp_ack_list(&log);
|
|
snprintf(out, sizeof(out), "%s", log.p != NULL ? log.p : "");
|
|
sp_buf_free(&log);
|
|
CHECK(strstr(out, "\"path\":\"/etc/passwd\"") != NULL,
|
|
"the pending list shows what is being asked");
|
|
}
|
|
(void)sp_ack_answer(id, 0, "test");
|
|
|
|
/*
|
|
* ---- an answer belongs to one request, once, in time ----
|
|
* The id used to be the pid: a "yes" that came too late stayed on disk
|
|
* and approved the next request of the same process unseen.
|
|
*/
|
|
{
|
|
int a, b;
|
|
|
|
a = sp_ack_request("run", "first", "destructive", 1);
|
|
b = sp_ack_request("run", "second", "destructive", 1);
|
|
CHECK(a > 0 && b > 0 && a != b,
|
|
"two requests of one process get two ids");
|
|
CHECK(a != (int)getpid(), "the id is not the pid");
|
|
(void)sp_ack_answer(b, 0, "test");
|
|
(void)sp_ack_wait(b, 1, who, sizeof(who));
|
|
|
|
CHECK(sp_ack_wait(a, 1, who, sizeof(who)) == 0,
|
|
"an unanswered request times out as no");
|
|
CHECK(sp_ack_answer(a, 1, "late") != 0,
|
|
"an answer after the timeout is refused");
|
|
id = sp_ack_request("run", "third", "destructive", 1);
|
|
CHECK(sp_ack_wait(id, 1, who, sizeof(who)) == 0,
|
|
"a late yes does not approve the next request");
|
|
|
|
id = sp_ack_request("run", "fourth", "destructive", 5);
|
|
CHECK(sp_ack_answer(id, 0, "first") == 0, "the first answer counts");
|
|
CHECK(sp_ack_answer(id, 1, "second") != 0,
|
|
"a second answer to the same request is refused");
|
|
CHECK(sp_ack_wait(id, 5, who, sizeof(who)) == 0 &&
|
|
strcmp(who, "first") == 0, "the first answer wins");
|
|
}
|
|
|
|
/*
|
|
* ---- the channel message says what is approved, and is only data ----
|
|
* It used to be "Zustimmung <id>" alone, so a phone said yes to a
|
|
* number.
|
|
*/
|
|
{
|
|
char chan[700], got[700], mark[700], args[1200];
|
|
FILE *f;
|
|
char msg[1024] = "";
|
|
|
|
snprintf(chan, sizeof(chan), "%s/chan.sh", dir);
|
|
snprintf(got, sizeof(got), "%s/chan.out", dir);
|
|
snprintf(mark, sizeof(mark), "%s/pwned", dir);
|
|
f = fopen(chan, "w");
|
|
if (f != NULL) {
|
|
fprintf(f, "#!/bin/sh\nprintf '%%s' \"$1\" > %s\n", got);
|
|
fclose(f);
|
|
}
|
|
(void)chmod(chan, 0700);
|
|
(void)setenv("SPLINTER_ACK_CHANNELS", chan, 1);
|
|
snprintf(args, sizeof(args),
|
|
"{\"command\":\"x'; touch %s; '\"}", mark);
|
|
id = sp_ack_request("run", args, "destructive", 5);
|
|
CHECK(sp_ack_notify(id, NULL) == 1, "the channel was fired");
|
|
f = fopen(got, "r");
|
|
if (f != NULL) {
|
|
size_t n = fread(msg, 1, sizeof(msg) - 1, f);
|
|
|
|
msg[n] = '\0';
|
|
fclose(f);
|
|
}
|
|
CHECK(strstr(msg, "run") != NULL && strstr(msg, "touch") != NULL,
|
|
"the message names the tool and its arguments");
|
|
CHECK(access(mark, F_OK) != 0,
|
|
"a quote in the arguments runs nothing");
|
|
(void)sp_ack_answer(id, 0, "test");
|
|
(void)sp_ack_wait(id, 1, who, sizeof(who));
|
|
(void)unsetenv("SPLINTER_ACK_CHANNELS");
|
|
}
|
|
|
|
/* ---- a directory others may write into is not trusted with a yes ---- */
|
|
CHECK(mkdtemp(open_dir) != NULL, "a directory for the permission test");
|
|
(void)chmod(open_dir, 0777);
|
|
(void)setenv("SPLINTER_ACK_DIR", open_dir, 1);
|
|
CHECK(sp_ack_request("run", "x", "destructive", 1) < 0,
|
|
"a world-writable ack directory is refused");
|
|
{
|
|
char cmd[700];
|
|
|
|
snprintf(cmd, sizeof(cmd), "rm -rf '%s' '%s'", dir, open_dir);
|
|
(void)system(cmd);
|
|
}
|
|
|
|
printf("\n%d checks, %d failures\n", checks, fails);
|
|
return fails == 0 ? 0 : 1;
|
|
}
|