shell32: Store the autocompletion object pointer in a window property rather than...
[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 /* Error codes could be pre-Win32 */
36 #define DE_SAMEFILE      0x71
37 #define DE_MANYSRC1DEST  0x72
38 #define DE_DIFFDIR       0x73
39 #define DE_OPCANCELLED   0x75
40 #define DE_DESTSUBTREE   0x76
41 #define DE_INVALIDFILES  0x7C
42 #define DE_DESTSAMETREE  0x7D
43 #define DE_FLDDESTISFILE 0x7E
44 #define DE_FILEDESTISFLD 0x80
45 #define expect_retval(ret, ret_prewin32)\
46     ok(retval == ret ||\
47        broken(retval == ret_prewin32),\
48        "Expected %d, got %d\n", ret, retval)
49
50 static BOOL old_shell32 = FALSE;
51
52 static CHAR CURR_DIR[MAX_PATH];
53 static const WCHAR UNICODE_PATH[] = {'c',':','\\',0x00ae,'\0','\0'};
54     /* "c:\®" can be used in all codepages */
55     /* Double-null termination needed for pFrom field of SHFILEOPSTRUCT */
56
57 static HMODULE hshell32;
58 static int (WINAPI *pSHCreateDirectoryExA)(HWND, LPCSTR, LPSECURITY_ATTRIBUTES);
59 static int (WINAPI *pSHCreateDirectoryExW)(HWND, LPCWSTR, LPSECURITY_ATTRIBUTES);
60 static int (WINAPI *pSHFileOperationW)(LPSHFILEOPSTRUCTW);
61 static DWORD_PTR (WINAPI *pSHGetFileInfoW)(LPCWSTR, DWORD , SHFILEINFOW*, UINT, UINT);
62 static int (WINAPI *pSHPathPrepareForWriteA)(HWND, IUnknown*, LPCSTR, DWORD);
63 static int (WINAPI *pSHPathPrepareForWriteW)(HWND, IUnknown*, LPCWSTR, DWORD);
64
65 static void InitFunctionPointers(void)
66 {
67     hshell32 = GetModuleHandleA("shell32.dll");
68     pSHCreateDirectoryExA = (void*)GetProcAddress(hshell32, "SHCreateDirectoryExA");
69     pSHCreateDirectoryExW = (void*)GetProcAddress(hshell32, "SHCreateDirectoryExW");
70     pSHFileOperationW = (void*)GetProcAddress(hshell32, "SHFileOperationW");
71     pSHGetFileInfoW = (void*)GetProcAddress(hshell32, "SHGetFileInfoW");
72     pSHPathPrepareForWriteA = (void*)GetProcAddress(hshell32, "SHPathPrepareForWriteA");
73     pSHPathPrepareForWriteW = (void*)GetProcAddress(hshell32, "SHPathPrepareForWriteW");
74 }
75
76 /* creates a file with the specified name for tests */
77 static void createTestFile(const CHAR *name)
78 {
79     HANDLE file;
80     DWORD written;
81
82     file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
83     ok(file != INVALID_HANDLE_VALUE, "Failure to open file %s\n", name);
84     WriteFile(file, name, strlen(name), &written, NULL);
85     WriteFile(file, "\n", strlen("\n"), &written, NULL);
86     CloseHandle(file);
87 }
88
89 static void createTestFileW(const WCHAR *name)
90 {
91     HANDLE file;
92
93     file = CreateFileW(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
94     ok(file != INVALID_HANDLE_VALUE, "Failure to open file\n");
95     CloseHandle(file);
96 }
97
98 static BOOL file_exists(const CHAR *name)
99 {
100     return GetFileAttributesA(name) != INVALID_FILE_ATTRIBUTES;
101 }
102
103 static BOOL dir_exists(const CHAR *name)
104 {
105     DWORD attr;
106     BOOL dir;
107
108     attr = GetFileAttributesA(name);
109     dir = ((attr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
110
111     return ((attr != INVALID_FILE_ATTRIBUTES) && dir);
112 }
113
114 static BOOL file_existsW(LPCWSTR name)
115 {
116   return GetFileAttributesW(name) != INVALID_FILE_ATTRIBUTES;
117 }
118
119 static BOOL file_has_content(const CHAR *name, const CHAR *content)
120 {
121     CHAR buf[MAX_PATH];
122     HANDLE file;
123     DWORD read;
124
125     file = CreateFileA(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
126     if (file == INVALID_HANDLE_VALUE)
127         return FALSE;
128     ReadFile(file, buf, MAX_PATH - 1, &read, NULL);
129     buf[read] = 0;
130     CloseHandle(file);
131     return strcmp(buf, content)==0;
132 }
133
134 /* initializes the tests */
135 static void init_shfo_tests(void)
136 {
137     int len;
138
139     GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
140     len = lstrlenA(CURR_DIR);
141
142     if(len && (CURR_DIR[len-1] == '\\'))
143         CURR_DIR[len-1] = 0;
144
145     createTestFile("test1.txt");
146     createTestFile("test2.txt");
147     createTestFile("test3.txt");
148     createTestFile("test_5.txt");
149     CreateDirectoryA("test4.txt", NULL);
150     CreateDirectoryA("testdir2", NULL);
151     CreateDirectoryA("testdir2\\nested", NULL);
152     createTestFile("testdir2\\one.txt");
153     createTestFile("testdir2\\nested\\two.txt");
154 }
155
156 /* cleans after tests */
157 static void clean_after_shfo_tests(void)
158 {
159     DeleteFileA("test1.txt");
160     DeleteFileA("test2.txt");
161     DeleteFileA("test3.txt");
162     DeleteFileA("test_5.txt");
163     DeleteFileA("one.txt");
164     DeleteFileA("test4.txt\\test1.txt");
165     DeleteFileA("test4.txt\\test2.txt");
166     DeleteFileA("test4.txt\\test3.txt");
167     RemoveDirectoryA("test4.txt");
168     DeleteFileA("testdir2\\one.txt");
169     DeleteFileA("testdir2\\test1.txt");
170     DeleteFileA("testdir2\\test2.txt");
171     DeleteFileA("testdir2\\test3.txt");
172     DeleteFileA("testdir2\\test4.txt\\test1.txt");
173     DeleteFileA("testdir2\\nested\\two.txt");
174     RemoveDirectoryA("testdir2\\test4.txt");
175     RemoveDirectoryA("testdir2\\nested");
176     RemoveDirectoryA("testdir2");
177     RemoveDirectoryA("c:\\testdir3");
178     DeleteFileA("nonexistent\\notreal\\test2.txt");
179     RemoveDirectoryA("nonexistent\\notreal");
180     RemoveDirectoryA("nonexistent");
181 }
182
183
184 static void test_get_file_info(void)
185 {
186     DWORD rc, rc2;
187     SHFILEINFOA shfi, shfi2;
188     SHFILEINFOW shfiw;
189     char notepad[MAX_PATH];
190
191     /* Test whether fields of SHFILEINFOA are always cleared */
192     memset(&shfi, 0xcf, sizeof(shfi));
193     rc=SHGetFileInfoA("", 0, &shfi, sizeof(shfi), 0);
194     ok(rc == 1, "SHGetFileInfoA('' | 0) should return 1, got 0x%x\n", rc);
195     todo_wine ok(shfi.hIcon == 0, "SHGetFileInfoA('' | 0) did not clear hIcon\n");
196     todo_wine ok(shfi.szDisplayName[0] == 0, "SHGetFileInfoA('' | 0) did not clear szDisplayName[0]\n");
197     todo_wine ok(shfi.szTypeName[0] == 0, "SHGetFileInfoA('' | 0) did not clear szTypeName[0]\n");
198     ok(shfi.iIcon == 0xcfcfcfcf ||
199        broken(shfi.iIcon != 0xcfcfcfcf), /* NT4 doesn't clear but sets this field */
200        "SHGetFileInfoA('' | 0) should not clear iIcon\n");
201     ok(shfi.dwAttributes == 0xcfcfcfcf ||
202        broken(shfi.dwAttributes != 0xcfcfcfcf), /* NT4 doesn't clear but sets this field */
203        "SHGetFileInfoA('' | 0) should not clear dwAttributes\n");
204
205     if (pSHGetFileInfoW)
206     {
207         HANDLE unset_icon;
208         /* Test whether fields of SHFILEINFOW are always cleared */
209         memset(&shfiw, 0xcf, sizeof(shfiw));
210         memset(&unset_icon, 0xcf, sizeof(unset_icon));
211         rc=pSHGetFileInfoW(NULL, 0, &shfiw, sizeof(shfiw), 0);
212         ok(!rc, "SHGetFileInfoW(NULL | 0) should fail\n");
213         ok(shfiw.hIcon == unset_icon, "SHGetFileInfoW(NULL | 0) should not clear hIcon\n");
214         ok(shfiw.szDisplayName[0] == 0xcfcf, "SHGetFileInfoW(NULL | 0) should not clear szDisplayName[0]\n");
215         ok(shfiw.szTypeName[0] == 0xcfcf, "SHGetFileInfoW(NULL | 0) should not clear szTypeName[0]\n");
216         ok(shfiw.iIcon == 0xcfcfcfcf, "SHGetFileInfoW(NULL | 0) should not clear iIcon\n");
217         ok(shfiw.dwAttributes == 0xcfcfcfcf, "SHGetFileInfoW(NULL | 0) should not clear dwAttributes\n");
218     }
219     else
220         win_skip("SHGetFileInfoW is not available\n");
221
222
223     /* Test some flag combinations that MSDN claims are not allowed,
224      * but which work anyway
225      */
226     memset(&shfi, 0xcf, sizeof(shfi));
227     rc=SHGetFileInfoA("c:\\nonexistent", FILE_ATTRIBUTE_DIRECTORY,
228                       &shfi, sizeof(shfi),
229                       SHGFI_ATTRIBUTES | SHGFI_USEFILEATTRIBUTES);
230     ok(rc == 1, "SHGetFileInfoA(c:\\nonexistent | SHGFI_ATTRIBUTES) should return 1, got 0x%x\n", rc);
231     if (rc)
232         ok(shfi.dwAttributes != 0xcfcfcfcf, "dwFileAttributes is not set\n");
233     todo_wine ok(shfi.hIcon == 0, "SHGetFileInfoA(c:\\nonexistent | SHGFI_ATTRIBUTES) did not clear hIcon\n");
234     todo_wine ok(shfi.szDisplayName[0] == 0, "SHGetFileInfoA(c:\\nonexistent | SHGFI_ATTRIBUTES) did not clear szDisplayName[0]\n");
235     todo_wine ok(shfi.szTypeName[0] == 0, "SHGetFileInfoA(c:\\nonexistent | SHGFI_ATTRIBUTES) did not clear szTypeName[0]\n");
236     ok(shfi.iIcon == 0xcfcfcfcf ||
237        broken(shfi.iIcon != 0xcfcfcfcf), /* NT4 doesn't clear but sets this field */
238        "SHGetFileInfoA(c:\\nonexistent | SHGFI_ATTRIBUTES) should not clear iIcon\n");
239
240     rc=SHGetFileInfoA("c:\\nonexistent", FILE_ATTRIBUTE_DIRECTORY,
241                       &shfi, sizeof(shfi),
242                       SHGFI_EXETYPE | SHGFI_USEFILEATTRIBUTES);
243     todo_wine ok(rc == 1, "SHGetFileInfoA(c:\\nonexistent | SHGFI_EXETYPE) should return 1, got 0x%x\n", rc);
244
245     /* Test SHGFI_USEFILEATTRIBUTES support */
246     strcpy(shfi.szDisplayName, "dummy");
247     shfi.iIcon=0xdeadbeef;
248     rc=SHGetFileInfoA("c:\\nonexistent", FILE_ATTRIBUTE_DIRECTORY,
249                       &shfi, sizeof(shfi),
250                       SHGFI_ICONLOCATION | SHGFI_USEFILEATTRIBUTES);
251     ok(rc == 1, "SHGetFileInfoA(c:\\nonexistent) should return 1, got 0x%x\n", rc);
252     if (rc)
253     {
254         ok(strcpy(shfi.szDisplayName, "dummy") != 0, "SHGetFileInfoA(c:\\nonexistent) displayname is not set\n");
255         ok(shfi.iIcon != 0xdeadbeef, "SHGetFileInfoA(c:\\nonexistent) iIcon is not set\n");
256     }
257
258     /* Wine does not have a default icon for text files, and Windows 98 fails
259      * if we give it an empty executable. So use notepad.exe as the test
260      */
261     if (SearchPath(NULL, "notepad.exe", NULL, sizeof(notepad), notepad, NULL))
262     {
263         strcpy(shfi.szDisplayName, "dummy");
264         shfi.iIcon=0xdeadbeef;
265         rc=SHGetFileInfoA(notepad, GetFileAttributes(notepad),
266                           &shfi, sizeof(shfi),
267                           SHGFI_ICONLOCATION | SHGFI_USEFILEATTRIBUTES);
268         ok(rc == 1, "SHGetFileInfoA(%s, SHGFI_USEFILEATTRIBUTES) should return 1, got 0x%x\n", notepad, rc);
269         strcpy(shfi2.szDisplayName, "dummy");
270         shfi2.iIcon=0xdeadbeef;
271         rc2=SHGetFileInfoA(notepad, 0,
272                            &shfi2, sizeof(shfi2),
273                            SHGFI_ICONLOCATION);
274         ok(rc2 == 1, "SHGetFileInfoA(%s) failed %x\n", notepad, rc2);
275         if (rc && rc2)
276         {
277             ok(lstrcmpi(shfi2.szDisplayName, shfi.szDisplayName) == 0, "wrong display name %s != %s\n", shfi.szDisplayName, shfi2.szDisplayName);
278             ok(shfi2.iIcon == shfi.iIcon, "wrong icon index %d != %d\n", shfi.iIcon, shfi2.iIcon);
279         }
280     }
281
282     /* with a directory now */
283     strcpy(shfi.szDisplayName, "dummy");
284     shfi.iIcon=0xdeadbeef;
285     rc=SHGetFileInfoA("test4.txt", GetFileAttributes("test4.txt"),
286                       &shfi, sizeof(shfi),
287                       SHGFI_ICONLOCATION | SHGFI_USEFILEATTRIBUTES);
288     ok(rc == 1, "SHGetFileInfoA(test4.txt/, SHGFI_USEFILEATTRIBUTES) should return 1, got 0x%x\n", rc);
289     strcpy(shfi2.szDisplayName, "dummy");
290     shfi2.iIcon=0xdeadbeef;
291     rc2=SHGetFileInfoA("test4.txt", 0,
292                       &shfi2, sizeof(shfi2),
293                       SHGFI_ICONLOCATION);
294     ok(rc2 == 1, "SHGetFileInfoA(test4.txt/) should return 1, got 0x%x\n", rc2);
295     if (rc && rc2)
296     {
297         ok(lstrcmpi(shfi2.szDisplayName, shfi.szDisplayName) == 0, "wrong display name %s != %s\n", shfi.szDisplayName, shfi2.szDisplayName);
298         ok(shfi2.iIcon == shfi.iIcon, "wrong icon index %d != %d\n", shfi.iIcon, shfi2.iIcon);
299     }
300     /* with drive root directory */
301     strcpy(shfi.szDisplayName, "dummy");
302     strcpy(shfi.szTypeName, "dummy");
303     shfi.hIcon=(HICON) 0xdeadbeef;
304     shfi.iIcon=0xdeadbeef;
305     shfi.dwAttributes=0xdeadbeef;
306     rc=SHGetFileInfoA("c:\\", 0, &shfi, sizeof(shfi),
307                       SHGFI_TYPENAME | SHGFI_DISPLAYNAME | SHGFI_ICON | SHGFI_SMALLICON);
308     ok(rc == 1, "SHGetFileInfoA(c:\\) should return 1, got 0x%x\n", rc);
309     ok(lstrcmp(shfi.szDisplayName, "dummy") != 0, "display name was expected to change\n");
310     ok(lstrcmp(shfi.szTypeName, "dummy") != 0, "type name was expected to change\n");
311     ok(shfi.hIcon != (HICON) 0xdeadbeef, "hIcon was expected to change\n");
312     ok(shfi.iIcon != 0xdeadbeef, "iIcon was expected to change\n");
313 }
314
315 static void test_get_file_info_iconlist(void)
316 {
317     /* Test retrieving a handle to the system image list, and
318      * what that returns for hIcon
319      */
320     HRESULT hr;
321     HIMAGELIST hSysImageList;
322     LPITEMIDLIST pidList;
323     SHFILEINFOA shInfoa;
324     SHFILEINFOW shInfow;
325
326     hr = SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidList);
327     if (FAILED(hr)) {
328          skip("can't get desktop pidl\n");
329          return;
330     }
331
332     memset(&shInfoa, 0xcf, sizeof(shInfoa));
333     hSysImageList = (HIMAGELIST) SHGetFileInfoA((const char *)pidList, 0,
334             &shInfoa, sizeof(shInfoa),
335             SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_PIDL);
336     ok((hSysImageList != INVALID_HANDLE_VALUE) && (hSysImageList > (HIMAGELIST) 0xffff), "Can't get handle for CSIDL_DESKTOP imagelist\n");
337     todo_wine ok(shInfoa.hIcon == 0, "SHGetFileInfoA(CSIDL_DESKTOP, SHGFI_SYSICONINDEX|SHGFI_SMALLICON|SHGFI_PIDL) did not clear hIcon\n");
338     todo_wine ok(shInfoa.szTypeName[0] == 0, "SHGetFileInfoA(CSIDL_DESKTOP, SHGFI_SYSICONINDEX|SHGFI_SMALLICON|SHGFI_PIDL) did not clear szTypeName[0]\n");
339     ok(shInfoa.iIcon != 0xcfcfcfcf, "SHGetFileInfoA(CSIDL_DESKTOP, SHGFI_SYSICONINDEX|SHGFI_SMALLICON|SHGFI_PIDL) should set iIcon\n");
340     ok(shInfoa.dwAttributes == 0xcfcfcfcf ||
341        shInfoa.dwAttributes ==  0 || /* Vista */
342        broken(shInfoa.dwAttributes != 0xcfcfcfcf), /* NT4 doesn't clear but sets this field */
343        "SHGetFileInfoA(CSIDL_DESKTOP, SHGFI_SYSICONINDEX|SHGFI_SMALLICON|SHGFI_PIDL), unexpected dwAttributes\n");
344     CloseHandle(hSysImageList);
345
346     if (!pSHGetFileInfoW)
347     {
348         win_skip("SHGetFileInfoW is not available\n");
349         ILFree(pidList);
350         return;
351     }
352
353     memset(&shInfow, 0xcf, sizeof(shInfow));
354     hSysImageList = (HIMAGELIST) pSHGetFileInfoW((const WCHAR *)pidList, 0,
355             &shInfow, sizeof(shInfow),
356             SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_PIDL);
357     if (!hSysImageList)
358     {
359         win_skip("SHGetFileInfoW is not implemented\n");
360         return;
361     }
362     ok((hSysImageList != INVALID_HANDLE_VALUE) && (hSysImageList > (HIMAGELIST) 0xffff), "Can't get handle for CSIDL_DESKTOP imagelist\n");
363     todo_wine ok(shInfow.hIcon == 0, "SHGetFileInfoW(CSIDL_DESKTOP, SHGFI_SYSICONINDEX|SHGFI_SMALLICON|SHGFI_PIDL) did not clear hIcon\n");
364     ok(shInfow.szTypeName[0] == 0, "SHGetFileInfoW(CSIDL_DESKTOP, SHGFI_SYSICONINDEX|SHGFI_SMALLICON|SHGFI_PIDL) did not clear szTypeName[0]\n");
365     ok(shInfow.iIcon != 0xcfcfcfcf, "SHGetFileInfoW(CSIDL_DESKTOP, SHGFI_SYSICONINDEX|SHGFI_SMALLICON|SHGFI_PIDL) should set iIcon\n");
366     ok(shInfow.dwAttributes == 0xcfcfcfcf ||
367        shInfoa.dwAttributes ==  0, /* Vista */
368        "SHGetFileInfoW(CSIDL_DESKTOP, SHGFI_SYSICONINDEX|SHGFI_SMALLICON|SHGFI_PIDL) unexpected dwAttributes\n");
369     CloseHandle(hSysImageList);
370
371     /* Various suposidly invalid flag testing */
372     memset(&shInfow, 0xcf, sizeof(shInfow));
373     hr =  pSHGetFileInfoW((const WCHAR *)pidList, 0, &shInfow, sizeof(shInfow),
374             SHGFI_SYSICONINDEX|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON);
375     ok(hr != 0, "SHGFI_SYSICONINDEX|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON Failed\n");
376     ok(shInfow.iIcon!=0xcfcfcfcf, "Icon Index Missing\n");
377     ok(shInfow.dwAttributes==0xcfcfcfcf ||
378        shInfoa.dwAttributes==0, /* Vista */
379        "unexpected dwAttributes\n");
380
381     memset(&shInfow, 0xcf, sizeof(shInfow));
382     hr = pSHGetFileInfoW((const WCHAR *)pidList, 0, &shInfow, sizeof(shInfow),
383             SHGFI_ICON|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON);
384     ok(hr != 0, " SHGFI_ICON|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON Failed\n");
385     ok(shInfow.iIcon!=0xcfcfcfcf, "Icon Index Missing\n");
386     ok(shInfow.hIcon!=(HICON)0xcfcfcfcf && shInfow.hIcon!=0,"hIcon invalid\n");
387     if (shInfow.hIcon!=(HICON)0xcfcfcfcf) DestroyIcon(shInfow.hIcon);
388     todo_wine ok(shInfow.dwAttributes==0,"dwAttributes not set\n");
389
390     memset(&shInfow, 0xcf, sizeof(shInfow));
391     hr = pSHGetFileInfoW((const WCHAR *)pidList, 0, &shInfow, sizeof(shInfow),
392             SHGFI_ICON|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_LARGEICON);
393     ok(hr != 0, "SHGFI_ICON|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_LARGEICON Failed\n");
394     ok(shInfow.iIcon!=0xcfcfcfcf, "Icon Index Missing\n");
395     ok(shInfow.hIcon!=(HICON)0xcfcfcfcf && shInfow.hIcon!=0,"hIcon invalid\n");
396     if (shInfow.hIcon != (HICON)0xcfcfcfcf) DestroyIcon(shInfow.hIcon);
397     todo_wine ok(shInfow.dwAttributes==0,"dwAttributes not set\n");
398
399     memset(&shInfow, 0xcf, sizeof(shInfow));
400     hr = pSHGetFileInfoW((const WCHAR *)pidList, 0, &shInfow, sizeof(shInfow),
401             SHGFI_SYSICONINDEX|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_LARGEICON);
402     ok(hr != 0, "SHGFI_SYSICONINDEX|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_LARGEICON Failed\n");
403     ok(shInfow.iIcon!=0xcfcfcfcf, "Icon Index Missing\n");
404     ok(shInfow.dwAttributes==0xcfcfcfcf ||
405        shInfoa.dwAttributes==0, /* Vista */
406        "unexpected dwAttributes\n");
407
408     memset(&shInfow, 0xcf, sizeof(shInfow));
409     hr = pSHGetFileInfoW((const WCHAR *)pidList, 0, &shInfow, sizeof(shInfow),
410             SHGFI_OPENICON|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON);
411     ok(hr != 0, "SHGFI_OPENICON|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON Failed\n");
412     todo_wine ok(shInfow.iIcon==0xcfcfcfcf, "Icon Index Modified\n");
413     ok(shInfow.dwAttributes==0xcfcfcfcf,"dwAttributes modified\n");
414
415     memset(&shInfow, 0xcf, sizeof(shInfow));
416     hr = pSHGetFileInfoW((const WCHAR *)pidList, 0, &shInfow, sizeof(shInfow),
417             SHGFI_SHELLICONSIZE|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON);
418     ok(hr != 0, "SHGFI_SHELLICONSIZE|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON Failed\n");
419     todo_wine ok(shInfow.iIcon==0xcfcfcfcf, "Icon Index Modified\n");
420     ok(shInfow.dwAttributes==0xcfcfcfcf,"dwAttributes modified\n");
421
422     memset(&shInfow, 0xcf, sizeof(shInfow));
423     hr = pSHGetFileInfoW((const WCHAR *)pidList, 0, &shInfow, sizeof(shInfow),
424             SHGFI_SHELLICONSIZE|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON);
425     ok(hr != 0, "SHGFI_SHELLICONSIZE|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON Failed\n");
426     todo_wine ok(shInfow.iIcon==0xcfcfcfcf, "Icon Index Modified\n");
427     ok(shInfow.dwAttributes==0xcfcfcfcf,"dwAttributes modified\n");
428
429     memset(&shInfow, 0xcf, sizeof(shInfow));
430     hr = pSHGetFileInfoW((const WCHAR *)pidList, 0, &shInfow, sizeof(shInfow),
431             SHGFI_SYSICONINDEX|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON|
432         SHGFI_ATTRIBUTES);
433     ok(hr != 0, "SHGFI_SYSICONINDEX|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON|SHGFI_ATTRIBUTES Failed\n");
434     ok(shInfow.iIcon!=0xcfcfcfcf, "Icon Index Missing\n");
435     ok(shInfow.dwAttributes!=0xcfcfcfcf,"dwAttributes not set\n");
436
437     memset(&shInfow, 0xcf, sizeof(shInfow));
438     hr = pSHGetFileInfoW((const WCHAR *)pidList, 0, &shInfow, sizeof(shInfow),
439             SHGFI_SYSICONINDEX|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON|
440         SHGFI_EXETYPE);
441     todo_wine ok(hr != 0, "SHGFI_SYSICONINDEX|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON|SHGFI_EXETYPE Failed\n");
442     ok(shInfow.iIcon!=0xcfcfcfcf, "Icon Index Missing\n");
443     ok(shInfow.dwAttributes==0xcfcfcfcf ||
444        shInfoa.dwAttributes==0, /* Vista */
445        "unexpected dwAttributes\n");
446
447     memset(&shInfow, 0xcf, sizeof(shInfow));
448     hr = pSHGetFileInfoW((const WCHAR *)pidList, 0, &shInfow, sizeof(shInfow),
449         SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON|SHGFI_EXETYPE);
450     todo_wine ok(hr != 0, "SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON|SHGFI_EXETYPE Failed\n");
451     todo_wine ok(shInfow.iIcon==0xcfcfcfcf, "Icon Index Modified\n");
452     ok(shInfow.dwAttributes==0xcfcfcfcf,"dwAttributes modified\n");
453
454     memset(&shInfow, 0xcf, sizeof(shInfow));
455     hr = pSHGetFileInfoW((const WCHAR *)pidList, 0, &shInfow, sizeof(shInfow),
456         SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON|SHGFI_ATTRIBUTES);
457     ok(hr != 0, "SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_SMALLICON|SHGFI_ATTRIBUTES Failed\n");
458     todo_wine ok(shInfow.iIcon==0xcfcfcfcf, "Icon Index Modified\n");
459     ok(shInfow.dwAttributes!=0xcfcfcfcf,"dwAttributes not set\n");
460
461     memset(&shInfow, 0xcf, sizeof(shInfow));
462     hr = pSHGetFileInfoW((const WCHAR *)pidList, 0, &shInfow, sizeof(shInfow),
463             SHGFI_SYSICONINDEX|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|
464         SHGFI_ATTRIBUTES);
465     ok(hr != 0, "SHGFI_SYSICONINDEX|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_ATTRIBUTES Failed\n");
466     ok(shInfow.iIcon!=0xcfcfcfcf, "Icon Index Missing\n");
467     ok(shInfow.dwAttributes!=0xcfcfcfcf,"dwAttributes not set\n");
468
469     memset(&shInfow, 0xcf, sizeof(shInfow));
470     hr = pSHGetFileInfoW((const WCHAR *)pidList, 0, &shInfow, sizeof(shInfow),
471         SHGFI_SYSICONINDEX|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_EXETYPE);
472     todo_wine ok(hr != 0, "SHGFI_SYSICONINDEX|SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_EXETYPE Failed\n");
473     ok(shInfow.iIcon!=0xcfcfcfcf, "Icon Index Missing\n");
474     ok(shInfow.dwAttributes==0xcfcfcfcf ||
475        shInfoa.dwAttributes==0, /* Vista */
476        "unexpected dwAttributes\n");
477
478     memset(&shInfow, 0xcf, sizeof(shInfow));
479     hr = pSHGetFileInfoW((const WCHAR *)pidList, 0, &shInfow, sizeof(shInfow),
480         SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_EXETYPE);
481     todo_wine ok(hr != 0, "SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_EXETYPE Failed\n");
482     todo_wine ok(shInfow.iIcon==0xcfcfcfcf, "Icon Index Modified\n");
483     ok(shInfow.dwAttributes==0xcfcfcfcf,"dwAttributes modified\n");
484
485     memset(&shInfow, 0xcf, sizeof(shInfow));
486     hr = pSHGetFileInfoW((const WCHAR *)pidList, 0, &shInfow, sizeof(shInfow),
487         SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_ATTRIBUTES);
488     ok(hr != 0, "SHGFI_USEFILEATTRIBUTES|SHGFI_PIDL|SHGFI_ATTRIBUTES Failed\n");
489     todo_wine ok(shInfow.iIcon==0xcfcfcfcf, "Icon Index Modified\n");
490     ok(shInfow.dwAttributes!=0xcfcfcfcf,"dwAttributes not set\n");
491
492     ILFree(pidList);
493 }
494
495
496 /*
497  puts into the specified buffer file names with current directory.
498  files - string with file names, separated by null characters. Ends on a double
499  null characters
500 */
501 static void set_curr_dir_path(CHAR *buf, const CHAR* files)
502 {
503     buf[0] = 0;
504     while (files[0])
505     {
506         strcpy(buf, CURR_DIR);
507         buf += strlen(buf);
508         buf[0] = '\\';
509         buf++;
510         strcpy(buf, files);
511         buf += strlen(buf) + 1;
512         files += strlen(files) + 1;
513     }
514     buf[0] = 0;
515 }
516
517
518 /* tests the FO_DELETE action */
519 static void test_delete(void)
520 {
521     SHFILEOPSTRUCTA shfo;
522     DWORD ret;
523     CHAR buf[sizeof(CURR_DIR)+sizeof("/test?.txt")+1];
524
525     sprintf(buf, "%s\\%s", CURR_DIR, "test?.txt");
526     buf[strlen(buf) + 1] = '\0';
527
528     shfo.hwnd = NULL;
529     shfo.wFunc = FO_DELETE;
530     shfo.pFrom = buf;
531     shfo.pTo = NULL;
532     shfo.fFlags = FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_SILENT;
533     shfo.hNameMappings = NULL;
534     shfo.lpszProgressTitle = NULL;
535
536     ok(!SHFileOperationA(&shfo), "Deletion was not successful\n");
537     ok(dir_exists("test4.txt"), "Directory should not have been removed\n");
538     ok(!file_exists("test1.txt"), "File should have been removed\n");
539     ok(!file_exists("test2.txt"), "File should have been removed\n");
540     ok(!file_exists("test3.txt"), "File should have been removed\n");
541
542     ret = SHFileOperationA(&shfo);
543     ok(ret == ERROR_SUCCESS, "Directory exists, but is not removed, ret=%d\n", ret);
544     ok(dir_exists("test4.txt"), "Directory should not have been removed\n");
545
546     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
547
548     ok(!SHFileOperationA(&shfo), "Directory is not removed\n");
549     ok(!dir_exists("test4.txt"), "Directory should have been removed\n");
550
551     ret = SHFileOperationA(&shfo);
552     ok(!ret, "The requested file does not exist, ret=%d\n", ret);
553
554     init_shfo_tests();
555     sprintf(buf, "%s\\%s", CURR_DIR, "test4.txt");
556     buf[strlen(buf) + 1] = '\0';
557     ok(MoveFileA("test1.txt", "test4.txt\\test1.txt"), "Filling the subdirectory failed\n");
558     ok(!SHFileOperationA(&shfo), "Directory is not removed\n");
559     ok(!dir_exists("test4.txt"), "Directory is not removed\n");
560
561     init_shfo_tests();
562     shfo.pFrom = "test1.txt\0test4.txt\0";
563     ok(!SHFileOperationA(&shfo), "Directory and a file are not removed\n");
564     ok(!file_exists("test1.txt"), "The file should have been removed\n");
565     ok(!dir_exists("test4.txt"), "Directory should have been removed\n");
566     ok(file_exists("test2.txt"), "This file should not have been removed\n");
567
568     /* FOF_FILESONLY does not delete a dir matching a wildcard */
569     init_shfo_tests();
570     shfo.fFlags |= FOF_FILESONLY;
571     shfo.pFrom = "*.txt\0";
572     ok(!SHFileOperation(&shfo), "Failed to delete files\n");
573     ok(!file_exists("test1.txt"), "test1.txt should have been removed\n");
574     ok(!file_exists("test_5.txt"), "test_5.txt should have been removed\n");
575     ok(dir_exists("test4.txt"), "test4.txt should not have been removed\n");
576
577     /* FOF_FILESONLY only deletes a dir if explicitly specified */
578     init_shfo_tests();
579     shfo.pFrom = "test_?.txt\0test4.txt\0";
580     ok(!SHFileOperation(&shfo), "Failed to delete files and directory\n");
581     ok(!dir_exists("test4.txt") ||
582        broken(dir_exists("test4.txt")), /* NT4 */
583       "test4.txt should have been removed\n");
584     ok(!file_exists("test_5.txt"), "test_5.txt should have been removed\n");
585     ok(file_exists("test1.txt"), "test1.txt should not have been removed\n");
586
587     /* try to delete an invalid filename */
588     if (0) {
589         /* this crashes on win9x */
590         init_shfo_tests();
591         shfo.pFrom = "\0";
592         shfo.fFlags &= ~FOF_FILESONLY;
593         shfo.fAnyOperationsAborted = FALSE;
594         ret = SHFileOperation(&shfo);
595         ok(ret == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", ret);
596         ok(!shfo.fAnyOperationsAborted, "Expected no aborted operations\n");
597         ok(file_exists("test1.txt"), "Expected test1.txt to exist\n");
598     }
599
600     /* try an invalid function */
601     init_shfo_tests();
602     shfo.pFrom = "test1.txt\0";
603     shfo.wFunc = 0;
604     ret = SHFileOperation(&shfo);
605     ok(ret == ERROR_INVALID_PARAMETER ||
606        broken(ret == ERROR_SUCCESS), /* Win9x, NT4 */
607        "Expected ERROR_INVALID_PARAMETER, got %d\n", ret);
608     ok(file_exists("test1.txt"), "Expected test1.txt to exist\n");
609
610     /* try an invalid list, only one null terminator */
611     if (0) {
612         /* this crashes on win9x */
613         init_shfo_tests();
614         shfo.pFrom = "";
615         shfo.wFunc = FO_DELETE;
616         ret = SHFileOperation(&shfo);
617         ok(ret == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", ret);
618         ok(file_exists("test1.txt"), "Expected test1.txt to exist\n");
619     }
620
621     /* delete a nonexistent file */
622     shfo.pFrom = "nonexistent.txt\0";
623     shfo.wFunc = FO_DELETE;
624     ret = SHFileOperation(&shfo);
625     todo_wine
626     ok(ret == 1026 ||
627        ret == ERROR_FILE_NOT_FOUND || /* Vista */
628        broken(ret == ERROR_SUCCESS), /* NT4 */
629        "Expected 1026 or ERROR_FILE_NOT_FOUND, got %d\n", ret);
630
631     /* delete a dir, and then a file inside the dir, same as
632     * deleting a nonexistent file
633     */
634     if (ret != ERROR_FILE_NOT_FOUND)
635     {
636         /* Vista would throw up a dialog box that we can't suppress */
637         init_shfo_tests();
638         shfo.pFrom = "testdir2\0testdir2\\one.txt\0";
639         ret = SHFileOperation(&shfo);
640         ok(ret == ERROR_PATH_NOT_FOUND ||
641            broken(ret == ERROR_SUCCESS), /* NT4 */
642            "Expected ERROR_PATH_NOT_FOUND, got %d\n", ret);
643         ok(!dir_exists("testdir2"), "Expected testdir2 to not exist\n");
644         ok(!file_exists("testdir2\\one.txt"), "Expected testdir2\\one.txt to not exist\n");
645     }
646     else
647         skip("Test would show a dialog box\n");
648
649     /* try the FOF_NORECURSION flag, continues deleting subdirs */
650     init_shfo_tests();
651     shfo.pFrom = "testdir2\0";
652     shfo.fFlags |= FOF_NORECURSION;
653     ret = SHFileOperation(&shfo);
654     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
655     ok(!file_exists("testdir2\\one.txt"), "Expected testdir2\\one.txt to not exist\n");
656     ok(!dir_exists("testdir2\\nested"), "Expected testdir2\\nested to not exist\n");
657 }
658
659 /* tests the FO_RENAME action */
660 static void test_rename(void)
661 {
662     SHFILEOPSTRUCTA shfo, shfo2;
663     CHAR from[5*MAX_PATH];
664     CHAR to[5*MAX_PATH];
665     DWORD retval;
666
667     shfo.hwnd = NULL;
668     shfo.wFunc = FO_RENAME;
669     shfo.pFrom = from;
670     shfo.pTo = to;
671     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
672     shfo.hNameMappings = NULL;
673     shfo.lpszProgressTitle = NULL;
674
675     set_curr_dir_path(from, "test1.txt\0");
676     set_curr_dir_path(to, "test4.txt\0");
677     retval = SHFileOperationA(&shfo);
678     ok(retval == ERROR_ALREADY_EXISTS ||
679        retval == DE_FILEDESTISFLD || /* Vista */
680        broken(retval == ERROR_INVALID_NAME), /* Win9x, NT4 */
681        "Expected ERROR_ALREADY_EXISTS or DE_FILEDESTISFLD, got %d\n", retval);
682     ok(file_exists("test1.txt"), "The file is renamed\n");
683
684     set_curr_dir_path(from, "test3.txt\0");
685     set_curr_dir_path(to, "test4.txt\\test1.txt\0");
686     retval = SHFileOperationA(&shfo);
687     if (retval == DE_DIFFDIR)
688     {
689         /* Vista and W2K8 (broken or new behavior ?) */
690         ok(!file_exists("test4.txt\\test1.txt"), "The file is renamed\n");
691     }
692     else
693     {
694         ok(retval == ERROR_SUCCESS, "File is renamed moving to other directory\n");
695         ok(file_exists("test4.txt\\test1.txt"), "The file is not renamed\n");
696     }
697
698     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
699     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
700     retval = SHFileOperationA(&shfo);
701     ok(retval == ERROR_GEN_FAILURE ||
702        retval == DE_MANYSRC1DEST || /* Vista */
703        broken(retval == ERROR_SUCCESS), /* Win9x */
704        "Expected ERROR_GEN_FAILURE or DE_MANYSRC1DEST , got %d\n", retval);
705     ok(file_exists("test1.txt"), "The file is renamed - many files are specified\n");
706
707     memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
708     shfo2.fFlags |= FOF_MULTIDESTFILES;
709
710     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
711     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
712     retval = SHFileOperationA(&shfo2);
713     ok(retval == ERROR_GEN_FAILURE ||
714        retval == DE_MANYSRC1DEST || /* Vista */
715        broken(retval == ERROR_SUCCESS), /* Win9x */
716        "Expected ERROR_GEN_FAILURE or DE_MANYSRC1DEST files, got %d\n", retval);
717     ok(file_exists("test1.txt"), "The file is not renamed - many files are specified\n");
718
719     set_curr_dir_path(from, "test1.txt\0");
720     set_curr_dir_path(to, "test6.txt\0");
721     retval = SHFileOperationA(&shfo);
722     ok(retval == ERROR_SUCCESS, "Rename file failed, retval = %d\n", retval);
723     ok(!file_exists("test1.txt"), "The file is not renamed\n");
724     ok(file_exists("test6.txt"), "The file is not renamed\n");
725
726     set_curr_dir_path(from, "test6.txt\0");
727     set_curr_dir_path(to, "test1.txt\0");
728     retval = SHFileOperationA(&shfo);
729     ok(retval == ERROR_SUCCESS, "Rename file back failed, retval = %d\n", retval);
730
731     set_curr_dir_path(from, "test4.txt\0");
732     set_curr_dir_path(to, "test6.txt\0");
733     retval = SHFileOperationA(&shfo);
734     ok(retval == ERROR_SUCCESS, "Rename dir failed, retval = %d\n", retval);
735     ok(!dir_exists("test4.txt"), "The dir is not renamed\n");
736     ok(dir_exists("test6.txt"), "The dir is not renamed\n");
737
738     set_curr_dir_path(from, "test6.txt\0");
739     set_curr_dir_path(to, "test4.txt\0");
740     retval = SHFileOperationA(&shfo);
741     ok(retval == ERROR_SUCCESS, "Rename dir back failed, retval = %d\n", retval);
742     ok(dir_exists("test4.txt"), "The dir is not renamed\n");
743
744     /* try to rename more than one file to a single file */
745     shfo.pFrom = "test1.txt\0test2.txt\0";
746     shfo.pTo = "a.txt\0";
747     retval = SHFileOperationA(&shfo);
748     ok(retval == ERROR_GEN_FAILURE ||
749        retval == DE_MANYSRC1DEST || /* Vista */
750        broken(retval == ERROR_SUCCESS), /* Win9x */
751        "Expected ERROR_GEN_FAILURE or DE_MANYSRC1DEST, got %d\n", retval);
752     ok(file_exists("test1.txt"), "Expected test1.txt to exist\n");
753     ok(file_exists("test2.txt"), "Expected test2.txt to exist\n");
754     ok(!file_exists("a.txt"), "Expected a.txt to not exist\n");
755
756     /* pFrom doesn't exist */
757     shfo.pFrom = "idontexist\0";
758     shfo.pTo = "newfile\0";
759     retval = SHFileOperationA(&shfo);
760     ok(retval == 1026 ||
761        retval == ERROR_FILE_NOT_FOUND || /* Vista */
762        broken(retval == ERROR_SUCCESS), /* NT4 */
763        "Expected 1026 or ERROR_FILE_NOT_FOUND, got %d\n", retval);
764     ok(!file_exists("newfile"), "Expected newfile to not exist\n");
765
766     /* pTo already exist */
767     shfo.pFrom = "test1.txt\0";
768     shfo.pTo = "test2.txt\0";
769     if (old_shell32)
770         shfo.fFlags |= FOF_NOCONFIRMMKDIR;
771     retval = SHFileOperationA(&shfo);
772     if (retval == ERROR_SUCCESS)
773     {
774         /* Vista and W2K8 (broken or new behavior ?) */
775         createTestFile("test1.txt");
776     }
777     else
778     {
779         ok(retval == ERROR_ALREADY_EXISTS ||
780            broken(retval == DE_OPCANCELLED) || /* NT4 */
781            broken(retval == ERROR_INVALID_NAME), /* Win9x */
782            "Expected ERROR_ALREADY_EXISTS, got %d\n", retval);
783     }
784
785     /* pFrom is valid, but pTo is empty */
786     shfo.pFrom = "test1.txt\0";
787     shfo.pTo = "\0";
788     retval = SHFileOperationA(&shfo);
789     ok(retval == ERROR_CANCELLED ||
790        retval == DE_DIFFDIR || /* Vista */
791        broken(retval == DE_OPCANCELLED) || /* Win9x */
792        broken(retval == 65652), /* NT4 */
793        "Expected ERROR_CANCELLED or DE_DIFFDIR\n");
794     ok(file_exists("test1.txt"), "Expected test1.txt to exist\n");
795
796     /* pFrom is empty */
797     shfo.pFrom = "\0";
798     retval = SHFileOperationA(&shfo);
799     ok(retval == ERROR_ACCESS_DENIED ||
800        retval == DE_MANYSRC1DEST || /* Vista */
801        broken(retval == ERROR_SUCCESS), /* Win9x */
802        "Expected ERROR_ACCESS_DENIED or DE_MANYSRC1DEST, got %d\n", retval);
803
804     /* pFrom is NULL, commented out because it crashes on nt 4.0 */
805     if (0)
806     {
807         shfo.pFrom = NULL;
808         retval = SHFileOperationA(&shfo);
809         ok(retval == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", retval);
810     }
811 }
812
813 /* tests the FO_COPY action */
814 static void test_copy(void)
815 {
816     SHFILEOPSTRUCTA shfo, shfo2;
817     CHAR from[5*MAX_PATH];
818     CHAR to[5*MAX_PATH];
819     FILEOP_FLAGS tmp_flags;
820     DWORD retval;
821     LPSTR ptr;
822     BOOL on_nt4 = FALSE;
823     BOOL ret;
824
825     if (old_shell32)
826     {
827         win_skip("Too many differences for old shell32\n");
828         return;
829     }
830
831     shfo.hwnd = NULL;
832     shfo.wFunc = FO_COPY;
833     shfo.pFrom = from;
834     shfo.pTo = to;
835     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
836     shfo.hNameMappings = NULL;
837     shfo.lpszProgressTitle = NULL;
838
839     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
840     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
841     retval = SHFileOperationA(&shfo);
842     if (dir_exists("test6.txt"))
843     {
844         /* Vista and W2K8 (broken or new behavior ?) */
845         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
846         ok(DeleteFileA("test6.txt\\test1.txt"), "The file is not copied - many files "
847            "are specified as a target\n");
848         DeleteFileA("test6.txt\\test2.txt");
849         RemoveDirectoryA("test6.txt\\test4.txt");
850         RemoveDirectoryA("test6.txt");
851     }
852     else
853     {
854         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
855         ok(!file_exists("test6.txt"), "The file is copied - many files are "
856            "specified as a target\n");
857     }
858
859     memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
860     shfo2.fFlags |= FOF_MULTIDESTFILES;
861
862     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
863     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
864     ok(!SHFileOperationA(&shfo2), "Can't copy many files\n");
865     ok(file_exists("test6.txt"), "The file is not copied - many files are "
866        "specified as a target\n");
867     DeleteFileA("test6.txt");
868     DeleteFileA("test7.txt");
869     RemoveDirectoryA("test8.txt");
870
871     /* number of sources do not correspond to number of targets */
872     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
873     set_curr_dir_path(to, "test6.txt\0test7.txt\0");
874     retval = SHFileOperationA(&shfo2);
875     if (dir_exists("test6.txt"))
876     {
877         /* Vista and W2K8 (broken or new behavior ?) */
878         ok(retval == DE_DESTSAMETREE, "Expected DE_DESTSAMETREE, got %d\n", retval);
879         ok(DeleteFileA("test6.txt\\test1.txt"), "The file is not copied - many files "
880            "are specified as a target\n");
881         RemoveDirectoryA("test6.txt");
882         ok(DeleteFileA("test7.txt\\test2.txt"), "The file is not copied - many files "
883            "are specified as a target\n");
884         RemoveDirectoryA("test7.txt");
885     }
886     else
887     {
888         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
889         ok(!file_exists("test6.txt"), "The file is copied - many files are "
890            "specified as a target\n");
891     }
892
893     set_curr_dir_path(from, "test1.txt\0");
894     set_curr_dir_path(to, "test4.txt\0");
895     ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are copied recursively\n");
896     ok(file_exists("test4.txt\\test1.txt"), "The file is copied\n");
897
898     set_curr_dir_path(from, "test?.txt\0");
899     set_curr_dir_path(to, "testdir2\0");
900     ok(!file_exists("testdir2\\test1.txt"), "The file is not copied yet\n");
901     ok(!file_exists("testdir2\\test4.txt"), "The directory is not copied yet\n");
902     ok(!SHFileOperationA(&shfo), "Files and directories are copied to directory\n");
903     ok(file_exists("testdir2\\test1.txt"), "The file is copied\n");
904     ok(file_exists("testdir2\\test4.txt"), "The directory is copied\n");
905     ok(file_exists("testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is copied\n");
906     clean_after_shfo_tests();
907
908     init_shfo_tests();
909     shfo.fFlags |= FOF_FILESONLY;
910     ok(!file_exists("testdir2\\test1.txt"), "The file is not copied yet\n");
911     ok(!file_exists("testdir2\\test4.txt"), "The directory is not copied yet\n");
912     ok(!SHFileOperationA(&shfo), "Files are copied to other directory\n");
913     ok(file_exists("testdir2\\test1.txt"), "The file is copied\n");
914     ok(!file_exists("testdir2\\test4.txt"), "The directory is copied\n");
915     clean_after_shfo_tests();
916
917     init_shfo_tests();
918     set_curr_dir_path(from, "test1.txt\0test2.txt\0");
919     ok(!file_exists("testdir2\\test1.txt"), "The file is not copied yet\n");
920     ok(!file_exists("testdir2\\test2.txt"), "The file is not copied yet\n");
921     ok(!SHFileOperationA(&shfo), "Files are copied to other directory\n");
922     ok(file_exists("testdir2\\test1.txt"), "The file is copied\n");
923     ok(file_exists("testdir2\\test2.txt"), "The file is copied\n");
924     clean_after_shfo_tests();
925
926     /* Copying multiple files with one not existing as source, fails the
927        entire operation in Win98/ME/2K/XP, but not in 95/NT */
928     init_shfo_tests();
929     tmp_flags = shfo.fFlags;
930     set_curr_dir_path(from, "test1.txt\0test10.txt\0test2.txt\0");
931     ok(!file_exists("testdir2\\test1.txt"), "The file is not copied yet\n");
932     ok(!file_exists("testdir2\\test2.txt"), "The file is not copied yet\n");
933     retval = SHFileOperationA(&shfo);
934     if (retval == ERROR_SUCCESS)
935         /* Win 95/NT returns success but copies only the files up to the nonexistent source */
936         ok(file_exists("testdir2\\test1.txt"), "The file is not copied\n");
937     else
938     {
939         /* Failure if one source file does not exist */
940         ok(retval == 1026 || /* Win 98/ME/2K/XP */
941            retval == ERROR_FILE_NOT_FOUND, /* Vista and W2K8 */
942            "Files are copied to other directory\n");
943         ok(!file_exists("testdir2\\test1.txt"), "The file is copied\n");
944     }
945     ok(!file_exists("testdir2\\test2.txt"), "The file is copied\n");
946     shfo.fFlags = tmp_flags;
947
948     /* copy into a nonexistent directory */
949     init_shfo_tests();
950     shfo.fFlags = FOF_NOCONFIRMMKDIR;
951     set_curr_dir_path(from, "test1.txt\0");
952     set_curr_dir_path(to, "nonexistent\\notreal\\test2.txt\0");
953     retval= SHFileOperation(&shfo);
954     ok(!retval, "Error copying into nonexistent directory\n");
955     ok(file_exists("nonexistent"), "nonexistent not created\n");
956     ok(file_exists("nonexistent\\notreal"), "nonexistent\\notreal not created\n");
957     ok(file_exists("nonexistent\\notreal\\test2.txt"), "Directory not created\n");
958     ok(!file_exists("nonexistent\\notreal\\test1.txt"), "test1.txt should not exist\n");
959
960     /* a relative dest directory is OK */
961     clean_after_shfo_tests();
962     init_shfo_tests();
963     shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0";
964     shfo.pTo = "testdir2\0";
965     retval = SHFileOperation(&shfo);
966     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
967     ok(file_exists("testdir2\\test1.txt"), "Expected testdir2\\test1 to exist\n");
968
969     /* try to overwrite an existing write protected file */
970     clean_after_shfo_tests();
971     init_shfo_tests();
972     tmp_flags = shfo.fFlags;
973     shfo.pFrom = "test1.txt\0";
974     shfo.pTo = "test2.txt\0";
975     /* suppress the error-dialog in win9x here */
976     shfo.fFlags = FOF_NOERRORUI | FOF_NOCONFIRMATION | FOF_SILENT;
977     ret = SetFileAttributesA(shfo.pTo, FILE_ATTRIBUTE_READONLY);
978     ok(ret, "Failure to set file attributes (error %x)\n", GetLastError());
979     retval = CopyFileA(shfo.pFrom, shfo.pTo, FALSE);
980     ok(!retval && GetLastError() == ERROR_ACCESS_DENIED, "CopyFileA should have fail with ERROR_ACCESS_DENIED\n");
981     retval = SHFileOperationA(&shfo);
982     /* Does not work on Win95, Win95B, NT4WS and NT4SRV */
983     ok(!retval || broken(retval == DE_OPCANCELLED), "SHFileOperationA failed to copy (error %x)\n", retval);
984     /* Set back normal attributes to make the file deletion succeed */
985     ret = SetFileAttributesA(shfo.pTo, FILE_ATTRIBUTE_NORMAL);
986     ok(ret, "Failure to set file attributes (error %x)\n", GetLastError());
987     shfo.fFlags = tmp_flags;
988
989     /* try to copy files to a file */
990     clean_after_shfo_tests();
991     init_shfo_tests();
992     shfo.pFrom = from;
993     shfo.pTo = to;
994     /* suppress the error-dialog in win9x here */
995     shfo.fFlags |= FOF_NOERRORUI;
996     set_curr_dir_path(from, "test1.txt\0test2.txt\0");
997     set_curr_dir_path(to, "test3.txt\0");
998     retval = SHFileOperation(&shfo);
999     if (retval == DE_FLDDESTISFILE || /* Vista and W2K8 */
1000         retval == DE_INVALIDFILES)    /* Win7 */
1001     {
1002         /* Most likely new behavior */
1003         ok(!shfo.fAnyOperationsAborted, "Didn't expect aborted operations\n");
1004     }
1005     else
1006     {
1007         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
1008         ok(shfo.fAnyOperationsAborted, "Expected aborted operations\n");
1009     }
1010     ok(!file_exists("test3.txt\\test2.txt"), "Expected test3.txt\\test2.txt to not exist\n");
1011
1012     /* try to copy many files to nonexistent directory */
1013     DeleteFile(to);
1014     shfo.fFlags &= ~FOF_NOERRORUI;
1015     shfo.fAnyOperationsAborted = FALSE;
1016     retval = SHFileOperation(&shfo);
1017     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1018     ok(DeleteFile("test3.txt\\test1.txt"), "Expected test3.txt\\test1.txt to exist\n");
1019     ok(DeleteFile("test3.txt\\test2.txt"), "Expected test3.txt\\test1.txt to exist\n");
1020     ok(RemoveDirectory(to), "Expected test3.txt to exist\n");
1021
1022     /* send in FOF_MULTIDESTFILES with too many destination files */
1023     init_shfo_tests();
1024     shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0";
1025     shfo.pTo = "testdir2\\a.txt\0testdir2\\b.txt\0testdir2\\c.txt\0testdir2\\d.txt\0";
1026     shfo.fFlags |= FOF_NOERRORUI | FOF_MULTIDESTFILES;
1027     retval = SHFileOperation(&shfo);
1028     if (dir_exists("testdir2\\a.txt"))
1029     {
1030         /* Vista and W2K8 (broken or new behavior ?) */
1031         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1032         ok(DeleteFile("testdir2\\a.txt\\test1.txt"), "Expected testdir2\\a.txt\\test1.txt to exist\n");
1033         RemoveDirectory("testdir2\\a.txt");
1034         ok(DeleteFile("testdir2\\b.txt\\test2.txt"), "Expected testdir2\\b.txt\\test2.txt to exist\n");
1035         RemoveDirectory("testdir2\\b.txt");
1036         ok(DeleteFile("testdir2\\c.txt\\test3.txt"), "Expected testdir2\\c.txt\\test3.txt to exist\n");
1037         RemoveDirectory("testdir2\\c.txt");
1038         ok(!file_exists("testdir2\\d.txt"), "Expected testdir2\\d.txt to not exist\n");
1039     }
1040     else
1041     {
1042         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
1043         ok(shfo.fAnyOperationsAborted ||
1044            broken(!shfo.fAnyOperationsAborted), /* NT4 */
1045            "Expected aborted operations\n");
1046         ok(!file_exists("testdir2\\a.txt"), "Expected testdir2\\a.txt to not exist\n");
1047     }
1048
1049     /* send in FOF_MULTIDESTFILES with too many destination files */
1050     shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0";
1051     shfo.pTo = "e.txt\0f.txt\0";
1052     shfo.fAnyOperationsAborted = FALSE;
1053     retval = SHFileOperation(&shfo);
1054     if (dir_exists("e.txt"))
1055     {
1056         /* Vista and W2K8 (broken or new behavior ?) */
1057         ok(retval == DE_SAMEFILE, "Expected DE_SAMEFILE, got %d\n", retval);
1058         ok(DeleteFile("e.txt\\test1.txt"), "Expected e.txt\\test1.txt to exist\n");
1059         RemoveDirectory("e.txt");
1060         ok(DeleteFile("f.txt\\test2.txt"), "Expected f.txt\\test2.txt to exist\n");
1061         RemoveDirectory("f.txt");
1062     }
1063     else
1064     {
1065         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
1066         ok(shfo.fAnyOperationsAborted ||
1067            broken(!shfo.fAnyOperationsAborted), /* NT4 */
1068            "Expected aborted operations\n");
1069         ok(!file_exists("e.txt"), "Expected e.txt to not exist\n");
1070     }
1071
1072     /* use FOF_MULTIDESTFILES with files and a source directory */
1073     shfo.pFrom = "test1.txt\0test2.txt\0test4.txt\0";
1074     shfo.pTo = "testdir2\\a.txt\0testdir2\\b.txt\0testdir2\\c.txt\0";
1075     shfo.fAnyOperationsAborted = FALSE;
1076     retval = SHFileOperation(&shfo);
1077     ok(retval == ERROR_SUCCESS ||
1078        broken(retval == 0x100a1), /* WinMe */
1079        "Expected ERROR_SUCCESS, got %d\n", retval);
1080     ok(DeleteFile("testdir2\\a.txt"), "Expected testdir2\\a.txt to exist\n");
1081     ok(DeleteFile("testdir2\\b.txt"), "Expected testdir2\\b.txt to exist\n");
1082     if (retval == ERROR_SUCCESS)
1083         ok(RemoveDirectory("testdir2\\c.txt"), "Expected testdir2\\c.txt to exist\n");
1084
1085     /* try many dest files without FOF_MULTIDESTFILES flag */
1086     shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0";
1087     shfo.pTo = "a.txt\0b.txt\0c.txt\0";
1088     shfo.fAnyOperationsAborted = FALSE;
1089     shfo.fFlags &= ~FOF_MULTIDESTFILES;
1090     retval = SHFileOperation(&shfo);
1091     if (dir_exists("a.txt"))
1092     {
1093         /* Vista and W2K8 (broken or new behavior ?) */
1094         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1095         ok(DeleteFile("a.txt\\test1.txt"), "Expected a.txt\\test1.txt to exist\n");
1096         ok(DeleteFile("a.txt\\test2.txt"), "Expected a.txt\\test2.txt to exist\n");
1097         ok(DeleteFile("a.txt\\test3.txt"), "Expected a.txt\\test3.txt to exist\n");
1098         RemoveDirectory("a.txt");
1099     }
1100     else
1101     {
1102         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
1103         ok(!file_exists("a.txt"), "Expected a.txt to not exist\n");
1104     }
1105
1106     /* try a glob */
1107     shfo.pFrom = "test?.txt\0";
1108     shfo.pTo = "testdir2\0";
1109     shfo.fFlags &= ~FOF_MULTIDESTFILES;
1110     retval = SHFileOperation(&shfo);
1111     ok(retval == ERROR_SUCCESS ||
1112        broken(retval == 0x100a1), /* WinMe */
1113        "Expected ERROR_SUCCESS, got %d\n", retval);
1114     ok(file_exists("testdir2\\test1.txt"), "Expected testdir2\\test1.txt to exist\n");
1115
1116     /* try a glob with FOF_FILESONLY */
1117     clean_after_shfo_tests();
1118     init_shfo_tests();
1119     shfo.pFrom = "test?.txt\0";
1120     shfo.fFlags |= FOF_FILESONLY;
1121     retval = SHFileOperation(&shfo);
1122     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1123     ok(file_exists("testdir2\\test1.txt"), "Expected testdir2\\test1.txt to exist\n");
1124     ok(!dir_exists("testdir2\\test4.txt"), "Expected testdir2\\test4.txt to not exist\n");
1125
1126     /* try a glob with FOF_MULTIDESTFILES and the same number
1127     * of dest files that we would expect
1128     */
1129     clean_after_shfo_tests();
1130     init_shfo_tests();
1131     shfo.pTo = "testdir2\\a.txt\0testdir2\\b.txt\0testdir2\\c.txt\0testdir2\\d.txt\0";
1132     shfo.fFlags &= ~FOF_FILESONLY;
1133     shfo.fFlags |= FOF_MULTIDESTFILES;
1134     retval = SHFileOperation(&shfo);
1135     if (dir_exists("testdir2\\a.txt"))
1136     {
1137         /* Vista and W2K8 (broken or new behavior ?) */
1138         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1139         ok(DeleteFile("testdir2\\a.txt\\test1.txt"), "Expected testdir2\\a.txt\\test1.txt to exist\n");
1140         ok(DeleteFile("testdir2\\a.txt\\test2.txt"), "Expected testdir2\\a.txt\\test2.txt to exist\n");
1141         ok(DeleteFile("testdir2\\a.txt\\test3.txt"), "Expected testdir2\\a.txt\\test3.txt to exist\n");
1142         ok(RemoveDirectory("testdir2\\a.txt\\test4.txt"), "Expected testdir2\\a.txt\\test4.txt to exist\n");
1143         RemoveDirectory("testdir2\\a.txt");
1144     }
1145     else
1146     {
1147         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
1148         ok(shfo.fAnyOperationsAborted ||
1149            broken(!shfo.fAnyOperationsAborted), /* NT4 */
1150            "Expected aborted operations\n");
1151         ok(!file_exists("testdir2\\a.txt"), "Expected testdir2\\test1.txt to not exist\n");
1152     }
1153     ok(!RemoveDirectory("b.txt"), "b.txt should not exist\n");
1154
1155     /* copy one file to two others, second is ignored */
1156     clean_after_shfo_tests();
1157     init_shfo_tests();
1158     shfo.pFrom = "test1.txt\0";
1159     shfo.pTo = "b.txt\0c.txt\0";
1160     shfo.fAnyOperationsAborted = FALSE;
1161     retval = SHFileOperation(&shfo);
1162     if (retval == DE_OPCANCELLED)
1163     {
1164         /* NT4 fails and doesn't copy any files */
1165         ok(!file_exists("b.txt"), "Expected b.txt to not exist\n");
1166         /* Needed to skip some tests */
1167         win_skip("Skipping some tests on NT4\n");
1168         on_nt4 = TRUE;
1169     }
1170     else
1171     {
1172         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1173         ok(DeleteFile("b.txt"), "Expected b.txt to exist\n");
1174     }
1175     ok(!DeleteFile("c.txt"), "Expected c.txt to not exist\n");
1176
1177     /* copy two file to three others, all fail */
1178     shfo.pFrom = "test1.txt\0test2.txt\0";
1179     shfo.pTo = "b.txt\0c.txt\0d.txt\0";
1180     retval = SHFileOperation(&shfo);
1181     if (dir_exists("b.txt"))
1182     {
1183         /* Vista and W2K8 (broken or new behavior ?) */
1184         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1185         ok(DeleteFile("b.txt\\test1.txt"), "Expected b.txt\\test1.txt to exist\n");
1186         RemoveDirectory("b.txt");
1187         ok(DeleteFile("c.txt\\test2.txt"), "Expected c.txt\\test2.txt to exist\n");
1188         RemoveDirectory("c.txt");
1189     }
1190     else
1191     {
1192         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
1193         ok(shfo.fAnyOperationsAborted ||
1194            broken(!shfo.fAnyOperationsAborted), /* NT4 */
1195            "Expected aborted operations\n");
1196         ok(!DeleteFile("b.txt"), "Expected b.txt to not exist\n");
1197     }
1198
1199     /* copy one file and one directory to three others */
1200     shfo.pFrom = "test1.txt\0test4.txt\0";
1201     shfo.pTo = "b.txt\0c.txt\0d.txt\0";
1202     shfo.fAnyOperationsAborted = FALSE;
1203     retval = SHFileOperation(&shfo);
1204     if (dir_exists("b.txt"))
1205     {
1206         /* Vista and W2K8 (broken or new behavior ?) */
1207         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1208         ok(DeleteFile("b.txt\\test1.txt"), "Expected b.txt\\test1.txt to exist\n");
1209         RemoveDirectory("b.txt");
1210         ok(RemoveDirectory("c.txt\\test4.txt"), "Expected c.txt\\test4.txt to exist\n");
1211         RemoveDirectory("c.txt");
1212     }
1213     else
1214     {
1215         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
1216         ok(shfo.fAnyOperationsAborted ||
1217            broken(!shfo.fAnyOperationsAborted), /* NT4 */
1218            "Expected aborted operations\n");
1219         ok(!DeleteFile("b.txt"), "Expected b.txt to not exist\n");
1220         ok(!DeleteFile("c.txt"), "Expected c.txt to not exist\n");
1221     }
1222
1223     /* copy a directory with a file beneath it, plus some files */
1224     createTestFile("test4.txt\\a.txt");
1225     shfo.pFrom = "test4.txt\0test1.txt\0";
1226     shfo.pTo = "testdir2\0";
1227     shfo.fFlags &= ~FOF_MULTIDESTFILES;
1228     shfo.fAnyOperationsAborted = FALSE;
1229     retval = SHFileOperation(&shfo);
1230     ok(retval == ERROR_SUCCESS ||
1231        broken(retval == 0x100a1), /* WinMe */
1232        "Expected ERROR_SUCCESS, got %d\n", retval);
1233     if (retval == ERROR_SUCCESS)
1234     {
1235         ok(DeleteFile("testdir2\\test1.txt"), "Expected testdir2\\test1.txt to exist\n");
1236         ok(DeleteFile("testdir2\\test4.txt\\a.txt"), "Expected a.txt to exist\n");
1237         ok(RemoveDirectory("testdir2\\test4.txt"), "Expected testdir2\\test4.txt to exist\n");
1238     }
1239
1240     /* copy one directory and a file in that dir to another dir */
1241     shfo.pFrom = "test4.txt\0test4.txt\\a.txt\0";
1242     shfo.pTo = "testdir2\0";
1243     retval = SHFileOperation(&shfo);
1244     ok(retval == ERROR_SUCCESS ||
1245        broken(retval == 0x100a1), /* WinMe */
1246        "Expected ERROR_SUCCESS, got %d\n", retval);
1247     if (retval == ERROR_SUCCESS)
1248     {
1249         ok(DeleteFile("testdir2\\test4.txt\\a.txt"), "Expected a.txt to exist\n");
1250         ok(DeleteFile("testdir2\\a.txt"), "Expected testdir2\\a.txt to exist\n");
1251     }
1252
1253     /* copy a file in a directory first, and then the directory to a nonexistent dir */
1254     shfo.pFrom = "test4.txt\\a.txt\0test4.txt\0";
1255     shfo.pTo = "nonexistent\0";
1256     retval = SHFileOperation(&shfo);
1257     if (dir_exists("nonexistent"))
1258     {
1259         /* Vista and W2K8 (broken or new behavior ?) */
1260         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1261         ok(DeleteFile("nonexistent\\test4.txt\\a.txt"), "Expected nonexistent\\test4.txt\\a.txt to exist\n");
1262         RemoveDirectory("nonexistent\\test4.txt");
1263         ok(DeleteFile("nonexistent\\a.txt"), "Expected nonexistent\\a.txt to exist\n");
1264         RemoveDirectory("nonexistent");
1265     }
1266     else
1267     {
1268         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
1269         ok(shfo.fAnyOperationsAborted ||
1270            broken(!shfo.fAnyOperationsAborted), /* NT4 */
1271            "Expected aborted operations\n");
1272         ok(!file_exists("nonexistent\\test4.txt"), "Expected nonexistent\\test4.txt to not exist\n");
1273     }
1274     DeleteFile("test4.txt\\a.txt");
1275
1276     /* destination is same as source file */
1277     shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0";
1278     shfo.pTo = "b.txt\0test2.txt\0c.txt\0";
1279     shfo.fAnyOperationsAborted = FALSE;
1280     shfo.fFlags = FOF_NOERRORUI | FOF_MULTIDESTFILES;
1281     retval = SHFileOperation(&shfo);
1282     if (retval == DE_OPCANCELLED)
1283     {
1284         /* NT4 fails and doesn't copy any files */
1285         ok(!file_exists("b.txt"), "Expected b.txt to not exist\n");
1286     }
1287     else
1288     {
1289         ok(retval == DE_SAMEFILE, "Expected DE_SAMEFILE, got %d\n", retval);
1290         ok(DeleteFile("b.txt"), "Expected b.txt to exist\n");
1291     }
1292     ok(!shfo.fAnyOperationsAborted, "Expected no operations to be aborted\n");
1293     ok(!file_exists("c.txt"), "Expected c.txt to not exist\n");
1294
1295     /* destination is same as source directory */
1296     shfo.pFrom = "test1.txt\0test4.txt\0test3.txt\0";
1297     shfo.pTo = "b.txt\0test4.txt\0c.txt\0";
1298     shfo.fAnyOperationsAborted = FALSE;
1299     retval = SHFileOperation(&shfo);
1300     if (retval == DE_OPCANCELLED)
1301     {
1302         /* NT4 fails and doesn't copy any files */
1303         ok(!file_exists("b.txt"), "Expected b.txt to not exist\n");
1304     }
1305     else
1306     {
1307         ok(retval == ERROR_SUCCESS ||
1308            retval == DE_DESTSAMETREE, /* Vista */
1309            "Expected ERROR_SUCCESS or DE_DESTSAMETREE, got %d\n", retval);
1310         ok(DeleteFile("b.txt"), "Expected b.txt to exist\n");
1311     }
1312     ok(!file_exists("c.txt"), "Expected c.txt to not exist\n");
1313
1314     /* copy a directory into itself, error displayed in UI */
1315     shfo.pFrom = "test4.txt\0";
1316     shfo.pTo = "test4.txt\\newdir\0";
1317     shfo.fFlags &= ~FOF_MULTIDESTFILES;
1318     shfo.fAnyOperationsAborted = FALSE;
1319     retval = SHFileOperation(&shfo);
1320     ok(retval == ERROR_SUCCESS ||
1321        retval == DE_DESTSUBTREE, /* Vista */
1322        "Expected ERROR_SUCCESS or DE_DESTSUBTREE, got %d\n", retval);
1323     ok(!RemoveDirectory("test4.txt\\newdir"), "Expected test4.txt\\newdir to not exist\n");
1324
1325     /* copy a directory to itself, error displayed in UI */
1326     shfo.pFrom = "test4.txt\0";
1327     shfo.pTo = "test4.txt\0";
1328     shfo.fAnyOperationsAborted = FALSE;
1329     retval = SHFileOperation(&shfo);
1330     ok(retval == ERROR_SUCCESS ||
1331        retval == DE_DESTSUBTREE, /* Vista */
1332        "Expected ERROR_SUCCESS or DE_DESTSUBTREE, got %d\n", retval);
1333
1334     /* copy a file into a directory, and the directory into itself */
1335     shfo.pFrom = "test1.txt\0test4.txt\0";
1336     shfo.pTo = "test4.txt\0";
1337     shfo.fAnyOperationsAborted = FALSE;
1338     shfo.fFlags |= FOF_NOCONFIRMATION;
1339     retval = SHFileOperation(&shfo);
1340     ok(retval == ERROR_SUCCESS ||
1341        retval == DE_DESTSUBTREE, /* Vista */
1342        "Expected ERROR_SUCCESS or DE_DESTSUBTREE, got %d\n", retval);
1343     ok(DeleteFile("test4.txt\\test1.txt"), "Expected test4.txt\\test1.txt to exist\n");
1344
1345     /* copy a file to a file, and the directory into itself */
1346     shfo.pFrom = "test1.txt\0test4.txt\0";
1347     shfo.pTo = "test4.txt\\a.txt\0";
1348     shfo.fAnyOperationsAborted = FALSE;
1349     retval = SHFileOperation(&shfo);
1350     if (dir_exists("test4.txt\\a.txt"))
1351     {
1352         /* Vista and W2K8 (broken or new behavior ?) */
1353         ok(retval == DE_DESTSUBTREE, "Expected DE_DESTSUBTREE, got %d\n", retval);
1354         ok(DeleteFile("test4.txt\\a.txt\\test1.txt"), "Expected test4.txt\\a.txt\\test1.txt to exist\n");
1355         RemoveDirectory("test4.txt\\a.txt");
1356     }
1357     else
1358     {
1359         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
1360         ok(!file_exists("test4.txt\\a.txt"), "Expected test4.txt\\a.txt to not exist\n");
1361     }
1362
1363     /* copy a nonexistent file to a nonexistent directory */
1364     shfo.pFrom = "e.txt\0";
1365     shfo.pTo = "nonexistent\0";
1366     shfo.fAnyOperationsAborted = FALSE;
1367     retval = SHFileOperation(&shfo);
1368     ok(retval == 1026 ||
1369        retval == ERROR_FILE_NOT_FOUND || /* Vista */
1370        broken(retval == ERROR_SUCCESS), /* NT4 */
1371        "Expected 1026 or ERROR_FILE_NOT_FOUND, got %d\n", retval);
1372     ok(!file_exists("nonexistent\\e.txt"), "Expected nonexistent\\e.txt to not exist\n");
1373     ok(!file_exists("nonexistent"), "Expected nonexistent to not exist\n");
1374
1375     /* Overwrite tests */
1376     clean_after_shfo_tests();
1377     init_shfo_tests();
1378     if (!on_nt4)
1379     {
1380         /* NT4 would throw up some dialog boxes and doesn't copy files that are needed
1381          * in subsequent tests.
1382          */
1383         shfo.fFlags = FOF_NOCONFIRMATION;
1384         shfo.pFrom = "test1.txt\0";
1385         shfo.pTo = "test2.txt\0";
1386         shfo.fAnyOperationsAborted = FALSE;
1387         /* without FOF_NOCONFIRMATION the confirmation is Yes/No */
1388         retval = SHFileOperation(&shfo);
1389         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1390         ok(file_has_content("test2.txt", "test1.txt\n"), "The file was not copied\n");
1391
1392         shfo.pFrom = "test3.txt\0test1.txt\0";
1393         shfo.pTo = "test2.txt\0one.txt\0";
1394         shfo.fFlags = FOF_NOCONFIRMATION | FOF_MULTIDESTFILES;
1395         /* without FOF_NOCONFIRMATION the confirmation is Yes/Yes to All/No/Cancel */
1396         retval = SHFileOperation(&shfo);
1397         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1398         ok(file_has_content("test2.txt", "test3.txt\n"), "The file was not copied\n");
1399
1400         shfo.pFrom = "one.txt\0";
1401         shfo.pTo = "testdir2\0";
1402         shfo.fFlags = FOF_NOCONFIRMATION;
1403         /* without FOF_NOCONFIRMATION the confirmation is Yes/No */
1404         retval = SHFileOperation(&shfo);
1405         ok(retval == 0, "Expected 0, got %d\n", retval);
1406         ok(file_has_content("testdir2\\one.txt", "test1.txt\n"), "The file was not copied\n");
1407     }
1408
1409     createTestFile("test4.txt\\test1.txt");
1410     shfo.pFrom = "test4.txt\0";
1411     shfo.pTo = "testdir2\0";
1412     /* WinMe needs FOF_NOERRORUI */
1413     shfo.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI;
1414     retval = SHFileOperation(&shfo);
1415     ok(retval == ERROR_SUCCESS ||
1416        broken(retval == 0x100a1), /* WinMe */
1417        "Expected ERROR_SUCCESS, got %d\n", retval);
1418     shfo.fFlags = FOF_NOCONFIRMATION;
1419     if (ERROR_SUCCESS)
1420     {
1421         createTestFile("test4.txt\\.\\test1.txt"); /* modify the content of the file */
1422         /* without FOF_NOCONFIRMATION the confirmation is "This folder already contains a folder named ..." */
1423         retval = SHFileOperation(&shfo);
1424         ok(retval == 0, "Expected 0, got %d\n", retval);
1425         ok(file_has_content("testdir2\\test4.txt\\test1.txt", "test4.txt\\.\\test1.txt\n"), "The file was not copied\n");
1426     }
1427
1428     createTestFile("one.txt");
1429
1430     /* pFrom contains bogus 2nd name longer than MAX_PATH */
1431     memset(from, 'a', MAX_PATH*2);
1432     memset(from+MAX_PATH*2, 0, 2);
1433     lstrcpyA(from, "one.txt");
1434     shfo.pFrom = from;
1435     shfo.pTo = "two.txt\0";
1436     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
1437     retval = SHFileOperation(&shfo);
1438     ok(retval == 1148 || retval == 1026 ||
1439        retval == ERROR_ACCESS_DENIED || /* win2k */
1440        retval == DE_INVALIDFILES, /* Vista */
1441        "Unexpected return value, got %d\n", retval);
1442     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
1443     if (dir_exists("two.txt"))
1444         /* Vista and W2K8 (broken or new behavior ?) */
1445         ok(RemoveDirectory("two.txt"), "Expected two.txt to exist\n");
1446     else
1447         ok(!DeleteFileA("two.txt"), "Expected file to not exist\n");
1448
1449     createTestFile("one.txt");
1450
1451     /* pTo contains bogus 2nd name longer than MAX_PATH */
1452     memset(to, 'a', MAX_PATH*2);
1453     memset(to+MAX_PATH*2, 0, 2);
1454     lstrcpyA(to, "two.txt");
1455     shfo.pFrom = "one.txt\0";
1456     shfo.pTo = to;
1457     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
1458     retval = SHFileOperation(&shfo);
1459     if (retval == DE_OPCANCELLED)
1460     {
1461         /* NT4 fails and doesn't copy any files */
1462         ok(!file_exists("two.txt"), "Expected two.txt to not exist\n");
1463     }
1464     else
1465     {
1466         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1467         ok(DeleteFileA("two.txt"), "Expected file to exist\n");
1468     }
1469     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
1470
1471     createTestFile("one.txt");
1472
1473     /* no FOF_MULTIDESTFILES, two files in pTo */
1474     shfo.pFrom = "one.txt\0";
1475     shfo.pTo = "two.txt\0three.txt\0";
1476     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
1477     retval = SHFileOperation(&shfo);
1478     if (retval == DE_OPCANCELLED)
1479     {
1480         /* NT4 fails and doesn't copy any files */
1481         ok(!file_exists("two.txt"), "Expected two.txt to not exist\n");
1482     }
1483     else
1484     {
1485         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1486         ok(DeleteFileA("two.txt"), "Expected file to exist\n");
1487     }
1488     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
1489
1490     createTestFile("one.txt");
1491
1492     /* both pFrom and pTo contain bogus 2nd names longer than MAX_PATH */
1493     memset(from, 'a', MAX_PATH*2);
1494     memset(from+MAX_PATH*2, 0, 2);
1495     memset(to, 'a', MAX_PATH*2);
1496     memset(to+MAX_PATH*2, 0, 2);
1497     lstrcpyA(from, "one.txt");
1498     lstrcpyA(to, "two.txt");
1499     shfo.pFrom = from;
1500     shfo.pTo = to;
1501     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
1502     retval = SHFileOperation(&shfo);
1503     ok(retval == 1148 || retval == 1026 ||
1504        retval == ERROR_ACCESS_DENIED ||  /* win2k */
1505        retval == DE_INVALIDFILES, /* Vista */
1506        "Unexpected return value, got %d\n", retval);
1507     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
1508     if (dir_exists("two.txt"))
1509         /* Vista and W2K8 (broken or new behavior ?) */
1510         ok(RemoveDirectory("two.txt"), "Expected two.txt to exist\n");
1511     else
1512         ok(!DeleteFileA("two.txt"), "Expected file to not exist\n");
1513
1514     createTestFile("one.txt");
1515
1516     /* pTo contains bogus 2nd name longer than MAX_PATH, FOF_MULTIDESTFILES */
1517     memset(to, 'a', MAX_PATH*2);
1518     memset(to+MAX_PATH*2, 0, 2);
1519     lstrcpyA(to, "two.txt");
1520     shfo.pFrom = "one.txt\0";
1521     shfo.pTo = to;
1522     shfo.fFlags = FOF_MULTIDESTFILES | FOF_NOCONFIRMATION |
1523                   FOF_SILENT | FOF_NOERRORUI;
1524     retval = SHFileOperation(&shfo);
1525     if (retval == DE_OPCANCELLED)
1526     {
1527         /* NT4 fails and doesn't copy any files */
1528         ok(!file_exists("two.txt"), "Expected two.txt to not exist\n");
1529     }
1530     else
1531     {
1532         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1533         ok(DeleteFileA("two.txt"), "Expected file to exist\n");
1534     }
1535     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
1536
1537     createTestFile("one.txt");
1538     createTestFile("two.txt");
1539
1540     /* pTo contains bogus 2nd name longer than MAX_PATH,
1541      * multiple source files,
1542      * dest directory does not exist
1543      */
1544     memset(to, 'a', 2 * MAX_PATH);
1545     memset(to+MAX_PATH*2, 0, 2);
1546     lstrcpyA(to, "threedir");
1547     shfo.pFrom = "one.txt\0two.txt\0";
1548     shfo.pTo = to;
1549     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
1550     retval = SHFileOperation(&shfo);
1551     if (dir_exists("threedir"))
1552     {
1553         /* Vista and W2K8 (broken or new behavior ?) */
1554         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1555         ok(DeleteFileA("threedir\\one.txt"), "Expected file to exist\n");
1556         ok(DeleteFileA("threedir\\two.txt"), "Expected file to exist\n");
1557         ok(RemoveDirectoryA("threedir"), "Expected dir to exist\n");
1558     }
1559     else
1560     {
1561         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
1562         ok(!DeleteFileA("threedir\\one.txt"), "Expected file to not exist\n");
1563         ok(!DeleteFileA("threedir\\two.txt"), "Expected file to not exist\n");
1564         ok(!DeleteFileA("threedir"), "Expected file to not exist\n");
1565         ok(!RemoveDirectoryA("threedir"), "Expected dir to not exist\n");
1566     }
1567     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
1568     ok(DeleteFileA("two.txt"), "Expected file to exist\n");
1569
1570     createTestFile("one.txt");
1571     createTestFile("two.txt");
1572     CreateDirectoryA("threedir", NULL);
1573
1574     /* pTo contains bogus 2nd name longer than MAX_PATH,
1575      * multiple source files,
1576      * dest directory does exist
1577      */
1578     memset(to, 'a', 2 * MAX_PATH);
1579     memset(to+MAX_PATH*2, 0, 2);
1580     lstrcpyA(to, "threedir");
1581     shfo.pFrom = "one.txt\0two.txt\0";
1582     shfo.pTo = to;
1583     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
1584     retval = SHFileOperation(&shfo);
1585     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1586     ok(DeleteFileA("threedir\\one.txt"), "Expected file to exist\n");
1587     ok(DeleteFileA("threedir\\two.txt"), "Expected file to exist\n");
1588     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
1589     ok(DeleteFileA("two.txt"), "Expected file to exist\n");
1590     ok(RemoveDirectoryA("threedir"), "Expected dir to exist\n");
1591
1592     if (0) {
1593         /* this crashes on win9x */
1594         createTestFile("one.txt");
1595         createTestFile("two.txt");
1596
1597         /* pTo contains bogus 2nd name longer than MAX_PATH,
1598          * multiple source files, FOF_MULTIDESTFILES
1599          * dest dir does not exist
1600          */
1601
1602         memset(to, 'a', 2 * MAX_PATH);
1603         memset(to+MAX_PATH*2, 0, 2);
1604         lstrcpyA(to, "threedir");
1605         shfo.pFrom = "one.txt\0two.txt\0";
1606         shfo.pTo = to;
1607         shfo.fFlags = FOF_MULTIDESTFILES | FOF_NOCONFIRMATION |
1608                       FOF_SILENT | FOF_NOERRORUI;
1609         retval = SHFileOperation(&shfo);
1610         ok(retval == ERROR_CANCELLED ||
1611            retval == ERROR_SUCCESS, /* win2k3 */
1612            "Expected ERROR_CANCELLED or ERROR_SUCCESS, got %d\n", retval);
1613         ok(!DeleteFileA("threedir\\one.txt"), "Expected file to not exist\n");
1614         ok(!DeleteFileA("threedir\\two.txt"), "Expected file to not exist\n");
1615         ok(DeleteFileA("one.txt"), "Expected file to exist\n");
1616         ok(DeleteFileA("two.txt"), "Expected file to exist\n");
1617         ok(!RemoveDirectoryA("threedir"), "Expected dir to not exist\n");
1618
1619         /* file exists in win2k */
1620         DeleteFileA("threedir");
1621     }
1622
1623
1624     createTestFile("one.txt");
1625     createTestFile("two.txt");
1626     CreateDirectoryA("threedir", NULL);
1627
1628     /* pTo contains bogus 2nd name longer than MAX_PATH,
1629      * multiple source files, FOF_MULTIDESTFILES
1630      * dest dir does exist
1631      */
1632     memset(to, 'a', 2 * MAX_PATH);
1633     memset(to+MAX_PATH*2, 0, 2);
1634     lstrcpyA(to, "threedir");
1635     ptr = to + lstrlenA(to) + 1;
1636     lstrcpyA(ptr, "fourdir");
1637     shfo.pFrom = "one.txt\0two.txt\0";
1638     shfo.pTo = to;
1639     shfo.fFlags = FOF_MULTIDESTFILES | FOF_NOCONFIRMATION |
1640                   FOF_SILENT | FOF_NOERRORUI;
1641     retval = SHFileOperation(&shfo);
1642     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1643     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
1644     ok(DeleteFileA("two.txt"), "Expected file to exist\n");
1645     ok(DeleteFileA("threedir\\one.txt"), "Expected file to exist\n");
1646     if (dir_exists("fourdir"))
1647     {
1648         /* Vista and W2K8 (broken or new behavior ?) */
1649         ok(!DeleteFileA("threedir\\two.txt"), "Expected file to not exist\n");
1650         ok(DeleteFileA("fourdir\\two.txt"), "Expected file to exist\n");
1651         RemoveDirectoryA("fourdir");
1652     }
1653     else
1654     {
1655         ok(DeleteFileA("threedir\\two.txt"), "Expected file to exist\n");
1656         ok(!DeleteFileA("fourdir"), "Expected file to not exist\n");
1657         ok(!RemoveDirectoryA("fourdir"), "Expected dir to not exist\n");
1658     }
1659     ok(RemoveDirectoryA("threedir"), "Expected dir to exist\n");
1660
1661     createTestFile("one.txt");
1662     createTestFile("two.txt");
1663     CreateDirectoryA("threedir", NULL);
1664
1665     /* multiple source files, FOF_MULTIDESTFILES
1666      * multiple dest files, but first dest dir exists
1667      * num files in lists is equal
1668      */
1669     shfo.pFrom = "one.txt\0two.txt\0";
1670     shfo.pTo = "threedir\0fourdir\0";
1671     shfo.fFlags = FOF_MULTIDESTFILES | FOF_NOCONFIRMATION |
1672                   FOF_SILENT | FOF_NOERRORUI;
1673     retval = SHFileOperation(&shfo);
1674     ok(retval == ERROR_CANCELLED ||
1675        retval == DE_FILEDESTISFLD || /* Vista */
1676        broken(retval == DE_OPCANCELLED), /* Win9x, NT4 */
1677        "Expected ERROR_CANCELLED or DE_FILEDESTISFLD. got %d\n", retval);
1678     if (file_exists("threedir\\threedir"))
1679     {
1680         /* NT4 */
1681         ok(DeleteFileA("threedir\\threedir"), "Expected file to exist\n");
1682     }
1683     ok(!DeleteFileA("threedir\\one.txt"), "Expected file to not exist\n");
1684     ok(!DeleteFileA("threedir\\two.txt"), "Expected file to not exist\n");
1685     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
1686     ok(DeleteFileA("two.txt"), "Expected file to exist\n");
1687     ok(RemoveDirectoryA("threedir"), "Expected dir to exist\n");
1688     ok(!DeleteFileA("fourdir"), "Expected file to not exist\n");
1689     ok(!RemoveDirectoryA("fourdir"), "Expected dir to not exist\n");
1690
1691     createTestFile("one.txt");
1692     createTestFile("two.txt");
1693     CreateDirectoryA("threedir", NULL);
1694
1695     /* multiple source files, FOF_MULTIDESTFILES
1696      * multiple dest files, but first dest dir exists
1697      * num files in lists is not equal
1698      */
1699     shfo.pFrom = "one.txt\0two.txt\0";
1700     shfo.pTo = "threedir\0fourdir\0five\0";
1701     shfo.fFlags = FOF_MULTIDESTFILES | FOF_NOCONFIRMATION |
1702                   FOF_SILENT | FOF_NOERRORUI;
1703     retval = SHFileOperation(&shfo);
1704     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1705     ok(DeleteFileA("one.txt"), "Expected file to exist\n");
1706     ok(DeleteFileA("two.txt"), "Expected file to exist\n");
1707     ok(DeleteFileA("threedir\\one.txt"), "Expected file to exist\n");
1708     if (dir_exists("fourdir"))
1709     {
1710         /* Vista and W2K8 (broken or new behavior ?) */
1711         ok(!DeleteFileA("threedir\\two.txt"), "Expected file to not exist\n");
1712         ok(DeleteFileA("fourdir\\two.txt"), "Expected file to exist\n");
1713         RemoveDirectoryA("fourdir");
1714     }
1715     else
1716     {
1717         ok(DeleteFileA("threedir\\two.txt"), "Expected file to exist\n");
1718         ok(!DeleteFileA("fourdir"), "Expected file to not exist\n");
1719         ok(!RemoveDirectoryA("fourdir"), "Expected dit to not exist\n");
1720     }
1721     ok(RemoveDirectoryA("threedir"), "Expected dir to exist\n");
1722     ok(!DeleteFileA("five"), "Expected file to not exist\n");
1723     ok(!RemoveDirectoryA("five"), "Expected dit to not exist\n");
1724
1725     createTestFile("aa.txt");
1726     createTestFile("ab.txt");
1727     CreateDirectoryA("one", NULL);
1728     CreateDirectoryA("two", NULL);
1729
1730     /* pFrom has a glob, pTo has more than one dest */
1731     shfo.pFrom = "a*.txt\0";
1732     shfo.pTo = "one\0two\0";
1733     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
1734     retval = SHFileOperation(&shfo);
1735     ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1736     ok(DeleteFileA("one\\aa.txt"), "Expected file to exist\n");
1737     ok(DeleteFileA("one\\ab.txt"), "Expected file to exist\n");
1738     ok(!DeleteFileA("two\\aa.txt"), "Expected file to not exist\n");
1739     ok(!DeleteFileA("two\\ab.txt"), "Expected file to not exist\n");
1740     ok(DeleteFileA("aa.txt"), "Expected file to exist\n");
1741     ok(DeleteFileA("ab.txt"), "Expected file to exist\n");
1742     ok(RemoveDirectoryA("one"), "Expected dir to exist\n");
1743     ok(RemoveDirectoryA("two"), "Expected dir to exist\n");
1744
1745     /* pTo is an empty string  */
1746     CreateDirectoryA("dir", NULL);
1747     createTestFile("dir\\abcdefgh.abc");
1748     shfo.pFrom = "dir\\abcdefgh.abc\0";
1749     shfo.pTo = "\0";
1750     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
1751     retval = SHFileOperation(&shfo);
1752     ok(retval == ERROR_SUCCESS ||
1753        broken(retval == DE_OPCANCELLED), /* NT4 */
1754        "Expected ERROR_SUCCESS, got %d\n", retval);
1755     if (retval == ERROR_SUCCESS)
1756         ok(DeleteFileA("abcdefgh.abc"), "Expected file to exist\n");
1757     ok(DeleteFileA("dir\\abcdefgh.abc"), "Expected file to exist\n");
1758     ok(RemoveDirectoryA("dir"), "Expected dir to exist\n");
1759 }
1760
1761 /* tests the FO_MOVE action */
1762 static void test_move(void)
1763 {
1764     SHFILEOPSTRUCTA shfo, shfo2;
1765     CHAR from[5*MAX_PATH];
1766     CHAR to[5*MAX_PATH];
1767     DWORD retval;
1768
1769     shfo.hwnd = NULL;
1770     shfo.wFunc = FO_MOVE;
1771     shfo.pFrom = from;
1772     shfo.pTo = to;
1773     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
1774     shfo.hNameMappings = NULL;
1775     shfo.lpszProgressTitle = NULL;
1776
1777     set_curr_dir_path(from, "test1.txt\0");
1778     set_curr_dir_path(to, "test4.txt\0");
1779     ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are moved recursively\n");
1780     ok(!file_exists("test1.txt"), "test1.txt should not exist\n");
1781     ok(file_exists("test4.txt\\test1.txt"), "The file is not moved\n");
1782
1783     set_curr_dir_path(from, "test?.txt\0");
1784     set_curr_dir_path(to, "testdir2\0");
1785     ok(!file_exists("testdir2\\test2.txt"), "The file is not moved yet\n");
1786     ok(!file_exists("testdir2\\test4.txt"), "The directory is not moved yet\n");
1787     ok(!SHFileOperationA(&shfo), "Files and directories are moved to directory\n");
1788     ok(file_exists("testdir2\\test2.txt"), "The file is moved\n");
1789     ok(file_exists("testdir2\\test4.txt"), "The directory is moved\n");
1790     ok(file_exists("testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is moved\n");
1791
1792     clean_after_shfo_tests();
1793     init_shfo_tests();
1794
1795     memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
1796     shfo2.fFlags |= FOF_MULTIDESTFILES;
1797
1798     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
1799     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
1800     if (old_shell32)
1801         shfo2.fFlags |= FOF_NOCONFIRMMKDIR;
1802     ok(!SHFileOperationA(&shfo2), "Move many files\n");
1803     ok(DeleteFileA("test6.txt"), "The file is not moved - many files are "
1804        "specified as a target\n");
1805     ok(DeleteFileA("test7.txt"), "The file is not moved\n");
1806     ok(RemoveDirectoryA("test8.txt"), "The directory is not moved\n");
1807
1808     init_shfo_tests();
1809
1810     /* number of sources do not correspond to number of targets */
1811     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
1812     set_curr_dir_path(to, "test6.txt\0test7.txt\0");
1813     retval = SHFileOperationA(&shfo2);
1814     if (dir_exists("test6.txt"))
1815     {
1816         if (retval == ERROR_SUCCESS)
1817         {
1818             /* Old shell32 */
1819             DeleteFileA("test6.txt\\test1.txt");
1820             DeleteFileA("test6.txt\\test2.txt");
1821             RemoveDirectoryA("test6.txt\\test4.txt");
1822             RemoveDirectoryA("test6.txt");
1823         }
1824         else
1825         {
1826             /* Vista and W2K8 (broken or new behavior ?) */
1827             ok(retval == DE_DESTSAMETREE, "Expected DE_DESTSAMETREE, got %d\n", retval);
1828             ok(DeleteFileA("test6.txt\\test1.txt"), "The file is not moved\n");
1829             RemoveDirectoryA("test6.txt");
1830             ok(DeleteFileA("test7.txt\\test2.txt"), "The file is not moved\n");
1831             RemoveDirectoryA("test7.txt");
1832         }
1833     }
1834     else
1835     {
1836         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
1837         ok(!file_exists("test6.txt"), "The file is not moved - many files are "
1838            "specified as a target\n");
1839     }
1840
1841     init_shfo_tests();
1842
1843     set_curr_dir_path(from, "test3.txt\0");
1844     set_curr_dir_path(to, "test4.txt\\test1.txt\0");
1845     ok(!SHFileOperationA(&shfo), "Can't move file to other directory\n");
1846     ok(file_exists("test4.txt\\test1.txt"), "The file is not moved\n");
1847
1848     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
1849     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
1850     if (old_shell32)
1851         shfo.fFlags |= FOF_NOCONFIRMMKDIR;
1852     retval = SHFileOperationA(&shfo);
1853     if (dir_exists("test6.txt"))
1854     {
1855         /* Old shell32 */
1856         /* Vista and W2K8 (broken or new behavior ?) */
1857         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1858         ok(DeleteFileA("test6.txt\\test1.txt"), "The file is not moved. Many files are specified\n");
1859         ok(DeleteFileA("test6.txt\\test2.txt"), "The file is not moved. Many files are specified\n");
1860         ok(DeleteFileA("test6.txt\\test4.txt\\test1.txt"), "The file is not moved. Many files are specified\n");
1861         ok(RemoveDirectoryA("test6.txt\\test4.txt"), "The directory is not moved. Many files are specified\n");
1862         RemoveDirectoryA("test6.txt");
1863         init_shfo_tests();
1864     }
1865     else
1866     {
1867         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
1868         ok(file_exists("test1.txt"), "The file is moved. Many files are specified\n");
1869         ok(dir_exists("test4.txt"), "The directory is moved. Many files are specified\n");
1870     }
1871
1872     set_curr_dir_path(from, "test1.txt\0");
1873     set_curr_dir_path(to, "test6.txt\0");
1874     ok(!SHFileOperationA(&shfo), "Move file failed\n");
1875     ok(!file_exists("test1.txt"), "The file is not moved\n");
1876     ok(file_exists("test6.txt"), "The file is not moved\n");
1877     set_curr_dir_path(from, "test6.txt\0");
1878     set_curr_dir_path(to, "test1.txt\0");
1879     ok(!SHFileOperationA(&shfo), "Move file back failed\n");
1880
1881     set_curr_dir_path(from, "test4.txt\0");
1882     set_curr_dir_path(to, "test6.txt\0");
1883     ok(!SHFileOperationA(&shfo), "Move dir failed\n");
1884     ok(!dir_exists("test4.txt"), "The dir is not moved\n");
1885     ok(dir_exists("test6.txt"), "The dir is moved\n");
1886     set_curr_dir_path(from, "test6.txt\0");
1887     set_curr_dir_path(to, "test4.txt\0");
1888     ok(!SHFileOperationA(&shfo), "Move dir back failed\n");
1889
1890     /* move one file to two others */
1891     init_shfo_tests();
1892     shfo.pFrom = "test1.txt\0";
1893     shfo.pTo = "a.txt\0b.txt\0";
1894     retval = SHFileOperationA(&shfo);
1895     if (retval == DE_OPCANCELLED)
1896     {
1897         /* NT4 fails and doesn't move any files */
1898         ok(!file_exists("a.txt"), "Expected a.txt to not exist\n");
1899         DeleteFileA("test1.txt");
1900     }
1901     else
1902     {
1903         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1904         if (old_shell32)
1905         {
1906             DeleteFile("a.txt\\a.txt");
1907             RemoveDirectoryA("a.txt");
1908         }
1909         else
1910             ok(DeleteFile("a.txt"), "Expected a.txt to exist\n");
1911         ok(!file_exists("test1.txt"), "Expected test1.txt to not exist\n");
1912     }
1913     ok(!file_exists("b.txt"), "Expected b.txt to not exist\n");
1914
1915     /* move two files to one other */
1916     shfo.pFrom = "test2.txt\0test3.txt\0";
1917     shfo.pTo = "test1.txt\0";
1918     retval = SHFileOperationA(&shfo);
1919     if (dir_exists("test1.txt"))
1920     {
1921         /* Old shell32 */
1922         /* Vista and W2K8 (broken or new behavior ?) */
1923         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1924         ok(DeleteFileA("test1.txt\\test2.txt"), "Expected test1.txt\\test2.txt to exist\n");
1925         ok(DeleteFileA("test1.txt\\test3.txt"), "Expected test1.txt\\test3.txt to exist\n");
1926         RemoveDirectoryA("test1.txt");
1927         createTestFile("test2.txt");
1928         createTestFile("test3.txt");
1929     }
1930     else
1931     {
1932         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
1933         ok(!file_exists("test1.txt"), "Expected test1.txt to not exist\n");
1934         ok(file_exists("test2.txt"), "Expected test2.txt to exist\n");
1935         ok(file_exists("test3.txt"), "Expected test3.txt to exist\n");
1936     }
1937
1938     /* move a directory into itself */
1939     shfo.pFrom = "test4.txt\0";
1940     shfo.pTo = "test4.txt\\b.txt\0";
1941     retval = SHFileOperationA(&shfo);
1942     ok(retval == ERROR_SUCCESS ||
1943        retval == DE_DESTSUBTREE, /* Vista */
1944        "Expected ERROR_SUCCESS or DE_DESTSUBTREE, got %d\n", retval);
1945     ok(!RemoveDirectory("test4.txt\\b.txt"), "Expected test4.txt\\b.txt to not exist\n");
1946     ok(dir_exists("test4.txt"), "Expected test4.txt to exist\n");
1947
1948     /* move many files without FOF_MULTIDESTFILES */
1949     shfo.pFrom = "test2.txt\0test3.txt\0";
1950     shfo.pTo = "d.txt\0e.txt\0";
1951     retval = SHFileOperationA(&shfo);
1952     if (dir_exists("d.txt"))
1953     {
1954         /* Old shell32 */
1955         /* Vista and W2K8 (broken or new behavior ?) */
1956         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
1957         ok(DeleteFileA("d.txt\\test2.txt"), "Expected d.txt\\test2.txt to exist\n");
1958         ok(DeleteFileA("d.txt\\test3.txt"), "Expected d.txt\\test3.txt to exist\n");
1959         RemoveDirectoryA("d.txt");
1960         createTestFile("test2.txt");
1961         createTestFile("test3.txt");
1962     }
1963     else
1964     {
1965         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
1966         ok(!DeleteFile("d.txt"), "Expected d.txt to not exist\n");
1967         ok(!DeleteFile("e.txt"), "Expected e.txt to not exist\n");
1968     }
1969
1970     /* number of sources != number of targets */
1971     shfo.pTo = "d.txt\0";
1972     shfo.fFlags |= FOF_MULTIDESTFILES;
1973     retval = SHFileOperationA(&shfo);
1974     if (dir_exists("d.txt"))
1975     {
1976         if (old_shell32)
1977         {
1978             DeleteFileA("d.txt\\test2.txt");
1979             DeleteFileA("d.txt\\test3.txt");
1980             RemoveDirectoryA("d.txt");
1981             createTestFile("test2.txt");
1982         }
1983         else
1984         {
1985             /* Vista and W2K8 (broken or new behavior ?) */
1986             ok(retval == DE_SAMEFILE,
1987                "Expected DE_SAMEFILE, got %d\n", retval);
1988             ok(DeleteFileA("d.txt\\test2.txt"), "Expected d.txt\\test2.txt to exist\n");
1989             ok(!file_exists("d.txt\\test3.txt"), "Expected d.txt\\test3.txt to not exist\n");
1990             RemoveDirectoryA("d.txt");
1991             createTestFile("test2.txt");
1992         }
1993     }
1994     else
1995     {
1996         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
1997         ok(!DeleteFile("d.txt"), "Expected d.txt to not exist\n");
1998     }
1999
2000     /* FO_MOVE does not create dest directories */
2001     shfo.pFrom = "test2.txt\0";
2002     shfo.pTo = "dir1\\dir2\\test2.txt\0";
2003     retval = SHFileOperationA(&shfo);
2004     if (dir_exists("dir1"))
2005     {
2006         /* Vista and W2K8 (broken or new behavior ?) */
2007         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
2008         ok(DeleteFileA("dir1\\dir2\\test2.txt"), "Expected dir1\\dir2\\test2.txt to exist\n");
2009         RemoveDirectoryA("dir1\\dir2");
2010         RemoveDirectoryA("dir1");
2011         createTestFile("test2.txt");
2012     }
2013     else
2014     {
2015         expect_retval(ERROR_CANCELLED, DE_OPCANCELLED /* Win9x, NT4 */);
2016     }
2017
2018     /* try to overwrite an existing file */
2019     shfo.pTo = "test3.txt\0";
2020     retval = SHFileOperationA(&shfo);
2021     if (retval == DE_OPCANCELLED)
2022     {
2023         /* NT4 fails and doesn't move any files */
2024         ok(file_exists("test2.txt"), "Expected test2.txt to exist\n");
2025     }
2026     else
2027     {
2028         ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", retval);
2029         ok(!file_exists("test2.txt"), "Expected test2.txt to not exist\n");
2030         if (old_shell32)
2031         {
2032             DeleteFileA("test3.txt\\test3.txt");
2033             RemoveDirectoryA("test3.txt");
2034         }
2035         else
2036             ok(file_exists("test3.txt"), "Expected test3.txt to exist\n");
2037     }
2038 }
2039
2040 static void test_sh_create_dir(void)
2041 {
2042     CHAR path[MAX_PATH];
2043     int ret;
2044
2045     if(!pSHCreateDirectoryExA)
2046     {
2047         win_skip("skipping SHCreateDirectoryExA tests\n");
2048         return;
2049     }
2050
2051     set_curr_dir_path(path, "testdir2\\test4.txt\0");
2052     ret = pSHCreateDirectoryExA(NULL, path, NULL);
2053     ok(ERROR_SUCCESS == ret, "SHCreateDirectoryEx failed to create directory recursively, ret = %d\n", ret);
2054     ok(file_exists("testdir2"), "The first directory is not created\n");
2055     ok(file_exists("testdir2\\test4.txt"), "The second directory is not created\n");
2056
2057     ret = pSHCreateDirectoryExA(NULL, path, NULL);
2058     ok(ERROR_ALREADY_EXISTS == ret, "SHCreateDirectoryEx should fail to create existing directory, ret = %d\n", ret);
2059
2060     ret = pSHCreateDirectoryExA(NULL, "c:\\testdir3", NULL);
2061     ok(file_exists("c:\\testdir3"), "The directory is not created\n");
2062 }
2063
2064 static void test_sh_path_prepare(void)
2065 {
2066     HRESULT res;
2067     CHAR path[MAX_PATH];
2068     CHAR UNICODE_PATH_A[MAX_PATH];
2069     BOOL UsedDefaultChar;
2070
2071     if(!pSHPathPrepareForWriteA)
2072     {
2073         win_skip("skipping SHPathPrepareForWriteA tests\n");
2074         return;
2075     }
2076
2077     /* directory exists, SHPPFW_NONE */
2078     set_curr_dir_path(path, "testdir2\0");
2079     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_NONE);
2080     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
2081
2082     /* directory exists, SHPPFW_IGNOREFILENAME */
2083     set_curr_dir_path(path, "testdir2\\test4.txt\0");
2084     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_IGNOREFILENAME);
2085     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
2086
2087     /* directory exists, SHPPFW_DIRCREATE */
2088     set_curr_dir_path(path, "testdir2\0");
2089     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_DIRCREATE);
2090     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
2091
2092     /* directory exists, SHPPFW_IGNOREFILENAME|SHPPFW_DIRCREATE */
2093     set_curr_dir_path(path, "testdir2\\test4.txt\0");
2094     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_IGNOREFILENAME|SHPPFW_DIRCREATE);
2095     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
2096     ok(!file_exists("nonexistent\\"), "nonexistent\\ exists but shouldn't\n");
2097
2098     /* file exists, SHPPFW_NONE */
2099     set_curr_dir_path(path, "test1.txt\0");
2100     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_NONE);
2101     ok(res == HRESULT_FROM_WIN32(ERROR_DIRECTORY) ||
2102        res == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) || /* WinMe */
2103        res == HRESULT_FROM_WIN32(ERROR_INVALID_NAME), /* Vista */
2104        "Unexpected result : 0x%08x\n", res);
2105
2106     /* file exists, SHPPFW_DIRCREATE */
2107     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_DIRCREATE);
2108     ok(res == HRESULT_FROM_WIN32(ERROR_DIRECTORY) ||
2109        res == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) || /* WinMe */
2110        res == HRESULT_FROM_WIN32(ERROR_INVALID_NAME), /* Vista */
2111        "Unexpected result : 0x%08x\n", res);
2112
2113     /* file exists, SHPPFW_NONE, trailing \ */
2114     set_curr_dir_path(path, "test1.txt\\\0");
2115     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_NONE);
2116     ok(res == HRESULT_FROM_WIN32(ERROR_DIRECTORY) ||
2117        res == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) || /* WinMe */
2118        res == HRESULT_FROM_WIN32(ERROR_INVALID_NAME), /* Vista */
2119        "Unexpected result : 0x%08x\n", res);
2120
2121     /* relative path exists, SHPPFW_DIRCREATE */
2122     res = pSHPathPrepareForWriteA(0, 0, ".\\testdir2", SHPPFW_DIRCREATE);
2123     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
2124
2125     /* relative path doesn't exist, SHPPFW_DIRCREATE -- Windows does not create the directory in this case */
2126     res = pSHPathPrepareForWriteA(0, 0, ".\\testdir2\\test4.txt", SHPPFW_DIRCREATE);
2127     ok(res == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), "res == 0x%08x, expected HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)\n", res);
2128     ok(!file_exists(".\\testdir2\\test4.txt\\"), ".\\testdir2\\test4.txt\\ exists but shouldn't\n");
2129
2130     /* directory doesn't exist, SHPPFW_NONE */
2131     set_curr_dir_path(path, "nonexistent\0");
2132     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_NONE);
2133     ok(res == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), "res == 0x%08x, expected HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)\n", res);
2134
2135     /* directory doesn't exist, SHPPFW_IGNOREFILENAME */
2136     set_curr_dir_path(path, "nonexistent\\notreal\0");
2137     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_IGNOREFILENAME);
2138     ok(res == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), "res == 0x%08x, expected HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)\n", res);
2139     ok(!file_exists("nonexistent\\notreal"), "nonexistent\\notreal exists but shouldn't\n");
2140     ok(!file_exists("nonexistent\\"), "nonexistent\\ exists but shouldn't\n");
2141
2142     /* directory doesn't exist, SHPPFW_IGNOREFILENAME|SHPPFW_DIRCREATE */
2143     set_curr_dir_path(path, "testdir2\\test4.txt\\\0");
2144     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_IGNOREFILENAME|SHPPFW_DIRCREATE);
2145     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
2146     ok(file_exists("testdir2\\test4.txt\\"), "testdir2\\test4.txt doesn't exist but should\n");
2147
2148     /* nested directory doesn't exist, SHPPFW_DIRCREATE */
2149     set_curr_dir_path(path, "nonexistent\\notreal\0");
2150     res = pSHPathPrepareForWriteA(0, 0, path, SHPPFW_DIRCREATE);
2151     ok(res == S_OK, "res == 0x%08x, expected S_OK\n", res);
2152     ok(file_exists("nonexistent\\notreal"), "nonexistent\\notreal doesn't exist but should\n");
2153
2154     /* SHPPFW_ASKDIRCREATE, SHPPFW_NOWRITECHECK, and SHPPFW_MEDIACHECKONLY are untested */
2155
2156     if(!pSHPathPrepareForWriteW)
2157     {
2158         win_skip("Skipping SHPathPrepareForWriteW tests\n");
2159         return;
2160     }
2161
2162     SetLastError(0xdeadbeef);
2163     UsedDefaultChar = FALSE;
2164     if (WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, UNICODE_PATH, -1, UNICODE_PATH_A, sizeof(UNICODE_PATH_A), NULL, &UsedDefaultChar) == 0)
2165     {
2166         win_skip("Could not convert Unicode path name to multibyte (%d)\n", GetLastError());
2167         return;
2168     }
2169     if (UsedDefaultChar)
2170     {
2171         win_skip("Could not find unique multibyte representation for directory name using default codepage\n");
2172         return;
2173     }
2174
2175     /* unicode directory doesn't exist, SHPPFW_NONE */
2176     RemoveDirectoryA(UNICODE_PATH_A);
2177     res = pSHPathPrepareForWriteW(0, 0, UNICODE_PATH, SHPPFW_NONE);
2178     ok(res == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), "res == %08x, expected HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)\n", res);
2179     ok(!file_exists(UNICODE_PATH_A), "unicode path was created but shouldn't be\n");
2180     RemoveDirectoryA(UNICODE_PATH_A);
2181
2182     /* unicode directory doesn't exist, SHPPFW_DIRCREATE */
2183     res = pSHPathPrepareForWriteW(0, 0, UNICODE_PATH, SHPPFW_DIRCREATE);
2184     ok(res == S_OK, "res == %08x, expected S_OK\n", res);
2185     ok(file_exists(UNICODE_PATH_A), "unicode path should've been created\n");
2186
2187     /* unicode directory exists, SHPPFW_NONE */
2188     res = pSHPathPrepareForWriteW(0, 0, UNICODE_PATH, SHPPFW_NONE);
2189     ok(res == S_OK, "ret == %08x, expected S_OK\n", res);
2190
2191     /* unicode directory exists, SHPPFW_DIRCREATE */
2192     res = pSHPathPrepareForWriteW(0, 0, UNICODE_PATH, SHPPFW_DIRCREATE);
2193     ok(res == S_OK, "ret == %08x, expected S_OK\n", res);
2194     RemoveDirectoryA(UNICODE_PATH_A);
2195 }
2196
2197 static void test_sh_new_link_info(void)
2198 {
2199     BOOL ret, mustcopy=TRUE;
2200     CHAR linkto[MAX_PATH];
2201     CHAR destdir[MAX_PATH];
2202     CHAR result[MAX_PATH];
2203     CHAR result2[MAX_PATH];
2204
2205     /* source file does not exist */
2206     set_curr_dir_path(linkto, "nosuchfile.txt\0");
2207     set_curr_dir_path(destdir, "testdir2\0");
2208     ret = SHGetNewLinkInfoA(linkto, destdir, result, &mustcopy, 0);
2209     ok(ret == FALSE ||
2210        broken(ret == lstrlenA(result) + 1), /* NT4 */
2211        "SHGetNewLinkInfoA succeeded\n");
2212     ok(mustcopy == FALSE, "mustcopy should be FALSE\n");
2213
2214     /* dest dir does not exist */
2215     set_curr_dir_path(linkto, "test1.txt\0");
2216     set_curr_dir_path(destdir, "nosuchdir\0");
2217     ret = SHGetNewLinkInfoA(linkto, destdir, result, &mustcopy, 0);
2218     ok(ret == TRUE ||
2219        broken(ret == lstrlenA(result) + 1), /* NT4 */
2220        "SHGetNewLinkInfoA failed, err=%i\n", GetLastError());
2221     ok(mustcopy == FALSE, "mustcopy should be FALSE\n");
2222
2223     /* source file exists */
2224     set_curr_dir_path(linkto, "test1.txt\0");
2225     set_curr_dir_path(destdir, "testdir2\0");
2226     ret = SHGetNewLinkInfoA(linkto, destdir, result, &mustcopy, 0);
2227     ok(ret == TRUE ||
2228        broken(ret == lstrlenA(result) + 1), /* NT4 */
2229        "SHGetNewLinkInfoA failed, err=%i\n", GetLastError());
2230     ok(mustcopy == FALSE, "mustcopy should be FALSE\n");
2231     ok(CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, destdir,
2232                       lstrlenA(destdir), result, lstrlenA(destdir)) == CSTR_EQUAL,
2233        "%s does not start with %s\n", result, destdir);
2234     ok(lstrlenA(result) > 4 && lstrcmpiA(result+lstrlenA(result)-4, ".lnk") == 0,
2235        "%s does not end with .lnk\n", result);
2236
2237     /* preferred target name already exists */
2238     createTestFile(result);
2239     ret = SHGetNewLinkInfoA(linkto, destdir, result2, &mustcopy, 0);
2240     ok(ret == TRUE ||
2241        broken(ret == lstrlenA(result2) + 1), /* NT4 */
2242        "SHGetNewLinkInfoA failed, err=%i\n", GetLastError());
2243     ok(mustcopy == FALSE, "mustcopy should be FALSE\n");
2244     ok(CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, destdir,
2245                       lstrlenA(destdir), result2, lstrlenA(destdir)) == CSTR_EQUAL,
2246        "%s does not start with %s\n", result2, destdir);
2247     ok(lstrlenA(result2) > 4 && lstrcmpiA(result2+lstrlenA(result2)-4, ".lnk") == 0,
2248        "%s does not end with .lnk\n", result2);
2249     ok(lstrcmpiA(result, result2) != 0, "%s and %s are the same\n", result, result2);
2250     DeleteFileA(result);
2251 }
2252
2253 static void test_unicode(void)
2254 {
2255     SHFILEOPSTRUCTW shfoW;
2256     int ret;
2257     HANDLE file;
2258
2259     if (!pSHFileOperationW)
2260     {
2261         skip("SHFileOperationW() is missing\n");
2262         return;
2263     }
2264
2265     shfoW.hwnd = NULL;
2266     shfoW.wFunc = FO_DELETE;
2267     shfoW.pFrom = UNICODE_PATH;
2268     shfoW.pTo = '\0';
2269     shfoW.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
2270     shfoW.hNameMappings = NULL;
2271     shfoW.lpszProgressTitle = NULL;
2272
2273     /* Clean up before start test */
2274     DeleteFileW(UNICODE_PATH);
2275     RemoveDirectoryW(UNICODE_PATH);
2276
2277     /* Make sure we are on a system that supports unicode */
2278     SetLastError(0xdeadbeef);
2279     file = CreateFileW(UNICODE_PATH, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
2280     if (GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
2281     {
2282         skip("Unicode tests skipped on non-unicode system\n");
2283         return;
2284     }
2285     CloseHandle(file);
2286
2287     /* Try to delete a file with unicode filename */
2288     ok(file_existsW(UNICODE_PATH), "The file does not exist\n");
2289     ret = pSHFileOperationW(&shfoW);
2290     ok(!ret, "File is not removed, ErrorCode: %d\n", ret);
2291     ok(!file_existsW(UNICODE_PATH), "The file should have been removed\n");
2292
2293     /* Try to trash a file with unicode filename */
2294     createTestFileW(UNICODE_PATH);
2295     shfoW.fFlags |= FOF_ALLOWUNDO;
2296     ok(file_existsW(UNICODE_PATH), "The file does not exist\n");
2297     ret = pSHFileOperationW(&shfoW);
2298     ok(!ret, "File is not removed, ErrorCode: %d\n", ret);
2299     ok(!file_existsW(UNICODE_PATH), "The file should have been removed\n");
2300
2301     if(!pSHCreateDirectoryExW)
2302     {
2303         skip("Skipping SHCreateDirectoryExW tests\n");
2304         return;
2305     }
2306
2307     /* Try to delete a directory with unicode filename */
2308     ret = pSHCreateDirectoryExW(NULL, UNICODE_PATH, NULL);
2309     ok(!ret, "SHCreateDirectoryExW returned %d\n", ret);
2310     ok(file_existsW(UNICODE_PATH), "The directory is not created\n");
2311     shfoW.fFlags &= ~FOF_ALLOWUNDO;
2312     ret = pSHFileOperationW(&shfoW);
2313     ok(!ret, "Directory is not removed, ErrorCode: %d\n", ret);
2314     ok(!file_existsW(UNICODE_PATH), "The directory should have been removed\n");
2315
2316     /* Try to trash a directory with unicode filename */
2317     ret = pSHCreateDirectoryExW(NULL, UNICODE_PATH, NULL);
2318     ok(!ret, "SHCreateDirectoryExW returned %d\n", ret);
2319     ok(file_existsW(UNICODE_PATH), "The directory was not created\n");
2320     shfoW.fFlags |= FOF_ALLOWUNDO;
2321     ret = pSHFileOperationW(&shfoW);
2322     ok(!ret, "Directory is not removed, ErrorCode: %d\n", ret);
2323     ok(!file_existsW(UNICODE_PATH), "The directory should have been removed\n");
2324 }
2325
2326 static void
2327 test_shlmenu(void) {
2328         HRESULT hres;
2329         hres = Shell_MergeMenus (0, 0, 0x42, 0x4242, 0x424242, 0);
2330         ok (hres == 0x4242, "expected 0x4242 but got %x\n", hres);
2331         hres = Shell_MergeMenus ((HMENU)42, 0, 0x42, 0x4242, 0x424242, 0);
2332         ok (hres == 0x4242, "expected 0x4242 but got %x\n", hres);
2333 }
2334
2335 /* Check for old shell32 (4.0.x) */
2336 static BOOL is_old_shell32(void)
2337 {
2338     SHFILEOPSTRUCTA shfo;
2339     CHAR from[5*MAX_PATH];
2340     CHAR to[5*MAX_PATH];
2341     DWORD retval;
2342
2343     shfo.hwnd = NULL;
2344     shfo.wFunc = FO_COPY;
2345     shfo.pFrom = from;
2346     shfo.pTo = to;
2347     /* FOF_NOCONFIRMMKDIR is needed for old shell32 */
2348     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI | FOF_MULTIDESTFILES | FOF_NOCONFIRMMKDIR;
2349     shfo.hNameMappings = NULL;
2350     shfo.lpszProgressTitle = NULL;
2351
2352     set_curr_dir_path(from, "test1.txt\0test2.txt\0test3.txt\0");
2353     set_curr_dir_path(to, "test6.txt\0test7.txt\0");
2354     retval = SHFileOperationA(&shfo);
2355
2356     /* Delete extra files on old shell32 and Vista+*/
2357     DeleteFileA("test6.txt\\test1.txt");
2358     /* Delete extra files on old shell32 */
2359     DeleteFileA("test6.txt\\test2.txt");
2360     DeleteFileA("test6.txt\\test3.txt");
2361     /* Delete extra directory on old shell32 and Vista+ */
2362     RemoveDirectoryA("test6.txt");
2363     /* Delete extra files/directories on Vista+*/
2364     DeleteFileA("test7.txt\\test2.txt");
2365     RemoveDirectoryA("test7.txt");
2366
2367     if (retval == ERROR_SUCCESS)
2368         return TRUE;
2369
2370     return FALSE;
2371 }
2372
2373 START_TEST(shlfileop)
2374 {
2375     InitFunctionPointers();
2376
2377     clean_after_shfo_tests();
2378
2379     init_shfo_tests();
2380     old_shell32 = is_old_shell32();
2381     if (old_shell32)
2382         win_skip("Need to cater for old shell32 (4.0.x) on Win95\n");
2383     clean_after_shfo_tests();
2384
2385     init_shfo_tests();
2386     test_get_file_info();
2387     test_get_file_info_iconlist();
2388     clean_after_shfo_tests();
2389
2390     init_shfo_tests();
2391     test_delete();
2392     clean_after_shfo_tests();
2393
2394     init_shfo_tests();
2395     test_rename();
2396     clean_after_shfo_tests();
2397
2398     init_shfo_tests();
2399     test_copy();
2400     clean_after_shfo_tests();
2401
2402     init_shfo_tests();
2403     test_move();
2404     clean_after_shfo_tests();
2405
2406     test_sh_create_dir();
2407     clean_after_shfo_tests();
2408
2409     init_shfo_tests();
2410     test_sh_path_prepare();
2411     clean_after_shfo_tests();
2412
2413     init_shfo_tests();
2414     test_sh_new_link_info();
2415     clean_after_shfo_tests();
2416
2417     test_unicode();
2418
2419     test_shlmenu();
2420 }