| 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: test4.c |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the GetFullPathNameW API. |
| 10 | ** GetFullPathNameW will be passed a directory that begins with '..'. |
| 11 | ** Example: ..\test_directory\testing.tmp. |
| 12 | ** To add to this test, we will also call SetCurrentDirectory to |
| 13 | ** ensure this is handled properly. |
| 14 | ** The test will create a file with in the parent directory |
| 15 | ** to verify that the returned directory is valid. |
| 16 | ** |
| 17 | ** Depends: SetCurrentDirectory, |
| 18 | ** CreateDirectory, |
| 19 | ** strcat, |
| 20 | ** memset, |
| 21 | ** CreateFile, |
| 22 | ** CloseHandle, |
| 23 | ** strcmp, |
| 24 | ** DeleteFileW, |
| 25 | ** RemoveDirectory. |
| 26 | ** |
| 27 | |
| 28 | ** |
| 29 | **===================================================================*/ |
| 30 | #define UNICODE |
| 31 | #include <palsuite.h> |
| 32 | |
| 33 | #ifdef WIN32 |
| 34 | const WCHAR szSeperator[] = {'\\','\\','\0'}; |
| 35 | #else |
| 36 | const WCHAR szSeperator[] = {'/','/','\0'}; |
| 37 | #endif |
| 38 | |
| 39 | const WCHAR szDotDot[] = {'.','.','\0'}; |
| 40 | const WCHAR szFileName[] = {'t','e','s','t','i','n','g','.','t','m','p','\0'}; |
| 41 | |
| 42 | int __cdecl main(int argc, char *argv[]) |
| 43 | { |
| 44 | DWORD dwRc = 0; |
| 45 | |
| 46 | WCHAR szReturnedPath[_MAX_DIR+1]; |
| 47 | WCHAR szFullFileName[_MAX_DIR+1]; |
| 48 | WCHAR szDirectory[256]; |
| 49 | WCHAR szCreatedDir[] = {'t','e','s','t','_','d','i','r','\0'}; |
| 50 | |
| 51 | LPWSTR pPathPtr; |
| 52 | HANDLE hFile = NULL; |
| 53 | BOOL bRetVal = FAIL; |
| 54 | |
| 55 | /* Initialize the PAL. |
| 56 | */ |
| 57 | if (0 != PAL_Initialize(argc,argv)) |
| 58 | { |
| 59 | return (FAIL); |
| 60 | } |
| 61 | |
| 62 | /* Initialize the buffer. |
| 63 | */ |
| 64 | memset(szDirectory, '\0', 256); |
| 65 | |
| 66 | /* Create the path to the next level of directory to create. |
| 67 | */ |
| 68 | wcscat(szDirectory, szDotDot); /* .. */ |
| 69 | wcscat(szDirectory, szSeperator); /* ../ */ |
| 70 | wcscat(szDirectory, szCreatedDir); /* ../test_directory */ |
| 71 | |
| 72 | /* Create a test directory. |
| 73 | */ |
| 74 | if (!CreateDirectoryW(szDirectory, NULL)) |
| 75 | { |
| 76 | Fail("ERROR:%u: Unable to create directories \"%S\".\n" , |
| 77 | GetLastError(), |
| 78 | szDirectory); |
| 79 | } |
| 80 | |
| 81 | /* Initialize the receiving char buffers. |
| 82 | */ |
| 83 | memset(szReturnedPath, 0, _MAX_DIR+1); |
| 84 | memset(szFullFileName, 0, _MAX_DIR+1); |
| 85 | |
| 86 | /* Create Full filename to pass, will include '..\' |
| 87 | * in the middle of the path. |
| 88 | */ |
| 89 | wcscat( szFullFileName, szDotDot ); /* .. */ |
| 90 | wcscat( szFullFileName, szSeperator ); /* ../ */ |
| 91 | wcscat( szFullFileName, szCreatedDir ); /* ../test_directory */ |
| 92 | wcscat( szFullFileName, szSeperator ); /* ../test_directory/ */ |
| 93 | wcscat( szFullFileName, szFileName ); /* ../test_directory/testing.tmp */ |
| 94 | |
| 95 | /* Get the full path to the filename. |
| 96 | */ |
| 97 | dwRc = GetFullPathNameW(szFullFileName, |
| 98 | _MAX_DIR, |
| 99 | szReturnedPath, |
| 100 | &pPathPtr); |
| 101 | if (dwRc == 0) |
| 102 | { |
| 103 | Trace("ERROR :%ld: GetFullPathName failed to " |
| 104 | "retrieve the path of \"%S\".\n" , |
| 105 | GetLastError(), |
| 106 | szFileName); |
| 107 | bRetVal = FAIL; |
| 108 | goto cleanUpOne; |
| 109 | } |
| 110 | |
| 111 | /* The returned value should be the parent directory with the |
| 112 | * file name appended. */ |
| 113 | hFile = CreateFileW(szReturnedPath, |
| 114 | GENERIC_READ, |
| 115 | FILE_SHARE_READ, |
| 116 | NULL, |
| 117 | CREATE_ALWAYS, |
| 118 | FILE_ATTRIBUTE_NORMAL, |
| 119 | NULL); |
| 120 | |
| 121 | if (hFile == INVALID_HANDLE_VALUE) |
| 122 | { |
| 123 | Trace("ERROR :%ld: CreateFileA failed to create \"%S\".\n" , |
| 124 | GetLastError(), |
| 125 | szReturnedPath); |
| 126 | bRetVal = FAIL; |
| 127 | goto cleanUpOne; |
| 128 | } |
| 129 | |
| 130 | /* Close the handle to the created file. |
| 131 | */ |
| 132 | if (CloseHandle(hFile) != TRUE) |
| 133 | { |
| 134 | Trace("ERROR :%ld: CloseHandle failed close hFile=0x%lx.\n" , |
| 135 | GetLastError()); |
| 136 | bRetVal = FAIL; |
| 137 | goto cleanUpTwo; |
| 138 | } |
| 139 | |
| 140 | /* Verify that the file was created, attempt to create |
| 141 | * the file again. */ |
| 142 | hFile = CreateFileW(szReturnedPath, |
| 143 | GENERIC_READ, |
| 144 | FILE_SHARE_READ, |
| 145 | NULL, |
| 146 | CREATE_NEW, |
| 147 | FILE_ATTRIBUTE_NORMAL, |
| 148 | NULL); |
| 149 | if ((hFile != INVALID_HANDLE_VALUE) && |
| 150 | (GetLastError() != ERROR_ALREADY_EXISTS)) |
| 151 | { |
| 152 | Trace("ERROR :%ld: CreateFileA succeeded to create file " |
| 153 | "\"%S\", that already existed.\n" , |
| 154 | GetLastError(), |
| 155 | szFullFileName); |
| 156 | bRetVal = FAIL; |
| 157 | goto cleanUpTwo; |
| 158 | } |
| 159 | |
| 160 | /* Verify that the returned filename is the same as the supplied. |
| 161 | */ |
| 162 | if (wcscmp(pPathPtr, szFileName) != 0) |
| 163 | { |
| 164 | Trace("ERROR : Returned filename \"%S\" is not equal to " |
| 165 | "supplied filename \"%S\".\n" , |
| 166 | pPathPtr, |
| 167 | szFileName); |
| 168 | bRetVal = FAIL; |
| 169 | goto cleanUpTwo; |
| 170 | } |
| 171 | |
| 172 | /* Successful test. |
| 173 | */ |
| 174 | bRetVal = PASS; |
| 175 | |
| 176 | cleanUpTwo: |
| 177 | |
| 178 | /* Delete the create file. |
| 179 | */ |
| 180 | if (DeleteFileW(szReturnedPath) != TRUE) |
| 181 | { |
| 182 | Fail("ERROR :%ld: DeleteFileA failed to delete \"%S\".\n" , |
| 183 | GetLastError(), |
| 184 | szFileName); |
| 185 | } |
| 186 | |
| 187 | cleanUpOne: |
| 188 | |
| 189 | /* Remove the empty directory. |
| 190 | */ |
| 191 | if (!RemoveDirectoryW(szDirectory)) |
| 192 | { |
| 193 | Fail("ERROR:%u: Unable to remove directory \"%s\".\n" , |
| 194 | GetLastError(), |
| 195 | szCreatedDir); |
| 196 | } |
| 197 | |
| 198 | /* Terminate the PAL.*/ |
| 199 | PAL_TerminateEx(bRetVal); |
| 200 | return bRetVal; |
| 201 | } |
| 202 | |