2 * Unit test suite for file functions
4 * Copyright 2002 Bill Currie
5 * Copyright 2005 Paul Rupe
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.
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.
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
22 #include "wine/test.h"
35 static void test_fdopen( void )
37 static const char buffer[] = {0,1,2,3,4,5,6,7,8,9};
42 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
43 write (fd, buffer, sizeof (buffer));
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");
52 unlink ("fdopen.tst");
55 static void test_fileops( void )
57 static const char outbuffer[] = "0,1,2,3,4,5,6,7,8,9";
65 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
66 write (fd, outbuffer, sizeof (outbuffer));
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");
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");
83 for (i = 0, c = EOF; i < sizeof(outbuffer); i++)
85 ok((c = fgetc(file)) == outbuffer[i], "fgetc returned wrong data\n");
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");
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);
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");
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");
122 unlink ("fdopen.tst");
125 static WCHAR* AtoW( char* p )
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 );
134 static void test_fgetwc( void )
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;
146 tempf=_tempnam(".","wne");
147 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
148 fputs(mytext,tempfh);
150 tempfh = fopen(tempf,"rt");
151 fgetws(wtextW,LLEN,tempfh);
152 mytextW = AtoW ((char*)mytext);
156 for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
158 diff_found |= (*aptr != *wptr);
160 ok(!(diff_found), "fgetwc difference found in TEXT mode\n");
161 if(mytextW) free (mytextW);
166 static void test_file_put_get( void )
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";
173 WCHAR wtextW[LLEN+1];
174 WCHAR *mytextW = NULL, *aptr, *wptr;
175 BOOL diff_found = FALSE;
178 tempf=_tempnam(".","wne");
179 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
180 fputs(mytext,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");
187 tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
188 fputs(dostext,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");
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");
199 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
200 fgetws(wtextW,LLEN,tempfh);
201 mytextW = AtoW ((char*)mytext);
205 for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
207 diff_found |= (*aptr != *wptr);
209 ok(!(diff_found), "fgetwc doesn't strip CR in TEXT mode\n");
210 if(mytextW) free (mytextW);
215 static void test_file_write_read( void )
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";
224 tempf=_tempnam(".","wne");
225 tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,
226 _S_IREAD | _S_IWRITE);
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");
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");
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");
246 ok( ret !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
248 tempf=_tempnam(".","wne");
249 tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_BINARY|_O_RDWR,0);
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");
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");
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");
269 ret =_chmod (tempf, _S_IREAD | _S_IWRITE);
271 "Can't chmod '%s' to read-write: %d\n", tempf, errno);
273 ok( ret !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
276 static void test_file_inherit_child(const char* fd_s)
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");
289 static void test_file_inherit_child_no(const char* fd_s)
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));
299 static void test_file_inherit( const char* selfname )
302 const char* arg_v[5];
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");
308 arg_v[1] = "tests/file.c";
309 arg_v[2] = buffer; sprintf(buffer, "%d", fd);
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");
316 ok(unlink("fdopen.tst") != 1, "Couldn't unlink\n");
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");
321 arg_v[1] = "tests/file.c";
322 arg_v[2] = buffer; sprintf(buffer, "%d", fd);
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);
329 ok(unlink("fdopen.tst") != 1, "Couldn't unlink\n");
332 static void test_tmpnam( void )
334 char name[MAX_PATH] = "abc";
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");
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");
351 static void test_chsize( void )
354 long cur, pos, count;
355 char temptext[] = "012345678";
356 char *tempfile = _tempnam( ".", "tst" );
358 ok( tempfile != NULL, "Couldn't create test file: %s\n", tempfile );
360 fd = _open( tempfile, _O_CREAT|_O_TRUNC|_O_RDWR, _S_IREAD|_S_IWRITE );
361 ok( fd > 0, "Couldn't open test file\n" );
363 count = _write( fd, temptext, sizeof(temptext) );
364 ok( count > 0, "Couldn't write to test file\n" );
366 /* get current file pointer */
367 cur = _lseek( fd, 0, SEEK_CUR );
369 /* make the file smaller */
370 ok( _chsize( fd, sizeof(temptext) / 2 ) == 0, "_chsize() failed\n" );
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" );
376 /* enlarge the file */
377 ok( _chsize( fd, sizeof(temptext) * 2 ) == 0, "_chsize() failed\n" );
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" );
392 arg_c = winetest_get_mainargs( &arg_v );
396 if (arg_c == 3) test_file_inherit_child(arg_v[2]); else test_file_inherit_child_no(arg_v[2]);
404 test_file_write_read();
405 test_file_inherit(arg_v[0]);