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()
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 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
244 if (GetFileAttributesA(pszFile
) != INVALID_FILE_ATTRIBUTES
)
245 dwCreateDisposition
= OPEN_EXISTING
;
247 dwCreateDisposition
= CREATE_NEW
;
249 handle
= CreateFileA(pszFile
, dwAccess
, dwShareMode
, NULL
,
250 dwCreateDisposition
, 0, NULL
);
252 ok(handle
!= INVALID_HANDLE_VALUE
, "Failed to CreateFile %s\n", pszFile
);
254 return (INT_PTR
)handle
;
257 static UINT
fci_read(INT_PTR hf
, void *memory
, UINT cb
, int *err
, void *pv
)
259 HANDLE handle
= (HANDLE
)hf
;
263 res
= ReadFile(handle
, memory
, cb
, &dwRead
, NULL
);
264 ok(res
, "Failed to ReadFile\n");
269 static UINT
fci_write(INT_PTR hf
, void *memory
, UINT cb
, int *err
, void *pv
)
271 HANDLE handle
= (HANDLE
)hf
;
275 res
= WriteFile(handle
, memory
, cb
, &dwWritten
, NULL
);
276 ok(res
, "Failed to WriteFile\n");
281 static int fci_close(INT_PTR hf
, int *err
, void *pv
)
283 HANDLE handle
= (HANDLE
)hf
;
284 ok(CloseHandle(handle
), "Failed to CloseHandle\n");
289 static long fci_seek(INT_PTR hf
, long dist
, int seektype
, int *err
, void *pv
)
291 HANDLE handle
= (HANDLE
)hf
;
294 ret
= SetFilePointer(handle
, dist
, NULL
, seektype
);
295 ok(ret
!= INVALID_SET_FILE_POINTER
, "Failed to SetFilePointer\n");
300 static int fci_delete(char *pszFile
, int *err
, void *pv
)
302 BOOL ret
= DeleteFileA(pszFile
);
303 ok(ret
, "Failed to DeleteFile %s\n", pszFile
);
308 static BOOL
get_temp_file(char *pszTempName
, int cbTempName
, void *pv
)
312 tempname
= HeapAlloc(GetProcessHeap(), 0, MAX_PATH
);
313 GetTempFileNameA(".", "xx", 0, tempname
);
315 if (tempname
&& (strlen(tempname
) < (unsigned)cbTempName
))
317 lstrcpyA(pszTempName
, tempname
);
318 HeapFree(GetProcessHeap(), 0, tempname
);
322 HeapFree(GetProcessHeap(), 0, tempname
);
327 static INT_PTR
get_open_info(char *pszName
, USHORT
*pdate
, USHORT
*ptime
,
328 USHORT
*pattribs
, int *err
, void *pv
)
330 BY_HANDLE_FILE_INFORMATION finfo
;
336 handle
= CreateFile(pszName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
337 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_SEQUENTIAL_SCAN
, NULL
);
339 ok(handle
!= INVALID_HANDLE_VALUE
, "Failed to CreateFile %s\n", pszName
);
341 res
= GetFileInformationByHandle(handle
, &finfo
);
342 ok(res
, "Expected GetFileInformationByHandle to succeed\n");
344 FileTimeToLocalFileTime(&finfo
.ftLastWriteTime
, &filetime
);
345 FileTimeToDosDateTime(&filetime
, pdate
, ptime
);
347 attrs
= GetFileAttributes(pszName
);
348 ok(attrs
!= INVALID_FILE_ATTRIBUTES
, "Failed to GetFileAttributes\n");
350 return (INT_PTR
)handle
;
353 static void add_file(HFCI hfci
, char *file
)
358 lstrcpyA(path
, CURR_DIR
);
359 lstrcatA(path
, "\\");
360 lstrcatA(path
, file
);
362 res
= FCIAddFile(hfci
, path
, file
, FALSE
, get_next_cabinet
, progress
,
363 get_open_info
, tcompTYPE_MSZIP
);
364 ok(res
, "Expected FCIAddFile to succeed\n");
367 static void set_cab_parameters(PCCAB pCabParams
)
369 ZeroMemory(pCabParams
, sizeof(CCAB
));
371 pCabParams
->cb
= MEDIA_SIZE
;
372 pCabParams
->cbFolderThresh
= FOLDER_THRESHOLD
;
373 pCabParams
->setID
= 0xbeef;
374 lstrcpyA(pCabParams
->szCabPath
, CURR_DIR
);
375 lstrcatA(pCabParams
->szCabPath
, "\\");
376 lstrcpyA(pCabParams
->szCab
, "extract.cab");
379 static void create_cab_file(void)
386 set_cab_parameters(&cabParams
);
388 hfci
= FCICreate(&erf
, file_placed
, mem_alloc
, mem_free
, fci_open
,
389 fci_read
, fci_write
, fci_close
, fci_seek
, fci_delete
,
390 get_temp_file
, &cabParams
, NULL
);
392 ok(hfci
!= NULL
, "Failed to create an FCI context\n");
394 add_file(hfci
, "a.txt");
395 add_file(hfci
, "b.txt");
396 add_file(hfci
, "testdir\\c.txt");
397 add_file(hfci
, "testdir\\d.txt");
399 res
= FCIFlushCabinet(hfci
, FALSE
, get_next_cabinet
, progress
);
400 ok(res
, "Failed to flush the cabinet\n");
402 res
= FCIDestroy(hfci
);
403 ok(res
, "Failed to destroy the cabinet\n");
406 static void test_ExtractFiles(void)
409 char destFolder
[MAX_PATH
];
411 lstrcpyA(destFolder
, CURR_DIR
);
412 lstrcatA(destFolder
, "\\");
413 lstrcatA(destFolder
, "dest");
415 /* try NULL cab file */
416 hr
= pExtractFiles(NULL
, destFolder
, 0, NULL
, NULL
, 0);
417 ok(hr
== E_INVALIDARG
, "Expected E_INVALIDARG, got %ld\n", hr
);
418 ok(RemoveDirectoryA("dest"), "Expected dest to exist\n");
420 /* try NULL destination */
421 hr
= pExtractFiles("extract.cab", NULL
, 0, NULL
, NULL
, 0);
422 ok(hr
== E_INVALIDARG
, "Expected E_INVALIDARG, got %ld\n", hr
);
423 ok(!RemoveDirectoryA("dest"), "Expected dest to not exist\n");
425 /* extract all files in the cab to nonexistent destination directory */
426 hr
= pExtractFiles("extract.cab", destFolder
, 0, NULL
, NULL
, 0);
427 ok(hr
== HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
),
428 "Expected %ld, got %ld\n", HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
), hr
);
429 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
430 ok(!DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to not exist\n");
431 ok(!RemoveDirectoryA("dest\\testdir"), "Exepected dest\\testdir to not exist\n");
432 ok(!RemoveDirectoryA("dest"), "Expected dest to not exist\n");
434 /* extract all files in the cab to the destination directory */
435 CreateDirectoryA("dest", NULL
);
436 hr
= pExtractFiles("extract.cab", destFolder
, 0, NULL
, NULL
, 0);
437 ok(hr
== S_OK
, "Expected S_OK, got %ld\n", hr
);
438 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
439 ok(DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to exist\n");
440 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
441 ok(DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to exist\n");
442 ok(RemoveDirectoryA("dest\\testdir"), "Exepected dest\\testdir to exist\n");
444 /* extract all files to a relative destination directory */
445 hr
= pExtractFiles("extract.cab", "dest", 0, NULL
, NULL
, 0);
446 ok(hr
== S_OK
, "Expected S_OK, got %ld\n", hr
);
447 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
448 ok(DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to exist\n");
449 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
450 ok(DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to exist\n");
451 ok(RemoveDirectoryA("dest\\testdir"), "Exepected dest\\testdir to exist\n");
453 /* only extract two of the files from the cab */
454 hr
= pExtractFiles("extract.cab", "dest", 0, "a.txt:testdir\\c.txt", NULL
, 0);
455 ok(hr
== S_OK
, "Expected S_OK, got %ld\n", hr
);
456 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
457 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
458 ok(RemoveDirectoryA("dest\\testdir"), "Exepected dest\\testdir to exist\n");
459 ok(!DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to not exist\n");
460 ok(!DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to not exist\n");
462 /* use valid chars before and after file list */
463 hr
= pExtractFiles("extract.cab", "dest", 0, " :\t: a.txt:testdir\\c.txt \t:", NULL
, 0);
464 ok(hr
== S_OK
, "Expected S_OK, got %ld\n", hr
);
465 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
466 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
467 ok(RemoveDirectoryA("dest\\testdir"), "Exepected dest\\testdir to exist\n");
468 ok(!DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to not exist\n");
469 ok(!DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to not exist\n");
471 /* use invalid chars before and after file list */
472 hr
= pExtractFiles("extract.cab", "dest", 0, " +-\\ a.txt:testdir\\c.txt a_:", NULL
, 0);
473 ok(hr
== E_FAIL
, "Expected E_FAIL, got %ld\n", hr
);
474 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
475 ok(!DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to not exist\n");
476 ok(!RemoveDirectoryA("dest\\testdir"), "Exepected dest\\testdir to not exist\n");
478 /* try an empty file list */
479 hr
= pExtractFiles("extract.cab", "dest", 0, "", NULL
, 0);
480 ok(hr
== E_FAIL
, "Expected E_FAIL, got %ld\n", hr
);
481 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
482 ok(!RemoveDirectoryA("dest\\testdir"), "Exepected dest\\testdir to not exist\n");
484 /* try a nonexistent file in the file list */
485 hr
= pExtractFiles("extract.cab", "dest", 0, "a.txt:idontexist:testdir\\c.txt", NULL
, 0);
486 ok(hr
== E_FAIL
, "Expected E_FAIL, got %ld\n", hr
);
487 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
488 ok(!DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to not exist\n");
489 ok(!RemoveDirectoryA("dest\\testdir"), "Exepected dest\\testdir to not exist\n");
492 static void test_AdvInstallFile(void)
495 char CURR_DIR
[MAX_PATH
];
496 char destFolder
[MAX_PATH
];
498 GetCurrentDirectoryA(MAX_PATH
, CURR_DIR
);
500 lstrcpyA(destFolder
, CURR_DIR
);
501 lstrcatA(destFolder
, "\\");
502 lstrcatA(destFolder
, "dest");
504 createTestFile("source.txt");
506 /* try invalid source directory */
507 hr
= pAdvInstallFile(NULL
, NULL
, "source.txt", destFolder
, "destination.txt", 0, 0);
508 ok(hr
== E_INVALIDARG
, "Expected E_INVALIDARG, got %ld\n", hr
);
509 ok(!DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to not exist\n");
511 /* try invalid source file */
512 hr
= pAdvInstallFile(NULL
, CURR_DIR
, NULL
, 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 destination directory */
517 hr
= pAdvInstallFile(NULL
, CURR_DIR
, "source.txt", NULL
, "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 copying to nonexistent destination directory */
522 hr
= pAdvInstallFile(NULL
, CURR_DIR
, "source.txt", destFolder
, "destination.txt", 0, 0);
523 ok(hr
== S_OK
, "Expected S_OK, got %ld\n", hr
);
524 ok(DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to exist\n");
526 /* native windows screws up if the source file doesn't exist */
528 /* test AIF_NOOVERWRITE behavior, asks the user to overwrite if AIF_QUIET is not specified */
529 createTestFile("dest\\destination.txt");
530 hr
= pAdvInstallFile(NULL
, CURR_DIR
, "source.txt", destFolder
,
531 "destination.txt", AIF_NOOVERWRITE
| AIF_QUIET
, 0);
532 ok(hr
== S_OK
, "Expected S_OK, got %ld\n", hr
);
533 ok(DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to exist\n");
534 ok(RemoveDirectoryA("dest"), "Expected dest to exist\n");
536 DeleteFileA("source.txt");
541 init_function_pointers();
545 test_AddDelBackupEntry();
547 test_AdvInstallFile();