2 * Unit test suite for lz32 functions
4 * Copyright 2004 Evan Parry, Daniel Kegel
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
28 #include "wine/test.h"
30 static char filename_[] = "testfile.xx_";
31 static char filename[] = "testfile.xxx";
32 static char filename2[] = "testfile.yyy";
34 static WCHAR filename_W[] = {'t','e','s','t','f','i','l','e','.','x','x','_',0};
35 static WCHAR filenameW[] = {'t','e','s','t','f','i','l','e','.','x','x','x',0};
37 /* This is the hex string representation of the file created by compressing
38 a simple text file with the contents "This is a test file."
40 The file was created using COMPRESS.EXE from the Windows Server 2003
41 Resource Kit from Microsoft. The resource kit was retrieved from the
44 http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en
46 static const unsigned char compressed_file[] =
47 {0x53,0x5A,0x44,0x44,0x88,0xF0,0x27,0x33,0x41,
48 0x74,0x75,0x14,0x00,0x00,0xDF,0x54,0x68,0x69,
49 0x73,0x20,0xF2,0xF0,0x61,0x20,0xFF,0x74,0x65,
50 0x73,0x74,0x20,0x66,0x69,0x6C,0x03,0x65,0x2E};
51 static const DWORD compressed_file_size = sizeof(compressed_file);
53 static const char uncompressed_data[] = "This is a test file.";
54 static const DWORD uncompressed_data_size = sizeof(uncompressed_data) - 1;
58 static void test_LZOpenFileA(void)
63 char expected[MAX_PATH];
64 char filled_0xA5[OFS_MAXPATHNAME];
66 SetLastError(0xfaceabee);
67 /* Check for nonexistent file. */
68 file = LZOpenFileA("badfilename_", &test, OF_READ);
69 ok(file == LZERROR_BADINHANDLE,
70 "LZOpenFileA succeeded on nonexistent file\n");
71 ok(GetLastError() == ERROR_FILE_NOT_FOUND,
72 "GetLastError() returns %ld\n", GetLastError());
75 /* Create an empty file. */
76 file = LZOpenFileA(filename_, &test, OF_CREATE);
77 ok(file >= 0, "LZOpenFileA failed on creation\n");
79 retval = GetFileAttributesA(filename_);
80 ok(retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %ld\n",
83 /* Check various opening options. */
84 file = LZOpenFileA(filename_, &test, OF_READ);
85 ok(file >= 0, "LZOpenFileA failed on read\n");
87 file = LZOpenFileA(filename_, &test, OF_WRITE);
88 ok(file >= 0, "LZOpenFileA failed on write\n");
90 file = LZOpenFileA(filename_, &test, OF_READWRITE);
91 ok(file >= 0, "LZOpenFileA failed on read/write\n");
93 file = LZOpenFileA(filename_, &test, OF_EXIST);
94 ok(file >= 0, "LZOpenFileA failed on read/write\n");
97 /* If the file "foo.xxx" does not exist, LZOpenFileA should then
98 check for the file "foo.xx_" and open that -- at least on some
99 operating systems. Doesn't seem to on my copy of Win98.
101 retval = GetCurrentDirectoryA(MAX_PATH, expected);
102 ok(retval > 0, "GetCurrentDirectoryA returned %ld, GLE=0x%lx\n",
103 retval, GetLastError());
104 lstrcatA(expected, "\\");
105 lstrcatA(expected, filename);
106 /* Compressed file name ends with underscore. */
107 retval = lstrlenA(expected);
108 expected[retval-1] = '_';
109 memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
110 memset(&test, 0xA5, sizeof(test));
112 /* Try to open compressed file. */
113 file = LZOpenFileA(filename, &test, OF_EXIST);
114 if(file != LZERROR_BADINHANDLE) {
115 ok(test.cBytes == sizeof(OFSTRUCT),
116 "LZOpenFileA set test.cBytes to %d\n", test.cBytes);
117 ok(test.nErrCode == 0, "LZOpenFileA set test.nErrCode to %d\n",
119 ok(lstrcmpA(test.szPathName, expected) == 0,
120 "LZOpenFileA returned '%s', but was expected to return '%s'\n",
121 test.szPathName, expected);
124 ok(test.cBytes == 0xA5,
125 "LZOpenFileA set test.cBytes to %d\n", test.cBytes);
126 ok(test.nErrCode == ERROR_FILE_NOT_FOUND,
127 "LZOpenFileA set test.nErrCode to %d\n", test.nErrCode);
128 ok(strncmp(test.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
129 "LZOpenFileA returned '%s', but was expected to return '%s'\n",
130 test.szPathName, filled_0xA5);
133 /* Delete the file then make sure it doesn't exist anymore. */
134 file = LZOpenFileA(filename_, &test, OF_DELETE);
135 ok(file >= 0, "LZOpenFileA failed on delete\n");
138 retval = GetFileAttributesA(filename_);
139 ok(retval == INVALID_FILE_ATTRIBUTES,
140 "GetFileAttributesA succeeded on deleted file\n");
143 static void test_LZRead(void)
151 /* Create the compressed file. */
152 file = CreateFileA(filename_, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 0);
153 ok(file != INVALID_HANDLE_VALUE, "Could not create test file\n");
154 retok = WriteFile(file, compressed_file, compressed_file_size, &ret, 0);
155 ok( retok, "WriteFile: error %ld\n", GetLastError());
156 ok(ret == compressed_file_size, "Wrote wrong number of bytes with WriteFile?\n");
159 cfile = LZOpenFileA(filename_, &test, OF_READ);
160 ok(cfile > 0, "LZOpenFileA failed\n");
162 ret = LZRead(cfile, buf, uncompressed_data_size);
163 ok(ret == uncompressed_data_size, "Read wrong number of bytes\n");
165 /* Compare what we read with what we think we should read. */
166 ok(memcmp(buf, uncompressed_data, uncompressed_data_size) == 0,
167 "buffer contents mismatch\n");
170 /* Wine returns the number of bytes actually read instead of an error */
171 ret = LZRead(cfile, buf, uncompressed_data_size);
172 ok(ret == LZERROR_READ, "Expected read-past-EOF to return LZERROR_READ\n");
177 ret = DeleteFileA(filename_);
178 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
181 static void test_LZCopy(void)
186 OFSTRUCT stest, dtest;
189 /* Create the compressed file. */
190 file = CreateFileA(filename_, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 0);
191 ok(file != INVALID_HANDLE_VALUE,
192 "CreateFileA: error %ld\n", GetLastError());
193 retok = WriteFile(file, compressed_file, compressed_file_size, &ret, 0);
194 ok( retok, "WriteFile error %ld\n", GetLastError());
195 ok(ret == compressed_file_size, "Wrote wrong number of bytes\n");
198 source = LZOpenFileA(filename_, &stest, OF_READ);
199 ok(source >= 0, "LZOpenFileA failed on compressed file\n");
200 dest = LZOpenFileA(filename2, &dtest, OF_CREATE);
201 ok(dest >= 0, "LZOpenFileA failed on creating new file %d\n", dest);
203 ret = LZCopy(source, dest);
204 ok(ret > 0, "LZCopy error\n");
209 file = CreateFileA(filename2, GENERIC_READ, 0, NULL, OPEN_EXISTING,
211 ok(file != INVALID_HANDLE_VALUE,
212 "CreateFileA: error %ld\n", GetLastError());
214 retok = ReadFile(file, buf, uncompressed_data_size*2, &ret, 0);
215 ok( retok && ret == uncompressed_data_size, "ReadFile: error %ld\n", GetLastError());
216 /* Compare what we read with what we think we should read. */
217 ok(!memcmp(buf, uncompressed_data, uncompressed_data_size),
218 "buffer contents mismatch\n");
221 ret = DeleteFileA(filename_);
222 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
223 ret = DeleteFileA(filename2);
224 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
227 static void test_LZOpenFileW(void)
232 char expected[MAX_PATH];
233 static WCHAR badfilenameW[] = {'b','a','d','f','i','l','e','n','a','m','e','.','x','t','n',0};
235 SetLastError(0xfaceabee);
236 /* Check for nonexistent file. */
237 file = LZOpenFileW(badfilenameW, &test, OF_READ);
238 if(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
240 trace("LZOpenFileW call not implemented, skipping rest of the test\n");
243 ok(file == LZERROR_BADINHANDLE,
244 "LZOpenFileW succeeded on nonexistent file\n");
247 /* Create an empty file. */
248 file = LZOpenFileW(filename_W, &test, OF_CREATE);
249 ok(file >= 0, "LZOpenFile failed on creation\n");
251 retval = GetFileAttributesW(filename_W);
252 ok(retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributes: error %ld\n",
255 /* Check various opening options. */
256 file = LZOpenFileW(filename_W, &test, OF_READ);
257 ok(file >= 0, "LZOpenFileW failed on read\n");
259 file = LZOpenFileW(filename_W, &test, OF_WRITE);
260 ok(file >= 0, "LZOpenFileW failed on write\n");
262 file = LZOpenFileW(filename_W, &test, OF_READWRITE);
263 ok(file >= 0, "LZOpenFileW failed on read/write\n");
265 file = LZOpenFileW(filename_W, &test, OF_EXIST);
266 ok(file >= 0, "LZOpenFileW failed on read/write\n");
269 /* If the file "foo.xxx" does not exist, LZOpenFileW should then
270 check for the file "foo.xx_" and open that -- at least on some
271 operating systems. Doesn't seem to on my copy of Win98.
273 retval = GetCurrentDirectoryA(MAX_PATH, expected);
274 ok(retval > 0, "GetCurrentDirectoryW returned %ld, GLE=0x%lx\n",
275 retval, GetLastError());
276 lstrcatA(expected, "\\");
277 /* It's probably better to use WideCharToMultiByte() on filenameW: */
278 lstrcatA(expected, filename);
279 /* Compressed file name ends with underscore. */
280 retval = lstrlenA(expected);
281 expected[retval-1] = '_';
282 memset(&test, 0xA5, sizeof(test));
284 /* Try to open compressed file. */
285 file = LZOpenFileW(filenameW, &test, OF_EXIST);
286 ok(file >= 0, "LZOpenFileW failed on switching to a compressed file name\n");
287 ok(test.cBytes == sizeof(OFSTRUCT),
288 "LZOpenFileW set test.cBytes to %d\n", test.cBytes);
289 ok(test.nErrCode == ERROR_SUCCESS, "LZOpenFileW set test.nErrCode to %d\n",
291 /* Note that W-function returns A-string by a OFSTRUCT.szPathName: */
292 ok(lstrcmpA(test.szPathName, expected) == 0,
293 "LZOpenFileW returned '%s', but was expected to return '%s'\n",
294 test.szPathName, expected);
297 /* Delete the file then make sure it doesn't exist anymore. */
298 file = LZOpenFileW(filename_W, &test, OF_DELETE);
299 ok(file >= 0, "LZOpenFileW failed on delete\n");
302 retval = GetFileAttributesW(filename_W);
303 ok(retval == INVALID_FILE_ATTRIBUTES,
304 "GetFileAttributesW succeeded on deleted file\n");
308 START_TEST(lzexpand_main)
310 buf = malloc(uncompressed_data_size * 2);