Overlay icons for .lnk files with a small arrow in the lower left
[wine] / dlls / lzexpand / tests / lzexpand_main.c
1 /*
2  * Unit test suite for lz32 functions
3  *
4  * Copyright 2004 Evan Parry, Daniel Kegel
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22 #include <windef.h>
23 #include <winbase.h>
24 #include <stdlib.h>
25
26 #ifndef STANDALONE
27 #include "wine/test.h"
28 #define ok2 ok
29 #else
30 /* To build outside Wine tree, compile with cl -DSTANDALONE -D_X86_ lzexpand_main.c lz32.lib */
31 #include <assert.h>
32 #include <stdio.h>
33 #define START_TEST(name) main(int argc, char **argv)
34 #define ok(condition, msg)       \
35         do { if(!(condition)) {  \
36                 fprintf(stderr,"failed at %d, msg:" msg "\n",__LINE__); \
37                 exit(1);         \
38         } } while(0)
39 #define ok2(condition, msg, arg) \
40         do { if(!(condition)) {  \
41                 fprintf(stderr,"failed at %d, msg:" msg "\n",__LINE__, arg); \
42                 exit(1);         \
43         } } while(0)
44 #define todo_wine
45 #endif
46
47 #include <winerror.h>
48 #include <lzexpand.h>
49
50 static char filename_[] = "testfile.xx_";
51 #if 0
52 static char filename[] = "testfile.xxx";
53 #endif
54 static char filename2[] = "testfile.yyy";
55
56 /* This is the hex string representation of the file created by compressing
57    a simple text file with the contents "This is a test file."
58  
59    The file was created using COMPRESS.EXE from the Windows Server 2003
60    Resource Kit from Microsoft.  The resource kit was retrieved from the
61    following URL:  
62
63    http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en
64  */
65 static const unsigned char compressed_file[] = 
66   {0x53,0x5A,0x44,0x44,0x88,0xF0,0x27,0x33,0x41,
67    0x74,0x75,0x14,0x00,0x00,0xDF,0x54,0x68,0x69,
68    0x73,0x20,0xF2,0xF0,0x61,0x20,0xFF,0x74,0x65,
69    0x73,0x74,0x20,0x66,0x69,0x6C,0x03,0x65,0x2E};
70 static const DWORD compressed_file_size = sizeof(compressed_file);
71
72 static const char uncompressed_data[] = "This is a test file.";
73 static const DWORD uncompressed_data_size = sizeof(uncompressed_data) - 1;
74
75 static char *buf;
76
77 static void test_lzopenfile(void)
78 {
79   OFSTRUCT test;
80   DWORD retval;
81   INT file;
82
83   /* Check for nonexistent file. */
84   file = LZOpenFile("badfilename_", &test, OF_READ);
85   ok(file == LZERROR_BADINHANDLE, 
86      "LZOpenFile succeeded on nonexistent file\n");
87   LZClose(file);
88
89   /* Create an empty file. */
90   file = LZOpenFile(filename_, &test, OF_CREATE);
91   ok(file >= 0, "LZOpenFile failed on creation\n");
92   LZClose(file);
93   retval = GetFileAttributes(filename_);
94   ok2(retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributes: error %ld\n", 
95      GetLastError());
96
97   /* Check various opening options. */
98   file = LZOpenFile(filename_, &test, OF_READ);
99   ok(file >= 0, "LZOpenFile failed on read\n");
100   LZClose(file);
101   file = LZOpenFile(filename_, &test, OF_WRITE);
102   ok(file >= 0, "LZOpenFile failed on write\n");
103   LZClose(file);
104   file = LZOpenFile(filename_, &test, OF_READWRITE);
105   ok(file >= 0, "LZOpenFile failed on read/write\n");
106   LZClose(file);
107   file = LZOpenFile(filename_, &test, OF_EXIST);
108   ok(file >= 0, "LZOpenFile failed on read/write\n");
109   LZClose(file);
110
111
112   /* If the file "foo.xxx" does not exist, LZOpenFile should then
113      check for the file "foo.xx_" and open that -- at least on some
114      operating systems.  Doesn't seem to on my copy of Win98.   
115      The Wine testing guidelines say we should accept the behavior of
116      any valid version of Windows.  Thus it seems we cannot check this?!
117      Revisit this at some point to see if this can be tested somehow.
118    */
119 #if 0
120   file = LZOpenFile(filename, &test, OF_EXIST);
121   ok(file != LZERROR_BADINHANDLE, 
122      "LZOpenFile \"filename_\" check failed\n");
123   LZClose(file);
124 #endif
125
126   /* Delete the file then make sure it doesn't exist anymore. */
127   file = LZOpenFile(filename_, &test, OF_DELETE);
128   ok(file >= 0, "LZOpenFile failed on delete\n");
129   LZClose(file);
130
131   retval = GetFileAttributes(filename_);
132   ok(retval == INVALID_FILE_ATTRIBUTES, 
133      "GetFileAttributes succeeded on deleted file\n");
134   
135 }
136
137 static void test_lzread(void)
138 {
139   HANDLE file;
140   DWORD ret;
141   int cfile;
142   OFSTRUCT test;
143   BOOL retok;
144
145   /* Create the compressed file. */
146   file = CreateFile(filename_, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 0);
147   ok(file != INVALID_HANDLE_VALUE, "Could not create test file\n");
148   retok = WriteFile(file, compressed_file, compressed_file_size, &ret, 0);
149   ok2( retok, "WriteFile: error %ld\n", GetLastError());
150   ok(ret == compressed_file_size, "Wrote wrong number of bytes with WriteFile?\n");
151   CloseHandle(file);
152
153   cfile = LZOpenFile(filename_, &test, OF_READ);
154   ok(cfile > 0, "LZOpenFile failed\n");
155
156   ret = LZRead(cfile, buf, uncompressed_data_size);
157   ok(ret == uncompressed_data_size, "Read wrong number of bytes\n");
158
159   /* Compare what we read with what we think we should read. */
160   ok(memcmp(buf, uncompressed_data, uncompressed_data_size) == 0,
161      "buffer contents mismatch\n");
162
163   todo_wine {
164      /* Wine returns the number of bytes actually read instead of an error */
165      ret = LZRead(cfile, buf, uncompressed_data_size);
166      ok(ret == LZERROR_READ, "Expected read-past-EOF to return LZERROR_READ\n");
167   }
168
169   LZClose(cfile);
170
171   ret = DeleteFile(filename_);
172   ok2(ret, "DeleteFile: error %ld\n", GetLastError());
173 }
174
175 static void test_lzcopy(void)
176 {
177   HANDLE file;
178   DWORD ret;
179   int source, dest;
180   OFSTRUCT stest, dtest;
181   BOOL retok;
182
183   /* Create the compressed file. */
184   file = CreateFile(filename_, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 0);
185   ok2(file != INVALID_HANDLE_VALUE, 
186      "CreateFile: error %ld\n", GetLastError());
187   retok = WriteFile(file, compressed_file, compressed_file_size, &ret, 0);
188   ok2( retok, "WriteFile error %ld\n", GetLastError());
189   ok(ret == compressed_file_size, "Wrote wrong number of bytes\n");
190   CloseHandle(file);
191
192   source = LZOpenFile(filename_, &stest, OF_READ);
193   ok(source >= 0, "LZOpenFile failed on compressed file\n");
194   dest = LZOpenFile(filename2, &dtest, OF_CREATE);
195   ok2(dest >= 0, "LZOpenFile failed on creating new file %d\n", dest);
196
197   ret = LZCopy(source, dest);
198   ok(ret > 0, "LZCopy error\n");
199
200   LZClose(source);
201   LZClose(dest);
202
203   file = CreateFile(filename2, GENERIC_READ, 0, NULL, OPEN_EXISTING,
204                     0, 0);
205   ok2(file != INVALID_HANDLE_VALUE,
206      "CreateFile: error %ld\n", GetLastError());
207
208   retok = ReadFile(file, buf, uncompressed_data_size*2, &ret, 0);
209   ok2( retok && ret == uncompressed_data_size, "ReadFile: error %ld\n", GetLastError());
210   /* Compare what we read with what we think we should read. */
211   ok(!memcmp(buf, uncompressed_data, uncompressed_data_size),
212      "buffer contents mismatch\n");
213   CloseHandle(file);
214
215   ret = DeleteFile(filename_);
216   ok2(ret, "DeleteFile: error %ld\n", GetLastError());
217   ret = DeleteFile(filename2);
218   ok2(ret, "DeleteFile: error %ld\n", GetLastError());
219 }
220
221 START_TEST(lzexpand_main)
222 {
223   buf = malloc(uncompressed_data_size * 2);
224   test_lzopenfile();
225   test_lzread();
226   test_lzcopy();
227   free(buf);
228 }