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