#define _POSIX_C_SOURCE 200809L #include "sleep_writer.h" #include "sleep_crypto.h" #include #include #include #include #include #include #ifdef _WIN32 #include #include static char *test_mkdtemp(char *path) { int i; for (i = 0; i < 1000; i++) { char candidate[512]; int n = snprintf(candidate, sizeof(candidate), "%s-%ld-%d", path, (long)_getpid(), i); if (n < 0 || (size_t)n >= sizeof(candidate)) return NULL; if (_mkdir(candidate) == 0) { strcpy(path, candidate); return path; } } return NULL; } #else #include #define test_mkdtemp mkdtemp #endif int main(void) { sleep_keypair_t keypair; const uint8_t content[] = "block"; const uint8_t *blocks[] = {content}; const size_t lengths[] = {sizeof(content) - 1}; #ifdef _WIN32 char directory[] = "build/windows/datgpt-writer"; #else char directory[] = "/tmp/datgpt-writer-XXXXXX"; #endif assert(sleep_crypto_init() == 0); assert(sleep_gen_keypair(&keypair) == 0); assert(test_mkdtemp(directory) != NULL); errno = 0; assert(sleep_write_feed(directory, "../escape", blocks, lengths, 1, &keypair) == -1); assert(errno == EINVAL); errno = 0; assert(sleep_write_feed(directory, "unsafe\\name", blocks, lengths, 1, &keypair) == -1); assert(errno == EINVAL); assert(sleep_write_feed(directory, "null-block", (const uint8_t *const[]){NULL}, (const size_t[]){1}, 1, &keypair) == -1); assert(errno == EINVAL); assert(sleep_write_feed(directory, "content", blocks, lengths, 1, &keypair) == 0); { char path[512]; struct stat st; assert(snprintf(path, sizeof(path), "%s/content.data", directory) > 0); assert(stat(path, &st) == 0 && st.st_size == (off_t)(sizeof(content) - 1)); } return 0; }