2 * Unit test suite for file functions
4 * Copyright 2002 Bill Currie
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.
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.
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
21 #include "wine/test.h"
34 static void test_fdopen( void )
36 static const char buffer[] = {0,1,2,3,4,5,6,7,8,9};
41 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
42 write (fd, buffer, sizeof (buffer));
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");
51 unlink ("fdopen.tst");
54 static void test_fileops( void )
56 static const char outbuffer[] = "0,1,2,3,4,5,6,7,8,9";
63 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
64 write (fd, outbuffer, sizeof (outbuffer));
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");
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");
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);
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");
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");
100 unlink ("fdopen.tst");
103 static WCHAR* AtoW( char* p )
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 );
112 static void test_fgetwc( void )
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;
124 tempf=_tempnam(".","wne");
125 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
126 fputs(mytext,tempfh);
128 tempfh = fopen(tempf,"rt");
129 fgetws(wtextW,LLEN,tempfh);
130 mytextW = AtoW ((char*)mytext);
134 for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
136 diff_found |= (*aptr != *wptr);
138 ok(!(diff_found), "fgetwc difference found in TEXT mode\n");
139 if(mytextW) free (mytextW);
144 static void test_file_put_get( void )
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";
151 WCHAR wtextW[LLEN+1];
152 WCHAR *mytextW = NULL, *aptr, *wptr;
153 BOOL diff_found = FALSE;
156 tempf=_tempnam(".","wne");
157 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
158 fputs(mytext,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");
165 tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
166 fputs(dostext,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");
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");
177 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
178 fgetws(wtextW,LLEN,tempfh);
179 mytextW = AtoW ((char*)mytext);
183 for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
185 diff_found |= (*aptr != *wptr);
187 ok(!(diff_found), "fgetwc doesn't strip CR in TEXT mode\n");
188 if(mytextW) free (mytextW);
193 static void test_file_write_read( void )
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";
202 tempf=_tempnam(".","wne");
203 tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,
204 _S_IREAD | _S_IWRITE);
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");
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");
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");
224 ok( ret !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
226 tempf=_tempnam(".","wne");
227 tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_BINARY|_O_RDWR,0);
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");
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");
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");
247 ret =_chmod (tempf, _S_IREAD | _S_IWRITE);
249 "Can't chmod '%s' to read-write: %d\n", tempf, errno);
251 ok( ret !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
254 static void test_file_inherit_child(const char* fd_s)
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");
267 static void test_file_inherit_child_no(const char* fd_s)
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));
277 static void test_file_inherit( const char* selfname )
280 const char* arg_v[5];
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");
286 arg_v[1] = "tests/file.c";
287 arg_v[2] = buffer; sprintf(buffer, "%d", fd);
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");
294 ok(unlink("fdopen.tst") != 1, "Couldn't unlink\n");
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");
299 arg_v[1] = "tests/file.c";
300 arg_v[2] = buffer; sprintf(buffer, "%d", fd);
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);
307 ok(unlink("fdopen.tst") != 1, "Couldn't unlink\n");
310 static void test_tmpnam( void )
312 char name[MAX_PATH] = "abc";
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");
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");
329 static void test_chsize( void )
332 long cur, pos, count;
333 char temptext[] = "012345678";
334 char *tempfile = _tempnam( ".", "tst" );
336 ok( tempfile != NULL, "Couldn't create test file: %s\n", tempfile );
338 fd = _open( tempfile, _O_CREAT|_O_TRUNC|_O_RDWR, _S_IREAD|_S_IWRITE );
339 ok( fd > 0, "Couldn't open test file\n" );
341 count = _write( fd, temptext, sizeof(temptext) );
342 ok( count > 0, "Couldn't write to test file\n" );
344 /* get current file pointer */
345 cur = _lseek( fd, 0, SEEK_CUR );
347 /* make the file smaller */
348 ok( _chsize( fd, sizeof(temptext) / 2 ) == 0, "_chsize() failed\n" );
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" );
354 /* enlarge the file */
355 ok( _chsize( fd, sizeof(temptext) * 2 ) == 0, "_chsize() failed\n" );
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" );
370 arg_c = winetest_get_mainargs( &arg_v );
374 if (arg_c == 3) test_file_inherit_child(arg_v[2]); else test_file_inherit_child_no(arg_v[2]);
382 test_file_write_read();
383 test_file_inherit(arg_v[0]);