| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*===================================================================== |
| 6 | ** |
| 7 | ** Source: GetFullPathNameW.c (test 1) |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the GetFullPathNameW function. |
| 10 | ** |
| 11 | ** |
| 12 | **===================================================================*/ |
| 13 | |
| 14 | #include <palsuite.h> |
| 15 | |
| 16 | const char* szFileName = "testing.tmp" ; |
| 17 | |
| 18 | int __cdecl main(int argc, char *argv[]) |
| 19 | { |
| 20 | DWORD dwRc = 0; |
| 21 | WCHAR szwReturnedPath[_MAX_DIR+1]; |
| 22 | WCHAR szwShortBuff[2]; |
| 23 | LPWSTR pPathPtr; |
| 24 | HANDLE hFile = NULL; |
| 25 | WCHAR* szwFileName = NULL; |
| 26 | |
| 27 | |
| 28 | if (0 != PAL_Initialize(argc,argv)) |
| 29 | { |
| 30 | return FAIL; |
| 31 | } |
| 32 | |
| 33 | szwFileName = convert((char*)szFileName); |
| 34 | |
| 35 | /* perform a short buffer test */ |
| 36 | if (GetFullPathNameW(szwFileName, 2, szwShortBuff, &pPathPtr) <= 2) |
| 37 | { |
| 38 | free(szwFileName); |
| 39 | /* this test should have failed but didn't */ |
| 40 | Fail("GetFullPathNameW: ERROR -> The API was passed a buffer that was" |
| 41 | " too small for the path name and yet it apparently passed.\n" ); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | memset(szwReturnedPath, 0, _MAX_DIR+1); |
| 46 | dwRc = GetFullPathNameW(szwFileName, |
| 47 | _MAX_DIR, |
| 48 | szwReturnedPath, |
| 49 | &pPathPtr); |
| 50 | |
| 51 | if (dwRc == 0) |
| 52 | { |
| 53 | /* this test should have passed but didn't */ |
| 54 | free(szwFileName); |
| 55 | Fail("GetFullPathNameW: ERROR -> Function failed for the " |
| 56 | "file \"%s\" with error code: %ld.\n" , szFileName, GetLastError()); |
| 57 | } |
| 58 | /* |
| 59 | * the returned value should be the current directory with the |
| 60 | * file name appended |
| 61 | */ |
| 62 | hFile = CreateFileW(szwFileName, |
| 63 | GENERIC_READ, |
| 64 | FILE_SHARE_READ, |
| 65 | NULL, |
| 66 | CREATE_ALWAYS, |
| 67 | FILE_ATTRIBUTE_NORMAL, |
| 68 | NULL); |
| 69 | if (hFile == INVALID_HANDLE_VALUE) |
| 70 | { |
| 71 | free(szwFileName); |
| 72 | Fail("GetFullPathNameW: ERROR -> CreateFileW failed to create " |
| 73 | "file \"%s\" with error code: %ld.\n" , |
| 74 | szFileName, |
| 75 | GetLastError()); |
| 76 | } |
| 77 | if (CloseHandle(hFile) != TRUE) |
| 78 | { |
| 79 | free(szwFileName); |
| 80 | Trace("GetFullPathNameW: ERROR -> CloseHandle failed with error " |
| 81 | "code: %ld.\n" , GetLastError()); |
| 82 | if (DeleteFileA(szFileName) != TRUE) |
| 83 | { |
| 84 | Trace("GetFullPathNameW: ERROR -> DeleteFileW failed to " |
| 85 | "delete the test file with error code: %ld.\n" , |
| 86 | GetLastError()); |
| 87 | } |
| 88 | PAL_TerminateEx(FAIL); |
| 89 | return FAIL; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * now try to create the file based on the returned value with the |
| 94 | * CREATE_NEW option which should fail since the file should |
| 95 | * already exist |
| 96 | */ |
| 97 | hFile = CreateFileW(szwReturnedPath, |
| 98 | GENERIC_READ, |
| 99 | FILE_SHARE_READ, |
| 100 | NULL, |
| 101 | CREATE_NEW, |
| 102 | FILE_ATTRIBUTE_NORMAL, |
| 103 | NULL); |
| 104 | if (hFile != INVALID_HANDLE_VALUE) |
| 105 | { |
| 106 | Trace("GetFullPathNameW: ERROR -> CreateFileW was able to " |
| 107 | "CREATE_NEW the returned file \"%s\". The returned file " |
| 108 | "name is therefore apparently wrong.\n" , |
| 109 | szwReturnedPath); |
| 110 | if (CloseHandle(hFile) != TRUE) |
| 111 | { |
| 112 | Trace("GetFullPathNameW: ERROR -> CloseHandle failed with " |
| 113 | "error code: %ld.\n" , GetLastError()); |
| 114 | } |
| 115 | if ((DeleteFileW(szwReturnedPath) != TRUE) || |
| 116 | (DeleteFileW(szwFileName) != TRUE)) |
| 117 | { |
| 118 | Trace("GetFullPathNameW: ERROR -> DeleteFileW failed to " |
| 119 | "delete the test files with error code: %ld.\n" , |
| 120 | GetLastError()); |
| 121 | } |
| 122 | free(szwFileName); |
| 123 | PAL_TerminateEx(FAIL); |
| 124 | return FAIL; |
| 125 | } |
| 126 | |
| 127 | /* now make sure the pPathPtr is the same as the file name */ |
| 128 | if (wcsncmp(pPathPtr, szwFileName, wcslen(szwFileName)) != 0) |
| 129 | { |
| 130 | Trace("GetFullPathNameW: ERROR -> %s != %s\n" , |
| 131 | pPathPtr, szFileName); |
| 132 | if ((DeleteFileW(szwReturnedPath) != TRUE) || |
| 133 | (DeleteFileW(szwFileName) != TRUE)) |
| 134 | { |
| 135 | Trace("GetFullPathNameW: ERROR -> DeleteFileW failed to " |
| 136 | "delete the test files with error code: %ld.\n" , |
| 137 | GetLastError()); |
| 138 | } |
| 139 | free(szwFileName); |
| 140 | PAL_TerminateEx(FAIL); |
| 141 | return FAIL; |
| 142 | } |
| 143 | |
| 144 | /* clean up */ |
| 145 | free(szwFileName); |
| 146 | if (DeleteFileA(szFileName) != TRUE) |
| 147 | { |
| 148 | Fail("GetFullPathNameW: ERROR -> DeleteFileW failed to " |
| 149 | "delete \"%s\" with error code: %ld.\n" , |
| 150 | szFileName, |
| 151 | GetLastError()); |
| 152 | } |
| 153 | |
| 154 | PAL_Terminate(); |
| 155 | return PASS; |
| 156 | } |
| 157 | |
| 158 | |