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: GetLongPathNameW.c Win32 version(test 1) |
8 | ** |
9 | ** Purpose: Tests the PAL implementation of the GetLongPathNameW function. |
10 | ** as expected under Win32 |
11 | ** |
12 | ** Depends on: |
13 | ** CreateDirectoryA |
14 | ** RemoveDirectoryW |
15 | ** |
16 | ** |
17 | **===================================================================*/ |
18 | /* |
19 | tests: |
20 | - test invalid path names |
21 | - test an already short path name |
22 | - test a long path name |
23 | - test with buffer size too small |
24 | */ |
25 | |
26 | #include <palsuite.h> |
27 | |
28 | |
29 | int __cdecl main(int argc, char *argv[]) |
30 | { |
31 | /* Since GetLongPathNameW operates differently under FreeBSD and Win32 this test |
32 | is for Win32 only. It runs the same tests as the FreeBSD version but checks for |
33 | different results |
34 | */ |
35 | |
36 | #if WIN32 |
37 | DWORD dwRc = 0; |
38 | WCHAR szwReturnedPath[MAX_LONGPATH]; |
39 | WCHAR szwSmallBuff[3]; |
40 | const char szShortPathName[] = {"testing" }; |
41 | const char szLongPathName[] = {"This_is_a_long_directory_name" }; |
42 | const char szShortenedPathName[] = {"THIS_I~1" }; |
43 | WCHAR* wLongPathPtr = NULL; |
44 | WCHAR* wShortPathPtr = NULL; |
45 | |
46 | |
47 | |
48 | if (0 != PAL_Initialize(argc,argv)) |
49 | { |
50 | return FAIL; |
51 | } |
52 | |
53 | memset(szwReturnedPath, 0, MAX_LONGPATH*sizeof(WCHAR)); |
54 | memset(szwSmallBuff, 0, 3*sizeof(WCHAR)); |
55 | wLongPathPtr = convert((char*)szLongPathName); |
56 | wShortPathPtr = convert((char*)szShortenedPathName); |
57 | |
58 | /* do some clean up just to be safe */ |
59 | RemoveDirectoryW(wLongPathPtr); |
60 | RemoveDirectoryW(wShortPathPtr); |
61 | |
62 | |
63 | /* attempt call on an invalid short path name */ |
64 | dwRc = GetLongPathNameW(wShortPathPtr, szwReturnedPath, MAX_LONGPATH); |
65 | if (dwRc != 0) |
66 | { |
67 | Trace("GetLongPathNameW: ERROR -> Call made with an invalid short " |
68 | "path \"%S\" returned \"%S\"\n" , |
69 | wShortPathPtr, |
70 | szwReturnedPath); |
71 | free (wLongPathPtr); |
72 | free (wShortPathPtr); |
73 | Fail("" ); |
74 | } |
75 | |
76 | |
77 | /* attempt call on an invalid long path name */ |
78 | dwRc = GetLongPathNameW(wLongPathPtr, szwReturnedPath, MAX_LONGPATH); |
79 | if (dwRc != 0) |
80 | { |
81 | Trace("GetLongPathNameW: ERROR -> Call made with an invalid long " |
82 | "path \"%S\" returned \"%S\"\n" , |
83 | wLongPathPtr, |
84 | szwReturnedPath); |
85 | free (wLongPathPtr); |
86 | free (wShortPathPtr); |
87 | Fail("" ); |
88 | } |
89 | |
90 | |
91 | /* create a long directory name */ |
92 | if (TRUE != CreateDirectoryW(wLongPathPtr, NULL)) |
93 | { |
94 | free(wLongPathPtr); |
95 | free(wShortPathPtr); |
96 | Fail("GetLongPathNameW: ERROR -> CreateDirectoryW failed with an error" |
97 | " code of %ld when asked to create a directory called \"%s\".\n" , |
98 | GetLastError(), |
99 | szLongPathName); |
100 | } |
101 | |
102 | |
103 | /* get the long path name */ |
104 | memset(szwReturnedPath, 0, MAX_LONGPATH*sizeof(WCHAR)); |
105 | dwRc = GetLongPathNameW(wShortPathPtr, szwReturnedPath, MAX_LONGPATH); |
106 | if (dwRc == 0) |
107 | { |
108 | Trace("GetLongPathNameW: ERROR -> failed with an error" |
109 | " code of %ld when asked for the long version of \"%s\".\n" , |
110 | GetLastError(), |
111 | szShortenedPathName); |
112 | if (RemoveDirectoryW(wLongPathPtr) != TRUE) |
113 | { |
114 | Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to " |
115 | " remove the directory \"%S\" with an error code of %ld.\n" , |
116 | wLongPathPtr, |
117 | GetLastError()); |
118 | } |
119 | free(wLongPathPtr); |
120 | free(wShortPathPtr); |
121 | Fail("" ); |
122 | } |
123 | |
124 | /* does the returned match the expected */ |
125 | if (wcsncmp(wLongPathPtr, szwReturnedPath, wcslen(wLongPathPtr)) || |
126 | (wcslen(wLongPathPtr) != wcslen(szwReturnedPath))) |
127 | { |
128 | if (RemoveDirectoryW(wLongPathPtr) != TRUE) |
129 | { |
130 | Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to " |
131 | " remove the directory \"%S\" with an error code of %ld.\n" , |
132 | wLongPathPtr, |
133 | GetLastError()); |
134 | } |
135 | free(wLongPathPtr); |
136 | free(wShortPathPtr); |
137 | Fail("GetLongPathNameW: ERROR -> The returned path, \"%S\" doesn't " |
138 | "match the expected return, \"%s\".\n" , |
139 | szwReturnedPath, |
140 | szLongPathName); |
141 | } |
142 | |
143 | /* does the length returned match the actual length */ |
144 | if (dwRc != wcslen(szwReturnedPath)) |
145 | { |
146 | if (RemoveDirectoryW(wLongPathPtr) != TRUE) |
147 | { |
148 | Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to " |
149 | " remove the directory \"%S\" with an error code of %ld.\n" , |
150 | wLongPathPtr, |
151 | GetLastError()); |
152 | } |
153 | free(wLongPathPtr); |
154 | free(wShortPathPtr); |
155 | Fail("GetLongPathNameW: ERROR -> The returned length, %ld, doesn't " |
156 | "match the string length, %ld.\n" , |
157 | dwRc, |
158 | wcslen(szwReturnedPath)); |
159 | } |
160 | |
161 | if (RemoveDirectoryW(wLongPathPtr) != TRUE) |
162 | { |
163 | Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to " |
164 | " remove the directory \"%S\" with an error code of %ld.\n" , |
165 | wLongPathPtr, |
166 | GetLastError()); |
167 | free(wShortPathPtr); |
168 | free(wLongPathPtr); |
169 | Fail("" ); |
170 | } |
171 | free(wShortPathPtr); |
172 | free(wLongPathPtr); |
173 | |
174 | |
175 | /* test an actual short name */ |
176 | /* create a long directory name */ |
177 | wShortPathPtr = convert((char*)szShortPathName); |
178 | RemoveDirectoryW(wShortPathPtr); |
179 | |
180 | if (TRUE != CreateDirectoryW(wShortPathPtr, NULL)) |
181 | { |
182 | Trace("GetLongPathNameW: ERROR -> CreateDirectoryW failed with an error" |
183 | " code of %ld when asked to create a directory called \"%s\".\n" , |
184 | GetLastError(), |
185 | szShortPathName); |
186 | free(wShortPathPtr); |
187 | Fail("" ); |
188 | } |
189 | |
190 | |
191 | /* get the long path name */ |
192 | memset(szwReturnedPath, 0, MAX_LONGPATH*sizeof(WCHAR)); |
193 | dwRc = GetLongPathNameW(wShortPathPtr, szwReturnedPath, MAX_LONGPATH); |
194 | if (dwRc == 0) |
195 | { |
196 | Trace("GetLongPathNameW: ERROR -> failed with an error" |
197 | " code of %ld when asked for the long version of \"%s\".\n" , |
198 | GetLastError(), |
199 | szShortenedPathName); |
200 | if (RemoveDirectoryW(wShortPathPtr) != TRUE) |
201 | { |
202 | Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to " |
203 | " remove the directory \"%S\" with an error code of %ld.\n" , |
204 | wShortPathPtr, |
205 | GetLastError()); |
206 | } |
207 | free(wShortPathPtr); |
208 | Fail("" ); |
209 | } |
210 | |
211 | /* does the returned match the expected */ |
212 | if (wcsncmp(wShortPathPtr, szwReturnedPath, wcslen(wShortPathPtr)) || |
213 | (wcslen(wShortPathPtr) != wcslen(szwReturnedPath))) |
214 | { |
215 | if (RemoveDirectoryW(wShortPathPtr) != TRUE) |
216 | { |
217 | Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to " |
218 | " remove the directory \"%S\" with an error code of %ld.\n" , |
219 | wShortPathPtr, |
220 | GetLastError()); |
221 | } |
222 | free(wShortPathPtr); |
223 | Fail("GetLongPathNameW: ERROR -> The returned path, \"%S\" doesn't " |
224 | "match the expected return, \"%s\".\n" , |
225 | szwReturnedPath, |
226 | szShortPathName); |
227 | } |
228 | |
229 | /* does the length returned match the actual length */ |
230 | if (dwRc != wcslen(szwReturnedPath)) |
231 | { |
232 | if (RemoveDirectoryW(wShortPathPtr) != TRUE) |
233 | { |
234 | Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to " |
235 | " remove the directory \"%S\" with an error code of %ld.\n" , |
236 | wShortPathPtr, |
237 | GetLastError()); |
238 | } |
239 | free(wShortPathPtr); |
240 | Fail("GetLongPathNameW: ERROR -> The returned length, %ld, doesn't " |
241 | "match the string length, %ld.\n" , |
242 | dwRc, |
243 | wcslen(szwReturnedPath)); |
244 | } |
245 | |
246 | /* test using a too small return buffer */ |
247 | dwRc = GetLongPathNameW(wShortPathPtr, szwSmallBuff, 3); |
248 | if ((dwRc != (strlen(szShortPathName)+1)) || /* +1 for the required NULL */ |
249 | szwSmallBuff[0] != '\0') |
250 | { |
251 | if (RemoveDirectoryW(wShortPathPtr) != TRUE) |
252 | { |
253 | Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to " |
254 | " remove the directory \"%S\" with an error code of %ld.\n" , |
255 | wShortPathPtr, |
256 | GetLastError()); |
257 | } |
258 | free(wShortPathPtr); |
259 | Fail("GetLongPathNameW: ERROR -> using a return buffer that was too" |
260 | " small was not handled properly.\n" ); |
261 | } |
262 | |
263 | if (RemoveDirectoryW(wShortPathPtr) != TRUE) |
264 | { |
265 | Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to " |
266 | " remove the directory \"%S\" with an error code of %ld.\n" , |
267 | wShortPathPtr, |
268 | GetLastError()); |
269 | free(wShortPathPtr); |
270 | Fail("" ); |
271 | } |
272 | free(wShortPathPtr); |
273 | |
274 | PAL_Terminate(); |
275 | |
276 | #endif /* WIN32 */ |
277 | |
278 | return PASS; |
279 | } |
280 | |
281 | |