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