mshtml: Added put_backgroundImage implementation.
[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     LPSTR ptr;
485
486     shfo.hwnd = NULL;
487     shfo.wFunc = FO_COPY;
488     shfo.pFrom = from;
489     shfo.pTo = to;
490     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
491     shfo.hNameMappings = NULL;
492     shfo.lpszProgressTitle = NULL;
493
494     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
495     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
496     ok(SHFileOperationA(&shfo), "Can't copy many files\n");
497     ok(!file_exists("test6.txt"), "The file is not copied - many files are "
498        "specified as a target\n");
499
500     memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
501     shfo2.fFlags |= FOF_MULTIDESTFILES;
502
503     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
504     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
505     ok(!SHFileOperationA(&shfo2), "Can't copy many files\n");
506     ok(file_exists("test6.txt"), "The file is copied - many files are "
507        "specified as a target\n");
508     DeleteFileA("test6.txt");
509     DeleteFileA("test7.txt");
510     RemoveDirectoryA("test8.txt");
511
512     /* number of sources do not correspond to number of targets */
513     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
514     set_curr_dir_path(to, "test6.txt\0test7.txt\0");
515     ok(SHFileOperationA(&shfo2), "Can't copy many files\n");
516     ok(!file_exists("test6.txt"), "The file is not copied - many files are "
517        "specified as a target\n");
518
519     set_curr_dir_path(from, "test1.txt\0");
520     set_curr_dir_path(to, "test4.txt\0");
521     ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are copied recursively\n");
522     ok(file_exists("test4.txt\\test1.txt"), "The file is copied\n");
523
524     set_curr_dir_path(from, "test?.txt\0");
525     set_curr_dir_path(to, "testdir2\0");
526     ok(!file_exists("testdir2\\test1.txt"), "The file is not copied yet\n");
527     ok(!file_exists("testdir2\\test4.txt"), "The directory is not copied yet\n");
528     ok(!SHFileOperationA(&shfo), "Files and directories are copied to directory\n");
529     ok(file_exists("testdir2\\test1.txt"), "The file is copied\n");
530     ok(file_exists("testdir2\\test4.txt"), "The directory is copied\n");
531     ok(file_exists("testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is copied\n");
532     clean_after_shfo_tests();
533
534     init_shfo_tests();
535     shfo.fFlags |= FOF_FILESONLY;
536     ok(!file_exists("testdir2\\test1.txt"), "The file is not copied yet\n");
537     ok(!file_exists("testdir2\\test4.txt"), "The directory is not copied yet\n");
538     ok(!SHFileOperationA(&shfo), "Files are copied to other directory\n");
539     ok(file_exists("testdir2\\test1.txt"), "The file is copied\n");
540     ok(!file_exists("testdir2\\test4.txt"), "The directory is copied\n");
541     clean_after_shfo_tests();
542
543     init_shfo_tests();
544     set_curr_dir_path(from, "test1.txt\0test2.txt\0");
545     ok(!file_exists("testdir2\\test1.txt"), "The file is not copied yet\n");
546     ok(!file_exists("testdir2\\test2.txt"), "The file is not copied yet\n");
547     ok(!SHFileOperationA(&shfo), "Files are copied to other directory\n");
548     ok(file_exists("testdir2\\test1.txt"), "The file is copied\n");
549     ok(file_exists("testdir2\\test2.txt"), "The file is copied\n");
550     clean_after_shfo_tests();
551
552     /* Copying multiple files with one not existing as source, fails the
553        entire operation in Win98/ME/2K/XP, but not in 95/NT */
554     init_shfo_tests();
555     tmp_flags = shfo.fFlags;
556     set_curr_dir_path(from, "test1.txt\0test10.txt\0test2.txt\0");
557     ok(!file_exists("testdir2\\test1.txt"), "The file is not copied yet\n");
558     ok(!file_exists("testdir2\\test2.txt"), "The file is not copied yet\n");
559     retval = SHFileOperationA(&shfo);
560     if (!retval)
561         /* Win 95/NT returns success but copies only the files up to the nonexistent source */
562         ok(file_exists("testdir2\\test1.txt"), "The file is not copied\n");
563     else
564     {
565         /* Win 98/ME/2K/XP fail the entire operation with return code 1026 if one source file does not exist */
566         ok(retval == 1026, "Files are copied to other directory\n");
567         ok(!file_exists("testdir2\\test1.txt"), "The file is copied\n");
568     }
569     ok(!file_exists("testdir2\\test2.txt"), "The file is copied\n");
570     shfo.fFlags = tmp_flags;
571
572     /* copy into a nonexistent directory */
573     init_shfo_tests();
574     shfo.fFlags = FOF_NOCONFIRMMKDIR;
575     set_curr_dir_path(from, "test1.txt\0");
576     set_curr_dir_path(to, "nonexistent\\notreal\\test2.txt\0");
577     retval= SHFileOperation(&shfo);
578         ok(!retval, "Error copying into nonexistent directory\n");
579         ok(file_exists("nonexistent"), "nonexistent not created\n");
580         ok(file_exists("nonexistent\\notreal"), "nonexistent\\notreal not created\n");
581         ok(file_exists("nonexistent\\notreal\\test2.txt"), "Directory not created\n");
582     ok(!file_exists("nonexistent\\notreal\\test1.txt"), "test1.txt should not exist\n");
583
584     /* a relative dest directory is OK */
585     clean_after_shfo_tests();
586     init_shfo_tests();
587     shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0";
588     shfo.pTo = "testdir2\0";
589     retval = SHFileOperation(&shfo);
590     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
591     ok(file_exists("testdir2\\test1.txt"), "Expected testdir2\\test1 to exist\n");
592
593     /* try to copy files to a file */
594     clean_after_shfo_tests();
595     init_shfo_tests();
596     shfo.pFrom = from;
597     shfo.pTo = to;
598     set_curr_dir_path(from, "test1.txt\0test2.txt\0");
599     set_curr_dir_path(to, "test3.txt\0");
600     retval = SHFileOperation(&shfo);
601         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
602     ok(shfo.fAnyOperationsAborted, "Expected aborted operations\n");
603     ok(!file_exists("test3.txt\\test2.txt"), "Expected test3.txt\\test2.txt to not exist\n");
604
605     /* try to copy many files to nonexistent directory */
606     DeleteFile(to);
607     shfo.fAnyOperationsAborted = FALSE;
608     retval = SHFileOperation(&shfo);
609         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
610         ok(DeleteFile("test3.txt\\test1.txt"), "Expected test3.txt\\test1.txt to exist\n");
611         ok(DeleteFile("test3.txt\\test2.txt"), "Expected test3.txt\\test1.txt to exist\n");
612         ok(RemoveDirectory(to), "Expected test3.txt to exist\n");
613
614     /* send in FOF_MULTIDESTFILES with too many destination files */
615     init_shfo_tests();
616     shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0";
617     shfo.pTo = "testdir2\\a.txt\0testdir2\\b.txt\0testdir2\\c.txt\0testdir2\\d.txt\0";
618     shfo.fFlags |= FOF_NOERRORUI | FOF_MULTIDESTFILES;
619     retval = SHFileOperation(&shfo);
620         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
621     ok(shfo.fAnyOperationsAborted, "Expected aborted operations\n");
622     ok(!file_exists("testdir2\\a.txt"), "Expected testdir2\\a.txt to not exist\n");
623
624     /* send in FOF_MULTIDESTFILES with too many destination files */
625     shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0";
626     shfo.pTo = "e.txt\0f.txt\0";
627     shfo.fAnyOperationsAborted = FALSE;
628     retval = SHFileOperation(&shfo);
629         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
630     ok(shfo.fAnyOperationsAborted, "Expected aborted operations\n");
631     ok(!file_exists("e.txt"), "Expected e.txt to not exist\n");
632
633     /* use FOF_MULTIDESTFILES with files and a source directory */
634     shfo.pFrom = "test1.txt\0test2.txt\0test4.txt\0";
635     shfo.pTo = "testdir2\\a.txt\0testdir2\\b.txt\0testdir2\\c.txt\0";
636     shfo.fAnyOperationsAborted = FALSE;
637     retval = SHFileOperation(&shfo);
638     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
639     ok(DeleteFile("testdir2\\a.txt"), "Expected testdir2\\a.txt to exist\n");
640     ok(DeleteFile("testdir2\\b.txt"), "Expected testdir2\\b.txt to exist\n");
641     ok(RemoveDirectory("testdir2\\c.txt"), "Expected testdir2\\c.txt to exist\n");
642
643     /* try many dest files without FOF_MULTIDESTFILES flag */
644     shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0";
645     shfo.pTo = "a.txt\0b.txt\0c.txt\0";
646     shfo.fAnyOperationsAborted = FALSE;
647     shfo.fFlags &= ~FOF_MULTIDESTFILES;
648     retval = SHFileOperation(&shfo);
649         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
650     ok(!file_exists("a.txt"), "Expected a.txt to not exist\n");
651
652     /* try a glob */
653     shfo.pFrom = "test?.txt\0";
654     shfo.pTo = "testdir2\0";
655     shfo.fFlags &= ~FOF_MULTIDESTFILES;
656     retval = SHFileOperation(&shfo);
657         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
658         ok(file_exists("testdir2\\test1.txt"), "Expected testdir2\\test1.txt to exist\n");
659
660     /* try a glob with FOF_FILESONLY */
661     clean_after_shfo_tests();
662     init_shfo_tests();
663     shfo.pFrom = "test?.txt\0";
664     shfo.fFlags |= FOF_FILESONLY;
665     retval = SHFileOperation(&shfo);
666         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
667         ok(file_exists("testdir2\\test1.txt"), "Expected testdir2\\test1.txt to exist\n");
668     ok(!file_exists("testdir2\\test4.txt"), "Expected testdir2\\test4.txt to not exist\n");
669
670     /* try a glob with FOF_MULTIDESTFILES and the same number
671     * of dest files that we would expect
672     */
673     clean_after_shfo_tests();
674     init_shfo_tests();
675     shfo.pTo = "testdir2\\a.txt\0testdir2\\b.txt\0testdir2\\c.txt\0testdir2\\d.txt\0";
676     shfo.fFlags &= ~FOF_FILESONLY;
677     shfo.fFlags |= FOF_MULTIDESTFILES;
678     retval = SHFileOperation(&shfo);
679         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
680     ok(shfo.fAnyOperationsAborted, "Expected aborted operations\n");
681     ok(!file_exists("testdir2\\a.txt"), "Expected testdir2\\test1.txt to not exist\n");
682     ok(!RemoveDirectory("b.txt"), "b.txt should not exist\n");
683
684     /* copy one file to two others, second is ignored */
685     clean_after_shfo_tests();
686     init_shfo_tests();
687     shfo.pFrom = "test1.txt\0";
688     shfo.pTo = "b.txt\0c.txt\0";
689     shfo.fAnyOperationsAborted = FALSE;
690     retval = SHFileOperation(&shfo);
691         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
692         ok(DeleteFile("b.txt"), "Expected b.txt to exist\n");
693     ok(!DeleteFile("c.txt"), "Expected c.txt to not exist\n");
694
695     /* copy two file to three others, all fail */
696     shfo.pFrom = "test1.txt\0test2.txt\0";
697     shfo.pTo = "b.txt\0c.txt\0d.txt\0";
698     retval = SHFileOperation(&shfo);
699         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
700     ok(shfo.fAnyOperationsAborted, "Expected operations to be aborted\n");
701     ok(!DeleteFile("b.txt"), "Expected b.txt to not exist\n");
702
703     /* copy one file and one directory to three others */
704     shfo.pFrom = "test1.txt\0test4.txt\0";
705     shfo.pTo = "b.txt\0c.txt\0d.txt\0";
706     shfo.fAnyOperationsAborted = FALSE;
707     retval = SHFileOperation(&shfo);
708         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
709     ok(shfo.fAnyOperationsAborted, "Expected operations to be aborted\n");
710     ok(!DeleteFile("b.txt"), "Expected b.txt to not exist\n");
711     ok(!DeleteFile("c.txt"), "Expected c.txt to not exist\n");
712
713     /* copy a directory with a file beneath it, plus some files */
714     createTestFile("test4.txt\\a.txt");
715     shfo.pFrom = "test4.txt\0test1.txt\0";
716     shfo.pTo = "testdir2\0";
717     shfo.fFlags &= ~FOF_MULTIDESTFILES;
718     shfo.fAnyOperationsAborted = FALSE;
719     retval = SHFileOperation(&shfo);
720     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
721     ok(DeleteFile("testdir2\\test1.txt"), "Expected newdir\\test1.txt to exist\n");
722     ok(DeleteFile("testdir2\\test4.txt\\a.txt"), "Expected a.txt to exist\n");
723     ok(RemoveDirectory("testdir2\\test4.txt"), "Expected testdir2\\test4.txt to exist\n");
724
725     /* copy one directory and a file in that dir to another dir */
726     shfo.pFrom = "test4.txt\0test4.txt\\a.txt\0";
727     shfo.pTo = "testdir2\0";
728     retval = SHFileOperation(&shfo);
729     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
730     ok(DeleteFile("testdir2\\test4.txt\\a.txt"), "Expected a.txt to exist\n");
731     ok(DeleteFile("testdir2\\a.txt"), "Expected testdir2\\a.txt to exist\n");
732
733     /* copy a file in a directory first, and then the directory to a nonexistent dir */
734     shfo.pFrom = "test4.txt\\a.txt\0test4.txt\0";
735     shfo.pTo = "nonexistent\0";
736     retval = SHFileOperation(&shfo);
737         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
738     ok(shfo.fAnyOperationsAborted, "Expected operations to be aborted\n");
739     ok(!file_exists("nonexistent\\test4.txt"), "Expected nonexistent\\test4.txt to not exist\n");
740     DeleteFile("test4.txt\\a.txt");
741
742     /* destination is same as source file */
743     shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0";
744     shfo.pTo = "b.txt\0test2.txt\0c.txt\0";
745     shfo.fAnyOperationsAborted = FALSE;
746     shfo.fFlags = FOF_NOERRORUI | FOF_MULTIDESTFILES;
747     retval = SHFileOperation(&shfo);
748         ok(retval == ERROR_NO_MORE_SEARCH_HANDLES,
749            "Expected ERROR_NO_MORE_SEARCH_HANDLES, got %d\n", retval);
750         ok(!shfo.fAnyOperationsAborted, "Expected no operations to be aborted\n");
751         ok(DeleteFile("b.txt"), "Expected b.txt to exist\n");
752     ok(!file_exists("c.txt"), "Expected c.txt to not exist\n");
753
754     /* destination is same as source directory */
755     shfo.pFrom = "test1.txt\0test4.txt\0test3.txt\0";
756     shfo.pTo = "b.txt\0test4.txt\0c.txt\0";
757     shfo.fAnyOperationsAborted = FALSE;
758     retval = SHFileOperation(&shfo);
759         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
760         ok(DeleteFile("b.txt"), "Expected b.txt to exist\n");
761     ok(!file_exists("c.txt"), "Expected c.txt to not exist\n");
762
763     /* copy a directory into itself, error displayed in UI */
764     shfo.pFrom = "test4.txt\0";
765     shfo.pTo = "test4.txt\\newdir\0";
766     shfo.fFlags &= ~FOF_MULTIDESTFILES;
767     shfo.fAnyOperationsAborted = FALSE;
768     retval = SHFileOperation(&shfo);
769         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
770     ok(!RemoveDirectory("test4.txt\\newdir"), "Expected test4.txt\\newdir to not exist\n");
771
772     /* copy a directory to itself, error displayed in UI */
773     shfo.pFrom = "test4.txt\0";
774     shfo.pTo = "test4.txt\0";
775     shfo.fAnyOperationsAborted = FALSE;
776     retval = SHFileOperation(&shfo);
777         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
778
779     /* copy a file into a directory, and the directory into itself */
780     shfo.pFrom = "test1.txt\0test4.txt\0";
781     shfo.pTo = "test4.txt\0";
782     shfo.fAnyOperationsAborted = FALSE;
783     shfo.fFlags |= FOF_NOCONFIRMATION;
784     retval = SHFileOperation(&shfo);
785         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
786     ok(DeleteFile("test4.txt\\test1.txt"), "Expected test4.txt\\test1.txt to exist\n");
787
788     /* copy a file to a file, and the directory into itself */
789     shfo.pFrom = "test1.txt\0test4.txt\0";
790     shfo.pTo = "test4.txt\\a.txt\0";
791     shfo.fAnyOperationsAborted = FALSE;
792     retval = SHFileOperation(&shfo);
793         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
794     ok(!file_exists("test4.txt\\a.txt"), "Expected test4.txt\\a.txt to not exist\n");
795
796     /* copy a nonexistent file to a nonexistent directory */
797     shfo.pFrom = "e.txt\0";
798     shfo.pTo = "nonexistent\0";
799     shfo.fAnyOperationsAborted = FALSE;
800     retval = SHFileOperation(&shfo);
801     ok(retval == 1026, "Expected 1026, got %d\n", retval);
802     ok(!file_exists("nonexistent\\e.txt"), "Expected nonexistent\\e.txt to not exist\n");
803     ok(!file_exists("nonexistent"), "Expected nonexistent to not exist\n");
804
805     /* Overwrite tests */
806     clean_after_shfo_tests();
807     init_shfo_tests();
808     shfo.fFlags = FOF_NOCONFIRMATION;
809     shfo.pFrom = "test1.txt\0";
810     shfo.pTo = "test2.txt\0";
811     shfo.fAnyOperationsAborted = FALSE;
812     /* without FOF_NOCONFIRMATION the confirmation is Yes/No */
813     retval = SHFileOperation(&shfo);
814     ok(retval == 0, "Expected 0, got %d\n", retval);
815     ok(file_has_content("test2.txt", "test1.txt\n"), "The file was not copied\n");
816
817     shfo.pFrom = "test3.txt\0test1.txt\0";
818     shfo.pTo = "test2.txt\0one.txt\0";
819     shfo.fFlags = FOF_NOCONFIRMATION | FOF_MULTIDESTFILES;
820     /* without FOF_NOCONFIRMATION the confirmation is Yes/Yes to All/No/Cancel */
821     retval = SHFileOperation(&shfo);
822     ok(retval == 0, "Expected 0, got %d\n", retval);
823     ok(file_has_content("test2.txt", "test3.txt\n"), "The file was not copied\n");
824
825     shfo.pFrom = "one.txt\0";
826     shfo.pTo = "testdir2\0";
827     shfo.fFlags = FOF_NOCONFIRMATION;
828     /* without FOF_NOCONFIRMATION the confirmation is Yes/No */
829     retval = SHFileOperation(&shfo);
830     ok(retval == 0, "Expected 0, got %d\n", retval);
831     ok(file_has_content("testdir2\\one.txt", "test1.txt\n"), "The file was not copied\n");
832
833     createTestFile("test4.txt\\test1.txt");
834     shfo.pFrom = "test4.txt\0";
835     shfo.pTo = "testdir2\0";
836     shfo.fFlags = FOF_NOCONFIRMATION;
837     ok(!SHFileOperation(&shfo), "First SHFileOperation failed\n");
838     createTestFile("test4.txt\\.\\test1.txt"); /* modify the content of the file */
839     /* without FOF_NOCONFIRMATION the confirmation is "This folder already contains a folder named ..." */
840     retval = SHFileOperation(&shfo);
841     ok(retval == 0, "Expected 0, got %d\n", retval);
842     ok(file_has_content("testdir2\\test4.txt\\test1.txt", "test4.txt\\.\\test1.txt\n"), "The file was not copied\n");
843
844     createTestFile("one.txt");
845
846     /* no double-NULL terminator for pFrom */
847     memset(from, 'a', MAX_PATH);
848     lstrcpyA(from, "one.txt");
849     shfo.pFrom = from;
850     shfo.pTo = "two.txt\0";
851     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
852     retval = SHFileOperation(&shfo);
853     ok(retval == 1148 || retval == 1026, "Expected 1148 or 1026, got %d\n", retval);
854     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
855     ok(!DeleteFileA("two.txt"), "Expected file to not exist\n");
856
857     createTestFile("one.txt");
858
859     /* no double-NULL terminator for pTo */
860     memset(to, 'a', MAX_PATH);
861     lstrcpyA(to, "two.txt");
862     shfo.pFrom = "one.txt\0";
863     shfo.pTo = to;
864     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
865     retval = SHFileOperation(&shfo);
866     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
867     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
868     ok(DeleteFileA("two.txt"), "Expected file to exist\n");
869
870     createTestFile("one.txt");
871
872     /* no FOF_MULTIDESTFILES, two files in pTo */
873     shfo.pFrom = "one.txt\0";
874     shfo.pTo = "two.txt\0three.txt\0";
875     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
876     retval = SHFileOperation(&shfo);
877     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
878     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
879     ok(DeleteFileA("two.txt"), "Expected file to exist\n");
880
881     createTestFile("one.txt");
882
883     /* no double-NULL terminator for pFrom and pTo */
884     memset(from, 'a', MAX_PATH);
885     memset(to, 'a', MAX_PATH);
886     lstrcpyA(from, "one.txt");
887     lstrcpyA(to, "two.txt");
888     shfo.pFrom = from;
889     shfo.pTo = to;
890     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
891     retval = SHFileOperation(&shfo);
892     ok(retval == 1148 || retval == 1026, "Expected 1148 or 1026, got %d\n", retval);
893     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
894     ok(!DeleteFileA("two.txt"), "Expected file to not exist\n");
895
896     createTestFile("one.txt");
897
898     /* no double-NULL terminator for pTo, FOF_MULTIDESTFILES */
899     memset(to, 'a', MAX_PATH);
900     lstrcpyA(to, "two.txt");
901     shfo.pFrom = "one.txt\0";
902     shfo.pTo = to;
903     shfo.fFlags = FOF_MULTIDESTFILES | FOF_NOCONFIRMATION |
904                   FOF_SILENT | FOF_NOERRORUI;
905     retval = SHFileOperation(&shfo);
906     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
907     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
908     ok(DeleteFileA("two.txt"), "Expected file to exist\n");
909
910     createTestFile("one.txt");
911     createTestFile("two.txt");
912
913     /* no double-NULL terminator for pTo,
914      * multiple source files,
915      * dest directory does not exist
916      */
917     memset(to, 'a', 2 * MAX_PATH);
918     lstrcpyA(to, "threedir");
919     shfo.pFrom = "one.txt\0two.txt\0";
920     shfo.pTo = to;
921     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
922     retval = SHFileOperation(&shfo);
923     ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
924     ok(!DeleteFileA("threedir\\one.txt"), "Expected file to not exist\n");
925     ok(!DeleteFileA("threedir\\two.txt"), "Expected file to not exist\n");
926     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
927     ok(DeleteFileA("two.txt"), "Expected file to exist\n");
928     ok(!DeleteFileA("threedir"), "Expected file to not exist\n");
929     ok(!RemoveDirectoryA("threedir"), "Expected dir to not exist\n");
930
931     createTestFile("one.txt");
932     createTestFile("two.txt");
933     CreateDirectoryA("threedir", NULL);
934
935     /* no double-NULL terminator for pTo,
936      * multiple source files,
937      * dest directory does exist
938      */
939     memset(to, 'a', 2 * MAX_PATH);
940     lstrcpyA(to, "threedir");
941     shfo.pFrom = "one.txt\0two.txt\0";
942     shfo.pTo = to;
943     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
944     retval = SHFileOperation(&shfo);
945     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
946     ok(DeleteFileA("threedir\\one.txt"), "Expected file to exist\n");
947     ok(DeleteFileA("threedir\\two.txt"), "Expected file to exist\n");
948     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
949     ok(DeleteFileA("two.txt"), "Expected file to exist\n");
950     ok(RemoveDirectoryA("threedir"), "Expected dir to exist\n");
951
952     createTestFile("one.txt");
953     createTestFile("two.txt");
954
955     /* no double-NULL terminator for pTo,
956      * multiple source files, FOF_MULTIDESTFILES
957      * dest dir does not exist
958      */
959     memset(to, 'a', 2 * MAX_PATH);
960     lstrcpyA(to, "threedir");
961     shfo.pFrom = "one.txt\0two.txt\0";
962     shfo.pTo = to;
963     shfo.fFlags = FOF_MULTIDESTFILES | FOF_NOCONFIRMATION |
964                   FOF_SILENT | FOF_NOERRORUI;
965     retval = SHFileOperation(&shfo);
966     ok(retval == ERROR_CANCELLED ||
967        retval == ERROR_SUCCESS, /* win2k3 */
968        "Expected ERROR_CANCELLED or ERROR_SUCCESS, got %d\n", retval);
969     ok(!DeleteFileA("threedir\\one.txt"), "Expected file to not exist\n");
970     ok(!DeleteFileA("threedir\\two.txt"), "Expected file to not exist\n");
971     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
972     ok(DeleteFileA("two.txt"), "Expected file to exist\n");
973     ok(!RemoveDirectoryA("threedir"), "Expected dir to not exist\n");
974
975     /* file exists in win2k */
976     DeleteFileA("threedir");
977
978     createTestFile("one.txt");
979     createTestFile("two.txt");
980     CreateDirectoryA("threedir", NULL);
981
982     /* no double-NULL terminator for pTo,
983      * multiple source files, FOF_MULTIDESTFILES
984      * dest dir does exist
985      */
986     memset(to, 'a', 2 * MAX_PATH);
987     lstrcpyA(to, "threedir");
988     ptr = to + lstrlenA(to) + 1;
989     lstrcpyA(ptr, "fourdir");
990     shfo.pFrom = "one.txt\0two.txt\0";
991     shfo.pTo = to;
992     shfo.fFlags = FOF_MULTIDESTFILES | FOF_NOCONFIRMATION |
993                   FOF_SILENT | FOF_NOERRORUI;
994     retval = SHFileOperation(&shfo);
995     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
996     ok(DeleteFileA("threedir\\one.txt"), "Expected file to exist\n");
997     ok(DeleteFileA("threedir\\two.txt"), "Expected file to exist\n");
998     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
999     ok(DeleteFileA("two.txt"), "Expected file to exist\n");
1000     ok(RemoveDirectoryA("threedir"), "Expected dir to exist\n");
1001     ok(!DeleteFileA("fourdir"), "Expected file to not exist\n");
1002     ok(!RemoveDirectoryA("fourdir"), "Expected dir to not exist\n");
1003
1004     createTestFile("one.txt");
1005     createTestFile("two.txt");
1006     CreateDirectoryA("threedir", NULL);
1007
1008     /* multiple source files, FOF_MULTIDESTFILES
1009      * multiple dest files, but first dest dir exists
1010      * num files in lists is equal
1011      */
1012     shfo.pFrom = "one.txt\0two.txt\0";
1013     shfo.pTo = "threedir\0fourdir\0";
1014     shfo.fFlags = FOF_MULTIDESTFILES | FOF_NOCONFIRMATION |
1015                   FOF_SILENT | FOF_NOERRORUI;
1016     retval = SHFileOperation(&shfo);
1017     ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
1018     ok(!DeleteFileA("threedir\\one.txt"), "Expected file to not exist\n");
1019     ok(!DeleteFileA("threedir\\two.txt"), "Expected file to not exist\n");
1020     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
1021     ok(DeleteFileA("two.txt"), "Expected file to exist\n");
1022     ok(RemoveDirectoryA("threedir"), "Expected dir to exist\n");
1023     ok(!DeleteFileA("fourdir"), "Expected file to not exist\n");
1024     ok(!RemoveDirectoryA("fourdir"), "Expected dit to not exist\n");
1025
1026     createTestFile("one.txt");
1027     createTestFile("two.txt");
1028     CreateDirectoryA("threedir", NULL);
1029
1030     /* multiple source files, FOF_MULTIDESTFILES
1031      * multiple dest files, but first dest dir exists
1032      * num files in lists is not equal
1033      */
1034     shfo.pFrom = "one.txt\0two.txt\0";
1035     shfo.pTo = "threedir\0fourdir\0five\0";
1036     shfo.fFlags = FOF_MULTIDESTFILES | FOF_NOCONFIRMATION |
1037                   FOF_SILENT | FOF_NOERRORUI;
1038     retval = SHFileOperation(&shfo);
1039     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1040     ok(DeleteFileA("threedir\\one.txt"), "Expected file to exist\n");
1041     ok(DeleteFileA("threedir\\two.txt"), "Expected file to exist\n");
1042     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
1043     ok(DeleteFileA("two.txt"), "Expected file to exist\n");
1044     ok(RemoveDirectoryA("threedir"), "Expected dir to exist\n");
1045     ok(!DeleteFileA("fourdir"), "Expected file to not exist\n");
1046     ok(!RemoveDirectoryA("fourdir"), "Expected dit to not exist\n");
1047     ok(!DeleteFileA("five"), "Expected file to not exist\n");
1048     ok(!RemoveDirectoryA("five"), "Expected dit to not exist\n");
1049
1050     createTestFile("aa.txt");
1051     createTestFile("ab.txt");
1052     CreateDirectoryA("one", NULL);
1053     CreateDirectoryA("two", NULL);
1054
1055     /* pFrom has a glob, pTo has more than one dest */
1056     shfo.pFrom = "a*.txt\0";
1057     shfo.pTo = "one\0two\0";
1058     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
1059     retval = SHFileOperation(&shfo);
1060     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1061     ok(DeleteFileA("one\\aa.txt"), "Expected file to exist\n");
1062     ok(DeleteFileA("one\\ab.txt"), "Expected file to exist\n");
1063     ok(!DeleteFileA("two\\aa.txt"), "Expected file to not exist\n");
1064     ok(!DeleteFileA("two\\ab.txt"), "Expected file to not exist\n");
1065     ok(DeleteFileA("aa.txt"), "Expected file to exist\n");
1066     ok(DeleteFileA("ab.txt"), "Expected file to exist\n");
1067     ok(RemoveDirectoryA("one"), "Expected dir to exist\n");
1068     ok(RemoveDirectoryA("two"), "Expected dir to exist\n");
1069 }
1070
1071 /* tests the FO_MOVE action */
1072 static void test_move(void)
1073 {
1074     SHFILEOPSTRUCTA shfo, shfo2;
1075     CHAR from[5*MAX_PATH];
1076     CHAR to[5*MAX_PATH];
1077     DWORD retval;
1078
1079     shfo.hwnd = NULL;
1080     shfo.wFunc = FO_MOVE;
1081     shfo.pFrom = from;
1082     shfo.pTo = to;
1083     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
1084     shfo.hNameMappings = NULL;
1085     shfo.lpszProgressTitle = NULL;
1086
1087     set_curr_dir_path(from, "test1.txt\0");
1088     set_curr_dir_path(to, "test4.txt\0");
1089     ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are moved recursively\n");
1090     ok(!file_exists("test1.txt"), "test1.txt should not exist\n");
1091     ok(file_exists("test4.txt\\test1.txt"), "The file is not moved\n");
1092
1093     set_curr_dir_path(from, "test?.txt\0");
1094     set_curr_dir_path(to, "testdir2\0");
1095     ok(!file_exists("testdir2\\test2.txt"), "The file is not moved yet\n");
1096     ok(!file_exists("testdir2\\test4.txt"), "The directory is not moved yet\n");
1097     ok(!SHFileOperationA(&shfo), "Files and directories are moved to directory\n");
1098     ok(file_exists("testdir2\\test2.txt"), "The file is moved\n");
1099     ok(file_exists("testdir2\\test4.txt"), "The directory is moved\n");
1100     ok(file_exists("testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is moved\n");
1101
1102     clean_after_shfo_tests();
1103     init_shfo_tests();
1104
1105     memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
1106     shfo2.fFlags |= FOF_MULTIDESTFILES;
1107
1108     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
1109     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
1110     ok(!SHFileOperationA(&shfo2), "Move many files\n");
1111     ok(file_exists("test6.txt"), "The file is moved - many files are "
1112        "specified as a target\n");
1113     DeleteFileA("test6.txt");
1114     DeleteFileA("test7.txt");
1115     RemoveDirectoryA("test8.txt");
1116
1117     init_shfo_tests();
1118
1119     /* number of sources do not correspond to number of targets */
1120     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
1121     set_curr_dir_path(to, "test6.txt\0test7.txt\0");
1122     ok(SHFileOperationA(&shfo2), "Can't move many files\n");
1123     ok(!file_exists("test6.txt"), "The file is not moved - many files are "
1124        "specified as a target\n");
1125
1126     init_shfo_tests();
1127
1128     set_curr_dir_path(from, "test3.txt\0");
1129     set_curr_dir_path(to, "test4.txt\\test1.txt\0");
1130     ok(!SHFileOperationA(&shfo), "File is moved moving to other directory\n");
1131     ok(file_exists("test4.txt\\test1.txt"), "The file is moved\n");
1132
1133     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
1134     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
1135     ok(SHFileOperationA(&shfo), "Cannot move many files\n");
1136     ok(file_exists("test1.txt"), "The file is not moved. Many files are specified\n");
1137     ok(file_exists("test4.txt"), "The directory is not moved. Many files are specified\n");
1138
1139     set_curr_dir_path(from, "test1.txt\0");
1140     set_curr_dir_path(to, "test6.txt\0");
1141     ok(!SHFileOperationA(&shfo), "Move file\n");
1142     ok(!file_exists("test1.txt"), "The file is moved\n");
1143     ok(file_exists("test6.txt"), "The file is moved\n");
1144     set_curr_dir_path(from, "test6.txt\0");
1145     set_curr_dir_path(to, "test1.txt\0");
1146     ok(!SHFileOperationA(&shfo), "Move file back\n");
1147
1148     set_curr_dir_path(from, "test4.txt\0");
1149     set_curr_dir_path(to, "test6.txt\0");
1150     ok(!SHFileOperationA(&shfo), "Move dir\n");
1151     ok(!file_exists("test4.txt"), "The dir is moved\n");
1152     ok(file_exists("test6.txt"), "The dir is moved\n");
1153     set_curr_dir_path(from, "test6.txt\0");
1154     set_curr_dir_path(to, "test4.txt\0");
1155     ok(!SHFileOperationA(&shfo), "Move dir back\n");
1156
1157     /* move one file to two others */
1158     init_shfo_tests();
1159     shfo.pFrom = "test1.txt\0";
1160     shfo.pTo = "a.txt\0b.txt\0";
1161     retval = SHFileOperationA(&shfo);
1162         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1163         ok(!file_exists("test1.txt"), "Expected test1.txt to not exist\n");
1164         ok(DeleteFile("a.txt"), "Expected a.txt to exist\n");
1165     ok(!file_exists("b.txt"), "Expected b.txt to not exist\n");
1166
1167     /* move two files to one other */
1168     shfo.pFrom = "test2.txt\0test3.txt\0";
1169     shfo.pTo = "test1.txt\0";
1170     retval = SHFileOperationA(&shfo);
1171         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
1172         ok(!file_exists("test1.txt"), "Expected test1.txt to not exist\n");
1173     ok(file_exists("test2.txt"), "Expected test2.txt to exist\n");
1174     ok(file_exists("test3.txt"), "Expected test3.txt to exist\n");
1175
1176     /* move a directory into itself */
1177     shfo.pFrom = "test4.txt\0";
1178     shfo.pTo = "test4.txt\\b.txt\0";
1179     retval = SHFileOperationA(&shfo);
1180         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1181     ok(!RemoveDirectory("test4.txt\\b.txt"), "Expected test4.txt\\b.txt to not exist\n");
1182     ok(file_exists("test4.txt"), "Expected test4.txt to exist\n");
1183
1184     /* move many files without FOF_MULTIDESTFILES */
1185     shfo.pFrom = "test2.txt\0test3.txt\0";
1186     shfo.pTo = "d.txt\0e.txt\0";
1187     retval = SHFileOperationA(&shfo);
1188         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
1189     ok(!DeleteFile("d.txt"), "Expected d.txt to not exist\n");
1190     ok(!DeleteFile("e.txt"), "Expected e.txt to not exist\n");
1191
1192     /* number of sources != number of targets */
1193     shfo.pTo = "d.txt\0";
1194     shfo.fFlags |= FOF_MULTIDESTFILES;
1195     retval = SHFileOperationA(&shfo);
1196         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
1197     ok(!DeleteFile("d.txt"), "Expected d.txt to not exist\n");
1198
1199     /* FO_MOVE does not create dest directories */
1200     shfo.pFrom = "test2.txt\0";
1201     shfo.pTo = "dir1\\dir2\\test2.txt\0";
1202     retval = SHFileOperationA(&shfo);
1203         ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %d\n", retval);
1204     ok(!file_exists("dir1"), "Expected dir1 to not exist\n");
1205
1206     /* try to overwrite an existing file */
1207     shfo.pTo = "test3.txt\0";
1208     retval = SHFileOperationA(&shfo);
1209         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1210         ok(!file_exists("test2.txt"), "Expected test2.txt to not exist\n");
1211     ok(file_exists("test3.txt"), "Expected test3.txt to exist\n");
1212 }
1213
1214 static void test_sh_create_dir(void)
1215 {
1216     CHAR path[MAX_PATH];
1217     int ret;
1218
1219     if(!pSHCreateDirectoryExA)
1220     {
1221         trace("skipping SHCreateDirectoryExA tests\n");
1222         return;
1223     }
1224
1225     set_curr_dir_path(path, "testdir2\\test4.txt\0");
1226     ret = pSHCreateDirectoryExA(NULL, path, NULL);
1227     ok(ERROR_SUCCESS == ret, "SHCreateDirectoryEx failed to create directory recursively, ret = %d\n", ret);
1228     ok(file_exists("testdir2"), "The first directory is not created\n");
1229     ok(file_exists("testdir2\\test4.txt"), "The second directory is not created\n");
1230
1231     ret = pSHCreateDirectoryExA(NULL, path, NULL);
1232     ok(ERROR_ALREADY_EXISTS == ret, "SHCreateDirectoryEx should fail to create existing directory, ret = %d\n", ret);
1233
1234     ret = pSHCreateDirectoryExA(NULL, "c:\\testdir3", NULL);
1235     ok(file_exists("c:\\testdir3"), "The directory is not created\n");
1236 }
1237
1238 static void test_sh_path_prepare(void)
1239 {
1240     HRESULT res;
1241     CHAR path[MAX_PATH];
1242
1243     if(!pSHPathPrepareForWriteA)
1244     {
1245         trace("skipping SHPathPrepareForWriteA tests\n");
1246             return;
1247     }
1248
1249     /* directory exists, SHPPFW_NONE */
1250     set_curr_dir_path(path, "testdir2\0");
1251     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_NONE);
1252     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
1253
1254     /* directory exists, SHPPFW_IGNOREFILENAME */
1255     set_curr_dir_path(path, "testdir2\\test4.txt\0");
1256     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_IGNOREFILENAME);
1257     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
1258
1259     /* directory exists, SHPPFW_DIRCREATE */
1260     set_curr_dir_path(path, "testdir2\0");
1261     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_DIRCREATE);
1262     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
1263
1264     /* directory exists, SHPPFW_IGNOREFILENAME|SHPPFW_DIRCREATE */
1265     set_curr_dir_path(path, "testdir2\\test4.txt\0");
1266     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_IGNOREFILENAME|SHPPFW_DIRCREATE);
1267     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
1268     ok(!file_exists("nonexistent\\"), "nonexistent\\ exists but shouldn't\n");
1269
1270     /* file exists, SHPPFW_NONE */
1271     set_curr_dir_path(path, "test1.txt\0");
1272     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_NONE);
1273     ok(res == HRESULT_FROM_WIN32(ERROR_DIRECTORY), "res == 0x%08x, expected HRESULT_FROM_WIN32(ERROR_DIRECTORY)\n", res);
1274
1275     /* file exists, SHPPFW_DIRCREATE */
1276     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_DIRCREATE);
1277     ok(res == HRESULT_FROM_WIN32(ERROR_DIRECTORY), "res == 0x%08x, expected HRESULT_FROM_WIN32(ERROR_DIRECTORY)\n", res);
1278
1279     /* file exists, SHPPFW_NONE, trailing \ */
1280     set_curr_dir_path(path, "test1.txt\\\0");
1281     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_NONE);
1282     ok(res == HRESULT_FROM_WIN32(ERROR_DIRECTORY), "res == 0x%08x, expected HRESULT_FROM_WIN32(ERROR_DIRECTORY)\n", res);
1283
1284     /* relative path exists, SHPPFW_DIRCREATE */
1285     res = pSHPathPrepareForWriteA(0, 0, ".\\testdir2", SHPPFW_DIRCREATE);
1286     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
1287
1288     /* relative path doesn't exist, SHPPFW_DIRCREATE -- Windows does not create the directory in this case */
1289     res = pSHPathPrepareForWriteA(0, 0, ".\\testdir2\\test4.txt", SHPPFW_DIRCREATE);
1290     ok(res == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), "res == 0x%08x, expected HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)\n", res);
1291     ok(!file_exists(".\\testdir2\\test4.txt\\"), ".\\testdir2\\test4.txt\\ exists but shouldn't\n");
1292
1293     /* directory doesn't exist, SHPPFW_NONE */
1294     set_curr_dir_path(path, "nonexistent\0");
1295     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_NONE);
1296     ok(res == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), "res == 0x%08x, expected HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)\n", res);
1297
1298     /* directory doesn't exist, SHPPFW_IGNOREFILENAME */
1299     set_curr_dir_path(path, "nonexistent\\notreal\0");
1300     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_IGNOREFILENAME);
1301     ok(res == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), "res == 0x%08x, expected HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)\n", res);
1302     ok(!file_exists("nonexistent\\notreal"), "nonexistent\\notreal exists but shouldn't\n");
1303     ok(!file_exists("nonexistent\\"), "nonexistent\\ exists but shouldn't\n");
1304
1305     /* directory doesn't exist, SHPPFW_IGNOREFILENAME|SHPPFW_DIRCREATE */
1306     set_curr_dir_path(path, "testdir2\\test4.txt\\\0");
1307     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_IGNOREFILENAME|SHPPFW_DIRCREATE);
1308     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
1309     ok(file_exists("testdir2\\test4.txt\\"), "testdir2\\test4.txt doesn't exist but should\n");
1310
1311     /* nested directory doesn't exist, SHPPFW_DIRCREATE */
1312     set_curr_dir_path(path, "nonexistent\\notreal\0");
1313     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_DIRCREATE);
1314     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
1315     ok(file_exists("nonexistent\\notreal"), "nonexistent\\notreal doesn't exist but should\n");
1316
1317     /* SHPPFW_ASKDIRCREATE, SHPPFW_NOWRITECHECK, and SHPPFW_MEDIACHECKONLY are untested */
1318
1319     if(!pSHPathPrepareForWriteW)
1320     {
1321         skip("Skipping SHPathPrepareForWriteW tests\n");
1322         return;
1323     }
1324     /* unicode directory doesn't exist, SHPPFW_NONE */
1325     res = pSHPathPrepareForWriteW(0, 0, UNICODE_PATH, SHPPFW_NONE);
1326     ok(res == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), "res == %08x, expected HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)\n", res);
1327     ok(!file_existsW(UNICODE_PATH), "unicode path was created but shouldn't be\n");
1328     RemoveDirectoryW(UNICODE_PATH);
1329
1330     /* unicode directory doesn't exist, SHPPFW_DIRCREATE */
1331     res = pSHPathPrepareForWriteW(0, 0, UNICODE_PATH, SHPPFW_DIRCREATE);
1332     ok(res == S_OK, "res == %08x, expected S_OK\n", res);
1333     ok(file_existsW(UNICODE_PATH), "unicode path should've been created\n");
1334
1335     /* unicode directory exists, SHPPFW_NONE */
1336     res = pSHPathPrepareForWriteW(0, 0, UNICODE_PATH, SHPPFW_NONE);
1337     ok(res == S_OK, "ret == %08x, expected S_OK\n", res);
1338
1339     /* unicode directory exists, SHPPFW_DIRCREATE */
1340     res = pSHPathPrepareForWriteW(0, 0, UNICODE_PATH, SHPPFW_DIRCREATE);
1341     ok(res == S_OK, "ret == %08x, expected S_OK\n", res);
1342     RemoveDirectoryW(UNICODE_PATH);
1343 }
1344
1345 static void test_unicode(void)
1346 {
1347     SHFILEOPSTRUCTW shfoW;
1348     int ret;
1349     HANDLE file;
1350
1351     if (!pSHFileOperationW)
1352     {
1353         skip("SHFileOperationW() is missing\n");
1354         return;
1355     }
1356
1357     shfoW.hwnd = NULL;
1358     shfoW.wFunc = FO_DELETE;
1359     shfoW.pFrom = UNICODE_PATH;
1360     shfoW.pTo = '\0';
1361     shfoW.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
1362     shfoW.hNameMappings = NULL;
1363     shfoW.lpszProgressTitle = NULL;
1364
1365     /* Clean up before start test */
1366     DeleteFileW(UNICODE_PATH);
1367     RemoveDirectoryW(UNICODE_PATH);
1368
1369     /* Make sure we are on a system that supports unicode */
1370     SetLastError(0xdeadbeef);
1371     file = CreateFileW(UNICODE_PATH, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
1372     if (GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
1373     {
1374         skip("Unicode tests skipped on non-unicode system\n");
1375         return;
1376     }
1377     CloseHandle(file);
1378
1379     /* Try to delete a file with unicode filename */
1380     ok(file_existsW(UNICODE_PATH), "The file does not exist\n");
1381     ret = pSHFileOperationW(&shfoW);
1382     ok(!ret, "File is not removed, ErrorCode: %d\n", ret);
1383     ok(!file_existsW(UNICODE_PATH), "The file should have been removed\n");
1384
1385     /* Try to trash a file with unicode filename */
1386     createTestFileW(UNICODE_PATH);
1387     shfoW.fFlags |= FOF_ALLOWUNDO;
1388     ok(file_existsW(UNICODE_PATH), "The file does not exist\n");
1389     ret = pSHFileOperationW(&shfoW);
1390     ok(!ret, "File is not removed, ErrorCode: %d\n", ret);
1391     ok(!file_existsW(UNICODE_PATH), "The file should have been removed\n");
1392
1393     if(!pSHCreateDirectoryExW)
1394     {
1395         skip("Skipping SHCreateDirectoryExW tests\n");
1396         return;
1397     }
1398
1399     /* Try to delete a directory with unicode filename */
1400     ret = pSHCreateDirectoryExW(NULL, UNICODE_PATH, NULL);
1401     ok(!ret, "SHCreateDirectoryExW returned %d\n", ret);
1402     ok(file_existsW(UNICODE_PATH), "The directory is not created\n");
1403     shfoW.fFlags &= ~FOF_ALLOWUNDO;
1404     ret = pSHFileOperationW(&shfoW);
1405     ok(!ret, "Directory is not removed, ErrorCode: %d\n", ret);
1406     ok(!file_existsW(UNICODE_PATH), "The directory should have been removed\n");
1407
1408     /* Try to trash a directory with unicode filename */
1409     ret = pSHCreateDirectoryExW(NULL, UNICODE_PATH, NULL);
1410     ok(!ret, "SHCreateDirectoryExW returned %d\n", ret);
1411     ok(file_existsW(UNICODE_PATH), "The directory was not created\n");
1412     shfoW.fFlags |= FOF_ALLOWUNDO;
1413     ret = pSHFileOperationW(&shfoW);
1414     ok(!ret, "Directory is not removed, ErrorCode: %d\n", ret);
1415     ok(!file_existsW(UNICODE_PATH), "The directory should have been removed\n");
1416 }
1417
1418 START_TEST(shlfileop)
1419 {
1420     InitFunctionPointers();
1421
1422     clean_after_shfo_tests();
1423
1424     init_shfo_tests();
1425     test_get_file_info();
1426     clean_after_shfo_tests();
1427
1428     init_shfo_tests();
1429     test_delete();
1430     clean_after_shfo_tests();
1431
1432     init_shfo_tests();
1433     test_rename();
1434     clean_after_shfo_tests();
1435
1436     init_shfo_tests();
1437     test_copy();
1438     clean_after_shfo_tests();
1439
1440     init_shfo_tests();
1441     test_move();
1442     clean_after_shfo_tests();
1443
1444     test_sh_create_dir();
1445     clean_after_shfo_tests();
1446
1447     init_shfo_tests();
1448     test_sh_path_prepare();
1449     clean_after_shfo_tests();
1450
1451     test_unicode();
1452 }