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
28 #include "wine/test.h"
30 CHAR CURR_DIR[MAX_PATH];
32 /* creates a file with the specified name for tests */
33 void createTestFile(CHAR *name)
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);
47 BOOL file_exists(CHAR *name)
49 return GetFileAttributesA(name) != 0xFFFFFFFF;
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");
121 ok(file_exists(".\\test4.txt"), "Directory should not be removed");
122 ok(!file_exists(".\\test1.txt"), "File should be removed");
124 ok(!SHFileOperationA(&shfo), "Directory exists, but is not removed");
125 ok(file_exists(".\\test4.txt"), "Directory should not be removed");
127 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
129 ok(!SHFileOperationA(&shfo), "Directory removed");
130 ok(!file_exists(".\\test4.txt"), "Directory should be removed");
132 ok(!SHFileOperationA(&shfo), "The requested file does not exist");
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");
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");
149 /* tests the FO_RENAME action */
152 SHFILEOPSTRUCTA shfo, shfo2;
157 shfo.wFunc = FO_RENAME;
160 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
161 shfo.hNameMappings = NULL;
162 shfo.lpszProgressTitle = NULL;
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");
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");
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 ");
180 memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
181 shfo2.fFlags |= FOF_MULTIDESTFILES;
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 ");
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");
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");
207 /* tests the FO_COPY action */
210 SHFILEOPSTRUCTA shfo, shfo2;
213 FILEOP_FLAGS tmp_flags;
216 shfo.wFunc = FO_COPY;
219 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
220 shfo.hNameMappings = NULL;
221 shfo.lpszProgressTitle = NULL;
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");
229 memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
230 shfo2.fFlags |= FOF_MULTIDESTFILES;
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");
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");
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");
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();
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();
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();
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;
292 /* tests the FO_MOVE action */
295 SHFILEOPSTRUCTA shfo, shfo2;
300 shfo.wFunc = FO_MOVE;
303 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
304 shfo.hNameMappings = NULL;
305 shfo.lpszProgressTitle = NULL;
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");
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");
321 clean_after_shfo_tests();
324 memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
325 shfo2.fFlags |= FOF_MULTIDESTFILES;
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");
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");
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");
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 ");
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");
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");
377 START_TEST(shlfileop)
379 clean_after_shfo_tests();
383 clean_after_shfo_tests();
387 clean_after_shfo_tests();
391 clean_after_shfo_tests();
395 clean_after_shfo_tests();