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