Fix subclassing to support nested messages.
[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
32 static void test_fdopen( void )
33 {
34     static char buffer[] = {0,1,2,3,4,5,6,7,8,9};
35     int fd;
36     FILE *file;
37
38     fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
39     write (fd, buffer, sizeof (buffer));
40     close (fd);
41
42     fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
43     lseek (fd, 5, SEEK_SET);
44     file = fdopen (fd, "rb");
45     ok (fread (buffer, 1, sizeof (buffer), file) == 5, "read wrong byte count\n");
46     ok (memcmp (buffer, buffer + 5, 5) == 0, "read wrong bytes\n");
47     fclose (file);
48     unlink ("fdopen.tst");
49 }
50
51 static void test_fileops( void )
52 {
53     static char outbuffer[] = "0,1,2,3,4,5,6,7,8,9";
54     char buffer[256];
55     WCHAR wbuffer[256];
56     int fd;
57     FILE *file;
58
59     fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
60     write (fd, outbuffer, sizeof (outbuffer));
61     close (fd);
62
63     fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
64     file = fdopen (fd, "rb");
65     ok(strlen(outbuffer) == (sizeof(outbuffer)-1),"strlen/sizeof error\n");
66     ok(fgets(buffer,sizeof(buffer),file) !=0,"fgets failed unexpected\n");
67     ok(fgets(buffer,sizeof(buffer),file) ==0,"fgets didn't signal EOF\n");
68     ok(feof(file) !=0,"feof doesn't signal EOF\n");
69     rewind(file);
70     ok(fgets(buffer,strlen(outbuffer),file) !=0,"fgets failed unexpected\n");
71     ok(lstrlenA(buffer) == strlen(outbuffer) -1,"fgets didn't read right size\n");
72     ok(fgets(buffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
73     ok(strlen(buffer) == 1,"fgets dropped chars\n");
74     ok(buffer[0] == outbuffer[strlen(outbuffer)-1],"fgets exchanged chars\n");
75     fclose (file);
76     fd = open ("fdopen.tst", O_RDONLY | O_TEXT);
77     file = fdopen (fd, "rt"); /* open in TEXT mode */
78     ok(fgetws(wbuffer,sizeof(wbuffer),file) !=0,"fgetws failed unexpected\n");
79     ok(fgetws(wbuffer,sizeof(wbuffer),file) ==0,"fgetws didn't signal EOF\n");
80     ok(feof(file) !=0,"feof doesn't signal EOF\n");
81     rewind(file);
82     ok(fgetws(wbuffer,strlen(outbuffer),file) !=0,"fgetws failed unexpected\n");
83     ok(lstrlenW(wbuffer) == (strlen(outbuffer) -1),"fgetws didn't read right size\n");
84     ok(fgetws(wbuffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
85     ok(lstrlenW(wbuffer) == 1,"fgets dropped chars\n");
86     fclose (file);
87     unlink ("fdopen.tst");
88
89
90 static WCHAR* AtoW( char* p )
91 {
92     WCHAR* buffer;
93     DWORD len = MultiByteToWideChar( CP_ACP, 0, p, -1, NULL, 0 );
94     buffer = malloc( len * sizeof(WCHAR) );
95     MultiByteToWideChar( CP_ACP, 0, p, -1, buffer, len );
96     return buffer;
97 }
98
99 static void test_fgetwc( void )
100 {
101 #define LLEN 512
102
103   char* tempf;
104   FILE *tempfh;
105   const char mytext[]= "This is test_fgetwc\n";
106   WCHAR wtextW[LLEN+1];
107   WCHAR *mytextW = NULL, *aptr, *wptr;
108   BOOL diff_found = FALSE;
109   unsigned int i;
110   
111   tempf=_tempnam(".","wne");
112   tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
113   fputs(mytext,tempfh);
114   fclose(tempfh);
115   tempfh = fopen(tempf,"rt");
116   fgetws(wtextW,LLEN,tempfh);
117   mytextW = AtoW ((char*)mytext);
118   aptr = mytextW;
119   wptr = wtextW;
120
121   for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
122     {
123       diff_found |= (*aptr != *wptr);
124     }
125   ok(!(diff_found), "fgetwc difference found in TEXT mode\n");
126   if(mytextW) free (mytextW);
127   fclose(tempfh);
128   unlink(tempf);
129 }
130   
131 static void test_file_put_get( void )
132 {
133   char* tempf;
134   FILE *tempfh;
135   const char mytext[]=  "This is a test_file_put_get\n";
136   const char dostext[]= "This is a test_file_put_get\r\n";
137   char btext[LLEN];
138   WCHAR wtextW[LLEN+1];
139   WCHAR *mytextW = NULL, *aptr, *wptr;
140   BOOL diff_found = FALSE;
141   unsigned int i;
142
143   tempf=_tempnam(".","wne");
144   tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
145   fputs(mytext,tempfh);
146   fclose(tempfh);
147   tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
148   fgets(btext,LLEN,tempfh);
149   ok( strlen(mytext) + 1 == strlen(btext),"TEXT/BINARY mode not handled for write\n");
150   ok( btext[strlen(mytext)-1] == '\r', "CR not written\n");
151   fclose(tempfh);
152   tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
153   fputs(dostext,tempfh);
154   fclose(tempfh);
155   tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
156   fgets(btext,LLEN,tempfh);
157   ok(strcmp(btext, mytext) == 0,"_O_TEXT read doesn't strip CR\n");
158   fclose(tempfh);
159   tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
160   fgets(btext,LLEN,tempfh);
161   ok(strcmp(btext, dostext) == 0,"_O_BINARY read doesn't preserve CR\n");
162
163   fclose(tempfh);
164   tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
165   fgetws(wtextW,LLEN,tempfh);
166   mytextW = AtoW ((char*)mytext);
167   aptr = mytextW;
168   wptr = wtextW;
169
170   for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
171     {
172       diff_found |= (*aptr != *wptr);
173     }
174   ok(!(diff_found), "fgetwc doesn't strip CR in TEXT mode\n");
175   if(mytextW) free (mytextW);
176   fclose(tempfh);
177   unlink(tempf);
178 }
179 static void test_file_write_read( void )
180 {
181   char* tempf;
182   int tempfd;
183   const char mytext[]=  "This is test_file_write_read\nsecond line\n";
184   const char dostext[]= "This is test_file_write_read\r\nsecond line\r\n";
185   char btext[LLEN];
186
187   tempf=_tempnam(".","wne");
188   ok((tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,
189                      _S_IREAD | _S_IWRITE)) != -1,
190      "Can't open '%s': %d\n", tempf, errno); /* open in TEXT mode */
191   ok(_write(tempfd,mytext,strlen(mytext)) == lstrlenA(mytext),
192      "_write _O_TEXT bad return value\n");
193   _close(tempfd);
194   tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
195   ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
196      "_read _O_BINARY got bad length\n");
197   ok( memcmp(dostext,btext,strlen(dostext)) == 0,
198       "problems with _O_TEXT _write / _O_BINARY _read\n");
199   ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
200   _close(tempfd);
201   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
202   ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
203      "_read _O_TEXT got bad length\n");
204   ok( memcmp(mytext,btext,strlen(mytext)) == 0,
205       "problems with _O_TEXT _write / _read\n");
206   _close(tempfd);
207   ok(unlink(tempf) !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
208
209   tempf=_tempnam(".","wne");
210   ok((tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_BINARY|_O_RDWR,0)) != -1,
211      "Can't open '%s': %d\n", tempf, errno); /* open in BINARY mode */
212   ok(_write(tempfd,dostext,strlen(dostext)) == lstrlenA(dostext),
213      "_write _O_BINARY bad return value\n");
214   _close(tempfd);
215   tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
216   ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
217      "_read _O_BINARY got bad length\n");
218   ok( memcmp(dostext,btext,strlen(dostext)) == 0,
219       "problems with _O_BINARY _write / _read\n");
220   ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
221   _close(tempfd);
222   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
223   ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
224      "_read _O_TEXT got bad length\n");
225   ok( memcmp(mytext,btext,strlen(mytext)) == 0,
226       "problems with _O_BINARY _write / _O_TEXT _read\n");
227   _close(tempfd);
228
229   ok(_chmod (tempf, _S_IREAD | _S_IWRITE) == 0,
230      "Can't chmod '%s' to read-write: %d\n", tempf, errno);
231   ok(unlink(tempf) !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
232 }
233
234 static void test_tmpnam( void )
235 {
236   char name[MAX_PATH] = "abc";
237   char *res;
238
239   res = tmpnam(NULL);
240   ok(res != NULL, "tmpnam returned NULL\n");
241   ok(res[0] == '\\', "first character is not a backslash\n");
242   ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
243   ok(res[strlen(res)-1] == '.', "first call - last character is not a dot\n");
244
245   res = tmpnam(name);
246   ok(res != NULL, "tmpnam returned NULL\n");
247   ok(res == name, "supplied buffer was not used\n");
248   ok(res[0] == '\\', "first character is not a backslash\n");
249   ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
250   ok(res[strlen(res)-1] != '.', "second call - last character is not a dot\n");
251 }
252
253
254
255 START_TEST(file)
256 {
257     test_fdopen();
258     test_fileops();
259     test_fgetwc();
260     test_file_put_get();
261     test_file_write_read();
262     test_tmpnam();
263 }