Artifact 9c4412939b0751c67693a7c7a7e5b2d4ea90387a56b39ce73ce38679ca607832:
- File
test/pic/test_files.c
— part of check-in
[0cc6f109a2]
at
2026-11-20 00:08:59
on branch images
— Images: validate at entry, cap per request, prune by retention
Every image is checked from its header (PNG IHDR with CRC, JPEG frame header, GIF screen, WebP) as Fossil sniffs types: an invalid header, a type that isn't the declared one, more than 8000 px a side or over 4.5 MB is refused; the stored type is the sniffed one, with width and height. RPC image errors are reported, never dropped. A request carries the newest 20 images up to 20 MB; older ones become Pi's placeholder. Image bytes are dropped once compacted away or after imageRetentionDays (default 30; 0 keeps them), leaving hash, type and size and an "[image removed]" placeholder; pic prune [--dry-run] runs it, and it also runs at open at most hourly. pic check re-hashes images. Imports keep images. Journal version 10. (user: dmestas, size: 13022) [annotate] [blame] [check-ins using]
/* The host executor's file operations: stat, read, write, mkdir, list, * remove, with the errno values the tools report. */ #include <assert.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> #include "pic.h" static int count(void *cb_state, const char *entry) { assert(strcmp(entry, ".") != 0 && strcmp(entry, "..") != 0); ++*(int *)cb_state; return 0; } static const unsigned char png[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4, 0x89, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0xf8, 0xcf, 0xc0, 0xf0, 0x1f, 0x00, 0x05, 0x00, 0x01, 0xff, 0x89, 0x99, 0x3d, 0x1d, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82}; static const char png_b64[] = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGP4z8DwHwAFAAH/iZk9HQAAAABJRU5ErkJggg=="; static int no_journal(void *ctx, const char *plan, size_t plan_len) { (void)ctx; (void)plan; (void)plan_len; return 0; } /* image.c: Pi's detection, inline types, limit and notes; base64. */ static void images(void) { static const unsigned char apng[] = {0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 13, 'I', 'H', 'D', 'R', 0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 'a', 'c', 'T', 'L', 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}; static const unsigned char bmp[30] = {'B', 'M', 0, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 24, 0}; unsigned char raw[128]; char enc[128]; size_t n; pic_image list[3]; char *text; assert(strcmp(pic_image_sniff(png, sizeof png), "image/png") == 0); assert(!pic_image_sniff(apng, sizeof apng)); /* animated: Pi refuses it */ assert(strcmp(pic_image_sniff((const unsigned char *)"\xff\xd8\xff\xe0", 4), "image/jpeg") == 0); assert(!pic_image_sniff((const unsigned char *)"\xff\xd8\xff\xf7", 4)); assert(strcmp(pic_image_sniff((const unsigned char *)"GIF89a..", 8), "image/gif") == 0); assert(strcmp(pic_image_sniff((const unsigned char *)"RIFF\0\0\0\0WEBPVP8 ", 16), "image/webp") == 0); assert(strcmp(pic_image_sniff(bmp, sizeof bmp), "image/bmp") == 0); assert(!pic_image_sniff((const unsigned char *)"hello", 5)); assert(strcmp(pic_image_inline_mime(" IMAGE/JPG; q=1"), "image/jpeg") == 0); assert(!pic_image_inline_mime("image/bmp") && !pic_image_inline_mime("image/svg+xml")); n = pic_base64_encode(png, sizeof png, enc); assert(n == strlen(png_b64) && strcmp(enc, png_b64) == 0); assert(pic_base64_decode(png_b64, strlen(png_b64), raw) == sizeof png && memcmp(raw, png, sizeof png) == 0); assert(pic_base64_decode("aGk", 3, raw) == 2 && memcmp(raw, "hi", 2) == 0); /* padding optional */ assert(pic_base64_decode("a-_\nw", 5, raw) == 3); /* URL-safe, whitespace */ assert(pic_base64_decode("a*bc", 4, raw) == (size_t)-1 && pic_base64_decode("ab=c", 4, raw) == (size_t)-1); /* Headers: the type from the bytes, and the size. */ { static const unsigned char wide[] = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x23, 0x28, 0x00, 0x00, 0x00, 0x0a, 0x08, 0x06, 0x00, 0x00, 0x00, 0xd9, 0xe4, 0xf5, 0x86}; /* 9000x10, CRC right */ static const unsigned char jpeg[] = {0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 'J', 'F', 'I', 'F', 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0xff, 0xff, 0xc0, 0x00, 0x11, 0x08, 0x00, 0x02, 0x00, 0x03, 0x03, 1, 0x22, 0, 2, 0x11, 1, 3, 0x11, 1, 0xff, 0xd9}; static const unsigned char jpeg_noframe[] = {0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 'J', 'F', 'I', 'F', 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0xff, 0xda, 0x00, 0x02, 0xff, 0xd9}; static const unsigned char gif[] = {'G', 'I', 'F', '8', '9', 'a', 5, 0, 7, 0, 0, 0, 0}; static const unsigned char vp8[] = {'R', 'I', 'F', 'F', 22, 0, 0, 0, 'W', 'E', 'B', 'P', 'V', 'P', '8', ' ', 10, 0, 0, 0, 0x10, 0, 0, 0x9d, 0x01, 0x2a, 40, 0, 30, 0}; static const unsigned char vp8l[] = {'R', 'I', 'F', 'F', 22, 0, 0, 0, 'W', 'E', 'B', 'P', 'V', 'P', '8', 'L', 10, 0, 0, 0, 0x2f, 9, 0x40, 1, 0, 0, 0, 0, 0, 0}; static const unsigned char vp8x[] = {'R', 'I', 'F', 'F', 22, 0, 0, 0, 'W', 'E', 'B', 'P', 'V', 'P', '8', 'X', 10, 0, 0, 0, 0, 0, 0, 0, 99, 0, 0, 49, 0, 0}; unsigned char bad[sizeof png]; char why[160]; pic_image img = {NULL, png, sizeof png, 0, 0}; assert(pic_image_check(&img, NULL, why, sizeof why) == PIC_IMAGE_OK && strcmp(img.mime, "image/png") == 0 && img.width == 1 && img.height == 1); img = (pic_image){NULL, png, sizeof png, 0, 0}; assert(pic_image_check(&img, "image/jpeg", why, sizeof why) == PIC_IMAGE_MISMATCH && strcmp(why, "declared image/jpeg but the data is image/png") == 0); memcpy(bad, png, sizeof png); bad[20] ^= 1; /* the height, under a CRC that no longer matches */ img = (pic_image){NULL, bad, sizeof bad, 0, 0}; assert(pic_image_check(&img, "image/png", why, sizeof why) == PIC_IMAGE_INVALID && strstr(why, "header is invalid")); img = (pic_image){NULL, wide, sizeof wide, 0, 0}; assert(pic_image_inspect(&img, NULL, NULL, 0) == PIC_IMAGE_OK && img.width == 9000 && img.height == 10); assert(pic_image_check(&img, NULL, why, sizeof why) == PIC_IMAGE_TOO_LARGE && strstr(why, "9000x10")); img = (pic_image){NULL, jpeg, sizeof jpeg, 0, 0}; assert(pic_image_check(&img, "image/jpg", NULL, 0) == PIC_IMAGE_OK && img.width == 3 && img.height == 2); img = (pic_image){NULL, jpeg_noframe, sizeof jpeg_noframe, 0, 0}; assert(pic_image_check(&img, NULL, NULL, 0) == PIC_IMAGE_INVALID); img = (pic_image){NULL, gif, sizeof gif, 0, 0}; assert(pic_image_check(&img, NULL, NULL, 0) == PIC_IMAGE_OK && img.width == 5 && img.height == 7); img = (pic_image){NULL, vp8, sizeof vp8, 0, 0}; assert(pic_image_check(&img, "image/webp", NULL, 0) == PIC_IMAGE_OK && img.width == 40 && img.height == 30); img = (pic_image){NULL, vp8l, sizeof vp8l, 0, 0}; assert(pic_image_check(&img, NULL, NULL, 0) == PIC_IMAGE_OK && img.width == 10 && img.height == 6); img = (pic_image){NULL, vp8x, sizeof vp8x, 0, 0}; assert(pic_image_check(&img, NULL, NULL, 0) == PIC_IMAGE_OK && img.width == 100 && img.height == 50); img = (pic_image){NULL, bmp, sizeof bmp, 0, 0}; assert(pic_image_check(&img, NULL, NULL, 0) == PIC_IMAGE_UNSUPPORTED && strcmp(img.mime, "image/bmp") == 0); assert(strcmp(pic_image_note(PIC_IMAGE_TOO_LARGE), PIC_IMAGE_TOO_BIG) == 0); assert(strcmp(pic_image_note(PIC_IMAGE_INVALID), PIC_IMAGE_UNCONVERTIBLE) == 0); /* Under 4.5MB of base64 fits: 3538942 bytes encode to exactly that. */ img = (pic_image){"image/png", png, 3538941, 1, 1}; assert(pic_image_fit(&img, NULL, 0) == PIC_IMAGE_OK); img.len = 3538942; assert(pic_image_fit(&img, why, sizeof why) == PIC_IMAGE_TOO_LARGE && strstr(why, "4.5MB")); } /* A BMP becomes Pi's note after a blank line; good images keep their * sniffed type; anything else fails the message, naming the image. */ list[0] = (pic_image){"image/bmp", bmp, sizeof bmp, 0, 0}; list[1] = (pic_image){"image/jpg", png, sizeof png, 0, 0}; n = 2; text = pic_image_prepare("look", list, &n, enc, sizeof enc); assert(!text && strcmp(enc, "image 2: declared image/jpg but the data is image/png") == 0); list[1] = (pic_image){NULL, png, sizeof png, 0, 0}; list[2] = (pic_image){"image/bmp", bmp, sizeof bmp, 0, 0}; n = 3; text = pic_image_prepare("look", list, &n, enc, sizeof enc); assert(text && n == 1 && strcmp(list[0].mime, "image/png") == 0 && list[0].width == 1); assert(strcmp(text, "look\n\n" PIC_IMAGE_UNCONVERTIBLE "\n" PIC_IMAGE_UNCONVERTIBLE) == 0); free(text); puts("PASS: images: detection, limit, notes and base64 as Pi's"); } /* read on an image file: Pi's note, and the bytes handed back to journal. */ static void read_image(const char *dir) { char path[600], *out; struct pic_tool_image image = {NULL, NULL, 0, 0, 0}; pic_tool_hooks h = {30, no_journal, NULL, NULL, 0, NULL, NULL, NULL, &image}; pic_tool_args a; int is_error = 0; FILE *f; memset(&a, 0, sizeof a); (void)snprintf(path, sizeof path, "%s/dot.bin", dir); /* sniffed, not named */ assert((f = fopen(path, "wb")) && fwrite(png, 1, sizeof png, f) == sizeof png && fclose(f) == 0); a.path = "dot.bin"; out = pic_tool_run(dir, "read", &a, &h, &is_error); assert(out && !is_error && strcmp(out, "Read image file [image/png]") == 0); assert(image.len == sizeof png && memcmp(image.data, png, sizeof png) == 0 && strcmp(image.mime, "image/png") == 0); assert(image.width == 1 && image.height == 1); free(out); free(image.data); /* A broken header: Pi's note, no image. */ { unsigned char bad[sizeof png]; memcpy(bad, png, sizeof png); bad[16] = 0x7f; /* width, under the old CRC */ assert((f = fopen(path, "wb")) && fwrite(bad, 1, sizeof bad, f) == sizeof bad && fclose(f) == 0); memset(&image, 0, sizeof image); out = pic_tool_run(dir, "read", &a, &h, &is_error); assert(out && !is_error && strcmp(out, "Read image file [image/png]\n" PIC_IMAGE_UNCONVERTIBLE) == 0 && !image.data); free(out); assert((f = fopen(path, "wb")) && fwrite(png, 1, sizeof png, f) == sizeof png && fclose(f) == 0); } /* Over the inline limit: the note only, nothing attached. */ assert((f = fopen(path, "ab"))); for (int i = 0; i < 3600; i++) { static const char kb[1024]; assert(fwrite(kb, 1, sizeof kb, f) == sizeof kb); } assert(fclose(f) == 0); memset(&image, 0, sizeof image); out = pic_tool_run(dir, "read", &a, &h, &is_error); assert(out && !is_error && strcmp(out, "Read image file [image/png]\n" PIC_IMAGE_TOO_BIG) == 0 && !image.data); free(out); assert(unlink(path) == 0); puts("PASS: read: an image file comes back as Pi's note and an attachment"); } static int stop(void *cb_state, const char *entry) { (void)cb_state; (void)entry; return 42; } int main(void) { const pic_executor *ex = pic_executor_get(); const char *tmp = getenv("TMPDIR"); char dir[512], sub[600], file[600]; char *buf = NULL; size_t len = 0; pic_file_stat st; int n = 0; assert(ex == pic_executor_host()); (void)snprintf(dir, sizeof dir, "%s/pic-files-XXXXXX", tmp && *tmp ? tmp : "/tmp"); assert(mkdtemp(dir)); (void)snprintf(sub, sizeof sub, "%s/sub", dir); (void)snprintf(file, sizeof file, "%s/sub/f.txt", dir); /* Files are created as POSIX does: the process umask applies. */ mode_t um = umask(0); (void)umask(um); assert(ex->mkdir(ex->state, sub) == 0); assert(ex->mkdir(ex->state, sub) == EEXIST); assert(ex->stat(ex->state, file, &st) == ENOENT); assert(ex->write(ex->state, file, "hello", 5, 0600) == 0); assert(ex->stat(ex->state, file, &st) == 0 && !st.is_dir && st.size == 5 && st.mode == (0600 & ~(unsigned)um)); assert(ex->stat(ex->state, sub, &st) == 0 && st.is_dir); assert(ex->write(ex->state, file, "bye", 3, 0644) == 0); /* replaced, with the new mode */ assert(ex->stat(ex->state, file, &st) == 0 && st.size == 3 && st.mode == (0644 & ~(unsigned)um)); assert(ex->read(ex->state, file, 1024, PIC_FILE_WRITABLE, &buf, &len) == 0); assert(len == 3 && strcmp(buf, "bye") == 0); free(buf); assert(ex->read(ex->state, file, 2, 0, &buf, &len) == EFBIG); assert(ex->read(ex->state, sub, 1024, 0, &buf, &len) == EISDIR); assert(ex->list(ex->state, sub, count, &n) == 0 && n == 1); assert(ex->list(ex->state, sub, stop, NULL) == 42); assert(ex->list(ex->state, file, count, &n) == ENOTDIR); assert(ex->remove(ex->state, file) == 0); assert(ex->remove(ex->state, file) == ENOENT); assert(ex->read(ex->state, file, 1024, 0, &buf, &len) == ENOENT); n = 0; assert(ex->list(ex->state, sub, count, &n) == 0 && n == 0); assert(rmdir(sub) == 0); puts("PASS: host executor files"); images(); read_image(dir); assert(rmdir(dir) == 0); return 0; }