dinput: Silence incorrect warning and move it to a valid place.
[wine] / dlls / shell32 / tests / shlfileop.c
1 /*
2  * Unit test of the SHFileOperation function.
3  *
4  * Copyright 2002 Andriy Palamarchuk
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define WINE_NOWINSOCK
25 #include <windows.h>
26 #include "shellapi.h"
27 #include "shlobj.h"
28
29 #include "wine/test.h"
30
31 #ifndef FOF_NORECURSION
32 #define FOF_NORECURSION 0x1000
33 #endif
34
35 static CHAR CURR_DIR[MAX_PATH];
36 static const WCHAR UNICODE_PATH[] = {'c',':','\\',0x00c4,'\0','\0'};
37     /* "c:\Ä", or "c:\A" with diaeresis */
38     /* Double-null termination needed for pFrom field of SHFILEOPSTRUCT */
39
40 static HMODULE hshell32;
41 static int (WINAPI *pSHCreateDirectoryExA)(HWND, LPCSTR, LPSECURITY_ATTRIBUTES);
42 static int (WINAPI *pSHCreateDirectoryExW)(HWND, LPCWSTR, LPSECURITY_ATTRIBUTES);
43 static int (WINAPI *pSHFileOperationW)(LPSHFILEOPSTRUCTW);
44 static int (WINAPI *pSHPathPrepareForWriteA)(HWND, IUnknown*, LPCSTR, DWORD);
45 static int (WINAPI *pSHPathPrepareForWriteW)(HWND, IUnknown*, LPCWSTR, DWORD);
46
47 static void InitFunctionPointers(void)
48 {
49     hshell32 = GetModuleHandleA("shell32.dll");
50     pSHCreateDirectoryExA = (void*)GetProcAddress(hshell32, "SHCreateDirectoryExA");
51     pSHCreateDirectoryExW = (void*)GetProcAddress(hshell32, "SHCreateDirectoryExW");
52     pSHFileOperationW = (void*)GetProcAddress(hshell32, "SHFileOperationW");
53     pSHPathPrepareForWriteA = (void*)GetProcAddress(hshell32, "SHPathPrepareForWriteA");
54     pSHPathPrepareForWriteW = (void*)GetProcAddress(hshell32, "SHPathPrepareForWriteW");
55 }
56
57 /* creates a file with the specified name for tests */
58 static void createTestFile(const CHAR *name)
59 {
60     HANDLE file;
61     DWORD written;
62
63     file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
64     ok(file != INVALID_HANDLE_VALUE, "Failure to open file %s\n", name);
65     WriteFile(file, name, strlen(name), &written, NULL);
66     WriteFile(file, "\n", strlen("\n"), &written, NULL);
67     CloseHandle(file);
68 }
69
70 static void createTestFileW(const WCHAR *name)
71 {
72     HANDLE file;
73
74     file = CreateFileW(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
75     ok(file != INVALID_HANDLE_VALUE, "Failure to open file\n");
76     CloseHandle(file);
77 }
78
79 static BOOL file_exists(const CHAR *name)
80 {
81     return GetFileAttributesA(name) != INVALID_FILE_ATTRIBUTES;
82 }
83
84 static BOOL file_existsW(LPCWSTR name)
85 {
86   return GetFileAttributesW(name) != INVALID_FILE_ATTRIBUTES;
87 }
88
89 static BOOL file_has_content(const CHAR *name, const CHAR *content)
90 {
91     CHAR buf[MAX_PATH];
92     HANDLE file;
93     DWORD read;
94
95     file = CreateFileA(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
96     if (file == INVALID_HANDLE_VALUE)
97         return FALSE;
98     ReadFile(file, buf, MAX_PATH - 1, &read, NULL);
99     buf[read] = 0;
100     CloseHandle(file);
101     return strcmp(buf, content)==0;
102 }
103
104 /* initializes the tests */
105 static void init_shfo_tests(void)
106 {
107     int len;
108
109     GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
110     len = lstrlenA(CURR_DIR);
111
112     if(len && (CURR_DIR[len-1] == '\\'))
113         CURR_DIR[len-1] = 0;
114
115     createTestFile("test1.txt");
116     createTestFile("test2.txt");
117     createTestFile("test3.txt");
118     createTestFile("test_5.txt");
119     CreateDirectoryA("test4.txt", NULL);
120     CreateDirectoryA("testdir2", NULL);
121     CreateDirectoryA("testdir2\\nested", NULL);
122     createTestFile("testdir2\\one.txt");
123     createTestFile("testdir2\\nested\\two.txt");
124 }
125
126 /* cleans after tests */
127 static void clean_after_shfo_tests(void)
128 {
129     DeleteFileA("test1.txt");
130     DeleteFileA("test2.txt");
131     DeleteFileA("test3.txt");
132     DeleteFileA("test_5.txt");
133     DeleteFileA("one.txt");
134     DeleteFileA("test4.txt\\test1.txt");
135     DeleteFileA("test4.txt\\test2.txt");
136     DeleteFileA("test4.txt\\test3.txt");
137     RemoveDirectoryA("test4.txt");
138     DeleteFileA("testdir2\\one.txt");
139     DeleteFileA("testdir2\\test1.txt");
140     DeleteFileA("testdir2\\test2.txt");
141     DeleteFileA("testdir2\\test3.txt");
142     DeleteFileA("testdir2\\test4.txt\\test1.txt");
143     DeleteFileA("testdir2\\nested\\two.txt");
144     RemoveDirectoryA("testdir2\\test4.txt");
145     RemoveDirectoryA("testdir2\\nested");
146     RemoveDirectoryA("testdir2");
147     RemoveDirectoryA("c:\\testdir3");
148     DeleteFileA("nonexistent\\notreal\\test2.txt");
149     RemoveDirectoryA("nonexistent\\notreal");
150     RemoveDirectoryA("nonexistent");
151 }
152
153
154 static void test_get_file_info(void)
155 {
156     DWORD rc, rc2;
157     SHFILEINFO shfi, shfi2;
158     char notepad[MAX_PATH];
159
160     /* Test some flag combinations that MSDN claims are not allowed,
161      * but which work anyway
162      */
163     shfi.dwAttributes=0xdeadbeef;
164     rc=SHGetFileInfoA("c:\\nonexistent", FILE_ATTRIBUTE_DIRECTORY,
165                       &shfi, sizeof(shfi),
166                       SHGFI_ATTRIBUTES | SHGFI_USEFILEATTRIBUTES);
167     todo_wine ok(rc, "SHGetFileInfoA(c:\\nonexistent | SHGFI_ATTRIBUTES) failed\n");
168     if (rc)
169         ok(shfi.dwAttributes != 0xdeadbeef, "dwFileAttributes is not set\n");
170
171     rc=SHGetFileInfoA("c:\\nonexistent", FILE_ATTRIBUTE_DIRECTORY,
172                       &shfi, sizeof(shfi),
173                       SHGFI_EXETYPE | SHGFI_USEFILEATTRIBUTES);
174     todo_wine ok(rc == 1, "SHGetFileInfoA(c:\\nonexistent | SHGFI_EXETYPE) returned %d\n", rc);
175
176     /* Test SHGFI_USEFILEATTRIBUTES support */
177     strcpy(shfi.szDisplayName, "dummy");
178     shfi.iIcon=0xdeadbeef;
179     rc=SHGetFileInfoA("c:\\nonexistent", FILE_ATTRIBUTE_DIRECTORY,
180                       &shfi, sizeof(shfi),
181                       SHGFI_ICONLOCATION | SHGFI_USEFILEATTRIBUTES);
182     ok(rc, "SHGetFileInfoA(c:\\nonexistent) failed\n");
183     if (rc)
184     {
185         ok(strcpy(shfi.szDisplayName, "dummy") != 0, "SHGetFileInfoA(c:\\nonexistent) displayname is not set\n");
186         ok(shfi.iIcon != 0xdeadbeef, "SHGetFileInfoA(c:\\nonexistent) iIcon is not set\n");
187     }
188
189     /* Wine does not have a default icon for text files, and Windows 98 fails
190      * if we give it an empty executable. So use notepad.exe as the test
191      */
192     if (SearchPath(NULL, "notepad.exe", NULL, sizeof(notepad), notepad, NULL))
193     {
194         strcpy(shfi.szDisplayName, "dummy");
195         shfi.iIcon=0xdeadbeef;
196         rc=SHGetFileInfoA(notepad, GetFileAttributes(notepad),
197                           &shfi, sizeof(shfi),
198                           SHGFI_ICONLOCATION | SHGFI_USEFILEATTRIBUTES);
199         ok(rc, "SHGetFileInfoA(%s, SHGFI_USEFILEATTRIBUTES) failed\n", notepad);
200         strcpy(shfi2.szDisplayName, "dummy");
201         shfi2.iIcon=0xdeadbeef;
202         rc2=SHGetFileInfoA(notepad, 0,
203                            &shfi2, sizeof(shfi2),
204                            SHGFI_ICONLOCATION);
205         ok(rc2, "SHGetFileInfoA(%s) failed\n", notepad);
206         if (rc && rc2)
207         {
208             ok(lstrcmpi(shfi2.szDisplayName, shfi.szDisplayName) == 0, "wrong display name %s != %s\n", shfi.szDisplayName, shfi2.szDisplayName);
209             ok(shfi2.iIcon == shfi.iIcon, "wrong icon index %d != %d\n", shfi.iIcon, shfi2.iIcon);
210         }
211     }
212
213     /* with a directory now */
214     strcpy(shfi.szDisplayName, "dummy");
215     shfi.iIcon=0xdeadbeef;
216     rc=SHGetFileInfoA("test4.txt", GetFileAttributes("test4.txt"),
217                       &shfi, sizeof(shfi),
218                       SHGFI_ICONLOCATION | SHGFI_USEFILEATTRIBUTES);
219     ok(rc, "SHGetFileInfoA(test4.txt/, SHGFI_USEFILEATTRIBUTES) failed\n");
220     strcpy(shfi2.szDisplayName, "dummy");
221     shfi2.iIcon=0xdeadbeef;
222     rc2=SHGetFileInfoA("test4.txt", 0,
223                       &shfi2, sizeof(shfi2),
224                       SHGFI_ICONLOCATION);
225     ok(rc2, "SHGetFileInfoA(test4.txt/) failed\n");
226     if (rc && rc2)
227     {
228         ok(lstrcmpi(shfi2.szDisplayName, shfi.szDisplayName) == 0, "wrong display name %s != %s\n", shfi.szDisplayName, shfi2.szDisplayName);
229         ok(shfi2.iIcon == shfi.iIcon, "wrong icon index %d != %d\n", shfi.iIcon, shfi2.iIcon);
230     }
231 }
232
233
234 /*
235  puts into the specified buffer file names with current directory.
236  files - string with file names, separated by null characters. Ends on a double
237  null characters
238 */
239 static void set_curr_dir_path(CHAR *buf, const CHAR* files)
240 {
241     buf[0] = 0;
242     while (files[0])
243     {
244         strcpy(buf, CURR_DIR);
245         buf += strlen(buf);
246         buf[0] = '\\';
247         buf++;
248         strcpy(buf, files);
249         buf += strlen(buf) + 1;
250         files += strlen(files) + 1;
251     }
252     buf[0] = 0;
253 }
254
255
256 /* tests the FO_DELETE action */
257 static void test_delete(void)
258 {
259     SHFILEOPSTRUCTA shfo;
260     DWORD ret;
261     CHAR buf[sizeof(CURR_DIR)+sizeof("/test?.txt")+1];
262
263     sprintf(buf, "%s\\%s", CURR_DIR, "test?.txt");
264     buf[strlen(buf) + 1] = '\0';
265
266     shfo.hwnd = NULL;
267     shfo.wFunc = FO_DELETE;
268     shfo.pFrom = buf;
269     shfo.pTo = "\0";
270     shfo.fFlags = FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_SILENT;
271     shfo.hNameMappings = NULL;
272     shfo.lpszProgressTitle = NULL;
273
274     ok(!SHFileOperationA(&shfo), "Deletion was not successful\n");
275     ok(file_exists("test4.txt"), "Directory should not have been removed\n");
276     ok(!file_exists("test1.txt"), "File should have been removed\n");
277
278     ret = SHFileOperationA(&shfo);
279     ok(!ret, "Directory exists, but is not removed, ret=%d\n", ret);
280     ok(file_exists("test4.txt"), "Directory should not have been removed\n");
281
282     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
283
284     ok(!SHFileOperationA(&shfo), "Directory is not removed\n");
285     ok(!file_exists("test4.txt"), "Directory should have been removed\n");
286
287     ret = SHFileOperationA(&shfo);
288     ok(!ret, "The requested file does not exist, ret=%d\n", ret);
289
290     init_shfo_tests();
291     sprintf(buf, "%s\\%s", CURR_DIR, "test4.txt");
292     buf[strlen(buf) + 1] = '\0';
293     ok(MoveFileA("test1.txt", "test4.txt\\test1.txt"), "Filling the subdirectory failed\n");
294     ok(!SHFileOperationA(&shfo), "Directory is not removed\n");
295     ok(!file_exists("test4.txt"), "Directory is not removed\n");
296
297     init_shfo_tests();
298     shfo.pFrom = "test1.txt\0test4.txt\0";
299     ok(!SHFileOperationA(&shfo), "Directory and a file are not removed\n");
300     ok(!file_exists("test1.txt"), "The file should have been removed\n");
301     ok(!file_exists("test4.txt"), "Directory should have been removed\n");
302     ok(file_exists("test2.txt"), "This file should not have been removed\n");
303
304     /* FOF_FILESONLY does not delete a dir matching a wildcard */
305     init_shfo_tests();
306     shfo.fFlags |= FOF_FILESONLY;
307     shfo.pFrom = "*.txt\0";
308     ok(!SHFileOperation(&shfo), "Failed to delete files\n");
309     ok(!file_exists("test1.txt"), "test1.txt should have been removed\n");
310     ok(!file_exists("test_5.txt"), "test_5.txt should have been removed\n");
311     ok(file_exists("test4.txt"), "test4.txt should not have been removed\n");
312
313     /* FOF_FILESONLY only deletes a dir if explicitly specified */
314     init_shfo_tests();
315     shfo.pFrom = "test_?.txt\0test4.txt\0";
316     ok(!SHFileOperation(&shfo), "Failed to delete files and directory\n");
317     ok(!file_exists("test4.txt"), "test4.txt should have been removed\n");
318     ok(!file_exists("test_5.txt"), "test_5.txt should have been removed\n");
319     ok(file_exists("test1.txt"), "test1.txt should not have been removed\n");
320
321     /* try to delete an invalid filename */
322     init_shfo_tests();
323     shfo.pFrom = "\0";
324     shfo.fFlags &= ~FOF_FILESONLY;
325     shfo.fAnyOperationsAborted = FALSE;
326     ret = SHFileOperation(&shfo);
327     ok(ret == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", ret);
328     ok(!shfo.fAnyOperationsAborted, "Expected no aborted operations\n");
329     ok(file_exists("test1.txt"), "Expected test1.txt to exist\n");
330
331     /* try an invalid function */
332     init_shfo_tests();
333     shfo.pFrom = "test1.txt\0";
334     shfo.wFunc = 0;
335     ret = SHFileOperation(&shfo);
336     ok(ret == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", ret);
337     ok(file_exists("test1.txt"), "Expected test1.txt to exist\n");
338
339     /* try an invalid list, only one null terminator */
340     init_shfo_tests();
341     shfo.pFrom = "";
342     shfo.wFunc = FO_DELETE;
343     ret = SHFileOperation(&shfo);
344     ok(ret == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", ret);
345     ok(file_exists("test1.txt"), "Expected test1.txt to exist\n");
346
347     /* delete a dir, and then a file inside the dir, same as
348     * deleting a nonexistent file
349     */
350     init_shfo_tests();
351     shfo.pFrom = "testdir2\0testdir2\\one.txt\0";
352     ret = SHFileOperation(&shfo);
353     ok(ret == ERROR_PATH_NOT_FOUND, "Expected ERROR_PATH_NOT_FOUND, got %d\n", ret);
354     ok(!file_exists("testdir2"), "Expected testdir2 to not exist\n");
355     ok(!file_exists("testdir2\\one.txt"), "Expected testdir2\\one.txt to not exist\n");
356
357     /* try the FOF_NORECURSION flag, continues deleting subdirs */
358     init_shfo_tests();
359     shfo.pFrom = "testdir2\0";
360     shfo.fFlags |= FOF_NORECURSION;
361     ret = SHFileOperation(&shfo);
362     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
363     ok(!file_exists("testdir2\\one.txt"), "Expected testdir2\\one.txt to not exist\n");
364     ok(!file_exists("testdir2\\nested"), "Expected testdir2\\nested to exist\n");
365 }
366
367 /* tests the FO_RENAME action */
368 static void test_rename(void)
369 {
370     SHFILEOPSTRUCTA shfo, shfo2;
371     CHAR from[5*MAX_PATH];
372     CHAR to[5*MAX_PATH];
373     DWORD retval;
374
375     shfo.hwnd = NULL;
376     shfo.wFunc = FO_RENAME;
377     shfo.pFrom = from;
378     shfo.pTo = to;
379     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
380     shfo.hNameMappings = NULL;
381     shfo.lpszProgressTitle = NULL;
382
383     set_curr_dir_path(from, "test1.txt\0");
384     set_curr_dir_path(to, "test4.txt\0");
385     ok(SHFileOperationA(&shfo), "File is not renamed moving to other directory "
386        "when specifying directory name only\n");
387     ok(file_exists("test1.txt"), "The file is removed\n");
388
389     set_curr_dir_path(from, "test3.txt\0");
390     set_curr_dir_path(to, "test4.txt\\test1.txt\0");
391     ok(!SHFileOperationA(&shfo), "File is renamed moving to other directory\n");
392     ok(file_exists("test4.txt\\test1.txt"), "The file is not renamed\n");
393
394     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
395     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
396     retval = SHFileOperationA(&shfo); /* W98 returns 0, W2K and newer returns ERROR_GEN_FAILURE, both do nothing */
397     ok(!retval || retval == ERROR_GEN_FAILURE || retval == ERROR_INVALID_TARGET_HANDLE,
398        "Can't rename many files, retval = %d\n", retval);
399     ok(file_exists("test1.txt"), "The file is renamed - many files are specified\n");
400
401     memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
402     shfo2.fFlags |= FOF_MULTIDESTFILES;
403
404     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
405     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
406     retval = SHFileOperationA(&shfo2); /* W98 returns 0, W2K and newer returns ERROR_GEN_FAILURE, both do nothing */
407     ok(!retval || retval == ERROR_GEN_FAILURE || retval == ERROR_INVALID_TARGET_HANDLE,
408        "Can't rename many files, retval = %d\n", retval);
409     ok(file_exists("test1.txt"), "The file is not renamed - many files are specified\n");
410
411     set_curr_dir_path(from, "test1.txt\0");
412     set_curr_dir_path(to, "test6.txt\0");
413     retval = SHFileOperationA(&shfo);
414     ok(!retval, "Rename file failed, retval = %d\n", retval);
415     ok(!file_exists("test1.txt"), "The file is not renamed\n");
416     ok(file_exists("test6.txt"), "The file is not renamed\n");
417
418     set_curr_dir_path(from, "test6.txt\0");
419     set_curr_dir_path(to, "test1.txt\0");
420     retval = SHFileOperationA(&shfo);
421     ok(!retval, "Rename file back failed, retval = %d\n", retval);
422
423     set_curr_dir_path(from, "test4.txt\0");
424     set_curr_dir_path(to, "test6.txt\0");
425     retval = SHFileOperationA(&shfo);
426     ok(!retval, "Rename dir failed, retval = %d\n", retval);
427     ok(!file_exists("test4.txt"), "The dir is not renamed\n");
428     ok(file_exists("test6.txt"), "The dir is not renamed\n");
429
430     set_curr_dir_path(from, "test6.txt\0");
431     set_curr_dir_path(to, "test4.txt\0");
432     retval = SHFileOperationA(&shfo);
433     ok(!retval, "Rename dir back failed, retval = %d\n", retval);
434
435     /* try to rename more than one file to a single file */
436     shfo.pFrom = "test1.txt\0test2.txt\0";
437     shfo.pTo = "a.txt\0";
438     retval = SHFileOperationA(&shfo);
439     ok(retval == ERROR_GEN_FAILURE, "Expected ERROR_GEN_FAILURE, got %d\n", retval);
440     ok(file_exists("test1.txt"), "Expected test1.txt to exist\n");
441     ok(file_exists("test2.txt"), "Expected test2.txt to exist\n");
442
443     /* pFrom doesn't exist */
444     shfo.pFrom = "idontexist\0";
445     shfo.pTo = "newfile\0";
446     retval = SHFileOperationA(&shfo);
447     ok(retval == 1026, "Expected 1026, got %d\n", retval);
448     ok(!file_exists("newfile"), "Expected newfile to not exist\n");
449
450     /* pTo already exist */
451     shfo.pFrom = "test1.txt\0";
452     shfo.pTo = "test2.txt\0";
453     retval = SHFileOperationA(&shfo);
454         ok(retval == ERROR_ALREADY_EXISTS, "Expected ERROR_ALREADY_EXISTS, got %d\n", retval);
455
456     /* pFrom is valid, but pTo is empty */
457     shfo.pFrom = "test1.txt\0";
458     shfo.pTo = "\0";
459     retval = SHFileOperationA(&shfo);
460         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
461     ok(file_exists("test1.txt"), "Expected test1.txt to exist\n");
462
463     /* pFrom is empty */
464     shfo.pFrom = "\0";
465     retval = SHFileOperationA(&shfo);
466         ok(retval == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", retval);
467
468     /* pFrom is NULL, commented out because it crashes on nt 4.0 */
469 #if 0
470     shfo.pFrom = NULL;
471     retval = SHFileOperationA(&shfo);
472     ok(retval == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", retval);
473 #endif
474 }
475
476 /* tests the FO_COPY action */
477 static void test_copy(void)
478 {
479     SHFILEOPSTRUCTA shfo, shfo2;
480     CHAR from[5*MAX_PATH];
481     CHAR to[5*MAX_PATH];
482     FILEOP_FLAGS tmp_flags;
483     DWORD retval;
484
485     shfo.hwnd = NULL;
486     shfo.wFunc = FO_COPY;
487     shfo.pFrom = from;
488     shfo.pTo = to;
489     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
490     shfo.hNameMappings = NULL;
491     shfo.lpszProgressTitle = NULL;
492
493     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
494     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
495     ok(SHFileOperationA(&shfo), "Can't copy many files\n");
496     ok(!file_exists("test6.txt"), "The file is not copied - many files are "
497        "specified as a target\n");
498
499     memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
500     shfo2.fFlags |= FOF_MULTIDESTFILES;
501
502     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
503     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
504     ok(!SHFileOperationA(&shfo2), "Can't copy many files\n");
505     ok(file_exists("test6.txt"), "The file is copied - many files are "
506        "specified as a target\n");
507     DeleteFileA("test6.txt");
508     DeleteFileA("test7.txt");
509     RemoveDirectoryA("test8.txt");
510
511     /* number of sources do not correspond to number of targets */
512     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
513     set_curr_dir_path(to, "test6.txt\0test7.txt\0");
514     ok(SHFileOperationA(&shfo2), "Can't copy many files\n");
515     ok(!file_exists("test6.txt"), "The file is not copied - many files are "
516        "specified as a target\n");
517
518     set_curr_dir_path(from, "test1.txt\0");
519     set_curr_dir_path(to, "test4.txt\0");
520     ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are copied recursively\n");
521     ok(file_exists("test4.txt\\test1.txt"), "The file is copied\n");
522
523     set_curr_dir_path(from, "test?.txt\0");
524     set_curr_dir_path(to, "testdir2\0");
525     ok(!file_exists("testdir2\\test1.txt"), "The file is not copied yet\n");
526     ok(!file_exists("testdir2\\test4.txt"), "The directory is not copied yet\n");
527     ok(!SHFileOperationA(&shfo), "Files and directories are copied to directory\n");
528     ok(file_exists("testdir2\\test1.txt"), "The file is copied\n");
529     ok(file_exists("testdir2\\test4.txt"), "The directory is copied\n");
530     ok(file_exists("testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is copied\n");
531     clean_after_shfo_tests();
532
533     init_shfo_tests();
534     shfo.fFlags |= FOF_FILESONLY;
535     ok(!file_exists("testdir2\\test1.txt"), "The file is not copied yet\n");
536     ok(!file_exists("testdir2\\test4.txt"), "The directory is not copied yet\n");
537     ok(!SHFileOperationA(&shfo), "Files are copied to other directory\n");
538     ok(file_exists("testdir2\\test1.txt"), "The file is copied\n");
539     ok(!file_exists("testdir2\\test4.txt"), "The directory is copied\n");
540     clean_after_shfo_tests();
541
542     init_shfo_tests();
543     set_curr_dir_path(from, "test1.txt\0test2.txt\0");
544     ok(!file_exists("testdir2\\test1.txt"), "The file is not copied yet\n");
545     ok(!file_exists("testdir2\\test2.txt"), "The file is not copied yet\n");
546     ok(!SHFileOperationA(&shfo), "Files are copied to other directory\n");
547     ok(file_exists("testdir2\\test1.txt"), "The file is copied\n");
548     ok(file_exists("testdir2\\test2.txt"), "The file is copied\n");
549     clean_after_shfo_tests();
550
551     /* Copying multiple files with one not existing as source, fails the
552        entire operation in Win98/ME/2K/XP, but not in 95/NT */
553     init_shfo_tests();
554     tmp_flags = shfo.fFlags;
555     set_curr_dir_path(from, "test1.txt\0test10.txt\0test2.txt\0");
556     ok(!file_exists("testdir2\\test1.txt"), "The file is not copied yet\n");
557     ok(!file_exists("testdir2\\test2.txt"), "The file is not copied yet\n");
558     retval = SHFileOperationA(&shfo);
559     if (!retval)
560         /* Win 95/NT returns success but copies only the files up to the nonexistent source */
561         ok(file_exists("testdir2\\test1.txt"), "The file is not copied\n");
562     else
563     {
564         /* Win 98/ME/2K/XP fail the entire operation with return code 1026 if one source file does not exist */
565         ok(retval == 1026, "Files are copied to other directory\n");
566         ok(!file_exists("testdir2\\test1.txt"), "The file is copied\n");
567     }
568     ok(!file_exists("testdir2\\test2.txt"), "The file is copied\n");
569     shfo.fFlags = tmp_flags;
570
571     /* copy into a nonexistent directory */
572     init_shfo_tests();
573     shfo.fFlags = FOF_NOCONFIRMMKDIR;
574     set_curr_dir_path(from, "test1.txt\0");
575     set_curr_dir_path(to, "nonexistent\\notreal\\test2.txt\0");
576     retval= SHFileOperation(&shfo);
577         ok(!retval, "Error copying into nonexistent directory\n");
578         ok(file_exists("nonexistent"), "nonexistent not created\n");
579         ok(file_exists("nonexistent\\notreal"), "nonexistent\\notreal not created\n");
580         ok(file_exists("nonexistent\\notreal\\test2.txt"), "Directory not created\n");
581     ok(!file_exists("nonexistent\\notreal\\test1.txt"), "test1.txt should not exist\n");
582
583     /* a relative dest directory is OK */
584     clean_after_shfo_tests();
585     init_shfo_tests();
586     shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0";
587     shfo.pTo = "testdir2\0";
588     retval = SHFileOperation(&shfo);
589     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
590     ok(file_exists("testdir2\\test1.txt"), "Expected testdir2\\test1 to exist\n");
591
592     /* try to copy files to a file */
593     clean_after_shfo_tests();
594     init_shfo_tests();
595     shfo.pFrom = from;
596     shfo.pTo = to;
597     set_curr_dir_path(from, "test1.txt\0test2.txt\0");
598     set_curr_dir_path(to, "test3.txt\0");
599     retval = SHFileOperation(&shfo);
600         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
601     ok(shfo.fAnyOperationsAborted, "Expected aborted operations\n");
602     ok(!file_exists("test3.txt\\test2.txt"), "Expected test3.txt\\test2.txt to not exist\n");
603
604     /* try to copy many files to nonexistent directory */
605     DeleteFile(to);
606     shfo.fAnyOperationsAborted = FALSE;
607     retval = SHFileOperation(&shfo);
608         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
609         ok(DeleteFile("test3.txt\\test1.txt"), "Expected test3.txt\\test1.txt to exist\n");
610         ok(DeleteFile("test3.txt\\test2.txt"), "Expected test3.txt\\test1.txt to exist\n");
611         ok(RemoveDirectory(to), "Expected test3.txt to exist\n");
612
613     /* send in FOF_MULTIDESTFILES with too many destination files */
614     init_shfo_tests();
615     shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0";
616     shfo.pTo = "testdir2\\a.txt\0testdir2\\b.txt\0testdir2\\c.txt\0testdir2\\d.txt\0";
617     shfo.fFlags |= FOF_NOERRORUI | FOF_MULTIDESTFILES;
618     retval = SHFileOperation(&shfo);
619         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
620     ok(shfo.fAnyOperationsAborted, "Expected aborted operations\n");
621     ok(!file_exists("testdir2\\a.txt"), "Expected testdir2\\a.txt to not exist\n");
622
623     /* send in FOF_MULTIDESTFILES with too many destination files */
624     shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0";
625     shfo.pTo = "e.txt\0f.txt\0";
626     shfo.fAnyOperationsAborted = FALSE;
627     retval = SHFileOperation(&shfo);
628         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
629     ok(shfo.fAnyOperationsAborted, "Expected aborted operations\n");
630     ok(!file_exists("e.txt"), "Expected e.txt to not exist\n");
631
632     /* use FOF_MULTIDESTFILES with files and a source directory */
633     shfo.pFrom = "test1.txt\0test2.txt\0test4.txt\0";
634     shfo.pTo = "testdir2\\a.txt\0testdir2\\b.txt\0testdir2\\c.txt\0";
635     shfo.fAnyOperationsAborted = FALSE;
636     retval = SHFileOperation(&shfo);
637     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
638     ok(DeleteFile("testdir2\\a.txt"), "Expected testdir2\\a.txt to exist\n");
639     ok(DeleteFile("testdir2\\b.txt"), "Expected testdir2\\b.txt to exist\n");
640     ok(RemoveDirectory("testdir2\\c.txt"), "Expected testdir2\\c.txt to exist\n");
641
642     /* try many dest files without FOF_MULTIDESTFILES flag */
643     shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0";
644     shfo.pTo = "a.txt\0b.txt\0c.txt\0";
645     shfo.fAnyOperationsAborted = FALSE;
646     shfo.fFlags &= ~FOF_MULTIDESTFILES;
647     retval = SHFileOperation(&shfo);
648         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
649     ok(!file_exists("a.txt"), "Expected a.txt to not exist\n");
650
651     /* try a glob */
652     shfo.pFrom = "test?.txt\0";
653     shfo.pTo = "testdir2\0";
654     shfo.fFlags &= ~FOF_MULTIDESTFILES;
655     retval = SHFileOperation(&shfo);
656         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
657         ok(file_exists("testdir2\\test1.txt"), "Expected testdir2\\test1.txt to exist\n");
658
659     /* try a glob with FOF_FILESONLY */
660     clean_after_shfo_tests();
661     init_shfo_tests();
662     shfo.pFrom = "test?.txt\0";
663     shfo.fFlags |= FOF_FILESONLY;
664     retval = SHFileOperation(&shfo);
665         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
666         ok(file_exists("testdir2\\test1.txt"), "Expected testdir2\\test1.txt to exist\n");
667     ok(!file_exists("testdir2\\test4.txt"), "Expected testdir2\\test4.txt to not exist\n");
668
669     /* try a glob with FOF_MULTIDESTFILES and the same number
670     * of dest files that we would expect
671     */
672     clean_after_shfo_tests();
673     init_shfo_tests();
674     shfo.pTo = "testdir2\\a.txt\0testdir2\\b.txt\0testdir2\\c.txt\0testdir2\\d.txt\0";
675     shfo.fFlags &= ~FOF_FILESONLY;
676     shfo.fFlags |= FOF_MULTIDESTFILES;
677     retval = SHFileOperation(&shfo);
678         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
679     ok(shfo.fAnyOperationsAborted, "Expected aborted operations\n");
680     ok(!file_exists("testdir2\\a.txt"), "Expected testdir2\\test1.txt to not exist\n");
681     ok(!RemoveDirectory("b.txt"), "b.txt should not exist\n");
682
683     /* copy one file to two others, second is ignored */
684     clean_after_shfo_tests();
685     init_shfo_tests();
686     shfo.pFrom = "test1.txt\0";
687     shfo.pTo = "b.txt\0c.txt\0";
688     shfo.fAnyOperationsAborted = FALSE;
689     retval = SHFileOperation(&shfo);
690         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
691         ok(DeleteFile("b.txt"), "Expected b.txt to exist\n");
692     ok(!DeleteFile("c.txt"), "Expected c.txt to not exist\n");
693
694     /* copy two file to three others, all fail */
695     shfo.pFrom = "test1.txt\0test2.txt\0";
696     shfo.pTo = "b.txt\0c.txt\0d.txt\0";
697     retval = SHFileOperation(&shfo);
698         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
699     ok(shfo.fAnyOperationsAborted, "Expected operations to be aborted\n");
700     ok(!DeleteFile("b.txt"), "Expected b.txt to not exist\n");
701
702     /* copy one file and one directory to three others */
703     shfo.pFrom = "test1.txt\0test4.txt\0";
704     shfo.pTo = "b.txt\0c.txt\0d.txt\0";
705     shfo.fAnyOperationsAborted = FALSE;
706     retval = SHFileOperation(&shfo);
707         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
708     ok(shfo.fAnyOperationsAborted, "Expected operations to be aborted\n");
709     ok(!DeleteFile("b.txt"), "Expected b.txt to not exist\n");
710     ok(!DeleteFile("c.txt"), "Expected c.txt to not exist\n");
711
712     /* copy a directory with a file beneath it, plus some files */
713     createTestFile("test4.txt\\a.txt");
714     shfo.pFrom = "test4.txt\0test1.txt\0";
715     shfo.pTo = "testdir2\0";
716     shfo.fFlags &= ~FOF_MULTIDESTFILES;
717     shfo.fAnyOperationsAborted = FALSE;
718     retval = SHFileOperation(&shfo);
719     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
720     ok(DeleteFile("testdir2\\test1.txt"), "Expected newdir\\test1.txt to exist\n");
721     ok(DeleteFile("testdir2\\test4.txt\\a.txt"), "Expected a.txt to exist\n");
722     ok(RemoveDirectory("testdir2\\test4.txt"), "Expected testdir2\\test4.txt to exist\n");
723
724     /* copy one directory and a file in that dir to another dir */
725     shfo.pFrom = "test4.txt\0test4.txt\\a.txt\0";
726     shfo.pTo = "testdir2\0";
727     retval = SHFileOperation(&shfo);
728     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
729     ok(DeleteFile("testdir2\\test4.txt\\a.txt"), "Expected a.txt to exist\n");
730     ok(DeleteFile("testdir2\\a.txt"), "Expected testdir2\\a.txt to exist\n");
731
732     /* copy a file in a directory first, and then the directory to a nonexistent dir */
733     shfo.pFrom = "test4.txt\\a.txt\0test4.txt\0";
734     shfo.pTo = "nonexistent\0";
735     retval = SHFileOperation(&shfo);
736         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
737     ok(shfo.fAnyOperationsAborted, "Expected operations to be aborted\n");
738     ok(!file_exists("nonexistent\\test4.txt"), "Expected nonexistent\\test4.txt to not exist\n");
739     DeleteFile("test4.txt\\a.txt");
740
741     /* destination is same as source file */
742     shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0";
743     shfo.pTo = "b.txt\0test2.txt\0c.txt\0";
744     shfo.fAnyOperationsAborted = FALSE;
745     shfo.fFlags = FOF_NOERRORUI | FOF_MULTIDESTFILES;
746     retval = SHFileOperation(&shfo);
747         ok(retval == ERROR_NO_MORE_SEARCH_HANDLES,
748            "Expected ERROR_NO_MORE_SEARCH_HANDLES, got %d\n", retval);
749         ok(!shfo.fAnyOperationsAborted, "Expected no operations to be aborted\n");
750         ok(DeleteFile("b.txt"), "Expected b.txt to exist\n");
751     ok(!file_exists("c.txt"), "Expected c.txt to not exist\n");
752
753     /* destination is same as source directory */
754     shfo.pFrom = "test1.txt\0test4.txt\0test3.txt\0";
755     shfo.pTo = "b.txt\0test4.txt\0c.txt\0";
756     shfo.fAnyOperationsAborted = FALSE;
757     retval = SHFileOperation(&shfo);
758         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
759         ok(DeleteFile("b.txt"), "Expected b.txt to exist\n");
760     ok(!file_exists("c.txt"), "Expected c.txt to not exist\n");
761
762     /* copy a directory into itself, error displayed in UI */
763     shfo.pFrom = "test4.txt\0";
764     shfo.pTo = "test4.txt\\newdir\0";
765     shfo.fFlags &= ~FOF_MULTIDESTFILES;
766     shfo.fAnyOperationsAborted = FALSE;
767     retval = SHFileOperation(&shfo);
768         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
769     ok(!RemoveDirectory("test4.txt\\newdir"), "Expected test4.txt\\newdir to not exist\n");
770
771     /* copy a directory to itself, error displayed in UI */
772     shfo.pFrom = "test4.txt\0";
773     shfo.pTo = "test4.txt\0";
774     shfo.fAnyOperationsAborted = FALSE;
775     retval = SHFileOperation(&shfo);
776         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
777
778     /* copy a file into a directory, and the directory into itself */
779     shfo.pFrom = "test1.txt\0test4.txt\0";
780     shfo.pTo = "test4.txt\0";
781     shfo.fAnyOperationsAborted = FALSE;
782     shfo.fFlags |= FOF_NOCONFIRMATION;
783     retval = SHFileOperation(&shfo);
784         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
785     ok(DeleteFile("test4.txt\\test1.txt"), "Expected test4.txt\\test1.txt to exist\n");
786
787     /* copy a file to a file, and the directory into itself */
788     shfo.pFrom = "test1.txt\0test4.txt\0";
789     shfo.pTo = "test4.txt\\a.txt\0";
790     shfo.fAnyOperationsAborted = FALSE;
791     retval = SHFileOperation(&shfo);
792         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
793     ok(!file_exists("test4.txt\\a.txt"), "Expected test4.txt\\a.txt to not exist\n");
794
795     /* copy a nonexistent file to a nonexistent directory */
796     shfo.pFrom = "e.txt\0";
797     shfo.pTo = "nonexistent\0";
798     shfo.fAnyOperationsAborted = FALSE;
799     retval = SHFileOperation(&shfo);
800     ok(retval == 1026, "Expected 1026, got %d\n", retval);
801     ok(!file_exists("nonexistent\\e.txt"), "Expected nonexistent\\e.txt to not exist\n");
802     ok(!file_exists("nonexistent"), "Expected nonexistent to not exist\n");
803
804     /* Overwrite tests */
805     clean_after_shfo_tests();
806     init_shfo_tests();
807     shfo.fFlags = FOF_NOCONFIRMATION;
808     shfo.pFrom = "test1.txt\0";
809     shfo.pTo = "test2.txt\0";
810     shfo.fAnyOperationsAborted = FALSE;
811     /* without FOF_NOCOFIRMATION the confirmation is Yes/No */
812     retval = SHFileOperation(&shfo);
813     ok(retval == 0, "Expected 0, got %d\n", retval);
814     ok(file_has_content("test2.txt", "test1.txt\n"), "The file was not copied\n");
815
816     shfo.pFrom = "test3.txt\0test1.txt\0";
817     shfo.pTo = "test2.txt\0one.txt\0";
818     shfo.fFlags = FOF_NOCONFIRMATION | FOF_MULTIDESTFILES;
819     /* without FOF_NOCOFIRMATION the confirmation is Yes/Yes to All/No/Cancel */
820     retval = SHFileOperation(&shfo);
821     ok(retval == 0, "Expected 0, got %d\n", retval);
822     ok(file_has_content("test2.txt", "test3.txt\n"), "The file was not copied\n");
823
824     shfo.pFrom = "one.txt\0";
825     shfo.pTo = "testdir2\0";
826     shfo.fFlags = FOF_NOCONFIRMATION;
827     /* without FOF_NOCOFIRMATION the confirmation is Yes/No */
828     retval = SHFileOperation(&shfo);
829     ok(retval == 0, "Expected 0, got %d\n", retval);
830     ok(file_has_content("testdir2\\one.txt", "test1.txt\n"), "The file was not copied\n");
831
832     createTestFile("test4.txt\\test1.txt");
833     shfo.pFrom = "test4.txt\0";
834     shfo.pTo = "testdir2\0";
835     shfo.fFlags = FOF_NOCONFIRMATION;
836     ok(!SHFileOperation(&shfo), "First SHFileOperation failed\n");
837     createTestFile("test4.txt\\.\\test1.txt"); /* modify the content of the file */
838     /* without FOF_NOCOFIRMATION the confirmation is "This folder already contains a folder named ..." */
839     retval = SHFileOperation(&shfo);
840     ok(retval == 0, "Expected 0, got %d\n", retval);
841     ok(file_has_content("testdir2\\test4.txt\\test1.txt", "test4.txt\\.\\test1.txt\n"), "The file was not copied\n");
842 }
843
844 /* tests the FO_MOVE action */
845 static void test_move(void)
846 {
847     SHFILEOPSTRUCTA shfo, shfo2;
848     CHAR from[5*MAX_PATH];
849     CHAR to[5*MAX_PATH];
850     DWORD retval;
851
852     shfo.hwnd = NULL;
853     shfo.wFunc = FO_MOVE;
854     shfo.pFrom = from;
855     shfo.pTo = to;
856     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
857     shfo.hNameMappings = NULL;
858     shfo.lpszProgressTitle = NULL;
859
860     set_curr_dir_path(from, "test1.txt\0");
861     set_curr_dir_path(to, "test4.txt\0");
862     ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are moved recursively\n");
863     ok(!file_exists("test1.txt"), "test1.txt should not exist\n");
864     ok(file_exists("test4.txt\\test1.txt"), "The file is not moved\n");
865
866     set_curr_dir_path(from, "test?.txt\0");
867     set_curr_dir_path(to, "testdir2\0");
868     ok(!file_exists("testdir2\\test2.txt"), "The file is not moved yet\n");
869     ok(!file_exists("testdir2\\test4.txt"), "The directory is not moved yet\n");
870     ok(!SHFileOperationA(&shfo), "Files and directories are moved to directory\n");
871     ok(file_exists("testdir2\\test2.txt"), "The file is moved\n");
872     ok(file_exists("testdir2\\test4.txt"), "The directory is moved\n");
873     ok(file_exists("testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is moved\n");
874
875     clean_after_shfo_tests();
876     init_shfo_tests();
877
878     memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
879     shfo2.fFlags |= FOF_MULTIDESTFILES;
880
881     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
882     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
883     ok(!SHFileOperationA(&shfo2), "Move many files\n");
884     ok(file_exists("test6.txt"), "The file is moved - many files are "
885        "specified as a target\n");
886     DeleteFileA("test6.txt");
887     DeleteFileA("test7.txt");
888     RemoveDirectoryA("test8.txt");
889
890     init_shfo_tests();
891
892     /* number of sources do not correspond to number of targets */
893     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
894     set_curr_dir_path(to, "test6.txt\0test7.txt\0");
895     ok(SHFileOperationA(&shfo2), "Can't move many files\n");
896     ok(!file_exists("test6.txt"), "The file is not moved - many files are "
897        "specified as a target\n");
898
899     init_shfo_tests();
900
901     set_curr_dir_path(from, "test3.txt\0");
902     set_curr_dir_path(to, "test4.txt\\test1.txt\0");
903     ok(!SHFileOperationA(&shfo), "File is moved moving to other directory\n");
904     ok(file_exists("test4.txt\\test1.txt"), "The file is moved\n");
905
906     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
907     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
908     ok(SHFileOperationA(&shfo), "Cannot move many files\n");
909     ok(file_exists("test1.txt"), "The file is not moved. Many files are specified\n");
910     ok(file_exists("test4.txt"), "The directory is not moved. Many files are specified\n");
911
912     set_curr_dir_path(from, "test1.txt\0");
913     set_curr_dir_path(to, "test6.txt\0");
914     ok(!SHFileOperationA(&shfo), "Move file\n");
915     ok(!file_exists("test1.txt"), "The file is moved\n");
916     ok(file_exists("test6.txt"), "The file is moved\n");
917     set_curr_dir_path(from, "test6.txt\0");
918     set_curr_dir_path(to, "test1.txt\0");
919     ok(!SHFileOperationA(&shfo), "Move file back\n");
920
921     set_curr_dir_path(from, "test4.txt\0");
922     set_curr_dir_path(to, "test6.txt\0");
923     ok(!SHFileOperationA(&shfo), "Move dir\n");
924     ok(!file_exists("test4.txt"), "The dir is moved\n");
925     ok(file_exists("test6.txt"), "The dir is moved\n");
926     set_curr_dir_path(from, "test6.txt\0");
927     set_curr_dir_path(to, "test4.txt\0");
928     ok(!SHFileOperationA(&shfo), "Move dir back\n");
929
930     /* move one file to two others */
931     init_shfo_tests();
932     shfo.pFrom = "test1.txt\0";
933     shfo.pTo = "a.txt\0b.txt\0";
934     retval = SHFileOperationA(&shfo);
935         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
936         ok(!file_exists("test1.txt"), "Expected test1.txt to not exist\n");
937         ok(DeleteFile("a.txt"), "Expected a.txt to exist\n");
938     ok(!file_exists("b.txt"), "Expected b.txt to not exist\n");
939
940     /* move two files to one other */
941     shfo.pFrom = "test2.txt\0test3.txt\0";
942     shfo.pTo = "test1.txt\0";
943     retval = SHFileOperationA(&shfo);
944         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
945         ok(!file_exists("test1.txt"), "Expected test1.txt to not exist\n");
946     ok(file_exists("test2.txt"), "Expected test2.txt to exist\n");
947     ok(file_exists("test3.txt"), "Expected test3.txt to exist\n");
948
949     /* move a directory into itself */
950     shfo.pFrom = "test4.txt\0";
951     shfo.pTo = "test4.txt\\b.txt\0";
952     retval = SHFileOperationA(&shfo);
953         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
954     ok(!RemoveDirectory("test4.txt\\b.txt"), "Expected test4.txt\\b.txt to not exist\n");
955     ok(file_exists("test4.txt"), "Expected test4.txt to exist\n");
956
957     /* move many files without FOF_MULTIDESTFILES */
958     shfo.pFrom = "test2.txt\0test3.txt\0";
959     shfo.pTo = "d.txt\0e.txt\0";
960     retval = SHFileOperationA(&shfo);
961         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
962     ok(!DeleteFile("d.txt"), "Expected d.txt to not exist\n");
963     ok(!DeleteFile("e.txt"), "Expected e.txt to not exist\n");
964
965     /* number of sources != number of targets */
966     shfo.pTo = "d.txt\0";
967     shfo.fFlags |= FOF_MULTIDESTFILES;
968     retval = SHFileOperationA(&shfo);
969         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
970     ok(!DeleteFile("d.txt"), "Expected d.txt to not exist\n");
971
972     /* FO_MOVE does not create dest directories */
973     shfo.pFrom = "test2.txt\0";
974     shfo.pTo = "dir1\\dir2\\test2.txt\0";
975     retval = SHFileOperationA(&shfo);
976         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
977     ok(!file_exists("dir1"), "Expected dir1 to not exist\n");
978
979     /* try to overwrite an existing file */
980     shfo.pTo = "test3.txt\0";
981     retval = SHFileOperationA(&shfo);
982         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
983         ok(!file_exists("test2.txt"), "Expected test2.txt to not exist\n");
984     ok(file_exists("test3.txt"), "Expected test3.txt to exist\n");
985 }
986
987 static void test_sh_create_dir(void)
988 {
989     CHAR path[MAX_PATH];
990     int ret;
991
992     if(!pSHCreateDirectoryExA)
993     {
994         trace("skipping SHCreateDirectoryExA tests\n");
995         return;
996     }
997
998     set_curr_dir_path(path, "testdir2\\test4.txt\0");
999     ret = pSHCreateDirectoryExA(NULL, path, NULL);
1000     ok(ERROR_SUCCESS == ret, "SHCreateDirectoryEx failed to create directory recursively, ret = %d\n", ret);
1001     ok(file_exists("testdir2"), "The first directory is not created\n");
1002     ok(file_exists("testdir2\\test4.txt"), "The second directory is not created\n");
1003
1004     ret = pSHCreateDirectoryExA(NULL, path, NULL);
1005     ok(ERROR_ALREADY_EXISTS == ret, "SHCreateDirectoryEx should fail to create existing directory, ret = %d\n", ret);
1006
1007     ret = pSHCreateDirectoryExA(NULL, "c:\\testdir3", NULL);
1008     ok(file_exists("c:\\testdir3"), "The directory is not created\n");
1009 }
1010
1011 static void test_sh_path_prepare(void)
1012 {
1013     HRESULT res;
1014     CHAR path[MAX_PATH];
1015
1016     if(!pSHPathPrepareForWriteA)
1017     {
1018         trace("skipping SHPathPrepareForWriteA tests\n");
1019             return;
1020     }
1021
1022     /* directory exists, SHPPFW_NONE */
1023     set_curr_dir_path(path, "testdir2\0");
1024     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_NONE);
1025     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
1026
1027     /* directory exists, SHPPFW_IGNOREFILENAME */
1028     set_curr_dir_path(path, "testdir2\\test4.txt\0");
1029     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_IGNOREFILENAME);
1030     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
1031
1032     /* directory exists, SHPPFW_DIRCREATE */
1033     set_curr_dir_path(path, "testdir2\0");
1034     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_DIRCREATE);
1035     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
1036
1037     /* directory exists, SHPPFW_IGNOREFILENAME|SHPPFW_DIRCREATE */
1038     set_curr_dir_path(path, "testdir2\\test4.txt\0");
1039     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_IGNOREFILENAME|SHPPFW_DIRCREATE);
1040     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
1041     ok(!file_exists("nonexistent\\"), "nonexistent\\ exists but shouldn't\n");
1042
1043     /* file exists, SHPPFW_NONE */
1044     set_curr_dir_path(path, "test1.txt\0");
1045     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_NONE);
1046     ok(res == HRESULT_FROM_WIN32(ERROR_DIRECTORY), "res == 0x%08x, expected HRESULT_FROM_WIN32(ERROR_DIRECTORY)\n", res);
1047
1048     /* file exists, SHPPFW_DIRCREATE */
1049     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_DIRCREATE);
1050     ok(res == HRESULT_FROM_WIN32(ERROR_DIRECTORY), "res == 0x%08x, expected HRESULT_FROM_WIN32(ERROR_DIRECTORY)\n", res);
1051
1052     /* file exists, SHPPFW_NONE, trailing \ */
1053     set_curr_dir_path(path, "test1.txt\\\0");
1054     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_NONE);
1055     ok(res == HRESULT_FROM_WIN32(ERROR_DIRECTORY), "res == 0x%08x, expected HRESULT_FROM_WIN32(ERROR_DIRECTORY)\n", res);
1056
1057     /* relative path exists, SHPPFW_DIRCREATE */
1058     res = pSHPathPrepareForWriteA(0, 0, ".\\testdir2", SHPPFW_DIRCREATE);
1059     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
1060
1061     /* relative path doesn't exist, SHPPFW_DIRCREATE -- Windows does not create the directory in this case */
1062     res = pSHPathPrepareForWriteA(0, 0, ".\\testdir2\\test4.txt", SHPPFW_DIRCREATE);
1063     ok(res == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), "res == 0x%08x, expected HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)\n", res);
1064     ok(!file_exists(".\\testdir2\\test4.txt\\"), ".\\testdir2\\test4.txt\\ exists but shouldn't\n");
1065
1066     /* directory doesn't exist, SHPPFW_NONE */
1067     set_curr_dir_path(path, "nonexistent\0");
1068     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_NONE);
1069     ok(res == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), "res == 0x%08x, expected HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)\n", res);
1070
1071     /* directory doesn't exist, SHPPFW_IGNOREFILENAME */
1072     set_curr_dir_path(path, "nonexistent\\notreal\0");
1073     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_IGNOREFILENAME);
1074     ok(res == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), "res == 0x%08x, expected HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)\n", res);
1075     ok(!file_exists("nonexistent\\notreal"), "nonexistent\\notreal exists but shouldn't\n");
1076     ok(!file_exists("nonexistent\\"), "nonexistent\\ exists but shouldn't\n");
1077
1078     /* directory doesn't exist, SHPPFW_IGNOREFILENAME|SHPPFW_DIRCREATE */
1079     set_curr_dir_path(path, "testdir2\\test4.txt\\\0");
1080     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_IGNOREFILENAME|SHPPFW_DIRCREATE);
1081     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
1082     ok(file_exists("testdir2\\test4.txt\\"), "testdir2\\test4.txt doesn't exist but should\n");
1083
1084     /* nested directory doesn't exist, SHPPFW_DIRCREATE */
1085     set_curr_dir_path(path, "nonexistent\\notreal\0");
1086     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_DIRCREATE);
1087     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
1088     ok(file_exists("nonexistent\\notreal"), "nonexistent\\notreal doesn't exist but should\n");
1089
1090     /* SHPPFW_ASKDIRCREATE, SHPPFW_NOWRITECHECK, and SHPPFW_MEDIACHECKONLY are untested */
1091
1092     if(!pSHPathPrepareForWriteW)
1093     {
1094         skip("Skipping SHPathPrepareForWriteW tests\n");
1095         return;
1096     }
1097     /* unicode directory doesn't exist, SHPPFW_NONE */
1098     res = pSHPathPrepareForWriteW(0, 0, UNICODE_PATH, SHPPFW_NONE);
1099     ok(res == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), "res == %08x, expected HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)\n", res);
1100     ok(!file_existsW(UNICODE_PATH), "unicode path was created but shouldn't be\n");
1101     RemoveDirectoryW(UNICODE_PATH);
1102
1103     /* unicode directory doesn't exist, SHPPFW_DIRCREATE */
1104     res = pSHPathPrepareForWriteW(0, 0, UNICODE_PATH, SHPPFW_DIRCREATE);
1105     ok(res == S_OK, "res == %08x, expected S_OK\n", res);
1106     ok(file_existsW(UNICODE_PATH), "unicode path should've been created\n");
1107
1108     /* unicode directory exists, SHPPFW_NONE */
1109     res = pSHPathPrepareForWriteW(0, 0, UNICODE_PATH, SHPPFW_NONE);
1110     ok(res == S_OK, "ret == %08x, expected S_OK\n", res);
1111
1112     /* unicode directory exists, SHPPFW_DIRCREATE */
1113     res = pSHPathPrepareForWriteW(0, 0, UNICODE_PATH, SHPPFW_DIRCREATE);
1114     ok(res == S_OK, "ret == %08x, expected S_OK\n", res);
1115     RemoveDirectoryW(UNICODE_PATH);
1116 }
1117
1118 static void test_unicode(void)
1119 {
1120     SHFILEOPSTRUCTW shfoW;
1121     int ret;
1122     HANDLE file;
1123
1124     if (!pSHFileOperationW)
1125     {
1126         skip("SHFileOperationW() is missing\n");
1127         return;
1128     }
1129
1130     shfoW.hwnd = NULL;
1131     shfoW.wFunc = FO_DELETE;
1132     shfoW.pFrom = UNICODE_PATH;
1133     shfoW.pTo = '\0';
1134     shfoW.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
1135     shfoW.hNameMappings = NULL;
1136     shfoW.lpszProgressTitle = NULL;
1137
1138     /* Clean up before start test */
1139     DeleteFileW(UNICODE_PATH);
1140     RemoveDirectoryW(UNICODE_PATH);
1141
1142     /* Make sure we are on a system that supports unicode */
1143     SetLastError(0xdeadbeef);
1144     file = CreateFileW(UNICODE_PATH, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
1145     if (GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
1146     {
1147         skip("Unicode tests skipped on non-unicode system\n");
1148         return;
1149     }
1150     CloseHandle(file);
1151
1152     /* Try to delete a file with unicode filename */
1153     ok(file_existsW(UNICODE_PATH), "The file does not exist\n");
1154     ret = pSHFileOperationW(&shfoW);
1155     ok(!ret, "File is not removed, ErrorCode: %d\n", ret);
1156     ok(!file_existsW(UNICODE_PATH), "The file should have been removed\n");
1157
1158     /* Try to trash a file with unicode filename */
1159     createTestFileW(UNICODE_PATH);
1160     shfoW.fFlags |= FOF_ALLOWUNDO;
1161     ok(file_existsW(UNICODE_PATH), "The file does not exist\n");
1162     ret = pSHFileOperationW(&shfoW);
1163     ok(!ret, "File is not removed, ErrorCode: %d\n", ret);
1164     ok(!file_existsW(UNICODE_PATH), "The file should have been removed\n");
1165
1166     if(!pSHCreateDirectoryExW)
1167     {
1168         skip("Skipping SHCreateDirectoryExW tests\n");
1169         return;
1170     }
1171
1172     /* Try to delete a directory with unicode filename */
1173     ret = pSHCreateDirectoryExW(NULL, UNICODE_PATH, NULL);
1174     ok(!ret, "SHCreateDirectoryExW returned %d\n", ret);
1175     ok(file_existsW(UNICODE_PATH), "The directory is not created\n");
1176     shfoW.fFlags &= ~FOF_ALLOWUNDO;
1177     ret = pSHFileOperationW(&shfoW);
1178     ok(!ret, "Directory is not removed, ErrorCode: %d\n", ret);
1179     ok(!file_existsW(UNICODE_PATH), "The directory should have been removed\n");
1180
1181     /* Try to trash a directory with unicode filename */
1182     ret = pSHCreateDirectoryExW(NULL, UNICODE_PATH, NULL);
1183     ok(!ret, "SHCreateDirectoryExW returned %d\n", ret);
1184     ok(file_existsW(UNICODE_PATH), "The directory was not created\n");
1185     shfoW.fFlags |= FOF_ALLOWUNDO;
1186     ret = pSHFileOperationW(&shfoW);
1187     ok(!ret, "Directory is not removed, ErrorCode: %d\n", ret);
1188     ok(!file_existsW(UNICODE_PATH), "The directory should have been removed\n");
1189 }
1190
1191 START_TEST(shlfileop)
1192 {
1193     InitFunctionPointers();
1194
1195     clean_after_shfo_tests();
1196
1197     init_shfo_tests();
1198     test_get_file_info();
1199     clean_after_shfo_tests();
1200
1201     init_shfo_tests();
1202     test_delete();
1203     clean_after_shfo_tests();
1204
1205     init_shfo_tests();
1206     test_rename();
1207     clean_after_shfo_tests();
1208
1209     init_shfo_tests();
1210     test_copy();
1211     clean_after_shfo_tests();
1212
1213     init_shfo_tests();
1214     test_move();
1215     clean_after_shfo_tests();
1216
1217     test_sh_create_dir();
1218     clean_after_shfo_tests();
1219
1220     init_shfo_tests();
1221     test_sh_path_prepare();
1222     clean_after_shfo_tests();
1223
1224     test_unicode();
1225 }