#include #include #include #include #include #include #include #include #define STATUS_FIFO "/tmp/looper_status" #define CMD_FIFO "/tmp/looper_cmd" static int write_cmd(const char *cmd) { int fd = open(CMD_FIFO, O_WRONLY | O_NONBLOCK); if (fd < 0) return -1; size_t len = strlen(cmd); int n = write(fd, cmd, len); if (n == (int)len && cmd[len-1] != '\n') write(fd, "\n", 1); close(fd); return (n >= 0) ? 0 : -1; } static int read_status_line(char *buf, size_t bufsize) { int fd = open(STATUS_FIFO, O_RDONLY | O_NONBLOCK); if (fd < 0) return -1; FILE *f = fdopen(fd, "r"); if (!f) { close(fd); return -1; } if (fgets(buf, bufsize, f) == NULL) { fclose(f); return -1; } fclose(f); return 0; } static pid_t start_looper(void) { pid_t pid = fork(); if (pid < 0) { perror("fork"); return -1; } if (pid == 0) { close(2); open("/dev/null", O_WRONLY); execl("./looper", "looper", NULL); perror("execl"); _exit(1); } return pid; } static int test_status_after_record(void) { printf("Test: status FIFO reports recording state\n"); pid_t pid = start_looper(); if (pid < 0) return 1; usleep(200000); if (write_cmd("record 0") != 0) { fprintf(stderr, " FAIL: cannot write record command\n"); kill(pid, SIGTERM); waitpid(pid, NULL, 0); return 1; } usleep(500000); char line[256]; if (read_status_line(line, sizeof(line)) != 0) { fprintf(stderr, " FAIL: cannot read status line\n"); kill(pid, SIGTERM); waitpid(pid, NULL, 0); return 1; } int ch, sc; char state[32]; if (sscanf(line, "CH=%d SC=%d STATE=%31s", &ch, &sc, state) != 3) { fprintf(stderr, " FAIL: malformed status line: %s\n", line); kill(pid, SIGTERM); waitpid(pid, NULL, 0); return 1; } if (ch != 0 || sc != 0 || strcmp(state, "RECORD") != 0) { fprintf(stderr, " FAIL: expected CH=0 SC=0 STATE=RECORD, got: CH=%d SC=%d STATE=%s\n", ch, sc, state); kill(pid, SIGTERM); waitpid(pid, NULL, 0); return 1; } printf(" PASS\n"); kill(pid, SIGTERM); waitpid(pid, NULL, 0); return 0; } int main(void) { int fail = 0; fail += test_status_after_record(); return fail; }