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)
41 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
42 sprintf(msg, "Failure to open file %s", name);
43 ok(file != INVALID_HANDLE_VALUE, msg);
44 WriteFile(file, name, strlen(name), &written, NULL);
45 WriteFile(file, "\n", strlen("\n"), &written, NULL);
49 BOOL file_exists(CHAR *name)
51 return GetFileAttributesA(name) != 0xFFFFFFFF;
54 /* initializes the tests */
55 void init_shfo_tests(void)
57 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
58 createTestFile(".\\test1.txt");
59 createTestFile(".\\test2.txt");
60 createTestFile(".\\test3.txt");
61 CreateDirectoryA(".\\test4.txt", NULL);
62 CreateDirectoryA(".\\testdir2", NULL);
65 /* cleans after tests */
66 void clean_after_shfo_tests(void)
68 DeleteFileA(".\\test1.txt");
69 DeleteFileA(".\\test2.txt");
70 DeleteFileA(".\\test3.txt");
71 DeleteFileA(".\\test4.txt\\test1.txt");
72 DeleteFileA(".\\test4.txt\\test2.txt");
73 DeleteFileA(".\\test4.txt\\test3.txt");
74 RemoveDirectoryA(".\\test4.txt");
75 DeleteFileA(".\\testdir2\\test1.txt");
76 DeleteFileA(".\\testdir2\\test2.txt");
77 DeleteFileA(".\\testdir2\\test3.txt");
78 DeleteFileA(".\\testdir2\\test4.txt\\test1.txt");
79 RemoveDirectoryA(".\\testdir2\\test4.txt");
80 RemoveDirectoryA(".\\testdir2");
84 puts into the specified buffer file names with current directory.
85 files - string with file names, separated by null characters. Ends on a double
88 void set_curr_dir_path(CHAR *buf, CHAR* files)
93 strcpy(buf, CURR_DIR);
98 buf += strlen(buf) + 1;
99 files += strlen(files) + 1;
105 /* tests the FO_DELETE action */
106 void test_delete(void)
108 SHFILEOPSTRUCTA shfo;
111 sprintf(buf, "%s\\%s", CURR_DIR, "test?.txt");
112 buf[strlen(buf) + 1] = '\0';
115 shfo.wFunc = FO_DELETE;
118 shfo.fFlags = FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_SILENT;
119 shfo.hNameMappings = NULL;
120 shfo.lpszProgressTitle = NULL;
122 ok(!SHFileOperationA(&shfo), "Deletion was successful");
123 ok(file_exists(".\\test4.txt"), "Directory should not be removed");
124 ok(!file_exists(".\\test1.txt"), "File should be removed");
126 ok(!SHFileOperationA(&shfo), "Directory exists, but is not removed");
127 ok(file_exists(".\\test4.txt"), "Directory should not be removed");
129 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
131 ok(!SHFileOperationA(&shfo), "Directory removed");
132 ok(!file_exists(".\\test4.txt"), "Directory should be removed");
134 ok(!SHFileOperationA(&shfo), "The requested file does not exist");
137 sprintf(buf, "%s\\%s", CURR_DIR, "test4.txt");
138 buf[strlen(buf) + 1] = '\0';
139 ok(MoveFileA(".\\test1.txt", ".\\test4.txt\\test1.txt"), "Fill the subdirectory");
140 ok(!SHFileOperationA(&shfo), "Directory removed");
141 ok(!file_exists(".\\test4.txt"), "Directory is removed");
144 shfo.pFrom = ".\\test1.txt\0.\\test4.txt\0";
145 ok(!SHFileOperationA(&shfo), "Directory and a file removed");
146 ok(!file_exists(".\\test1.txt"), "The file should be removed");
147 ok(!file_exists(".\\test4.txt"), "Directory should be removed");
148 ok(file_exists(".\\test2.txt"), "This file should not be removed");
151 /* tests the FO_RENAME action */
154 SHFILEOPSTRUCTA shfo, shfo2;
160 shfo.wFunc = FO_RENAME;
163 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
164 shfo.hNameMappings = NULL;
165 shfo.lpszProgressTitle = NULL;
167 set_curr_dir_path(from, "test1.txt\0");
168 set_curr_dir_path(to, "test4.txt\0");
169 ok(SHFileOperationA(&shfo), "File is not renamed moving to other directory "
170 "when specifying directory name only");
171 ok(file_exists(".\\test1.txt"), "The file is not removed");
173 set_curr_dir_path(from, "test3.txt\0");
174 set_curr_dir_path(to, "test4.txt\\test1.txt\0");
175 ok(!SHFileOperationA(&shfo), "File is renamed moving to other directory");
176 ok(file_exists(".\\test4.txt\\test1.txt"), "The file is renamed");
178 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
179 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
180 retval = SHFileOperationA(&shfo); /* W98 returns 0, W2K and newer returns ERROR_GEN_FAILURE, both do nothing */
181 ok(!retval || retval == ERROR_GEN_FAILURE, "Can't rename many files, retval = %lx", retval);
182 ok(file_exists(".\\test1.txt"), "The file is not renamed - many files are specified ");
184 memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
185 shfo2.fFlags |= FOF_MULTIDESTFILES;
187 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
188 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
189 retval = SHFileOperationA(&shfo2); /* W98 returns 0, W2K and newer returns ERROR_GEN_FAILURE, both do nothing */
190 ok(!retval || retval == ERROR_GEN_FAILURE, "Can't rename many files, retval = %lx", retval);
191 ok(file_exists(".\\test1.txt"), "The file is not renamed - many files are specified ");
193 set_curr_dir_path(from, "test1.txt\0");
194 set_curr_dir_path(to, "test6.txt\0");
195 ok(!SHFileOperationA(&shfo), "Rename file");
196 ok(!file_exists(".\\test1.txt"), "The file is renamed");
197 ok(file_exists(".\\test6.txt"), "The file is renamed");
198 set_curr_dir_path(from, "test6.txt\0");
199 set_curr_dir_path(to, "test1.txt\0");
200 ok(!SHFileOperationA(&shfo), "Rename file back");
202 set_curr_dir_path(from, "test4.txt\0");
203 set_curr_dir_path(to, "test6.txt\0");
204 ok(!SHFileOperationA(&shfo), "Rename dir");
205 ok(!file_exists(".\\test4.txt"), "The dir is renamed");
206 ok(file_exists(".\\test6.txt"), "The dir is renamed ");
207 set_curr_dir_path(from, "test6.txt\0");
208 set_curr_dir_path(to, "test4.txt\0");
209 ok(!SHFileOperationA(&shfo), "Rename dir back");
212 /* tests the FO_COPY action */
215 SHFILEOPSTRUCTA shfo, shfo2;
218 FILEOP_FLAGS tmp_flags;
222 shfo.wFunc = FO_COPY;
225 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
226 shfo.hNameMappings = NULL;
227 shfo.lpszProgressTitle = NULL;
229 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
230 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
231 ok(SHFileOperationA(&shfo), "Can't copy many files");
232 ok(!file_exists(".\\test6.txt"), "The file is not copied - many files are "
233 "specified as a target");
235 memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
236 shfo2.fFlags |= FOF_MULTIDESTFILES;
238 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
239 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
240 ok(!SHFileOperationA(&shfo2), "Can't copy many files");
241 ok(file_exists(".\\test6.txt"), "The file is copied - many files are "
242 "specified as a target");
243 DeleteFileA(".\\test6.txt");
244 DeleteFileA(".\\test7.txt");
245 RemoveDirectoryA(".\\test8.txt");
247 /* number of sources do not correspond to number of targets */
248 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
249 set_curr_dir_path(to, "test6.txt\0test7.txt\0");
250 ok(SHFileOperationA(&shfo2), "Can't copy many files");
251 ok(!file_exists(".\\test6.txt"), "The file is not copied - many files are "
252 "specified as a target");
254 set_curr_dir_path(from, "test1.txt\0");
255 set_curr_dir_path(to, "test4.txt\0");
256 ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are copied recursively");
257 ok(file_exists(".\\test4.txt\\test1.txt"), "The file is copied");
259 set_curr_dir_path(from, "test?.txt\0");
260 set_curr_dir_path(to, "testdir2\0");
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 and directories are copied to directory ");
264 ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied");
265 ok(file_exists(".\\testdir2\\test4.txt"), "The directory is copied");
266 ok(file_exists(".\\testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is copied");
267 clean_after_shfo_tests();
270 shfo.fFlags |= FOF_FILESONLY;
271 ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet");
272 ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not copied yet");
273 ok(!SHFileOperationA(&shfo), "Files are copied to other directory ");
274 ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied");
275 ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is copied");
276 clean_after_shfo_tests();
279 set_curr_dir_path(from, "test1.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 clean_after_shfo_tests();
287 /* Copying multiple files with one not existing as source, fails the
288 entire operation in Win98/ME/2K/XP, but not in 95/NT */
290 tmp_flags = shfo.fFlags;
291 set_curr_dir_path(from, "test1.txt\0test10.txt\0test2.txt\0");
292 ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet");
293 ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not copied yet");
294 retval = SHFileOperationA(&shfo);
296 /* Win 95/NT returns success but copies only the files up to the non-existent source */
297 ok(file_exists(".\\testdir2\\test1.txt"), "The file is not copied");
300 /* Win 98/ME/2K/XP fail the entire operation with return code 1026 if one source file does not exist */
301 ok(retval == 1026, "Files are copied to other directory ");
302 ok(!file_exists(".\\testdir2\\test1.txt"), "The file is copied");
304 ok(!file_exists(".\\testdir2\\test2.txt"), "The file is copied");
305 shfo.fFlags = tmp_flags;
308 /* tests the FO_MOVE action */
311 SHFILEOPSTRUCTA shfo, shfo2;
316 shfo.wFunc = FO_MOVE;
319 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
320 shfo.hNameMappings = NULL;
321 shfo.lpszProgressTitle = NULL;
323 set_curr_dir_path(from, "test1.txt\0");
324 set_curr_dir_path(to, "test4.txt\0");
325 ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are moved recursively");
326 ok(file_exists(".\\test4.txt\\test1.txt"), "The file is moved");
328 set_curr_dir_path(from, "test?.txt\0");
329 set_curr_dir_path(to, "testdir2\0");
330 ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not moved yet");
331 ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not moved yet");
332 ok(!SHFileOperationA(&shfo), "Files and directories are moved to directory ");
333 ok(file_exists(".\\testdir2\\test2.txt"), "The file is moved");
334 ok(file_exists(".\\testdir2\\test4.txt"), "The directory is moved");
335 ok(file_exists(".\\testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is moved");
337 clean_after_shfo_tests();
340 memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
341 shfo2.fFlags |= FOF_MULTIDESTFILES;
343 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
344 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
345 ok(!SHFileOperationA(&shfo2), "Move many files");
346 ok(file_exists(".\\test6.txt"), "The file is moved - many files are "
347 "specified as a target");
348 DeleteFileA(".\\test6.txt");
349 DeleteFileA(".\\test7.txt");
350 RemoveDirectoryA(".\\test8.txt");
354 /* number of sources do not correspond to number of targets */
355 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
356 set_curr_dir_path(to, "test6.txt\0test7.txt\0");
357 ok(SHFileOperationA(&shfo2), "Can't move many files");
358 ok(!file_exists(".\\test6.txt"), "The file is not moved - many files are "
359 "specified as a target");
363 set_curr_dir_path(from, "test3.txt\0");
364 set_curr_dir_path(to, "test4.txt\\test1.txt\0");
365 ok(!SHFileOperationA(&shfo), "File is moved moving to other directory");
366 ok(file_exists(".\\test4.txt\\test1.txt"), "The file is moved");
368 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
369 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
370 ok(SHFileOperationA(&shfo), "Can not move many files");
371 ok(file_exists(".\\test1.txt"), "The file is not moved. Many files are specified ");
372 ok(file_exists(".\\test4.txt"), "The directory not is moved. Many files are specified ");
374 set_curr_dir_path(from, "test1.txt\0");
375 set_curr_dir_path(to, "test6.txt\0");
376 ok(!SHFileOperationA(&shfo), "Move file");
377 ok(!file_exists(".\\test1.txt"), "The file is moved");
378 ok(file_exists(".\\test6.txt"), "The file is moved ");
379 set_curr_dir_path(from, "test6.txt\0");
380 set_curr_dir_path(to, "test1.txt\0");
381 ok(!SHFileOperationA(&shfo), "Move file back");
383 set_curr_dir_path(from, "test4.txt\0");
384 set_curr_dir_path(to, "test6.txt\0");
385 ok(!SHFileOperationA(&shfo), "Move dir");
386 ok(!file_exists(".\\test4.txt"), "The dir is moved");
387 ok(file_exists(".\\test6.txt"), "The dir is moved ");
388 set_curr_dir_path(from, "test6.txt\0");
389 set_curr_dir_path(to, "test4.txt\0");
390 ok(!SHFileOperationA(&shfo), "Move dir back");
393 START_TEST(shlfileop)
395 clean_after_shfo_tests();
399 clean_after_shfo_tests();
403 clean_after_shfo_tests();
407 clean_after_shfo_tests();
411 clean_after_shfo_tests();