2 * Unit tests for the File Decompression Interface
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 CHAR CURR_DIR[MAX_PATH];
35 static void *fdi_alloc(ULONG cb)
37 return HeapAlloc(GetProcessHeap(), 0, cb);
40 static void *fdi_alloc_bad(ULONG cb)
45 static void fdi_free(void *pv)
47 HeapFree(GetProcessHeap(), 0, pv);
50 static INT_PTR fdi_open(char *pszFile, int oflag, int pmode)
53 handle = CreateFileA(pszFile, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
54 OPEN_EXISTING, 0, NULL );
55 if (handle == INVALID_HANDLE_VALUE)
57 return (INT_PTR) handle;
60 static UINT fdi_read(INT_PTR hf, void *pv, UINT cb)
62 HANDLE handle = (HANDLE) hf;
64 if (ReadFile(handle, pv, cb, &dwRead, NULL))
69 static UINT fdi_write(INT_PTR hf, void *pv, UINT cb)
71 HANDLE handle = (HANDLE) hf;
73 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
78 static int fdi_close(INT_PTR hf)
80 HANDLE handle = (HANDLE) hf;
81 return CloseHandle(handle) ? 0 : -1;
84 static long fdi_seek(INT_PTR hf, long dist, int seektype)
86 HANDLE handle = (HANDLE) hf;
87 return SetFilePointer(handle, dist, NULL, seektype);
90 static void test_FDICreate(void)
95 /* native crashes if pfnalloc is NULL */
97 /* FDICreate does not crash with a NULL pfnfree,
98 * but FDIDestroy will crash when it tries to access it.
102 SetLastError(0xdeadbeef);
103 erf.erfOper = 0xcafefeed;
104 erf.erfType = 0xdeadbabe;
105 erf.fError = 0xdecaface;
106 hfdi = FDICreate(fdi_alloc, NULL, fdi_open, fdi_read,
107 fdi_write, fdi_close, fdi_seek,
109 ok(hfdi != NULL, "Expected non-NULL context\n");
110 ok(GetLastError() == 0xdeadbeef,
111 "Expected 0xdeadbeef, got %d\n", GetLastError());
112 ok(erf.erfOper == 0xcafefeed, "Expected 0xcafefeed, got %d\n", erf.erfOper);
113 ok(erf.erfType == 0xdeadbabe, "Expected 0xdeadbabe, got %d\n", erf.erfType);
114 ok(erf.fError == 0xdecaface, "Expected 0xdecaface, got %d\n", erf.fError);
119 SetLastError(0xdeadbeef);
120 erf.erfOper = 0xcafefeed;
121 erf.erfType = 0xdeadbabe;
122 erf.fError = 0xdecaface;
123 hfdi = FDICreate(fdi_alloc, fdi_free, NULL, fdi_read,
124 fdi_write, fdi_close, fdi_seek,
126 ok(hfdi != NULL, "Expected non-NULL context\n");
127 ok(GetLastError() == 0xdeadbeef,
128 "Expected 0xdeadbeef, got %d\n", GetLastError());
129 ok((erf.erfOper == 0xcafefeed || erf.erfOper == 0 /* Vista */), "Expected 0xcafefeed or 0, got %d\n", erf.erfOper);
130 ok((erf.erfType == 0xdeadbabe || erf.erfType == 0 /* Vista */), "Expected 0xdeadbabe or 0, got %d\n", erf.erfType);
131 ok((erf.fError == 0xdecaface || erf.fError == 0 /* Vista */), "Expected 0xdecaface or 0, got %d\n", erf.fError);
135 SetLastError(0xdeadbeef);
136 erf.erfOper = 0xcafefeed;
137 erf.erfType = 0xdeadbabe;
138 erf.fError = 0xdecaface;
139 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, NULL,
140 fdi_write, fdi_close, fdi_seek,
142 ok(hfdi != NULL, "Expected non-NULL context\n");
143 ok(GetLastError() == 0xdeadbeef,
144 "Expected 0xdeadbeef, got %d\n", GetLastError());
145 ok((erf.erfOper == 0xcafefeed || erf.erfOper == 0 /* Vista */), "Expected 0xcafefeed or 0, got %d\n", erf.erfOper);
146 ok((erf.erfType == 0xdeadbabe || erf.erfType == 0 /* Vista */), "Expected 0xdeadbabe or 0, got %d\n", erf.erfType);
147 ok((erf.fError == 0xdecaface || erf.fError == 0 /* Vista */), "Expected 0xdecaface or 0, got %d\n", erf.fError);
151 SetLastError(0xdeadbeef);
152 erf.erfOper = 0xcafefeed;
153 erf.erfType = 0xdeadbabe;
154 erf.fError = 0xdecaface;
155 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read,
156 NULL, fdi_close, fdi_seek,
158 ok(hfdi != NULL, "Expected non-NULL context\n");
159 ok(GetLastError() == 0xdeadbeef,
160 "Expected 0xdeadbeef, got %d\n", GetLastError());
161 ok((erf.erfOper == 0xcafefeed || erf.erfOper == 0 /* Vista */), "Expected 0xcafefeed or 0, got %d\n", erf.erfOper);
162 ok((erf.erfType == 0xdeadbabe || erf.erfType == 0 /* Vista */), "Expected 0xdeadbabe or 0, got %d\n", erf.erfType);
163 ok((erf.fError == 0xdecaface || erf.fError == 0 /* Vista */), "Expected 0xdecaface or 0, got %d\n", erf.fError);
167 SetLastError(0xdeadbeef);
168 erf.erfOper = 0xcafefeed;
169 erf.erfType = 0xdeadbabe;
170 erf.fError = 0xdecaface;
171 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read,
172 fdi_write, NULL, fdi_seek,
174 ok(hfdi != NULL, "Expected non-NULL context\n");
175 ok(GetLastError() == 0xdeadbeef,
176 "Expected 0xdeadbeef, got %d\n", GetLastError());
177 ok((erf.erfOper == 0xcafefeed || erf.erfOper == 0 /* Vista */), "Expected 0xcafefeed or 0, got %d\n", erf.erfOper);
178 ok((erf.erfType == 0xdeadbabe || erf.erfType == 0 /* Vista */), "Expected 0xdeadbabe or 0, got %d\n", erf.erfType);
179 ok((erf.fError == 0xdecaface || erf.fError == 0 /* Vista */), "Expected 0xdecaface or 0, got %d\n", erf.fError);
183 SetLastError(0xdeadbeef);
184 erf.erfOper = 0xcafefeed;
185 erf.erfType = 0xdeadbabe;
186 erf.fError = 0xdecaface;
187 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read,
188 fdi_write, fdi_close, NULL,
190 ok(hfdi != NULL, "Expected non-NULL context\n");
191 ok(GetLastError() == 0xdeadbeef,
192 "Expected 0xdeadbeef, got %d\n", GetLastError());
193 ok((erf.erfOper == 0xcafefeed || erf.erfOper == 0 /* Vista */), "Expected 0xcafefeed or 0, got %d\n", erf.erfOper);
194 ok((erf.erfType == 0xdeadbabe || erf.erfType == 0 /* Vista */), "Expected 0xdeadbabe or 0, got %d\n", erf.erfType);
195 ok((erf.fError == 0xdecaface || erf.fError == 0 /* Vista */), "Expected 0xdecaface or 0, got %d\n", erf.fError);
199 SetLastError(0xdeadbeef);
200 erf.erfOper = 0xcafefeed;
201 erf.erfType = 0xdeadbabe;
202 erf.fError = 0xdecaface;
203 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read,
204 fdi_write, fdi_close, fdi_seek,
206 /* XP sets hfdi to a non-NULL value, but Vista sets it to NULL! */
207 ok(GetLastError() == 0xdeadbeef,
208 "Expected 0xdeadbeef, got %d\n", GetLastError());
209 /* NULL is passed to FDICreate instead of &erf, so don't retest the erf member values. */
214 SetLastError(0xdeadbeef);
215 erf.erfOper = 0xcafefeed;
216 erf.erfType = 0xdeadbabe;
217 erf.fError = 0xdecaface;
218 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read,
219 fdi_write, fdi_close, fdi_seek,
221 ok(hfdi != NULL, "Expected non-NULL context\n");
222 ok(GetLastError() == 0xdeadbeef,
223 "Expected 0xdeadbeef, got %d\n", GetLastError());
224 ok((erf.erfOper == 0xcafefeed || erf.erfOper == 0 /* Vista */), "Expected 0xcafefeed or 0, got %d\n", erf.erfOper);
225 ok((erf.erfType == 0xdeadbabe || erf.erfType == 0 /* Vista */), "Expected 0xdeadbabe or 0, got %d\n", erf.erfType);
226 ok((erf.fError == 0xdecaface || erf.fError == 0 /* Vista */), "Expected 0xdecaface or 0, got %d\n", erf.fError);
231 SetLastError(0xdeadbeef);
232 erf.erfOper = 0xcafefeed;
233 erf.erfType = 0xdeadbabe;
234 erf.fError = 0xdecaface;
235 hfdi = FDICreate(fdi_alloc_bad, fdi_free, fdi_open, fdi_read,
236 fdi_write, fdi_close, fdi_seek,
238 ok(hfdi == NULL, "Expected NULL context, got %p\n", hfdi);
239 ok(erf.erfOper == FDIERROR_ALLOC_FAIL,
240 "Expected FDIERROR_ALLOC_FAIL, got %d\n", erf.erfOper);
241 ok(erf.fError == TRUE, "Expected TRUE, got %d\n", erf.fError);
244 ok(GetLastError() == 0xdeadbeef,
245 "Expected 0xdeadbeef, got %d\n", GetLastError());
246 ok(erf.erfType == 0, "Expected 0, got %d\n", erf.erfType);
250 static void test_FDIDestroy(void)
256 /* native crashes if hfdi is NULL or invalid */
258 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read,
259 fdi_write, fdi_close, fdi_seek,
261 ok(hfdi != NULL, "Expected non-NULL context\n");
263 /* successfully destroy hfdi */
264 ret = FDIDestroy(hfdi);
265 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
267 /* native crashes if you try to destroy hfdi twice */
270 /* try to destroy hfdi again */
271 ret = FDIDestroy(hfdi);
272 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
276 static void createTestFile(const CHAR *name)
281 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
282 ok(file != INVALID_HANDLE_VALUE, "Failure to open file %s\n", name);
283 WriteFile(file, name, strlen(name), &written, NULL);
284 WriteFile(file, "\n", strlen("\n"), &written, NULL);
288 static void create_test_files(void)
292 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
293 len = lstrlenA(CURR_DIR);
295 if(len && (CURR_DIR[len-1] == '\\'))
298 createTestFile("a.txt");
299 createTestFile("b.txt");
300 CreateDirectoryA("testdir", NULL);
301 createTestFile("testdir\\c.txt");
302 createTestFile("testdir\\d.txt");
305 static void delete_test_files(void)
307 DeleteFileA("a.txt");
308 DeleteFileA("b.txt");
309 DeleteFileA("testdir\\c.txt");
310 DeleteFileA("testdir\\d.txt");
311 RemoveDirectoryA("testdir");
313 DeleteFileA("extract.cab");
318 static void *mem_alloc(ULONG cb)
320 return HeapAlloc(GetProcessHeap(), 0, cb);
323 static void mem_free(void *memory)
325 HeapFree(GetProcessHeap(), 0, memory);
328 static BOOL get_next_cabinet(PCCAB pccab, ULONG cbPrevCab, void *pv)
333 static long progress(UINT typeStatus, ULONG cb1, ULONG cb2, void *pv)
338 static int file_placed(PCCAB pccab, char *pszFile, long cbFile,
339 BOOL fContinuation, void *pv)
344 static INT_PTR fci_open(char *pszFile, int oflag, int pmode, int *err, void *pv)
348 DWORD dwShareMode = 0;
349 DWORD dwCreateDisposition = OPEN_EXISTING;
351 dwAccess = GENERIC_READ | GENERIC_WRITE;
352 /* FILE_SHARE_DELETE is not supported by Windows Me/98/95 */
353 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
355 if (GetFileAttributesA(pszFile) != INVALID_FILE_ATTRIBUTES)
356 dwCreateDisposition = OPEN_EXISTING;
358 dwCreateDisposition = CREATE_NEW;
360 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
361 dwCreateDisposition, 0, NULL);
363 ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszFile);
365 return (INT_PTR)handle;
368 static UINT fci_read(INT_PTR hf, void *memory, UINT cb, int *err, void *pv)
370 HANDLE handle = (HANDLE)hf;
374 res = ReadFile(handle, memory, cb, &dwRead, NULL);
375 ok(res, "Failed to ReadFile\n");
380 static UINT fci_write(INT_PTR hf, void *memory, UINT cb, int *err, void *pv)
382 HANDLE handle = (HANDLE)hf;
386 res = WriteFile(handle, memory, cb, &dwWritten, NULL);
387 ok(res, "Failed to WriteFile\n");
392 static int fci_close(INT_PTR hf, int *err, void *pv)
394 HANDLE handle = (HANDLE)hf;
395 ok(CloseHandle(handle), "Failed to CloseHandle\n");
400 static long fci_seek(INT_PTR hf, long dist, int seektype, int *err, void *pv)
402 HANDLE handle = (HANDLE)hf;
405 ret = SetFilePointer(handle, dist, NULL, seektype);
406 ok(ret != INVALID_SET_FILE_POINTER, "Failed to SetFilePointer\n");
411 static int fci_delete(char *pszFile, int *err, void *pv)
413 BOOL ret = DeleteFileA(pszFile);
414 ok(ret, "Failed to DeleteFile %s\n", pszFile);
419 static BOOL get_temp_file(char *pszTempName, int cbTempName, void *pv)
423 tempname = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
424 GetTempFileNameA(".", "xx", 0, tempname);
426 if (tempname && (strlen(tempname) < (unsigned)cbTempName))
428 lstrcpyA(pszTempName, tempname);
429 HeapFree(GetProcessHeap(), 0, tempname);
433 HeapFree(GetProcessHeap(), 0, tempname);
438 static INT_PTR get_open_info(char *pszName, USHORT *pdate, USHORT *ptime,
439 USHORT *pattribs, int *err, void *pv)
441 BY_HANDLE_FILE_INFORMATION finfo;
447 handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL,
448 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
450 ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszName);
452 res = GetFileInformationByHandle(handle, &finfo);
453 ok(res, "Expected GetFileInformationByHandle to succeed\n");
455 FileTimeToLocalFileTime(&finfo.ftLastWriteTime, &filetime);
456 FileTimeToDosDateTime(&filetime, pdate, ptime);
458 attrs = GetFileAttributes(pszName);
459 ok(attrs != INVALID_FILE_ATTRIBUTES, "Failed to GetFileAttributes\n");
460 /* fixme: should convert attrs to *pattribs, make sure
461 * have a test that catches the fact that we don't?
464 return (INT_PTR)handle;
467 static void add_file(HFCI hfci, char *file)
472 lstrcpyA(path, CURR_DIR);
473 lstrcatA(path, "\\");
474 lstrcatA(path, file);
476 res = FCIAddFile(hfci, path, file, FALSE, get_next_cabinet, progress,
477 get_open_info, tcompTYPE_MSZIP);
478 ok(res, "Expected FCIAddFile to succeed\n");
481 static void set_cab_parameters(PCCAB pCabParams)
483 ZeroMemory(pCabParams, sizeof(CCAB));
485 pCabParams->cb = MEDIA_SIZE;
486 pCabParams->cbFolderThresh = FOLDER_THRESHOLD;
487 pCabParams->setID = 0xbeef;
488 lstrcpyA(pCabParams->szCabPath, CURR_DIR);
489 lstrcatA(pCabParams->szCabPath, "\\");
490 lstrcpyA(pCabParams->szCab, "extract.cab");
493 static void create_cab_file(void)
498 static CHAR a_txt[] = "a.txt",
500 testdir_c_txt[] = "testdir\\c.txt",
501 testdir_d_txt[] = "testdir\\d.txt";
504 set_cab_parameters(&cabParams);
506 hfci = FCICreate(&erf, file_placed, mem_alloc, mem_free, fci_open,
507 fci_read, fci_write, fci_close, fci_seek, fci_delete,
508 get_temp_file, &cabParams, NULL);
510 ok(hfci != NULL, "Failed to create an FCI context\n");
512 add_file(hfci, a_txt);
513 add_file(hfci, b_txt);
514 add_file(hfci, testdir_c_txt);
515 add_file(hfci, testdir_d_txt);
517 res = FCIFlushCabinet(hfci, FALSE, get_next_cabinet, progress);
518 ok(res, "Failed to flush the cabinet\n");
520 res = FCIDestroy(hfci);
521 ok(res, "Failed to destroy the cabinet\n");
524 static void test_FDIIsCabinet(void)
530 FDICABINETINFO cabinfo;
531 char temp[] = "temp.txt";
532 char extract[] = "extract.cab";
537 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read,
538 fdi_write, fdi_close, fdi_seek,
540 ok(hfdi != NULL, "Expected non-NULL context\n");
542 /* native crashes if hfdi or cabinfo are NULL or invalid */
544 /* invalid file handle */
545 ZeroMemory(&cabinfo, sizeof(FDICABINETINFO));
546 SetLastError(0xdeadbeef);
547 ret = FDIIsCabinet(hfdi, (int)INVALID_HANDLE_VALUE, &cabinfo);
548 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
549 ok(GetLastError() == ERROR_INVALID_HANDLE,
550 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
551 ok(cabinfo.cbCabinet == 0, "Expected 0, got %ld\n", cabinfo.cbCabinet);
552 ok(cabinfo.cFiles == 0, "Expected 0, got %d\n", cabinfo.cFiles);
553 ok(cabinfo.cFolders == 0, "Expected 0, got %d\n", cabinfo.cFolders);
554 ok(cabinfo.iCabinet == 0, "Expected 0, got %d\n", cabinfo.iCabinet);
555 ok(cabinfo.setID == 0, "Expected 0, got %d\n", cabinfo.setID);
557 createTestFile("temp.txt");
558 fd = fdi_open(temp, 0, 0);
560 /* file handle doesn't point to a cabinet */
561 ZeroMemory(&cabinfo, sizeof(FDICABINETINFO));
562 SetLastError(0xdeadbeef);
563 ret = FDIIsCabinet(hfdi, fd, &cabinfo);
564 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
565 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
566 ok(cabinfo.cbCabinet == 0, "Expected 0, got %ld\n", cabinfo.cbCabinet);
567 ok(cabinfo.cFiles == 0, "Expected 0, got %d\n", cabinfo.cFiles);
568 ok(cabinfo.cFolders == 0, "Expected 0, got %d\n", cabinfo.cFolders);
569 ok(cabinfo.iCabinet == 0, "Expected 0, got %d\n", cabinfo.iCabinet);
570 ok(cabinfo.setID == 0, "Expected 0, got %d\n", cabinfo.setID);
573 DeleteFileA("temp.txt");
576 fd = fdi_open(extract, 0, 0);
577 ZeroMemory(&cabinfo, sizeof(FDICABINETINFO));
578 SetLastError(0xdeadbeef);
579 ret = FDIIsCabinet(hfdi, fd, &cabinfo);
580 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
581 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
582 ok(cabinfo.cFiles == 4, "Expected 4, got %d\n", cabinfo.cFiles);
583 ok(cabinfo.cFolders == 1, "Expected 1, got %d\n", cabinfo.cFolders);
584 ok(cabinfo.setID == 0xbeef, "Expected 0xbeef, got %d\n", cabinfo.setID);
587 ok(cabinfo.cbCabinet == 182, "Expected 182, got %ld\n", cabinfo.cbCabinet);
588 ok(cabinfo.iCabinet == 0, "Expected 0, got %d\n", cabinfo.iCabinet);
597 INT_PTR __cdecl CopyProgress (FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
602 static void test_FDICopy(void)
609 char name[] = "extract.cab";
610 char path[MAX_PATH + 1];
612 GetCurrentDirectoryA(MAX_PATH, path);
614 set_cab_parameters(&cabParams);
616 hfci = FCICreate(&erf, file_placed, mem_alloc, mem_free, fci_open,
617 fci_read, fci_write, fci_close, fci_seek,
618 fci_delete, get_temp_file, &cabParams, NULL);
620 ret = FCIFlushCabinet(hfci, FALSE, get_next_cabinet, progress);
621 ok(ret, "Failed to flush the cabinet\n");
625 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read,
626 fdi_write, fdi_close, fdi_seek,
629 ret=FDICopy(hfdi, name, path, 0, CopyProgress, NULL, 0);
632 ok(ret, "Expected FDICopy to succeed\n");