Skip to content
pi-c Artifact [7d78c7a66b]
Login

Artifact [7d78c7a66b]

Artifact 7d78c7a66b4e9a203fea91a98b3923e795eeeb152093397923332f559afbd165:


/* 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 = {.exec_seconds = 30, .started = no_journal, .image = &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 void run_edit_case(const char *dir, const char *initial, size_t initial_len, const char **olds,
                          const char **news, size_t nedits, int want_err, const char *want_msg_sub, const char *want,
                          size_t want_len) {
    const pic_executor *ex = pic_executor_get();
    char path[600], *out, *got = NULL;
    size_t got_len = 0;
    pic_tool_hooks h = {.exec_seconds = 30, .started = no_journal};
    pic_tool_args a;
    int is_error = 0;
    (void)snprintf(path, sizeof path, "%s/edit.txt", dir);
    assert(ex->write(ex->state, path, initial, initial_len, 0644) == 0);
    memset(&a, 0, sizeof a);
    a.path = "edit.txt";
    a.old_text = olds;
    a.new_text = news;
    a.nedits = nedits;
    out = pic_tool_run(dir, "edit", &a, &h, &is_error);
    assert(out && is_error == want_err);
    if (want_msg_sub) {
        assert(strstr(out, want_msg_sub) != NULL);
    }
    free(out);
    assert(ex->read(ex->state, path, 65536, 0, &got, &got_len) == 0);
    assert(got_len == want_len && memcmp(got, want, want_len) == 0);
    free(got);
    assert(ex->remove(ex->state, path) == 0);
}

/* edit: fuzzy matching fallback (every normalized codepoint, trailing
 * horizontal whitespace, ambiguity, mixed exact+fuzzy) and per-region CRLF. */
