Added version information.
[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 <stdio.h>
22  
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wtypes.h"
26 #include "shellapi.h"
27
28 #include "wine/test.h"
29
30 CHAR CURR_DIR[MAX_PATH];
31
32 /* creates a file with the specified name for tests */
33 void createTestFile(CHAR *name)
34 {
35     HANDLE file;
36     DWORD written;
37     CHAR msg[MAX_PATH];
38
39     file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
40     sprintf(msg, "Failure to open file %s", name);
41     ok(file != INVALID_HANDLE_VALUE, msg);
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) != 0xFFFFFFFF;
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");
121     ok(file_exists(".\\test4.txt"), "Directory should not be removed");
122     ok(!file_exists(".\\test1.txt"), "File should be removed");
123
124     ok(!SHFileOperationA(&shfo), "Directory exists, but is not removed");
125     ok(file_exists(".\\test4.txt"), "Directory should not be removed");
126
127     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
128
129     ok(!SHFileOperationA(&shfo), "Directory removed");
130     ok(!file_exists(".\\test4.txt"), "Directory should be removed");
131
132     ok(!SHFileOperationA(&shfo), "The requested file does not exist");
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");
138     ok(!SHFileOperationA(&shfo), "Directory removed");
139     ok(!file_exists(".\\test4.txt"), "Directory is removed");
140
141     init_shfo_tests();
142     shfo.pFrom = ".\\test1.txt\0.\\test4.txt\0";
143     ok(!SHFileOperationA(&shfo), "Directory and a file removed");
144     ok(!file_exists(".\\test1.txt"), "The file should be removed");
145     ok(!file_exists(".\\test4.txt"), "Directory should be removed");
146     ok(file_exists(".\\test2.txt"), "This file should not be removed");
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
156     shfo.hwnd = NULL;
157     shfo.wFunc = FO_RENAME;
158     shfo.pFrom = from;
159     shfo.pTo = to;
160     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
161     shfo.hNameMappings = NULL;
162     shfo.lpszProgressTitle = NULL;
163
164     set_curr_dir_path(from, "test1.txt\0");
165     set_curr_dir_path(to, "test4.txt\0");
166     ok(SHFileOperationA(&shfo), "File is not renamed moving to other directory "
167        "when specifying directory name only");
168     ok(file_exists(".\\test1.txt"), "The file is not removed");
169
170     set_curr_dir_path(from, "test3.txt\0");
171     set_curr_dir_path(to, "test4.txt\\test1.txt\0");
172     ok(!SHFileOperationA(&shfo), "File is renamed moving to other directory");
173     ok(file_exists(".\\test4.txt\\test1.txt"), "The file is renamed");
174
175     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
176     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
177     ok(SHFileOperationA(&shfo), "Can't rename many files");
178     ok(file_exists(".\\test1.txt"), "The file is not renamed - many files are specified ");
179
180     memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
181     shfo2.fFlags |= FOF_MULTIDESTFILES;
182
183     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
184     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
185     ok(SHFileOperationA(&shfo2), "Can't rename many files");
186     ok(file_exists(".\\test1.txt"), "The file is not renamed - many files are specified ");
187
188     set_curr_dir_path(from, "test1.txt\0");
189     set_curr_dir_path(to, "test6.txt\0");
190     ok(!SHFileOperationA(&shfo), "Rename file");
191     ok(!file_exists(".\\test1.txt"), "The file is renamed");
192     ok(file_exists(".\\test6.txt"), "The file is renamed ");
193     set_curr_dir_path(from, "test6.txt\0");
194     set_curr_dir_path(to, "test1.txt\0");
195     ok(!SHFileOperationA(&shfo), "Rename file back");
196
197     set_curr_dir_path(from, "test4.txt\0");
198     set_curr_dir_path(to, "test6.txt\0");
199     ok(!SHFileOperationA(&shfo), "Rename dir");
200     ok(!file_exists(".\\test4.txt"), "The dir is renamed");
201     ok(file_exists(".\\test6.txt"), "The dir is renamed ");
202     set_curr_dir_path(from, "test6.txt\0");
203     set_curr_dir_path(to, "test4.txt\0");
204     ok(!SHFileOperationA(&shfo), "Rename dir back");
205 }
206
207 /* tests the FO_COPY action */
208 void test_copy(void)
209 {
210     SHFILEOPSTRUCTA shfo, shfo2;
211     CHAR from[MAX_PATH];
212     CHAR to[MAX_PATH];
213     FILEOP_FLAGS tmp_flags;
214     DWORD retval;
215
216     shfo.hwnd = NULL;
217     shfo.wFunc = FO_COPY;
218     shfo.pFrom = from;
219     shfo.pTo = to;
220     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
221     shfo.hNameMappings = NULL;
222     shfo.lpszProgressTitle = NULL;
223
224     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
225     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
226     ok(SHFileOperationA(&shfo), "Can't copy many files");
227     ok(!file_exists(".\\test6.txt"), "The file is not copied - many files are "
228        "specified as a target");
229
230     memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
231     shfo2.fFlags |= FOF_MULTIDESTFILES;
232
233     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
234     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
235     ok(!SHFileOperationA(&shfo2), "Can't copy many files");
236     ok(file_exists(".\\test6.txt"), "The file is copied - many files are "
237        "specified as a target");
238     DeleteFileA(".\\test6.txt");
239     DeleteFileA(".\\test7.txt");
240     RemoveDirectoryA(".\\test8.txt");
241
242     /* number of sources do not correspond to number of targets */
243     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
244     set_curr_dir_path(to, "test6.txt\0test7.txt\0");
245     ok(SHFileOperationA(&shfo2), "Can't copy many files");
246     ok(!file_exists(".\\test6.txt"), "The file is not copied - many files are "
247        "specified as a target");
248
249     set_curr_dir_path(from, "test1.txt\0");
250     set_curr_dir_path(to, "test4.txt\0");
251     ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are copied recursively");
252     ok(file_exists(".\\test4.txt\\test1.txt"), "The file is copied");
253
254     set_curr_dir_path(from, "test?.txt\0");
255     set_curr_dir_path(to, "testdir2\0");
256     ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet");
257     ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not copied yet");
258     ok(!SHFileOperationA(&shfo), "Files and directories are copied to directory ");
259     ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied");
260     ok(file_exists(".\\testdir2\\test4.txt"), "The directory is copied");
261     ok(file_exists(".\\testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is copied");
262     clean_after_shfo_tests();
263
264     init_shfo_tests();
265     shfo.fFlags |= FOF_FILESONLY;
266     ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet");
267     ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not copied yet");
268     ok(!SHFileOperationA(&shfo), "Files are copied to other directory ");
269     ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied");
270     ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is copied");
271     clean_after_shfo_tests();
272
273     init_shfo_tests();
274     set_curr_dir_path(from, "test1.txt\0test2.txt\0");
275     ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet");
276     ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not copied yet");
277     ok(!SHFileOperationA(&shfo), "Files are copied to other directory ");
278     ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied");
279     ok(file_exists(".\\testdir2\\test2.txt"), "The file is copied");
280     clean_after_shfo_tests();
281
282     /* Copying multiple files with one not existing as source, fails the
283        entire operation in Win98/ME/2K/XP, but not in 95/NT */
284     init_shfo_tests();
285     tmp_flags = shfo.fFlags;
286     set_curr_dir_path(from, "test1.txt\0test10.txt\0test2.txt\0");
287     ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet");
288     ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not copied yet");
289     retval = SHFileOperationA(&shfo);
290     if (!retval)
291       /* Win 95/NT returns success but copies only the files up to the nonexisting source */
292       ok(file_exists(".\\testdir2\\test1.txt"), "The file is not copied");
293     else
294     {
295       /* Win 98/ME/2K/XP fail the entire operation with return code 1026 if one source file does not exist */
296       ok(retval == 1026, "Files are copied to other directory ");
297       ok(!file_exists(".\\testdir2\\test1.txt"), "The file is copied");
298     }
299     ok(!file_exists(".\\testdir2\\test2.txt"), "The file is copied");
300     shfo.fFlags = tmp_flags;
301 }
302
303 /* tests the FO_MOVE action */
304 void test_move(void)
305 {
306     SHFILEOPSTRUCTA shfo, shfo2;
307     CHAR from[MAX_PATH];
308     CHAR to[MAX_PATH];
309
310     shfo.hwnd = NULL;
311     shfo.wFunc = FO_MOVE;
312     shfo.pFrom = from;
313     shfo.pTo = to;
314     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
315     shfo.hNameMappings = NULL;
316     shfo.lpszProgressTitle = NULL;
317
318     set_curr_dir_path(from, "test1.txt\0");
319     set_curr_dir_path(to, "test4.txt\0");
320     ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are moved recursively");
321     ok(file_exists(".\\test4.txt\\test1.txt"), "The file is moved");
322
323     set_curr_dir_path(from, "test?.txt\0");
324     set_curr_dir_path(to, "testdir2\0");
325     ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not moved yet");
326     ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not moved yet");
327     ok(!SHFileOperationA(&shfo), "Files and directories are moved to directory ");
328     ok(file_exists(".\\testdir2\\test2.txt"), "The file is moved");
329     ok(file_exists(".\\testdir2\\test4.txt"), "The directory is moved");
330     ok(file_exists(".\\testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is moved");
331
332     clean_after_shfo_tests();
333     init_shfo_tests();
334
335     memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
336     shfo2.fFlags |= FOF_MULTIDESTFILES;
337
338     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
339     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
340     ok(!SHFileOperationA(&shfo2), "Move many files");
341     ok(file_exists(".\\test6.txt"), "The file is moved - many files are "
342        "specified as a target");
343     DeleteFileA(".\\test6.txt");
344     DeleteFileA(".\\test7.txt");
345     RemoveDirectoryA(".\\test8.txt");
346
347     init_shfo_tests();
348
349     /* number of sources do not correspond to number of targets */
350     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
351     set_curr_dir_path(to, "test6.txt\0test7.txt\0");
352     ok(SHFileOperationA(&shfo2), "Can't move many files");
353     ok(!file_exists(".\\test6.txt"), "The file is not moved - many files are "
354        "specified as a target");
355
356     init_shfo_tests();
357
358     set_curr_dir_path(from, "test3.txt\0");
359     set_curr_dir_path(to, "test4.txt\\test1.txt\0");
360     ok(!SHFileOperationA(&shfo), "File is moved moving to other directory");
361     ok(file_exists(".\\test4.txt\\test1.txt"), "The file is moved");
362
363     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
364     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
365     ok(SHFileOperationA(&shfo), "Can not move many files");
366     ok(file_exists(".\\test1.txt"), "The file is not moved. Many files are specified ");
367     ok(file_exists(".\\test4.txt"), "The directory not is moved. Many files are specified ");
368
369     set_curr_dir_path(from, "test1.txt\0");
370     set_curr_dir_path(to, "test6.txt\0");
371     ok(!SHFileOperationA(&shfo), "Move file");
372     ok(!file_exists(".\\test1.txt"), "The file is moved");
373     ok(file_exists(".\\test6.txt"), "The file is moved ");
374     set_curr_dir_path(from, "test6.txt\0");
375     set_curr_dir_path(to, "test1.txt\0");
376     ok(!SHFileOperationA(&shfo), "Move file back");
377
378     set_curr_dir_path(from, "test4.txt\0");
379     set_curr_dir_path(to, "test6.txt\0");
380     ok(!SHFileOperationA(&shfo), "Move dir");
381     ok(!file_exists(".\\test4.txt"), "The dir is moved");
382     ok(file_exists(".\\test6.txt"), "The dir is moved ");
383     set_curr_dir_path(from, "test6.txt\0");
384     set_curr_dir_path(to, "test4.txt\0");
385     ok(!SHFileOperationA(&shfo), "Move dir back");
386 }
387
388 START_TEST(shlfileop)
389 {
390     clean_after_shfo_tests();
391
392     init_shfo_tests();
393     test_delete();
394     clean_after_shfo_tests();
395
396     init_shfo_tests();
397     test_rename();
398     clean_after_shfo_tests();
399
400     init_shfo_tests();
401     test_copy();
402     clean_after_shfo_tests();
403
404     init_shfo_tests();
405     test_move();
406     clean_after_shfo_tests();
407 }