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: MoveFileExW.c
8**
9** Purpose: Tests the PAL implementation of the MoveFileExW function.
10**
11**
12**===================================================================*/
13
14#include <palsuite.h>
15
16
17LPWSTR lpSource[4];
18LPWSTR lpDestination[4];
19LPWSTR lpFiles[14];
20
21DWORD dwFlag[2] = {MOVEFILE_COPY_ALLOWED, MOVEFILE_REPLACE_EXISTING};
22
23
24
25int createExisting(void)
26{
27 HANDLE tempFile = NULL;
28 HANDLE tempFile2 = NULL;
29
30 /* create the src_existing file and dst_existing file */
31 tempFile = CreateFileW(lpSource[0], GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
32 FILE_ATTRIBUTE_NORMAL, 0);
33 tempFile2 = CreateFileW(lpDestination[0], GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
34 FILE_ATTRIBUTE_NORMAL, 0);
35 CloseHandle(tempFile2);
36 CloseHandle(tempFile);
37
38 if ((tempFile == NULL) || (tempFile2 == NULL))
39 {
40 Trace("ERROR: couldn't create %S or %S\n", lpSource[0],
41 lpDestination[0]);
42 return FAIL;
43 }
44
45 /* create the src_dir_existing and dst_dir_existing directory and files */
46 CreateDirectoryW(lpSource[2], NULL);
47
48 tempFile = CreateFileW(lpFiles[0], GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
49 FILE_ATTRIBUTE_NORMAL, 0);
50 tempFile2 = CreateFileW(lpFiles[1], GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
51 FILE_ATTRIBUTE_NORMAL, 0);
52 CloseHandle(tempFile2);
53 CloseHandle(tempFile);
54
55 if ((tempFile == NULL) || (tempFile2 == NULL))
56 {
57 Trace("ERROR: couldn't create src_dir_existing\\test01.tmp\n");
58 return FAIL;
59 }
60
61 CreateDirectoryW(lpDestination[2], NULL);
62 tempFile = CreateFileW(lpFiles[2], GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
63 FILE_ATTRIBUTE_NORMAL, 0);
64 tempFile2 = CreateFileW(lpFiles[3], GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
65 FILE_ATTRIBUTE_NORMAL, 0);
66 CloseHandle(tempFile2);
67 CloseHandle(tempFile);
68
69 if ((tempFile == NULL) || (tempFile2 == NULL))
70 {
71 Trace("ERROR: couldn't create dst_dir_existing\\test01.tmp\n");
72 return FAIL;
73 }
74 return PASS;
75}
76
77void removeDirectoryHelper(LPWSTR dir, int location)
78{
79 DWORD dwAtt = GetFileAttributesW(dir);
80// Trace(" Value of location[%d], and directorye [%S]\n", location, dir);
81
82 if (( dwAtt != INVALID_FILE_ATTRIBUTES ) && ( dwAtt & FILE_ATTRIBUTE_DIRECTORY) )
83 {
84 if(!RemoveDirectoryW(dir))
85 {
86 Fail("ERROR: Failed to remove Directory [%S], Error Code [%d], location [%d]\n", dir, GetLastError(), location);
87 }
88 }
89}
90
91void removeFileHelper(LPWSTR wfile, int location)
92{
93 FILE *fp;
94 char * pfile = convertC(wfile);
95
96// Trace(" Value of location[%d], and file [%s]\n", location, pfile);
97 fp = fopen( pfile, "r");
98
99 if (fp != NULL)
100 {
101 if(fclose(fp))
102 {
103 Fail("ERROR: Failed to close the file [%S], Error Code [%d], location [%d]\n", wfile, GetLastError(), location);
104 }
105
106 if(!DeleteFileW(wfile))
107 {
108 Fail("ERROR: Failed to delete file [%S], Error Code [%d], location [%d]\n", wfile, GetLastError(), location);
109 }
110 else
111 {
112 // Trace("Success: deleted file [%S], Error Code [%d], location [%d]\n", wfile, GetLastError(), location);
113 }
114 }
115
116 free(pfile);
117}
118
119void removeAll(void)
120{
121 DWORD dwAtt;
122 /* get rid of destination dirs and files */
123 removeFileHelper(lpSource[0], 11);
124// lpSource[0] = convert("src_existing.tmp");
125
126 removeFileHelper(lpSource[1], 12);
127 //lpSource[1] = convert("src_non-existant.tmp");
128
129 removeFileHelper(lpFiles[0], 13);
130// lpFiles[0] = convert("src_dir_existing\\test01.tmp");
131
132 removeFileHelper(lpFiles[1], 14);
133// lpFiles[1] = convert("src_dir_existing\\test02.tmp");
134
135 removeDirectoryHelper(lpSource[2], 101);
136// lpSource[2] = convert("src_dir_existing");
137
138 removeFileHelper(lpFiles[4], 15);
139// lpFiles[4] = convert("src_dir_non-existant\\test01.tmp");
140
141 removeFileHelper(lpFiles[5], 16);
142// lpFiles[5] = convert("src_dir_non-existant\\test02.tmp");
143
144 removeDirectoryHelper(lpSource[3], 102);
145// lpSource[3] = convert("src_dir_non-existant");
146
147 /* get rid of destination dirs and files */
148 dwAtt = GetFileAttributesW(lpDestination[0]);
149 if (( dwAtt != INVALID_FILE_ATTRIBUTES ) && ( dwAtt & FILE_ATTRIBUTE_DIRECTORY) )
150 {
151 removeFileHelper(lpFiles[6], 18);
152 // lpFiles[6] = convert("dst_existing.tmp\\test01.tmp");
153 removeFileHelper(lpFiles[7], 19);
154 // lpFiles[7] = convert("dst_existing.tmp\\test02.tmp");
155 removeDirectoryHelper(lpDestination[0], 103);
156 // lpDestination[0] = convert("dst_existing.tmp");
157
158 }
159 else
160 {
161 removeFileHelper(lpDestination[0], 17);
162 // lpDestination[0] = convert("dst_existing.tmp");
163 }
164
165 dwAtt = GetFileAttributesW(lpDestination[1]);
166 if (( dwAtt != INVALID_FILE_ATTRIBUTES ) && ( dwAtt & FILE_ATTRIBUTE_DIRECTORY) )
167 {
168 removeFileHelper(lpFiles[8], 21);
169 // lpFiles[8] = convert("dst_non-existant.tmp\\test01.tmp");
170 removeFileHelper(lpFiles[9], 22);
171 // lpFiles[9] = convert("dst_non-existant.tmp\\test02.tmp");
172 removeDirectoryHelper(lpDestination[1], 104);
173 // lpDestination[1] = convert("dst_non-existant.tmp");
174
175 }
176 else
177 {
178 removeFileHelper(lpDestination[1], 19);
179 //lpDestination[1] = convert("dst_non-existant.tmp");
180 }
181
182 dwAtt = GetFileAttributesW(lpDestination[2]);
183 if (( dwAtt != INVALID_FILE_ATTRIBUTES ) && ( dwAtt & FILE_ATTRIBUTE_DIRECTORY) )
184 {
185 removeFileHelper(lpFiles[10], 24);
186 // lpFiles[10] = convert("dst_dir_existing\\test01.tmp");
187 removeFileHelper(lpFiles[11], 25);
188 // lpFiles[11] = convert("dst_dir_existing\\test02.tmp");
189 removeDirectoryHelper(lpDestination[2], 105);
190 // lpDestination[2] = convert("dst_dir_existing");
191
192 }
193 else
194 {
195 removeFileHelper(lpDestination[2], 23);
196 // lpDestination[2] = convert("dst_dir_existing");
197
198 }
199
200 dwAtt = GetFileAttributesW(lpDestination[3]);
201 if (( dwAtt != INVALID_FILE_ATTRIBUTES ) && ( dwAtt & FILE_ATTRIBUTE_DIRECTORY) )
202 {
203 removeFileHelper(lpFiles[12], 26);
204 // lpFiles[12] = convert("dst_dir_non-existant\\test01.tmp");
205 removeFileHelper(lpFiles[13], 27);
206 // lpFiles[13] = convert("dst_dir_non-existant\\test02.tmp");
207 removeDirectoryHelper(lpDestination[3], 106);
208 // lpDestination[3] = convert("dst_dir_non-existant");
209
210 }
211 else
212 {
213 removeFileHelper(lpDestination[3], 107);
214 // lpDestination[3] = convert("dst_dir_non-existant");
215
216 }
217
218}
219
220int __cdecl main(int argc, char *argv[])
221{
222 BOOL bRc = TRUE;
223 char results[40];
224 FILE* resultsFile = NULL;
225 int i, j, k, nCounter = 0;
226 int res = FAIL;
227 WCHAR tempSource[] = {'t','e','m','p','k','.','t','m','p','\0'};
228 WCHAR tempDest[] = {'t','e','m','p','2','.','t','m','p','\0'};
229 HANDLE hFile;
230 DWORD result;
231
232 if (0 != PAL_Initialize(argc,argv))
233 {
234 return FAIL;
235 }
236
237 lpSource[0] = convert("src_existing.tmp");
238 lpSource[1] = convert("src_non-existant.tmp");
239 lpSource[2] = convert("src_dir_existing");
240 lpSource[3] = convert("src_dir_non-existant");
241
242 lpDestination[0] = convert("dst_existing.tmp");
243 lpDestination[1] = convert("dst_non-existant.tmp");
244 lpDestination[2] = convert("dst_dir_existing");
245 lpDestination[3] = convert("dst_dir_non-existant");
246
247 lpFiles[0] = convert("src_dir_existing\\test01.tmp");
248 lpFiles[1] = convert("src_dir_existing\\test02.tmp");
249 lpFiles[2] = convert("dst_dir_existing\\test01.tmp");
250 lpFiles[3] = convert("dst_dir_existing\\test02.tmp");
251 lpFiles[4] = convert("src_dir_non-existant\\test01.tmp");
252 lpFiles[5] = convert("src_dir_non-existant\\test02.tmp");
253
254 lpFiles[6] = convert("dst_existing.tmp\\test01.tmp");
255 lpFiles[7] = convert("dst_existing.tmp\\test02.tmp");
256
257 lpFiles[8] = convert("dst_non-existant.tmp\\test01.tmp");
258 lpFiles[9] = convert("dst_non-existant.tmp\\test02.tmp");
259
260 lpFiles[10] = convert("dst_dir_existing\\test01.tmp");
261 lpFiles[11] = convert("dst_dir_existing\\test02.tmp");
262
263 lpFiles[12] = convert("dst_dir_non-existant\\test01.tmp");
264 lpFiles[13] = convert("dst_dir_non-existant\\test02.tmp");
265
266 /* read in the expected results to compare with actual results */
267 memset (results, 0, 34);
268 resultsFile = fopen("expectedresults.txt", "r");
269 if (resultsFile == NULL)
270 {
271 Trace("MoveFileExW ERROR: Unable to open \"expectedresults.txt\"\n");
272 goto EXIT;
273 }
274
275 fgets(results, 34, resultsFile);
276 fclose(resultsFile);
277
278// Trace("Value of results[%]=%s\n", i, results);
279 for( i = 0; i < 32; i++)
280 {
281 Trace("Value of results[%d]=%c\n", i, results[i]);
282 }
283 nCounter = 0;
284
285
286 /* clean the slate */
287 removeAll();
288 if (createExisting() != PASS)
289 {
290 goto EXIT;
291 }
292
293 /* lpSource loop */
294 for (i = 0; i < 4; i++)
295 {
296 /* lpDestination loop */
297 for (j = 0; j < 4; j++)
298 {
299 /* dwFlag loop */
300 for (k = 0; k < 2; k++)
301 {
302
303 //if(nCounter == 22)
304 //{
305 //exit(1);
306 //}
307 /* move the file to the new location */
308 bRc = MoveFileExW(lpSource[i], lpDestination[j], dwFlag[k]);
309
310 if (!(
311 ((bRc == TRUE) && (results[nCounter] == '1'))
312 ||
313 ((bRc == FALSE ) && (results[nCounter] == '0')) )
314 )
315 {
316 Trace("MoveFileExW(%S, %S, %s): Values of i[%d], j[%d], k [%d] and results[%d]=%c LastError[%d]Flag[%d]FAILED\n",
317 lpSource[i], lpDestination[j],
318 k == 1 ?
319 "MOVEFILE_REPLACE_EXISTING":"MOVEFILE_COPY_ALLOWED", i, j, k, nCounter, results[nCounter], GetLastError(), bRc);
320 goto EXIT;
321 }
322
323 //Trace("MoveFileExW(%S, %S, %s): Values of i[%d], j[%d], k [%d] and results[%d]=%c \n",
324 // lpSource[i], lpDestination[j],
325 // k == 1 ?
326 // "MOVEFILE_REPLACE_EXISTING":"MOVEFILE_COPY_ALLOWED", i, j, k, nCounter, results[nCounter]);
327
328
329 /* undo the last move */
330 removeAll();
331 if (createExisting() != PASS)
332 {
333 goto EXIT;
334 }
335 //Trace("Counter [%d] over \n", nCounter);
336 nCounter++;
337 }
338 }
339 }
340
341 /* create the temp source file */
342 hFile = CreateFileW(tempSource, GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
343 FILE_ATTRIBUTE_NORMAL, 0);
344
345 if( hFile == INVALID_HANDLE_VALUE )
346 {
347 Trace("MoveFileExW: CreateFile failed to "
348 "create the file correctly.\n");
349 goto EXIT;
350 }
351
352 bRc = CloseHandle(hFile);
353 if(!bRc)
354 {
355 Trace("MoveFileExW: CloseHandle failed to close the "
356 "handle correctly. yo %u\n",GetLastError());
357 goto EXIT;
358 }
359
360 /* set the file attributes to be readonly */
361 bRc = SetFileAttributesW(tempSource, FILE_ATTRIBUTE_READONLY);
362 if(!bRc)
363 {
364 Trace("MoveFileExW: SetFileAttributes failed to set file "
365 "attributes correctly. ERROR:%u\n",GetLastError());
366 goto EXIT;
367 }
368
369 /* move the file to the new location */
370 bRc = MoveFileExW(tempSource, tempDest, MOVEFILE_COPY_ALLOWED );
371 if(!bRc)
372 {
373 Trace("MoveFileExW(%S, %S, %s): GetFileAttributes "
374 "failed to get the file's attributes.\n",
375 tempSource, tempDest, "MOVEFILE_COPY_ALLOWED");
376 goto EXIT;
377 }
378
379 /* check that the newly moved file has the same file attributes
380 as the original */
381 result = GetFileAttributesW(tempDest);
382 if(result == 0)
383 {
384 Trace("MoveFileExW: GetFileAttributes failed to get "
385 "the file's attributes.\n");
386 goto EXIT;
387 }
388
389 if((result & FILE_ATTRIBUTE_READONLY) != FILE_ATTRIBUTE_READONLY)
390 {
391 Trace("MoveFileExW: GetFileAttributes failed to get "
392 "the correct file attributes.\n");
393 goto EXIT;
394 }
395
396 /* set the file attributes back to normal, to be deleted */
397 bRc = SetFileAttributesW(tempDest, FILE_ATTRIBUTE_NORMAL);
398 if(!bRc)
399 {
400 Trace("MoveFileExW: SetFileAttributes "
401 "failed to set file attributes correctly.\n");
402 goto EXIT;
403 }
404
405 /* delete the newly moved file */
406 bRc = DeleteFileW(tempDest);
407 if(!bRc)
408 {
409 Trace("MoveFileExW: DeleteFileW failed to delete the"
410 "file correctly.\n");
411 goto EXIT;
412 }
413
414 res = PASS;
415
416EXIT:
417 removeAll();
418 for (i=0; i<4; i++)
419 {
420 free(lpSource[i]);
421 free(lpDestination[i]);
422 }
423 for (i=0; i<14; i++)
424 {
425 free(lpFiles[i]);
426 }
427
428 PAL_TerminateEx(res);
429 return res;
430}
431
432