client/tests/getenv.c (3224B)
1 #include <assert.h> 2 #include <stdint.h> 3 #include <stdio.h> 4 #include <string.h> 5 #ifdef _WIN32 6 #include <windows.h> 7 static char *(__cdecl *get_variable)(const char *); 8 static int *(__cdecl *crt_errno)(void); 9 #else 10 #include "guest_crt.h" 11 #include "guest_memory.h" 12 recomp_func_t recomp_lookup(uint32_t address) { (void)address; abort(); } 13 recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } 14 #endif 15 16 static char *lookup(const char *name) 17 { 18 #ifdef _WIN32 19 return get_variable(name); 20 #else 21 if (name) strcpy((char *)ADDR(0x50000100u), name); 22 g_esp = 0x5000ff00u; 23 MEM32(g_esp) = RECOMP_RETADDR; 24 MEM32(g_esp + 4) = name ? 0x50000100u : 0; 25 g_ebx = 0x12345678; g_esi = 0x23456789; g_edi = 0x3456789a; g_ebp = 0x456789ab; 26 guest_crt_lookup("getenv")(); 27 assert(g_esp == 0x5000ff04u && MEM32(g_esp) == (name ? 0x50000100u : 0)); 28 assert(g_ebx == 0x12345678 && g_esi == 0x23456789 && g_edi == 0x3456789a && g_ebp == 0x456789ab); 29 return (char *)ADDR(g_eax); 30 #endif 31 } 32 33 int main(void) 34 { 35 char setting[] = "AO_NAME=Setupf"; 36 char *environment[] = {setting, "AO_NAME_MORE=suffix", "Empty=", "A=B=C", 37 "MiXeD=first", "mixed=second", "=C:=C:\\fake", "\xc0VAR=extended", NULL}; 38 const struct { const char *name, *value; int entry; } cases[] = { 39 {"AO_NAME", "Setupf", 0}, {"ao_name", "Setupf", 0}, {"AO", NULL, -1}, 40 {"AO_NAME_MORE", "suffix", 1}, {"empty", "", 2}, {"A", "B=C", 3}, 41 {"A=B", "C", 3}, {"mixed", "first", 4}, {"=C:", "C:\\fake", 6}, 42 {"", "C:=C:\\fake", 6}, {"MISSING", NULL, -1}, {NULL, NULL, -1}, 43 {"\xc0var", "extended", 7}, {"\xe0VAR", NULL, -1} 44 }; 45 #ifdef _WIN32 46 HMODULE module = LoadLibraryA("msvcrt.dll"); 47 get_variable = (void *)GetProcAddress(module, "getenv"); 48 crt_errno = (void *)GetProcAddress(module, "_errno"); 49 char ***table = (void *)GetProcAddress(module, "_environ"); 50 assert(get_variable && crt_errno && table); 51 get_variable("AO_UNIT_ABSENT"); 52 char **saved = *table; 53 *table = environment; 54 int *error = crt_errno(); 55 #else 56 assert(guest_map("getenv test", 0x50000000u, 0x10000u)); 57 g_fs_base = 0x50004000u; 58 char *arguments[] = {"./game", NULL}; 59 guest_crt_startup(1, arguments, environment); 60 int *error = (void *)ADDR(guest_crt_errno_address()); 61 #endif 62 for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { 63 *error = 77; 64 65 char *value = lookup(cases[i].name); 66 67 assert(*error == (cases[i].name ? 77 : 22)); 68 if (!cases[i].value) assert(!value); 69 else { 70 assert(value && !strcmp(value, cases[i].value)); 71 #ifdef _WIN32 72 assert(value == environment[cases[i].entry] + strlen(cases[i].name) + 1); 73 #else 74 uint32_t table = MEM32(guest_crt_data("_environ")); 75 assert(g_eax == MEM32(table + cases[i].entry * 4) + strlen(cases[i].name) + 1); 76 #endif 77 } 78 } 79 lookup("AO_NAME")[0] = 'X'; 80 assert(!strcmp(lookup("ao_name"), "Xetupf")); 81 #ifdef _WIN32 82 *table = saved; 83 #endif 84 puts("getenv: case folding, exact prefixes, empty values, duplicate order, guest pointers, writable values and errno passed"); 85 return 0; 86 }