static void edit_fuzzy_and_crlf(const char *dir) {
    /* Exact match is preferred when both exact and fuzzy candidates exist. */
    {
        const char *orig = "say \xE2\x80\x98hi\xE2\x80\x99\nsay 'hi'\n";
        const char *want = "say \xE2\x80\x98hi\xE2\x80\x99\nsay 'bye'\n";
        const char *olds[] = {"say 'hi'"};
        const char *news[] = {"say 'bye'"};
        run_edit_case(dir, orig, strlen(orig), olds, news, 1, 0, "Successfully replaced 1", want, strlen(want));
    }
    /* All curly single quotes U+2018..U+201B. */
    {
        const char *orig = "pre \xE2\x80\x98" "a\xE2\x80\x99 \xE2\x80\x9A" "b\xE2\x80\x9B post\n";
        const char *want = "pre OK post\n";
        const char *olds[] = {"'a' 'b'"};
        const char *news[] = {"OK"};
        run_edit_case(dir, orig, strlen(orig), olds, news, 1, 0, NULL, want, strlen(want));
    }
    /* All curly double quotes U+201C..U+201F. */
    {
        const char *orig = "pre \xE2\x80\x9C" "a\xE2\x80\x9D \xE2\x80\x9E" "b\xE2\x80\x9F post\n";
        const char *want = "pre OK post\n";
        const char *olds[] = {"\"a\" \"b\""};
        const char *news[] = {"OK"};
        run_edit_case(dir, orig, strlen(orig), olds, news, 1, 0, NULL, want, strlen(want));
    }
    /* All Unicode dashes U+2010..U+2015 and U+2212. */
    {
        const char *orig = "a\xE2\x80\x90" "b\xE2\x80\x91" "c\xE2\x80\x92" "d\xE2\x80\x93"
                           "e\xE2\x80\x94" "f\xE2\x80\x95" "g\xE2\x88\x92" "h\n";
        const char *want = "dashes\n";
        const char *olds[] = {"a-b-c-d-e-f-g-h"};
        const char *news[] = {"dashes"};
        run_edit_case(dir, orig, strlen(orig), olds, news, 1, 0, NULL, want, strlen(want));
    }
    /* All Unicode spaces: U+00A0, U+2002..U+200A, U+202F, U+205F, U+3000. */
    {
        const char *orig = "0\xC2\xA0" "1\xE2\x80\x82" "2\xE2\x80\x83" "3\xE2\x80\x84" "4\xE2\x80\x85"
                           "5\xE2\x80\x86" "6\xE2\x80\x87" "7\xE2\x80\x88" "8\xE2\x80\x89" "9\xE2\x80\x8A"
                           "a\xE2\x80\xAF" "b\xE2\x81\x9F" "c\xE3\x80\x80" "d\n";
        const char *want = "spaces\n";
        const char *olds[] = {"0 1 2 3 4 5 6 7 8 9 a b c d"};
        const char *news[] = {"spaces"};
        run_edit_case(dir, orig, strlen(orig), olds, news, 1, 0, NULL, want, strlen(want));
    }
    /* Trailing horizontal whitespace stripped on matched lines; untouched lines
     * (including blank whitespace-only lines after a matched line) kept byte-for-byte. */
    {
        const char *orig = "keep  \nline one  \t\nline two \n   \ntail\t \n";
        const char *want = "keep  \nreplaced\n   \ntail\t \n";
        const char *olds[] = {"line one\nline two\n"};
        const char *news[] = {"replaced\n"};
        run_edit_case(dir, orig, strlen(orig), olds, news, 1, 0, NULL, want, strlen(want));
    }
    /* Without trailing newline in oldText, trailing spaces on the matched end-of-line are consumed. */
    {
        const char *orig = "line one  \nline two\t \nline three\n";
        const char *want = "A\nB\nline three\n";
        const char *olds[] = {"line one\nline two"};
        const char *news[] = {"A\nB"};
        run_edit_case(dir, orig, strlen(orig), olds, news, 1, 0, NULL, want, strlen(want));
    }
    /* Whitespace-only oldText must not produce an empty fuzzy match. */
    {
        const char *orig = "hello\n";
        const char *olds[] = {"   "};
        const char *news[] = {"x"};
        run_edit_case(dir, orig, strlen(orig), olds, news, 1, 1, "Could not find the exact text", orig, strlen(orig));
    }
    /* Fuzzy duplicates rejected cleanly. */
    {
        const char *orig = "a\xE2\x80\x93" "b\na\xE2\x80\x94" "b\n";
        const char *olds[] = {"a-b"};
        const char *news[] = {"x"};
        run_edit_case(dir, orig, strlen(orig), olds, news, 1, 1, "Found 2 occurrences", orig, strlen(orig));
    }
    /* Overlapping exact and fuzzy edits rejected. */
    {
        const char *orig = "say \xE2\x80\x98hello world\xE2\x80\x99\n";
        const char *olds[] = {"hello world", "'hello world'"};
        const char *news[] = {"HI", "'BYE'"};
        run_edit_case(dir, orig, strlen(orig), olds, news, 2, 1, "edits[0] and edits[1] overlap", orig, strlen(orig));
    }
    /* Disjoint exact + fuzzy edits on mixed LF/CRLF file with UTF-8 BOM. */
    {
        const char *orig = "\xEF\xBB\xBFkeep\r\nexact\n\xE2\x80\x98" "fuzzy\xE2\x80\x99  \r\ntail\n";
        const char *want = "\xEF\xBB\xBFkeep\r\nEX1\nEX2\n'FZ1'\r\n'FZ2'\r\ntail\n";
        const char *olds[] = {"exact\n", "'fuzzy'\n"};
        const char *news[] = {"EX1\nEX2\n", "'FZ1'\n'FZ2'\n"};
        run_edit_case(dir, orig, strlen(orig), olds, news, 2, 0, NULL, want, strlen(want));
    }
    /* Mixed LF/CRLF: single-line edit adopts its own line's ending (and previous
     * line's ending for an unterminated final line). */
    {
        const char *orig = "a\nb\r\nX = 1\r\n";
        const char *want = "a\nb\r\nX = 2\r\nX = 3\r\n";
        const char *olds[] = {"X = 1"};
        const char *news[] = {"X = 2\nX = 3"};
        run_edit_case(dir, orig, strlen(orig), olds, news, 1, 0, NULL, want, strlen(want));
    }
    {
        const char *orig = "a\r\nb\nc";
        const char *want = "a\r\nb\nC1\nC2";
        const char *olds[] = {"c"};
        const char *news[] = {"C1\nC2"};
        run_edit_case(dir, orig, strlen(orig), olds, news, 1, 0, NULL, want, strlen(want));
    }
    /* Fuzzy match starting on a whitespace-only leading line (byte 0 and mid-file)
     * must consume that line's stripped spaces rather than gluing newText onto them. */
    {
        const char *orig = "    \n\xE2\x80\x98" "x\xE2\x80\x99\n";
        const char *want = "Y\n";
        const char *olds[] = {"\n'x'\n"};
        const char *news[] = {"Y\n"};
        run_edit_case(dir, orig, strlen(orig), olds, news, 1, 0, NULL, want, strlen(want));
    }
    {
        const char *orig = "void f() {\n    \n    // \xE2\x80\x98" "x\xE2\x80\x99\n}\n";
        const char *want = "void f() {\n    // 'y'\n}\n";
        const char *olds[] = {"\n    // 'x'\n"};
        const char *news[] = {"    // 'y'\n"};
        run_edit_case(dir, orig, strlen(orig), olds, news, 1, 0, NULL, want, strlen(want));
    }
    /* Truncated UTF-8 lead bytes at end of line must not read out of bounds during fuzzy scan. */
    {
        const char *orig = "bad\xE2\nmid\xE2\x80\n\xE2\x80\x98" "end\xE2\x80\x99\xC2";
        const char *want = "bad\xE2\nmid\xE2\x80\nEND\xC2";
        const char *olds[] = {"'end'"};
        const char *news[] = {"END"};
        run_edit_case(dir, orig, strlen(orig), olds, news, 1, 0, NULL, want, strlen(want));
    }
    puts("PASS: edit: fuzzy matching and per-region mixed CRLF preservation");
}

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);
    {
        char fifo[600], big[600];
        pic_file_state fst;
        (void)snprintf(fifo, sizeof fifo, "%s/sub/pipe", dir);
        assert(mkfifo(fifo, 0600) == 0);
        assert(pic_file_state_read_bounded(fifo, 1024, PIC_FILE_KEEP_MAX, 0, &fst) == 0);
        assert(fst.exists && !fst.kept && !fst.hash[0] && !fst.data);
        pic_file_state_read(fifo, -1, &fst);
        assert(fst.exists && !fst.kept && !fst.hash[0] && !fst.data);
        assert(unlink(fifo) == 0);

        (void)snprintf(big, sizeof big, "%s/sub/big.txt", dir);
        assert(ex->write(ex->state, big, "0123456789abcdefghij", 20, 0600) == 0);
        assert(pic_file_state_read_bounded(big, 4, 10, 0, &fst) == 1);
        assert(fst.exists && !fst.kept && !fst.hash[0] && !fst.data);
        assert(pic_file_state_read_bounded(big, 32, 32, 0, &fst) == 0);
        assert(fst.exists && fst.kept && fst.hash[0] && fst.data && fst.size == 20);
        pic_file_state_free(&fst);
        assert(ex->remove(ex->state, big) == 0);
    }

    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);
    edit_fuzzy_and_crlf(dir);
    assert(rmdir(dir) == 0);
    return 0;
}