Skip to content

Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit

16.7 KB
Raw Blame
#include "../../helpers/shortcuts.c"

#include <ftw.h>
#include <limits.h>
#include <signal.h>
#include <stdarg.h>

typedef struct { char s[PATH_MAX]; } path_t;

static unsigned checks;
static char     sandbox[PATH_MAX];
static const char header[] = "#!/bin/bash\n# autogenerated file - do not edit\n\nopen steam://run/";

static void check(int condition, const char *name) {
    checks++;
    if (!condition) {
        fprintf(stderr, "shortcutcheck: failed: %s\nfixtures: %s\n", name, sandbox);
        exit(1);
    }
}

static path_t P(const char *format, ...) {
    path_t p;
    va_list args;
    va_start(args, format);
    int n = vsnprintf(p.s, sizeof(p.s), format, args);
    va_end(args);
    if (n < 0 || n >= (int)sizeof(p.s)) abort();
    return p;
}

static void mkdirs(const char *path) {
    path_t p = P("%s", path);
    for (char *c = p.s + 1; *c; c++) {
        if (*c != '/') continue;
        *c = 0;
        check(mkdir(p.s, 0755) == 0 || errno == EEXIST, "mkdir parent");
        *c = '/';
    }
    check(mkdir(p.s, 0755) == 0 || errno == EEXIST, "mkdir");
}

static void put_n(const char *path, const void *data, size_t length) {
    path_t parent = P("%s", path);
    *strrchr(parent.s, '/') = 0;
    mkdirs(parent.s);
    int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
    check(fd >= 0 && write(fd, data, length) == (ssize_t)length && close(fd) == 0, path);
}

static void put(const char *path, const char *text) {
    put_n(path, text, strlen(text));
}

static int is(const char *path, const char *text) {
    char buffer[256];
    int fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
    if (fd < 0) return 0;
    ssize_t n = read(fd, buffer, sizeof(buffer));
    close(fd);
    return n == (ssize_t)strlen(text) && memcmp(buffer, text, (size_t)n) == 0;
}

static int exists(const char *path) {
    struct stat st;
    return lstat(path, &st) == 0;
}

static int remove_entry(const char *path, const struct stat *st, int flag, struct FTW *ftw) {
    return remove(path);
}

static void remove_item(const char *path) {
    struct stat st;
    check(lstat(path, &st) == 0 && (S_ISDIR(st.st_mode) ? nftw(path, remove_entry, 16, FTW_DEPTH | FTW_PHYS)
                                                        : unlink(path)) == 0, path);
}

static path_t fixture(const char *name) {
    path_t home = P("%s/%s", sandbox, name);
    mkdirs(P("%s/Applications", home.s).s);
    put(P("%s/steam.icns", home.s).s, "old icon");
    return home;
}

static void update(const char *home, const char *source) {
    np_shortcuts_icons(home, "440", source, P("%s/steam.icns", home).s);
}

static void put_script(const char *path, const char *appid) {
    put(path, P("%s%s\n\n", header, appid).s);
}

static path_t bundle_in(const char *home, const char *folder, const char *name, const char *appid) {
    path_t app = P("%s/%s/%s.app", home, folder, name);
    put_script(P("%s/Contents/MacOS/run.sh", app.s).s, appid);
    put(P("%s/Contents/Resources/shortcut.icns", app.s).s, "old icon");
    return app;
}

static path_t bundle(const char *home, const char *name, const char *appid) {
    return bundle_in(home, "Applications", name, appid);
}

static path_t icon_of(const char *app) {
    return P("%s/Contents/Resources/shortcut.icns", app);
}

static int read_ok(const char *path, size_t limit, const char *text) {
    blob_t data;
    int ok = read_at(AT_FDCWD, path, limit, &data);
    ok = ok && (!text || (data.length == strlen(text) && memcmp(data.bytes, text, data.length) == 0));
    free(data.bytes);
    return ok;
}

