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 1)
8**
9** Purpose: Tests the PAL implementation of the GetTempFileNameW function.
10**
11**
12**===================================================================*/
13
14#include <palsuite.h>
15
16
17
18int __cdecl main(int argc, char *argv[])
19{
20 UINT uiError = 0;
21 const UINT uUnique = 0;
22 WCHAR* wPrefix = NULL;
23 WCHAR* wPath = NULL;
24 WCHAR wReturnedName[256];
25 WCHAR wTempString[256];
26
27 if (0 != PAL_Initialize(argc,argv))
28 {
29 return FAIL;
30 }
31
32
33 // valid path with null ext
34 wPath = convert(".");
35 uiError = GetTempFileNameW(wPath, wPrefix, uUnique, wReturnedName);
36 free (wPath);
37 if (uiError == 0)
38 {
39 Fail("GetTempFileNameW: ERROR -> Call failed with a valid path "
40 "with the error code: %ld\n", GetLastError());
41 }
42 else
43 {
44 // verify temp file was created
45 if (GetFileAttributesW(wReturnedName) == -1)
46 {
47 Fail("GetTempFileNameW: ERROR -> GetFileAttributes failed on the "
48 "returned temp file with error code: %ld.\n", GetLastError());
49 }
50 if (DeleteFileW(wReturnedName) != TRUE)
51 {
52 Fail("GetTempFileNameW: ERROR -> DeleteFileW failed to delete"
53 "the created temp file with error code: %lld.\n", GetLastError());
54 }
55 }
56
57
58 // valid path with valid prefix
59 wPath = convert(".");
60 wPrefix = convert("cfr");
61 uiError = GetTempFileNameW(wPath, wPrefix, uUnique, wReturnedName);
62 free (wPath);
63 free (wPrefix);
64 if (uiError == 0)
65 {
66 Fail("GetTempFileNameW: ERROR -> Call failed with a valid path and "
67 "prefix with the error code: %ld\n", GetLastError());
68 }
69 else
70 {
71 // verify temp file was created
72 if (GetFileAttributesW(wReturnedName) == -1)
73 {
74 Fail("GetTempFileNameW: ERROR -> GetFileAttributes failed on the "
75 "returned temp file with error code: %ld.\n", GetLastError());
76 }
77 if (DeleteFileW(wReturnedName) != TRUE)
78 {
79 Fail("GetTempFileNameW: ERROR -> DeleteFileW failed to delete"
80 "the created temp file with error code: %lld.\n", GetLastError());
81 }
82 }
83
84 // valid path with long prefix
85 wPath = convert(".");
86 wPrefix = convert("cfrwxyz");
87 uiError = GetTempFileNameW(wPath, wPrefix, uUnique, wReturnedName);
88 if (uiError == 0)
89 {
90 free (wPath);
91 free (wPrefix);
92 Fail("GetTempFileNameW: ERROR -> Call failed with a valid path and "
93 "prefix with the error code: %ld\n", GetLastError());
94 }
95 else
96 {
97 // verify temp file was created
98 if (GetFileAttributesW(wReturnedName) == -1)
99 {
100 free (wPath);
101 free (wPrefix);
102 Fail("GetTempFileNameW: ERROR -> GetFileAttributes failed on the "
103 "returned temp file with error code: %ld.\n", GetLastError());
104 }
105
106 // now verify that it only used the first 3 characters of the prefix
107 swprintf_s(wTempString, _countof(wTempString), convert("%s\\%s"), wPath, wPrefix);
108 if (memcmp(wTempString, wReturnedName, wcslen(wTempString)*sizeof(WCHAR)) == 0)
109 {
110 free (wPath);
111 free (wPrefix);
112 Fail("GetTempFileNameW: ERROR -> It appears that an improper prefix "
113 "was used.\n");
114 }
115
116 if (DeleteFileW(wReturnedName) != TRUE)
117 {
118 free (wPath);
119 free (wPrefix);
120 Fail("GetTempFileNameW: ERROR -> DeleteFileW failed to delete"
121 "the created temp file with error code: %lld.\n", GetLastError());
122 }
123 }
124
125 free (wPath);
126 free (wPrefix);
127 PAL_Terminate();
128 return PASS;
129}
130