Files
splinter/tests/test_provstate.c
T
lotherk 4cdaf2680b store: a write that cannot finish no longer replaces the good file
jobrec_write(), write_sidecar() and provstate's write_state() wrote into a
temp, ignored the result and renamed it over the live file. A full disk (or
EIO) then left a torn json where a job record or a key's quota state used to
be. For provstate that is the dangerous direction: a truncated state parses as
all zeroes, so "this key is out of quota until 19:00" became "admit it" and the
harness hammered a spent account.

All three now fflush+fsync+fclose, unlink the temp when any step fails, and
only then rename. Test proof, both tests fail on the old code:

  test_provstate.c  RLIMIT_FSIZE=8, write_state fails -> last_status still 429,
                    preflight still waits
  test_jobs.c       RLIMIT_FSIZE=8, delegate_retry cannot write -> the record on
                    disk is still a whole json, no .tmp left behind

make check: all tests passed. tests/check_asan.sh: all 45 binaries clean.
2026-11-23 06:33:12 +00:00

136 lines
4.3 KiB
C

/*
* splinter -- shared provider/key state.
*
* The state is what makes two splinter processes agree that a key is out of
* quota: one writes, the other reads, and the answer is the same. HOME points
* at a fresh directory, so the test never touches the real ~/.splinter.
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "provstate.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)
{
char dir[] = "/tmp/splinter-prov-XXXXXX";
char path[700], fp1[32], fp2[32];
struct sp_prov_state st;
struct stat sb;
long now, ready = 0;
char reason[128];
CHECK(mkdtemp(dir) != NULL);
CHECK(setenv("HOME", dir, 1) == 0);
now = (long)time(NULL);
/* no state yet: admit */
CHECK(sp_provstate_load("openai", "key-a", &st) != 0);
CHECK(sp_provstate_preflight("openai", "key-a", now, &ready, reason,
sizeof(reason)) == 0);
CHECK(ready == 0);
/* a 429 with Retry-After becomes a quota wait both processes can see */
sp_provstate_account("openai", "key-a", 429, 10, 20, 3600);
CHECK(sp_provstate_load("openai", "key-a", &st) == 0);
CHECK(st.last_status == 429);
CHECK(st.req_count == 1);
CHECK(st.tok_in == 10 && st.tok_out == 20);
CHECK(st.quota_ready_at > now && st.quota_ready_at <= now + 3601);
CHECK(sp_provstate_preflight("openai", "key-a", now, &ready, reason,
sizeof(reason)) == 1);
CHECK(ready == st.quota_ready_at);
CHECK(strstr(reason, "quota") != NULL);
/* the state file is private */
snprintf(path, sizeof(path), "%s/.splinter/providers/openai", dir);
CHECK(stat(path, &sb) == 0);
CHECK((sb.st_mode & 077) == 0);
CHECK(sp_provstate_base("openai", "key-a", path, sizeof(path)) != NULL);
/* a 503 is a short backoff, on its own key */
sp_provstate_account("openai", "key-b", 503, 0, 0, 30);
CHECK(sp_provstate_load("openai", "key-b", &st) == 0);
CHECK(st.backoff_until > now);
CHECK(sp_provstate_preflight("openai", "key-b", now, &ready, reason,
sizeof(reason)) == 2);
CHECK(strstr(reason, "backoff") != NULL);
/* keys are separate: another key is still admitted */
CHECK(sp_provstate_preflight("openai", "key-c", now, &ready, reason,
sizeof(reason)) == 0);
/* the fingerprint is stable and does not contain the secret */
CHECK(sp_key_fingerprint("secret-one", fp1, sizeof(fp1)) != NULL);
CHECK(sp_key_fingerprint("secret-one", fp2, sizeof(fp2)) != NULL);
CHECK(strcmp(fp1, fp2) == 0);
CHECK(strstr(fp1, "secret") == NULL);
CHECK(sp_key_fingerprint("secret-two", fp2, sizeof(fp2)) != NULL);
CHECK(strcmp(fp1, fp2) != 0);
CHECK(sp_key_fingerprint("", fp2, sizeof(fp2)) == NULL);
/*
* ---- a write that cannot finish must not rewrite the state ----
*
* The file used to be written with fprintf() into a temp, fclose() was
* ignored and the temp renamed over the good file. A full disk then
* replaced "this key is out of quota until ready_at" with a torn json
* that parses as all zeroes - the preflight admitted a key the state
* says to wait for. RLIMIT_FSIZE makes the short write deterministic.
*/
{
struct rlimit rl;
struct sigaction sa, oldsa;
long short_ready = 0;
sp_provstate_account("short", "k", 429, 1, 1, 3600);
CHECK(sp_provstate_load("short", "k", &st) == 0);
CHECK(sp_provstate_preflight("short", "k", now, &short_ready,
reason, sizeof(reason)) == 1);
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
CHECK(sigaction(SIGXFSZ, &sa, &oldsa) == 0);
CHECK(getrlimit(RLIMIT_FSIZE, &rl) == 0);
{
struct rlimit lim = rl;
lim.rlim_cur = 8; /* shorter than the json */
CHECK(setrlimit(RLIMIT_FSIZE, &lim) == 0);
}
/* this write cannot finish: the good state must survive */
sp_provstate_account("short", "k", 200, 1, 1, 0);
CHECK(setrlimit(RLIMIT_FSIZE, &rl) == 0);
CHECK(sigaction(SIGXFSZ, &oldsa, NULL) == 0);
CHECK(sp_provstate_load("short", "k", &st) == 0);
CHECK(st.last_status == 429);
short_ready = 0;
CHECK(sp_provstate_preflight("short", "k", now, &short_ready,
reason, sizeof(reason)) == 1);
}
snprintf(path, sizeof(path), "rm -rf '%s'", dir);
(void)system(path);
if (failures == 0)
printf("test_provstate: ok\n");
return failures == 0 ? 0 : 1;
}