push 862965e7f7bbbfb960006fcded9111ae18c06ef6
[wine/hacks.git] / dlls / winhttp / tests / notification.c
blobde8508adc90d6788f1c450466c99fae96883742d
1 /*
2 * test status notifications
4 * Copyright 2008 Hans Leidekker for CodeWeavers
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.
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.
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
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winhttp.h>
27 #include "wine/test.h"
29 static const WCHAR user_agent[] = {'w','i','n','e','t','e','s','t',0};
31 enum api
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
43 struct notification
45 enum api function; /* api responsible for notification */
46 unsigned int status; /* status received */
47 int todo;
48 int ignore;
51 struct info
53 enum api function;
54 const struct notification *test;
55 unsigned int count;
56 unsigned int index;
57 HANDLE wait;
58 unsigned int line;
61 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
63 BOOL status_ok, function_ok;
64 struct info *info = (struct info *)context;
65 unsigned int i = info->index;
67 if (status == WINHTTP_CALLBACK_STATUS_HANDLE_CREATED)
69 DWORD size = sizeof(struct info *);
70 WinHttpQueryOption( handle, WINHTTP_OPTION_CONTEXT_VALUE, &info, &size );
72 ok(i < info->count, "unexpected notification 0x%08x\n", status);
73 if (i >= info->count) return;
75 status_ok = (info->test[i].status == status);
76 function_ok = (info->test[i].function == info->function);
77 if (!info->test[i].ignore && !info->test[i].todo)
79 ok(status_ok, "%u: expected status 0x%08x got 0x%08x\n", info->line, info->test[i].status, status);
80 ok(function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function);
82 else if (!info->test[i].ignore)
84 todo_wine ok(status_ok, "%u: expected status 0x%08x got 0x%08x\n", info->line, info->test[i].status, status);
85 if (status_ok)
87 todo_wine ok(function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function);
90 if (status_ok) info->index++;
91 if (status & WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS) SetEvent( info->wait );
94 static const struct notification cache_test[] =
96 { winhttp_connect, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, 0 },
97 { winhttp_open_request, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, 0 },
98 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_RESOLVING_NAME, 0 },
99 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_NAME_RESOLVED, 0 },
100 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER, 0 },
101 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER, 0 },
102 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_SENDING_REQUEST, 0 },
103 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_REQUEST_SENT, 0 },
104 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE, 0 },
105 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED, 0 },
106 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION, 0 },
107 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED, 0 },
108 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 0 },
109 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1 },
110 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1 }
113 static void setup_test( struct info *info, enum api function, unsigned int line )
115 info->function = function;
116 info->line = line;
119 static void test_connection_cache( void )
121 static const WCHAR codeweavers[] = {'w','w','w','.','c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0};
123 HANDLE ses, con, req;
124 DWORD size, status;
125 BOOL ret;
126 struct info info, *context = &info;
128 info.test = cache_test;
129 info.count = sizeof(cache_test) / sizeof(cache_test[0]);
130 info.index = 0;
131 info.wait = NULL;
133 ses = WinHttpOpen( user_agent, 0, NULL, NULL, 0 );
134 ok(ses != NULL, "failed to open session %u\n", GetLastError());
136 WinHttpSetStatusCallback( ses, check_notification, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0 );
138 ret = WinHttpSetOption( ses, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
139 ok(ret, "failed to set context value %u\n", GetLastError());
141 setup_test( &info, winhttp_connect, __LINE__ );
142 con = WinHttpConnect( ses, codeweavers, 0, 0 );
143 ok(con != NULL, "failed to open a connection %u\n", GetLastError());
145 setup_test( &info, winhttp_open_request, __LINE__ );
146 req = WinHttpOpenRequest( con, NULL, NULL, NULL, NULL, NULL, 0 );
147 ok(req != NULL, "failed to open a request %u\n", GetLastError());
149 setup_test( &info, winhttp_send_request, __LINE__ );
150 ret = WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 );
151 ok(ret, "failed to send request %u\n", GetLastError());
153 setup_test( &info, winhttp_receive_response, __LINE__ );
154 ret = WinHttpReceiveResponse( req, NULL );
155 ok(ret, "failed to receive response %u\n", GetLastError());
157 size = sizeof(status);
158 ret = WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &status, &size, NULL );
159 ok(ret, "failed unexpectedly %u\n", GetLastError());
160 ok(status == 200, "request failed unexpectedly %u\n", status);
162 setup_test( &info, winhttp_close_handle, __LINE__ );
163 WinHttpCloseHandle( req );
164 WinHttpCloseHandle( con );
165 WinHttpCloseHandle( ses );
167 Sleep(2000); /* make sure connection is evicted from cache */
169 info.index = 0;
171 ses = WinHttpOpen( user_agent, 0, NULL, NULL, 0 );
172 ok(ses != NULL, "failed to open session %u\n", GetLastError());
174 WinHttpSetStatusCallback( ses, check_notification, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0 );
176 ret = WinHttpSetOption( ses, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
177 ok(ret, "failed to set context value %u\n", GetLastError());
179 setup_test( &info, winhttp_connect, __LINE__ );
180 con = WinHttpConnect( ses, codeweavers, 0, 0 );
181 ok(con != NULL, "failed to open a connection %u\n", GetLastError());
183 setup_test( &info, winhttp_open_request, __LINE__ );
184 req = WinHttpOpenRequest( con, NULL, NULL, NULL, NULL, NULL, 0 );
185 ok(req != NULL, "failed to open a request %u\n", GetLastError());
187 ret = WinHttpSetOption( req, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
188 ok(ret, "failed to set context value %u\n", GetLastError());
190 setup_test( &info, winhttp_send_request, __LINE__ );
191 ret = WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 );
192 ok(ret, "failed to send request %u\n", GetLastError());
194 setup_test( &info, winhttp_receive_response, __LINE__ );
195 ret = WinHttpReceiveResponse( req, NULL );
196 ok(ret, "failed to receive response %u\n", GetLastError());
198 size = sizeof(status);
199 ret = WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &status, &size, NULL );
200 ok(ret, "failed unexpectedly %u\n", GetLastError());
201 ok(status == 200, "request failed unexpectedly %u\n", status);
203 setup_test( &info, winhttp_close_handle, __LINE__ );
204 WinHttpCloseHandle( req );
205 WinHttpCloseHandle( con );
206 WinHttpCloseHandle( ses );
209 static const struct notification redirect_test[] =
211 { winhttp_connect, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, 0 },
212 { winhttp_open_request, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, 0 },
213 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_RESOLVING_NAME, 0 },
214 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_NAME_RESOLVED, 0 },
215 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER, 0 },
216 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER, 0 },
217 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_SENDING_REQUEST, 0 },
218 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_REQUEST_SENT, 0 },
219 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE, 0 },
220 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED, 0 },
221 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_REDIRECT, 0 },
222 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESOLVING_NAME, 0 },
223 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_NAME_RESOLVED, 0 },
224 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER, 0 },
225 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER, 0 },
226 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_SENDING_REQUEST, 0 },
227 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_REQUEST_SENT, 0 },
228 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE, 0 },
229 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED, 0 },
230 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION, 0 },
231 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED, 0 },
232 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 0 },
233 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1 },
234 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1 }
237 static void test_redirect( void )
239 static const WCHAR codeweavers[] = {'c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0};
241 HANDLE ses, con, req;
242 DWORD size, status;
243 BOOL ret;
244 struct info info, *context = &info;
246 info.test = redirect_test;
247 info.count = sizeof(redirect_test) / sizeof(redirect_test[0]);
248 info.index = 0;
249 info.wait = NULL;
251 ses = WinHttpOpen( user_agent, 0, NULL, NULL, 0 );
252 ok(ses != NULL, "failed to open session %u\n", GetLastError());
254 WinHttpSetStatusCallback( ses, check_notification, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0 );
256 ret = WinHttpSetOption( ses, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
257 ok(ret, "failed to set context value %u\n", GetLastError());
259 setup_test( &info, winhttp_connect, __LINE__ );
260 con = WinHttpConnect( ses, codeweavers, 0, 0 );
261 ok(con != NULL, "failed to open a connection %u\n", GetLastError());
263 setup_test( &info, winhttp_open_request, __LINE__ );
264 req = WinHttpOpenRequest( con, NULL, NULL, NULL, NULL, NULL, 0 );
265 ok(req != NULL, "failed to open a request %u\n", GetLastError());
267 setup_test( &info, winhttp_send_request, __LINE__ );
268 ret = WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 );
270 setup_test( &info, winhttp_receive_response, __LINE__ );
271 ret = WinHttpReceiveResponse( req, NULL );
272 ok(ret, "failed to receive response %u\n", GetLastError());
274 size = sizeof(status);
275 ret = WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &status, &size, NULL );
276 ok(ret, "failed unexpectedly %u\n", GetLastError());
277 ok(status == 200, "request failed unexpectedly %u\n", status);
279 setup_test( &info, winhttp_close_handle, __LINE__ );
280 WinHttpCloseHandle( req );
281 WinHttpCloseHandle( con );
282 WinHttpCloseHandle( ses );
285 static const struct notification async_test[] =
287 { winhttp_connect, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, 0 },
288 { winhttp_open_request, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, 0 },
289 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_RESOLVING_NAME, 0 },
290 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_NAME_RESOLVED, 0 },
291 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER, 0 },
292 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER, 0 },
293 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_SENDING_REQUEST, 0 },
294 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_REQUEST_SENT, 0 },
295 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE, 0 },
296 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE, 0 },
297 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED, 0 },
298 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_REDIRECT, 0 },
299 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESOLVING_NAME, 0 },
300 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_NAME_RESOLVED, 0 },
301 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER, 0 },
302 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER, 0 },
303 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_SENDING_REQUEST, 0 },
304 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_REQUEST_SENT, 0 },
305 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE, 0 },
306 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED, 0 },
307 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE, 0 },
308 { winhttp_query_data, WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE, 0 },
309 { winhttp_read_data, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE, 0, 1 },
310 { winhttp_read_data, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED, 0, 1 },
311 { winhttp_read_data, WINHTTP_CALLBACK_STATUS_READ_COMPLETE, 0, 1 },
312 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION, 0 },
313 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED, 0 },
314 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 0 },
315 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1 },
316 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1 }
319 static void test_async( void )
321 static const WCHAR codeweavers[] = {'c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0};
323 HANDLE ses, con, req;
324 DWORD size, status;
325 BOOL ret;
326 struct info info, *context = &info;
327 char buffer[1024];
329 info.test = async_test;
330 info.count = sizeof(async_test) / sizeof(async_test[0]);
331 info.index = 0;
332 info.wait = CreateEvent( NULL, FALSE, FALSE, NULL );
334 ses = WinHttpOpen( user_agent, 0, NULL, NULL, WINHTTP_FLAG_ASYNC );
335 ok(ses != NULL, "failed to open session %u\n", GetLastError());
337 WinHttpSetStatusCallback( ses, check_notification, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0 );
339 ret = WinHttpSetOption( ses, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
340 ok(ret, "failed to set context value %u\n", GetLastError());
342 setup_test( &info, winhttp_connect, __LINE__ );
343 con = WinHttpConnect( ses, codeweavers, 0, 0 );
344 ok(con != NULL, "failed to open a connection %u\n", GetLastError());
346 setup_test( &info, winhttp_open_request, __LINE__ );
347 req = WinHttpOpenRequest( con, NULL, NULL, NULL, NULL, NULL, 0 );
348 ok(req != NULL, "failed to open a request %u\n", GetLastError());
350 setup_test( &info, winhttp_send_request, __LINE__ );
351 ret = WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 );
352 ok(ret, "failed to send request %u\n", GetLastError());
354 WaitForSingleObject( info.wait, INFINITE );
356 setup_test( &info, winhttp_receive_response, __LINE__ );
357 ret = WinHttpReceiveResponse( req, NULL );
358 ok(ret, "failed to receive response %u\n", GetLastError());
360 WaitForSingleObject( info.wait, INFINITE );
362 size = sizeof(status);
363 ret = WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &status, &size, NULL );
364 ok(ret, "failed unexpectedly %u\n", GetLastError());
365 ok(status == 200, "request failed unexpectedly %u\n", status);
367 setup_test( &info, winhttp_query_data, __LINE__ );
368 ret = WinHttpQueryDataAvailable( req, NULL );
369 ok(ret, "failed to query data available %u\n", GetLastError());
371 WaitForSingleObject( info.wait, INFINITE );
373 setup_test( &info, winhttp_read_data, __LINE__ );
374 ret = WinHttpReadData( req, buffer, sizeof(buffer), NULL );
375 ok(ret, "failed to query data available %u\n", GetLastError());
377 WaitForSingleObject( info.wait, INFINITE );
379 setup_test( &info, winhttp_close_handle, __LINE__ );
380 WinHttpCloseHandle( req );
381 WinHttpCloseHandle( con );
382 WinHttpCloseHandle( ses );
383 CloseHandle( info.wait );
386 START_TEST (notification)
388 test_connection_cache();
389 test_redirect();
390 Sleep(2000); /* make sure previous connection is evicted from cache */
391 test_async();