crypt32: Add additional path for Solaris 11 Express.
[wine] / dlls / winhttp / tests / notification.c
1 /*
2  * test status notifications
3  *
4  * Copyright 2008 Hans Leidekker for CodeWeavers
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winhttp.h>
26
27 #include "wine/test.h"
28
29 static const WCHAR user_agent[] = {'w','i','n','e','t','e','s','t',0};
30
31 enum api
32 {
33     winhttp_connect = 1,
34     winhttp_open_request,
35     winhttp_send_request,
36     winhttp_receive_response,
37     winhttp_query_data,
38     winhttp_read_data,
39     winhttp_write_data,
40     winhttp_close_handle
41 };
42
43 struct notification
44 {
45     enum api function;      /* api responsible for notification */
46     unsigned int status;    /* status received */
47     int todo;
48     int ignore;
49     int skipped_for_proxy;
50 };
51
52 struct info
53 {
54     enum api function;
55     const struct notification *test;
56     unsigned int count;
57     unsigned int index;
58     HANDLE wait;
59     unsigned int line;
60 };
61
62 static BOOL proxy_active(void)
63 {
64     WINHTTP_PROXY_INFO proxy_info;
65     BOOL active = FALSE;
66
67     if (WinHttpGetDefaultProxyConfiguration(&proxy_info))
68     {
69         active = (proxy_info.lpszProxy != NULL);
70         if (active)
71             GlobalFree((HGLOBAL) proxy_info.lpszProxy);
72         if (proxy_info.lpszProxyBypass != NULL)
73             GlobalFree((HGLOBAL) proxy_info.lpszProxyBypass);
74     }
75     else
76        active = FALSE;
77
78     return active;
79 }
80
81 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
82 {
83     BOOL status_ok, function_ok;
84     struct info *info = (struct info *)context;
85     unsigned int i = info->index;
86
87     if (status == WINHTTP_CALLBACK_STATUS_HANDLE_CREATED)
88     {
89         DWORD size = sizeof(struct info *);
90         WinHttpQueryOption( handle, WINHTTP_OPTION_CONTEXT_VALUE, &info, &size );
91     }
92     ok(i < info->count, "unexpected notification 0x%08x\n", status);
93     if (i >= info->count) return;
94
95     status_ok   = (info->test[i].status == status);
96     function_ok = (info->test[i].function == info->function);
97     if (!info->test[i].ignore && !info->test[i].todo)
98     {
99         ok(status_ok, "%u: expected status 0x%08x got 0x%08x\n", info->line, info->test[i].status, status);
100         ok(function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function);
101     }
102     else if (!info->test[i].ignore)
103     {
104         todo_wine ok(status_ok, "%u: expected status 0x%08x got 0x%08x\n", info->line, info->test[i].status, status);
105         if (status_ok)
106         {
107             todo_wine ok(function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function);
108         }
109     }
110     if (status_ok) info->index++;
111     if (proxy_active())
112     {
113         while (info->test[info->index].skipped_for_proxy)
114             info->index++;
115     }
116
117     if (status & (WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS | WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING))
118     {
119         SetEvent( info->wait );
120     }
121 }
122
123 static const struct notification cache_test[] =
124 {
125     { winhttp_connect,          WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, 0 },
126     { winhttp_open_request,     WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, 0 },
127     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_RESOLVING_NAME, 0 },
128     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_NAME_RESOLVED, 0 },
129     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER, 0 },
130     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER, 0 },
131     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_SENDING_REQUEST, 0 },
132     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_REQUEST_SENT, 0 },
133     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE, 0 },
134     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED, 0 },
135     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION, 0, 1 },
136     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED, 0, 1 },
137     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 0, 1 },
138     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1, 1 },
139     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1, 1 }
140 };
141
142 static void setup_test( struct info *info, enum api function, unsigned int line )
143 {
144     info->function = function;
145     info->line = line;
146 }
147
148 static void test_connection_cache( void )
149 {
150     static const WCHAR codeweavers[] = {'w','w','w','.','c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0};
151
152     HANDLE ses, con, req;
153     DWORD size, status;
154     BOOL ret;
155     struct info info, *context = &info;
156
157     info.test  = cache_test;
158     info.count = sizeof(cache_test) / sizeof(cache_test[0]);
159     info.index = 0;
160     info.wait = NULL;
161
162     ses = WinHttpOpen( user_agent, 0, NULL, NULL, 0 );
163     ok(ses != NULL, "failed to open session %u\n", GetLastError());
164
165     WinHttpSetStatusCallback( ses, check_notification, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0 );
166
167     ret = WinHttpSetOption( ses, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
168     ok(ret, "failed to set context value %u\n", GetLastError());
169
170     setup_test( &info, winhttp_connect, __LINE__ );
171     con = WinHttpConnect( ses, codeweavers, 0, 0 );
172     ok(con != NULL, "failed to open a connection %u\n", GetLastError());
173
174     setup_test( &info, winhttp_open_request, __LINE__ );
175     req = WinHttpOpenRequest( con, NULL, NULL, NULL, NULL, NULL, 0 );
176     ok(req != NULL, "failed to open a request %u\n", GetLastError());
177
178     setup_test( &info, winhttp_send_request, __LINE__ );
179     ret = WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 );
180     ok(ret, "failed to send request %u\n", GetLastError());
181
182     setup_test( &info, winhttp_receive_response, __LINE__ );
183     ret = WinHttpReceiveResponse( req, NULL );
184     ok(ret, "failed to receive response %u\n", GetLastError());
185
186     size = sizeof(status);
187     ret = WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &status, &size, NULL );
188     ok(ret, "failed unexpectedly %u\n", GetLastError());
189     ok(status == 200, "request failed unexpectedly %u\n", status);
190
191     setup_test( &info, winhttp_close_handle, __LINE__ );
192     WinHttpCloseHandle( req );
193     WinHttpCloseHandle( con );
194     WinHttpCloseHandle( ses );
195
196     Sleep(2000); /* make sure connection is evicted from cache */
197
198     info.index = 0;
199
200     ses = WinHttpOpen( user_agent, 0, NULL, NULL, 0 );
201     ok(ses != NULL, "failed to open session %u\n", GetLastError());
202
203     WinHttpSetStatusCallback( ses, check_notification, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0 );
204
205     ret = WinHttpSetOption( ses, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
206     ok(ret, "failed to set context value %u\n", GetLastError());
207
208     setup_test( &info, winhttp_connect, __LINE__ );
209     con = WinHttpConnect( ses, codeweavers, 0, 0 );
210     ok(con != NULL, "failed to open a connection %u\n", GetLastError());
211
212     setup_test( &info, winhttp_open_request, __LINE__ );
213     req = WinHttpOpenRequest( con, NULL, NULL, NULL, NULL, NULL, 0 );
214     ok(req != NULL, "failed to open a request %u\n", GetLastError());
215
216     ret = WinHttpSetOption( req, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
217     ok(ret, "failed to set context value %u\n", GetLastError());
218
219     setup_test( &info, winhttp_send_request, __LINE__ );
220     ret = WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 );
221     ok(ret, "failed to send request %u\n", GetLastError());
222
223     setup_test( &info, winhttp_receive_response, __LINE__ );
224     ret = WinHttpReceiveResponse( req, NULL );
225     ok(ret, "failed to receive response %u\n", GetLastError());
226
227     size = sizeof(status);
228     ret = WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &status, &size, NULL );
229     ok(ret, "failed unexpectedly %u\n", GetLastError());
230     ok(status == 200, "request failed unexpectedly %u\n", status);
231
232     setup_test( &info, winhttp_close_handle, __LINE__ );
233     WinHttpCloseHandle( req );
234     WinHttpCloseHandle( con );
235     WinHttpCloseHandle( ses );
236 }
237
238 static const struct notification redirect_test[] =
239 {
240     { winhttp_connect,          WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, 0 },
241     { winhttp_open_request,     WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, 0 },
242     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_RESOLVING_NAME, 0 },
243     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_NAME_RESOLVED, 0 },
244     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER, 0 },
245     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER, 0 },
246     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_SENDING_REQUEST, 0 },
247     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_REQUEST_SENT, 0 },
248     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE, 0 },
249     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED, 0 },
250     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_REDIRECT, 0 },
251     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESOLVING_NAME, 0, 0, 1 },
252     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_NAME_RESOLVED, 0, 0, 1 },
253     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER, 0, 0, 1 },
254     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER, 0, 0, 1 },
255     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_SENDING_REQUEST, 0 },
256     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_REQUEST_SENT, 0 },
257     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE, 0 },
258     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED, 0 },
259     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION, 0, 1 },
260     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED, 0, 1 },
261     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 0, 1 },
262     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1, 1 },
263     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1, 1 }
264 };
265
266 static void test_redirect( void )
267 {
268     static const WCHAR codeweavers[] = {'c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0};
269
270     HANDLE ses, con, req;
271     DWORD size, status;
272     BOOL ret;
273     struct info info, *context = &info;
274
275     info.test  = redirect_test;
276     info.count = sizeof(redirect_test) / sizeof(redirect_test[0]);
277     info.index = 0;
278     info.wait = NULL;
279
280     ses = WinHttpOpen( user_agent, 0, NULL, NULL, 0 );
281     ok(ses != NULL, "failed to open session %u\n", GetLastError());
282
283     WinHttpSetStatusCallback( ses, check_notification, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0 );
284
285     ret = WinHttpSetOption( ses, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
286     ok(ret, "failed to set context value %u\n", GetLastError());
287
288     setup_test( &info, winhttp_connect, __LINE__ );
289     con = WinHttpConnect( ses, codeweavers, 0, 0 );
290     ok(con != NULL, "failed to open a connection %u\n", GetLastError());
291
292     setup_test( &info, winhttp_open_request, __LINE__ );
293     req = WinHttpOpenRequest( con, NULL, NULL, NULL, NULL, NULL, 0 );
294     ok(req != NULL, "failed to open a request %u\n", GetLastError());
295
296     setup_test( &info, winhttp_send_request, __LINE__ );
297     ret = WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 );
298     ok(ret, "failed to send request %u\n", GetLastError());
299
300     setup_test( &info, winhttp_receive_response, __LINE__ );
301     ret = WinHttpReceiveResponse( req, NULL );
302     ok(ret, "failed to receive response %u\n", GetLastError());
303
304     size = sizeof(status);
305     ret = WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &status, &size, NULL );
306     ok(ret, "failed unexpectedly %u\n", GetLastError());
307     ok(status == 200, "request failed unexpectedly %u\n", status);
308
309     setup_test( &info, winhttp_close_handle, __LINE__ );
310     WinHttpCloseHandle( req );
311     WinHttpCloseHandle( con );
312     WinHttpCloseHandle( ses );
313 }
314
315 static const struct notification async_test[] =
316 {
317     { winhttp_connect,          WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, 0 },
318     { winhttp_open_request,     WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, 0 },
319     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_RESOLVING_NAME, 0 },
320     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_NAME_RESOLVED, 0 },
321     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER, 0 },
322     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER, 0 },
323     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_SENDING_REQUEST, 0 },
324     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_REQUEST_SENT, 0 },
325     { winhttp_send_request,     WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE, 0 },
326     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE, 0 },
327     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED, 0 },
328     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_REDIRECT, 0 },
329     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESOLVING_NAME, 0, 0, 1 },
330     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_NAME_RESOLVED, 0, 0, 1 },
331     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER, 0, 0, 1 },
332     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER, 0, 0, 1 },
333     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_SENDING_REQUEST, 0 },
334     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_REQUEST_SENT, 0 },
335     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE, 0 },
336     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED, 0 },
337     { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE, 0 },
338     { winhttp_query_data,       WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE, 0 },
339     { winhttp_read_data,        WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE, 0, 1 },
340     { winhttp_read_data,        WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED, 0, 1 },
341     { winhttp_read_data,        WINHTTP_CALLBACK_STATUS_READ_COMPLETE, 0, 1 },
342     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION, 0, 1 },
343     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED, 0, 1 },
344     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 0, 1 },
345     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1, 1 },
346     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1, 1 }
347 };
348
349 static void test_async( void )
350 {
351     static const WCHAR codeweavers[] = {'c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0};
352
353     HANDLE ses, con, req;
354     DWORD size, status;
355     BOOL ret;
356     struct info info, *context = &info;
357     char buffer[1024];
358
359     info.test  = async_test;
360     info.count = sizeof(async_test) / sizeof(async_test[0]);
361     info.index = 0;
362     info.wait = CreateEvent( NULL, FALSE, FALSE, NULL );
363
364     ses = WinHttpOpen( user_agent, 0, NULL, NULL, WINHTTP_FLAG_ASYNC );
365     ok(ses != NULL, "failed to open session %u\n", GetLastError());
366
367     WinHttpSetStatusCallback( ses, check_notification, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0 );
368
369     ret = WinHttpSetOption( ses, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
370     ok(ret, "failed to set context value %u\n", GetLastError());
371
372     setup_test( &info, winhttp_connect, __LINE__ );
373     con = WinHttpConnect( ses, codeweavers, 0, 0 );
374     ok(con != NULL, "failed to open a connection %u\n", GetLastError());
375
376     setup_test( &info, winhttp_open_request, __LINE__ );
377     req = WinHttpOpenRequest( con, NULL, NULL, NULL, NULL, NULL, 0 );
378     ok(req != NULL, "failed to open a request %u\n", GetLastError());
379
380     setup_test( &info, winhttp_send_request, __LINE__ );
381     ret = WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 );
382     ok(ret, "failed to send request %u\n", GetLastError());
383
384     WaitForSingleObject( info.wait, INFINITE );
385
386     setup_test( &info, winhttp_receive_response, __LINE__ );
387     ret = WinHttpReceiveResponse( req, NULL );
388     ok(ret, "failed to receive response %u\n", GetLastError());
389
390     WaitForSingleObject( info.wait, INFINITE );
391
392     size = sizeof(status);
393     ret = WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &status, &size, NULL );
394     ok(ret, "failed unexpectedly %u\n", GetLastError());
395     ok(status == 200, "request failed unexpectedly %u\n", status);
396
397     setup_test( &info, winhttp_query_data, __LINE__ );
398     ret = WinHttpQueryDataAvailable( req, NULL );
399     ok(ret, "failed to query data available %u\n", GetLastError());
400
401     WaitForSingleObject( info.wait, INFINITE );
402
403     setup_test( &info, winhttp_read_data, __LINE__ );
404     ret = WinHttpReadData( req, buffer, sizeof(buffer), NULL );
405     ok(ret, "failed to query data available %u\n", GetLastError());
406
407     WaitForSingleObject( info.wait, INFINITE );
408
409     setup_test( &info, winhttp_close_handle, __LINE__ );
410     WinHttpCloseHandle( req );
411     WinHttpCloseHandle( con );
412     WinHttpCloseHandle( ses );
413
414     WaitForSingleObject( info.wait, INFINITE );
415     CloseHandle( info.wait );
416 }
417
418 START_TEST (notification)
419 {
420     test_connection_cache();
421     test_redirect();
422     Sleep(2000); /* make sure previous connection is evicted from cache */
423     test_async();
424 }