client/tests/chmod.c (4202B)
1 #include <assert.h> 2 #include <stdint.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #ifdef _WIN32 7 #include <windows.h> 8 static HMODULE crt; 9 #else 10 #include "guest_stdio.h" 11 #include "guest_crt.h" 12 #include "guest_memory.h" 13 #include <sys/stat.h> 14 #include <unistd.h> 15 recomp_func_t recomp_lookup(uint32_t address) { (void)address; abort(); } 16 recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } 17 #endif 18 19 static void check_mode(const char *path, unsigned mode, int expected_result, unsigned expected_error) 20 { 21 #ifdef _WIN32 22 int *(__cdecl *crt_errno)(void) = (void *)GetProcAddress(crt, "_errno"); 23 int (__cdecl *crt_chmod)(const char *, int) = (void *)GetProcAddress(crt, "_chmod"); 24 assert(crt_errno && crt_chmod); 25 *crt_errno() = 77; 26 #else 27 strcpy((char *)ADDR(0x50002000u), path); 28 MEM32(guest_crt_errno_address()) = 77; 29 g_esp = 0x5000ff00u; 30 MEM32(g_esp + 4) = 0x50002000u; 31 MEM32(g_esp + 8) = mode; 32 g_ebx = 0x12345678u; 33 g_esi = 0x23456789u; 34 g_edi = 0x3456789au; 35 g_ebp = 0x456789abu; 36 #endif 37 38 #ifdef _WIN32 39 int result = crt_chmod(path, mode); 40 unsigned error = *crt_errno(); 41 #else 42 guest_stdio_lookup("_chmod")(); 43 int result = (int32_t)g_eax; 44 unsigned error = MEM32(guest_crt_errno_address()); 45 #endif 46 47 if (result != expected_result || error != expected_error) 48 fprintf(stderr, "chmod %s mode %x: result %d errno %u\n", path, mode, result, error); 49 assert(result == expected_result && error == expected_error); 50 if (!result) { 51 #ifdef _WIN32 52 DWORD attributes = GetFileAttributesA(path); 53 assert(attributes != INVALID_FILE_ATTRIBUTES); 54 assert(!!(attributes & FILE_ATTRIBUTE_READONLY) == !(mode & 0x80)); 55 #else 56 char normalized[128]; 57 strcpy(normalized, path); 58 for (char *p = normalized; *p; ++p) if (*p == '\\') *p = '/'; 59 struct stat info; 60 assert(!stat(normalized, &info)); 61 assert(!!(info.st_mode & S_IWUSR) == !!(mode & 0x80)); 62 assert((info.st_mode & 0444) == 0444 && !(info.st_mode & 0111)); 63 #endif 64 } 65 #ifndef _WIN32 66 assert(g_esp == 0x5000ff04u); 67 assert(g_ebx == 0x12345678u && g_esi == 0x23456789u); 68 assert(g_edi == 0x3456789au && g_ebp == 0x456789abu); 69 #endif 70 } 71 72 int main(void) 73 { 74 /* Compare the legacy system CRT in isolated directories. */ 75 #ifdef _WIN32 76 crt = LoadLibraryA("msvcrt.dll"); 77 assert(crt); 78 char temporary[MAX_PATH], directory[MAX_PATH]; 79 assert(GetTempPathA(sizeof(temporary), temporary)); 80 assert(GetTempFileNameA(temporary, "aoc", 0, directory)); 81 assert(DeleteFileA(directory) && CreateDirectoryA(directory, NULL)); 82 assert(SetCurrentDirectoryA(directory) && CreateDirectoryA("nested", NULL)); 83 #else 84 assert(guest_map("chmod test memory", 0x50000000u, 0x10000u)); 85 g_fs_base = 0x50001000u; 86 char directory[] = "/tmp/ao-chmod-test-XXXXXX"; 87 assert(mkdtemp(directory) && !chdir(directory) && !mkdir("nested", 0700)); 88 #endif 89 FILE *file = fopen("nested/file.txt", "w"); 90 assert(file && fputs("preserved", file) >= 0 && !fclose(file)); 91 #ifndef _WIN32 92 assert(!chmod("nested/file.txt", 0644)); 93 #endif 94 check_mode("nested\\file.txt", 0x100, 0, 77); 95 check_mode("nested/file.txt", 0x180, 0, 77); 96 check_mode("nested/file.txt", 0, 0, 77); 97 check_mode("nested/file.txt", 0x80, 0, 77); 98 check_mode("nested/file.txt", 1, 0, 77); 99 check_mode("nested/file.txt", 0xffffffffu, 0, 77); 100 check_mode("missing", 0x180, -1, 2); 101 check_mode("missing\\file", 0x180, -1, 2); 102 check_mode("nested\\file.txt\\child", 0x180, -1, 2); 103 check_mode("", 0x180, -1, 2); 104 file = fopen("nested/file.txt", "r"); 105 char contents[16] = {0}; 106 assert(file && fread(contents, 1, sizeof(contents), file) == 9 && !fclose(file)); 107 assert(!strcmp(contents, "preserved") && !remove("nested/file.txt")); 108 #ifdef _WIN32 109 assert(RemoveDirectoryA("nested") && SetCurrentDirectoryA(temporary) && RemoveDirectoryA(directory)); 110 #else 111 assert(!rmdir("nested") && !chdir("/tmp") && !rmdir(directory)); 112 #endif 113 puts("chmod: 10 cases, read-only transitions, mode bits, errors, preserved contents and ABI passed"); 114 return 0; 115 }