aorecomp

Anarchy Online classic in the browser

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


client/tests/browser_runtime.c (7184B)


      1 #include "recomp_types.h"
      2 #include "guest_memory.h"
      3 #include "linux_network.h"
      4 #include "linux_sync.h"
      5 #include <assert.h>
      6 #include <emscripten.h>
      7 #include <emscripten/threading.h>
      8 #include <stdio.h>
      9 #include <sys/utsname.h>
     10 #include <pthread.h>
     11 
     12 void browser_initialize(unsigned character_id);
     13 
     14 static uint32_t call(recomp_func_t function, unsigned count, const uint32_t *arguments)
     15 {
     16     assert(function);
     17     g_esp = 0x10f000;
     18     for (unsigned i = 0; i < count; ++i) MEM32(g_esp + 4 + i * 4) = arguments[i];
     19     function();
     20     assert(g_esp == 0x10f000 + (count + 1) * 4);
     21     return g_eax;
     22 }
     23 
     24 static void hostname_queries(int initialized)
     25 {
     26     struct utsname system;
     27     assert(uname(&system) == 0);
     28     size_t length = strlen(system.nodename);
     29     assert(length > 0 && length < 256);
     30     assert(linux_network_lookup("gethostname", 0) == linux_network_lookup(NULL, 57));
     31     const struct { int size; int null_buffer; } cases[] = {
     32         {-1, 0}, {0, 0}, {1, 0}, {length, 0},
     33         {length + 1, 0}, {256, 0}, {256, 1}, {0, 1},
     34     };
     35     for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) {
     36         unsigned char expected[264];
     37         memset(expected, 0xa5, sizeof(expected));
     38         memcpy((void *)ADDR(0x106000), expected, sizeof(expected));
     39         uint32_t error = !initialized ? 10093 :
     40             cases[i].null_buffer || cases[i].size <= (int)length ? 10014 : 0;
     41         if (!error) memcpy(expected + 4, system.nodename, length + 1);
     42         MEM32(g_fs_base + 0x34) = 777;
     43 
     44         uint32_t result = call(linux_network_lookup(NULL, 57), 2,
     45             (uint32_t[]){cases[i].null_buffer ? 0 : 0x106004, cases[i].size});
     46 
     47         assert(result == (error ? UINT32_MAX : 0));
     48         assert(MEM32(g_fs_base + 0x34) == error);
     49         assert(!memcmp((void *)ADDR(0x106000), expected, sizeof(expected)));
     50     }
     51 }
     52 
     53 static void *format_worker(void *argument)
     54 {
     55     g_fs_base = 0x107000;
     56     uint32_t text = call(linux_network_lookup(NULL, 12), 1, (uint32_t[]){0xffffffff});
     57     assert(!strcmp((char *)ADDR(text), "255.255.255.255"));
     58     return NULL;
     59 }
     60 
     61 static void address_strings(void)
     62 {
     63     assert(linux_network_lookup("inet_ntoa", 0) == linux_network_lookup(NULL, 12));
     64     const struct { uint32_t address; const char *text; } cases[] = {
     65         {0, "0.0.0.0"}, {0xffffffff, "255.255.255.255"},
     66         {0x0100007f, "127.0.0.1"}, {0x2a6433c6, "198.51.100.42"},
     67         {0x010200c0, "192.0.2.1"},
     68     };
     69     uint32_t text = 0;
     70     for (unsigned i = 0; i < 5; ++i) {
     71         MEM32(g_fs_base + 0x34) = 777;
     72 
     73         uint32_t result = call(linux_network_lookup(NULL, 12), 1, &cases[i].address);
     74 
     75         assert(!strcmp((char *)ADDR(result), cases[i].text));
     76         assert(MEM32(g_fs_base + 0x34) == 0);
     77         assert(!i || result == text);
     78         text = result;
     79     }
     80     pthread_t thread;
     81     assert(!pthread_create(&thread, NULL, format_worker, NULL));
     82     assert(!pthread_join(thread, NULL));
     83     assert(!strcmp((char *)ADDR(text), "192.0.2.1"));
     84 }
     85 
     86 static void socket_address_queries(uint32_t socket, uint32_t error)
     87 {
     88     assert(linux_network_lookup("getsockname", 0) == linux_network_lookup(NULL, 6));
     89     const struct { uint32_t output, length; int size; } cases[] = {
     90         {0, 0x106020, 16}, {0x106004, 0, 16}, {0x106004, 0x106020, -1},
     91         {0x106004, 0x106020, 15}, {0x106004, 0x106020, 16}, {0x106004, 0x106020, 24},
     92     };
     93     for (unsigned i = 0; i < 6; ++i) {
     94         memset((void *)ADDR(0x106000), 0xa5, 32);
     95         MEM32(0x106020) = cases[i].size;
     96         MEM32(g_fs_base + 0x34) = 777;
     97         uint32_t expected = error == 10093 || error == 10038 ? error : i < 4 ? 10014 : error;
     98 
     99         uint32_t result = call(linux_network_lookup(NULL, 6), 3,
    100             (uint32_t[]){socket, cases[i].output, cases[i].length});
    101 
    102         assert(result == (expected ? UINT32_MAX : 0));
    103         assert(MEM32(g_fs_base + 0x34) == expected);
    104         assert(MEM32(0x106000) == 0xa5a5a5a5 && MEM32(0x106014) == 0xa5a5a5a5);
    105         if (expected) {
    106             assert(MEM32(0x106020) == (uint32_t)cases[i].size);
    107             for (unsigned j = 4; j < 20; ++j) assert(MEM8(0x106000 + j) == 0xa5);
    108         } else {
    109             assert(MEM32(0x106020) == 16);
    110             assert(MEM16(0x106004) == 2 && MEM16(0x106006) == 0);
    111             assert(MEM32(0x106008) == 0x0100007f);
    112             assert(MEM32(0x10600c) == 0 && MEM32(0x106010) == 0);
    113         }
    114     }
    115 }
    116 
    117 int main(void)
    118 {
    119     assert(!emscripten_is_main_browser_thread());
    120     browser_initialize(1);
    121     assert(guest_map("browser test", 0x100000, 0x10000));
    122     g_fs_base = 0x100000;
    123     hostname_queries(0);
    124     address_strings();
    125     socket_address_queries(0, 10093);
    126     uint32_t arguments[] = {0x0202, 0x101000};
    127     assert(call(linux_network_lookup("WSAStartup", 0), 2, arguments) == 0);
    128     hostname_queries(1);
    129     socket_address_queries(0, 10038);
    130     uint32_t socket = call(linux_network_lookup("socket", 0), 3, (uint32_t[]){2, 1, 0});
    131     assert(socket != UINT32_MAX);
    132     socket_address_queries(socket, 10022);
    133     MEM16(0x102000) = 2;
    134     assert(call(linux_network_lookup("connect", 0), 3, (uint32_t[]){socket, 0x102000, 16}) == 0);
    135     socket_address_queries(socket, 0);
    136     // Unsupported half-close leaves the connection available for the test below.
    137     assert(linux_network_lookup("shutdown", 0) == linux_network_lookup(NULL, 22));
    138     assert(call(linux_network_lookup(NULL, 22), 2, (uint32_t[]){socket, 1}) == UINT32_MAX);
    139 
    140     assert(MEM32(g_fs_base + 0x34) == 10045);
    141 
    142     uint32_t event = call(linux_sync_lookup("CreateEventA"), 4, (uint32_t[]){0, 1, 0, 0});
    143     assert(event);
    144     assert(call(linux_network_lookup("WSAEventSelect", 0), 3, (uint32_t[]){socket, event, 33}) == 0);
    145     const unsigned char cookie[] = {
    146         0xdf, 0xdf, 0, 1, 0, 1, 0, 32, 0, 0, 0, 1, 0, 0, 0, 2,
    147         0, 0, 0, 0x1b, 0, 0, 0, 73, 0, 0, 0, 0, 0, 0, 0, 0
    148     };
    149     memcpy((void *)ADDR(0x103000), cookie, sizeof(cookie));
    150 
    151     assert(call(linux_network_lookup("send", 0), 4, (uint32_t[]){socket, 0x103000, 7, 0}) == 7);
    152     assert(call(linux_network_lookup("send", 0), 4, (uint32_t[]){socket, 0x103007, 25, 0}) == 25);
    153     assert(call(linux_sync_lookup("WaitForSingleObject"), 2, (uint32_t[]){event, 10000}) == 0);
    154     assert(call(linux_network_lookup("WSAEnumNetworkEvents", 0), 3, (uint32_t[]){socket, event, 0x104000}) == 0);
    155     uint32_t received = call(linux_network_lookup("recv", 0), 4, (uint32_t[]){socket, 0x105000, 1024, 0});
    156     FILE *asset = fopen("cd_image/data/db/ResourceDatabase.dat", "rb");
    157     assert(asset);
    158     unsigned char bytes[16];
    159     size_t read = fread(bytes, 1, sizeof(bytes), asset);
    160     fclose(asset);
    161 
    162     assert(MEM32(0x104000) & 1);
    163     assert(received >= 20 && received <= 1024);
    164     assert(MEM8(0x105010) == 0x5f && MEM8(0x105011) == 0x4b);
    165     assert(MEM8(0x105012) == 0x1a && MEM8(0x105013) == 0x39);
    166     assert(read == 16);
    167     assert(call(linux_network_lookup("closesocket", 0), 1, &socket) == 0);
    168     assert(call(linux_network_lookup("WSACleanup", 0), 0, NULL) == 0);
    169     hostname_queries(0);
    170     address_strings();
    171     puts("PASS: worker waits, asset range reads, WebSocket AO initialization and network events");
    172     emscripten_exit_with_live_runtime();
    173 }