aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/tests/guest_stat.c (6111B)


      1 #define _GNU_SOURCE
      2 #include <assert.h>
      3 #include <stdint.h>
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 #include <string.h>
      7 #include <time.h>
      8 #ifdef _WIN32
      9 #include <windows.h>
     10 #include <winioctl.h>
     11 static HMODULE crt;
     12 static uint32_t storage[11];
     13 #else
     14 #include "guest_stdio.h"
     15 #include "guest_crt.h"
     16 #include "guest_memory.h"
     17 #include <fcntl.h>
     18 #include <sys/stat.h>
     19 #include <unistd.h>
     20 recomp_func_t recomp_lookup(uint32_t address) { (void)address; abort(); }
     21 recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); }
     22 #endif
     23 static int64_t timestamp;
     24 
     25 static unsigned char *record(void)
     26 {
     27 #ifdef _WIN32
     28     return (unsigned char *)storage + 4;
     29 #else
     30     return (unsigned char *)ADDR(0x50005004u);
     31 #endif
     32 }
     33 
     34 static int query(const char *path)
     35 {
     36     memset(record() - 4, 0xa5, 44);
     37 #ifdef _WIN32
     38     int *(__cdecl *crt_errno)(void) = (void *)GetProcAddress(crt, "_errno");
     39     int (__cdecl *crt_stat)(const char *, void *) = (void *)GetProcAddress(crt, "_stat");
     40     assert(crt_errno && crt_stat);
     41     *crt_errno() = 77;
     42     int result = crt_stat(path, record());
     43     unsigned error = *crt_errno();
     44 #else
     45     strcpy((char *)ADDR(0x50002000u), path);
     46     MEM32(guest_crt_errno_address()) = 77;
     47     g_esp = 0x5000ff00u;
     48     MEM32(g_esp + 4) = 0x50002000u;
     49     MEM32(g_esp + 8) = 0x50005004u;
     50     g_ebx = 0x12345678u;
     51     g_esi = 0x23456789u;
     52     g_edi = 0x3456789au;
     53     g_ebp = 0x456789abu;
     54     guest_stdio_lookup("_stat")();
     55     int result = (int32_t)g_eax;
     56     unsigned error = MEM32(guest_crt_errno_address());
     57     assert(g_esp == 0x5000ff04u);
     58     assert(g_ebx == 0x12345678u && g_esi == 0x23456789u);
     59     assert(g_edi == 0x3456789au && g_ebp == 0x456789abu);
     60 #endif
     61     assert(error == (result ? 2 : 77));
     62     assert(*(uint32_t *)(record() - 4) == 0xa5a5a5a5u);
     63     assert(*(uint32_t *)(record() + 36) == 0xa5a5a5a5u);
     64     return result;
     65 }
     66 
     67 static void check_file(const char *path, unsigned mode, uint32_t size)
     68 {
     69     unsigned char *bytes = record();
     70 
     71     int result = query(path);
     72 
     73     assert(result == 0);
     74     assert(*(uint32_t *)bytes == *(uint32_t *)(bytes + 16));
     75     assert(*(uint16_t *)(bytes + 4) == 0);
     76     assert(*(uint16_t *)(bytes + 6) == mode);
     77     assert(*(uint16_t *)(bytes + 8) == 1);
     78     assert(*(uint16_t *)(bytes + 10) == 0 && *(uint16_t *)(bytes + 12) == 0);
     79     assert(bytes[14] == 0xa5 && bytes[15] == 0xa5);
     80     assert(*(uint32_t *)(bytes + 20) == size);
     81     for (unsigned i = 24; i < 36; i += 4) {
     82         int64_t value = *(uint32_t *)(bytes + i);
     83         int64_t now = time(NULL);
     84         if (value < timestamp - 1 || value > now + 1)
     85             fprintf(stderr, "%s time offset %u: %lld outside creation interval\n", path, i, (long long)value);
     86         assert(value >= timestamp - 1 && value <= now + 1);
     87     }
     88 }
     89 
     90 static void missing_path_preserves_output(const char *path)
     91 {
     92     unsigned char expected[44];
     93     memset(expected, 0xa5, sizeof(expected));
     94 
     95     int result = query(path);
     96 
     97     assert(result == -1 && !memcmp(record() - 4, expected, sizeof(expected)));
     98 }
     99 
    100 static void resize_sparse_file(uint64_t size)
    101 {
    102 #ifdef _WIN32
    103     HANDLE file = CreateFileA("large.dat", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL);
    104     assert(file != INVALID_HANDLE_VALUE);
    105     DWORD written;
    106     assert(DeviceIoControl(file, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &written, NULL));
    107     LARGE_INTEGER length;
    108     length.QuadPart = size;
    109     assert(SetFilePointerEx(file, length, NULL, FILE_BEGIN) && SetEndOfFile(file));
    110     assert(CloseHandle(file));
    111 #else
    112     int file = open("large.dat", O_WRONLY | O_CREAT, 0644);
    113     assert(file >= 0 && !ftruncate(file, size) && !close(file));
    114 #endif
    115 }
    116 
    117 int main(void)
    118 {
    119     /* Use current timestamps to avoid the legacy CRT's historical-DST bug. */
    120     timestamp = time(NULL);
    121 #ifdef _WIN32
    122     crt = LoadLibraryA("msvcrt.dll");
    123     assert(crt);
    124     char temporary[MAX_PATH], directory[MAX_PATH];
    125     assert(GetTempPathA(sizeof(temporary), temporary));
    126     assert(GetTempFileNameA(temporary, "ast", 0, directory));
    127     assert(DeleteFileA(directory) && CreateDirectoryA(directory, NULL));
    128     assert(SetCurrentDirectoryA(directory) && CreateDirectoryA("nested", NULL));
    129 #else
    130     assert(guest_map("stat test memory", 0x50000000u, 0x10000u));
    131     g_fs_base = 0x50001000u;
    132     char directory[] = "/tmp/ao-stat-test-XXXXXX";
    133     assert(mkdtemp(directory) && !chdir(directory) && !mkdir("nested", 0700));
    134 #endif
    135     const char *names[] = {"file.txt", "file.EXE", "file.com", "file.bat", "file.cmd", "file.py", "nested/file"};
    136     for (unsigned i = 0; i < 7; ++i) {
    137         FILE *file = fopen(names[i], "wb");
    138         assert(file && fwrite("abcdefghij", 1, 10, file) == 10 && !fclose(file));
    139         check_file(names[i], i >= 1 && i <= 4 ? 0x81ff : 0x81b6, 10);
    140     }
    141     check_file("nested\\file", 0x81b6, 10);
    142     check_file("nested", 0x41ff, 0);
    143     check_file(".", 0x41ff, 0);
    144 #ifdef _WIN32
    145     assert(SetFileAttributesA("file.txt", FILE_ATTRIBUTE_READONLY));
    146 #else
    147     assert(!chmod("file.txt", 0444));
    148 #endif
    149     check_file("file.txt", 0x8124, 10);
    150     missing_path_preserves_output("missing");
    151     missing_path_preserves_output("missing/file");
    152     missing_path_preserves_output("file.txt/child");
    153     missing_path_preserves_output("nested\\");
    154     missing_path_preserves_output("nested/");
    155     missing_path_preserves_output("*");
    156     missing_path_preserves_output("");
    157     resize_sparse_file(0x80000001ull);
    158     check_file("large.dat", 0x81b6, 0x80000001u);
    159     resize_sparse_file(0x100000009ull);
    160     check_file("large.dat", 0x81b6, 9);
    161 #ifdef _WIN32
    162     assert(SetFileAttributesA("file.txt", FILE_ATTRIBUTE_NORMAL));
    163 #else
    164     assert(!chmod("file.txt", 0644));
    165 #endif
    166     for (unsigned i = 0; i < 7; ++i) assert(!remove(names[i]));
    167     assert(!remove("large.dat"));
    168 #ifdef _WIN32
    169     assert(RemoveDirectoryA("nested") && SetCurrentDirectoryA(temporary) && RemoveDirectoryA(directory));
    170 #else
    171     assert(!rmdir("nested") && !chdir("/tmp") && !rmdir(directory));
    172 #endif
    173     puts("stat: 20 cases, 36-byte layout, modes, timestamps, sparse sizes, errors, padding and ABI passed");
    174     return 0;
    175 }