client/tests/create_directory.c (3174B)
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 #else 9 #include "linux_platform.h" 10 #include "guest_memory.h" 11 #include <sys/stat.h> 12 #include <unistd.h> 13 #endif 14 15 static void check_create(const char *name, int expected_result, unsigned expected_error) 16 { 17 #ifdef _WIN32 18 SetLastError(777); 19 #else 20 strcpy((char *)ADDR(0x50002000u), name); 21 MEM32(g_fs_base + 0x34) = 777; 22 g_esp = 0x5000ff00u; 23 MEM32(g_esp + 4) = 0x50002000u; 24 MEM32(g_esp + 8) = 0; 25 g_ebx = 0x12345678u; 26 g_esi = 0x23456789u; 27 g_edi = 0x3456789au; 28 g_ebp = 0x456789abu; 29 #endif 30 31 #ifdef _WIN32 32 int result = CreateDirectoryA(name, NULL); 33 unsigned error = GetLastError(); 34 #else 35 linux_platform_lookup("KERNEL32.dll", "CreateDirectoryA")(); 36 int result = g_eax; 37 unsigned error = MEM32(g_fs_base + 0x34); 38 #endif 39 40 if (result != expected_result || error != expected_error) 41 fprintf(stderr, "CreateDirectory %s: result %d error %u\n", name, result, error); 42 assert(result == expected_result && error == expected_error); 43 #ifndef _WIN32 44 assert(g_esp == 0x5000ff0cu); 45 assert(g_ebx == 0x12345678u && g_esi == 0x23456789u); 46 assert(g_edi == 0x3456789au && g_ebp == 0x456789abu); 47 #endif 48 } 49 50 int main(void) 51 { 52 /* Tests own a fresh temporary directory on each OS. */ 53 #ifdef _WIN32 54 char temporary[MAX_PATH], directory[MAX_PATH]; 55 assert(GetTempPathA(sizeof(temporary), temporary)); 56 assert(GetTempFileNameA(temporary, "aot", 0, directory)); 57 assert(DeleteFileA(directory) && CreateDirectoryA(directory, NULL)); 58 assert(SetCurrentDirectoryA(directory)); 59 #else 60 assert(guest_map("CreateDirectory test memory", 0x50000000u, 0x10000u)); 61 g_fs_base = 0x50001000u; 62 char directory[] = "/tmp/ao-create-test-XXXXXX"; 63 assert(mkdtemp(directory) && !chdir(directory)); 64 #endif 65 FILE *file = fopen("file.txt", "w"); 66 assert(file && !fclose(file)); 67 check_create("plain", 1, 777); 68 check_create("plain", 0, 183); 69 check_create("file.txt", 0, 183); 70 check_create("file.txt\\child", 0, 3); 71 check_create("missing\\child", 0, 3); 72 check_create("", 0, 3); 73 check_create(".", 0, 183); 74 check_create("PLAIN\\nested", 1, 777); 75 check_create("plain/nested", 0, 183); 76 check_create("trailing\\", 1, 777); 77 #ifdef _WIN32 78 assert(GetFileAttributesA("plain/nested") & FILE_ATTRIBUTE_DIRECTORY); 79 assert(GetFileAttributesA("missing") == INVALID_FILE_ATTRIBUTES); 80 assert(DeleteFileA("file.txt")); 81 assert(RemoveDirectoryA("plain/nested") && RemoveDirectoryA("plain")); 82 assert(RemoveDirectoryA("trailing")); 83 assert(SetCurrentDirectoryA(temporary) && RemoveDirectoryA(directory)); 84 #else 85 struct stat info; 86 assert(!stat("plain/nested", &info) && S_ISDIR(info.st_mode)); 87 assert(access("missing", F_OK) != 0); 88 assert(!unlink("file.txt")); 89 assert(!rmdir("plain/nested") && !rmdir("plain") && !rmdir("trailing")); 90 assert(!chdir("/tmp") && !rmdir(directory)); 91 #endif 92 puts("CreateDirectory: 10 cases, real directories, separators, existing paths, missing parents and ABI passed"); 93 return 0; 94 }