Release 0.9.61.
[wine/gsoc-2012-control.git] / dlls / advpack / tests / files.c
blob530c3b2aebc0ab68c55c9533a94768ecb1ec05ab
1 /*
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
21 #include <stdio.h>
22 #include <windows.h>
23 #include <advpub.h>
24 #include <fci.h>
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 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 CHAR CURR_DIR[MAX_PATH];
39 static void init_function_pointers(void)
41 hAdvPack = LoadLibraryA("advpack.dll");
43 if (hAdvPack)
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)
54 HANDLE file;
55 DWORD written;
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);
61 CloseHandle(file);
64 static void create_test_files(void)
66 int len;
68 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
69 len = lstrlenA(CURR_DIR);
71 if(len && (CURR_DIR[len-1] == '\\'))
72 CURR_DIR[len-1] = 0;
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)
84 DeleteFileA("a.txt");
85 DeleteFileA("b.txt");
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)
96 BOOL ret;
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);
103 return ret;
106 static void test_AddDelBackupEntry(void)
108 HRESULT res;
109 CHAR path[MAX_PATH];
110 CHAR windir[MAX_PATH];
112 lstrcpyA(path, CURR_DIR);
113 lstrcatA(path, "\\backup\\basename.INI");
115 /* native AddDelBackupEntry crashes if lpcszBaseName is NULL */
117 /* try a NULL file list */
118 res = pAddDelBackupEntry(NULL, "backup", "basename", AADBE_ADD_ENTRY);
119 ok(res == S_OK, "Expected S_OK, got %d\n", res);
120 ok(!DeleteFileA(path), "Expected path to not exist\n");
122 lstrcpyA(path, CURR_DIR);
123 lstrcatA(path, "\\backup\\.INI");
125 /* try an empty base name */
126 res = pAddDelBackupEntry("one\0two\0three\0", "backup", "", AADBE_ADD_ENTRY);
127 ok(res == S_OK, "Expected S_OK, got %d\n", res);
128 ok(!DeleteFileA(path), "Expected path to not exist\n");
130 lstrcpyA(path, CURR_DIR);
131 lstrcatA(path, "\\basename.INI");
133 /* try an invalid flag */
134 res = pAddDelBackupEntry("one\0two\0three\0", NULL, "basename", 0);
135 ok(res == S_OK, "Expected S_OK, got %d\n", res);
136 ok(!DeleteFileA(path), "Expected path to not exist\n");
138 lstrcpyA(path, "c:\\basename.INI");
140 /* create the INF file */
141 res = pAddDelBackupEntry("one\0two\0three\0", "c:\\", "basename", AADBE_ADD_ENTRY);
142 ok(res == S_OK, "Expected S_OK, got %d\n", res);
143 ok(check_ini_file_attr(path), "Expected ini file to be hidden\n");
144 ok(DeleteFileA(path), "Expected path to exist\n");
146 lstrcpyA(path, CURR_DIR);
147 lstrcatA(path, "\\backup\\basename.INI");
149 /* try to create the INI file in a nonexistent directory */
150 RemoveDirectoryA("backup");
151 res = pAddDelBackupEntry("one\0two\0three\0", "backup", "basename", AADBE_ADD_ENTRY);
152 ok(res == S_OK, "Expected S_OK, got %d\n", res);
153 ok(!check_ini_file_attr(path), "Expected ini file to not be hidden\n");
154 ok(!DeleteFileA(path), "Expected path to not exist\n");
156 /* try an existent, relative backup directory */
157 CreateDirectoryA("backup", NULL);
158 res = pAddDelBackupEntry("one\0two\0three\0", "backup", "basename", AADBE_ADD_ENTRY);
159 ok(res == S_OK, "Expected S_OK, got %d\n", res);
160 ok(check_ini_file_attr(path), "Expected ini file to be hidden\n");
161 ok(DeleteFileA(path), "Expected path to exist\n");
162 RemoveDirectoryA("backup");
164 GetWindowsDirectoryA(windir, sizeof(windir));
165 sprintf(path, "%s\\basename.INI", windir);
167 /* try a NULL backup dir, INI is created in the windows directory */
168 res = pAddDelBackupEntry("one\0two\0three\0", NULL, "basename", AADBE_ADD_ENTRY);
169 ok(res == S_OK, "Expected S_OK, got %d\n", res);
171 /* remove the entries with AADBE_DEL_ENTRY */
172 SetFileAttributesA(path, FILE_ATTRIBUTE_NORMAL);
173 res = pAddDelBackupEntry("one\0three\0", NULL, "basename", AADBE_DEL_ENTRY);
174 SetFileAttributesA(path, FILE_ATTRIBUTE_NORMAL);
175 ok(res == S_OK, "Expected S_OK, got %d\n", res);
176 ok(DeleteFileA(path), "Expected path to exist\n");
179 /* the FCI callbacks */
181 static void *mem_alloc(ULONG cb)
183 return HeapAlloc(GetProcessHeap(), 0, cb);
186 static void mem_free(void *memory)
188 HeapFree(GetProcessHeap(), 0, memory);
191 static BOOL get_next_cabinet(PCCAB pccab, ULONG cbPrevCab, void *pv)
193 return TRUE;
196 static long progress(UINT typeStatus, ULONG cb1, ULONG cb2, void *pv)
198 return 0;
201 static int file_placed(PCCAB pccab, char *pszFile, long cbFile,
202 BOOL fContinuation, void *pv)
204 return 0;
207 static INT_PTR fci_open(char *pszFile, int oflag, int pmode, int *err, void *pv)
209 HANDLE handle;
210 DWORD dwAccess = 0;
211 DWORD dwShareMode = 0;
212 DWORD dwCreateDisposition = OPEN_EXISTING;
214 dwAccess = GENERIC_READ | GENERIC_WRITE;
215 /* FILE_SHARE_DELETE is not supported by Windows Me/98/95 */
216 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
218 if (GetFileAttributesA(pszFile) != INVALID_FILE_ATTRIBUTES)
219 dwCreateDisposition = OPEN_EXISTING;
220 else
221 dwCreateDisposition = CREATE_NEW;
223 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
224 dwCreateDisposition, 0, NULL);
226 ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszFile);
228 return (INT_PTR)handle;
231 static UINT fci_read(INT_PTR hf, void *memory, UINT cb, int *err, void *pv)
233 HANDLE handle = (HANDLE)hf;
234 DWORD dwRead;
235 BOOL res;
237 res = ReadFile(handle, memory, cb, &dwRead, NULL);
238 ok(res, "Failed to ReadFile\n");
240 return dwRead;
243 static UINT fci_write(INT_PTR hf, void *memory, UINT cb, int *err, void *pv)
245 HANDLE handle = (HANDLE)hf;
246 DWORD dwWritten;
247 BOOL res;
249 res = WriteFile(handle, memory, cb, &dwWritten, NULL);
250 ok(res, "Failed to WriteFile\n");
252 return dwWritten;
255 static int fci_close(INT_PTR hf, int *err, void *pv)
257 HANDLE handle = (HANDLE)hf;
258 ok(CloseHandle(handle), "Failed to CloseHandle\n");
260 return 0;
263 static long fci_seek(INT_PTR hf, long dist, int seektype, int *err, void *pv)
265 HANDLE handle = (HANDLE)hf;
266 DWORD ret;
268 ret = SetFilePointer(handle, dist, NULL, seektype);
269 ok(ret != INVALID_SET_FILE_POINTER, "Failed to SetFilePointer\n");
271 return ret;
274 static int fci_delete(char *pszFile, int *err, void *pv)
276 BOOL ret = DeleteFileA(pszFile);
277 ok(ret, "Failed to DeleteFile %s\n", pszFile);
279 return 0;
282 static BOOL get_temp_file(char *pszTempName, int cbTempName, void *pv)
284 LPSTR tempname;
286 tempname = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
287 GetTempFileNameA(".", "xx", 0, tempname);
289 if (tempname && (strlen(tempname) < (unsigned)cbTempName))
291 lstrcpyA(pszTempName, tempname);
292 HeapFree(GetProcessHeap(), 0, tempname);
293 return TRUE;
296 HeapFree(GetProcessHeap(), 0, tempname);
298 return FALSE;
301 static INT_PTR get_open_info(char *pszName, USHORT *pdate, USHORT *ptime,
302 USHORT *pattribs, int *err, void *pv)
304 BY_HANDLE_FILE_INFORMATION finfo;
305 FILETIME filetime;
306 HANDLE handle;
307 DWORD attrs;
308 BOOL res;
310 handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL,
311 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
313 ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszName);
315 res = GetFileInformationByHandle(handle, &finfo);
316 ok(res, "Expected GetFileInformationByHandle to succeed\n");
318 FileTimeToLocalFileTime(&finfo.ftLastWriteTime, &filetime);
319 FileTimeToDosDateTime(&filetime, pdate, ptime);
321 attrs = GetFileAttributes(pszName);
322 ok(attrs != INVALID_FILE_ATTRIBUTES, "Failed to GetFileAttributes\n");
324 return (INT_PTR)handle;
327 static void add_file(HFCI hfci, char *file)
329 char path[MAX_PATH];
330 BOOL res;
332 lstrcpyA(path, CURR_DIR);
333 lstrcatA(path, "\\");
334 lstrcatA(path, file);
336 res = FCIAddFile(hfci, path, file, FALSE, get_next_cabinet, progress,
337 get_open_info, tcompTYPE_MSZIP);
338 ok(res, "Expected FCIAddFile to succeed\n");
341 static void set_cab_parameters(PCCAB pCabParams)
343 ZeroMemory(pCabParams, sizeof(CCAB));
345 pCabParams->cb = MEDIA_SIZE;
346 pCabParams->cbFolderThresh = FOLDER_THRESHOLD;
347 pCabParams->setID = 0xbeef;
348 lstrcpyA(pCabParams->szCabPath, CURR_DIR);
349 lstrcatA(pCabParams->szCabPath, "\\");
350 lstrcpyA(pCabParams->szCab, "extract.cab");
353 static void create_cab_file(void)
355 CCAB cabParams;
356 HFCI hfci;
357 ERF erf;
358 static CHAR a_txt[] = "a.txt",
359 b_txt[] = "b.txt",
360 testdir_c_txt[] = "testdir\\c.txt",
361 testdir_d_txt[] = "testdir\\d.txt";
362 BOOL res;
364 set_cab_parameters(&cabParams);
366 hfci = FCICreate(&erf, file_placed, mem_alloc, mem_free, fci_open,
367 fci_read, fci_write, fci_close, fci_seek, fci_delete,
368 get_temp_file, &cabParams, NULL);
370 ok(hfci != NULL, "Failed to create an FCI context\n");
372 add_file(hfci, a_txt);
373 add_file(hfci, b_txt);
374 add_file(hfci, testdir_c_txt);
375 add_file(hfci, testdir_d_txt);
377 res = FCIFlushCabinet(hfci, FALSE, get_next_cabinet, progress);
378 ok(res, "Failed to flush the cabinet\n");
380 res = FCIDestroy(hfci);
381 ok(res, "Failed to destroy the cabinet\n");
384 static void test_ExtractFiles(void)
386 HRESULT hr;
387 char destFolder[MAX_PATH];
389 lstrcpyA(destFolder, CURR_DIR);
390 lstrcatA(destFolder, "\\");
391 lstrcatA(destFolder, "dest");
393 /* try NULL cab file */
394 hr = pExtractFiles(NULL, destFolder, 0, NULL, NULL, 0);
395 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
396 ok(RemoveDirectoryA("dest"), "Expected dest to exist\n");
398 /* try NULL destination */
399 hr = pExtractFiles("extract.cab", NULL, 0, NULL, NULL, 0);
400 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
401 ok(!RemoveDirectoryA("dest"), "Expected dest to not exist\n");
403 /* extract all files in the cab to nonexistent destination directory */
404 hr = pExtractFiles("extract.cab", destFolder, 0, NULL, NULL, 0);
405 ok(hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) ||
406 hr == E_FAIL, /* win95 */
407 "Expected %08x or %08x, got %08x\n", E_FAIL,
408 HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), hr);
409 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
410 ok(!DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to not exist\n");
411 ok(!RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to not exist\n");
412 ok(!RemoveDirectoryA("dest"), "Expected dest to not exist\n");
414 /* extract all files in the cab to the destination directory */
415 CreateDirectoryA("dest", NULL);
416 hr = pExtractFiles("extract.cab", destFolder, 0, NULL, NULL, 0);
417 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
418 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
419 ok(DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to exist\n");
420 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
421 ok(DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to exist\n");
422 ok(RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to exist\n");
424 /* extract all files to a relative destination directory */
425 hr = pExtractFiles("extract.cab", "dest", 0, NULL, NULL, 0);
426 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
427 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
428 ok(DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to exist\n");
429 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
430 ok(DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to exist\n");
431 ok(RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to exist\n");
433 /* only extract two of the files from the cab */
434 hr = pExtractFiles("extract.cab", "dest", 0, "a.txt:testdir\\c.txt", NULL, 0);
435 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
436 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
437 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
438 ok(RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to exist\n");
439 ok(!DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to not exist\n");
440 ok(!DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to not exist\n");
442 /* use valid chars before and after file list */
443 hr = pExtractFiles("extract.cab", "dest", 0, " :\t: a.txt:testdir\\c.txt \t:", NULL, 0);
444 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
445 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
446 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
447 ok(RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to exist\n");
448 ok(!DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to not exist\n");
449 ok(!DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to not exist\n");
451 /* use invalid chars before and after file list */
452 hr = pExtractFiles("extract.cab", "dest", 0, " +-\\ a.txt:testdir\\c.txt a_:", NULL, 0);
453 ok(hr == E_FAIL, "Expected E_FAIL, got %d\n", hr);
454 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
455 ok(!DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to not exist\n");
456 ok(!RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to not exist\n");
458 /* try an empty file list */
459 hr = pExtractFiles("extract.cab", "dest", 0, "", NULL, 0);
460 ok(hr == E_FAIL, "Expected E_FAIL, got %d\n", hr);
461 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
462 ok(!RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to not exist\n");
464 /* try a nonexistent file in the file list */
465 hr = pExtractFiles("extract.cab", "dest", 0, "a.txt:idontexist:testdir\\c.txt", NULL, 0);
466 ok(hr == E_FAIL, "Expected E_FAIL, got %d\n", hr);
467 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
468 ok(!DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to not exist\n");
469 ok(!RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to not exist\n");
472 static void test_AdvInstallFile(void)
474 HRESULT hr;
475 HMODULE hmod;
476 char CURR_DIR[MAX_PATH];
477 char destFolder[MAX_PATH];
479 hmod = LoadLibrary("setupapi.dll");
480 if (!hmod)
482 skip("setupapi.dll not present\n");
483 return;
486 FreeLibrary(hmod);
488 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
490 lstrcpyA(destFolder, CURR_DIR);
491 lstrcatA(destFolder, "\\");
492 lstrcatA(destFolder, "dest");
494 createTestFile("source.txt");
496 /* try invalid source directory */
497 hr = pAdvInstallFile(NULL, NULL, "source.txt", destFolder, "destination.txt", 0, 0);
498 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
499 ok(!DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to not exist\n");
501 /* try invalid source file */
502 hr = pAdvInstallFile(NULL, CURR_DIR, NULL, destFolder, "destination.txt", 0, 0);
503 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
504 ok(!DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to not exist\n");
506 /* try invalid destination directory */
507 hr = pAdvInstallFile(NULL, CURR_DIR, "source.txt", NULL, "destination.txt", 0, 0);
508 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
509 ok(!DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to not exist\n");
511 /* try copying to nonexistent destination directory */
512 hr = pAdvInstallFile(NULL, CURR_DIR, "source.txt", destFolder, "destination.txt", 0, 0);
513 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
514 ok(DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to exist\n");
516 /* native windows screws up if the source file doesn't exist */
518 /* test AIF_NOOVERWRITE behavior, asks the user to overwrite if AIF_QUIET is not specified */
519 createTestFile("dest\\destination.txt");
520 hr = pAdvInstallFile(NULL, CURR_DIR, "source.txt", destFolder,
521 "destination.txt", AIF_NOOVERWRITE | AIF_QUIET, 0);
522 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
523 ok(DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to exist\n");
524 ok(RemoveDirectoryA("dest"), "Expected dest to exist\n");
526 DeleteFileA("source.txt");
529 START_TEST(files)
531 init_function_pointers();
532 create_test_files();
533 create_cab_file();
535 test_AddDelBackupEntry();
536 test_ExtractFiles();
537 test_AdvInstallFile();
539 delete_test_files();
541 FreeLibrary(hAdvPack);