/* * splinter -- memory scoping. * * The one thing that must never break: the user id. Every request carries it, * and it comes from who is running - not from a setting someone can forget. */ #include #include #include #include #include #include "splinter.h" #include "config.h" #include "memory.h" static int failures; #define CHECK(c) do { if (!(c)) { fprintf(stderr, "FAIL %s:%d: %s\n", \ __FILE__, __LINE__, #c); failures++; } } while (0) int main(void) { const char *user; /* from the environment */ CHECK(setenv("USER", "lotherk", 1) == 0); user = sp_memory_user(); CHECK(user != NULL); CHECK(strcmp(user, "lotherk") == 0); /* without USER it falls back to the passwd entry, never to empty */ CHECK(unsetenv("USER") == 0); user = sp_memory_user(); CHECK(user != NULL); CHECK(strlen(user) > 0); /* * ---- memory_key_env reaches the server, and only through stdin ---- * It used to become SPLINTER_MEM_KEY in curl's environment, which curl * never reads: no request carried a key. A fake curl records its argv * and the config it is handed. */ { char dir[] = "/var/tmp/splinter-mem-XXXXXX"; char path[600], line[1400], cfgf[620], argf[620]; char buf[4096]; const char *oldpath = getenv("PATH"); static struct sp_config c; struct sp_registry r; struct sp_buf out; const struct sp_tool *t; FILE *f; size_t n; CHECK(mkdtemp(dir) != NULL); snprintf(cfgf, sizeof(cfgf), "%s/cfg", dir); snprintf(argf, sizeof(argf), "%s/args", dir); snprintf(path, sizeof(path), "%s/curl", dir); f = fopen(path, "w"); CHECK(f != NULL); if (f != NULL) { fprintf(f, "#!/bin/sh\nprintf '%%s\\n' \"$@\" > %s\n" "cat > %s\necho '{\"results\":[]}'\n", argf, cfgf); fclose(f); } (void)chmod(path, 0755); snprintf(line, sizeof(line), "%s:%s", dir, oldpath != NULL ? oldpath : "/usr/bin:/bin"); (void)setenv("PATH", line, 1); (void)setenv("MY_MEM0_KEY", "m0-geheim", 1); sp_config_init(&c); snprintf(c.memory_url, sizeof(c.memory_url), "http://mem0.example"); snprintf(c.memory_key_env, sizeof(c.memory_key_env), "MY_MEM0_KEY"); memset(&r, 0, sizeof(r)); CHECK(sp_memory_register(&r, &c) == 0); CHECK(sp_buf_init(&out) == 0); t = sp_registry_find(&r, "memory_search"); CHECK(t != NULL); if (t != NULL) (void)t->run("{\"query\":\"x\"}", &out, t->ctx); buf[0] = '\0'; if ((f = fopen(cfgf, "r")) != NULL) { n = fread(buf, 1, sizeof(buf) - 1, f); buf[n] = '\0'; fclose(f); } CHECK(strstr(buf, "Authorization: Token m0-geheim") != NULL); buf[0] = '\0'; if ((f = fopen(argf, "r")) != NULL) { n = fread(buf, 1, sizeof(buf) - 1, f); buf[n] = '\0'; fclose(f); } CHECK(buf[0] != '\0' && strstr(buf, "m0-geheim") == NULL); sp_buf_free(&out); sp_registry_free(&r); (void)setenv("PATH", oldpath != NULL ? oldpath : "/usr/bin:/bin", 1); snprintf(line, sizeof(line), "rm -rf '%s'", dir); (void)system(line); } if (failures == 0) printf("test_memory: ok\n"); return failures == 0 ? 0 : 1; }