Files
splinter/tests/test_remote_list.c
T
lotherk 3b0de63e06 protocol: the model list carries its tag too
The listings got a request tag so a late answer could not be read as the
next request's; `models` was left out. A probe that outlived the client's
15 s timeout stayed in the socket and the following plain `models` read it
as its own answer - /model then drew the endpoint's models where the
configuration was asked for, and the picker's second request drew a stale
list.

Test: tests/test_remote_list.c, fake server answers the first models
request twice and never the second. Before the fix the second call
returned 2 rows (FAIL nm == 0), after it 0. Full suite green, ASan clean
on the 46 test binaries.
2026-11-23 12:55:04 +00:00

274 lines
7.5 KiB
C

/*
* splinter -- the remote backend's listings: a late answer is not a row.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* What it guards: ui_remote.c caches the last listing it received (the event
* carries the rows, and the picker's callback loop runs after the answer). The
* counters used to be cleared when the NEXT answer arrived, so a server that
* did not answer at all left the previous listing in place - the picker drew
* one source's sessions under another source's name. Found 2026-11-22.
*
* The fake server speaks the real greeting and then answers exactly one
* "sessions" request; the second one is dropped on purpose.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#include "splinter.h"
#include "ui_backend.h"
static int failures;
#define CHECK(c) do { \
if (!(c)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); \
failures++; \
} \
} while (0)
struct seen {
char id[64][96];
int n;
};
static void
count_row(void *ctx, const char *id, const char *title, int current, long mtime)
{
struct seen *s = ctx;
(void)title;
(void)current;
(void)mtime;
if (s->n < 64)
snprintf(s->id[s->n++], sizeof(s->id[0]), "%s", id);
}
/* model rows: selector, label, current */
static void
collect_model(void *ctx, const char *sel, const char *label, int current)
{
struct seen *s = ctx;
(void)label;
(void)current;
if (s->n < 64)
snprintf(s->id[s->n++], sizeof(s->id[0]), "%s", sel);
}
/* the source rows come through a different callback shape */
static void
collect_source(void *ctx, const char *name, int count)
{
struct seen *s = ctx;
(void)count;
if (s->n < 64)
snprintf(s->id[s->n++], sizeof(s->id[0]), "%s", name);
}
/*
* A server that greets, answers the first request of each kind and then goes
* quiet. Which requests it answered is appended to `mark`, so a failing run
* can tell "never asked" from "asked and ignored".
*
* The first "sessions" answer is sent TWICE. A client that stops reading at the
* first event leaves the second one in the socket, where the next request of
* the same kind reads it as its own answer - the rows of the old request drawn
* under the new one's name. That is a different failure from "no answer at
* all", and only the duplicate catches it.
*/
/* the request's tag, or 0 when it has none: echoed back like the real server */
static long
tag_of(const char *line)
{
const char *p = strstr(line, "\"tag\":");
return p != NULL ? strtol(p + 6, NULL, 10) : 0;
}
static void
serve(int fd, const char *mark)
{
FILE *f, *m;
char line[1024];
int answered_sessions = 0, answered_sources = 0, answered_models = 0;
f = fdopen(fd, "r+");
if (f == NULL)
_exit(1);
m = fopen(mark, "a");
fprintf(f, "{\"ev\":\"ready\",\"protocol\":2,\"version\":\"test\"}\n");
fprintf(f, "{\"ev\":\"state\",\"session\":\"s-1\",\"dir\":\"/tmp\","
"\"model\":\"m\",\"agents\":[]}\n");
fprintf(f, "{\"ev\":\"history\",\"items\":[]}\n");
fprintf(f, "{\"ev\":\"commands\",\"names\":[]}\n");
fflush(f);
while (fgets(line, sizeof(line), f) != NULL) {
if (strstr(line, "\"cmd\":\"sessions\"") != NULL) {
long t = tag_of(line);
if (answered_sessions++)
continue; /* the second one is never answered */
fprintf(f, "{\"ev\":\"sessions\",\"tag\":%ld,\"items\":["
"{\"id\":\"erste\",\"title\":\"erste\",\"mtime\":1},"
"{\"id\":\"zweite\",\"title\":\"zweite\",\"mtime\":2}]}\n", t);
fprintf(f, "{\"ev\":\"sessions\",\"tag\":%ld,\"items\":["
"{\"id\":\"erste\",\"title\":\"erste\",\"mtime\":1},"
"{\"id\":\"zweite\",\"title\":\"zweite\",\"mtime\":2}]}\n", t);
if (m != NULL)
fputs("sessions\n", m);
} else if (strstr(line, "\"cmd\":\"sources\"") != NULL) {
long t = tag_of(line);
if (answered_sources++)
continue;
fprintf(f, "{\"ev\":\"sources\",\"tag\":%ld,\"items\":["
"{\"name\":\"alpha\",\"count\":3},"
"{\"name\":\"beta\",\"count\":4}]}\n", t);
if (m != NULL)
fputs("sources\n", m);
} else if (strstr(line, "\"cmd\":\"models\"") != NULL) {
long t = tag_of(line);
if (answered_models++)
continue;
fprintf(f, "{\"ev\":\"models\",\"tag\":%ld,\"items\":["
"{\"sel\":\"m1\",\"label\":\"m1\",\"current\":false},"
"{\"sel\":\"m2\",\"label\":\"m2\",\"current\":false}]}\n", t);
fprintf(f, "{\"ev\":\"models\",\"tag\":%ld,\"items\":["
"{\"sel\":\"m1\",\"label\":\"m1\",\"current\":false},"
"{\"sel\":\"m2\",\"label\":\"m2\",\"current\":false}]}\n", t);
if (m != NULL)
fputs("models\n", m);
} else {
continue;
}
fflush(f);
if (m != NULL)
fflush(m);
}
/* keep the socket open: a closed one is a different failure */
for (;;)
pause();
}
int
main(void)
{
char tmpl[] = "/tmp/splinter-remlist-XXXXXX";
char *dir = mkdtemp(tmpl);
char path[100], mark[200];
struct sockaddr_un addr;
struct seen s1, s2;
const struct ui_backend *be;
struct sp_buf err;
pid_t child;
int ls, fd, i;
int n1, n2;
if (dir == NULL) {
perror("mkdtemp");
return 1;
}
snprintf(path, sizeof(path), "%s/sock", dir);
snprintf(mark, sizeof(mark), "%s/mark", dir);
ls = socket(AF_UNIX, SOCK_STREAM, 0);
if (ls < 0) {
perror("socket");
return 1;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
if (bind(ls, (struct sockaddr *)&addr, sizeof(addr)) != 0 ||
listen(ls, 4) != 0) {
perror("bind/listen");
return 1;
}
child = fork();
if (child == 0) {
fd = accept(ls, NULL, NULL);
close(ls);
if (fd < 0)
_exit(1);
serve(fd, mark);
_exit(0);
}
close(ls);
sp_buf_init(&err);
be = sp_ui_backend_remote(path, &err);
if (be == NULL) {
fprintf(stderr, "FAIL attach: %s\n",
err.p != NULL ? err.p : "(no reason given)");
kill(child, SIGKILL);
waitpid(child, NULL, 0);
return 1;
}
/* the first listing is the one the server really sent */
memset(&s1, 0, sizeof(s1));
n1 = be->source_sessions(be->ctx, "splinter", count_row, &s1);
CHECK(n1 == 2);
CHECK(s1.n == 2 && strcmp(s1.id[0], "erste") == 0);
/* and now a second request the server does not answer */
memset(&s2, 0, sizeof(s2));
n2 = be->source_sessions(be->ctx, "claude", count_row, &s2);
if (n2 != 0) {
fprintf(stderr, "the unanswered request still drew %d row(s):", n2);
for (i = 0; i < s2.n; i++)
fprintf(stderr, " %s", s2.id[i]);
fprintf(stderr, "\n");
}
CHECK(n2 == 0);
CHECK(s2.n == 0);
/* the source list has the same shape of bug: one answered, one dropped */
{
int nsrc;
nsrc = be->sources(be->ctx, collect_source, &s2);
CHECK(nsrc == 2);
CHECK(s2.n == 2 && strcmp(s2.id[0], "alpha") == 0);
memset(&s2, 0, sizeof(s2));
nsrc = be->sources(be->ctx, collect_source, &s2);
CHECK(nsrc == 0);
CHECK(s2.n == 0);
}
/*
* And models: the picker's list has the same shape. The duplicate answer
* must not be read as the answer to the next request, or /model draws a
* stale list (or the probe's rows under the plain request's name).
*/
{
int nm;
memset(&s2, 0, sizeof(s2));
nm = be->models(be->ctx, collect_model, &s2);
CHECK(nm == 2);
CHECK(s2.n == 2 && strcmp(s2.id[0], "m1") == 0);
memset(&s2, 0, sizeof(s2));
nm = be->models(be->ctx, collect_model, &s2);
CHECK(nm == 0);
CHECK(s2.n == 0);
}
be->close(be->ctx);
kill(child, SIGKILL);
waitpid(child, NULL, 0);
sp_buf_free(&err);
unlink(path);
rmdir(dir);
if (failures == 0)
printf("test_remote_list: ok\n");
return failures != 0;
}