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