static void script_checks(void) {
    path_t home = fixture("scripts");
    path_t app = bundle(home.s, "Candidate", "440");
    path_t run = P("%s/Contents/MacOS/run.sh", app.s);
    path_t icon = icon_of(app.s);
    path_t source = P("%s/new.icns", home.s);
    put(source.s, "new icon");
    int fd = open(app.s, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
    check(fd >= 0, "open script fixture");
    for (int i = 0; i < 3; i++) {
        const char *appid = (const char *[]){"1", "440", "2147483647"}[i];
        put_script(run.s, appid);
        check(is_shortcut(fd, appid), "Steam's script accepted");
    }
    put_script(run.s, "440");
    check(!is_shortcut(fd, "44") && !is_shortcut(fd, "4400") && !is_shortcut(fd, "0440"), "script for another ID rejected");

    static char oversized[4097];
    memset(oversized, '\n', sizeof(oversized));
    memcpy(oversized, header, strlen(header));
    const struct { const char *text; size_t length; } invalid[] = {
        { "", 0 },
        { header, strlen(header) },
        { "#!/bin/bash\n# autogenerated file - do not edit\n\nopen steam://run/440", 0 },
        { "#!/bin/bash\n# autogenerated file - do not edit\n\nopen steam://run/440\n", 0 },
        { "#!/bin/bash\n# autogenerated file - do not edit\n\nopen steam://run/440\r\n", 0 },
        { "#!/bin/bash\n# autogenerated file - do not edit\n\nopen steam://run/440\n\n\n", 0 },
        { "#!/bin/bash\n# autogenerated file - do not edit\n\nexport CUSTOM_LAUNCH=1\nopen steam://run/440\n\n", 0 },
        { "echo edited\n#!/bin/bash\n# autogenerated file - do not edit\n\nopen steam://run/440\n\n", 0 },
        { "#!/bin/bash\n# autogenerated file - do not edit\n\nopen steam://run/440\n\necho edited\n", 0 },
        { "#!/bin/bash\n# edited file - do not edit\n\nopen steam://run/440\n\n", 0 },
        { "#!/bin/bash\n# autogenerated file - do not edit\n\nopen steam://run/440\n\n\0", 71 },
        { "#!/bin/bash\n# autogenerated file - do not edit\n\nopen steam://run/4\0000\n\n", 70 },
        { oversized, sizeof(oversized) },
    };
    for (size_t i = 0; i < sizeof(invalid) / sizeof(*invalid); i++) {
        put_n(run.s, invalid[i].text, invalid[i].length ? invalid[i].length : strlen(invalid[i].text));
        check(!is_shortcut(fd, "440"), "reject edited, truncated, NUL or oversized script");
        update(home.s, source.s);
        check(is(icon.s, "old icon"), "invalid script icon unchanged");
    }

    remove_item(run.s);
    check(mkfifo(run.s, 0600) == 0, "create script FIFO");
    check(!is_shortcut(fd, "440"), "FIFO script rejected without waiting for writer");
    update(home.s, source.s);
    check(is(icon.s, "old icon"), "FIFO script icon unchanged");
    close(fd);

    path_t input = P("%s/read-input", home.s);
    put(input.s, "abcd");
    check(read_ok(input.s, 4, "abcd"), "read_at exact bound");
    check(!read_ok(input.s, 3, NULL), "read_at oversized");
    put(input.s, "");
    check(!read_ok(input.s, 4, NULL), "read_at empty");
    check(!read_ok(home.s, 4096, NULL), "read_at directory");
    check(!read_ok(run.s, 4096, NULL), "read_at FIFO");
    remove_item(input.s);
    check(symlink(source.s, input.s) == 0, "create input symlink");
    check(!read_ok(input.s, 4096, NULL), "read_at symlink");
}

static void desktop_checks(void) {
    path_t home = fixture("desktop-icons");
    path_t app = bundle(home.s, "Target", "440");
    path_t desktop = bundle_in(home.s, "Desktop", "Target", "440");
    path_t other = bundle_in(home.s, "Desktop", "OtherGame", "570");
    path_t custom = bundle_in(home.s, "Desktop", "Customized", "440");
    path_t custom_run = P("%s/Contents/MacOS/run.sh", custom.s);
    path_t custom_script = P("%s440\n\necho customized\n", header);
    put(custom_run.s, custom_script.s);
    path_t fifo = bundle_in(home.s, "Desktop", "FIFO", "440");
    path_t run = P("%s/Contents/MacOS/run.sh", fifo.s);
    remove_item(run.s);
    check(mkfifo(run.s, 0600) == 0, "create Desktop script FIFO");
    path_t source = P("%s/source.icns", home.s);
    put(source.s, "new icon");
    update(home.s, source.s);
    check(is(icon_of(app.s).s, "new icon") && is(icon_of(desktop.s).s, "new icon"),
          "same-ID Applications and Desktop icons both updated");
    check(is(icon_of(other.s).s, "old icon") && is(icon_of(custom.s).s, "old icon") && is(icon_of(fifo.s).s, "old icon"),
          "unrelated, customized and FIFO Desktop icons unchanged");
    check(is(custom_run.s, custom_script.s), "customized Desktop script unchanged");

    for (int i = 0; i < 2; i++) {
        const char *folder = i ? "Desktop" : "Applications";
        const char *missing = i ? "Applications" : "Desktop";
        home = fixture(P("icons-only-%s", folder).s);
        app = bundle_in(home.s, folder, "Target", "440");
        if (i) remove_item(P("%s/Applications", home.s).s);
        check(!exists(P("%s/%s", home.s, missing).s), "other shortcut folder is missing");
        source = P("%s/source.icns", home.s);
        put(source.s, "new icon");
        update(home.s, source.s);
        check(is(icon_of(app.s).s, "new icon"), "missing sibling folder still updates the other");
        check(!exists(P("%s/%s", home.s, missing).s), "icon update does not create missing shortcut folder");
    }
}

static void symlink_checks(const char *folder) {
    const char *parts[] = {"", "Contents", "Contents/MacOS", "Contents/MacOS/run.sh",
                           "Contents/Resources", "Contents/Resources/shortcut.icns"};
    for (size_t i = 0; i < sizeof(parts) / sizeof(*parts); i++) {
        path_t home = fixture(P("symlink-%s-%zu", folder, i).s);
        path_t app = bundle_in(home.s, folder, "Candidate", "440");
        path_t link_path = *parts[i] ? P("%s/%s", app.s, parts[i]) : app;
        path_t target = P("%s/outside-target", home.s);
        check(rename(link_path.s, target.s) == 0, "move symlink target outside shortcut folder");
        check(symlink(target.s, link_path.s) == 0, "create bundle component symlink");
        path_t source = P("%s/new.icns", home.s);
        put(source.s, "new icon");
        update(home.s, source.s);
        check(is(icon_of(app.s).s, "old icon"), P("icon update skips symlink: %s", parts[i]).s);
        check(exists(target.s), "symlink target retained");
        struct stat st;
        check(lstat(link_path.s, &st) == 0 && S_ISLNK(st.st_mode), "symlink itself unchanged");
    }
}

static int same_mtime(const struct stat *a, const struct stat *b) {
    return same_time(a->st_mtimespec, b->st_mtimespec);
}

static void icon_checks(void) {
    path_t home = fixture("icons");
    path_t app = bundle(home.s, "Target", "440");
    path_t other = bundle(home.s, "OtherGame", "570");
    path_t icon = icon_of(app.s);
    path_t source = P("%s/source.icns", home.s);
    path_t original = P("%s/hardlinked-original.icns", home.s);
    put(source.s, "new icon");
    check(link(icon.s, original.s) == 0, "hardlink original icon");
    int held = open(icon.s, O_RDONLY | O_CLOEXEC);
    check(held >= 0, "hold old icon inode");
    update(home.s, source.s);
    check(is(icon.s, "new icon"), "target icon updated");
    check(is(original.s, "old icon"), "hardlinked original not overwritten");
    struct stat before, after;
    check(fstat(held, &before) == 0 && stat(icon.s, &after) == 0 && before.st_ino != after.st_ino,
          "icon replacement uses new inode");
    char old[8];
    check(read(held, old, sizeof(old)) == sizeof(old) && memcmp(old, "old icon", sizeof(old)) == 0,
          "open reader retains complete old icon");
    close(held);
    check(is(icon_of(other.s).s, "old icon"), "other game ID isolated");

    struct timespec stamp[2] = {{1000000000, 123456000}, {1000000000, 123456000}};
    check(utimensat(AT_FDCWD, icon.s, stamp, 0) == 0 && utimensat(AT_FDCWD, app.s, stamp, 0) == 0,
          "set deterministic mtimes");
    struct stat app_before, app_after;
    check(stat(icon.s, &before) == 0 && stat(app.s, &app_before) == 0, "stat before same icon");
    update(home.s, source.s);
    check(stat(icon.s, &after) == 0 && before.st_ino == after.st_ino && same_mtime(&before, &after),
          "replaced icon is left alone on a second run");
    check(stat(app.s, &app_after) == 0 && same_mtime(&app_before, &app_after), "second run preserves bundle mtime");

    path_t resources = P("%s/Contents/Resources", app.s);
    put(icon.s, "old icon");
    put(source.s, "replacement");
    check(chmod(resources.s, 0500) == 0, "lock Resources directory");
    int writable = access(resources.s, W_OK) == 0;
    update(home.s, source.s);
    check(chmod(resources.s, 0700) == 0, "unlock Resources directory");
    if (!writable) check(is(icon.s, "old icon"), "locked directory leaves old icon intact");
    else fprintf(stderr, "shortcutcheck: locked-directory check skipped: permissions bypassed\n");
    check(exists(P("%s/Contents/MacOS/run.sh", app.s).s), "optional icon failure preserves launcher");
    DIR *dir = opendir(resources.s);
    int entries = 0;
    for (struct dirent *e; dir && (e = readdir(dir));)
        if (e->d_name[0] != '.' || strncmp(e->d_name, ".bridgeplay-icon-", 17) == 0) entries++;
    if (dir) closedir(dir);
    check(entries == 1, "no temporary icon files left behind");
    update(home.s, source.s);
    check(is(icon.s, "replacement"), "unlocked directory gets the icon");
    remove_item(icon.s);
    update(home.s, source.s);
    check(!exists(icon.s), "missing icon is not created");
    remove_item(resources.s);
    update(home.s, source.s);
    check(!exists(resources.s) && exists(P("%s/Contents/MacOS/run.sh", app.s).s),
          "missing optional Resources leaves launcher intact");

    put(icon.s, "old icon");
    const char *kinds[] = {"missing", "empty", "directory", "fifo", "symlink", "oversized", "unreadable"};
    for (size_t i = 0; i < sizeof(kinds) / sizeof(*kinds); i++) {
        const char *kind = kinds[i];
        path_t bad = P("%s/%s", home.s, kind);
        if (!strcmp(kind, "empty")) put(bad.s, "");
        if (!strcmp(kind, "directory")) mkdirs(bad.s);
        if (!strcmp(kind, "fifo")) check(mkfifo(bad.s, 0600) == 0, "create source FIFO");
        if (!strcmp(kind, "symlink")) check(symlink(source.s, bad.s) == 0, "create source symlink");
        if (!strcmp(kind, "oversized")) {
            int fd = open(bad.s, O_WRONLY | O_CREAT | O_EXCL, 0600);
            check(fd >= 0 && ftruncate(fd, ICON_LIMIT + 1) == 0, "create oversized sparse icon source");
            close(fd);
        }
        if (!strcmp(kind, "unreadable")) {
            put(bad.s, "unreadable source");
            check(chmod(bad.s, 0000) == 0, "make source unreadable");
            if (access(bad.s, R_OK) == 0) {
                check(chmod(bad.s, 0600) == 0, "restore bypassed source permissions");
                fprintf(stderr, "shortcutcheck: unreadable-source check skipped: permissions bypassed\n");
                continue;
            }
        }
        update(home.s, bad.s);
        if (!strcmp(kind, "unreadable")) check(chmod(bad.s, 0600) == 0, "restore source permissions");
        check(is(icon.s, "old icon"), P("source error leaves icon unchanged: %s", kind).s);
    }

    home = fixture("finder-locked");
    app = bundle(home.s, "Locked", "440");
    source = P("%s/source.icns", home.s);
    put(source.s, "new icon");
    check(chflags(app.s, UF_IMMUTABLE) == 0, "Finder-lock bundle");
    update(home.s, source.s);
    check(chflags(app.s, 0) == 0, "unlock bundle after optional timestamp failure");
    check(is(icon_of(app.s).s, "new icon"), "Finder-locked bundle permits icon replacement without a timestamp update");
}

static void stock_checks(void) {
    path_t home = fixture("stock");
    path_t stock = P("%s/steam.icns", home.s);
    path_t source = P("%s/game.icns", home.s);
    put(source.s, "game art");
    path_t apps = bundle(home.s, "Game", "440");
    path_t desk = bundle_in(home.s, "Desktop", "Game", "440");
    path_t custom = bundle_in(home.s, "Desktop", "Custom", "440");
    path_t icon = icon_of(apps.s);
    put(icon_of(custom.s).s, "user icon");

    path_t linked = P("%s/linked-steam.icns", home.s);
    check(symlink(stock.s, linked.s) == 0, "create stock symlink");
    path_t missing = P("%s/missing", home.s);
    const char *bad[] = {missing.s, linked.s};
    for (int i = 0; i < 2; i++) {
        np_shortcuts_icons(home.s, "440", source.s, bad[i]);
        check(is(icon.s, "old icon"), "unusable stock file changes nothing");
    }
    np_shortcuts_icons(home.s, "440", stock.s, stock.s);
    check(is(icon.s, "old icon"), "stock artwork is not written over stock");

    update(home.s, source.s);
    check(is(icon.s, "game art"), "stock Applications icon replaced");
    check(is(icon_of(desk.s).s, "game art"), "stock Desktop icon replaced");
    check(is(icon_of(custom.s).s, "user icon"), "custom icon kept");
    put(source.s, "newer art");
    update(home.s, source.s);
    check(is(icon.s, "game art"), "replaced icon is not replaced again");
}

int main(void) {
    signal(SIGALRM, SIG_DFL);
    alarm(60);
    const char *tmp = getenv("TMPDIR");
    snprintf(sandbox, sizeof(sandbox), "%s/shortcutcheck.XXXXXX", tmp && *tmp ? tmp : "/tmp");
    check(mkdtemp(sandbox) != NULL, "create disposable sandbox");
    script_checks();
    desktop_checks();
    symlink_checks("Applications");
    symlink_checks("Desktop");
    icon_checks();
    stock_checks();
    remove_item(sandbox);
    alarm(0);
    printf("shortcutcheck: %u checks passed\n", checks);
    return 0;
}