Skip to content
pi-c Artifact [93f1af89eb]
Login

Artifact [93f1af89eb]

Artifact 93f1af89ebaef4d9569f8e60d668bf9e75213b1924131e423950d81f39c739a0:


/* 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"));
    assert(!pic_image_refusal("image/png", 70));
    assert(strcmp(pic_image_refusal("image/bmp", 70), PIC_IMAGE_UNCONVERTIBLE) == 0);
    /* Under 4.5MB of base64 goes inline: 3538942 bytes encode to exactly that. */
    assert(!pic_image_refusal("image/png", 3538941));
    assert(strcmp(pic_image_refusal("image/png", 3538942), PIC_IMAGE_TOO_BIG) == 0);
    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);
    /* Kept images move to the front with their inline type; the others
     * become notes after a blank line, as Pi's prompt normalization does. */
    list[0] = (pic_image){"image/bmp", png, sizeof png};
    list[1] = (pic_image){"image/jpg", png, sizeof png};
    list[2] = (pic_image){"text/plain", png, sizeof png};
    n = 3;
    text = pic_image_prepare("look", list, &n);
    assert(text && n == 1 && strcmp(list[0].mime, "image/jpeg") == 0);
    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};
    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);
    free(out);
    free(image.data);
    /* 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;
}