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";
62 fd
= open ("fdopen.tst", O_WRONLY
| O_CREAT
| O_BINARY
, _S_IREAD
|_S_IWRITE
);
63 write (fd
, outbuffer
, sizeof (outbuffer
));
66 fd
= open ("fdopen.tst", O_RDONLY
| O_BINARY
);
67 file
= fdopen (fd
, "rb");
68 ok(strlen(outbuffer
) == (sizeof(outbuffer
)-1),"strlen/sizeof error\n");
69 ok(fgets(buffer
,sizeof(buffer
),file
) !=0,"fgets failed unexpected\n");
70 ok(fgets(buffer
,sizeof(buffer
),file
) ==0,"fgets didn't signal EOF\n");
71 ok(feof(file
) !=0,"feof doesn't signal EOF\n");
73 ok(fgets(buffer
,strlen(outbuffer
),file
) !=0,"fgets failed unexpected\n");
74 ok(lstrlenA(buffer
) == strlen(outbuffer
) -1,"fgets didn't read right size\n");
75 ok(fgets(buffer
,sizeof(outbuffer
),file
) !=0,"fgets failed unexpected\n");
76 ok(strlen(buffer
) == 1,"fgets dropped chars\n");
77 ok(buffer
[0] == outbuffer
[strlen(outbuffer
)-1],"fgets exchanged chars\n");
79 fd
= open ("fdopen.tst", O_RDONLY
| O_TEXT
);
80 file
= fdopen (fd
, "rt"); /* open in TEXT mode */
81 ok(fgetws(wbuffer
,sizeof(wbuffer
),file
) !=0,"fgetws failed unexpected\n");
82 ok(fgetws(wbuffer
,sizeof(wbuffer
),file
) ==0,"fgetws didn't signal EOF\n");
83 ok(feof(file
) !=0,"feof doesn't signal EOF\n");
85 ok(fgetws(wbuffer
,strlen(outbuffer
),file
) !=0,"fgetws failed unexpected\n");
86 ok(lstrlenW(wbuffer
) == (strlen(outbuffer
) -1),"fgetws didn't read right size\n");
87 ok(fgetws(wbuffer
,sizeof(outbuffer
),file
) !=0,"fgets failed unexpected\n");
88 ok(lstrlenW(wbuffer
) == 1,"fgets dropped chars\n");
90 unlink ("fdopen.tst");
93 static WCHAR
* AtoW( char* p
)
96 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, p
, -1, NULL
, 0 );
97 buffer
= malloc( len
* sizeof(WCHAR
) );
98 MultiByteToWideChar( CP_ACP
, 0, p
, -1, buffer
, len
);
102 static void test_fgetwc( void )
108 static const char mytext
[]= "This is test_fgetwc\n";
109 WCHAR wtextW
[LLEN
+1];
110 WCHAR
*mytextW
= NULL
, *aptr
, *wptr
;
111 BOOL diff_found
= FALSE
;
114 tempf
=_tempnam(".","wne");
115 tempfh
= fopen(tempf
,"wt"); /* open in TEXT mode */
116 fputs(mytext
,tempfh
);
118 tempfh
= fopen(tempf
,"rt");
119 fgetws(wtextW
,LLEN
,tempfh
);
120 mytextW
= AtoW ((char*)mytext
);
124 for (i
=0; i
<strlen(mytext
); i
++, aptr
++, wptr
++)
126 diff_found
|= (*aptr
!= *wptr
);
128 ok(!(diff_found
), "fgetwc difference found in TEXT mode\n");
129 if(mytextW
) free (mytextW
);
134 static void test_file_put_get( void )
138 static const char mytext
[]= "This is a test_file_put_get\n";
139 static const char dostext
[]= "This is a test_file_put_get\r\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
,"rb"); /* open in TEXT mode */
151 fgets(btext
,LLEN
,tempfh
);
152 ok( strlen(mytext
) + 1 == strlen(btext
),"TEXT/BINARY mode not handled for write\n");
153 ok( btext
[strlen(mytext
)-1] == '\r', "CR not written\n");
155 tempfh
= fopen(tempf
,"wb"); /* open in BINARY mode */
156 fputs(dostext
,tempfh
);
158 tempfh
= fopen(tempf
,"rt"); /* open in TEXT mode */
159 fgets(btext
,LLEN
,tempfh
);
160 ok(strcmp(btext
, mytext
) == 0,"_O_TEXT read doesn't strip CR\n");
162 tempfh
= fopen(tempf
,"rb"); /* open in TEXT mode */
163 fgets(btext
,LLEN
,tempfh
);
164 ok(strcmp(btext
, dostext
) == 0,"_O_BINARY read doesn't preserve CR\n");
167 tempfh
= fopen(tempf
,"rt"); /* open in TEXT mode */
168 fgetws(wtextW
,LLEN
,tempfh
);
169 mytextW
= AtoW ((char*)mytext
);
173 for (i
=0; i
<strlen(mytext
); i
++, aptr
++, wptr
++)
175 diff_found
|= (*aptr
!= *wptr
);
177 ok(!(diff_found
), "fgetwc doesn't strip CR in TEXT mode\n");
178 if(mytextW
) free (mytextW
);
183 static void test_file_write_read( void )
187 static const char mytext
[]= "This is test_file_write_read\nsecond line\n";
188 static const char dostext
[]= "This is test_file_write_read\r\nsecond line\r\n";
192 tempf
=_tempnam(".","wne");
193 tempfd
= _open(tempf
,_O_CREAT
|_O_TRUNC
|_O_TEXT
|_O_RDWR
,
194 _S_IREAD
| _S_IWRITE
);
196 "Can't open '%s': %d\n", tempf
, errno
); /* open in TEXT mode */
197 ok(_write(tempfd
,mytext
,strlen(mytext
)) == lstrlenA(mytext
),
198 "_write _O_TEXT bad return value\n");
200 tempfd
= _open(tempf
,_O_RDONLY
|_O_BINARY
,0); /* open in BINARY mode */
201 ok(_read(tempfd
,btext
,LLEN
) == lstrlenA(dostext
),
202 "_read _O_BINARY got bad length\n");
203 ok( memcmp(dostext
,btext
,strlen(dostext
)) == 0,
204 "problems with _O_TEXT _write / _O_BINARY _read\n");
205 ok( btext
[strlen(dostext
)-2] == '\r', "CR not written or read\n");
207 tempfd
= _open(tempf
,_O_RDONLY
|_O_TEXT
); /* open in TEXT mode */
208 ok(_read(tempfd
,btext
,LLEN
) == lstrlenA(mytext
),
209 "_read _O_TEXT got bad length\n");
210 ok( memcmp(mytext
,btext
,strlen(mytext
)) == 0,
211 "problems with _O_TEXT _write / _read\n");
214 ok( ret
!=-1 ,"Can't unlink '%s': %d\n", tempf
, errno
);
216 tempf
=_tempnam(".","wne");
217 tempfd
= _open(tempf
,_O_CREAT
|_O_TRUNC
|_O_BINARY
|_O_RDWR
,0);
219 "Can't open '%s': %d\n", tempf
, errno
); /* open in BINARY mode */
220 ok(_write(tempfd
,dostext
,strlen(dostext
)) == lstrlenA(dostext
),
221 "_write _O_BINARY bad return value\n");
223 tempfd
= _open(tempf
,_O_RDONLY
|_O_BINARY
,0); /* open in BINARY mode */
224 ok(_read(tempfd
,btext
,LLEN
) == lstrlenA(dostext
),
225 "_read _O_BINARY got bad length\n");
226 ok( memcmp(dostext
,btext
,strlen(dostext
)) == 0,
227 "problems with _O_BINARY _write / _read\n");
228 ok( btext
[strlen(dostext
)-2] == '\r', "CR not written or read\n");
230 tempfd
= _open(tempf
,_O_RDONLY
|_O_TEXT
); /* open in TEXT mode */
231 ok(_read(tempfd
,btext
,LLEN
) == lstrlenA(mytext
),
232 "_read _O_TEXT got bad length\n");
233 ok( memcmp(mytext
,btext
,strlen(mytext
)) == 0,
234 "problems with _O_BINARY _write / _O_TEXT _read\n");
237 ret
=_chmod (tempf
, _S_IREAD
| _S_IWRITE
);
239 "Can't chmod '%s' to read-write: %d\n", tempf
, errno
);
241 ok( ret
!=-1 ,"Can't unlink '%s': %d\n", tempf
, errno
);
244 static void test_file_inherit_child(const char* fd_s
)
250 ret
=write(fd
, "Success", 8);
251 ok( ret
== 8, "Couldn't write in child process on %d (%s)\n", fd
, strerror(errno
));
252 lseek(fd
, 0, SEEK_SET
);
253 ok(read(fd
, buffer
, sizeof (buffer
)) == 8, "Couldn't read back the data\n");
254 ok(memcmp(buffer
, "Success", 8) == 0, "Couldn't read back the data\n");
257 static void test_file_inherit_child_no(const char* fd_s
)
262 ret
= write(fd
, "Success", 8);
263 ok( ret
== -1 && errno
== EBADF
,
264 "Wrong write result in child process on %d (%s)\n", fd
, strerror(errno
));
267 static void test_file_inherit( const char* selfname
)
270 const char* arg_v
[5];
273 fd
= open ("fdopen.tst", O_CREAT
| O_RDWR
| O_BINARY
, _S_IREAD
|_S_IWRITE
);
274 ok(fd
!= -1, "Couldn't create test file\n");
276 arg_v
[1] = "tests/file.c";
277 arg_v
[2] = buffer
; sprintf(buffer
, "%d", fd
);
279 _spawnvp(_P_WAIT
, selfname
, arg_v
);
280 ok(tell(fd
) == 8, "bad position %lu expecting 8\n", tell(fd
));
281 lseek(fd
, 0, SEEK_SET
);
282 ok(read(fd
, buffer
, sizeof (buffer
)) == 8 && memcmp(buffer
, "Success", 8) == 0, "Couldn't read back the data\n");
284 ok(unlink("fdopen.tst") != 1, "Couldn't unlink\n");
286 fd
= open ("fdopen.tst", O_CREAT
| O_RDWR
| O_BINARY
| O_NOINHERIT
, _S_IREAD
|_S_IWRITE
);
287 ok(fd
!= -1, "Couldn't create test file\n");
289 arg_v
[1] = "tests/file.c";
290 arg_v
[2] = buffer
; sprintf(buffer
, "%d", fd
);
293 _spawnvp(_P_WAIT
, selfname
, arg_v
);
294 ok(tell(fd
) == 0, "bad position %lu expecting 0\n", tell(fd
));
295 ok(read(fd
, buffer
, sizeof (buffer
)) == 0, "Found unexpected data (%s)\n", buffer
);
297 ok(unlink("fdopen.tst") != 1, "Couldn't unlink\n");
300 static void test_tmpnam( void )
302 char name
[MAX_PATH
] = "abc";
306 ok(res
!= NULL
, "tmpnam returned NULL\n");
307 ok(res
[0] == '\\', "first character is not a backslash\n");
308 ok(strchr(res
+1, '\\') == 0, "file not in the root directory\n");
309 ok(res
[strlen(res
)-1] == '.', "first call - last character is not a dot\n");
312 ok(res
!= NULL
, "tmpnam returned NULL\n");
313 ok(res
== name
, "supplied buffer was not used\n");
314 ok(res
[0] == '\\', "first character is not a backslash\n");
315 ok(strchr(res
+1, '\\') == 0, "file not in the root directory\n");
316 ok(res
[strlen(res
)-1] != '.', "second call - last character is a dot\n");
326 arg_c
= winetest_get_mainargs( &arg_v
);
330 if (arg_c
== 3) test_file_inherit_child(arg_v
[2]); else test_file_inherit_child_no(arg_v
[2]);
338 test_file_write_read();
339 test_file_inherit(arg_v
[0]);