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 */
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 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)
68 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
69 len = lstrlenA(CURR_DIR);
71 if(len && (CURR_DIR[len-1] == '\\'))
74 createTestFile("a.txt");
75 createTestFile("b.txt");
76 CreateDirectoryA("testdir", NULL);
77 createTestFile("testdir\\c.txt");
78 createTestFile("testdir\\d.txt");
79 CreateDirectoryA("dest", NULL);
82 static void delete_test_files(void)
86 DeleteFileA("testdir\\c.txt");
87 DeleteFileA("testdir\\d.txt");
88 RemoveDirectoryA("testdir");
89 RemoveDirectoryA("dest");
91 DeleteFileA("extract.cab");
94 static BOOL check_ini_file_attr(LPSTR filename)
97 DWORD expected = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY;
98 DWORD attr = GetFileAttributesA(filename);
100 ret = (attr & expected) && (attr != INVALID_FILE_ATTRIBUTES);
101 SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL);
108 static BOOL check_ini_contents(LPSTR filename, BOOL add)
110 CHAR field[FIELD_LEN];
111 BOOL ret = TRUE, match;
113 GetPrivateProfileStringA("backup", "one", NULL, field, FIELD_LEN, filename);
114 match = !lstrcmpA(field, "-1,0,0,0,0,0,-1");
115 if ((add && !match) || (!add && match))
118 GetPrivateProfileStringA("backup", "two", NULL, field, FIELD_LEN, filename);
119 if (lstrcmpA(field, "-1,0,0,0,0,0,-1"))
122 GetPrivateProfileStringA("backup", "three", NULL, field, FIELD_LEN, filename);
123 match = !lstrcmpA(field, "-1,0,0,0,0,0,-1");
124 if ((add && !match) || (!add && match))
130 static void test_AddDelBackupEntry(void)
135 lstrcpyA(path, CURR_DIR);
136 lstrcatA(path, "\\backup\\basename.INI");
138 /* native AddDelBackupEntry crashes if lpcszBaseName is NULL */
140 /* try a NULL file list */
141 res = pAddDelBackupEntry(NULL, "backup", "basename", AADBE_ADD_ENTRY);
142 ok(res == S_OK, "Expected S_OK, got %ld\n", res);
143 ok(!DeleteFileA(path), "Expected path to not exist\n");
145 lstrcpyA(path, CURR_DIR);
146 lstrcatA(path, "\\backup\\.INI");
148 /* try an empty base name */
149 res = pAddDelBackupEntry("one\0two\0three\0", "backup", "", AADBE_ADD_ENTRY);
150 ok(res == S_OK, "Expected S_OK, got %ld\n", res);
151 ok(!DeleteFileA(path), "Expected path to not exist\n");
153 lstrcpyA(path, CURR_DIR);
154 lstrcatA(path, "\\basename.INI");
156 /* try an invalid flag */
157 res = pAddDelBackupEntry("one\0two\0three\0", NULL, "basename", 0);
158 ok(res == S_OK, "Expected S_OK, got %ld\n", res);
159 ok(!DeleteFileA(path), "Expected path to not exist\n");
161 lstrcpyA(path, "c:\\basename.INI");
163 /* create the INF file */
164 res = pAddDelBackupEntry("one\0two\0three\0", "c:\\", "basename", AADBE_ADD_ENTRY);
165 ok(res == S_OK, "Expected S_OK, got %ld\n", res);
166 ok(check_ini_file_attr(path), "Expected ini file to be hidden\n");
167 ok(check_ini_contents(path, TRUE), "Expected ini contents to match\n");
168 ok(DeleteFileA(path), "Expected path to exist\n");
170 lstrcpyA(path, CURR_DIR);
171 lstrcatA(path, "\\backup\\basename.INI");
173 /* try to create the INI file in a nonexistent directory */
174 RemoveDirectoryA("backup");
175 res = pAddDelBackupEntry("one\0two\0three\0", "backup", "basename", AADBE_ADD_ENTRY);
176 ok(res == S_OK, "Expected S_OK, got %ld\n", res);
177 ok(!check_ini_file_attr(path), "Expected ini file to not be hidden\n");
178 ok(!check_ini_contents(path, TRUE), "Expected ini contents to not match\n");
179 ok(!DeleteFileA(path), "Expected path to not exist\n");
181 /* try an existent, relative backup directory */
182 CreateDirectoryA("backup", NULL);
183 res = pAddDelBackupEntry("one\0two\0three\0", "backup", "basename", AADBE_ADD_ENTRY);
184 ok(res == S_OK, "Expected S_OK, got %ld\n", res);
185 ok(check_ini_file_attr(path), "Expected ini file to be hidden\n");
186 ok(check_ini_contents(path, TRUE), "Expected ini contents to match\n");
187 ok(DeleteFileA(path), "Expected path to exist\n");
188 RemoveDirectoryA("backup");
190 lstrcpyA(path, "c:\\windows\\basename.INI");
192 /* try a NULL backup dir, INI is created in c:\windows */
193 res = pAddDelBackupEntry("one\0two\0three\0", NULL, "basename", AADBE_ADD_ENTRY);
194 ok(res == S_OK, "Expected S_OK, got %ld\n", res);
195 ok(check_ini_contents(path, TRUE), "Expected ini contents to match\n");
197 /* remove the entries with AADBE_DEL_ENTRY */
198 SetFileAttributesA(path, FILE_ATTRIBUTE_NORMAL);
199 res = pAddDelBackupEntry("one\0three\0", NULL, "basename", AADBE_DEL_ENTRY);
200 SetFileAttributesA(path, FILE_ATTRIBUTE_NORMAL);
201 ok(res == S_OK, "Expected S_OK, got %ld\n", res);
202 ok(check_ini_contents(path, FALSE), "Expected ini contents to match\n");
203 ok(DeleteFileA(path), "Expected path to exist\n");
206 /* the FCI callbacks */
208 static void *mem_alloc(ULONG cb)
210 return HeapAlloc(GetProcessHeap(), 0, cb);
213 static void mem_free(void *memory)
215 HeapFree(GetProcessHeap(), 0, memory);
218 static BOOL get_next_cabinet(PCCAB pccab, ULONG cbPrevCab, void *pv)
223 static long progress(UINT typeStatus, ULONG cb1, ULONG cb2, void *pv)
228 static int file_placed(PCCAB pccab, char *pszFile, long cbFile,
229 BOOL fContinuation, void *pv)
234 static INT_PTR fci_open(char *pszFile, int oflag, int pmode, int *err, void *pv)
238 DWORD dwShareMode = 0;
239 DWORD dwCreateDisposition = OPEN_EXISTING;
241 dwAccess = GENERIC_READ | GENERIC_WRITE;
242 /* FILE_SHARE_DELETE is not supported by Windows Me/98/95 */
243 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
245 if (GetFileAttributesA(pszFile) != INVALID_FILE_ATTRIBUTES)
246 dwCreateDisposition = OPEN_EXISTING;
248 dwCreateDisposition = CREATE_NEW;
250 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
251 dwCreateDisposition, 0, NULL);
253 ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszFile);
255 return (INT_PTR)handle;
258 static UINT fci_read(INT_PTR hf, void *memory, UINT cb, int *err, void *pv)
260 HANDLE handle = (HANDLE)hf;
264 res = ReadFile(handle, memory, cb, &dwRead, NULL);
265 ok(res, "Failed to ReadFile\n");
270 static UINT fci_write(INT_PTR hf, void *memory, UINT cb, int *err, void *pv)
272 HANDLE handle = (HANDLE)hf;
276 res = WriteFile(handle, memory, cb, &dwWritten, NULL);
277 ok(res, "Failed to WriteFile\n");
282 static int fci_close(INT_PTR hf, int *err, void *pv)
284 HANDLE handle = (HANDLE)hf;
285 ok(CloseHandle(handle), "Failed to CloseHandle\n");
290 static long fci_seek(INT_PTR hf, long dist, int seektype, int *err, void *pv)
292 HANDLE handle = (HANDLE)hf;
295 ret = SetFilePointer(handle, dist, NULL, seektype);
296 ok(ret != INVALID_SET_FILE_POINTER, "Failed to SetFilePointer\n");
301 static int fci_delete(char *pszFile, int *err, void *pv)
303 BOOL ret = DeleteFileA(pszFile);
304 ok(ret, "Failed to DeleteFile %s\n", pszFile);
309 static BOOL get_temp_file(char *pszTempName, int cbTempName, void *pv)
313 tempname = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
314 GetTempFileNameA(".", "xx", 0, tempname);
316 if (tempname && (strlen(tempname) < (unsigned)cbTempName))
318 lstrcpyA(pszTempName, tempname);
319 HeapFree(GetProcessHeap(), 0, tempname);
323 HeapFree(GetProcessHeap(), 0, tempname);
328 static INT_PTR get_open_info(char *pszName, USHORT *pdate, USHORT *ptime,
329 USHORT *pattribs, int *err, void *pv)
331 BY_HANDLE_FILE_INFORMATION finfo;
337 handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL,
338 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
340 ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszName);
342 res = GetFileInformationByHandle(handle, &finfo);
343 ok(res, "Expected GetFileInformationByHandle to succeed\n");
345 FileTimeToLocalFileTime(&finfo.ftLastWriteTime, &filetime);
346 FileTimeToDosDateTime(&filetime, pdate, ptime);
348 attrs = GetFileAttributes(pszName);
349 ok(attrs != INVALID_FILE_ATTRIBUTES, "Failed to GetFileAttributes\n");
351 return (INT_PTR)handle;
354 static void add_file(HFCI hfci, char *file)
359 lstrcpyA(path, CURR_DIR);
360 lstrcatA(path, "\\");
361 lstrcatA(path, file);
363 res = FCIAddFile(hfci, path, file, FALSE, get_next_cabinet, progress,
364 get_open_info, tcompTYPE_MSZIP);
365 ok(res, "Expected FCIAddFile to succeed\n");
368 static void set_cab_parameters(PCCAB pCabParams)
370 ZeroMemory(pCabParams, sizeof(CCAB));
372 pCabParams->cb = MEDIA_SIZE;
373 pCabParams->cbFolderThresh = FOLDER_THRESHOLD;
374 pCabParams->setID = 0xbeef;
375 lstrcpyA(pCabParams->szCabPath, CURR_DIR);
376 lstrcatA(pCabParams->szCabPath, "\\");
377 lstrcpyA(pCabParams->szCab, "extract.cab");
380 static void create_cab_file(void)
385 static CHAR a_txt[] = "a.txt",
387 testdir_c_txt[] = "testdir\\c.txt",
388 testdir_d_txt[] = "testdir\\d.txt";
391 set_cab_parameters(&cabParams);
393 hfci = FCICreate(&erf, file_placed, mem_alloc, mem_free, fci_open,
394 fci_read, fci_write, fci_close, fci_seek, fci_delete,
395 get_temp_file, &cabParams, NULL);
397 ok(hfci != NULL, "Failed to create an FCI context\n");
399 add_file(hfci, a_txt);
400 add_file(hfci, b_txt);
401 add_file(hfci, testdir_c_txt);
402 add_file(hfci, testdir_d_txt);
404 res = FCIFlushCabinet(hfci, FALSE, get_next_cabinet, progress);
405 ok(res, "Failed to flush the cabinet\n");
407 res = FCIDestroy(hfci);
408 ok(res, "Failed to destroy the cabinet\n");
411 static void test_ExtractFiles(void)
414 char destFolder[MAX_PATH];
416 lstrcpyA(destFolder, CURR_DIR);
417 lstrcatA(destFolder, "\\");
418 lstrcatA(destFolder, "dest");
420 /* try NULL cab file */
421 hr = pExtractFiles(NULL, destFolder, 0, NULL, NULL, 0);
422 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %ld\n", hr);
423 ok(RemoveDirectoryA("dest"), "Expected dest to exist\n");
425 /* try NULL destination */
426 hr = pExtractFiles("extract.cab", NULL, 0, NULL, NULL, 0);
427 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %ld\n", hr);
428 ok(!RemoveDirectoryA("dest"), "Expected dest to not exist\n");
430 /* extract all files in the cab to nonexistent destination directory */
431 hr = pExtractFiles("extract.cab", destFolder, 0, NULL, NULL, 0);
432 ok(hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND),
433 "Expected %ld, got %ld\n", HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), hr);
434 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
435 ok(!DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to not exist\n");
436 ok(!RemoveDirectoryA("dest\\testdir"), "Exepected dest\\testdir to not exist\n");
437 ok(!RemoveDirectoryA("dest"), "Expected dest to not exist\n");
439 /* extract all files in the cab to the destination directory */
440 CreateDirectoryA("dest", NULL);
441 hr = pExtractFiles("extract.cab", destFolder, 0, NULL, NULL, 0);
442 ok(hr == S_OK, "Expected S_OK, got %ld\n", hr);
443 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
444 ok(DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to exist\n");
445 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
446 ok(DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to exist\n");
447 ok(RemoveDirectoryA("dest\\testdir"), "Exepected dest\\testdir to exist\n");
449 /* extract all files to a relative destination directory */
450 hr = pExtractFiles("extract.cab", "dest", 0, NULL, NULL, 0);
451 ok(hr == S_OK, "Expected S_OK, got %ld\n", hr);
452 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
453 ok(DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to exist\n");
454 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
455 ok(DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to exist\n");
456 ok(RemoveDirectoryA("dest\\testdir"), "Exepected dest\\testdir to exist\n");
458 /* only extract two of the files from the cab */
459 hr = pExtractFiles("extract.cab", "dest", 0, "a.txt:testdir\\c.txt", NULL, 0);
460 ok(hr == S_OK, "Expected S_OK, got %ld\n", hr);
461 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
462 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
463 ok(RemoveDirectoryA("dest\\testdir"), "Exepected dest\\testdir to exist\n");
464 ok(!DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to not exist\n");
465 ok(!DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to not exist\n");
467 /* use valid chars before and after file list */
468 hr = pExtractFiles("extract.cab", "dest", 0, " :\t: a.txt:testdir\\c.txt \t:", NULL, 0);
469 ok(hr == S_OK, "Expected S_OK, got %ld\n", hr);
470 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
471 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
472 ok(RemoveDirectoryA("dest\\testdir"), "Exepected dest\\testdir to exist\n");
473 ok(!DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to not exist\n");
474 ok(!DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to not exist\n");
476 /* use invalid chars before and after file list */
477 hr = pExtractFiles("extract.cab", "dest", 0, " +-\\ a.txt:testdir\\c.txt a_:", NULL, 0);
478 ok(hr == E_FAIL, "Expected E_FAIL, got %ld\n", hr);
479 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
480 ok(!DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to not exist\n");
481 ok(!RemoveDirectoryA("dest\\testdir"), "Exepected dest\\testdir to not exist\n");
483 /* try an empty file list */
484 hr = pExtractFiles("extract.cab", "dest", 0, "", NULL, 0);
485 ok(hr == E_FAIL, "Expected E_FAIL, got %ld\n", hr);
486 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
487 ok(!RemoveDirectoryA("dest\\testdir"), "Exepected dest\\testdir to not exist\n");
489 /* try a nonexistent file in the file list */
490 hr = pExtractFiles("extract.cab", "dest", 0, "a.txt:idontexist:testdir\\c.txt", NULL, 0);
491 ok(hr == E_FAIL, "Expected E_FAIL, got %ld\n", hr);
492 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
493 ok(!DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to not exist\n");
494 ok(!RemoveDirectoryA("dest\\testdir"), "Exepected dest\\testdir to not exist\n");
497 static void test_AdvInstallFile(void)
500 char CURR_DIR[MAX_PATH];
501 char destFolder[MAX_PATH];
503 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
505 lstrcpyA(destFolder, CURR_DIR);
506 lstrcatA(destFolder, "\\");
507 lstrcatA(destFolder, "dest");
509 createTestFile("source.txt");
511 /* try invalid source directory */
512 hr = pAdvInstallFile(NULL, NULL, "source.txt", destFolder, "destination.txt", 0, 0);
513 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %ld\n", hr);
514 ok(!DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to not exist\n");
516 /* try invalid source file */
517 hr = pAdvInstallFile(NULL, CURR_DIR, NULL, destFolder, "destination.txt", 0, 0);
518 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %ld\n", hr);
519 ok(!DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to not exist\n");
521 /* try invalid destination directory */
522 hr = pAdvInstallFile(NULL, CURR_DIR, "source.txt", NULL, "destination.txt", 0, 0);
523 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %ld\n", hr);
524 ok(!DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to not exist\n");
526 /* try copying to nonexistent destination directory */
527 hr = pAdvInstallFile(NULL, CURR_DIR, "source.txt", destFolder, "destination.txt", 0, 0);
528 ok(hr == S_OK, "Expected S_OK, got %ld\n", hr);
529 ok(DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to exist\n");
531 /* native windows screws up if the source file doesn't exist */
533 /* test AIF_NOOVERWRITE behavior, asks the user to overwrite if AIF_QUIET is not specified */
534 createTestFile("dest\\destination.txt");
535 hr = pAdvInstallFile(NULL, CURR_DIR, "source.txt", destFolder,
536 "destination.txt", AIF_NOOVERWRITE | AIF_QUIET, 0);
537 ok(hr == S_OK, "Expected S_OK, got %ld\n", hr);
538 ok(DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to exist\n");
539 ok(RemoveDirectoryA("dest"), "Expected dest to exist\n");
541 DeleteFileA("source.txt");
546 init_function_pointers();
550 test_AddDelBackupEntry();
552 test_AdvInstallFile();