shlfileop.c 16.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Unit test of the SHFileOperation function.
 *
 * Copyright 2002 Andriy Palamarchuk
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

21
#include <stdarg.h>
22
#include <stdio.h>
23

24
#define WINE_NOWINSOCK
25
#include "windef.h"
26 27 28
#include "winbase.h"
#include "wtypes.h"
#include "shellapi.h"
29
#include "shlobj.h"
30 31

#include "wine/test.h"
32 33 34

CHAR CURR_DIR[MAX_PATH];

35 36 37 38 39 40 41 42 43 44 45
static HMODULE hshell32;
static int (WINAPI *pSHCreateDirectoryExA)(HWND, LPCSTR, LPSECURITY_ATTRIBUTES);

static void InitFunctionPointers(void)
{
    hshell32 = GetModuleHandleA("shell32.dll");

    if(hshell32)
	pSHCreateDirectoryExA = (void*)GetProcAddress(hshell32, "SHCreateDirectoryExA");
}

46 47 48 49 50 51
/* creates a file with the specified name for tests */
void createTestFile(CHAR *name)
{
    HANDLE file;
    DWORD written;

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 55 56 57 58 59 60
    WriteFile(file, name, strlen(name), &written, NULL);
    WriteFile(file, "\n", strlen("\n"), &written, NULL);
    CloseHandle(file);
}

BOOL file_exists(CHAR *name)
{
61
    return GetFileAttributesA(name) != INVALID_FILE_ATTRIBUTES;
62 63 64 65 66 67 68 69 70 71
}

/* initializes the tests */
void init_shfo_tests(void)
{
    GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
    createTestFile(".\\test1.txt");
    createTestFile(".\\test2.txt");
    createTestFile(".\\test3.txt");
    CreateDirectoryA(".\\test4.txt", NULL);
72
    CreateDirectoryA(".\\testdir2", NULL);
73 74 75 76 77 78 79 80 81 82 83 84
}

/* cleans after tests */
void clean_after_shfo_tests(void)
{
    DeleteFileA(".\\test1.txt");
    DeleteFileA(".\\test2.txt");
    DeleteFileA(".\\test3.txt");
    DeleteFileA(".\\test4.txt\\test1.txt");
    DeleteFileA(".\\test4.txt\\test2.txt");
    DeleteFileA(".\\test4.txt\\test3.txt");
    RemoveDirectoryA(".\\test4.txt");
85 86 87 88 89 90
    DeleteFileA(".\\testdir2\\test1.txt");
    DeleteFileA(".\\testdir2\\test2.txt");
    DeleteFileA(".\\testdir2\\test3.txt");
    DeleteFileA(".\\testdir2\\test4.txt\\test1.txt");
    RemoveDirectoryA(".\\testdir2\\test4.txt");
    RemoveDirectoryA(".\\testdir2");
91 92
}

93
/*
94 95
 puts into the specified buffer file names with current directory.
 files - string with file names, separated by null characters. Ends on a double
96 97
 null characters
*/
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
void set_curr_dir_path(CHAR *buf, CHAR* files)
{
    buf[0] = 0;
    while (files[0])
    {
        strcpy(buf, CURR_DIR);
        buf += strlen(buf);
        buf[0] = '\\';
        buf++;
        strcpy(buf, files);
        buf += strlen(buf) + 1;
        files += strlen(files) + 1;
    }
    buf[0] = 0;
}


