INT21_GetFreeDiskSpace(): The drive parameter is found in the DL
[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     SetLastError(0);
129     rc = HttpSendRequestA(hor, "", 0xffffffff,0x0,0x0);
130     if (flags)
131         ok(((rc == 0)&&(GetLastError()==997)),
132             "Asyncronous HttpSendRequest NOT returning 0 with error 997");
133     else
134         ok((rc != 0) || GetLastError() == 12007, /* 12007 == XP */
135            "Syncronous HttpSendRequest returning 0, error %ld", GetLastError());
136     trace("HttpSendRequestA <--\n");
137
138     while ((flags)&&(!goon))
139         Sleep(100);
140
141     length = 4;
142     rc = InternetQueryOptionA(hor,0x17,&out,&length);
143     trace("Option 0x17 -> %li  %li\n",rc,out);
144
145     length = 100;
146     rc = InternetQueryOptionA(hor,0x22,buffer,&length);
147     trace("Option 0x22 -> %li  %s\n",rc,buffer);
148
149     length = 4000;
150     rc = HttpQueryInfoA(hor,0x16,buffer,&length,0x0);
151     buffer[length]=0;
152     trace("Option 0x16 -> %li  %s\n",rc,buffer);
153
154     length = 4000;
155     rc = InternetQueryOptionA(hor,0x22,buffer,&length);
156     buffer[length]=0;
157     trace("Option 0x22 -> %li  %s\n",rc,buffer);
158
159     length = 16;
160     rc = HttpQueryInfoA(hor,0x5,&buffer,&length,0x0);
161     trace("Option 0x5 -> %li  %s  (%li)\n",rc,buffer,GetLastError());
162
163     length = 100;
164     rc = HttpQueryInfoA(hor,0x1,buffer,&length,0x0);
165     buffer[length]=0;
166     trace("Option 0x1 -> %li  %s\n",rc,buffer);
167
168     length = 100;
169     trace("Entery Query loop\n");
170
171     while (length)
172     {
173
174         rc = InternetQueryDataAvailable(hor,&length,0x0,0x0);
175         ok((rc != 0),"InternetQueryDataAvailable failed");
176
177         if (length)
178         {
179             char *buffer;
180             buffer = (char*)HeapAlloc(GetProcessHeap(),0,length+1);
181
182             rc = InternetReadFile(hor,buffer,length,&length);
183
184             buffer[length]=0;
185
186             trace("ReadFile -> %li %li\n",rc,length);
187
188             HeapFree(GetProcessHeap(),0,buffer);
189         }
190     }
191 abort:
192     if (hor != 0x0) {
193         rc = InternetCloseHandle(hor);
194         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed");
195     }
196     if (hic != 0x0) {
197         rc = InternetCloseHandle(hic);
198         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed");
199     }
200     if (hi != 0x0) {
201       rc = InternetCloseHandle(hi);
202       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed");
203       if (flags)
204           Sleep(100);
205     }
206 }
207
208 void InternetOpenUrlA_test(void)
209 {
210   HINTERNET myhinternet, myhttp;
211   char buffer[0x400];
212   URL_COMPONENTSA urlComponents;
213   char protocol[32], hostName[1024], userName[1024];
214   char password[1024], extra[1024], path[1024];
215   DWORD size, readbytes, totalbytes=0;
216   
217   myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
218   ok((myhinternet != 0), "InternetOpen failed, error %lx\n",GetLastError());
219   size = 0x400;
220   ok (InternetCanonicalizeUrl("http://LTspice.linear-tech.com/fieldsync2/release.log.gz",buffer, &size,ICU_BROWSER_MODE),
221       "InternetCanonicalizeUrl failed, error %lx\n",GetLastError());
222   
223   urlComponents.dwStructSize = sizeof(URL_COMPONENTSA);
224   urlComponents.lpszScheme = protocol;
225   urlComponents.dwSchemeLength = 32;
226   urlComponents.lpszHostName = hostName;
227   urlComponents.dwHostNameLength = 1024;
228   urlComponents.lpszUserName = userName;
229   urlComponents.dwUserNameLength = 1024;
230   urlComponents.lpszPassword = password;
231   urlComponents.dwPasswordLength = 1024;
232   urlComponents.lpszUrlPath = path;
233   urlComponents.dwUrlPathLength = 2048;
234   urlComponents.lpszExtraInfo = extra;
235   urlComponents.dwExtraInfoLength = 1024;
236   ok((InternetCrackUrl("http://LTspice.linear-tech.com/fieldsync2/release.log.gz", 0,0,&urlComponents)),
237      "InternetCrackUrl failed, error %lx\n",GetLastError());
238   SetLastError(0);
239   myhttp = InternetOpenUrl(myhinternet, "http://LTspice.linear-tech.com/fieldsync2/release.log.gz", 0, 0,
240                            INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
241   if (GetLastError() == 12007)
242     return; /* WinXP returns this when not connected to the net */
243   ok((myhttp != 0),"InternetOpenUrl failed, error %lx\n",GetLastError());
244   ok(InternetReadFile(myhttp, buffer,0x400,&readbytes), "InternetReadFile failed, error %lx\n",GetLastError());
245   totalbytes += readbytes;
246   while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
247     totalbytes += readbytes;
248   trace("read 0x%08lx bytes\n",totalbytes);
249 }
250   
251 void InternetCrackUrl_test(void)
252 {
253   URL_COMPONENTSA urlComponents;
254   char protocol[32], hostName[1024], userName[1024];
255   char password[1024], extra[1024], path[1024];
256
257   urlComponents.dwStructSize = sizeof(URL_COMPONENTSA);
258   urlComponents.lpszScheme = protocol;
259   urlComponents.dwSchemeLength = 32;
260   urlComponents.lpszHostName = hostName;
261   urlComponents.dwHostNameLength = 1024;
262   urlComponents.lpszUserName = userName;
263   urlComponents.dwUserNameLength = 1024;
264   urlComponents.lpszPassword = password;
265   urlComponents.dwPasswordLength = 1024;
266   urlComponents.lpszUrlPath = path;
267   urlComponents.dwUrlPathLength = 2048;
268   urlComponents.lpszExtraInfo = extra;
269   urlComponents.dwExtraInfoLength = 1024;
270   ok((InternetCrackUrl("http://LTspice.linear-tech.com/fieldsync2/release.log.gz", 0,0,&urlComponents)),
271      "InternetCrackUrl failed, error %lx\n",GetLastError());
272   ok((strcmp("/fieldsync2/release.log.gz",path) == 0),"path cracked wrong");
273 }
274
275 START_TEST(http)
276 {
277     winapi_test(0x10000000);
278     winapi_test(0x00000000);
279     InternetCrackUrl_test();
280     InternetOpenUrlA_test();
281     
282 }