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