Removed W->A from DEFWND_ImmIsUIMessageW.
[wine] / dlls / msvcrt / tests / file.c
1 /*
2  * Unit test suite for file functions
3  *
4  * Copyright 2002 Bill Currie
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 "wine/test.h"
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include <io.h>
28 #include <windef.h>
29 #include <winbase.h>
30 #include <winnls.h>
31 #include <process.h>
32 #include <errno.h>
33
34 static void test_fdopen( void )
35 {
36     static const char buffer[] = {0,1,2,3,4,5,6,7,8,9};
37     char ibuf[10];
38     int fd;
39     FILE *file;
40
41     fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
42     write (fd, buffer, sizeof (buffer));
43     close (fd);
44
45     fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
46     lseek (fd, 5, SEEK_SET);
47     file = fdopen (fd, "rb");
48     ok (fread (ibuf, 1, sizeof (buffer), file) == 5, "read wrong byte count\n");
49     ok (memcmp (ibuf, buffer + 5, 5) == 0, "read wrong bytes\n");
50     fclose (file);
51     unlink ("fdopen.tst");
52 }
53
54 static void test_fileops( void )
55 {
56     static const char outbuffer[] = "0,1,2,3,4,5,6,7,8,9";
57     char buffer[256];
58     WCHAR wbuffer[256];
59     int fd;
60     FILE *file;
61
62     fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
63     write (fd, outbuffer, sizeof (outbuffer));
64     close (fd);
65
66     fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
67     file = fdopen (fd, "rb");
68     ok(strlen(outbuffer) == (sizeof(outbuffer)-1),"strlen/sizeof error\n");
69     ok(fgets(buffer,sizeof(buffer),file) !=0,"fgets failed unexpected\n");
70     ok(fgets(buffer,sizeof(buffer),file) ==0,"fgets didn't signal EOF\n");
71     ok(feof(file) !=0,"feof doesn't signal EOF\n");
72     rewind(file);
73     ok(fgets(buffer,strlen(outbuffer),file) !=0,"fgets failed unexpected\n");
74     ok(lstrlenA(buffer) == strlen(outbuffer) -1,"fgets didn't read right size\n");
75     ok(fgets(buffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
76     ok(strlen(buffer) == 1,"fgets dropped chars\n");
77     ok(buffer[0] == outbuffer[strlen(outbuffer)-1],"fgets exchanged chars\n");
78     fclose (file);
79     fd = open ("fdopen.tst", O_RDONLY | O_TEXT);
80     file = fdopen (fd, "rt"); /* open in TEXT mode */
81     ok(fgetws(wbuffer,sizeof(wbuffer),file) !=0,"fgetws failed unexpected\n");
82     ok(fgetws(wbuffer,sizeof(wbuffer),file) ==0,"fgetws didn't signal EOF\n");
83     ok(feof(file) !=0,"feof doesn't signal EOF\n");
84     rewind(file);
85     ok(fgetws(wbuffer,strlen(outbuffer),file) !=0,"fgetws failed unexpected\n");
86     ok(lstrlenW(wbuffer) == (strlen(outbuffer) -1),"fgetws didn't read right size\n");
87     ok(fgetws(wbuffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
88     ok(lstrlenW(wbuffer) == 1,"fgets dropped chars\n");
89     fclose (file);
90     unlink ("fdopen.tst");
91 }
92
93 static WCHAR* AtoW( char* p )
94 {
95     WCHAR* buffer;
96     DWORD len = MultiByteToWideChar( CP_ACP, 0, p, -1, NULL, 0 );
97     buffer = malloc( len * sizeof(WCHAR) );
98     MultiByteToWideChar( CP_ACP, 0, p, -1, buffer, len );
99     return buffer;
100 }
101
102 static void test_fgetwc( void )
103 {
104 #define LLEN 512
105
106   char* tempf;
107   FILE *tempfh;
108   static const char mytext[]= "This is test_fgetwc\n";
109   WCHAR wtextW[LLEN+1];
110   WCHAR *mytextW = NULL, *aptr, *wptr;
111   BOOL diff_found = FALSE;
112   unsigned int i;
113
114   tempf=_tempnam(".","wne");
115   tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
116   fputs(mytext,tempfh);
117   fclose(tempfh);
118   tempfh = fopen(tempf,"rt");
119   fgetws(wtextW,LLEN,tempfh);
120   mytextW = AtoW ((char*)mytext);
121   aptr = mytextW;
122   wptr = wtextW;
123
124   for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
125     {
126       diff_found |= (*aptr != *wptr);
127     }
128   ok(!(diff_found), "fgetwc difference found in TEXT mode\n");
129   if(mytextW) free (mytextW);
130   fclose(tempfh);
131   unlink(tempf);
132 }
133
134 static void test_file_put_get( void )
135 {
136   char* tempf;
137   FILE *tempfh;
138   static const char mytext[]=  "This is a test_file_put_get\n";
139   static const char dostext[]= "This is a test_file_put_get\r\n";
140   char btext[LLEN];
141   WCHAR wtextW[LLEN+1];
142   WCHAR *mytextW = NULL, *aptr, *wptr;
143   BOOL diff_found = FALSE;
144   unsigned int i;
145
146   tempf=_tempnam(".","wne");
147   tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
148   fputs(mytext,tempfh);
149   fclose(tempfh);
150   tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
151   fgets(btext,LLEN,tempfh);
152   ok( strlen(mytext) + 1 == strlen(btext),"TEXT/BINARY mode not handled for write\n");
153   ok( btext[strlen(mytext)-1] == '\r', "CR not written\n");
154   fclose(tempfh);
155   tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
156   fputs(dostext,tempfh);
157   fclose(tempfh);
158   tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
159   fgets(btext,LLEN,tempfh);
160   ok(strcmp(btext, mytext) == 0,"_O_TEXT read doesn't strip CR\n");
161   fclose(tempfh);
162   tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
163   fgets(btext,LLEN,tempfh);
164   ok(strcmp(btext, dostext) == 0,"_O_BINARY read doesn't preserve CR\n");
165
166   fclose(tempfh);
167   tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
168   fgetws(wtextW,LLEN,tempfh);
169   mytextW = AtoW ((char*)mytext);
170   aptr = mytextW;
171   wptr = wtextW;
172
173   for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
174     {
175       diff_found |= (*aptr != *wptr);
176     }
177   ok(!(diff_found), "fgetwc doesn't strip CR in TEXT mode\n");
178   if(mytextW) free (mytextW);
179   fclose(tempfh);
180   unlink(tempf);
181 }
182
183 static void test_file_write_read( void )
184 {
185   char* tempf;
186   int tempfd;
187   static const char mytext[]=  "This is test_file_write_read\nsecond line\n";
188   static const char dostext[]= "This is test_file_write_read\r\nsecond line\r\n";
189   char btext[LLEN];
190
191   tempf=_tempnam(".","wne");
192   ok((tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,
193                      _S_IREAD | _S_IWRITE)) != -1,
194      "Can't open '%s': %d\n", tempf, errno); /* open in TEXT mode */
195   ok(_write(tempfd,mytext,strlen(mytext)) == lstrlenA(mytext),
196      "_write _O_TEXT bad return value\n");
197   _close(tempfd);
198   tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
199   ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
200      "_read _O_BINARY got bad length\n");
201   ok( memcmp(dostext,btext,strlen(dostext)) == 0,
202       "problems with _O_TEXT _write / _O_BINARY _read\n");
203   ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
204   _close(tempfd);
205   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
206   ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
207      "_read _O_TEXT got bad length\n");
208   ok( memcmp(mytext,btext,strlen(mytext)) == 0,
209       "problems with _O_TEXT _write / _read\n");
210   _close(tempfd);
211   ok(unlink(tempf) !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
212
213   tempf=_tempnam(".","wne");
214   ok((tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_BINARY|_O_RDWR,0)) != -1,
215      "Can't open '%s': %d\n", tempf, errno); /* open in BINARY mode */
216   ok(_write(tempfd,dostext,strlen(dostext)) == lstrlenA(dostext),
217      "_write _O_BINARY bad return value\n");
218   _close(tempfd);
219   tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
220   ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
221      "_read _O_BINARY got bad length\n");
222   ok( memcmp(dostext,btext,strlen(dostext)) == 0,
223       "problems with _O_BINARY _write / _read\n");
224   ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
225   _close(tempfd);
226   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
227   ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
228      "_read _O_TEXT got bad length\n");
229   ok( memcmp(mytext,btext,strlen(mytext)) == 0,
230       "problems with _O_BINARY _write / _O_TEXT _read\n");
231   _close(tempfd);
232
233   ok(_chmod (tempf, _S_IREAD | _S_IWRITE) == 0,
234      "Can't chmod '%s' to read-write: %d\n", tempf, errno);
235   ok(unlink(tempf) !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
236 }
237
238 static void test_file_inherit_child(const char* fd_s)
239 {
240     int fd = atoi(fd_s);
241     char buffer[32];
242
243     ok(write(fd, "Success", 8) == 8, "Couldn't write in child process on %d (%s)\n", fd, strerror(errno));
244     lseek(fd, 0, SEEK_SET);
245     ok(read(fd, buffer, sizeof (buffer)) == 8, "Couldn't read back the data\n");
246     ok(memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
247 }
248
249 static void test_file_inherit_child_no(const char* fd_s)
250 {
251     int fd = atoi(fd_s);
252
253     ok(write(fd, "Success", 8) == -1 && errno == EBADF, 
254        "Wrong write result in child process on %d (%s)\n", fd, strerror(errno));
255 }
256  
257 static void test_file_inherit( const char* selfname )
258 {
259     int                 fd;
260     const char*         arg_v[5];
261     char                buffer[16];
262
263     fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY, _S_IREAD |_S_IWRITE);
264     ok(fd != -1, "Couldn't create test file\n ");
265     arg_v[0] = selfname;
266     arg_v[1] = "tests/file.c";
267     arg_v[2] = buffer; sprintf(buffer, "%d", fd);
268     arg_v[3] = 0;
269     _spawnvp(_P_WAIT, selfname, arg_v);
270     ok(tell(fd) == 8, "bad position %lu expecting 8\n", tell(fd));
271     lseek(fd, 0, SEEK_SET);
272     ok(read(fd, buffer, sizeof (buffer)) == 8 && memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
273     close (fd);
274     ok(unlink("fdopen.tst") != 1, "Couldn't unlink\n");
275     
276     fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY | O_NOINHERIT, _S_IREAD |_S_IWRITE);
277     ok(fd != -1, "Couldn't create test file\n ");
278     arg_v[0] = selfname;
279     arg_v[1] = "tests/file.c";
280     arg_v[2] = buffer; sprintf(buffer, "%d", fd);
281     arg_v[3] = buffer;
282     arg_v[4] = 0;
283     _spawnvp(_P_WAIT, selfname, arg_v);
284     ok(tell(fd) == 0, "bad position %lu expecting 0\n", tell(fd));
285     ok(read(fd, buffer, sizeof (buffer)) == 0, "Found unexpected data (%s)\n", buffer);
286     close (fd);
287     ok(unlink("fdopen.tst") != 1, "Couldn't unlink\n");
288 }
289
290 static void test_tmpnam( void )
291 {
292   char name[MAX_PATH] = "abc";
293   char *res;
294
295   res = tmpnam(NULL);
296   ok(res != NULL, "tmpnam returned NULL\n");
297   ok(res[0] == '\\', "first character is not a backslash\n");
298   ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
299   ok(res[strlen(res)-1] == '.', "first call - last character is not a dot\n");
300
301   res = tmpnam(name);
302   ok(res != NULL, "tmpnam returned NULL\n");
303   ok(res == name, "supplied buffer was not used\n");
304   ok(res[0] == '\\', "first character is not a backslash\n");
305   ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
306   ok(res[strlen(res)-1] != '.', "second call - last character is a dot\n");
307 }
308
309
310
311 START_TEST(file)
312 {
313     int arg_c;
314     char** arg_v;
315
316     arg_c = winetest_get_mainargs( &arg_v );
317
318     if (arg_c >= 3)
319     {
320         if (arg_c == 3) test_file_inherit_child(arg_v[2]); else test_file_inherit_child_no(arg_v[2]);
321         return;
322     }
323
324     test_fdopen();
325     test_fileops();
326     test_fgetwc();
327     test_file_put_get();
328     test_file_write_read();
329     test_file_inherit(arg_v[0]);
330     test_tmpnam();
331 }