kernel32: FindFirstChangeNotification needs a static IO_STATUS_BLOCK.
[wine] / dlls / msvcrt / tests / file.c
1 /*
2  * Unit test suite for file functions
3  *
4  * Copyright 2002 Bill Currie
5  * Copyright 2005 Paul Rupe
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "wine/test.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <io.h>
29 #include <windef.h>
30 #include <winbase.h>
31 #include <winnls.h>
32 #include <process.h>
33 #include <errno.h>
34
35 static void test_fdopen( void )
36 {
37     static const char buffer[] = {0,1,2,3,4,5,6,7,8,9};
38     char ibuf[10];
39     int fd;
40     FILE *file;
41
42     fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
43     write (fd, buffer, sizeof (buffer));
44     close (fd);
45
46     fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
47     lseek (fd, 5, SEEK_SET);
48     file = fdopen (fd, "rb");
49     ok (fread (ibuf, 1, sizeof (buffer), file) == 5, "read wrong byte count\n");
50     ok (memcmp (ibuf, buffer + 5, 5) == 0, "read wrong bytes\n");
51     fclose (file);
52     unlink ("fdopen.tst");
53 }
54
55 static void test_fileops( void )
56 {
57     static const char outbuffer[] = "0,1,2,3,4,5,6,7,8,9";
58     char buffer[256];
59     WCHAR wbuffer[256];
60     int fd;
61     FILE *file;
62     fpos_t pos;
63     int i, c;
64
65     fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
66     write (fd, outbuffer, sizeof (outbuffer));
67     close (fd);
68
69     fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
70     file = fdopen (fd, "rb");
71     ok(strlen(outbuffer) == (sizeof(outbuffer)-1),"strlen/sizeof error\n");
72     ok(fgets(buffer,sizeof(buffer),file) !=0,"fgets failed unexpected\n");
73     ok(fgets(buffer,sizeof(buffer),file) ==0,"fgets didn't signal EOF\n");
74     ok(feof(file) !=0,"feof doesn't signal EOF\n");
75     rewind(file);
76     ok(fgets(buffer,strlen(outbuffer),file) !=0,"fgets failed unexpected\n");
77     ok(lstrlenA(buffer) == lstrlenA(outbuffer) -1,"fgets didn't read right size\n");
78     ok(fgets(buffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
79     ok(strlen(buffer) == 1,"fgets dropped chars\n");
80     ok(buffer[0] == outbuffer[strlen(outbuffer)-1],"fgets exchanged chars\n");
81
82     rewind(file);
83     for (i = 0, c = EOF; i < sizeof(outbuffer); i++)
84     {
85         ok((c = fgetc(file)) == outbuffer[i], "fgetc returned wrong data\n");
86     }
87     ok((c = fgetc(file)) == EOF, "getc did not return EOF\n");
88     ok(feof(file), "feof did not return EOF\n");
89     ok(ungetc(c, file) == EOF, "ungetc(EOF) did not return EOF\n");
90     ok(feof(file), "feof after ungetc(EOF) did not return EOF\n");
91     ok((c = fgetc(file)) == EOF, "getc did not return EOF\n");
92     c = outbuffer[sizeof(outbuffer) - 1];
93     ok(ungetc(c, file) == c, "ungetc did not return its input\n");
94     ok(!feof(file), "feof after ungetc returned EOF\n");
95     ok((c = fgetc(file)) != EOF, "getc after ungetc returned EOF\n");
96     ok(c == outbuffer[sizeof(outbuffer) - 1],
97        "getc did not return ungetc'd data\n");
98     ok(!feof(file), "feof after getc returned EOF prematurely\n");
99     ok((c = fgetc(file)) == EOF, "getc did not return EOF\n");
100     ok(feof(file), "feof after getc did not return EOF\n");
101
102     rewind(file);
103     ok(fgetpos(file,&pos) == 0, "fgetpos failed unexpected\n");
104     ok(pos == 0, "Unexpected result of fgetpos 0x%Lx\n", pos);
105     pos = (ULONGLONG)sizeof (outbuffer);
106     ok(fsetpos(file, &pos) == 0, "fsetpos failed unexpected\n");
107     ok(fgetpos(file,&pos) == 0, "fgetpos failed unexpected\n");
108     ok(pos == (ULONGLONG)sizeof (outbuffer), "Unexpected result of fgetpos 0x%Lx\n", pos);
109
110     fclose (file);
111     fd = open ("fdopen.tst", O_RDONLY | O_TEXT);
112     file = fdopen (fd, "rt"); /* open in TEXT mode */
113     ok(fgetws(wbuffer,sizeof(wbuffer),file) !=0,"fgetws failed unexpected\n");
114     ok(fgetws(wbuffer,sizeof(wbuffer),file) ==0,"fgetws didn't signal EOF\n");
115     ok(feof(file) !=0,"feof doesn't signal EOF\n");
116     rewind(file);
117     ok(fgetws(wbuffer,strlen(outbuffer),file) !=0,"fgetws failed unexpected\n");
118     ok(lstrlenW(wbuffer) == (lstrlenA(outbuffer) -1),"fgetws didn't read right size\n");
119     ok(fgetws(wbuffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
120     ok(lstrlenW(wbuffer) == 1,"fgets dropped chars\n");
121     fclose (file);
122     unlink ("fdopen.tst");
123 }
124
125 static WCHAR* AtoW( char* p )
126 {
127     WCHAR* buffer;
128     DWORD len = MultiByteToWideChar( CP_ACP, 0, p, -1, NULL, 0 );
129     buffer = malloc( len * sizeof(WCHAR) );
130     MultiByteToWideChar( CP_ACP, 0, p, -1, buffer, len );
131     return buffer;
132 }
133
134 static void test_fgetwc( void )
135 {
136 #define LLEN 512
137
138   char* tempf;
139   FILE *tempfh;
140   static const char mytext[]= "This is test_fgetwc\n";
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,"rt");
151   fgetws(wtextW,LLEN,tempfh);
152   mytextW = AtoW ((char*)mytext);
153   aptr = mytextW;
154   wptr = wtextW;
155
156   for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
157     {
158       diff_found |= (*aptr != *wptr);
159     }
160   ok(!(diff_found), "fgetwc difference found in TEXT mode\n");
161   if(mytextW) free (mytextW);
162   fclose(tempfh);
163   unlink(tempf);
164 }
165
166 static void test_file_put_get( void )
167 {
168   char* tempf;
169   FILE *tempfh;
170   static const char mytext[]=  "This is a test_file_put_get\n";
171   static const char dostext[]= "This is a test_file_put_get\r\n";
172   char btext[LLEN];
173   WCHAR wtextW[LLEN+1];
174   WCHAR *mytextW = NULL, *aptr, *wptr;
175   BOOL diff_found = FALSE;
176   unsigned int i;
177
178   tempf=_tempnam(".","wne");
179   tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
180   fputs(mytext,tempfh);
181   fclose(tempfh);
182   tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
183   fgets(btext,LLEN,tempfh);
184   ok( strlen(mytext) + 1 == strlen(btext),"TEXT/BINARY mode not handled for write\n");
185   ok( btext[strlen(mytext)-1] == '\r', "CR not written\n");
186   fclose(tempfh);
187   tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
188   fputs(dostext,tempfh);
189   fclose(tempfh);
190   tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
191   fgets(btext,LLEN,tempfh);
192   ok(strcmp(btext, mytext) == 0,"_O_TEXT read doesn't strip CR\n");
193   fclose(tempfh);
194   tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
195   fgets(btext,LLEN,tempfh);
196   ok(strcmp(btext, dostext) == 0,"_O_BINARY read doesn't preserve CR\n");
197
198   fclose(tempfh);
199   tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
200   fgetws(wtextW,LLEN,tempfh);
201   mytextW = AtoW ((char*)mytext);
202   aptr = mytextW;
203   wptr = wtextW;
204
205   for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
206     {
207       diff_found |= (*aptr != *wptr);
208     }
209   ok(!(diff_found), "fgetwc doesn't strip CR in TEXT mode\n");
210   if(mytextW) free (mytextW);
211   fclose(tempfh);
212   unlink(tempf);
213 }
214
215 static void test_file_write_read( void )
216 {
217   char* tempf;
218   int tempfd;
219   static const char mytext[]=  "This is test_file_write_read\nsecond line\n";
220   static const char dostext[]= "This is test_file_write_read\r\nsecond line\r\n";
221   char btext[LLEN];
222   int ret;
223
224   tempf=_tempnam(".","wne");
225   tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,
226                      _S_IREAD | _S_IWRITE);
227   ok( tempfd != -1,
228      "Can't open '%s': %d\n", tempf, errno); /* open in TEXT mode */
229   ok(_write(tempfd,mytext,strlen(mytext)) == lstrlenA(mytext),
230      "_write _O_TEXT bad return value\n");
231   _close(tempfd);
232   tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
233   ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
234      "_read _O_BINARY got bad length\n");
235   ok( memcmp(dostext,btext,strlen(dostext)) == 0,
236       "problems with _O_TEXT _write / _O_BINARY _read\n");
237   ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
238   _close(tempfd);
239   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
240   ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
241      "_read _O_TEXT got bad length\n");
242   ok( memcmp(mytext,btext,strlen(mytext)) == 0,
243       "problems with _O_TEXT _write / _read\n");
244   _close(tempfd);
245
246   memset(btext, 0, LLEN);
247   tempfd = _open(tempf,_O_APPEND|_O_RDWR); /* open for APPEND in default mode */
248   ok(tell(tempfd) == 0, "bad position %lu expecting 0\n", tell(tempfd));
249   ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext), "_read _O_APPEND got bad length\n");
250   ok( memcmp(mytext,btext,strlen(mytext)) == 0, "problems with _O_APPEND _read\n");
251   _close(tempfd);
252
253   /* Test reading only \n or \r */
254   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
255   _lseek(tempfd, -1, FILE_END);
256   ret = _read(tempfd,btext,LLEN);
257   ok(ret == 1, "_read expected 1 got bad length: %d\n", ret);
258   _lseek(tempfd, -2, FILE_END);
259   ret = _read(tempfd,btext,LLEN);
260   ok(ret == 1 && *btext == '\n', "_read expected '\\n' got bad length: %d\n", ret);
261   _lseek(tempfd, -3, FILE_END);
262   ret = _read(tempfd,btext,2);
263   todo_wine ok(ret == 1 && *btext == 'e', "_read expected 'e' got \"%.*s\" bad length: %d\n", ret, btext, ret);
264   todo_wine ok(tell(tempfd) == 42, "bad position %lu expecting 42\n", tell(tempfd));
265   _close(tempfd);
266
267   ret = unlink(tempf);
268   ok( ret == 0 ,"Can't unlink '%s': %d\n", tempf, errno);
269
270   tempf=_tempnam(".","wne");
271   tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_BINARY|_O_RDWR,0);
272   ok( tempfd != -1,
273      "Can't open '%s': %d\n", tempf, errno); /* open in BINARY mode */
274   ok(_write(tempfd,dostext,strlen(dostext)) == lstrlenA(dostext),
275      "_write _O_BINARY bad return value\n");
276   _close(tempfd);
277   tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
278   ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
279      "_read _O_BINARY got bad length\n");
280   ok( memcmp(dostext,btext,strlen(dostext)) == 0,
281       "problems with _O_BINARY _write / _read\n");
282   ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
283   _close(tempfd);
284   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
285   ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
286      "_read _O_TEXT got bad length\n");
287   ok( memcmp(mytext,btext,strlen(mytext)) == 0,
288       "problems with _O_BINARY _write / _O_TEXT _read\n");
289   _close(tempfd);
290
291    ret =_chmod (tempf, _S_IREAD | _S_IWRITE);
292   ok( ret == 0,
293      "Can't chmod '%s' to read-write: %d\n", tempf, errno);
294   ret = unlink(tempf);
295   ok( ret == 0 ,"Can't unlink '%s': %d\n", tempf, errno);
296 }
297
298 static void test_file_inherit_child(const char* fd_s)
299 {
300     int fd = atoi(fd_s);
301     char buffer[32];
302     int ret;
303
304     ret =write(fd, "Success", 8);
305     ok( ret == 8, "Couldn't write in child process on %d (%s)\n", fd, strerror(errno));
306     lseek(fd, 0, SEEK_SET);
307     ok(read(fd, buffer, sizeof (buffer)) == 8, "Couldn't read back the data\n");
308     ok(memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
309 }
310
311 static void test_file_inherit_child_no(const char* fd_s)
312 {
313     int fd = atoi(fd_s);
314     int ret;
315
316     ret = write(fd, "Success", 8);
317     ok( ret == -1 && errno == EBADF, 
318        "Wrong write result in child process on %d (%s)\n", fd, strerror(errno));
319 }
320  
321 static void test_file_inherit( const char* selfname )
322 {
323     int                 fd;
324     const char*         arg_v[5];
325     char                buffer[16];
326
327     fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY, _S_IREAD |_S_IWRITE);
328     ok(fd != -1, "Couldn't create test file\n");
329     arg_v[0] = selfname;
330     arg_v[1] = "tests/file.c";
331     arg_v[2] = buffer; sprintf(buffer, "%d", fd);
332     arg_v[3] = 0;
333     _spawnvp(_P_WAIT, selfname, arg_v);
334     ok(tell(fd) == 8, "bad position %lu expecting 8\n", tell(fd));
335     lseek(fd, 0, SEEK_SET);
336     ok(read(fd, buffer, sizeof (buffer)) == 8 && memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
337     close (fd);
338     ok(unlink("fdopen.tst") == 0, "Couldn't unlink\n");
339     
340     fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY | O_NOINHERIT, _S_IREAD |_S_IWRITE);
341     ok(fd != -1, "Couldn't create test file\n");
342     arg_v[0] = selfname;
343     arg_v[1] = "tests/file.c";
344     arg_v[2] = buffer; sprintf(buffer, "%d", fd);
345     arg_v[3] = buffer;
346     arg_v[4] = 0;
347     _spawnvp(_P_WAIT, selfname, arg_v);
348     ok(tell(fd) == 0, "bad position %lu expecting 0\n", tell(fd));
349     ok(read(fd, buffer, sizeof (buffer)) == 0, "Found unexpected data (%s)\n", buffer);
350     close (fd);
351     ok(unlink("fdopen.tst") == 0, "Couldn't unlink\n");
352 }
353
354 static void test_tmpnam( void )
355 {
356   char name[MAX_PATH] = "abc";
357   char *res;
358
359   res = tmpnam(NULL);
360   ok(res != NULL, "tmpnam returned NULL\n");
361   ok(res[0] == '\\', "first character is not a backslash\n");
362   ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
363   ok(res[strlen(res)-1] == '.', "first call - last character is not a dot\n");
364
365   res = tmpnam(name);
366   ok(res != NULL, "tmpnam returned NULL\n");
367   ok(res == name, "supplied buffer was not used\n");
368   ok(res[0] == '\\', "first character is not a backslash\n");
369   ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
370   ok(res[strlen(res)-1] != '.', "second call - last character is a dot\n");
371 }
372
373 static void test_chsize( void )
374 {
375     int fd;
376     long cur, pos, count;
377     char temptext[] = "012345678";
378     char *tempfile = _tempnam( ".", "tst" );
379     
380     ok( tempfile != NULL, "Couldn't create test file: %s\n", tempfile );
381
382     fd = _open( tempfile, _O_CREAT|_O_TRUNC|_O_RDWR, _S_IREAD|_S_IWRITE );
383     ok( fd > 0, "Couldn't open test file\n" );
384
385     count = _write( fd, temptext, sizeof(temptext) );
386     ok( count > 0, "Couldn't write to test file\n" );
387
388     /* get current file pointer */
389     cur = _lseek( fd, 0, SEEK_CUR );
390
391     /* make the file smaller */
392     ok( _chsize( fd, sizeof(temptext) / 2 ) == 0, "_chsize() failed\n" );
393
394     pos = _lseek( fd, 0, SEEK_CUR );
395     ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
396     ok( _filelength( fd ) == sizeof(temptext) / 2, "Wrong file size\n" );
397
398     /* enlarge the file */
399     ok( _chsize( fd, sizeof(temptext) * 2 ) == 0, "_chsize() failed\n" ); 
400
401     pos = _lseek( fd, 0, SEEK_CUR );
402     ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
403     ok( _filelength( fd ) == sizeof(temptext) * 2, "Wrong file size\n" );
404
405     _close( fd );
406     _unlink( tempfile );
407 }
408
409 static void test_fopen_fclose_fcloseall( void )
410 {
411     char fname1[] = "empty1";
412     char fname2[] = "empty2";
413     char fname3[] = "empty3";
414     FILE *stream1, *stream2, *stream3, *stream4;
415     int ret, numclosed;
416
417     /* testing fopen() */
418     stream1 = fopen(fname1, "w+");
419     ok(stream1 != NULL, "The file '%s' was not opened\n", fname1);
420     stream2 = fopen(fname2, "w ");
421     ok(stream2 != NULL, "The file '%s' was not opened\n", fname2 );
422     _unlink(fname3);
423     stream3 = fopen(fname3, "r");
424     ok(stream3 == NULL, "The file '%s' shouldn't exist before\n", fname3 );
425     stream3 = fopen(fname3, "w+");
426     ok(stream3 != NULL, "The file '%s' should be opened now\n", fname3 );
427     errno = 0xfaceabad;
428     stream4 = fopen("", "w+");
429     ok(stream4 == NULL && errno == ENOENT, 
430        "filename is empty, errno = %d (expected 2)\n", errno);
431     errno = 0xfaceabad;
432     stream4 = fopen(NULL, "w+");
433     ok(stream4 == NULL && (errno == EINVAL || errno == ENOENT), 
434        "filename is NULL, errno = %d (expected 2 or 22)\n", errno);
435
436     /* testing fclose() */
437     ret = fclose(stream2);
438     ok(ret == 0, "The file '%s' was not closed\n", fname2);
439     ret = fclose(stream3);
440     ok(ret == 0, "The file '%s' was not closed\n", fname3);
441     ret = fclose(stream2);
442     ok(ret == EOF, "Closing file '%s' returned %d\n", fname2, ret);
443     ret = fclose(stream3);
444     ok(ret == EOF, "Closing file '%s' returned %d\n", fname3, ret);
445
446     /* testing fcloseall() */
447     numclosed = _fcloseall();
448     /* fname1 should be closed here */
449     ok(numclosed == 1, "Number of files closed by fcloseall(): %u\n", numclosed);
450     numclosed = _fcloseall();
451     ok(numclosed == 0, "Number of files closed by fcloseall(): %u\n", numclosed);
452
453     ok(_unlink(fname1) == 0, "Couldn't unlink file named '%s'\n", fname1);
454     ok(_unlink(fname2) == 0, "Couldn't unlink file named '%s'\n", fname2);
455     ok(_unlink(fname3) == 0, "Couldn't unlink file named '%s'\n", fname3);
456 }
457
458 START_TEST(file)
459 {
460     int arg_c;
461     char** arg_v;
462
463     arg_c = winetest_get_mainargs( &arg_v );
464
465     /* testing low-level I/O */
466     if (arg_c >= 3)
467     {
468         if (arg_c == 3) test_file_inherit_child(arg_v[2]); 
469         else test_file_inherit_child_no(arg_v[2]);
470         return;
471     }
472     test_file_inherit(arg_v[0]);
473     test_file_write_read();
474     test_chsize();
475
476     /* testing stream I/O */
477     test_fdopen();
478     test_fopen_fclose_fcloseall();
479     test_fileops();
480     test_fgetwc();
481     test_file_put_get();
482     test_tmpnam();
483 }