Add more dialog controls, do something when they're clicked on.
[wine/multimedia.git] / dlls / msvcrt / tests / file.c
blobeb9531702a3a6b56f7e513676fbe8c946cbd31a1
1 /*
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"
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>
34 static void test_fdopen( void )
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;
41 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
42 write (fd, buffer, sizeof (buffer));
43 close (fd);
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");
54 static void test_fileops( void )
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;
62 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
63 write (fd, outbuffer, sizeof (outbuffer));
64 close (fd);
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");
72 rewind(file);
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");
78 fclose (file);
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");
84 rewind(file);
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");
89 fclose (file);
90 unlink ("fdopen.tst");
93 static WCHAR* AtoW( char* p )
95 WCHAR* buffer;
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 );
99 return buffer;
102 static void test_fgetwc( void )
104 #define LLEN 512
106 char* tempf;
107 FILE *tempfh;
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;
112 unsigned int i;
114 tempf=_tempnam(".","wne");
115 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
116 fputs(mytext,tempfh);
117 fclose(tempfh);
118 tempfh = fopen(tempf,"rt");
119 fgetws(wtextW,LLEN,tempfh);
120 mytextW = AtoW ((char*)mytext);
121 aptr = mytextW;
122 wptr = wtextW;
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);
130 fclose(tempfh);
131 unlink(tempf);
134 static void test_file_put_get( void )
136 char* tempf;
137 FILE *tempfh;
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";
140 char btext[LLEN];
141 WCHAR wtextW[LLEN+1];
142 WCHAR *mytextW = NULL, *aptr, *wptr;
143 BOOL diff_found = FALSE;
144 unsigned int i;
146 tempf=_tempnam(".","wne");
147 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
148 fputs(mytext,tempfh);
149 fclose(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");
154 fclose(tempfh);
155 tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
156 fputs(dostext,tempfh);
157 fclose(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");
161 fclose(tempfh);
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");
166 fclose(tempfh);
167 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
168 fgetws(wtextW,LLEN,tempfh);
169 mytextW = AtoW ((char*)mytext);
170 aptr = mytextW;
171 wptr = wtextW;
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);
179 fclose(tempfh);
180 unlink(tempf);
183 static void test_file_write_read( void )
185 char* tempf;
186 int tempfd;
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";
189 char btext[LLEN];
190 int ret;
192 tempf=_tempnam(".","wne");
193 tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,
194 _S_IREAD | _S_IWRITE);
195 ok( tempfd != -1,
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");
199 _close(tempfd);
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");
206 _close(tempfd);
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");
212 _close(tempfd);
213 ret = unlink(tempf);
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);
218 ok( tempfd != -1,
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");
222 _close(tempfd);
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");
229 _close(tempfd);
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");
235 _close(tempfd);
237 ret =_chmod (tempf, _S_IREAD | _S_IWRITE);
238 ok( ret == 0,
239 "Can't chmod '%s' to read-write: %d\n", tempf, errno);
240 ret = unlink(tempf);
241 ok( ret !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
244 static void test_file_inherit_child(const char* fd_s)
246 int fd = atoi(fd_s);
247 char buffer[32];
248 int ret;
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)
259 int fd = atoi(fd_s);
260 int ret;
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 )
269 int fd;
270 const char* arg_v[5];
271 char buffer[16];
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");
275 arg_v[0] = selfname;
276 arg_v[1] = "tests/file.c";
277 arg_v[2] = buffer; sprintf(buffer, "%d", fd);
278 arg_v[3] = 0;
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");
283 close (fd);
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");
288 arg_v[0] = selfname;
289 arg_v[1] = "tests/file.c";
290 arg_v[2] = buffer; sprintf(buffer, "%d", fd);
291 arg_v[3] = buffer;
292 arg_v[4] = 0;
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);
296 close (fd);
297 ok(unlink("fdopen.tst") != 1, "Couldn't unlink\n");
300 static void test_tmpnam( void )
302 char name[MAX_PATH] = "abc";
303 char *res;
305 res = tmpnam(NULL);
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");
311 res = tmpnam(name);
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");
321 START_TEST(file)
323 int arg_c;
324 char** arg_v;
326 arg_c = winetest_get_mainargs( &arg_v );
328 if (arg_c >= 3)
330 if (arg_c == 3) test_file_inherit_child(arg_v[2]); else test_file_inherit_child_no(arg_v[2]);
331 return;
334 test_fdopen();
335 test_fileops();
336 test_fgetwc();
337 test_file_put_get();
338 test_file_write_read();
339 test_file_inherit(arg_v[0]);
340 test_tmpnam();