ok() does not support '%S'. Store the Ansi version, convert to Unicode
[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
215     shfo.hwnd = NULL;
216     shfo.wFunc = FO_COPY;
217     shfo.pFrom = from;
218     shfo.pTo = to;
219     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
220     shfo.hNameMappings = NULL;
221     shfo.lpszProgressTitle = NULL;
222
223     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
224     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
225     ok(SHFileOperationA(&shfo), "Can't copy many files");
226     ok(!file_exists(".\\test6.txt"), "The file is not copied - many files are "
227        "specified as a target");
228
229     memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
230     shfo2.fFlags |= FOF_MULTIDESTFILES;
231
232     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
233     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
234     ok(!SHFileOperationA(&shfo2), "Can't copy many files");
235     ok(file_exists(".\\test6.txt"), "The file is copied - many files are "
236        "specified as a target");
237     DeleteFileA(".\\test6.txt");
238     DeleteFileA(".\\test7.txt");
239     RemoveDirectoryA(".\\test8.txt");
240
241     /* number of sources do not correspond to number of targets */
242     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
243     set_curr_dir_path(to, "test6.txt\0test7.txt\0");
244     ok(SHFileOperationA(&shfo2), "Can't copy many files");
245     ok(!file_exists(".\\test6.txt"), "The file is not copied - many files are "
246        "specified as a target");
247
248     set_curr_dir_path(from, "test1.txt\0");
249     set_curr_dir_path(to, "test4.txt\0");
250     ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are copied recursively");
251     ok(file_exists(".\\test4.txt\\test1.txt"), "The file is copied");
252
253     set_curr_dir_path(from, "test?.txt\0");
254     set_curr_dir_path(to, "testdir2\0");
255     ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet");
256     ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not copied yet");
257     ok(!SHFileOperationA(&shfo), "Files and directories are copied to directory ");
258     ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied");
259     ok(file_exists(".\\testdir2\\test4.txt"), "The directory is copied");
260     ok(file_exists(".\\testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is copied");
261     clean_after_shfo_tests();
262
263     init_shfo_tests();
264     shfo.fFlags |= FOF_FILESONLY;
265     ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet");
266     ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not copied yet");
267     ok(!SHFileOperationA(&shfo), "Files are copied to other directory ");
268     ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied");
269     ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is copied");
270     clean_after_shfo_tests();
271
272     init_shfo_tests();
273     set_curr_dir_path(from, "test1.txt\0test2.txt\0");
274     ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet");
275     ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not copied yet");
276     ok(!SHFileOperationA(&shfo), "Files are copied to other directory ");
277     ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied");
278     ok(file_exists(".\\testdir2\\test2.txt"), "The file is copied");
279     clean_after_shfo_tests();
280
281     init_shfo_tests();
282     tmp_flags = shfo.fFlags;
283     set_curr_dir_path(from, "test1.txt\0test10.txt\0test2.txt\0");
284     ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet");
285     ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not copied yet");
286     ok(!SHFileOperationA(&shfo), "Files are copied to other directory ");
287     ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied");
288     ok(!file_exists(".\\testdir2\\test2.txt"), "The file is copied");
289     shfo.fFlags = tmp_flags;
290 }
291
292 /* tests the FO_MOVE action */
293 void test_move(void)
294 {
295     SHFILEOPSTRUCTA shfo, shfo2;
296     CHAR from[MAX_PATH];
297     CHAR to[MAX_PATH];
298
299     shfo.hwnd = NULL;
300     shfo.wFunc = FO_MOVE;
301     shfo.pFrom = from;
302     shfo.pTo = to;
303     shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
304     shfo.hNameMappings = NULL;
305     shfo.lpszProgressTitle = NULL;
306
307     set_curr_dir_path(from, "test1.txt\0");
308     set_curr_dir_path(to, "test4.txt\0");
309     ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are moved recursively");
310     ok(file_exists(".\\test4.txt\\test1.txt"), "The file is moved");
311
312     set_curr_dir_path(from, "test?.txt\0");
313     set_curr_dir_path(to, "testdir2\0");
314     ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not moved yet");
315     ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not moved yet");
316     ok(!SHFileOperationA(&shfo), "Files and directories are moved to directory ");
317     ok(file_exists(".\\testdir2\\test2.txt"), "The file is moved");
318     ok(file_exists(".\\testdir2\\test4.txt"), "The directory is moved");
319     ok(file_exists(".\\testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is moved");
320
321     clean_after_shfo_tests();
322     init_shfo_tests();
323
324     memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
325     shfo2.fFlags |= FOF_MULTIDESTFILES;
326
327     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
328     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
329     ok(!SHFileOperationA(&shfo2), "Move many files");
330     ok(file_exists(".\\test6.txt"), "The file is moved - many files are "
331        "specified as a target");
332     DeleteFileA(".\\test6.txt");
333     DeleteFileA(".\\test7.txt");
334     RemoveDirectoryA(".\\test8.txt");
335
336     init_shfo_tests();
337
338     /* number of sources do not correspond to number of targets */
339     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
340     set_curr_dir_path(to, "test6.txt\0test7.txt\0");
341     ok(SHFileOperationA(&shfo2), "Can't move many files");
342     ok(!file_exists(".\\test6.txt"), "The file is not moved - many files are "
343        "specified as a target");
344
345     init_shfo_tests();
346
347     set_curr_dir_path(from, "test3.txt\0");
348     set_curr_dir_path(to, "test4.txt\\test1.txt\0");
349     ok(!SHFileOperationA(&shfo), "File is moved moving to other directory");
350     ok(file_exists(".\\test4.txt\\test1.txt"), "The file is moved");
351
352     set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
353     set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
354     ok(SHFileOperationA(&shfo), "Can not move many files");
355     ok(file_exists(".\\test1.txt"), "The file is not moved. Many files are specified ");
356     ok(file_exists(".\\test4.txt"), "The directory not is moved. Many files are specified ");
357
358     set_curr_dir_path(from, "test1.txt\0");
359     set_curr_dir_path(to, "test6.txt\0");
360     ok(!SHFileOperationA(&shfo), "Move file");
361     ok(!file_exists(".\\test1.txt"), "The file is moved");
362     ok(file_exists(".\\test6.txt"), "The file is moved ");
363     set_curr_dir_path(from, "test6.txt\0");
364     set_curr_dir_path(to, "test1.txt\0");
365     ok(!SHFileOperationA(&shfo), "Move file back");
366
367     set_curr_dir_path(from, "test4.txt\0");
368     set_curr_dir_path(to, "test6.txt\0");
369     ok(!SHFileOperationA(&shfo), "Move dir");
370     ok(!file_exists(".\\test4.txt"), "The dir is moved");
371     ok(file_exists(".\\test6.txt"), "The dir is moved ");
372     set_curr_dir_path(from, "test6.txt\0");
373     set_curr_dir_path(to, "test4.txt\0");
374     ok(!SHFileOperationA(&shfo), "Move dir back");
375 }
376
377 START_TEST(shlfileop)
378 {
379     clean_after_shfo_tests();
380
381     init_shfo_tests();
382     test_delete();
383     clean_after_shfo_tests();
384
385     init_shfo_tests();
386     test_rename();
387     clean_after_shfo_tests();
388
389     init_shfo_tests();
390     test_copy();
391     clean_after_shfo_tests();
392
393     init_shfo_tests();
394     test_move();
395     clean_after_shfo_tests();
396 }