comctl32: A couple fixes for tab icon offsets.
[wine] / dlls / wininet / tests / http.c
1 /*
2  * Wininet - Http tests
3  *
4  * Copyright 2002 Aric Stewart
5  * Copyright 2004 Mike McCormack
6  * Copyright 2005 Hans Leidekker
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wininet.h"
30
31 #include "wine/test.h"
32
33 #define TEST_URL "http://www.winehq.org/site/about"
34
35 static HANDLE hCompleteEvent;
36
37 static VOID WINAPI callback(
38      HINTERNET hInternet,
39      DWORD dwContext,
40      DWORD dwInternetStatus,
41      LPVOID lpvStatusInformation,
42      DWORD dwStatusInformationLength
43 )
44 {
45     switch (dwInternetStatus)
46     {
47         case INTERNET_STATUS_RESOLVING_NAME:
48             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %ld\n",
49                 GetCurrentThreadId(), hInternet, dwContext,
50                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
51             break;
52         case INTERNET_STATUS_NAME_RESOLVED:
53             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %ld\n",
54                 GetCurrentThreadId(), hInternet, dwContext,
55                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
56             break;
57         case INTERNET_STATUS_CONNECTING_TO_SERVER:
58             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %ld\n",
59                 GetCurrentThreadId(), hInternet, dwContext,
60                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
61             break;
62         case INTERNET_STATUS_CONNECTED_TO_SERVER:
63             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %ld\n",
64                 GetCurrentThreadId(), hInternet, dwContext,
65                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
66             break;
67         case INTERNET_STATUS_SENDING_REQUEST:
68             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %ld\n",
69                 GetCurrentThreadId(), hInternet, dwContext,
70                 lpvStatusInformation,dwStatusInformationLength);
71             break;
72         case INTERNET_STATUS_REQUEST_SENT:
73             ok(dwStatusInformationLength == sizeof(DWORD),
74                 "info length should be sizeof(DWORD) instead of %ld\n",
75                 dwStatusInformationLength);
76             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%lx %ld\n",
77                 GetCurrentThreadId(), hInternet, dwContext,
78                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
79             break;
80         case INTERNET_STATUS_RECEIVING_RESPONSE:
81             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %ld\n",
82                 GetCurrentThreadId(), hInternet, dwContext,
83                 lpvStatusInformation,dwStatusInformationLength);
84             break;
85         case INTERNET_STATUS_RESPONSE_RECEIVED:
86             ok(dwStatusInformationLength == sizeof(DWORD),
87                 "info length should be sizeof(DWORD) instead of %ld\n",
88                 dwStatusInformationLength);
89             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%lx %ld\n",
90                 GetCurrentThreadId(), hInternet, dwContext,
91                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
92             break;
93         case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
94             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %ld\n",
95                 GetCurrentThreadId(), hInternet,dwContext,
96                 lpvStatusInformation,dwStatusInformationLength);
97             break;
98         case INTERNET_STATUS_PREFETCH:
99             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %ld\n",
100                 GetCurrentThreadId(), hInternet, dwContext,
101                 lpvStatusInformation,dwStatusInformationLength);
102             break;
103         case INTERNET_STATUS_CLOSING_CONNECTION:
104             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %ld\n",
105                 GetCurrentThreadId(), hInternet, dwContext,
106                 lpvStatusInformation,dwStatusInformationLength);
107             break;
108         case INTERNET_STATUS_CONNECTION_CLOSED:
109             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %ld\n",
110                 GetCurrentThreadId(), hInternet, dwContext,
111                 lpvStatusInformation,dwStatusInformationLength);
112             break;
113         case INTERNET_STATUS_HANDLE_CREATED:
114             ok(dwStatusInformationLength == sizeof(HINTERNET),
115                 "info length should be sizeof(HINTERNET) instead of %ld\n",
116                 dwStatusInformationLength);
117             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %ld\n",
118                 GetCurrentThreadId(), hInternet, dwContext,
119                 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
120             break;
121         case INTERNET_STATUS_HANDLE_CLOSING:
122             ok(dwStatusInformationLength == sizeof(HINTERNET),
123                 "info length should be sizeof(HINTERNET) instead of %ld\n",
124                 dwStatusInformationLength);
125             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %ld\n",
126                 GetCurrentThreadId(), hInternet, dwContext,
127                 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
128             break;
129         case INTERNET_STATUS_REQUEST_COMPLETE:
130         {
131             INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
132             ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
133                 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %ld\n",
134                 dwStatusInformationLength);
135             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%ld} %ld\n",
136                 GetCurrentThreadId(), hInternet, dwContext,
137                 iar->dwResult,iar->dwError,dwStatusInformationLength);
138             SetEvent(hCompleteEvent);
139             break;
140         }
141         case INTERNET_STATUS_REDIRECT:
142             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %ld\n",
143                 GetCurrentThreadId(), hInternet, dwContext,
144                 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
145             break;
146         case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
147             trace("%04lx:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %ld\n",
148                 GetCurrentThreadId(), hInternet, dwContext,
149                 lpvStatusInformation, dwStatusInformationLength);
150             break;
151         default:
152             trace("%04lx:Callback %p 0x%lx %ld %p %ld\n",
153                 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
154                 lpvStatusInformation, dwStatusInformationLength);
155     }
156 }
157
158 static void InternetReadFile_test(int flags)
159 {
160     DWORD rc;
161     CHAR buffer[4000];
162     DWORD length;
163     DWORD out;
164     const char *types[2] = { "*", NULL };
165     HINTERNET hi, hic = 0, hor = 0;
166
167     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
168
169     trace("Starting InternetReadFile test with flags 0x%x\n",flags);
170
171     trace("InternetOpenA <--\n");
172     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
173     ok((hi != 0x0),"InternetOpen failed with error %ld\n", GetLastError());
174     trace("InternetOpenA -->\n");
175
176     if (hi == 0x0) goto abort;
177
178     InternetSetStatusCallback(hi,&callback);
179
180     trace("InternetConnectA <--\n");
181     hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
182                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
183     ok((hic != 0x0),"InternetConnect failed with error %ld\n", GetLastError());
184     trace("InternetConnectA -->\n");
185
186     if (hic == 0x0) goto abort;
187
188     trace("HttpOpenRequestA <--\n");
189     hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
190                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
191                            0xdeadbead);
192     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
193         /*
194          * If the internet name can't be resolved we are probably behind
195          * a firewall or in some other way not directly connected to the
196          * Internet. Not enough reason to fail the test. Just ignore and
197          * abort.
198          */
199     } else  {
200         ok((hor != 0x0),"HttpOpenRequest failed with error %ld\n", GetLastError());
201     }
202     trace("HttpOpenRequestA -->\n");
203
204     if (hor == 0x0) goto abort;
205
206     trace("HttpSendRequestA -->\n");
207     SetLastError(0xdeadbeef);
208     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
209     if (flags & INTERNET_FLAG_ASYNC)
210         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
211             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
212     else
213         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
214            "Synchronous HttpSendRequest returning 0, error %ld\n", GetLastError());
215     trace("HttpSendRequestA <--\n");
216
217     if (flags & INTERNET_FLAG_ASYNC)
218         WaitForSingleObject(hCompleteEvent, INFINITE);
219
220     length = 4;
221     rc = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
222     trace("Option 0x17 -> %li  %li\n",rc,out);
223
224     length = 100;
225     rc = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
226     trace("Option 0x22 -> %li  %s\n",rc,buffer);
227
228     length = 4000;
229     rc = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
230     buffer[length]=0;
231     trace("Option 0x16 -> %li  %s\n",rc,buffer);
232
233     length = 4000;
234     rc = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
235     buffer[length]=0;
236     trace("Option 0x22 -> %li  %s\n",rc,buffer);
237
238     length = 16;
239     rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
240     trace("Option 0x5 -> %li  %s  (%li)\n",rc,buffer,GetLastError());
241
242     length = 100;
243     rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
244     buffer[length]=0;
245     trace("Option 0x1 -> %li  %s\n",rc,buffer);
246
247     SetLastError(0xdeadbeef);
248     rc = InternetReadFile(NULL, buffer, 100, &length);
249     ok(!rc, "InternetReadFile should have failed\n");
250     ok(GetLastError() == ERROR_INVALID_HANDLE,
251         "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %ld\n",
252         GetLastError());
253
254     length = 100;
255     trace("Entering Query loop\n");
256
257     while (length)
258     {
259         rc = InternetQueryDataAvailable(hor,&length,0x0,0x0);
260         ok(!(rc == 0 && length != 0),"InternetQueryDataAvailable failed\n");
261
262         if (length)
263         {
264             char *buffer;
265             buffer = HeapAlloc(GetProcessHeap(),0,length+1);
266
267             rc = InternetReadFile(hor,buffer,length,&length);
268
269             buffer[length]=0;
270
271             trace("ReadFile -> %li %li\n",rc,length);
272
273             HeapFree(GetProcessHeap(),0,buffer);
274         }
275     }
276 abort:
277     if (hor != 0x0) {
278         SetLastError(0xdeadbeef);
279         rc = InternetCloseHandle(hor);
280         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
281         SetLastError(0xdeadbeef);
282         rc = InternetCloseHandle(hor);
283         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
284         ok (GetLastError() == ERROR_INVALID_HANDLE,
285             "Double close of handle should have set ERROR_INVALID_HANDLE instead of %ld\n",
286             GetLastError());
287     }
288     if (hic != 0x0) {
289         rc = InternetCloseHandle(hic);
290         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
291     }
292     if (hi != 0x0) {
293       rc = InternetCloseHandle(hi);
294       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
295       if (flags & INTERNET_FLAG_ASYNC)
296           Sleep(100);
297     }
298     CloseHandle(hCompleteEvent);
299 }
300
301 static void InternetReadFileExA_test(int flags)
302 {
303     DWORD rc;
304     DWORD length;
305     const char *types[2] = { "*", NULL };
306     HINTERNET hi, hic = 0, hor = 0;
307     INTERNET_BUFFERS inetbuffers;
308
309     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
310
311     trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
312
313     trace("InternetOpenA <--\n");
314     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
315     ok((hi != 0x0),"InternetOpen failed with error %ld\n", GetLastError());
316     trace("InternetOpenA -->\n");
317
318     if (hi == 0x0) goto abort;
319
320     InternetSetStatusCallback(hi,&callback);
321
322     trace("InternetConnectA <--\n");
323     hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
324                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
325     ok((hic != 0x0),"InternetConnect failed with error %ld\n", GetLastError());
326     trace("InternetConnectA -->\n");
327
328     if (hic == 0x0) goto abort;
329
330     trace("HttpOpenRequestA <--\n");
331     hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
332                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
333                            0xdeadbead);
334     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
335         /*
336          * If the internet name can't be resolved we are probably behind
337          * a firewall or in some other way not directly connected to the
338          * Internet. Not enough reason to fail the test. Just ignore and
339          * abort.
340          */
341     } else  {
342         ok((hor != 0x0),"HttpOpenRequest failed with error %ld\n", GetLastError());
343     }
344     trace("HttpOpenRequestA -->\n");
345
346     if (hor == 0x0) goto abort;
347
348     trace("HttpSendRequestA -->\n");
349     SetLastError(0xdeadbeef);
350     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
351     if (flags & INTERNET_FLAG_ASYNC)
352         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
353             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
354     else
355         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
356            "Synchronous HttpSendRequest returning 0, error %ld\n", GetLastError());
357     trace("HttpSendRequestA <--\n");
358
359     if (!rc && (GetLastError() == ERROR_IO_PENDING))
360         WaitForSingleObject(hCompleteEvent, INFINITE);
361
362     /* tests invalid dwStructSize */
363     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
364     inetbuffers.lpcszHeader = NULL;
365     inetbuffers.dwHeadersLength = 0;
366     inetbuffers.dwBufferLength = 10;
367     inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
368     inetbuffers.dwOffsetHigh = 1234;
369     inetbuffers.dwOffsetLow = 5678;
370     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
371     ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
372         "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %ld\n",
373         rc ? "TRUE" : "FALSE", GetLastError());
374     HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
375
376     /* tests to see whether lpcszHeader is used - it isn't */
377     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
378     inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
379     inetbuffers.dwHeadersLength = 255;
380     inetbuffers.dwBufferLength = 0;
381     inetbuffers.lpvBuffer = NULL;
382     inetbuffers.dwOffsetHigh = 1234;
383     inetbuffers.dwOffsetLow = 5678;
384     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
385     ok(rc, "InternetReadFileEx failed with error %ld\n", GetLastError());
386
387     rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
388     ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
389         "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %ld\n",
390         rc ? "TRUE" : "FALSE", GetLastError());
391
392     length = 0;
393     trace("Entering Query loop\n");
394
395     while (TRUE)
396     {
397         inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
398         inetbuffers.dwBufferLength = 1024;
399         inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
400         inetbuffers.dwOffsetHigh = 1234;
401         inetbuffers.dwOffsetLow = 5678;
402
403         rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
404         if (!rc)
405         {
406             if (GetLastError() == ERROR_IO_PENDING)
407             {
408                 trace("InternetReadFileEx -> PENDING\n");
409                 WaitForSingleObject(hCompleteEvent, INFINITE);
410             }
411             else
412             {
413                 trace("InternetReadFileEx -> FAILED %ld\n", GetLastError());
414                 break;
415             }
416         }
417         else
418             trace("InternetReadFileEx -> SUCCEEDED\n");
419
420         trace("read %li bytes\n", inetbuffers.dwBufferLength);
421         ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
422
423         ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
424             "InternetReadFileEx sets offsets to 0x%lx%08lx\n",
425             inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
426
427         HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
428
429         if (!inetbuffers.dwBufferLength)
430             break;
431
432         length += inetbuffers.dwBufferLength;
433     }
434     trace("Finished. Read %ld bytes\n", length);
435
436 abort:
437     if (hor) {
438         rc = InternetCloseHandle(hor);
439         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
440         rc = InternetCloseHandle(hor);
441         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
442     }
443     if (hic) {
444         rc = InternetCloseHandle(hic);
445         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
446     }
447     if (hi) {
448       rc = InternetCloseHandle(hi);
449       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
450       if (flags & INTERNET_FLAG_ASYNC)
451           Sleep(100);
452     }
453     CloseHandle(hCompleteEvent);
454 }
455
456 static void InternetOpenUrlA_test(void)
457 {
458   HINTERNET myhinternet, myhttp;
459   char buffer[0x400];
460   DWORD size, readbytes, totalbytes=0;
461   BOOL ret;
462   
463   myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
464   ok((myhinternet != 0), "InternetOpen failed, error %lx\n",GetLastError());
465   size = 0x400;
466   ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
467   ok( ret, "InternetCanonicalizeUrl failed, error %lx\n",GetLastError());
468
469   SetLastError(0);
470   myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
471                            INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
472   if (GetLastError() == 12007)
473     return; /* WinXP returns this when not connected to the net */
474   ok((myhttp != 0),"InternetOpenUrl failed, error %lx\n",GetLastError());
475   ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
476   ok( ret, "InternetReadFile failed, error %lx\n",GetLastError());
477   totalbytes += readbytes;
478   while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
479     totalbytes += readbytes;
480   trace("read 0x%08lx bytes\n",totalbytes);
481 }
482
483 static void InternetTimeFromSystemTimeA_test(void)
484 {
485     BOOL ret;
486     static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
487     char string[INTERNET_RFC1123_BUFSIZE];
488     static const char expect[] = "Fri, 07 Jan 2005 12:06:35 GMT";
489
490     ret = InternetTimeFromSystemTimeA( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
491     ok( ret, "InternetTimeFromSystemTimeA failed (%ld)\n", GetLastError() );
492
493     ok( !memcmp( string, expect, sizeof(expect) ),
494         "InternetTimeFromSystemTimeA failed (%ld)\n", GetLastError() );
495 }
496
497 static void InternetTimeFromSystemTimeW_test(void)
498 {
499     BOOL ret;
500     static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
501     WCHAR string[INTERNET_RFC1123_BUFSIZE + 1];
502     static const WCHAR expect[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
503                                     '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
504
505     ret = InternetTimeFromSystemTimeW( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
506     ok( ret, "InternetTimeFromSystemTimeW failed (%ld)\n", GetLastError() );
507
508     ok( !memcmp( string, expect, sizeof(expect) ),
509         "InternetTimeFromSystemTimeW failed (%ld)\n", GetLastError() );
510 }
511
512 static void InternetTimeToSystemTimeA_test(void)
513 {
514     BOOL ret;
515     SYSTEMTIME time;
516     static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
517     static const char string[] = "Fri, 07 Jan 2005 12:06:35 GMT";
518     static const char string2[] = " fri 7 jan 2005 12 06 35";
519
520     ret = InternetTimeToSystemTimeA( string, &time, 0 );
521     ok( ret, "InternetTimeToSystemTimeA failed (%ld)\n", GetLastError() );
522     ok( !memcmp( &time, &expect, sizeof(expect) ),
523         "InternetTimeToSystemTimeA failed (%ld)\n", GetLastError() );
524
525     ret = InternetTimeToSystemTimeA( string2, &time, 0 );
526     ok( ret, "InternetTimeToSystemTimeA failed (%ld)\n", GetLastError() );
527     ok( !memcmp( &time, &expect, sizeof(expect) ),
528         "InternetTimeToSystemTimeA failed (%ld)\n", GetLastError() );
529 }
530
531 static void InternetTimeToSystemTimeW_test(void)
532 {
533     BOOL ret;
534     SYSTEMTIME time;
535     static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
536     static const WCHAR string[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
537                                     '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
538     static const WCHAR string2[] = { ' ','f','r','i',' ','7',' ','j','a','n',' ','2','0','0','5',' ',
539                                      '1','2',' ','0','6',' ','3','5',0 };
540     static const WCHAR string3[] = { 'F','r',0 };
541
542     ret = InternetTimeToSystemTimeW( NULL, NULL, 0 );
543     ok( !ret, "InternetTimeToSystemTimeW succeeded (%ld)\n", GetLastError() );
544
545     ret = InternetTimeToSystemTimeW( NULL, &time, 0 );
546     ok( !ret, "InternetTimeToSystemTimeW succeeded (%ld)\n", GetLastError() );
547
548     ret = InternetTimeToSystemTimeW( string, NULL, 0 );
549     ok( !ret, "InternetTimeToSystemTimeW succeeded (%ld)\n", GetLastError() );
550
551     ret = InternetTimeToSystemTimeW( string, &time, 1 );
552     ok( ret, "InternetTimeToSystemTimeW failed (%ld)\n", GetLastError() );
553
554     ret = InternetTimeToSystemTimeW( string, &time, 0 );
555     ok( ret, "InternetTimeToSystemTimeW failed (%ld)\n", GetLastError() );
556     ok( !memcmp( &time, &expect, sizeof(expect) ),
557         "InternetTimeToSystemTimeW failed (%ld)\n", GetLastError() );
558
559     ret = InternetTimeToSystemTimeW( string2, &time, 0 );
560     ok( ret, "InternetTimeToSystemTimeW failed (%ld)\n", GetLastError() );
561     ok( !memcmp( &time, &expect, sizeof(expect) ),
562         "InternetTimeToSystemTimeW failed (%ld)\n", GetLastError() );
563
564     ret = InternetTimeToSystemTimeW( string3, &time, 0 );
565     ok( ret, "InternetTimeToSystemTimeW failed (%ld)\n", GetLastError() );
566 }
567
568 static void HttpSendRequestEx_test(void)
569 {
570     HINTERNET hSession;
571     HINTERNET hConnect;
572     HINTERNET hRequest;
573
574     INTERNET_BUFFERS BufferIn;
575     DWORD dwBytesWritten;
576     DWORD dwBytesRead;
577     CHAR szBuffer[256];
578     int i;
579     BOOL ret;
580
581     static const char szPostData[] = "mode=Test";
582     static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
583     
584     hSession = InternetOpen("Wine Regression Test",
585             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
586     ok( hSession != NULL ,"Unable to open Internet session\n");
587     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
588             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
589             0);
590     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
591     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
592             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
593     ok( hRequest != NULL, "Failed to open request handle\n");
594
595
596     BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
597     BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
598     BufferIn.lpcszHeader = szContentType;
599     BufferIn.dwHeadersLength = sizeof(szContentType);
600     BufferIn.dwHeadersTotal = sizeof(szContentType);
601     BufferIn.lpvBuffer = (LPVOID)szPostData;
602     BufferIn.dwBufferLength = 3;
603     BufferIn.dwBufferTotal = sizeof(szPostData)-1;
604     BufferIn.dwOffsetLow = 0;
605     BufferIn.dwOffsetHigh = 0;
606
607     ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
608     ok(ret, "HttpSendRequestEx Failed with error %ld\n", GetLastError());
609
610     for (i = 3; szPostData[i]; i++)
611         ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
612                 "InternetWriteFile failed\n");
613
614     ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
615
616     ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
617             "Unable to read response\n");
618     szBuffer[dwBytesRead] = 0;
619
620     ok(dwBytesRead == 13,"Read %lu bytes instead of 13\n",dwBytesRead);
621     ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
622
623     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
624     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
625     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
626 }
627
628 static void HttpHeaders_test(void)
629 {
630     HINTERNET hSession;
631     HINTERNET hConnect;
632     HINTERNET hRequest;
633     CHAR      buffer[256];
634     DWORD     len = 256;
635     DWORD     index = 0;
636
637     hSession = InternetOpen("Wine Regression Test",
638             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
639     ok( hSession != NULL ,"Unable to open Internet session\n");
640     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
641             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
642             0);
643     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
644     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
645             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
646     ok( hRequest != NULL, "Failed to open request handle\n");
647
648     index = 0;
649     len = sizeof(buffer);
650     strcpy(buffer,"Warning");
651     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
652                buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
653     
654     ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
655             "Failed to add new header\n");
656
657     index = 0;
658     len = sizeof(buffer);
659     strcpy(buffer,"Warning");
660     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
661                 buffer,&len,&index),"Unable to query header\n");
662     ok(index == 1, "Index was not incremented\n");
663     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
664     len = sizeof(buffer);
665     strcpy(buffer,"Warning");
666     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
667                 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
668
669     ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
670             "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
671
672     index = 0;
673     len = sizeof(buffer);
674     strcpy(buffer,"Warning");
675     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
676                 buffer,&len,&index),"Unable to query header\n");
677     ok(index == 1, "Index was not incremented\n");
678     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
679     len = sizeof(buffer);
680     strcpy(buffer,"Warning");
681     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
682                 buffer,&len,&index),"Failed to get second header\n");
683     ok(index == 2, "Index was not incremented\n");
684     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
685     len = sizeof(buffer);
686     strcpy(buffer,"Warning");
687     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
688                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
689
690     ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
691
692     index = 0;
693     len = sizeof(buffer);
694     strcpy(buffer,"Warning");
695     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
696                 buffer,&len,&index),"Unable to query header\n");
697     ok(index == 1, "Index was not incremented\n");
698     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
699     len = sizeof(buffer);
700     strcpy(buffer,"Warning");
701     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
702                 buffer,&len,&index),"Failed to get second header\n");
703     ok(index == 2, "Index was not incremented\n");
704     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
705     len = sizeof(buffer);
706     strcpy(buffer,"Warning");
707     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
708                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
709     
710     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
711
712     index = 0;
713     len = sizeof(buffer);
714     strcpy(buffer,"Warning");
715     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
716                 buffer,&len,&index),"Unable to query header\n");
717     ok(index == 1, "Index was not incremented\n");
718     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
719     len = sizeof(buffer);
720     strcpy(buffer,"Warning");
721     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
722                 buffer,&len,&index),"Failed to get second header\n");
723     ok(index == 2, "Index was not incremented\n");
724     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
725     len = sizeof(buffer);
726     strcpy(buffer,"Warning");
727     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
728                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
729
730     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
731
732     index = 0;
733     len = sizeof(buffer);
734     strcpy(buffer,"Warning");
735     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
736                 buffer,&len,&index),"Unable to query header\n");
737     ok(index == 1, "Index was not incremented\n");
738     ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
739     len = sizeof(buffer);
740     strcpy(buffer,"Warning");
741     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
742     ok(index == 2, "Index was not incremented\n");
743     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
744     len = sizeof(buffer);
745     strcpy(buffer,"Warning");
746     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
747
748     ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
749
750     index = 0;
751     len = sizeof(buffer);
752     strcpy(buffer,"Warning");
753     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
754     ok(index == 1, "Index was not incremented\n");
755     ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
756     len = sizeof(buffer);
757     strcpy(buffer,"Warning");
758     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
759     ok(index == 2, "Index was not incremented\n");
760     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
761     len = sizeof(buffer);
762     strcpy(buffer,"Warning");
763     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
764
765     ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
766
767     index = 0;
768     len = sizeof(buffer);
769     strcpy(buffer,"Warning");
770     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
771     ok(index == 1, "Index was not incremented\n");
772     ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
773     len = sizeof(buffer);
774     strcpy(buffer,"Warning");
775     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
776     ok(index == 2, "Index was not incremented\n");
777     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
778     len = sizeof(buffer);
779     strcpy(buffer,"Warning");
780     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
781
782     ok(HttpAddRequestHeaders(hRequest,"Warning:test7",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE), "HTTP_ADDREQ_FLAG_ADD with HTTP_ADDREQ_FLAG_REPALCE Did not work\n");
783
784     index = 0;
785     len = sizeof(buffer);
786     strcpy(buffer,"Warning");
787     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
788     ok(index == 1, "Index was not incremented\n");
789     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
790     len = sizeof(buffer);
791     strcpy(buffer,"Warning");
792     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
793     ok(index == 2, "Index was not incremented\n");
794     ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
795     len = sizeof(buffer);
796     strcpy(buffer,"Warning");
797     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
798
799     
800     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
801     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
802     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
803 }
804
805
806 START_TEST(http)
807 {
808     InternetReadFile_test(INTERNET_FLAG_ASYNC);
809     InternetReadFile_test(0);
810     InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
811     InternetOpenUrlA_test();
812     InternetTimeFromSystemTimeA_test();
813     InternetTimeFromSystemTimeW_test();
814     InternetTimeToSystemTimeA_test();
815     InternetTimeToSystemTimeW_test();
816     HttpSendRequestEx_test();
817     HttpHeaders_test();
818 }