Fixes for -Wmissing-declaration and -Wwrite-string warnings.
[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     fpos_t pos;
62
63     fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
64     write (fd, outbuffer, sizeof (outbuffer));
65     close (fd);
66
67     fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
68     file = fdopen (fd, "rb");
69     ok(strlen(outbuffer) == (sizeof(outbuffer)-1),"strlen/sizeof error\n");
70     ok(fgets(buffer,sizeof(buffer),file) !=0,"fgets failed unexpected\n");
71     ok(fgets(buffer,sizeof(buffer),file) ==0,"fgets didn't signal EOF\n");
72     ok(feof(file) !=0,"feof doesn't signal EOF\n");
73     rewind(file);
74     ok(fgets(buffer,strlen(outbuffer),file) !=0,"fgets failed unexpected\n");
75     ok(lstrlenA(buffer) == lstrlenA(outbuffer) -1,"fgets didn't read right size\n");
76     ok(fgets(buffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
77     ok(strlen(buffer) == 1,"fgets dropped chars\n");
78     ok(buffer[0] == outbuffer[strlen(outbuffer)-1],"fgets exchanged chars\n");
79
80     rewind(file);
81     ok(fgetpos(file,&pos) == 0, "fgetpos failed unexpected\n");
82     ok(pos == 0, "Unexpected result of fgetpos 0x%Lx\n", pos);
83     pos = (ULONGLONG)sizeof (outbuffer);
84     ok(fsetpos(file, &pos) == 0, "fsetpos failed unexpected\n");
85     ok(fgetpos(file,&pos) == 0, "fgetpos failed unexpected\n");
86     ok(pos == (ULONGLONG)sizeof (outbuffer), "Unexpected result of fgetpos 0x%Lx\n", pos);
87
88     fclose (file);
89     fd = open ("fdopen.tst", O_RDONLY | O_TEXT);
90     file = fdopen (fd, "rt"); /* open in TEXT mode */
91     ok(fgetws(wbuffer,sizeof(wbuffer),file) !=0,"fgetws failed unexpected\n");
92     ok(fgetws(wbuffer,sizeof(wbuffer),file) ==0,"fgetws didn't signal EOF\n");
93     ok(feof(file) !=0,"feof doesn't signal EOF\n");
94     rewind(file);
95     ok(fgetws(wbuffer,strlen(outbuffer),file) !=0,"fgetws failed unexpected\n");
96     ok(lstrlenW(wbuffer) == (lstrlenA(outbuffer) -1),"fgetws didn't read right size\n");
97     ok(fgetws(wbuffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
98     ok(lstrlenW(wbuffer) == 1,"fgets dropped chars\n");
99     fclose (file);
100     unlink ("fdopen.tst");
101 }
102
103 static WCHAR* AtoW( char* p )
104 {
105     WCHAR* buffer;
106     DWORD len = MultiByteToWideChar( CP_ACP, 0, p, -1, NULL, 0 );
107     buffer = malloc( len * sizeof(WCHAR) );
108     MultiByteToWideChar( CP_ACP, 0, p, -1, buffer, len );
109     return buffer;
110 }
111
112 static void test_fgetwc( void )
113 {
114 #define LLEN 512
115
116   char* tempf;
117   FILE *tempfh;
118   static const char mytext[]= "This is test_fgetwc\n";
119   WCHAR wtextW[LLEN+1];
120   WCHAR *mytextW = NULL, *aptr, *wptr;
121   BOOL diff_found = FALSE;
122   unsigned int i;
123
124   tempf=_tempnam(".","wne");
125   tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
126   fputs(mytext,tempfh);
127   fclose(tempfh);
128   tempfh = fopen(tempf,"rt");
129   fgetws(wtextW,LLEN,tempfh);
130   mytextW = AtoW ((char*)mytext);
131   aptr = mytextW;
132   wptr = wtextW;
133
134   for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
135     {
136       diff_found |= (*aptr != *wptr);
137     }
138   ok(!(diff_found), "fgetwc difference found in TEXT mode\n");
139   if(mytextW) free (mytextW);
140   fclose(tempfh);
141   unlink(tempf);
142 }
143
144 static void test_file_put_get( void )
145 {
146   char* tempf;
147   FILE *tempfh;
148   static const char mytext[]=  "This is a test_file_put_get\n";
149   static const char dostext[]= "This is a test_file_put_get\r\n";
150   char btext[LLEN];
151   WCHAR wtextW[LLEN+1];
152   WCHAR *mytextW = NULL, *aptr, *wptr;
153   BOOL diff_found = FALSE;
154   unsigned int i;
155
156   tempf=_tempnam(".","wne");
157   tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
158   fputs(mytext,tempfh);
159   fclose(tempfh);
160   tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
161   fgets(btext,LLEN,tempfh);
162   ok( strlen(mytext) + 1 == strlen(btext),"TEXT/BINARY mode not handled for write\n");
163   ok( btext[strlen(mytext)-1] == '\r', "CR not written\n");
164   fclose(tempfh);
165   tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
166   fputs(dostext,tempfh);
167   fclose(tempfh);
168   tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
169   fgets(btext,LLEN,tempfh);
170   ok(strcmp(btext, mytext) == 0,"_O_TEXT read doesn't strip CR\n");
171   fclose(tempfh);
172   tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
173   fgets(btext,LLEN,tempfh);
174   ok(strcmp(btext, dostext) == 0,"_O_BINARY read doesn't preserve CR\n");
175
176   fclose(tempfh);
177   tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
178   fgetws(wtextW,LLEN,tempfh);
179   mytextW = AtoW ((char*)mytext);
180   aptr = mytextW;
181   wptr = wtextW;
182
183   for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
184     {
185       diff_found |= (*aptr != *wptr);
186     }
187   ok(!(diff_found), "fgetwc doesn't strip CR in TEXT mode\n");
188   if(mytextW) free (mytextW);
189   fclose(tempfh);
190   unlink(tempf);
191 }
192
193 static void test_file_write_read( void )
194 {
195   char* tempf;
196   int tempfd;
197   static const char mytext[]=  "This is test_file_write_read\nsecond line\n";
198   static const char dostext[]= "This is test_file_write_read\r\nsecond line\r\n";
199   char btext[LLEN];
200   int ret;
201
202   tempf=_tempnam(".","wne");
203   tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,
204                      _S_IREAD | _S_IWRITE);
205   ok( tempfd != -1,
206      "Can't open '%s': %d\n", tempf, errno); /* open in TEXT mode */
207   ok(_write(tempfd,mytext,strlen(mytext)) == lstrlenA(mytext),
208      "_write _O_TEXT bad return value\n");
209   _close(tempfd);
210   tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
211   ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
212      "_read _O_BINARY got bad length\n");
213   ok( memcmp(dostext,btext,strlen(dostext)) == 0,
214       "problems with _O_TEXT _write / _O_BINARY _read\n");
215   ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
216   _close(tempfd);
217   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
218   ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
219      "_read _O_TEXT got bad length\n");
220   ok( memcmp(mytext,btext,strlen(mytext)) == 0,
221       "problems with _O_TEXT _write / _read\n");
222   _close(tempfd);
223   ret = unlink(tempf);
224   ok( ret !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
225
226   tempf=_tempnam(".","wne");
227   tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_BINARY|_O_RDWR,0);
228   ok( tempfd != -1,
229      "Can't open '%s': %d\n", tempf, errno); /* open in BINARY mode */
230   ok(_write(tempfd,dostext,strlen(dostext)) == lstrlenA(dostext),
231      "_write _O_BINARY bad return value\n");
232   _close(tempfd);
233   tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
234   ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
235      "_read _O_BINARY got bad length\n");
236   ok( memcmp(dostext,btext,strlen(dostext)) == 0,
237       "problems with _O_BINARY _write / _read\n");
238   ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
239   _close(tempfd);
240   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
241   ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
242      "_read _O_TEXT got bad length\n");
243   ok( memcmp(mytext,btext,strlen(mytext)) == 0,
244       "problems with _O_BINARY _write / _O_TEXT _read\n");
245   _close(tempfd);
246
247    ret =_chmod (tempf, _S_IREAD | _S_IWRITE);
248   ok( ret == 0,
249      "Can't chmod '%s' to read-write: %d\n", tempf, errno);
250   ret = unlink(tempf);
251   ok( ret !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
252 }
253
254 static void test_file_inherit_child(const char* fd_s)
255 {
256     int fd = atoi(fd_s);
257     char buffer[32];
258     int ret;
259
260     ret =write(fd, "Success", 8);
261     ok( ret == 8, "Couldn't write in child process on %d (%s)\n", fd, strerror(errno));
262     lseek(fd, 0, SEEK_SET);
263     ok(read(fd, buffer, sizeof (buffer)) == 8, "Couldn't read back the data\n");
264     ok(memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
265 }
266
267 static void test_file_inherit_child_no(const char* fd_s)
268 {
269     int fd = atoi(fd_s);
270     int ret;
271
272     ret = write(fd, "Success", 8);
273     ok( ret == -1 && errno == EBADF, 
274        "Wrong write result in child process on %d (%s)\n", fd, strerror(errno));
275 }
276  
277 static void test_file_inherit( const char* selfname )
278 {
279     int                 fd;
280     const char*         arg_v[5];
281     char                buffer[16];
282
283     fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY, _S_IREAD |_S_IWRITE);
284     ok(fd != -1, "Couldn't create test file\n");
285     arg_v[0] = selfname;
286     arg_v[1] = "tests/file.c";
287     arg_v[2] = buffer; sprintf(buffer, "%d", fd);
288     arg_v[3] = 0;
289     _spawnvp(_P_WAIT, selfname, arg_v);
290     ok(tell(fd) == 8, "bad position %lu expecting 8\n", tell(fd));
291     lseek(fd, 0, SEEK_SET);
292     ok(read(fd, buffer, sizeof (buffer)) == 8 && memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
293     close (fd);
294     ok(unlink("fdopen.tst") != 1, "Couldn't unlink\n");
295     
296     fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY | O_NOINHERIT, _S_IREAD |_S_IWRITE);
297     ok(fd != -1, "Couldn't create test file\n");
298     arg_v[0] = selfname;
299     arg_v[1] = "tests/file.c";
300     arg_v[2] = buffer; sprintf(buffer, "%d", fd);
301     arg_v[3] = buffer;
302     arg_v[4] = 0;
303     _spawnvp(_P_WAIT, selfname, arg_v);
304     ok(tell(fd) == 0, "bad position %lu expecting 0\n", tell(fd));
305     ok(read(fd, buffer, sizeof (buffer)) == 0, "Found unexpected data (%s)\n", buffer);
306     close (fd);
307     ok(unlink("fdopen.tst") != 1, "Couldn't unlink\n");
308 }
309
310 static void test_tmpnam( void )
311 {
312   char name[MAX_PATH] = "abc";
313   char *res;
314
315   res = tmpnam(NULL);
316   ok(res != NULL, "tmpnam returned NULL\n");
317   ok(res[0] == '\\', "first character is not a backslash\n");
318   ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
319   ok(res[strlen(res)-1] == '.', "first call - last character is not a dot\n");
320
321   res = tmpnam(name);
322   ok(res != NULL, "tmpnam returned NULL\n");
323   ok(res == name, "supplied buffer was not used\n");
324   ok(res[0] == '\\', "first character is not a backslash\n");
325   ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
326   ok(res[strlen(res)-1] != '.', "second call - last character is a dot\n");
327 }
328
329 static void test_chsize( void )
330 {
331     int fd;
332     long cur, pos, count;
333     char temptext[] = "012345678";
334     char *tempfile = _tempnam( ".", "tst" );
335     
336     ok( tempfile != NULL, "Couldn't create test file: %s\n", tempfile );
337
338     fd = _open( tempfile, _O_CREAT|_O_TRUNC|_O_RDWR, _S_IREAD|_S_IWRITE );
339     ok( fd > 0, "Couldn't open test file\n" );
340
341     count = _write( fd, temptext, sizeof(temptext) );
342     ok( count > 0, "Couldn't write to test file\n" );
343
344     /* get current file pointer */
345     cur = _lseek( fd, 0, SEEK_CUR );
346
347     /* make the file smaller */
348     ok( _chsize( fd, sizeof(temptext) / 2 ) == 0, "_chsize() failed\n" );
349
350     pos = _lseek( fd, 0, SEEK_CUR );
351     ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
352     ok( _filelength( fd ) == sizeof(temptext) / 2, "Wrong file size\n" );
353
354     /* enlarge the file */
355     ok( _chsize( fd, sizeof(temptext) * 2 ) == 0, "_chsize() failed\n" ); 
356
357     pos = _lseek( fd, 0, SEEK_CUR );
358     ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
359     ok( _filelength( fd ) == sizeof(temptext) * 2, "Wrong file size\n" );
360
361     _close( fd );
362     _unlink( tempfile );
363 }
364
365 START_TEST(file)
366 {
367     int arg_c;
368     char** arg_v;
369
370     arg_c = winetest_get_mainargs( &arg_v );
371
372     if (arg_c >= 3)
373     {
374         if (arg_c == 3) test_file_inherit_child(arg_v[2]); else test_file_inherit_child_no(arg_v[2]);
375         return;
376     }
377
378     test_fdopen();
379     test_fileops();
380     test_fgetwc();
381     test_file_put_get();
382     test_file_write_read();
383     test_file_inherit(arg_v[0]);
384     test_tmpnam();
385     test_chsize();
386 }