aorecomp

Anarchy Online classic in the browser

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


client/tests/browser_storage.c (9235B)


      1 #include <assert.h>
      2 #include <errno.h>
      3 #include <stdio.h>
      4 #include <string.h>
      5 #include <sys/stat.h>
      6 #include <emscripten.h>
      7 #include <emscripten/html5.h>
      8 
      9 void browser_initialize(unsigned character_id);
     10 void browser_select_character(unsigned character_id);
     11 
     12 static void replace_occlusion_cache(void)
     13 {
     14     const char *path = "cd_image/OCC/OCCBL_705.i";
     15     const char *remote = "/remote/cd_image/OCC/OCCBL_705.i";
     16     FILE *source = fopen(remote, "rb"), *copy = fopen(path, "rb");
     17     assert(source && copy);
     18     unsigned char expected[8192], actual[8192];
     19     size_t size;
     20     while ((size = fread(expected, 1, sizeof(expected), source))) {
     21         assert(fread(actual, 1, size, copy) == size);
     22         assert(!memcmp(expected, actual, size));
     23     }
     24     assert(!ferror(source) && fgetc(copy) == EOF);
     25     fclose(source);
     26     fclose(copy);
     27     struct stat before, after;
     28     assert(stat(remote, &before) == 0 && before.st_size > 8);
     29 
     30     copy = fopen(path, "wb");
     31     assert(copy);
     32     assert(fwrite("occluded", 1, 8, copy) == 8);
     33     assert(fclose(copy) == 0);
     34 
     35     assert(stat(remote, &after) == 0 && after.st_size == before.st_size);
     36     assert(stat(path, &after) == 0 && after.st_size == 8);
     37     copy = fopen(path, "rb");
     38     assert(copy && fread(actual, 1, 8, copy) == 8);
     39     assert(!memcmp(actual, "occluded", 8));
     40     fclose(copy);
     41 }
     42 
     43 static void create_occlusion_cache(void)
     44 {
     45     struct stat info;
     46     assert(stat("cd_image/OCC/storage-test.i", &info) == -1);
     47 
     48     FILE *file = fopen("cd_image/OCC/storage-test.i", "wb");
     49     assert(file && fwrite("cache", 1, 5, file) == 5);
     50     assert(fclose(file) == 0);
     51 
     52     assert(stat("cd_image/OCC/storage-test.i", &info) == 0 && info.st_size == 5);
     53 }
     54 
     55 static void read_across_asset_chunk(void)
     56 {
     57     FILE *file = fopen("cd_image/sound/SourceFiles/SM_Sandy_Game_Dummy.sbf", "rb");
     58     assert(file);
     59     assert(setvbuf(file, NULL, _IONBF, 0) == 0);
     60     assert(fseek(file, 0, SEEK_END) == 0 && ftell(file) == 1110471);
     61     unsigned char actual[16];
     62     memset(actual, 0xa5, sizeof(actual));
     63     assert(fseek(file, 1048568, SEEK_SET) == 0);
     64 
     65     size_t size = fread(actual, 1, sizeof(actual), file);
     66     fclose(file);
     67 
     68     assert(size == 16);
     69     const unsigned char expected[16] = {0};
     70     assert(!memcmp(actual, expected, sizeof(expected)));
     71 }
     72 
     73 static void preferences_use_canvas_dimensions(void)
     74 {
     75     int width, height, configured_width = 0, configured_height = 0;
     76     assert(emscripten_get_canvas_element_size("#canvas", &width, &height) == EMSCRIPTEN_RESULT_SUCCESS);
     77     FILE *file = fopen("Prefs/Main.cfg", "r");
     78     assert(file);
     79     char line[4096];
     80 
     81     while (fgets(line, sizeof(line), file)) {
     82         if (!strncmp(line, "DisplayWidth ", 13)) assert(sscanf(line + 13, "%d", &configured_width) == 1);
     83         if (!strncmp(line, "DisplayHeight ", 14)) assert(sscanf(line + 14, "%d", &configured_height) == 1);
     84     }
     85     assert(!ferror(file));
     86     fclose(file);
     87 
     88     assert(configured_width == width && configured_height == height);
     89 }
     90 
     91 static void icon_positions_survive_reload(unsigned character_id)
     92 {
     93     int resume = MAIN_THREAD_EM_ASM_INT({
     94         return new URLSearchParams(location.search).has('resume');
     95     });
     96     char path[128];
     97     snprintf(path, sizeof(path), "Prefs/localtest/Char%u/IconPositions.bin", character_id);
     98     const char *expected = character_id == 42 ? "icon positions" : "other position";
     99     if (!resume) {
    100         if (character_id == 43) {
    101             FILE *file = fopen(path, "rb");
    102             assert(!file && errno == ENOENT);
    103         }
    104         FILE *file = fopen(path, "wb");
    105         assert(file);
    106         assert(fwrite(expected, 1, 14, file) == 14);
    107         assert(fclose(file) == 0);
    108         return;
    109     }
    110 
    111     FILE *file = fopen(path, "rb");
    112     assert(file);
    113     char content[15] = {0};
    114     assert(fread(content, 1, sizeof(content), file) == 14);
    115     assert(!strcmp(content, expected));
    116     assert(fclose(file) == 0);
    117 }
    118 
    119 static void references_survive_reload(unsigned character_id, int resume)
    120 {
    121     char path[128];
    122     snprintf(path, sizeof(path), "Prefs/localtest/Char%u/References.bin", character_id);
    123     const unsigned char first[] = {0,0,3,235, 0,0,0,1, 0,0,0,0, 0,0,195,80, 0,0,0,53, 0,0,0,3, 'o','n','e'};
    124     const unsigned char other[] = {0,0,3,235, 0,0,0,1, 0,0,0,0, 0,0,195,80, 0,0,0,54, 0,0,0,3, 't','w','o'};
    125     const unsigned char empty[] = {0,0,3,235, 0,0,0,0};
    126     int cleared = MAIN_THREAD_EM_ASM_INT({ return new URLSearchParams(location.search).has('empty'); });
    127     const unsigned char *expected = cleared ? empty : character_id == 42 ? first : other;
    128     size_t length = cleared ? sizeof(empty) : sizeof(first);
    129     unsigned char actual[32] = {0};
    130     FILE *file = fopen(path, "rb");
    131 
    132     size_t size = 0;
    133     if (resume) {
    134         assert(file);
    135         size = fread(actual, 1, sizeof(actual), file);
    136         assert(!ferror(file));
    137         assert(fclose(file) == 0);
    138     }
    139 
    140     if (resume) {
    141         assert(size == length && !memcmp(actual, expected, length));
    142     } else {
    143         assert(!file && errno == ENOENT);
    144         file = fopen(path, "wb");
    145         assert(file && fwrite(expected, 1, length, file) == length);
    146         assert(fclose(file) == 0);
    147     }
    148 
    149     /* Native deletion rewrites the file, including truncating old records. */
    150     int clear = MAIN_THREAD_EM_ASM_INT({ return new URLSearchParams(location.search).has('clear'); });
    151     if (clear) {
    152         file = fopen(path, "wb");
    153         assert(file && fwrite(empty, 1, sizeof(empty), file) == sizeof(empty));
    154         assert(fclose(file) == 0);
    155     }
    156 }
    157 
    158 static void ignored_players_survive_reload(unsigned character_id, int resume)
    159 {
    160     char path[128];
    161     snprintf(path, sizeof(path), "Prefs/localtest/Char%u/IgnoreList.bin", character_id);
    162     const unsigned char first[] = {0,0,0,2, 0,0,0,53, 0,0,0,54};
    163     const unsigned char other[] = {0,0,0,1, 0,0,0,42};
    164     const unsigned char empty[] = {0,0,0,0};
    165     int cleared = MAIN_THREAD_EM_ASM_INT({ return new URLSearchParams(location.search).has('empty'); });
    166     const unsigned char *expected = cleared ? empty : character_id == 42 ? first : other;
    167     size_t length = cleared ? sizeof(empty) : character_id == 42 ? sizeof(first) : sizeof(other);
    168 
    169     FILE *file = fopen(path, "rb");
    170     unsigned char actual[16] = {0};
    171     size_t size = 0;
    172     if (resume) {
    173         assert(file);
    174         size = fread(actual, 1, sizeof(actual), file);
    175         assert(!ferror(file));
    176         assert(fclose(file) == 0);
    177     }
    178 
    179     if (resume) {
    180         assert(size == length && !memcmp(actual, expected, length));
    181     } else {
    182         assert(!file && errno == ENOENT);
    183         file = fopen(path, "wb");
    184         assert(file && fwrite(expected, 1, length, file) == length);
    185         assert(fclose(file) == 0);
    186     }
    187 
    188     /* Removing the last ignore saves a zero count, truncating the old IDs. */
    189     int clear = MAIN_THREAD_EM_ASM_INT({ return new URLSearchParams(location.search).has('clear'); });
    190     if (clear) {
    191         file = fopen(path, "wb");
    192         assert(file && fwrite(empty, 1, sizeof(empty), file) == sizeof(empty));
    193         assert(fclose(file) == 0);
    194     }
    195 }
    196 
    197 int main(void)
    198 {
    199     unsigned character_id = MAIN_THREAD_EM_ASM_INT({
    200         return new URLSearchParams(location.search).has('other') ? 43 : 42;
    201     });
    202     int resume = MAIN_THREAD_EM_ASM_INT({
    203         return new URLSearchParams(location.search).has('resume');
    204     });
    205 
    206     browser_initialize(resume ? character_id : 0);
    207 
    208     if (!resume) {
    209         browser_select_character(character_id);
    210         browser_select_character(character_id);
    211     }
    212 
    213     char path[128];
    214     snprintf(path, sizeof(path), "Prefs/localtest/Char%u/Char.cfg", character_id);
    215     FILE *preferences = fopen(path, "r");
    216     assert(preferences);
    217     char content[128] = {0};
    218     assert(fread(content, 1, sizeof(content) - 1, preferences) == 29);
    219     fclose(preferences);
    220     assert(!strcmp(content, resume ? "IsFirstTime 0\nIsTipEnabled 1\n" : "IsFirstTime 1\nIsTipEnabled 0\n"));
    221     int pending = MAIN_THREAD_EM_ASM_INT({
    222         return new URLSearchParams(location.search).has('pending');
    223     });
    224     if (!resume && !pending) {
    225         preferences = fopen(path, "w");
    226         assert(preferences);
    227         assert(fputs("IsFirstTime 0\nIsTipEnabled 1\n", preferences) >= 0);
    228         assert(fclose(preferences) == 0);
    229     }
    230     preferences_use_canvas_dimensions();
    231     icon_positions_survive_reload(character_id);
    232     if (!pending) {
    233         references_survive_reload(character_id, resume);
    234         ignored_players_survive_reload(character_id, resume);
    235     }
    236     replace_occlusion_cache();
    237     create_occlusion_cache();
    238     read_across_asset_chunk();
    239     puts("PASS: browser occlusion caches preserve assets and support writes");
    240     puts("PASS: asset reads preserve bytes across the 1 MiB boundary");
    241     puts("PASS: private preferences use the canvas dimensions");
    242     puts("PASS: native first-run state and character preference edits persist");
    243     puts("PASS: creator identity transitions mount persistent character preferences");
    244     puts("PASS: native icon positions use persistent browser storage");
    245     puts("PASS: native references persist with character isolation and deletion");
    246     puts("PASS: native ignore lists persist with character isolation and removal");
    247 }