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];
65 static char badfilename_[] = "badfilename_";
67 SetLastError(0xfaceabee);
68 /* Check for nonexistent file. */
69 file = LZOpenFileA(badfilename_, &test, OF_READ);
70 ok(file == LZERROR_BADINHANDLE,
71 "LZOpenFileA succeeded on nonexistent file\n");
72 ok(GetLastError() == ERROR_FILE_NOT_FOUND,
73 "GetLastError() returns %ld\n", GetLastError());
76 /* Create an empty file. */
77 file = LZOpenFileA(filename_, &test, OF_CREATE);
78 ok(file >= 0, "LZOpenFileA failed on creation\n");
80 retval = GetFileAttributesA(filename_);
81 ok(retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %ld\n",
84 /* Check various opening options. */
85 file = LZOpenFileA(filename_, &test, OF_READ);
86 ok(file >= 0, "LZOpenFileA failed on read\n");
88 file = LZOpenFileA(filename_, &test, OF_WRITE);
89 ok(file >= 0, "LZOpenFileA failed on write\n");
91 file = LZOpenFileA(filename_, &test, OF_READWRITE);
92 ok(file >= 0, "LZOpenFileA failed on read/write\n");
94 file = LZOpenFileA(filename_, &test, OF_EXIST);
95 ok(file >= 0, "LZOpenFileA failed on read/write\n");
98 /* If the file "foo.xxx" does not exist, LZOpenFileA should then
99 check for the file "foo.xx_" and open that -- at least on some
100 operating systems. Doesn't seem to on my copy of Win98.
102 retval = GetCurrentDirectoryA(MAX_PATH, expected);
103 ok(retval > 0, "GetCurrentDirectoryA returned %ld, GLE=0x%lx\n",
104 retval, GetLastError());
105 lstrcatA(expected, "\\");
106 lstrcatA(expected, filename);
107 /* Compressed file name ends with underscore. */
108 retval = lstrlenA(expected);
109 expected[retval-1] = '_';
110 memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
111 memset(&test, 0xA5, sizeof(test));
113 /* Try to open compressed file. */
114 file = LZOpenFileA(filename, &test, OF_EXIST);
115 if(file != LZERROR_BADINHANDLE) {
116 ok(test.cBytes == sizeof(OFSTRUCT),
117 "LZOpenFileA set test.cBytes to %d\n", test.cBytes);
118 ok(test.nErrCode == 0, "LZOpenFileA set test.nErrCode to %d\n",
120 ok(lstrcmpA(test.szPathName, expected) == 0,
121 "LZOpenFileA returned '%s', but was expected to return '%s'\n",
122 test.szPathName, expected);
125 ok(test.cBytes == 0xA5,
126 "LZOpenFileA set test.cBytes to %d\n", test.cBytes);
127 ok(test.nErrCode == ERROR_FILE_NOT_FOUND,
128 "LZOpenFileA set test.nErrCode to %d\n", test.nErrCode);
129 ok(strncmp(test.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
130 "LZOpenFileA returned '%s', but was expected to return '%s'\n",
131 test.szPathName, filled_0xA5);
134 /* Delete the file then make sure it doesn't exist anymore. */
135 file = LZOpenFileA(filename_, &test, OF_DELETE);
136 ok(file >= 0, "LZOpenFileA failed on delete\n");
139 retval = GetFileAttributesA(filename_);
140 ok(retval == INVALID_FILE_ATTRIBUTES,
141 "GetFileAttributesA succeeded on deleted file\n");
144 static void test_LZRead(void)
152 /* Create the compressed file. */
153 file = CreateFileA(filename_, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 0);
154 ok(file != INVALID_HANDLE_VALUE, "Could not create test file\n");
155 retok = WriteFile(file, compressed_file, compressed_file_size, &ret, 0);
156 ok( retok, "WriteFile: error %ld\n", GetLastError());
157 ok(ret == compressed_file_size, "Wrote wrong number of bytes with WriteFile?\n");
160 cfile = LZOpenFileA(filename_, &test, OF_READ);
161 ok(cfile > 0, "LZOpenFileA failed\n");
163 ret = LZRead(cfile, buf, uncompressed_data_size);
164 ok(ret == uncompressed_data_size, "Read wrong number of bytes\n");
166 /* Compare what we read with what we think we should read. */
167 ok(memcmp(buf, uncompressed_data, uncompressed_data_size) == 0,
168 "buffer contents mismatch\n");
171 /* Wine returns the number of bytes actually read instead of an error */
172 ret = LZRead(cfile, buf, uncompressed_data_size);
173 ok(ret == LZERROR_READ, "Expected read-past-EOF to return LZERROR_READ\n");
178 ret = DeleteFileA(filename_);
179 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
182 static void test_LZCopy(void)
187 OFSTRUCT stest, dtest;
190 /* Create the compressed file. */
191 file = CreateFileA(filename_, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 0);
192 ok(file != INVALID_HANDLE_VALUE,
193 "CreateFileA: error %ld\n", GetLastError());
194 retok = WriteFile(file, compressed_file, compressed_file_size, &ret, 0);
195 ok( retok, "WriteFile error %ld\n", GetLastError());
196 ok(ret == compressed_file_size, "Wrote wrong number of bytes\n");
199 source = LZOpenFileA(filename_, &stest, OF_READ);
200 ok(source >= 0, "LZOpenFileA failed on compressed file\n");
201 dest = LZOpenFileA(filename2, &dtest, OF_CREATE);
202 ok(dest >= 0, "LZOpenFileA failed on creating new file %d\n", dest);
204 ret = LZCopy(source, dest);
205 ok(ret > 0, "LZCopy error\n");
210 file = CreateFileA(filename2, GENERIC_READ, 0, NULL, OPEN_EXISTING,
212 ok(file != INVALID_HANDLE_VALUE,
213 "CreateFileA: error %ld\n", GetLastError());
215 retok = ReadFile(file, buf, uncompressed_data_size*2, &ret, 0);
216 ok( retok && ret == uncompressed_data_size, "ReadFile: error %ld\n", GetLastError());
217 /* Compare what we read with what we think we should read. */
218 ok(!memcmp(buf, uncompressed_data, uncompressed_data_size),
219 "buffer contents mismatch\n");
222 ret = DeleteFileA(filename_);
223 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
224 ret = DeleteFileA(filename2);
225 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
228 static void test_LZOpenFileW(void)
233 char expected[MAX_PATH];
234 static WCHAR badfilenameW[] = {'b','a','d','f','i','l','e','n','a','m','e','.','x','t','n',0};
236 SetLastError(0xfaceabee);
237 /* Check for nonexistent file. */
238 file = LZOpenFileW(badfilenameW, &test, OF_READ);
239 if(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
241 trace("LZOpenFileW call not implemented, skipping rest of the test\n");
244 ok(file == LZERROR_BADINHANDLE,
245 "LZOpenFileW succeeded on nonexistent file\n");
248 /* Create an empty file. */
249 file = LZOpenFileW(filename_W, &test, OF_CREATE);
250 ok(file >= 0, "LZOpenFile failed on creation\n");
252 retval = GetFileAttributesW(filename_W);
253 ok(retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributes: error %ld\n",
256 /* Check various opening options. */
257 file = LZOpenFileW(filename_W, &test, OF_READ);
258 ok(file >= 0, "LZOpenFileW failed on read\n");
260 file = LZOpenFileW(filename_W, &test, OF_WRITE);
261 ok(file >= 0, "LZOpenFileW failed on write\n");
263 file = LZOpenFileW(filename_W, &test, OF_READWRITE);
264 ok(file >= 0, "LZOpenFileW failed on read/write\n");
266 file = LZOpenFileW(filename_W, &test, OF_EXIST);
267 ok(file >= 0, "LZOpenFileW failed on read/write\n");
270 /* If the file "foo.xxx" does not exist, LZOpenFileW should then
271 check for the file "foo.xx_" and open that -- at least on some
272 operating systems. Doesn't seem to on my copy of Win98.
274 retval = GetCurrentDirectoryA(MAX_PATH, expected);
275 ok(retval > 0, "GetCurrentDirectoryW returned %ld, GLE=0x%lx\n",
276 retval, GetLastError());
277 lstrcatA(expected, "\\");
278 /* It's probably better to use WideCharToMultiByte() on filenameW: */
279 lstrcatA(expected, filename);
280 /* Compressed file name ends with underscore. */
281 retval = lstrlenA(expected);
282 expected[retval-1] = '_';
283 memset(&test, 0xA5, sizeof(test));
285 /* Try to open compressed file. */
286 file = LZOpenFileW(filenameW, &test, OF_EXIST);
287 ok(file >= 0, "LZOpenFileW failed on switching to a compressed file name\n");
288 ok(test.cBytes == sizeof(OFSTRUCT),
289 "LZOpenFileW set test.cBytes to %d\n", test.cBytes);
290 ok(test.nErrCode == ERROR_SUCCESS, "LZOpenFileW set test.nErrCode to %d\n",
292 /* Note that W-function returns A-string by a OFSTRUCT.szPathName: */
293 ok(lstrcmpA(test.szPathName, expected) == 0,
294 "LZOpenFileW returned '%s', but was expected to return '%s'\n",
295 test.szPathName, expected);
298 /* Delete the file then make sure it doesn't exist anymore. */
299 file = LZOpenFileW(filename_W, &test, OF_DELETE);
300 ok(file >= 0, "LZOpenFileW failed on delete\n");
303 retval = GetFileAttributesW(filename_W);
304 ok(retval == INVALID_FILE_ATTRIBUTES,
305 "GetFileAttributesW succeeded on deleted file\n");
309 START_TEST(lzexpand_main)
311 buf = malloc(uncompressed_data_size * 2);