/* tests the FO_DELETE action */
void test_delete(void)
{
    SHFILEOPSTRUCTA shfo;
119
    DWORD ret;
120 121 122 123 124
    CHAR buf[MAX_PATH];

    sprintf(buf, "%s\\%s", CURR_DIR, "test?.txt");
    buf[strlen(buf) + 1] = '\0';

125
    shfo.hwnd = NULL;
126 127 128 129 130 131 132
    shfo.wFunc = FO_DELETE;
    shfo.pFrom = buf;
    shfo.pTo = "\0";
    shfo.fFlags = FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_SILENT;
    shfo.hNameMappings = NULL;
    shfo.lpszProgressTitle = NULL;

133 134 135
    ok(!SHFileOperationA(&shfo), "Deletion was successful\n");
    ok(file_exists(".\\test4.txt"), "Directory should not be removed\n");
    ok(!file_exists(".\\test1.txt"), "File should be removed\n");
136

137 138
    ret = SHFileOperationA(&shfo);
    ok(!ret, "Directory exists, but is not removed, ret=%ld\n", ret);
139
    ok(file_exists(".\\test4.txt"), "Directory should not be removed\n");
140

141
    shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
142

143 144
    ok(!SHFileOperationA(&shfo), "Directory removed\n");
    ok(!file_exists(".\\test4.txt"), "Directory should be removed\n");
145

146 147
    ret = SHFileOperationA(&shfo);
    ok(!ret, "The requested file does not exist, ret=%ld\n", ret);
148 149 150 151

    init_shfo_tests();
    sprintf(buf, "%s\\%s", CURR_DIR, "test4.txt");
    buf[strlen(buf) + 1] = '\0';
152 153 154
    ok(MoveFileA(".\\test1.txt", ".\\test4.txt\\test1.txt"), "Fill the subdirectory\n");
    ok(!SHFileOperationA(&shfo), "Directory removed\n");
    ok(!file_exists(".\\test4.txt"), "Directory is removed\n");
155 156 157

    init_shfo_tests();
    shfo.pFrom = ".\\test1.txt\0.\\test4.txt\0";
158 159 160 161
    ok(!SHFileOperationA(&shfo), "Directory and a file removed\n");
    ok(!file_exists(".\\test1.txt"), "The file should be removed\n");
    ok(!file_exists(".\\test4.txt"), "Directory should be removed\n");
    ok(file_exists(".\\test2.txt"), "This file should not be removed\n");
162 163 164 165 166
}

/* tests the FO_RENAME action */
void test_rename()
{
167
    SHFILEOPSTRUCTA shfo, shfo2;
168 169
    CHAR from[MAX_PATH];
    CHAR to[MAX_PATH];
170
    DWORD retval;
171

172
    shfo.hwnd = NULL;
173 174 175 176 177 178 179 180 181 182
    shfo.wFunc = FO_RENAME;
    shfo.pFrom = from;
    shfo.pTo = to;
    shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
    shfo.hNameMappings = NULL;
    shfo.lpszProgressTitle = NULL;

    set_curr_dir_path(from, "test1.txt\0");
    set_curr_dir_path(to, "test4.txt\0");
    ok(SHFileOperationA(&shfo), "File is not renamed moving to other directory "
183 184
       "when specifying directory name only\n");
    ok(file_exists(".\\test1.txt"), "The file is not removed\n");
185 186 187

    set_curr_dir_path(from, "test3.txt\0");
    set_curr_dir_path(to, "test4.txt\\test1.txt\0");
188 189
    ok(!SHFileOperationA(&shfo), "File is renamed moving to other directory\n");
    ok(file_exists(".\\test4.txt\\test1.txt"), "The file is renamed\n");
190 191 192

    set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
    set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
193
    retval = SHFileOperationA(&shfo); /* W98 returns 0, W2K and newer returns ERROR_GEN_FAILURE, both do nothing */
194 195
    ok(!retval || retval == ERROR_GEN_FAILURE || retval == ERROR_REDIR_PAUSED,
       "Can't rename many files, retval = %lx\n", retval);
196
    ok(file_exists(".\\test1.txt"), "The file is not renamed - many files are specified\n");
197

198 199 200 201 202
    memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
    shfo2.fFlags |= FOF_MULTIDESTFILES;

    set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
    set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
203
    retval = SHFileOperationA(&shfo2); /* W98 returns 0, W2K and newer returns ERROR_GEN_FAILURE, both do nothing */
204 205
    ok(!retval || retval == ERROR_GEN_FAILURE || retval == ERROR_REDIR_PAUSED,
       "Can't rename many files, retval = %lx\n", retval);
206
    ok(file_exists(".\\test1.txt"), "The file is not renamed - many files are specified\n");
207

208 209
    set_curr_dir_path(from, "test1.txt\0");
    set_curr_dir_path(to, "test6.txt\0");
210 211 212
    ok(!SHFileOperationA(&shfo), "Rename file\n");
    ok(!file_exists(".\\test1.txt"), "The file is renamed\n");
    ok(file_exists(".\\test6.txt"), "The file is renamed\n");
213 214
    set_curr_dir_path(from, "test6.txt\0");
    set_curr_dir_path(to, "test1.txt\0");
215
    ok(!SHFileOperationA(&shfo), "Rename file back\n");
216 217 218

    set_curr_dir_path(from, "test4.txt\0");
    set_curr_dir_path(to, "test6.txt\0");
219 220 221
    ok(!SHFileOperationA(&shfo), "Rename dir\n");
    ok(!file_exists(".\\test4.txt"), "The dir is renamed\n");
    ok(file_exists(".\\test6.txt"), "The dir is renamed\n");
222 223
    set_curr_dir_path(from, "test6.txt\0");
    set_curr_dir_path(to, "test4.txt\0");
224
    ok(!SHFileOperationA(&shfo), "Rename dir back\n");
225 226
}

