Fixed definitions of TTTOOLINFOA/W_V1_SIZE and
[wine] / dlls / wininet / tests / http.c
1 #include <stdarg.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include "windef.h"
6 #include "winbase.h"
7 #include "wininet.h"
8
9 #include "wine/test.h"
10
11 int goon = 0;
12
13 VOID WINAPI callback(
14      HINTERNET hInternet,
15      DWORD dwContext,
16      DWORD dwInternetStatus,
17      LPVOID lpvStatusInformation,
18      DWORD dwStatusInformationLength
19 )
20 {
21     char name[124];
22
23     switch (dwInternetStatus)
24     {
25         case INTERNET_STATUS_RESOLVING_NAME:
26             strcpy(name,"INTERNET_STATUS_RESOLVING_NAME");
27             break;
28         case INTERNET_STATUS_NAME_RESOLVED:
29             strcpy(name,"INTERNET_STATUS_NAME_RESOLVED");
30             break;
31         case INTERNET_STATUS_CONNECTING_TO_SERVER:
32             strcpy(name,"INTERNET_STATUS_CONNECTING_TO_SERVER");
33             break;
34         case INTERNET_STATUS_CONNECTED_TO_SERVER:
35             strcpy(name,"INTERNET_STATUS_CONNECTED_TO_SERVER");
36             break;
37         case INTERNET_STATUS_SENDING_REQUEST:
38             strcpy(name,"INTERNET_STATUS_SENDING_REQUEST");
39             break;
40         case INTERNET_STATUS_REQUEST_SENT:
41             strcpy(name,"INTERNET_STATUS_REQUEST_SENT");
42             break;
43         case INTERNET_STATUS_RECEIVING_RESPONSE:
44             strcpy(name,"INTERNET_STATUS_RECEIVING_RESPONSE");
45             break;
46         case INTERNET_STATUS_RESPONSE_RECEIVED:
47             strcpy(name,"INTERNET_STATUS_RESPONSE_RECEIVED");
48             break;
49         case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
50             strcpy(name,"INTERNET_STATUS_CTL_RESPONSE_RECEIVED");
51             break;
52         case INTERNET_STATUS_PREFETCH:
53             strcpy(name,"INTERNET_STATUS_PREFETCH");
54             break;
55         case INTERNET_STATUS_CLOSING_CONNECTION:
56             strcpy(name,"INTERNET_STATUS_CLOSING_CONNECTION");
57             break;
58         case INTERNET_STATUS_CONNECTION_CLOSED:
59             strcpy(name,"INTERNET_STATUS_CONNECTION_CLOSED");
60             break;
61         case INTERNET_STATUS_HANDLE_CREATED:
62             strcpy(name,"INTERNET_STATUS_HANDLE_CREATED");
63             break;
64         case INTERNET_STATUS_HANDLE_CLOSING:
65             strcpy(name,"INTERNET_STATUS_HANDLE_CLOSING");
66             break;
67         case INTERNET_STATUS_REQUEST_COMPLETE:
68             strcpy(name,"INTERNET_STATUS_REQUEST_COMPLETE");
69             goon = 1;
70             break;
71         case INTERNET_STATUS_REDIRECT:
72             strcpy(name,"INTERNET_STATUS_REDIRECT");
73             break;
74         case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
75             strcpy(name,"INTERNET_STATUS_INTERMEDIATE_RESPONSE");
76             break;
77     }
78
79     trace("Callback %p 0x%lx %s(%li) %p %ld\n",hInternet,dwContext,name,dwInternetStatus,lpvStatusInformation,dwStatusInformationLength);
80 }
81
82 void winapi_test(int flags)
83 {
84     DWORD rc;
85     CHAR buffer[4000];
86     DWORD length;
87     DWORD out;
88     const char *types[2] = { "*", NULL };
89     HINTERNET hi, hic = 0, hor = 0;
90
91     trace("Starting with flags 0x%x\n",flags);
92
93     trace("InternetOpenA <--\n");
94     hi = InternetOpenA("",0x0,0x0,0x0,flags);
95     ok((hi != 0x0),"InternetOpen Failed");
96     trace("InternetOpenA -->\n");
97
98     if (hi == 0x0) goto abort;
99
100     InternetSetStatusCallback(hi,&callback);
101
102     trace("InternetConnectA <--\n");
103     hic=InternetConnectA(hi,"www.winehq.com",0x0,0x0,0x0,0x3,0x0,0xdeadbeef);
104     ok((hic != 0x0),"InternetConnect Failed");
105     trace("InternetConnectA -->\n");
106
107     if (hic == 0x0) goto abort;
108
109     trace("HttpOpenRequestA <--\n");
110     hor = HttpOpenRequestA(hic, "GET",
111                           "/about/",
112                           0x0,0x0,types,0x00400800,0xdeadbead);
113     if (hor == 0x0 && GetLastError() == 12007 /* ERROR_INTERNET_NAME_NOT_RESOLVED */) {
114         /*
115          * If the internet name can't be resolved we are probably behind
116          * a firewall or in some other way not directly connected to the
117          * Internet. Not enough reason to fail the test. Just ignore and
118          * abort.
119          */
120     } else  {
121         ok((hor != 0x0),"HttpOpenRequest Failed");
122     }
123     trace("HttpOpenRequestA -->\n");
124
125     if (hor == 0x0) goto abort;
126
127     trace("HttpSendRequestA -->\n");
128     rc = HttpSendRequestA(hor, "", 0xffffffff,0x0,0x0);
129     if (flags)
130         ok(((rc == 0)&&(GetLastError()==997)),
131             "Asyncronous HttpSendRequest NOT returning 0 with error 997");
132     else
133         ok((rc != 0), "Syncronous HttpSendRequest returning 0");
134     trace("HttpSendRequestA <--\n");
135
136     while ((flags)&&(!goon))
137         Sleep(100);
138
139     length = 4;
140     rc = InternetQueryOptionA(hor,0x17,&out,&length);
141     trace("Option 0x17 -> %li  %li\n",rc,out);
142
143     length = 100;
144     rc = InternetQueryOptionA(hor,0x22,buffer,&length);
145     trace("Option 0x22 -> %li  %s\n",rc,buffer);
146
147     length = 4000;
148     rc = HttpQueryInfoA(hor,0x16,buffer,&length,0x0);
149     buffer[length]=0;
150     trace("Option 0x16 -> %li  %s\n",rc,buffer);
151
152     length = 4000;
153     rc = InternetQueryOptionA(hor,0x22,buffer,&length);
154     buffer[length]=0;
155     trace("Option 0x22 -> %li  %s\n",rc,buffer);
156
157     length = 16;
158     rc = HttpQueryInfoA(hor,0x5,&buffer,&length,0x0);
159     trace("Option 0x5 -> %li  %s  (%li)\n",rc,buffer,GetLastError());
160
161     length = 100;
162     rc = HttpQueryInfoA(hor,0x1,buffer,&length,0x0);
163     buffer[length]=0;
164     trace("Option 0x1 -> %li  %s\n",rc,buffer);
165
166     length = 100;
167     trace("Entery Query loop\n");
168
169     while (length)
170     {
171
172         rc = InternetQueryDataAvailable(hor,&length,0x0,0x0);
173         ok((rc != 0),"InternetQueryDataAvailable failed");
174
175         if (length)
176         {
177             char *buffer;
178             buffer = (char*)HeapAlloc(GetProcessHeap(),0,length+1);
179
180             rc = InternetReadFile(hor,buffer,length,&length);
181
182             buffer[length]=0;
183
184             trace("ReadFile -> %li %li\n",rc,length);
185
186             HeapFree(GetProcessHeap(),0,buffer);
187         }
188     }
189 abort:
190     if (hor != 0x0) {
191         rc = InternetCloseHandle(hor);
192         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed");
193     }
194     if (hic != 0x0) {
195         rc = InternetCloseHandle(hic);
196         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed");
197     }
198     if (hi != 0x0) {
199       rc = InternetCloseHandle(hi);
200       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed");
201       if (flags)
202           Sleep(100);
203     }
204 }
205
206 void InternetOpenUrlA_test(void)
207 {
208   HINTERNET myhinternet, myhttp;
209   char buffer[0x400];
210   URL_COMPONENTSA urlComponents;
211   char protocol[32], hostName[1024], userName[1024];
212   char password[1024], extra[1024], path[1024];
213   DWORD size, readbytes, totalbytes=0;
214   
215   myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
216   ok((myhinternet != 0), "InternetOpen failed, error %lx\n",GetLastError());
217   size = 0x400;
218   ok (InternetCanonicalizeUrl("http://LTspice.linear-tech.com/fieldsync2/release.log.gz",buffer, &size,ICU_BROWSER_MODE),
219       "InternetCanonicalizeUrl failed, error %lx\n",GetLastError());
220   
221   urlComponents.dwStructSize = sizeof(URL_COMPONENTSA);
222   urlComponents.lpszScheme = protocol;
223   urlComponents.dwSchemeLength = 32;
224   urlComponents.lpszHostName = hostName;
225   urlComponents.dwHostNameLength = 1024;
226   urlComponents.lpszUserName = userName;
227   urlComponents.dwUserNameLength = 1024;
228   urlComponents.lpszPassword = password;
229   urlComponents.dwPasswordLength = 1024;
230   urlComponents.lpszUrlPath = path;
231   urlComponents.dwUrlPathLength = 2048;
232   urlComponents.lpszExtraInfo = extra;
233   urlComponents.dwExtraInfoLength = 1024;
234   ok((InternetCrackUrl("http://LTspice.linear-tech.com/fieldsync2/release.log.gz", 0,0,&urlComponents)),
235      "InternetCrackUrl failed, error %lx\n",GetLastError());
236   myhttp = InternetOpenUrl(myhinternet, "http://LTspice.linear-tech.com/fieldsync2/release.log.gz", 0, 0,
237                            INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
238   ok((myhttp != 0),"InternetOpenUrl failed, error %lx\n",GetLastError());
239   ok(InternetReadFile(myhttp, buffer,0x400,&readbytes), "InternetReadFile failed, error %lx\n",GetLastError());
240   totalbytes += readbytes;
241   while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
242     totalbytes += readbytes;
243   trace("read 0x%08lx bytes\n",totalbytes);
244 }
245   
246 void InternetCrackUrl_test(void)
247 {
248   URL_COMPONENTSA urlComponents;
249   char protocol[32], hostName[1024], userName[1024];
250   char password[1024], extra[1024], path[1024];
251
252   urlComponents.dwStructSize = sizeof(URL_COMPONENTSA);
253   urlComponents.lpszScheme = protocol;
254   urlComponents.dwSchemeLength = 32;
255   urlComponents.lpszHostName = hostName;
256   urlComponents.dwHostNameLength = 1024;
257   urlComponents.lpszUserName = userName;
258   urlComponents.dwUserNameLength = 1024;
259   urlComponents.lpszPassword = password;
260   urlComponents.dwPasswordLength = 1024;
261   urlComponents.lpszUrlPath = path;
262   urlComponents.dwUrlPathLength = 2048;
263   urlComponents.lpszExtraInfo = extra;
264   urlComponents.dwExtraInfoLength = 1024;
265   ok((InternetCrackUrl("http://LTspice.linear-tech.com/fieldsync2/release.log.gz", 0,0,&urlComponents)),
266      "InternetCrackUrl failed, error %lx\n",GetLastError());
267   ok((strcmp("/fieldsync2/release.log.gz",path) == 0),"path cracked wrong");
268 }
269
270 START_TEST(http)
271 {
272     winapi_test(0x10000000);
273     winapi_test(0x00000000);
274     InternetCrackUrl_test();
275     InternetOpenUrlA_test();
276     
277 }