/* Hooks under an executor that runs no processes (a build with no OS): * every firing journals one failure, nothing runs, nothing crashes, and * the journal stays sound. */ #include #include #include #include #include #include "hooks.h" #include "pic.h" static int failures; static void expect(int ok, const char *what) { if (!ok) { fprintf(stderr, "FAIL: %s\n", what); failures++; } } static long long count(pic_store *s, const char *sql) { sqlite3_stmt *q = NULL; long long n = -1; if (sqlite3_prepare_v2(s->db, sql, -1, &q, NULL) == SQLITE_OK && sqlite3_step(q) == SQLITE_ROW) { n = sqlite3_column_int64(q, 0); } sqlite3_finalize(q); return n; } int main(void) { const char *tmp = getenv("TMPDIR"); char dir[512], path[600], ran[600]; pic_executor none; pic_store s; FILE *f; int64_t first = 0; (void)snprintf(dir, sizeof dir, "%s/pic-test-hooks.XXXXXX", tmp && *tmp ? tmp : "/tmp"); if (!mkdtemp(dir)) { perror("mkdtemp"); return 1; } (void)snprintf(path, sizeof path, "%s/home", dir); (void)mkdir(path, 0700); (void)setenv("HOME", path, 1); (void)snprintf(path, sizeof path, "%s/home/.pic", dir); (void)mkdir(path, 0700); (void)snprintf(path, sizeof path, "%s/ws", dir); (void)mkdir(path, 0700); (void)snprintf(ran, sizeof ran, "%s/ran", dir); (void)snprintf(path, sizeof path, "%s/home/.pic/hooks.json", dir); if (!(f = fopen(path, "w"))) { perror(path); return 1; } fprintf(f, "{\"hooks\": {\"SessionStart\": [{\"hooks\": [{\"type\": \"command\", \"command\": \"touch %s\"}]}]," " \"UserPromptSubmit\": [{\"hooks\": [{\"type\": \"command\", \"command\": \"touch %s\"}," " {\"type\": \"command\", \"command\": \"touch %s.2\"}]}]}}\n", ran, ran, ran); fclose(f); none = *pic_executor_host(); none.spawn = NULL; pic_executor_set(&none); (void)snprintf(path, sizeof path, "%s/j", dir); if (pic_open(&s, path) != 0) { fprintf(stderr, "open: %s\n", s.err); return 1; } (void)snprintf(path, sizeof path, "%s/ws", dir); s.hooks = pic_hooks_load(path, NULL, NULL); expect(s.hooks != NULL, "hooks.json loads"); expect(pic_hooks_session_start(&s, "abcd1234", "startup") == 0, "SessionStart goes on without a spawn"); expect(pic_hooks_prompt(&s, "abcd1234", "hi", &first) == 0, "UserPromptSubmit neither blocks nor fails"); expect(count(&s, "SELECT count(*) FROM pic_hook_run WHERE command IS NOT NULL") == 2, "one failure row per firing"); expect(count(&s, "SELECT count(*) FROM pic_hook_run WHERE command IS NOT NULL AND state = 'done'" " AND error LIKE 'hooks are unavailable%'") == 2, "each recorded as hooks unavailable"); expect(count(&s, "SELECT count(*) FROM pic_hook_run WHERE command IS NULL") == 2, "each firing closed"); expect(access(ran, F_OK) != 0, "no command ran"); expect(pic_check(&s) == 0, "the journal stays sound"); pic_hooks_free(s.hooks); pic_close(&s); pic_executor_set(NULL); if (failures == 0) { printf("PASS: hooks under an executor without spawn: one recorded failure per firing, nothing run\n"); } return failures ? 1 : 0; }