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
31 #include "wine/test.h"
33 CHAR CURR_DIR[MAX_PATH];
35 static HMODULE hshell32;
36 static int (WINAPI *pSHCreateDirectoryExA)(HWND, LPCSTR, LPSECURITY_ATTRIBUTES);
38 static void InitFunctionPointers(void)
40 hshell32 = GetModuleHandleA("shell32.dll");
43 pSHCreateDirectoryExA = (void*)GetProcAddress(hshell32, "SHCreateDirectoryExA");
46 /* creates a file with the specified name for tests */
47 static void createTestFile(const CHAR *name)
52 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
53 ok(file != INVALID_HANDLE_VALUE, "Failure to open file %s\n", name);
54 WriteFile(file, name, strlen(name), &written, NULL);
55 WriteFile(file, "\n", strlen("\n"), &written, NULL);
59 static BOOL file_exists(const CHAR *name)
61 return GetFileAttributesA(name) != INVALID_FILE_ATTRIBUTES;
64 /* initializes the tests */
65 static void init_shfo_tests(void)
69 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
70 len = lstrlenA(CURR_DIR);
72 if(len && (CURR_DIR[len-1] == '\\'))
75 createTestFile(".\\test1.txt");
76 createTestFile(".\\test2.txt");
77 createTestFile(".\\test3.txt");
78 CreateDirectoryA(".\\test4.txt", NULL);
79 CreateDirectoryA(".\\testdir2", NULL);
82 /* cleans after tests */
83 static void clean_after_shfo_tests(void)
85 DeleteFileA(".\\test1.txt");
86 DeleteFileA(".\\test2.txt");
87 DeleteFileA(".\\test3.txt");
88 DeleteFileA(".\\test4.txt\\test1.txt");
89 DeleteFileA(".\\test4.txt\\test2.txt");
90 DeleteFileA(".\\test4.txt\\test3.txt");
91 RemoveDirectoryA(".\\test4.txt");
92 DeleteFileA(".\\testdir2\\test1.txt");
93 DeleteFileA(".\\testdir2\\test2.txt");
94 DeleteFileA(".\\testdir2\\test3.txt");
95 DeleteFileA(".\\testdir2\\test4.txt\\test1.txt");
96 RemoveDirectoryA(".\\testdir2\\test4.txt");
97 RemoveDirectoryA(".\\testdir2");
98 DeleteFileA(".\\nonexistent\\test2.txt");
99 RemoveDirectoryA(".\\nonexistent");
103 puts into the specified buffer file names with current directory.
104 files - string with file names, separated by null characters. Ends on a double
107 static void set_curr_dir_path(CHAR *buf, const CHAR* files)
112 strcpy(buf, CURR_DIR);
117 buf += strlen(buf) + 1;
118 files += strlen(files) + 1;
124 /* tests the FO_DELETE action */
125 static void test_delete(void)
127 SHFILEOPSTRUCTA shfo;
131 sprintf(buf, "%s\\%s", CURR_DIR, "test?.txt");
132 buf[strlen(buf) + 1] = '\0';
135 shfo.wFunc = FO_DELETE;
138 shfo.fFlags = FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_SILENT;
139 shfo.hNameMappings = NULL;
140 shfo.lpszProgressTitle = NULL;
142 ok(!SHFileOperationA(&shfo), "Deletion was successful\n");
143 ok(file_exists(".\\test4.txt"), "Directory should not be removed\n");
144 ok(!file_exists(".\\test1.txt"), "File should be removed\n");
146 ret = SHFileOperationA(&shfo);
147 ok(!ret, "Directory exists, but is not removed, ret=%ld\n", ret);
148 ok(file_exists(".\\test4.txt"), "Directory should not be removed\n");
150 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
152 ok(!SHFileOperationA(&shfo), "Directory removed\n");
153 ok(!file_exists(".\\test4.txt"), "Directory should be removed\n");
155 ret = SHFileOperationA(&shfo);
156 ok(!ret, "The requested file does not exist, ret=%ld\n", ret);
159 sprintf(buf, "%s\\%s", CURR_DIR, "test4.txt");
160 buf[strlen(buf) + 1] = '\0';
161 ok(MoveFileA(".\\test1.txt", ".\\test4.txt\\test1.txt"), "Fill the subdirectory\n");
162 ok(!SHFileOperationA(&shfo), "Directory removed\n");
163 ok(!file_exists(".\\test4.txt"), "Directory is removed\n");
166 shfo.pFrom = ".\\test1.txt\0.\\test4.txt\0";
167 ok(!SHFileOperationA(&shfo), "Directory and a file removed\n");
168 ok(!file_exists(".\\test1.txt"), "The file should be removed\n");
169 ok(!file_exists(".\\test4.txt"), "Directory should be removed\n");
170 ok(file_exists(".\\test2.txt"), "This file should not be removed\n");
173 /* tests the FO_RENAME action */
174 static void test_rename(void)
176 SHFILEOPSTRUCTA shfo, shfo2;
182 shfo.wFunc = FO_RENAME;
185 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
186 shfo.hNameMappings = NULL;
187 shfo.lpszProgressTitle = NULL;
189 set_curr_dir_path(from, "test1.txt\0");
190 set_curr_dir_path(to, "test4.txt\0");
191 ok(SHFileOperationA(&shfo), "File is not renamed moving to other directory "
192 "when specifying directory name only\n");
193 ok(file_exists(".\\test1.txt"), "The file is removed\n");
195 set_curr_dir_path(from, "test3.txt\0");
196 set_curr_dir_path(to, "test4.txt\\test1.txt\0");
197 ok(!SHFileOperationA(&shfo), "File is renamed moving to other directory\n");
198 ok(file_exists(".\\test4.txt\\test1.txt"), "The file is not renamed\n");
200 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
201 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
202 retval = SHFileOperationA(&shfo); /* W98 returns 0, W2K and newer returns ERROR_GEN_FAILURE, both do nothing */
203 ok(!retval || retval == ERROR_GEN_FAILURE || retval == ERROR_INVALID_TARGET_HANDLE,
204 "Can't rename many files, retval = %ld\n", retval);
205 ok(file_exists(".\\test1.txt"), "The file is renamed - many files are specified\n");
207 memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
208 shfo2.fFlags |= FOF_MULTIDESTFILES;
210 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
211 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
212 retval = SHFileOperationA(&shfo2); /* W98 returns 0, W2K and newer returns ERROR_GEN_FAILURE, both do nothing */
213 ok(!retval || retval == ERROR_GEN_FAILURE || retval == ERROR_INVALID_TARGET_HANDLE,
214 "Can't rename many files, retval = %ld\n", retval);
215 ok(file_exists(".\\test1.txt"), "The file is not renamed - many files are specified\n");
217 set_curr_dir_path(from, "test1.txt\0");
218 set_curr_dir_path(to, "test6.txt\0");
219 retval = SHFileOperationA(&shfo);
220 ok(!retval, "Rename file failed, retval = %ld\n", retval);
221 ok(!file_exists(".\\test1.txt"), "The file is not renamed\n");
222 ok(file_exists(".\\test6.txt"), "The file is not renamed\n");
224 set_curr_dir_path(from, "test6.txt\0");
225 set_curr_dir_path(to, "test1.txt\0");
226 retval = SHFileOperationA(&shfo);
227 ok(!retval, "Rename file back failed, retval = %ld\n", retval);
229 set_curr_dir_path(from, "test4.txt\0");
230 set_curr_dir_path(to, "test6.txt\0");
231 retval = SHFileOperationA(&shfo);
232 ok(!retval, "Rename dir failed, retval = %ld\n", retval);
233 ok(!file_exists(".\\test4.txt"), "The dir is not renamed\n");
234 ok(file_exists(".\\test6.txt"), "The dir is not renamed\n");
236 set_curr_dir_path(from, "test6.txt\0");
237 set_curr_dir_path(to, "test4.txt\0");
238 retval = SHFileOperationA(&shfo);
239 ok(!retval, "Rename dir back failed, retval = %ld\n", retval);
242 /* tests the FO_COPY action */
243 static void test_copy(void)
245 SHFILEOPSTRUCTA shfo, shfo2;
248 FILEOP_FLAGS tmp_flags;
252 shfo.wFunc = FO_COPY;
255 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
256 shfo.hNameMappings = NULL;
257 shfo.lpszProgressTitle = NULL;
259 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
260 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
261 ok(SHFileOperationA(&shfo), "Can't copy many files\n");
262 ok(!file_exists(".\\test6.txt"), "The file is not copied - many files are "
263 "specified as a target\n");
265 memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
266 shfo2.fFlags |= FOF_MULTIDESTFILES;
268 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
269 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
270 ok(!SHFileOperationA(&shfo2), "Can't copy many files\n");
271 ok(file_exists(".\\test6.txt"), "The file is copied - many files are "
272 "specified as a target\n");
273 DeleteFileA(".\\test6.txt");
274 DeleteFileA(".\\test7.txt");
275 RemoveDirectoryA(".\\test8.txt");
277 /* number of sources do not correspond to number of targets */
278 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
279 set_curr_dir_path(to, "test6.txt\0test7.txt\0");
280 ok(SHFileOperationA(&shfo2), "Can't copy many files\n");
281 ok(!file_exists(".\\test6.txt"), "The file is not copied - many files are "
282 "specified as a target\n");
284 set_curr_dir_path(from, "test1.txt\0");
285 set_curr_dir_path(to, "test4.txt\0");
286 ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are copied recursively\n");
287 ok(file_exists(".\\test4.txt\\test1.txt"), "The file is copied\n");
289 set_curr_dir_path(from, "test?.txt\0");
290 set_curr_dir_path(to, "testdir2\0");
291 ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet\n");
292 ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not copied yet\n");
293 ok(!SHFileOperationA(&shfo), "Files and directories are copied to directory\n");
294 ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied\n");
295 ok(file_exists(".\\testdir2\\test4.txt"), "The directory is copied\n");
296 ok(file_exists(".\\testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is copied\n");
297 clean_after_shfo_tests();
300 shfo.fFlags |= FOF_FILESONLY;
301 ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet\n");
302 ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not copied yet\n");
303 ok(!SHFileOperationA(&shfo), "Files are copied to other directory\n");
304 ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied\n");
305 ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is copied\n");
306 clean_after_shfo_tests();
309 set_curr_dir_path(from, "test1.txt\0test2.txt\0");
310 ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet\n");
311 ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not copied yet\n");
312 ok(!SHFileOperationA(&shfo), "Files are copied to other directory\n");
313 ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied\n");
314 ok(file_exists(".\\testdir2\\test2.txt"), "The file is copied\n");
315 clean_after_shfo_tests();
317 /* Copying multiple files with one not existing as source, fails the
318 entire operation in Win98/ME/2K/XP, but not in 95/NT */
320 tmp_flags = shfo.fFlags;
321 set_curr_dir_path(from, "test1.txt\0test10.txt\0test2.txt\0");
322 ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet\n");
323 ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not copied yet\n");
324 retval = SHFileOperationA(&shfo);
326 /* Win 95/NT returns success but copies only the files up to the nonexistent source */
327 ok(file_exists(".\\testdir2\\test1.txt"), "The file is not copied\n");
330 /* Win 98/ME/2K/XP fail the entire operation with return code 1026 if one source file does not exist */
331 ok(retval == 1026, "Files are copied to other directory\n");
332 ok(!file_exists(".\\testdir2\\test1.txt"), "The file is copied\n");
334 ok(!file_exists(".\\testdir2\\test2.txt"), "The file is copied\n");
335 shfo.fFlags = tmp_flags;
337 /* copy into a nonexistent directory */
339 shfo.fFlags = FOF_NOCONFIRMMKDIR;
340 set_curr_dir_path(from, "test1.txt\0");
341 set_curr_dir_path(to, "nonexistent\\test2.txt\0");
342 retval= SHFileOperation(&shfo);
346 ok(!retval, "Error copying into nonexistent directory\n");
347 ok(file_exists(".\\nonexistent\\test2.txt"), "Directory not created\n");
351 /* tests the FO_MOVE action */
352 static void test_move(void)
354 SHFILEOPSTRUCTA shfo, shfo2;
359 shfo.wFunc = FO_MOVE;
362 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
363 shfo.hNameMappings = NULL;
364 shfo.lpszProgressTitle = NULL;
366 set_curr_dir_path(from, "test1.txt\0");
367 set_curr_dir_path(to, "test4.txt\0");
368 ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are moved recursively\n");
369 ok(file_exists(".\\test4.txt\\test1.txt"), "The file is moved\n");
371 set_curr_dir_path(from, "test?.txt\0");
372 set_curr_dir_path(to, "testdir2\0");
373 ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not moved yet\n");
374 ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not moved yet\n");
375 ok(!SHFileOperationA(&shfo), "Files and directories are moved to directory\n");
376 ok(file_exists(".\\testdir2\\test2.txt"), "The file is moved\n");
377 ok(file_exists(".\\testdir2\\test4.txt"), "The directory is moved\n");
378 ok(file_exists(".\\testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is moved\n");
380 clean_after_shfo_tests();
383 memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
384 shfo2.fFlags |= FOF_MULTIDESTFILES;
386 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
387 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
388 ok(!SHFileOperationA(&shfo2), "Move many files\n");
389 ok(file_exists(".\\test6.txt"), "The file is moved - many files are "
390 "specified as a target\n");
391 DeleteFileA(".\\test6.txt");
392 DeleteFileA(".\\test7.txt");
393 RemoveDirectoryA(".\\test8.txt");
397 /* number of sources do not correspond to number of targets */
398 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
399 set_curr_dir_path(to, "test6.txt\0test7.txt\0");
400 ok(SHFileOperationA(&shfo2), "Can't move many files\n");
401 ok(!file_exists(".\\test6.txt"), "The file is not moved - many files are "
402 "specified as a target\n");
406 set_curr_dir_path(from, "test3.txt\0");
407 set_curr_dir_path(to, "test4.txt\\test1.txt\0");
408 ok(!SHFileOperationA(&shfo), "File is moved moving to other directory\n");
409 ok(file_exists(".\\test4.txt\\test1.txt"), "The file is moved\n");
411 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
412 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
413 ok(SHFileOperationA(&shfo), "Cannot move many files\n");
414 ok(file_exists(".\\test1.txt"), "The file is not moved. Many files are specified\n");
415 ok(file_exists(".\\test4.txt"), "The directory is not moved. Many files are specified\n");
417 set_curr_dir_path(from, "test1.txt\0");
418 set_curr_dir_path(to, "test6.txt\0");
419 ok(!SHFileOperationA(&shfo), "Move file\n");
420 ok(!file_exists(".\\test1.txt"), "The file is moved\n");
421 ok(file_exists(".\\test6.txt"), "The file is moved\n");
422 set_curr_dir_path(from, "test6.txt\0");
423 set_curr_dir_path(to, "test1.txt\0");
424 ok(!SHFileOperationA(&shfo), "Move file back\n");
426 set_curr_dir_path(from, "test4.txt\0");
427 set_curr_dir_path(to, "test6.txt\0");
428 ok(!SHFileOperationA(&shfo), "Move dir\n");
429 ok(!file_exists(".\\test4.txt"), "The dir is moved\n");
430 ok(file_exists(".\\test6.txt"), "The dir is moved\n");
431 set_curr_dir_path(from, "test6.txt\0");
432 set_curr_dir_path(to, "test4.txt\0");
433 ok(!SHFileOperationA(&shfo), "Move dir back\n");
436 static void test_sh_create_dir(void)
441 if(!pSHCreateDirectoryExA)
443 trace("skipping SHCreateDirectoryExA tests\n");
447 set_curr_dir_path(path, "testdir2\\test4.txt\0");
448 ret = pSHCreateDirectoryExA(NULL, path, NULL);
449 ok(ERROR_SUCCESS == ret, "SHCreateDirectoryEx failed to create directory recursively, ret = %d\n", ret);
450 ok(file_exists(".\\testdir2"), "The first directory is not created\n");
451 ok(file_exists(".\\testdir2\\test4.txt"), "The second directory is not created\n");
453 ret = pSHCreateDirectoryExA(NULL, path, NULL);
454 ok(ERROR_ALREADY_EXISTS == ret, "SHCreateDirectoryEx should fail to create existing directory, ret = %d\n", ret);
457 START_TEST(shlfileop)
459 InitFunctionPointers();
461 clean_after_shfo_tests();
465 clean_after_shfo_tests();
469 clean_after_shfo_tests();
473 clean_after_shfo_tests();
477 clean_after_shfo_tests();
479 test_sh_create_dir();
480 clean_after_shfo_tests();