2 * Unit tests for advpack.dll file functions
4 * Copyright (C) 2006 James Hawkins
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/test.h"
27 /* make the max size large so there is only one cab file */
28 #define MEDIA_SIZE 999999999
29 #define FOLDER_THRESHOLD 900000
31 /* function pointers */
32 static HMODULE hAdvPack;
33 static HRESULT (WINAPI *pAddDelBackupEntry)(LPCSTR, LPCSTR, LPCSTR, DWORD);
34 static HRESULT (WINAPI *pExtractFiles)(LPCSTR, LPCSTR, DWORD, LPCSTR, LPVOID, DWORD);
35 static HRESULT (WINAPI *pAdvInstallFile)(HWND,LPCSTR,LPCSTR,LPCSTR,LPCSTR,DWORD,DWORD);
37 static CHAR CURR_DIR[MAX_PATH];
39 static void init_function_pointers(void)
41 hAdvPack = LoadLibraryA("advpack.dll");
45 pAddDelBackupEntry = (void *)GetProcAddress(hAdvPack, "AddDelBackupEntry");
46 pExtractFiles = (void *)GetProcAddress(hAdvPack, "ExtractFiles");
47 pAdvInstallFile = (void*)GetProcAddress(hAdvPack, "AdvInstallFile");
51 /* creates a file with the specified name for tests */
52 static void createTestFile(const CHAR *name)
57 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
58 ok(file != INVALID_HANDLE_VALUE, "Failure to open file %s\n", name);
59 WriteFile(file, name, strlen(name), &written, NULL);
60 WriteFile(file, "\n", strlen("\n"), &written, NULL);
64 static void create_test_files(void)
66 createTestFile("a.txt");
67 createTestFile("b.txt");
68 CreateDirectoryA("testdir", NULL);
69 createTestFile("testdir\\c.txt");
70 createTestFile("testdir\\d.txt");
71 CreateDirectoryA("dest", NULL);
74 static void delete_test_files(void)
78 DeleteFileA("testdir\\c.txt");
79 DeleteFileA("testdir\\d.txt");
80 RemoveDirectoryA("testdir");
81 RemoveDirectoryA("dest");
83 DeleteFileA("extract.cab");
86 static BOOL check_ini_file_attr(LPSTR filename)
89 DWORD expected = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY;
90 DWORD attr = GetFileAttributesA(filename);
92 ret = (attr & expected) && (attr != INVALID_FILE_ATTRIBUTES);
93 SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL);
98 static void test_AddDelBackupEntry(void)
103 CHAR windir[MAX_PATH];
105 lstrcpyA(path, CURR_DIR);
106 lstrcatA(path, "\\backup\\basename.INI");
108 /* native AddDelBackupEntry crashes if lpcszBaseName is NULL */
110 /* try a NULL file list */
111 res = pAddDelBackupEntry(NULL, "backup", "basename", AADBE_ADD_ENTRY);
112 ok(res == S_OK, "Expected S_OK, got %d\n", res);
113 ok(!DeleteFileA(path), "Expected path to not exist\n");
115 lstrcpyA(path, CURR_DIR);
116 lstrcatA(path, "\\backup\\.INI");
118 /* try an empty base name */
119 res = pAddDelBackupEntry("one\0two\0three\0", "backup", "", AADBE_ADD_ENTRY);
120 ok(res == S_OK, "Expected S_OK, got %d\n", res);
121 ok(!DeleteFileA(path), "Expected path to not exist\n");
123 lstrcpyA(path, CURR_DIR);
124 lstrcatA(path, "\\basename.INI");
126 /* try an invalid flag */
127 res = pAddDelBackupEntry("one\0two\0three\0", NULL, "basename", 0);
128 ok(res == S_OK, "Expected S_OK, got %d\n", res);
129 ok(!DeleteFileA(path), "Expected path to not exist\n");
131 lstrcpyA(path, "c:\\basename.INI");
133 /* create the INF file */
134 res = pAddDelBackupEntry("one\0two\0three\0", "c:\\", "basename", AADBE_ADD_ENTRY);
135 ok(res == S_OK, "Expected S_OK, got %d\n", res);
136 if (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES)
138 ok(check_ini_file_attr(path), "Expected ini file to be hidden\n");
139 ok(DeleteFileA(path), "Expected path to exist\n");
142 win_skip("Test file could not be created\n");
144 lstrcpyA(path, CURR_DIR);
145 lstrcatA(path, "\\backup\\basename.INI");
147 /* try to create the INI file in a nonexistent directory */
148 RemoveDirectoryA("backup");
149 res = pAddDelBackupEntry("one\0two\0three\0", "backup", "basename", AADBE_ADD_ENTRY);
150 ok(res == S_OK, "Expected S_OK, got %d\n", res);
151 ok(!check_ini_file_attr(path), "Expected ini file to not be hidden\n");
152 ok(!DeleteFileA(path), "Expected path to not exist\n");
154 /* try an existent, relative backup directory */
155 CreateDirectoryA("backup", NULL);
156 res = pAddDelBackupEntry("one\0two\0three\0", "backup", "basename", AADBE_ADD_ENTRY);
157 ok(res == S_OK, "Expected S_OK, got %d\n", res);
158 ok(check_ini_file_attr(path), "Expected ini file to be hidden\n");
159 ok(DeleteFileA(path), "Expected path to exist\n");
160 RemoveDirectoryA("backup");
162 GetWindowsDirectoryA(windir, sizeof(windir));
163 sprintf(path, "%s\\basename.INI", windir);
165 /* try a NULL backup dir, INI is created in the windows directory */
166 res = pAddDelBackupEntry("one\0two\0three\0", NULL, "basename", AADBE_ADD_ENTRY);
167 ok(res == S_OK, "Expected S_OK, got %d\n", res);
169 /* remove the entries with AADBE_DEL_ENTRY */
170 SetFileAttributesA(path, FILE_ATTRIBUTE_NORMAL);
171 res = pAddDelBackupEntry("one\0three\0", NULL, "basename", AADBE_DEL_ENTRY);
172 SetFileAttributesA(path, FILE_ATTRIBUTE_NORMAL);
173 ok(res == S_OK, "Expected S_OK, got %d\n", res);
174 ret = DeleteFileA(path);
176 broken(ret == FALSE), /* win98 */
177 "Expected path to exist\n");
180 /* the FCI callbacks */
182 static void * CDECL mem_alloc(ULONG cb)
184 return HeapAlloc(GetProcessHeap(), 0, cb);
187 static void CDECL mem_free(void *memory)
189 HeapFree(GetProcessHeap(), 0, memory);
192 static BOOL CDECL get_next_cabinet(PCCAB pccab, ULONG cbPrevCab, void *pv)
197 static LONG CDECL progress(UINT typeStatus, ULONG cb1, ULONG cb2, void *pv)
202 static int CDECL file_placed(PCCAB pccab, char *pszFile, LONG cbFile,
203 BOOL fContinuation, void *pv)
208 static INT_PTR CDECL fci_open(char *pszFile, int oflag, int pmode, int *err, void *pv)
212 DWORD dwShareMode = 0;
213 DWORD dwCreateDisposition = OPEN_EXISTING;
215 dwAccess = GENERIC_READ | GENERIC_WRITE;
216 /* FILE_SHARE_DELETE is not supported by Windows Me/98/95 */
217 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
219 if (GetFileAttributesA(pszFile) != INVALID_FILE_ATTRIBUTES)
220 dwCreateDisposition = OPEN_EXISTING;
222 dwCreateDisposition = CREATE_NEW;
224 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
225 dwCreateDisposition, 0, NULL);
227 ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszFile);
229 return (INT_PTR)handle;
232 static UINT CDECL fci_read(INT_PTR hf, void *memory, UINT cb, int *err, void *pv)
234 HANDLE handle = (HANDLE)hf;
238 res = ReadFile(handle, memory, cb, &dwRead, NULL);
239 ok(res, "Failed to ReadFile\n");
244 static UINT CDECL fci_write(INT_PTR hf, void *memory, UINT cb, int *err, void *pv)
246 HANDLE handle = (HANDLE)hf;
250 res = WriteFile(handle, memory, cb, &dwWritten, NULL);
251 ok(res, "Failed to WriteFile\n");
256 static int CDECL fci_close(INT_PTR hf, int *err, void *pv)
258 HANDLE handle = (HANDLE)hf;
259 ok(CloseHandle(handle), "Failed to CloseHandle\n");
264 static LONG CDECL fci_seek(INT_PTR hf, LONG dist, int seektype, int *err, void *pv)
266 HANDLE handle = (HANDLE)hf;
269 ret = SetFilePointer(handle, dist, NULL, seektype);
270 ok(ret != INVALID_SET_FILE_POINTER, "Failed to SetFilePointer\n");
275 static int CDECL fci_delete(char *pszFile, int *err, void *pv)
277 BOOL ret = DeleteFileA(pszFile);
278 ok(ret, "Failed to DeleteFile %s\n", pszFile);
283 static BOOL CDECL get_temp_file(char *pszTempName, int cbTempName, void *pv)
287 tempname = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
288 GetTempFileNameA(".", "xx", 0, tempname);
290 if (tempname && (strlen(tempname) < (unsigned)cbTempName))
292 lstrcpyA(pszTempName, tempname);
293 HeapFree(GetProcessHeap(), 0, tempname);
297 HeapFree(GetProcessHeap(), 0, tempname);
302 static INT_PTR CDECL get_open_info(char *pszName, USHORT *pdate, USHORT *ptime,
303 USHORT *pattribs, int *err, void *pv)
305 BY_HANDLE_FILE_INFORMATION finfo;
311 handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL,
312 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
314 ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszName);
316 res = GetFileInformationByHandle(handle, &finfo);
317 ok(res, "Expected GetFileInformationByHandle to succeed\n");
319 FileTimeToLocalFileTime(&finfo.ftLastWriteTime, &filetime);
320 FileTimeToDosDateTime(&filetime, pdate, ptime);
322 attrs = GetFileAttributes(pszName);
323 ok(attrs != INVALID_FILE_ATTRIBUTES, "Failed to GetFileAttributes\n");
325 return (INT_PTR)handle;
328 static void add_file(HFCI hfci, char *file)
333 lstrcpyA(path, CURR_DIR);
334 lstrcatA(path, "\\");
335 lstrcatA(path, file);
337 res = FCIAddFile(hfci, path, file, FALSE, get_next_cabinet, progress,
338 get_open_info, tcompTYPE_MSZIP);
339 ok(res, "Expected FCIAddFile to succeed\n");
342 static void set_cab_parameters(PCCAB pCabParams)
344 ZeroMemory(pCabParams, sizeof(CCAB));
346 pCabParams->cb = MEDIA_SIZE;
347 pCabParams->cbFolderThresh = FOLDER_THRESHOLD;
348 pCabParams->setID = 0xbeef;
349 lstrcpyA(pCabParams->szCabPath, CURR_DIR);
350 lstrcatA(pCabParams->szCabPath, "\\");
351 lstrcpyA(pCabParams->szCab, "extract.cab");
354 static void create_cab_file(void)
359 static CHAR a_txt[] = "a.txt",
361 testdir_c_txt[] = "testdir\\c.txt",
362 testdir_d_txt[] = "testdir\\d.txt";
365 set_cab_parameters(&cabParams);
367 hfci = FCICreate(&erf, file_placed, mem_alloc, mem_free, fci_open,
368 fci_read, fci_write, fci_close, fci_seek, fci_delete,
369 get_temp_file, &cabParams, NULL);
371 ok(hfci != NULL, "Failed to create an FCI context\n");
373 add_file(hfci, a_txt);
374 add_file(hfci, b_txt);
375 add_file(hfci, testdir_c_txt);
376 add_file(hfci, testdir_d_txt);
378 res = FCIFlushCabinet(hfci, FALSE, get_next_cabinet, progress);
379 ok(res, "Failed to flush the cabinet\n");
381 res = FCIDestroy(hfci);
382 ok(res, "Failed to destroy the cabinet\n");
385 static void test_ExtractFiles(void)
388 char destFolder[MAX_PATH];
390 lstrcpyA(destFolder, CURR_DIR);
391 lstrcatA(destFolder, "\\");
392 lstrcatA(destFolder, "dest");
394 /* try NULL cab file */
395 hr = pExtractFiles(NULL, destFolder, 0, NULL, NULL, 0);
396 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
397 ok(RemoveDirectoryA("dest"), "Expected dest to exist\n");
399 /* try NULL destination */
400 hr = pExtractFiles("extract.cab", NULL, 0, NULL, NULL, 0);
401 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
402 ok(!RemoveDirectoryA("dest"), "Expected dest to not exist\n");
404 /* extract all files in the cab to nonexistent destination directory */
405 hr = pExtractFiles("extract.cab", destFolder, 0, NULL, NULL, 0);
406 ok(hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) ||
407 hr == E_FAIL, /* win95 */
408 "Expected %08x or %08x, got %08x\n", E_FAIL,
409 HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), hr);
410 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
411 ok(!DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to not exist\n");
412 ok(!RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to not exist\n");
413 ok(!RemoveDirectoryA("dest"), "Expected dest to not exist\n");
415 /* extract all files in the cab to the destination directory */
416 CreateDirectoryA("dest", NULL);
417 hr = pExtractFiles("extract.cab", destFolder, 0, NULL, NULL, 0);
418 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
419 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
420 ok(DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to exist\n");
421 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
422 ok(DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to exist\n");
423 ok(RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to exist\n");
425 /* extract all files to a relative destination directory */
426 hr = pExtractFiles("extract.cab", "dest", 0, NULL, NULL, 0);
427 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
428 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
429 ok(DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to exist\n");
430 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
431 ok(DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to exist\n");
432 ok(RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to exist\n");
434 /* only extract two of the files from the cab */
435 hr = pExtractFiles("extract.cab", "dest", 0, "a.txt:testdir\\c.txt", NULL, 0);
436 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
437 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
438 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
439 ok(RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to exist\n");
440 ok(!DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to not exist\n");
441 ok(!DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to not exist\n");
443 /* use valid chars before and after file list */
444 hr = pExtractFiles("extract.cab", "dest", 0, " :\t: a.txt:testdir\\c.txt \t:", NULL, 0);
445 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
446 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
447 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
448 ok(RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to exist\n");
449 ok(!DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to not exist\n");
450 ok(!DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to not exist\n");
452 /* use invalid chars before and after file list */
453 hr = pExtractFiles("extract.cab", "dest", 0, " +-\\ a.txt:testdir\\c.txt a_:", NULL, 0);
454 ok(hr == E_FAIL, "Expected E_FAIL, got %d\n", hr);
455 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
456 ok(!DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to not exist\n");
457 ok(!RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to not exist\n");
459 /* try an empty file list */
460 hr = pExtractFiles("extract.cab", "dest", 0, "", NULL, 0);
461 ok(hr == E_FAIL, "Expected E_FAIL, got %d\n", hr);
462 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
463 ok(!RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to not exist\n");
465 /* try a nonexistent file in the file list */
466 hr = pExtractFiles("extract.cab", "dest", 0, "a.txt:idontexist:testdir\\c.txt", NULL, 0);
467 ok(hr == E_FAIL, "Expected E_FAIL, got %d\n", hr);
468 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
469 ok(!DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to not exist\n");
470 ok(!RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to not exist\n");
473 static void test_AdvInstallFile(void)
477 char CURR_DIR[MAX_PATH];
478 char destFolder[MAX_PATH];
480 hmod = LoadLibrary("setupapi.dll");
483 skip("setupapi.dll not present\n");
489 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
491 lstrcpyA(destFolder, CURR_DIR);
492 lstrcatA(destFolder, "\\");
493 lstrcatA(destFolder, "dest");
495 createTestFile("source.txt");
497 /* try invalid source directory */
498 hr = pAdvInstallFile(NULL, NULL, "source.txt", destFolder, "destination.txt", 0, 0);
499 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
500 ok(!DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to not exist\n");
502 /* try invalid source file */
503 hr = pAdvInstallFile(NULL, CURR_DIR, NULL, destFolder, "destination.txt", 0, 0);
504 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
505 ok(!DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to not exist\n");
507 /* try invalid destination directory */
508 hr = pAdvInstallFile(NULL, CURR_DIR, "source.txt", NULL, "destination.txt", 0, 0);
509 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
510 ok(!DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to not exist\n");
512 /* try copying to nonexistent destination directory */
513 hr = pAdvInstallFile(NULL, CURR_DIR, "source.txt", destFolder, "destination.txt", 0, 0);
514 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
515 ok(DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to exist\n");
517 /* native windows screws up if the source file doesn't exist */
519 /* test AIF_NOOVERWRITE behavior, asks the user to overwrite if AIF_QUIET is not specified */
520 createTestFile("dest\\destination.txt");
521 hr = pAdvInstallFile(NULL, CURR_DIR, "source.txt", destFolder,
522 "destination.txt", AIF_NOOVERWRITE | AIF_QUIET, 0);
523 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
524 ok(DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to exist\n");
525 ok(RemoveDirectoryA("dest"), "Expected dest to exist\n");
527 DeleteFileA("source.txt");
533 char temp_path[MAX_PATH], prev_path[MAX_PATH];
535 init_function_pointers();
537 GetCurrentDirectoryA(MAX_PATH, prev_path);
538 GetTempPath(MAX_PATH, temp_path);
539 SetCurrentDirectoryA(temp_path);
541 lstrcpyA(CURR_DIR, temp_path);
542 len = lstrlenA(CURR_DIR);
544 if(len && (CURR_DIR[len - 1] == '\\'))
545 CURR_DIR[len - 1] = 0;
550 test_AddDelBackupEntry();
552 test_AdvInstallFile();
556 FreeLibrary(hAdvPack);
557 SetCurrentDirectoryA(prev_path);