Remove redundant check.
[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   ret = unlink(tempf);
246   ok( ret !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
247
248   tempf=_tempnam(".","wne");
249   tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_BINARY|_O_RDWR,0);
250   ok( tempfd != -1,
251      "Can't open '%s': %d\n", tempf, errno); /* open in BINARY mode */
252   ok(_write(tempfd,dostext,strlen(dostext)) == lstrlenA(dostext),
253      "_write _O_BINARY bad return value\n");
254   _close(tempfd);
255   tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
256   ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
257      "_read _O_BINARY got bad length\n");
258   ok( memcmp(dostext,btext,strlen(dostext)) == 0,
259       "problems with _O_BINARY _write / _read\n");
260   ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
261   _close(tempfd);
262   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
263   ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
264      "_read _O_TEXT got bad length\n");
265   ok( memcmp(mytext,btext,strlen(mytext)) == 0,
266       "problems with _O_BINARY _write / _O_TEXT _read\n");
267   _close(tempfd);
268
269    ret =_chmod (tempf, _S_IREAD | _S_IWRITE);
270   ok( ret == 0,
271      "Can't chmod '%s' to read-write: %d\n", tempf, errno);
272   ret = unlink(tempf);
273   ok( ret !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
274 }
275
276 static void test_file_inherit_child(const char* fd_s)
277 {
278     int fd = atoi(fd_s);
279     char buffer[32];
280     int ret;
281
282     ret =write(fd, "Success", 8);
283     ok( ret == 8, "Couldn't write in child process on %d (%s)\n", fd, strerror(errno));
284     lseek(fd, 0, SEEK_SET);
285     ok(read(fd, buffer, sizeof (buffer)) == 8, "Couldn't read back the data\n");
286     ok(memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
287 }
288
289 static void test_file_inherit_child_no(const char* fd_s)
290 {
291     int fd = atoi(fd_s);
292     int ret;
293
294     ret = write(fd, "Success", 8);
295     ok( ret == -1 && errno == EBADF, 
296        "Wrong write result in child process on %d (%s)\n", fd, strerror(errno));
297 }
298  
299 static void test_file_inherit( const char* selfname )
300 {
301     int                 fd;
302     const char*         arg_v[5];
303     char                buffer[16];
304
305     fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY, _S_IREAD |_S_IWRITE);
306     ok(fd != -1, "Couldn't create test file\n");
307     arg_v[0] = selfname;
308     arg_v[1] = "tests/file.c";
309     arg_v[2] = buffer; sprintf(buffer, "%d", fd);
310     arg_v[3] = 0;
311     _spawnvp(_P_WAIT, selfname, arg_v);
312     ok(tell(fd) == 8, "bad position %lu expecting 8\n", tell(fd));
313     lseek(fd, 0, SEEK_SET);
314     ok(read(fd, buffer, sizeof (buffer)) == 8 && memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
315     close (fd);
316     ok(unlink("fdopen.tst") != 1, "Couldn't unlink\n");
317     
318     fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY | O_NOINHERIT, _S_IREAD |_S_IWRITE);
319     ok(fd != -1, "Couldn't create test file\n");
320     arg_v[0] = selfname;
321     arg_v[1] = "tests/file.c";
322     arg_v[2] = buffer; sprintf(buffer, "%d", fd);
323     arg_v[3] = buffer;
324     arg_v[4] = 0;
325     _spawnvp(_P_WAIT, selfname, arg_v);
326     ok(tell(fd) == 0, "bad position %lu expecting 0\n", tell(fd));
327     ok(read(fd, buffer, sizeof (buffer)) == 0, "Found unexpected data (%s)\n", buffer);
328     close (fd);
329     ok(unlink("fdopen.tst") != 1, "Couldn't unlink\n");
330 }
331
332 static void test_tmpnam( void )
333 {
334   char name[MAX_PATH] = "abc";
335   char *res;
336
337   res = tmpnam(NULL);
338   ok(res != NULL, "tmpnam returned NULL\n");
339   ok(res[0] == '\\', "first character is not a backslash\n");
340   ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
341   ok(res[strlen(res)-1] == '.', "first call - last character is not a dot\n");
342
343   res = tmpnam(name);
344   ok(res != NULL, "tmpnam returned NULL\n");
345   ok(res == name, "supplied buffer was not used\n");
346   ok(res[0] == '\\', "first character is not a backslash\n");
347   ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
348   ok(res[strlen(res)-1] != '.', "second call - last character is a dot\n");
349 }
350
351 static void test_chsize( void )
352 {
353     int fd;
354     long cur, pos, count;
355     char temptext[] = "012345678";
356     char *tempfile = _tempnam( ".", "tst" );
357     
358     ok( tempfile != NULL, "Couldn't create test file: %s\n", tempfile );
359
360     fd = _open( tempfile, _O_CREAT|_O_TRUNC|_O_RDWR, _S_IREAD|_S_IWRITE );
361     ok( fd > 0, "Couldn't open test file\n" );
362
363     count = _write( fd, temptext, sizeof(temptext) );
364     ok( count > 0, "Couldn't write to test file\n" );
365
366     /* get current file pointer */
367     cur = _lseek( fd, 0, SEEK_CUR );
368
369     /* make the file smaller */
370     ok( _chsize( fd, sizeof(temptext) / 2 ) == 0, "_chsize() failed\n" );
371
372     pos = _lseek( fd, 0, SEEK_CUR );
373     ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
374     ok( _filelength( fd ) == sizeof(temptext) / 2, "Wrong file size\n" );
375
376     /* enlarge the file */
377     ok( _chsize( fd, sizeof(temptext) * 2 ) == 0, "_chsize() failed\n" ); 
378
379     pos = _lseek( fd, 0, SEEK_CUR );
380     ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
381     ok( _filelength( fd ) == sizeof(temptext) * 2, "Wrong file size\n" );
382
383     _close( fd );
384     _unlink( tempfile );
385 }
386
387 START_TEST(file)
388 {
389     int arg_c;
390     char** arg_v;
391
392     arg_c = winetest_get_mainargs( &arg_v );
393
394     if (arg_c >= 3)
395     {
396         if (arg_c == 3) test_file_inherit_child(arg_v[2]); else test_file_inherit_child_no(arg_v[2]);
397         return;
398     }
399
400     test_fdopen();
401     test_fileops();
402     test_fgetwc();
403     test_file_put_get();
404     test_file_write_read();
405     test_file_inherit(arg_v[0]);
406     test_tmpnam();
407     test_chsize();
408 }