| 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: GetTempFileNameW.c (test 3) |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the GetTempFileNameW function. |
| 10 | ** Checks the file attributes and ensures that getting a file name, |
| 11 | ** deleting the file and getting another doesn't produce the same |
| 12 | ** as the just deleted file. Also checks the file size is 0. |
| 13 | ** |
| 14 | ** Depends on: |
| 15 | ** GetFileAttributesW |
| 16 | ** DeleteFileW |
| 17 | ** CreateFileW |
| 18 | ** GetFileSize |
| 19 | ** CloseHandle |
| 20 | ** |
| 21 | ** |
| 22 | **===================================================================*/ |
| 23 | |
| 24 | #include <palsuite.h> |
| 25 | |
| 26 | |
| 27 | |
| 28 | int __cdecl main(int argc, char *argv[]) |
| 29 | { |
| 30 | const UINT uUnique = 0; |
| 31 | UINT uiError; |
| 32 | WCHAR szwReturnedName[MAX_LONGPATH]; |
| 33 | WCHAR szwReturnedName_02[MAX_LONGPATH]; |
| 34 | DWORD dwFileSize = 0; |
| 35 | HANDLE hFile; |
| 36 | const WCHAR szwDot[] = {'.','\0'}; |
| 37 | const WCHAR szwPre[] = {'c','\0'}; |
| 38 | |
| 39 | if (0 != PAL_Initialize(argc, argv)) |
| 40 | { |
| 41 | return FAIL; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /* valid path with null prefix */ |
| 46 | uiError = GetTempFileNameW(szwDot, szwPre, uUnique, szwReturnedName); |
| 47 | if (uiError == 0) |
| 48 | { |
| 49 | Fail("GetTempFileNameW: ERROR -> Call failed with a valid path " |
| 50 | "with the error code: %u.\n" , |
| 51 | GetLastError()); |
| 52 | } |
| 53 | |
| 54 | /* verify temp file was created */ |
| 55 | if (GetFileAttributesW(szwReturnedName) == -1) |
| 56 | { |
| 57 | Fail("GetTempFileNameW: ERROR -> GetFileAttributes failed on the " |
| 58 | "returned temp file \"%S\" with error code: %u.\n" , |
| 59 | szwReturnedName, |
| 60 | GetLastError()); |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | ** verify that the file size is 0 bytes |
| 65 | */ |
| 66 | |
| 67 | hFile = CreateFileW(szwReturnedName, |
| 68 | GENERIC_READ, |
| 69 | FILE_SHARE_READ, |
| 70 | NULL, |
| 71 | OPEN_EXISTING, |
| 72 | FILE_ATTRIBUTE_NORMAL, |
| 73 | NULL); |
| 74 | if (hFile == INVALID_HANDLE_VALUE) |
| 75 | { |
| 76 | Trace("GetTempFileNameW: ERROR -> CreateFileW failed to open" |
| 77 | " the created temp file with error code: %u.\n" , |
| 78 | GetLastError()); |
| 79 | if (!DeleteFileW(szwReturnedName)) |
| 80 | { |
| 81 | Trace("GetTempFileNameW: ERROR -> DeleteFileW failed to delete" |
| 82 | " the created temp file with error code: %u.\n" , |
| 83 | GetLastError()); |
| 84 | } |
| 85 | Fail("" ); |
| 86 | } |
| 87 | |
| 88 | if ((dwFileSize = GetFileSize(hFile, NULL)) != (DWORD)0) |
| 89 | { |
| 90 | Trace("GetTempFileNameW: ERROR -> GetFileSize returned %u whereas" |
| 91 | "it should have returned 0.\n" , |
| 92 | dwFileSize); |
| 93 | if (!CloseHandle(hFile)) |
| 94 | { |
| 95 | Trace("GetTempFileNameW: ERROR -> CloseHandle was unable to close the " |
| 96 | "opened file. GetLastError returned %u.\n" , |
| 97 | GetLastError()); |
| 98 | } |
| 99 | if (!DeleteFileW(szwReturnedName)) |
| 100 | { |
| 101 | Trace("GetTempFileNameW: ERROR -> DeleteFileW failed to delete" |
| 102 | " the created temp file with error code: %u.\n" , |
| 103 | GetLastError()); |
| 104 | } |
| 105 | Fail("" ); |
| 106 | } |
| 107 | |
| 108 | if (!CloseHandle(hFile)) |
| 109 | { |
| 110 | Fail("GetTempFileNameW: ERROR -> CloseHandle was unable to close the " |
| 111 | "opened file. GetLastError returned %u.\n" , |
| 112 | GetLastError()); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /* delete the file to see if we get the same name next time around */ |
| 117 | if (DeleteFileW(szwReturnedName) != TRUE) |
| 118 | { |
| 119 | Fail("GetTempFileNameW: ERROR -> DeleteFileW failed to delete" |
| 120 | " the created temp file with error code: %u.\n" , |
| 121 | GetLastError()); |
| 122 | } |
| 123 | |
| 124 | /* get another and make sure it's not the same as the last */ |
| 125 | uiError = GetTempFileNameW(szwDot, szwPre, uUnique, szwReturnedName_02); |
| 126 | if (uiError == 0) |
| 127 | { |
| 128 | Fail("GetTempFileNameW: ERROR -> Call failed with a valid path " |
| 129 | "with the error code: %u.\n" , |
| 130 | GetLastError()); |
| 131 | } |
| 132 | |
| 133 | /* did we get different names? */ |
| 134 | if (wcsncmp(szwReturnedName, szwReturnedName_02, wcslen(szwReturnedName)) == 0) |
| 135 | { |
| 136 | Fail("GetTempFileNameW: ERROR -> The first call returned \"%S\". " |
| 137 | "The second call returned \"%S\" and the two should not be" |
| 138 | " the same.\n" , |
| 139 | szwReturnedName, |
| 140 | szwReturnedName_02); |
| 141 | if (!DeleteFileW(szwReturnedName_02)) |
| 142 | { |
| 143 | Trace("GetTempFileNameW: ERROR -> DeleteFileW failed to delete" |
| 144 | " the created temp file with error code: %u.\n" , |
| 145 | GetLastError()); |
| 146 | } |
| 147 | Fail("" ); |
| 148 | } |
| 149 | |
| 150 | /* clean up */ |
| 151 | if (!DeleteFileW(szwReturnedName_02)) |
| 152 | { |
| 153 | Fail("GetTempFileNameW: ERROR -> DeleteFileW failed to delete" |
| 154 | " the created temp file with error code: %u.\n" , |
| 155 | GetLastError()); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | PAL_Terminate(); |
| 160 | return PASS; |
| 161 | } |
| 162 | |