client/tests/browser_testing.c (17984B)
1 #include "recomp_types.h" 2 #include <assert.h> 3 4 void browser_test_request(void); 5 void browser_test_frame(void); 6 void browser_test_pick_completed(uint32_t camera); 7 static unsigned publications; 8 static char *snapshot; 9 10 void ao_test_publish(const char *json) 11 { 12 free(snapshot); 13 snapshot = strdup(json); 14 ++publications; 15 } 16 17 int linux_graphics_project(const float position[3], float screen[3]) 18 { 19 memcpy(screen, position, 12); 20 return 1; 21 } 22 23 static void snapshot_copies_native_state_without_changing_guest_registers(void) 24 { 25 guest_map("test engine", 0x2d010000, 0x10000); 26 guest_map("test UI", 0x27040000, 0x10000); 27 guest_map("test action view", 0x2b0e0000, 0x10000); 28 guest_map("test directory", 0x38060000, 0x10000); 29 guest_map("test objects", 0x50000000, 0x10000); 30 MEM32(0x2d01752c) = 0x50000000; 31 MEM32(0x50000088) = 0x50001000; 32 MEM32(0x500000d8) = 51050; 33 MEM32(0x500000dc) = 99; 34 MEM32(0x27048be8) = 1024; 35 MEM32(0x27048bec) = 768; 36 MEM32(0x3806c110) = 0x50000200; 37 MEM32(0x50000204) = 0x50000300; 38 MEM32(0x50000208) = 0x50000400; 39 MEM32(0x50000304) = 0x50000500; 40 memcpy((void *)ADDR(0x50000500), (uint32_t[]){0x50000400, 0, 0x50000400, 50000, 7, 0x50001000}, 24); 41 MEM32(0x50001010) = 1; 42 MEM32(0x50001060) = 0x50002000; 43 MEM32(0x50001078) = 705; 44 MEM32(0x500010e8) = 0x50003000; 45 MEM32(0x50003004) = 0x50004000; 46 MEM32(0x50001150) = 0x50006000; 47 MEM32(0x50001168) = 0x50008000; 48 MEM32(0x50008070) = 50000; 49 MEM32(0x50008074) = 9; 50 MEM32(0x5000600c) = 0x50006100; 51 MEM32(0x50006104) = 0x50006200; 52 MEM32(0x50006204) = 0x50006300; 53 MEM32(0x50006300 + 48 * 4) = 0x50007000; 54 memcpy((void *)ADDR(0x50007000), (uint32_t[]){0, 3 << 16, 51005, 25, 1, 25812, 25812, 0}, 32); 55 memcpy((void *)ADDR(0x50002054), (float[]){10, 20, 30}, 12); 56 57 /* Mission statels retain distinct typed identities with the same numeric ID. */ 58 MEM32(0x50000304) = 0x50000600; 59 memcpy((void *)ADDR(0x50000600), (uint32_t[]){0x50000500, 0, 0x50000700, 56001, 99, 0x50009000}, 24); 60 memcpy((void *)ADDR(0x50000700), (uint32_t[]){0x50000400, 0, 0x50000400, 56006, 99, 0x5000a000}, 24); 61 MEM32(0x50009060) = 0x5000b000; 62 MEM32(0x5000a060) = 0x5000c000; 63 memcpy((void *)ADDR(0x5000b054), (float[]){100, 115, 148}, 12); 64 memcpy((void *)ADDR(0x5000c054), (float[]){89, 115, 151}, 12); 65 g_eax = 123; 66 g_esp = 456; 67 68 browser_test_pick_completed(0); 69 browser_test_frame(); 70 assert(publications == 0); 71 browser_test_request(); 72 browser_test_frame(); 73 74 assert(publications == 1); 75 assert(strstr(snapshot, "\"frame\":2")); 76 assert(strstr(snapshot, "\"localId\":7")); 77 assert(strstr(snapshot, "\"position\":[10,20,30]")); 78 assert(strstr(snapshot, "\"type\":56001,\"id\":99,\"position\":[100,115,148],\"screenPosition\":[100,115,148]")); 79 assert(strstr(snapshot, "\"type\":56006,\"id\":99,\"position\":[89,115,151],\"screenPosition\":[89,115,151]")); 80 assert(strstr(snapshot, "\"hover\":[51050,99]")); 81 assert(strstr(snapshot, "\"pick\":null")); 82 assert(strstr(snapshot, "\"selected\":[50000,9]")); 83 assert(strstr(snapshot, "\"actions\":[]")); 84 assert(strstr(snapshot, "\"slot\":48,\"type\":51005,\"id\":25,\"quality\":1,\"templates\":[25812,25812],\"count\":3")); 85 assert(g_eax == 123 && g_esp == 456); 86 } 87 88 static void snapshots_include_reclaim_booths_without_mutation(void) 89 { 90 MEM32(0x50000304) = 0x50000600; 91 MEM32(0x5000070c) = 51059; 92 93 browser_test_request(); 94 browser_test_frame(); 95 96 assert(strstr(snapshot, "\"type\":51059,\"id\":99,\"position\":[89,115,151],\"screenPosition\":[89,115,151]")); 97 assert(MEM32(0x5000070c) == 51059 && MEM32(0x50000710) == 99); 98 assert(g_eax == 123 && g_esp == 456); 99 } 100 101 static void snapshots_report_native_creation_state(void) 102 { 103 MEM32(0x2b0ee7d0) = 0x5000d000; 104 MEM32(0x2b0ee7e0) = 0x5000d000; 105 MEM32(0x5000d040) = 4; 106 MEM32(0x5000d05c) = 32; 107 MEM32(0x5000d0a4) = 0x1008; 108 109 browser_test_request(); 110 browser_test_frame(); 111 112 assert(strstr(snapshot, "\"creation\":{\"scene\":4,\"hover\":32,\"nameState\":4104,\"ready\":true}")); 113 assert(MEM32(0x5000d0a4) == 0x1008 && g_eax == 123 && g_esp == 456); 114 MEM32(0x2b0ee7d0) = MEM32(0x2b0ee7e0) = 0; 115 } 116 117 static void snapshots_preserve_wrapped_weapon_results(void) 118 { 119 MEM32(0x5000116c) = 0x50008100; 120 MEM32(0x50008124) = 0x50008200; 121 MEM32(0x50008220) = 0x50008700; 122 MEM32(0x50008704) = 0x500085a0; 123 MEM32(0x500085a4) = 0x50008600; 124 MEM32(0x50008668) = 20; 125 MEM32(0x50008210) = 0x50008300; 126 MEM32(0x50008304) = 0x50008400; 127 MEM32(0x50008214) = 1; 128 MEM32(0x50008218) = 49; 129 MEM32(0x50008400 + 49 * 8) = 7; 130 MEM32(0x50008404 + 49 * 8) = 4; 131 MEM32(0x50008400) = 13; 132 MEM32(0x50008404) = 5; 133 134 browser_test_request(); 135 browser_test_frame(); 136 137 assert(strstr(snapshot, "\"weapons\":[{\"slot\":6,\"loadedAmmo\":20,\"read\":49,\"write\":1,\"pending\":[[7,4],[13,5]]}]")); 138 assert(MEM32(0x50008668) == 20); 139 assert(MEM32(0x50008214) == 1 && MEM32(0x50008218) == 49); 140 assert(MEM32(0x50008400 + 49 * 8) == 7 && MEM32(0x50008404 + 49 * 8) == 4); 141 assert(g_eax == 123 && g_esp == 456); 142 143 /* An unbound controller has no finite ammunition or item stats to read. */ 144 MEM32(0x50008220) = 0; 145 browser_test_request(); 146 browser_test_frame(); 147 assert(strstr(snapshot, "\"slot\":6,\"loadedAmmo\":-1")); 148 assert(MEM32(0x50008214) == 1 && MEM32(0x50008218) == 49); 149 assert(g_eax == 123 && g_esp == 456); 150 MEM32(0x50008220) = 0x50008700; 151 } 152 153 static void completed_picks_record_the_query_pixel_and_result(void) 154 { 155 MEM32(0x2b0ee640) = 0x5000f000; 156 MEM32(0x5000f010) = 0x5000f100; 157 memcpy((void *)ADDR(0x5000f12c), (int32_t[]){13, 9, 814, 610}, 16); 158 MEM32(0x3806ec04) = 0x5000d000; 159 MEM32(0x5000d080) = 0x5000e000; 160 MEM32(0x5000e060) = 0x5000e200; 161 memcpy((void *)ADDR(0x5000e254), (float[]){10, 22, 35}, 12); 162 memcpy((void *)ADDR(0x5000e108), (float[]){-0.5f, 0.25f}, 8); 163 MEM32(0x5000e0f0) = 51005; 164 MEM32(0x5000e0f4) = 75; 165 166 browser_test_pick_completed(0x5000e000); 167 browser_test_request(); 168 browser_test_frame(); 169 170 assert(strstr(snapshot, "\"pick\":{\"sequence\":1,\"position\":[213,234],\"identity\":[51005,75]}")); 171 assert(strstr(snapshot, "\"camera\":{\"firstPerson\":true,\"angles\":[0,0],\"position\":[10,22,35]}")); 172 assert(strstr(snapshot, "\"hover\":[51050,99]")); 173 assert(g_eax == 123 && g_esp == 456); 174 } 175 176 static void snapshots_do_not_advance_the_pick_generation(void) 177 { 178 browser_test_request(); 179 browser_test_frame(); 180 181 assert(strstr(snapshot, "\"pick\":{\"sequence\":1,\"position\":[213,234],\"identity\":[51005,75]}")); 182 } 183 184 static void snapshots_observe_directory_clear_and_new_frames(void) 185 { 186 MEM32(0x50000304) = 0x50000400; 187 MEM32(0x3806ec04) = 0; 188 189 browser_test_request(); 190 browser_test_frame(); 191 192 assert(publications == 4); 193 assert(strstr(snapshot, "\"frame\":5")); 194 assert(strstr(snapshot, "\"localId\":0")); 195 assert(strstr(snapshot, "\"actors\":[]")); 196 assert(strstr(snapshot, "\"pick\":null")); 197 } 198 199 static void snapshots_observe_local_and_remote_actions_without_mutation(void) 200 { 201 MEM32(0x50000304) = 0x50000500; 202 MEM32(0x50001184) = 0x5000d800; 203 MEM32(0x5000d804) = 4; 204 MEM32(0x5000d808) = 0x5000d900; 205 MEM32(0x5000d904) = 0x5000da00; 206 memcpy((void *)ADDR(0x5000da00), (uint32_t[]){0x5000db00, 0, 0x5000dc00, 0x5000dd00}, 16); 207 MEM32(0x5000db18) = 6; 208 MEM32(0x5000dc18) = 3; 209 MEM32(0x5000dd18) = 12; 210 211 browser_test_request(); 212 browser_test_frame(); 213 214 assert(strstr(snapshot, "\"actions\":[6,3,12]")); 215 assert(MEM32(0x5000d804) == 4 && MEM32(0x5000db18) == 6); 216 assert(MEM32(0x5000dc18) == 3 && MEM32(0x5000dd18) == 12); 217 assert(g_eax == 123 && g_esp == 456); 218 219 /* A remote actor exposes the same action list without exposing inventory. */ 220 MEM32(0x50000088) = 0; 221 browser_test_request(); 222 browser_test_frame(); 223 assert(strstr(snapshot, "\"actions\":[6,3,12]")); 224 assert(!strstr(snapshot, "\"inventory\":")); 225 assert(MEM32(0x5000d804) == 4 && MEM32(0x5000db18) == 6); 226 assert(MEM32(0x5000dc18) == 3 && MEM32(0x5000dd18) == 12); 227 assert(g_eax == 123 && g_esp == 456); 228 MEM32(0x50000088) = 0x50001000; 229 } 230 231 static void snapshots_record_visible_grid_cells_and_scroll_without_mutation(void) 232 { 233 guest_map("test icon grid", 0x51000000, 0x10000); 234 MEM32(0x27048b7c) = 0x51000000; 235 MEM32(0x51000000) = 0x51000100; 236 MEM8(0x51000004) = 1; 237 MEM32(0x51000104) = 0x51000200; 238 memcpy((void *)ADDR(0x51000200), (uint32_t[]){0x51000220, 0x51000240, 0}, 12); 239 memcpy((void *)ADDR(0x51000220), (uint32_t[]){0x51000230, 0x51000200, 0x51000400}, 12); 240 memcpy((void *)ADDR(0x51000230), (uint32_t[]){0x51000240, 0x51000220, 0x51000600}, 12); 241 memcpy((void *)ADDR(0x51000240), (uint32_t[]){0x51000200, 0x51000230, 0x5100ffac}, 12); 242 243 /* The scrollbar itself is inactive, but still identifies the visible grid. */ 244 MEM32(0x51000400) = MEM32(0x51000600) = 0x27034524; 245 MEM32(0x510004b4) = 0x27011540; 246 MEM32(0x510004b8) = 0x51000800; 247 MEM32(0x510006b4) = 0x12345678; 248 MEM32(0x5100ffac) = 0x2703429c; 249 MEM32(0x51000800) = 0x51000a00; 250 MEM32(0x51000808) = 200; 251 MEM32(0x5100080c) = 250; 252 MEM32(0x51000810) = 3; 253 MEM32(0x51000814) = 2; 254 MEM32(0x51000818) = MEM32(0x5100081c) = 52; 255 MEM32(0x5100082c) = 1; 256 MEM8(0x51000835) = 1; 257 MEM32(0x5100083c) = 1; 258 MEM32(0x51000a04) = 0x51000b00; 259 MEM32(0x51000a08) = 9; 260 MEM32(0x51000b00) = 0x51000b20; 261 for (unsigned i = 0; i < 9; ++i) { 262 uint32_t node = 0x51000b20 + i * 16, object = 0x51001000 + i * 256; 263 MEM32(node) = i == 8 ? 0x51000b00 : node + 16; 264 MEM32(node + 8) = object; 265 MEM8(object + 8) = i >= 3; 266 MEM8(object + 0x70) = i != 3; 267 MEM32(object + 0x2c) = 200 + i % 3 * 52; 268 MEM32(object + 0x30) = 250 + ((int)i / 3 - 1) * 52; 269 MEM32(object + 0x34) = MEM32(object + 0x2c) + 44; 270 MEM32(object + 0x38) = MEM32(object + 0x30) + 44; 271 } 272 MEM32(0x51001354) = MEM32(0x51001554) = 104; 273 MEM32(0x51001358) = 77; 274 MEM32(0x51001558) = 48; 275 unsigned char before[100]; 276 memcpy(before, (void *)ADDR(0x51000800), sizeof(before)); 277 278 browser_test_request(); 279 browser_test_frame(); 280 281 assert(strstr(snapshot, "\"grids\":[{\"position\":[200,250],\"cellSize\":[52,52],\"columns\":3,\"rows\":2,\"firstRow\":1,\"totalCells\":9,\"dragType\":1")); 282 assert(strstr(snapshot, "\"cells\":[{\"index\":3,\"identity\":[104,77],\"bounds\":[200,250,244,294],\"dummy\":false}")); 283 assert(strstr(snapshot, "{\"index\":4,\"identity\":[0,0],\"bounds\":[252,250,296,294],\"dummy\":true}")); 284 assert(strstr(snapshot, "{\"index\":5,\"identity\":[104,48],\"bounds\":[304,250,348,294],\"dummy\":true}")); 285 assert(strstr(snapshot, "{\"index\":8,\"identity\":[0,0],\"bounds\":[304,302,348,346],\"dummy\":true}")); 286 assert(!strstr(snapshot, "\"index\":0")); 287 assert(!memcmp(before, (void *)ADDR(0x51000800), sizeof(before))); 288 assert(MEM32(0x51001358) == 77 && MEM8(0x51001570) == 1); 289 assert(g_eax == 123 && g_esp == 456); 290 291 /* Hidden grids are omitted even while their scrollbar stays registered. */ 292 MEM8(0x51000835) = 0; 293 browser_test_request(); 294 browser_test_frame(); 295 assert(strstr(snapshot, "\"grids\":[]")); 296 } 297 298 static void snapshots_record_dragged_identity_without_mutation(void) 299 { 300 MEM32(0x27048b6c) = 0x51000c00; 301 MEM32(0x51000c20) = 0x51001300; 302 303 browser_test_request(); 304 browser_test_frame(); 305 306 assert(strstr(snapshot, "\"dragged\":[104,77]")); 307 assert(MEM32(0x51000c20) == 0x51001300 && MEM32(0x51001358) == 77); 308 assert(g_eax == 123 && g_esp == 456); 309 MEM32(0x51000c20) = 0; 310 browser_test_request(); 311 browser_test_frame(); 312 assert(strstr(snapshot, "\"dragged\":[0,0]")); 313 } 314 315 static void snapshots_record_uploaded_mission_identities_without_mutation(void) 316 { 317 MEM32(0x2b0eeb14) = 0x51002000; 318 MEM32(0x5100205c) = 0x51002100; 319 MEM8(0x510021dc) = 1; 320 MEM32(0x510021c0) = 56003; 321 MEM32(0x510021c4) = 17; 322 MEM8(0x2b0ef07c) = 1; 323 MEM32(0x2b0ef070) = 56003; 324 MEM32(0x2b0ef074) = 18; 325 MEM32(0x2b0ef0a4) = 0x51002300; 326 MEM32(0x5100233c) = 0x51002400; 327 MEM8(0x51002430) = 1; 328 MEM32(0x51002414) = 56003; 329 MEM32(0x51002418) = 19; 330 331 browser_test_request(); 332 browser_test_frame(); 333 334 assert(strstr(snapshot, "\"missionWaypoints\":{\"local\":[[56003,17]],\"planet\":[56003,18],\"compass\":[56003,19]}")); 335 assert(MEM8(0x510021dc) == 1 && MEM32(0x510021c4) == 17); 336 assert(MEM8(0x2b0ef07c) == 1 && MEM32(0x2b0ef074) == 18); 337 assert(MEM8(0x51002430) == 1 && MEM32(0x51002418) == 19); 338 assert(g_eax == 123 && g_esp == 456); 339 340 /* Retired coordinates stay in memory but are no longer active markers. */ 341 MEM8(0x510021dc) = MEM8(0x2b0ef07c) = MEM8(0x51002430) = 0; 342 browser_test_request(); 343 browser_test_frame(); 344 assert(strstr(snapshot, "\"missionWaypoints\":{\"local\":[],\"planet\":null,\"compass\":null}")); 345 } 346 347 static void snapshots_distinguish_raw_and_effective_armor_without_mutation(void) 348 { 349 MEM32(0x50001154) = 0x50005100; 350 MEM32(0x50005108) = 0x50005200; 351 MEM32(0x50005204) = 0x50005300; 352 MEM32(0x50005304) = 0x50005300; 353 for (unsigned i = 0; i < 8; ++i) 354 MEM32(0x50004000 + (90 + i) * 4) = 10 + i; 355 356 browser_test_request(); 357 browser_test_frame(); 358 359 assert(strstr(snapshot, "\"effectiveArmor\":[10,11,12,13,14,15,16,17]")); 360 assert(g_eax == 123 && g_esp == 456); 361 362 const int modifiers[8] = {20, -3, 4, 5, 6, 7, 8, 9}; 363 const int children[8][2] = {{-1, -1}, {0, 2}, {-1, -1}, {1, 6}, {-1, 5}, {-1, -1}, {4, 7}, {-1, -1}}; 364 MEM32(0x50005304) = 0x50005460; 365 for (unsigned i = 0; i < 8; ++i) { 366 uint32_t node = 0x50005400 + i * 32; 367 MEM32(node) = children[i][0] < 0 ? 0x50005300 : 0x50005400 + children[i][0] * 32; 368 MEM32(node + 8) = children[i][1] < 0 ? 0x50005300 : 0x50005400 + children[i][1] * 32; 369 MEM32(node + 12) = 90 + i; 370 MEM32(node + 16) = modifiers[i]; 371 } 372 unsigned char before[0x600]; 373 memcpy(before, (void *)ADDR(0x50005000), sizeof(before)); 374 375 browser_test_request(); 376 browser_test_frame(); 377 378 assert(strstr(snapshot, "\"effectiveArmor\":[30,8,16,18,20,22,24,26]")); 379 assert(!memcmp(before, (void *)ADDR(0x50005000), sizeof(before))); 380 assert(MEM32(0x50004000 + 91 * 4) == 11); 381 assert(g_eax == 123 && g_esp == 456); 382 383 /* Missing keys use zero modifiers rather than a neighboring tree node. */ 384 MEM32(0x500054c0) = 0x50005300; 385 browser_test_request(); 386 browser_test_frame(); 387 assert(strstr(snapshot, "\"effectiveArmor\":[30,8,16,18,14,15,24,26]")); 388 MEM32(0x50001154) = 0; 389 } 390 391 static void snapshots_observe_door_animation_without_mutation(void) 392 { 393 /* Place a Door_t at the existing statel directory entry. */ 394 MEM32(0x5000070c) = 51016; 395 uint32_t object = 0x5000a000, door = object - 0x8c; 396 MEM32(door + 0x34) = 128; 397 MEM8(door + 0x1c5) = 1; 398 MEM32(object + 0xc4) = 0x5000d000; 399 MEM32(0x5000d010) = 0x5000e000; 400 MEMF(0x5000d024) = 0.75f; 401 MEMF(0x5000e12c) = 0.25f; 402 403 browser_test_request(); 404 browser_test_frame(); 405 406 assert(strstr(snapshot, "\"door\":{\"flags\":128,\"latched\":true,\"duration\":0.75,\"time\":0.25}")); 407 assert(MEM32(door + 0x34) == 128 && MEM8(door + 0x1c5) == 1); 408 assert(MEMF(0x5000e12c) == 0.25f); 409 assert(g_eax == 123 && g_esp == 456); 410 } 411 412 static void snapshots_observe_cursor_artwork_without_mutation(void) 413 { 414 /* The current shape owns both its hotspot and scaled sprite dimensions. */ 415 MEM32(0x27048bb4) = 0x5000f000; 416 MEM32(0x5000f008) = 4; 417 MEM8(0x5000f010) = 1; 418 MEM32(0x27048630) = 12; 419 MEM32(0x27048634) = 25; 420 MEM32(0x27048640) = 0x5000f100; 421 MEMF(0x5000f13c) = 48; 422 MEMF(0x5000f140) = 48; 423 424 browser_test_request(); 425 browser_test_frame(); 426 427 assert(strstr(snapshot, "\"cursor\":{\"shape\":4,\"visible\":true,\"hotspot\":[12,25],\"drawSize\":[48,48]}")); 428 assert(MEM32(0x5000f008) == 4 && MEM8(0x5000f010) == 1); 429 assert(g_eax == 123 && g_esp == 456); 430 } 431 432 static void snapshots_include_native_item_menu_actions(void) 433 { 434 guest_map("test item menu", 0x52000000, 0x10000); 435 MEM32(0x27048b98) = 0x52000000; 436 MEM32(0x52000000) = 104; 437 MEM32(0x52000004) = 48; 438 MEM32(0x52000010) = 1; 439 MEM32(0x52000014) = 0x52000100; 440 MEM32(0x5200015c) = 5; 441 MEM32(0x52000168) = 100; 442 MEM32(0x5200016c) = 120; 443 444 browser_test_request(); 445 browser_test_frame(); 446 447 assert(strstr(snapshot, "\"itemMenu\":{\"identity\":[104,48],\"actions\":[{\"command\":5,\"bounds\":[100,120,132,152]}]}")); 448 assert(MEM32(0x5200015c) == 5 && g_eax == 123 && g_esp == 456); 449 MEM32(0x27048b98) = 0; 450 browser_test_request(); 451 browser_test_frame(); 452 assert(strstr(snapshot, "\"itemMenu\":null")); 453 } 454 455 int main(void) 456 { 457 snapshot_copies_native_state_without_changing_guest_registers(); 458 completed_picks_record_the_query_pixel_and_result(); 459 snapshots_do_not_advance_the_pick_generation(); 460 snapshots_observe_directory_clear_and_new_frames(); 461 snapshots_observe_local_and_remote_actions_without_mutation(); 462 snapshots_include_reclaim_booths_without_mutation(); 463 snapshots_report_native_creation_state(); 464 snapshots_preserve_wrapped_weapon_results(); 465 snapshots_record_visible_grid_cells_and_scroll_without_mutation(); 466 snapshots_record_dragged_identity_without_mutation(); 467 snapshots_record_uploaded_mission_identities_without_mutation(); 468 snapshots_distinguish_raw_and_effective_armor_without_mutation(); 469 snapshots_observe_door_animation_without_mutation(); 470 snapshots_observe_cursor_artwork_without_mutation(); 471 snapshots_include_native_item_menu_actions(); 472 free(snapshot); 473 puts("PASS: frame snapshots, native inventory, directory clearing and guest state preservation"); 474 }