2 * Unit test of the SHFileOperation function.
4 * Copyright 2002 Andriy Palamarchuk
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.
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.
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
24 #define WINE_NOWINSOCK
30 #include "wine/test.h"
32 CHAR CURR_DIR[MAX_PATH];
34 /* creates a file with the specified name for tests */
35 void createTestFile(CHAR *name)
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);
47 BOOL file_exists(CHAR *name)
49 return GetFileAttributesA(name) != INVALID_FILE_ATTRIBUTES;
52 /* initializes the tests */
53 void init_shfo_tests(void)
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);
63 /* cleans after tests */
64 void clean_after_shfo_tests(void)
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");
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
86 void set_curr_dir_path(CHAR *buf, CHAR* files)
91 strcpy(buf, CURR_DIR);
96 buf += strlen(buf) + 1;
97 files += strlen(files) + 1;
103 /* tests the FO_DELETE action */
104 void test_delete(void)
106 SHFILEOPSTRUCTA shfo;
109 sprintf(buf, "%s\\%s", CURR_DIR, "test?.txt");
110 buf[strlen(buf) + 1] = '\0';
113 shfo.wFunc = FO_DELETE;
116 shfo.fFlags = FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_SILENT;
117 shfo.hNameMappings = NULL;
118 shfo.lpszProgressTitle = NULL;
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");
124 ok(!SHFileOperationA(&shfo), "Directory exists, but is not removed\n");
125 ok(file_exists(".\\test4.txt"), "Directory should not be removed\n");
127 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
129 ok(!SHFileOperationA(&shfo), "Directory removed\n");
130 ok(!file_exists(".\\test4.txt"), "Directory should be removed\n");
132 ok(!SHFileOperationA(&shfo), "The requested file does not exist\n");
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");
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");
149 /* tests the FO_RENAME action */
152 SHFILEOPSTRUCTA shfo, shfo2;
158 shfo.wFunc = FO_RENAME;
161 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
162 shfo.hNameMappings = NULL;
163 shfo.lpszProgressTitle = NULL;
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");
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");
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");
182 memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
183 shfo2.fFlags |= FOF_MULTIDESTFILES;
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");
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");
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");
210 /* tests the FO_COPY action */
213 SHFILEOPSTRUCTA shfo, shfo2;
216 FILEOP_FLAGS tmp_flags;
220 shfo.wFunc = FO_COPY;
223 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
224 shfo.hNameMappings = NULL;
225 shfo.lpszProgressTitle = NULL;
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");
233 memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
234 shfo2.fFlags |= FOF_MULTIDESTFILES;
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");
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");
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");
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();
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();
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();
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 */
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);
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");
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");
302 ok(!file_exists(".\\testdir2\\test2.txt"), "The file is copied\n");
303 shfo.fFlags = tmp_flags;
306 /* tests the FO_MOVE action */
309 SHFILEOPSTRUCTA shfo, shfo2;
314 shfo.wFunc = FO_MOVE;
317 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
318 shfo.hNameMappings = NULL;
319 shfo.lpszProgressTitle = NULL;
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");
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");
335 clean_after_shfo_tests();
338 memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
339 shfo2.fFlags |= FOF_MULTIDESTFILES;
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");
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");
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");
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");
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");
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");
391 START_TEST(shlfileop)
393 clean_after_shfo_tests();
397 clean_after_shfo_tests();
401 clean_after_shfo_tests();
405 clean_after_shfo_tests();
409 clean_after_shfo_tests();