push 9abda7d3ca27cda7f3dc00cfc283807cf0e2403b
[wine/hacks.git] / dlls / advpack / tests / files.c
blobd018975dcaf523a181ad903dd863be8ae4cd2eb0
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 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)
76 DeleteFileA("a.txt");
77 DeleteFileA("b.txt");
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)
88 BOOL ret;
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);
95 return ret;
98 static void test_AddDelBackupEntry(void)
100 HRESULT res;
101 CHAR path[MAX_PATH];
102 CHAR windir[MAX_PATH];
104 lstrcpyA(path, CURR_DIR);
105 lstrcatA(path, "\\backup\\basename.INI");
107 /* native AddDelBackupEntry crashes if lpcszBaseName is NULL */
109 /* try a NULL file list */
110 res = pAddDelBackupEntry(NULL, "backup", "basename", AADBE_ADD_ENTRY);
111 ok(res == S_OK, "Expected S_OK, got %d\n", res);
112 ok(!DeleteFileA(path), "Expected path to not exist\n");
114 lstrcpyA(path, CURR_DIR);
115 lstrcatA(path, "\\backup\\.INI");
117 /* try an empty base name */
118 res = pAddDelBackupEntry("one\0two\0three\0", "backup", "", 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, "\\basename.INI");
125 /* try an invalid flag */
126 res = pAddDelBackupEntry("one\0two\0three\0", NULL, "basename", 0);
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, "c:\\basename.INI");
132 /* create the INF file */
133 res = pAddDelBackupEntry("one\0two\0three\0", "c:\\", "basename", AADBE_ADD_ENTRY);
134 ok(res == S_OK, "Expected S_OK, got %d\n", res);
135 ok(check_ini_file_attr(path), "Expected ini file to be hidden\n");
136 ok(DeleteFileA(path), "Expected path to exist\n");
138 lstrcpyA(path, CURR_DIR);
139 lstrcatA(path, "\\backup\\basename.INI");
141 /* try to create the INI file in a nonexistent directory */
142 RemoveDirectoryA("backup");
143 res = pAddDelBackupEntry("one\0two\0three\0", "backup", "basename", AADBE_ADD_ENTRY);
144 ok(res == S_OK, "Expected S_OK, got %d\n", res);
145 ok(!check_ini_file_attr(path), "Expected ini file to not be hidden\n");
146 ok(!DeleteFileA(path), "Expected path to not exist\n");
148 /* try an existent, relative backup directory */
149 CreateDirectoryA("backup", NULL);
150 res = pAddDelBackupEntry("one\0two\0three\0", "backup", "basename", AADBE_ADD_ENTRY);
151 ok(res == S_OK, "Expected S_OK, got %d\n", res);
152 ok(check_ini_file_attr(path), "Expected ini file to be hidden\n");
153 ok(DeleteFileA(path), "Expected path to exist\n");
154 RemoveDirectoryA("backup");
156 GetWindowsDirectoryA(windir, sizeof(windir));
157 sprintf(path, "%s\\basename.INI", windir);
159 /* try a NULL backup dir, INI is created in the windows directory */
160 res = pAddDelBackupEntry("one\0two\0three\0", NULL, "basename", AADBE_ADD_ENTRY);
161 ok(res == S_OK, "Expected S_OK, got %d\n", res);
163 /* remove the entries with AADBE_DEL_ENTRY */
164 SetFileAttributesA(path, FILE_ATTRIBUTE_NORMAL);
165 res = pAddDelBackupEntry("one\0three\0", NULL, "basename", AADBE_DEL_ENTRY);
166 SetFileAttributesA(path, FILE_ATTRIBUTE_NORMAL);
167 ok(res == S_OK, "Expected S_OK, got %d\n", res);
168 ok(DeleteFileA(path), "Expected path to exist\n");
171 /* the FCI callbacks */
173 static void *mem_alloc(ULONG cb)
175 return HeapAlloc(GetProcessHeap(), 0, cb);
178 static void mem_free(void *memory)
180 HeapFree(GetProcessHeap(), 0, memory);
183 static BOOL get_next_cabinet(PCCAB pccab, ULONG cbPrevCab, void *pv)
185 return TRUE;
188 static long progress(UINT typeStatus, ULONG cb1, ULONG cb2, void *pv)
190 return 0;
193 static int file_placed(PCCAB pccab, char *pszFile, long cbFile,
194 BOOL fContinuation, void *pv)
196 return 0;
199 static INT_PTR fci_open(char *pszFile, int oflag, int pmode, int *err, void *pv)
201 HANDLE handle;
202 DWORD dwAccess = 0;
203 DWORD dwShareMode = 0;
204 DWORD dwCreateDisposition = OPEN_EXISTING;
206 dwAccess = GENERIC_READ | GENERIC_WRITE;
207 /* FILE_SHARE_DELETE is not supported by Windows Me/98/95 */
208 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
210 if (GetFileAttributesA(pszFile) != INVALID_FILE_ATTRIBUTES)
211 dwCreateDisposition = OPEN_EXISTING;
212 else
213 dwCreateDisposition = CREATE_NEW;
215 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
216 dwCreateDisposition, 0, NULL);
218 ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszFile);
220 return (INT_PTR)handle;
223 static UINT fci_read(INT_PTR hf, void *memory, UINT cb, int *err, void *pv)
225 HANDLE handle = (HANDLE)hf;
226 DWORD dwRead;
227 BOOL res;
229 res = ReadFile(handle, memory, cb, &dwRead, NULL);
230 ok(res, "Failed to ReadFile\n");
232 return dwRead;
235 static UINT fci_write(INT_PTR hf, void *memory, UINT cb, int *err, void *pv)
237 HANDLE handle = (HANDLE)hf;
238 DWORD dwWritten;
239 BOOL res;
241 res = WriteFile(handle, memory, cb, &dwWritten, NULL);
242 ok(res, "Failed to WriteFile\n");
244 return dwWritten;
247 static int fci_close(INT_PTR hf, int *err, void *pv)
249 HANDLE handle = (HANDLE)hf;
250 ok(CloseHandle(handle), "Failed to CloseHandle\n");
252 return 0;
255 static long fci_seek(INT_PTR hf, long dist, int seektype, int *err, void *pv)
257 HANDLE handle = (HANDLE)hf;
258 DWORD ret;
260 ret = SetFilePointer(handle, dist, NULL, seektype);
261 ok(ret != INVALID_SET_FILE_POINTER, "Failed to SetFilePointer\n");
263 return ret;
266 static int fci_delete(char *pszFile, int *err, void *pv)
268 BOOL ret = DeleteFileA(pszFile);
269 ok(ret, "Failed to DeleteFile %s\n", pszFile);
271 return 0;
274 static BOOL get_temp_file(char *pszTempName, int cbTempName, void *pv)
276 LPSTR tempname;
278 tempname = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
279 GetTempFileNameA(".", "xx", 0, tempname);
281 if (tempname && (strlen(tempname) < (unsigned)cbTempName))
283 lstrcpyA(pszTempName, tempname);
284 HeapFree(GetProcessHeap(), 0, tempname);
285 return TRUE;
288 HeapFree(GetProcessHeap(), 0, tempname);
290 return FALSE;
293 static INT_PTR get_open_info(char *pszName, USHORT *pdate, USHORT *ptime,
294 USHORT *pattribs, int *err, void *pv)
296 BY_HANDLE_FILE_INFORMATION finfo;
297 FILETIME filetime;
298 HANDLE handle;
299 DWORD attrs;
300 BOOL res;
302 handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL,
303 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
305 ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszName);
307 res = GetFileInformationByHandle(handle, &finfo);
308 ok(res, "Expected GetFileInformationByHandle to succeed\n");
310 FileTimeToLocalFileTime(&finfo.ftLastWriteTime, &filetime);
311 FileTimeToDosDateTime(&filetime, pdate, ptime);
313 attrs = GetFileAttributes(pszName);
314 ok(attrs != INVALID_FILE_ATTRIBUTES, "Failed to GetFileAttributes\n");
316 return (INT_PTR)handle;
319 static void add_file(HFCI hfci, char *file)
321 char path[MAX_PATH];
322 BOOL res;
324 lstrcpyA(path, CURR_DIR);
325 lstrcatA(path, "\\");
326 lstrcatA(path, file);
328 res = FCIAddFile(hfci, path, file, FALSE, get_next_cabinet, progress,
329 get_open_info, tcompTYPE_MSZIP);
330 ok(res, "Expected FCIAddFile to succeed\n");
333 static void set_cab_parameters(PCCAB pCabParams)
335 ZeroMemory(pCabParams, sizeof(CCAB));
337 pCabParams->cb = MEDIA_SIZE;
338 pCabParams->cbFolderThresh = FOLDER_THRESHOLD;
339 pCabParams->setID = 0xbeef;
340 lstrcpyA(pCabParams->szCabPath, CURR_DIR);
341 lstrcatA(pCabParams->szCabPath, "\\");
342 lstrcpyA(pCabParams->szCab, "extract.cab");
345 static void create_cab_file(void)
347 CCAB cabParams;
348 HFCI hfci;
349 ERF erf;
350 static CHAR a_txt[] = "a.txt",
351 b_txt[] = "b.txt",
352 testdir_c_txt[] = "testdir\\c.txt",
353 testdir_d_txt[] = "testdir\\d.txt";
354 BOOL res;
356 set_cab_parameters(&cabParams);
358 hfci = FCICreate(&erf, file_placed, mem_alloc, mem_free, fci_open,
359 fci_read, fci_write, fci_close, fci_seek, fci_delete,
360 get_temp_file, &cabParams, NULL);
362 ok(hfci != NULL, "Failed to create an FCI context\n");
364 add_file(hfci, a_txt);
365 add_file(hfci, b_txt);
366 add_file(hfci, testdir_c_txt);
367 add_file(hfci, testdir_d_txt);
369 res = FCIFlushCabinet(hfci, FALSE, get_next_cabinet, progress);
370 ok(res, "Failed to flush the cabinet\n");
372 res = FCIDestroy(hfci);
373 ok(res, "Failed to destroy the cabinet\n");
376 static void test_ExtractFiles(void)
378 HRESULT hr;
379 char destFolder[MAX_PATH];
381 lstrcpyA(destFolder, CURR_DIR);
382 lstrcatA(destFolder, "\\");
383 lstrcatA(destFolder, "dest");
385 /* try NULL cab file */
386 hr = pExtractFiles(NULL, destFolder, 0, NULL, NULL, 0);
387 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
388 ok(RemoveDirectoryA("dest"), "Expected dest to exist\n");
390 /* try NULL destination */
391 hr = pExtractFiles("extract.cab", NULL, 0, NULL, NULL, 0);
392 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
393 ok(!RemoveDirectoryA("dest"), "Expected dest to not exist\n");
395 /* extract all files in the cab to nonexistent destination directory */
396 hr = pExtractFiles("extract.cab", destFolder, 0, NULL, NULL, 0);
397 ok(hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) ||
398 hr == E_FAIL, /* win95 */
399 "Expected %08x or %08x, got %08x\n", E_FAIL,
400 HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), hr);
401 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
402 ok(!DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to not exist\n");
403 ok(!RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to not exist\n");
404 ok(!RemoveDirectoryA("dest"), "Expected dest to not exist\n");
406 /* extract all files in the cab to the destination directory */
407 CreateDirectoryA("dest", NULL);
408 hr = pExtractFiles("extract.cab", destFolder, 0, NULL, NULL, 0);
409 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
410 ok(DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to exist\n");
411 ok(DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to exist\n");
412 ok(DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
413 ok(DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to exist\n");
414 ok(RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to exist\n");
416 /* extract all files to a relative destination directory */
417 hr = pExtractFiles("extract.cab", "dest", 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 /* only extract two of the files from the cab */
426 hr = pExtractFiles("extract.cab", "dest", 0, "a.txt:testdir\\c.txt", 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\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to exist\n");
430 ok(RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to exist\n");
431 ok(!DeleteFileA("dest\\b.txt"), "Expected dest\\b.txt to not exist\n");
432 ok(!DeleteFileA("dest\\testdir\\d.txt"), "Expected dest\\testdir\\d.txt to not exist\n");
434 /* use valid chars before and after file list */
435 hr = pExtractFiles("extract.cab", "dest", 0, " :\t: a.txt:testdir\\c.txt \t:", 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 invalid chars before and after file list */
444 hr = pExtractFiles("extract.cab", "dest", 0, " +-\\ a.txt:testdir\\c.txt a_:", NULL, 0);
445 ok(hr == E_FAIL, "Expected E_FAIL, got %d\n", hr);
446 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
447 ok(!DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to not exist\n");
448 ok(!RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to not exist\n");
450 /* try an empty file list */
451 hr = pExtractFiles("extract.cab", "dest", 0, "", NULL, 0);
452 ok(hr == E_FAIL, "Expected E_FAIL, got %d\n", hr);
453 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
454 ok(!RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to not exist\n");
456 /* try a nonexistent file in the file list */
457 hr = pExtractFiles("extract.cab", "dest", 0, "a.txt:idontexist:testdir\\c.txt", NULL, 0);
458 ok(hr == E_FAIL, "Expected E_FAIL, got %d\n", hr);
459 ok(!DeleteFileA("dest\\a.txt"), "Expected dest\\a.txt to not exist\n");
460 ok(!DeleteFileA("dest\\testdir\\c.txt"), "Expected dest\\testdir\\c.txt to not exist\n");
461 ok(!RemoveDirectoryA("dest\\testdir"), "Expected dest\\testdir to not exist\n");
464 static void test_AdvInstallFile(void)
466 HRESULT hr;
467 HMODULE hmod;
468 char CURR_DIR[MAX_PATH];
469 char destFolder[MAX_PATH];
471 hmod = LoadLibrary("setupapi.dll");
472 if (!hmod)
474 skip("setupapi.dll not present\n");
475 return;
478 FreeLibrary(hmod);
480 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
482 lstrcpyA(destFolder, CURR_DIR);
483 lstrcatA(destFolder, "\\");
484 lstrcatA(destFolder, "dest");
486 createTestFile("source.txt");
488 /* try invalid source directory */
489 hr = pAdvInstallFile(NULL, NULL, "source.txt", destFolder, "destination.txt", 0, 0);
490 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
491 ok(!DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to not exist\n");
493 /* try invalid source file */
494 hr = pAdvInstallFile(NULL, CURR_DIR, NULL, destFolder, "destination.txt", 0, 0);
495 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
496 ok(!DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to not exist\n");
498 /* try invalid destination directory */
499 hr = pAdvInstallFile(NULL, CURR_DIR, "source.txt", NULL, "destination.txt", 0, 0);
500 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
501 ok(!DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to not exist\n");
503 /* try copying to nonexistent destination directory */
504 hr = pAdvInstallFile(NULL, CURR_DIR, "source.txt", destFolder, "destination.txt", 0, 0);
505 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
506 ok(DeleteFileA("dest\\destination.txt"), "Expected dest\\destination.txt to exist\n");
508 /* native windows screws up if the source file doesn't exist */
510 /* test AIF_NOOVERWRITE behavior, asks the user to overwrite if AIF_QUIET is not specified */
511 createTestFile("dest\\destination.txt");
512 hr = pAdvInstallFile(NULL, CURR_DIR, "source.txt", destFolder,
513 "destination.txt", AIF_NOOVERWRITE | AIF_QUIET, 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");
516 ok(RemoveDirectoryA("dest"), "Expected dest to exist\n");
518 DeleteFileA("source.txt");
521 START_TEST(files)
523 DWORD len;
524 char temp_path[MAX_PATH], prev_path[MAX_PATH];
526 init_function_pointers();
528 GetCurrentDirectoryA(MAX_PATH, prev_path);
529 GetTempPath(MAX_PATH, temp_path);
530 SetCurrentDirectoryA(temp_path);
532 lstrcpyA(CURR_DIR, temp_path);
533 len = lstrlenA(CURR_DIR);
535 if(len && (CURR_DIR[len - 1] == '\\'))
536 CURR_DIR[len - 1] = 0;
538 create_test_files();
539 create_cab_file();
541 test_AddDelBackupEntry();
542 test_ExtractFiles();
543 test_AdvInstallFile();
545 delete_test_files();
547 FreeLibrary(hAdvPack);
548 SetCurrentDirectoryA(prev_path);