Update shell32's version to WinXP level.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23  
24 #define WINE_NOWINSOCK
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wtypes.h"
28 #include "shellapi.h"
29
30 #include "wine/test.h"
31
32 CHAR CURR_DIR[MAX_PATH];
33
34 /* creates a file with the specified name for tests */
35 void createTestFile(CHAR *name)
36 {
37     HANDLE file;
38     DWORD written;
39
40     file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
41     ok(file != INVALID_HANDLE_VALUE, "Failure to open file %s\n", name);
42     WriteFile(file, name, strlen(name), &written, NULL);
43     WriteFile(file, "\n", strlen("\n"), &written, NULL);
44     CloseHandle(file);
45 }
46
47 BOOL file_exists(CHAR *name)
48 {
49     return GetFileAttributesA(name) != INVALID_FILE_ATTRIBUTES;
50 }
51
52 /* initializes the tests */
53 void init_shfo_tests(void)
54 {
55     GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
56     createTestFile(".\\test1.txt");
57     createTestFile(".\\test2.txt");
58     createTestFile(".\\test3.txt");
59     CreateDirectoryA(".\\test4.txt", NULL);
60     CreateDirectoryA(".\\testdir2", NULL);
61 }
62
63 /* cleans after tests */
64 void clean_after_shfo_tests(void)
65 {
66     DeleteFileA(".\\test1.txt");
67     DeleteFileA(".\\test2.txt");
68     DeleteFileA(".\\test3.txt");
69     DeleteFileA(".\\test4.txt\\test1.txt");
70     DeleteFileA(".\\test4.txt\\test2.txt");
71     DeleteFileA(".\\test4.txt\\test3.txt");
72     RemoveDirectoryA(".\\test4.txt");
73     DeleteFileA(".\\testdir2\\test1.txt");
74     DeleteFileA(".\\testdir2\\test2.txt");
75     DeleteFileA(".\\testdir2\\test3.txt");
76     DeleteFileA(".\\testdir2\\test4.txt\\test1.txt");
77     RemoveDirectoryA(".\\testdir2\\test4.txt");
78     RemoveDirectoryA(".\\testdir2");
79 }
80
81 /*
82  puts into the specified buffer file names with current directory.
83  files - string with file names, separated by null characters. Ends on a double
84  null characters
85 */
86 void set_curr_dir_path(CHAR *buf, CHAR* files)
87 {
88     buf[0] = 0;
89     while (files[0])
90     {
91         strcpy(buf, CURR_DIR);
92         buf += strlen(buf);
93         buf[0] = '\\';
94         buf++;
95         strcpy(buf, files);
96         buf += strlen(buf) + 1;
97         files += strlen(files) + 1;
98     }
99     buf[0] = 0;
100 }
101
102
103 /* tests the FO_DELETE action */
104 void test_delete(void)
105 {
106     SHFILEOPSTRUCTA shfo;
107     CHAR buf[MAX_PATH];
108
109     sprintf(buf, "%s\\%s", CURR_DIR, "test?.txt");
110     buf[strlen(buf) + 1] = '\0';
111
112     shfo.hwnd = NULL;
113     shfo.wFunc = FO_DELETE;
114     shfo.pFrom = buf;
115     shfo.pTo = "\0";
116     shfo.fFlags = FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_SILENT;
117     shfo.hNameMappings = NULL;
118     shfo.lpszProgressTitle = NULL;
119
120     ok(!SHFileOperationA(&shfo), "Deletion was successful\n");
121     ok(file_exists(".\\test4.txt"), "Directory should not be removed\n");
122     ok(!file_exists(".\\test1.txt"), "File should be removed\n");
123
124     ok(!SHFileOperationA(&shfo), "Directory exists, but is not removed\n");
125     ok(file_exists(".\\test4.txt"), "Directory should not be removed\n");
126
127     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
128
129     ok(!SHFileOperationA(&shfo), "Directory removed\n");
130     ok(!file_exists(".\\test4.txt"), "Directory should be removed\n");
131
132     ok(!SHFileOperationA(&shfo), "The requested file does not exist\n");
133
134     init_shfo_tests();
135     sprintf(buf, "%s\\%s", CURR_DIR, "test4.txt");
136     buf[strlen(buf) + 1] = '\0';
137     ok(MoveFileA(".\\test1.txt", ".\\test4.txt\\test1.txt"), "Fill the subdirectory\n");
138     ok(!SHFileOperationA(&shfo), "Directory removed\n");
139     ok(!file_exists(".\\test4.txt"), "Directory is removed\n");
140
141     init_shfo_tests();
142     shfo.pFrom = ".\\test1.txt\0.\\test4.txt\0";
143     ok(!SHFileOperationA(&shfo), "Directory and a file removed\n");
144     ok(!file_exists(".\\test1.txt"), "The file should be removed\n");
145     ok(!file_exists(".\\test4.txt"), "Directory should be removed\n");
146     ok(file_exists(".\\test2.txt"), "This file should not be removed\n");
147 }
148
149 /* tests the FO_RENAME action */
150 void test_rename()
151 {
152     SHFILEOPSTRUCTA shfo, shfo2;
153     CHAR from[MAX_PATH];
154     CHAR to[MAX_PATH];
155     DWORD retval;
156
157     shfo.hwnd = NULL;
158     shfo.wFunc = FO_RENAME;
159     shfo.pFrom = from;
160     shfo.pTo = to;
161     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
162     shfo.hNameMappings = NULL;
163     shfo.lpszProgressTitle = NULL;
164
165     set_curr_dir_path(from, "test1.txt\0");
166     set_curr_dir_path(to, "test4.txt\0");
167     ok(SHFileOperationA(&shfo), "File is not renamed moving to other directory "
168        "when specifying directory name only\n");
169     ok(file_exists(".\\test1.txt"), "The file is not removed\n");
170
171     set_curr_dir_path(from, "test3.txt\0");
172     set_curr_dir_path(to, "test4.txt\\test1.txt\0");
173     ok(!SHFileOperationA(&shfo), "File is renamed moving to other directory\n");
174     ok(file_exists(".\\test4.txt\\test1.txt"), "The file is renamed\n");
175
176     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
177     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
178     retval = SHFileOperationA(&shfo); /* W98 returns 0, W2K and newer returns ERROR_GEN_FAILURE, both do nothing */
179     ok(!retval || retval == ERROR_GEN_FAILURE, "Can't rename many files, retval = %lx\n", retval);
180     ok(file_exists(".\\test1.txt"), "The file is not renamed - many files are specified\n");
181
182     memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
183     shfo2.fFlags |= FOF_MULTIDESTFILES;
184
185     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
186     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
187     retval = SHFileOperationA(&shfo2); /* W98 returns 0, W2K and newer returns ERROR_GEN_FAILURE, both do nothing */
188     ok(!retval || retval == ERROR_GEN_FAILURE, "Can't rename many files, retval = %lx\n", retval);
189     ok(file_exists(".\\test1.txt"), "The file is not renamed - many files are specified\n");
190
191     set_curr_dir_path(from, "test1.txt\0");
192     set_curr_dir_path(to, "test6.txt\0");
193     ok(!SHFileOperationA(&shfo), "Rename file\n");
194     ok(!file_exists(".\\test1.txt"), "The file is renamed\n");
195     ok(file_exists(".\\test6.txt"), "The file is renamed\n");
196     set_curr_dir_path(from, "test6.txt\0");
197     set_curr_dir_path(to, "test1.txt\0");
198     ok(!SHFileOperationA(&shfo), "Rename file back\n");
199
200     set_curr_dir_path(from, "test4.txt\0");
201     set_curr_dir_path(to, "test6.txt\0");
202     ok(!SHFileOperationA(&shfo), "Rename dir\n");
203     ok(!file_exists(".\\test4.txt"), "The dir is renamed\n");
204     ok(file_exists(".\\test6.txt"), "The dir is renamed\n");
205     set_curr_dir_path(from, "test6.txt\0");
206     set_curr_dir_path(to, "test4.txt\0");
207     ok(!SHFileOperationA(&shfo), "Rename dir back\n");
208 }
209
210 /* tests the FO_COPY action */
211 void test_copy(void)
212 {
213     SHFILEOPSTRUCTA shfo, shfo2;
214     CHAR from[MAX_PATH];
215     CHAR to[MAX_PATH];
216     FILEOP_FLAGS tmp_flags;
217     DWORD retval;
218
219     shfo.hwnd = NULL;
220     shfo.wFunc = FO_COPY;
221     shfo.pFrom = from;
222     shfo.pTo = to;
223     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
224     shfo.hNameMappings = NULL;
225     shfo.lpszProgressTitle = NULL;
226
227     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
228     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
229     ok(SHFileOperationA(&shfo), "Can't copy many files\n");
230     ok(!file_exists(".\\test6.txt"), "The file is not copied - many files are "
231        "specified as a target\n");
232
233     memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
234     shfo2.fFlags |= FOF_MULTIDESTFILES;
235
236     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
237     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
238     ok(!SHFileOperationA(&shfo2), "Can't copy many files\n");
239     ok(file_exists(".\\test6.txt"), "The file is copied - many files are "
240        "specified as a target\n");
241     DeleteFileA(".\\test6.txt");
242     DeleteFileA(".\\test7.txt");
243     RemoveDirectoryA(".\\test8.txt");
244
245     /* number of sources do not correspond to number of targets */
246     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
247     set_curr_dir_path(to, "test6.txt\0test7.txt\0");
248     ok(SHFileOperationA(&shfo2), "Can't copy many files\n");
249     ok(!file_exists(".\\test6.txt"), "The file is not copied - many files are "
250        "specified as a target\n");
251
252     set_curr_dir_path(from, "test1.txt\0");
253     set_curr_dir_path(to, "test4.txt\0");
254     ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are copied recursively\n");
255     ok(file_exists(".\\test4.txt\\test1.txt"), "The file is copied\n");
256
257     set_curr_dir_path(from, "test?.txt\0");
258     set_curr_dir_path(to, "testdir2\0");
259     ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet\n");
260     ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not copied yet\n");
261     ok(!SHFileOperationA(&shfo), "Files and directories are copied to directory\n");
262     ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied\n");
263     ok(file_exists(".\\testdir2\\test4.txt"), "The directory is copied\n");
264     ok(file_exists(".\\testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is copied\n");
265     clean_after_shfo_tests();
266
267     init_shfo_tests();
268     shfo.fFlags |= FOF_FILESONLY;
269     ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet\n");
270     ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not copied yet\n");
271     ok(!SHFileOperationA(&shfo), "Files are copied to other directory\n");
272     ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied\n");
273     ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is copied\n");
274     clean_after_shfo_tests();
275
276     init_shfo_tests();
277     set_curr_dir_path(from, "test1.txt\0test2.txt\0");
278     ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet\n");
279     ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not copied yet\n");
280     ok(!SHFileOperationA(&shfo), "Files are copied to other directory \n");
281     ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied\n");
282     ok(file_exists(".\\testdir2\\test2.txt"), "The file is copied\n");
283     clean_after_shfo_tests();
284
285     /* Copying multiple files with one not existing as source, fails the
286        entire operation in Win98/ME/2K/XP, but not in 95/NT */
287     init_shfo_tests();
288     tmp_flags = shfo.fFlags;
289     set_curr_dir_path(from, "test1.txt\0test10.txt\0test2.txt\0");
290     ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet\n");
291     ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not copied yet\n");
292     retval = SHFileOperationA(&shfo);
293     if (!retval)
294       /* Win 95/NT returns success but copies only the files up to the non-existent source */
295       ok(file_exists(".\\testdir2\\test1.txt"), "The file is not copied\n");
296     else
297     {
298       /* Win 98/ME/2K/XP fail the entire operation with return code 1026 if one source file does not exist */
299       ok(retval == 1026, "Files are copied to other directory\n");
300       ok(!file_exists(".\\testdir2\\test1.txt"), "The file is copied\n");
301     }
302     ok(!file_exists(".\\testdir2\\test2.txt"), "The file is copied\n");
303     shfo.fFlags = tmp_flags;
304 }
305
306 /* tests the FO_MOVE action */
307 void test_move(void)
308 {
309     SHFILEOPSTRUCTA shfo, shfo2;
310     CHAR from[MAX_PATH];
311     CHAR to[MAX_PATH];
312
313     shfo.hwnd = NULL;
314     shfo.wFunc = FO_MOVE;
315     shfo.pFrom = from;
316     shfo.pTo = to;
317     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
318     shfo.hNameMappings = NULL;
319     shfo.lpszProgressTitle = NULL;
320
321     set_curr_dir_path(from, "test1.txt\0");
322     set_curr_dir_path(to, "test4.txt\0");
323     ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are moved recursively\n");
324     ok(file_exists(".\\test4.txt\\test1.txt"), "The file is moved\n");
325
326     set_curr_dir_path(from, "test?.txt\0");
327     set_curr_dir_path(to, "testdir2\0");
328     ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not moved yet\n");
329     ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not moved yet\n");
330     ok(!SHFileOperationA(&shfo), "Files and directories are moved to directory\n");
331     ok(file_exists(".\\testdir2\\test2.txt"), "The file is moved\n");
332     ok(file_exists(".\\testdir2\\test4.txt"), "The directory is moved\n");
333     ok(file_exists(".\\testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is moved\n");
334
335     clean_after_shfo_tests();
336     init_shfo_tests();
337
338     memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
339     shfo2.fFlags |= FOF_MULTIDESTFILES;
340
341     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
342     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
343     ok(!SHFileOperationA(&shfo2), "Move many files\n");
344     ok(file_exists(".\\test6.txt"), "The file is moved - many files are "
345        "specified as a target\n");
346     DeleteFileA(".\\test6.txt");
347     DeleteFileA(".\\test7.txt");
348     RemoveDirectoryA(".\\test8.txt");
349
350     init_shfo_tests();
351
352     /* number of sources do not correspond to number of targets */
353     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
354     set_curr_dir_path(to, "test6.txt\0test7.txt\0");
355     ok(SHFileOperationA(&shfo2), "Can't move many files\n");
356     ok(!file_exists(".\\test6.txt"), "The file is not moved - many files are "
357        "specified as a target\n");
358
359     init_shfo_tests();
360
361     set_curr_dir_path(from, "test3.txt\0");
362     set_curr_dir_path(to, "test4.txt\\test1.txt\0");
363     ok(!SHFileOperationA(&shfo), "File is moved moving to other directory\n");
364     ok(file_exists(".\\test4.txt\\test1.txt"), "The file is moved\n");
365
366     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
367     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
368     ok(SHFileOperationA(&shfo), "Can not move many files\n");
369     ok(file_exists(".\\test1.txt"), "The file is not moved. Many files are specified\n");
370     ok(file_exists(".\\test4.txt"), "The directory not is moved. Many files are specified\n");
371
372     set_curr_dir_path(from, "test1.txt\0");
373     set_curr_dir_path(to, "test6.txt\0");
374     ok(!SHFileOperationA(&shfo), "Move file\n");
375     ok(!file_exists(".\\test1.txt"), "The file is moved\n");
376     ok(file_exists(".\\test6.txt"), "The file is moved\n");
377     set_curr_dir_path(from, "test6.txt\0");
378     set_curr_dir_path(to, "test1.txt\0");
379     ok(!SHFileOperationA(&shfo), "Move file back\n");
380
381     set_curr_dir_path(from, "test4.txt\0");
382     set_curr_dir_path(to, "test6.txt\0");
383     ok(!SHFileOperationA(&shfo), "Move dir\n");
384     ok(!file_exists(".\\test4.txt"), "The dir is moved\n");
385     ok(file_exists(".\\test6.txt"), "The dir is moved\n");
386     set_curr_dir_path(from, "test6.txt\0");
387     set_curr_dir_path(to, "test4.txt\0");
388     ok(!SHFileOperationA(&shfo), "Move dir back\n");
389 }
390
391 START_TEST(shlfileop)
392 {
393     clean_after_shfo_tests();
394
395     init_shfo_tests();
396     test_delete();
397     clean_after_shfo_tests();
398
399     init_shfo_tests();
400     test_rename();
401     clean_after_shfo_tests();
402
403     init_shfo_tests();
404     test_copy();
405     clean_after_shfo_tests();
406
407     init_shfo_tests();
408     test_move();
409     clean_after_shfo_tests();
410 }