227 228 229 230 231 232 233
/* tests the FO_COPY action */
void test_copy(void)
{
    SHFILEOPSTRUCTA shfo, shfo2;
    CHAR from[MAX_PATH];
    CHAR to[MAX_PATH];
    FILEOP_FLAGS tmp_flags;
234
    DWORD retval;
235

236
    shfo.hwnd = NULL;
237 238 239 240 241 242 243 244 245
    shfo.wFunc = FO_COPY;
    shfo.pFrom = from;
    shfo.pTo = to;
    shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
    shfo.hNameMappings = NULL;
    shfo.lpszProgressTitle = NULL;

    set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
    set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
246
    ok(SHFileOperationA(&shfo), "Can't copy many files\n");
247
    ok(!file_exists(".\\test6.txt"), "The file is not copied - many files are "
248
       "specified as a target\n");
249 250 251 252 253 254

    memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
    shfo2.fFlags |= FOF_MULTIDESTFILES;

    set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
    set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
255
    ok(!SHFileOperationA(&shfo2), "Can't copy many files\n");
256
    ok(file_exists(".\\test6.txt"), "The file is copied - many files are "
257
       "specified as a target\n");
258 259 260 261 262 263 264
    DeleteFileA(".\\test6.txt");
    DeleteFileA(".\\test7.txt");
    RemoveDirectoryA(".\\test8.txt");

    /* number of sources do not correspond to number of targets */
    set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
    set_curr_dir_path(to, "test6.txt\0test7.txt\0");
265
    ok(SHFileOperationA(&shfo2), "Can't copy many files\n");
266
    ok(!file_exists(".\\test6.txt"), "The file is not copied - many files are "
267
       "specified as a target\n");
268 269 270

    set_curr_dir_path(from, "test1.txt\0");
    set_curr_dir_path(to, "test4.txt\0");
271 272
    ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are copied recursively\n");
    ok(file_exists(".\\test4.txt\\test1.txt"), "The file is copied\n");
273 274 275

    set_curr_dir_path(from, "test?.txt\0");
    set_curr_dir_path(to, "testdir2\0");
276 277 278 279 280 281
    ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet\n");
    ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not copied yet\n");
    ok(!SHFileOperationA(&shfo), "Files and directories are copied to directory\n");
    ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied\n");
    ok(file_exists(".\\testdir2\\test4.txt"), "The directory is copied\n");
    ok(file_exists(".\\testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is copied\n");
282 283 284 285
    clean_after_shfo_tests();

    init_shfo_tests();
    shfo.fFlags |= FOF_FILESONLY;
286 287 288 289 290
    ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet\n");
    ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not copied yet\n");
    ok(!SHFileOperationA(&shfo), "Files are copied to other directory\n");
    ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied\n");
    ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is copied\n");
291 292 293 294
    clean_after_shfo_tests();

    init_shfo_tests();
    set_curr_dir_path(from, "test1.txt\0test2.txt\0");
295 296 297 298 299
    ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet\n");
    ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not copied yet\n");
    ok(!SHFileOperationA(&shfo), "Files are copied to other directory \n");
    ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied\n");
    ok(file_exists(".\\testdir2\\test2.txt"), "The file is copied\n");
300 301
    clean_after_shfo_tests();

302 303
    /* Copying multiple files with one not existing as source, fails the
       entire operation in Win98/ME/2K/XP, but not in 95/NT */
304 305 306
    init_shfo_tests();
    tmp_flags = shfo.fFlags;
    set_curr_dir_path(from, "test1.txt\0test10.txt\0test2.txt\0");
307 308
    ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet\n");
    ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not copied yet\n");
309 310
    retval = SHFileOperationA(&shfo);
    if (!retval)
311
      /* Win 95/NT returns success but copies only the files up to the nonexistent source */
312
      ok(file_exists(".\\testdir2\\test1.txt"), "The file is not copied\n");
313 314 315
    else
    {
      /* Win 98/ME/2K/XP fail the entire operation with return code 1026 if one source file does not exist */
316 317
      ok(retval == 1026, "Files are copied to other directory\n");
      ok(!file_exists(".\\testdir2\\test1.txt"), "The file is copied\n");
318
    }
319
    ok(!file_exists(".\\testdir2\\test2.txt"), "The file is copied\n");
320 321 322 323 324 325 326 327 328 329
    shfo.fFlags = tmp_flags;
}

/* tests the FO_MOVE action */
void test_move(void)
{
    SHFILEOPSTRUCTA shfo, shfo2;
    CHAR from[MAX_PATH];
    CHAR to[MAX_PATH];

330
    shfo.hwnd = NULL;
331 332 333 334 335 336 337 338 339
    shfo.wFunc = FO_MOVE;
    shfo.pFrom = from;
    shfo.pTo = to;
    shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
    shfo.hNameMappings = NULL;
    shfo.lpszProgressTitle = NULL;

    set_curr_dir_path(from, "test1.txt\0");
    set_curr_dir_path(to, "test4.txt\0");
340 341
    ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are moved recursively\n");
    ok(file_exists(".\\test4.txt\\test1.txt"), "The file is moved\n");
342 343 344

    set_curr_dir_path(from, "test?.txt\0");
    set_curr_dir_path(to, "testdir2\0");
345 346 347 348 349 350
    ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not moved yet\n");
    ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not moved yet\n");
    ok(!SHFileOperationA(&shfo), "Files and directories are moved to directory\n");
    ok(file_exists(".\\testdir2\\test2.txt"), "The file is moved\n");
    ok(file_exists(".\\testdir2\\test4.txt"), "The directory is moved\n");
    ok(file_exists(".\\testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is moved\n");
351 352 353 354 355 356 357 358 359

    clean_after_shfo_tests();
    init_shfo_tests();

    memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
    shfo2.fFlags |= FOF_MULTIDESTFILES;

    set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
    set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
360
    ok(!SHFileOperationA(&shfo2), "Move many files\n");
361
    ok(file_exists(".\\test6.txt"), "The file is moved - many files are "
362
       "specified as a target\n");
363 364 365 366 367 368 369 370 371
    DeleteFileA(".\\test6.txt");
    DeleteFileA(".\\test7.txt");
    RemoveDirectoryA(".\\test8.txt");

    init_shfo_tests();

    /* number of sources do not correspond to number of targets */
    set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
    set_curr_dir_path(to, "test6.txt\0test7.txt\0");
372
    ok(SHFileOperationA(&shfo2), "Can't move many files\n");
373
    ok(!file_exists(".\\test6.txt"), "The file is not moved - many files are "
374
       "specified as a target\n");
375 376 377 378 379

    init_shfo_tests();

    set_curr_dir_path(from, "test3.txt\0");
    set_curr_dir_path(to, "test4.txt\\test1.txt\0");
380 381
    ok(!SHFileOperationA(&shfo), "File is moved moving to other directory\n");
    ok(file_exists(".\\test4.txt\\test1.txt"), "The file is moved\n");
382 383 384

    set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
    set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
385 386
    ok(SHFileOperationA(&shfo), "Can not move many files\n");
    ok(file_exists(".\\test1.txt"), "The file is not moved. Many files are specified\n");
387
    ok(file_exists(".\\test4.txt"), "The directory is not moved. Many files are specified\n");
388 389 390

    set_curr_dir_path(from, "test1.txt\0");
    set_curr_dir_path(to, "test6.txt\0");
391 392 393
    ok(!SHFileOperationA(&shfo), "Move file\n");
    ok(!file_exists(".\\test1.txt"), "The file is moved\n");
    ok(file_exists(".\\test6.txt"), "The file is moved\n");
394 395
    set_curr_dir_path(from, "test6.txt\0");
    set_curr_dir_path(to, "test1.txt\0");
396
    ok(!SHFileOperationA(&shfo), "Move file back\n");
397 398 399

    set_curr_dir_path(from, "test4.txt\0");
    set_curr_dir_path(to, "test6.txt\0");
400 401 402
    ok(!SHFileOperationA(&shfo), "Move dir\n");
    ok(!file_exists(".\\test4.txt"), "The dir is moved\n");
    ok(file_exists(".\\test6.txt"), "The dir is moved\n");
403 404
    set_curr_dir_path(from, "test6.txt\0");
    set_curr_dir_path(to, "test4.txt\0");
405
    ok(!SHFileOperationA(&shfo), "Move dir back\n");
406 407
}

408 409 410 411 412
void test_sh_create_dir()
{
    CHAR path[MAX_PATH];
    int ret;

413 414 415 416 417 418
    if(!pSHCreateDirectoryExA)
    {
	trace("skipping SHCreateDirectoryExA tests\n");
	return;
    }

419
    set_curr_dir_path(path, "testdir2\\test4.txt\0");
420
    ret = pSHCreateDirectoryExA(NULL, path, NULL);
421 422 423 424
    ok(ERROR_SUCCESS == ret, "SHCreateDirectoryEx failed to create directory recursively, ret = %d\n", ret);
    ok(file_exists(".\\testdir2"), "The first directory is not created\n");
    ok(file_exists(".\\testdir2\\test4.txt"), "The second directory is not created\n");

425
    ret = pSHCreateDirectoryExA(NULL, path, NULL);
426 427 428
    ok(ERROR_ALREADY_EXISTS == ret, "SHCreateDirectoryEx should fail to create existing directory, ret = %d\n", ret);
}

429 430
START_TEST(shlfileop)
{
431 432
    InitFunctionPointers();

433 434 435 436 437 438 439 440 441
    clean_after_shfo_tests();

    init_shfo_tests();
    test_delete();
    clean_after_shfo_tests();

    init_shfo_tests();
    test_rename();
    clean_after_shfo_tests();
442 443 444 445 446 447 448 449

    init_shfo_tests();
    test_copy();
    clean_after_shfo_tests();

    init_shfo_tests();
    test_move();
    clean_after_shfo_tests();
450 451 452

    test_sh_create_dir();
    clean_after_shfo_tests();
453
}