ole32: Fix memory leaks in the storage test.
[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 };
50
51 struct info
52 {
53     enum api function;
54     const struct notification *test;
55     unsigned int count;
56     unsigned int index;
57     HANDLE wait;
58     unsigned int line;
59 };
60
61 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
62 {
63     BOOL status_ok, function_ok;
64     struct info *info = (struct info *)context;
65     unsigned int i = info->index;
66
67     if (status == WINHTTP_CALLBACK_STATUS_HANDLE_CREATED)
68     {
69         DWORD size = sizeof(struct info *);
70         WinHttpQueryOption( handle, WINHTTP_OPTION_CONTEXT_VALUE, &info, &size );
71     }
72     ok(i < info->count, "unexpected notification 0x%08x\n", status);
73     if (i >= info->count) return;
74
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)
78     {
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);
81     }
82     else if (!info->test[i].ignore)
83     {
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)
86         {
87             todo_wine ok(function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function);
88         }
89     }
90     if (status_ok) info->index++;
91     if (status & WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS) SetEvent( info->wait );
92 }
93
94 static const struct notification cache_test[] =
95 {
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, 1 },
107     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED, 0, 1 },
108     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 0, 1 },
109     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1, 1 },
110     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1, 1 }
111 };
112
113 static void setup_test( struct info *info, enum api function, unsigned int line )
114 {
115     info->function = function;
116     info->line = line;
117 }
118
119 static void test_connection_cache( void )
120 {
121     static const WCHAR codeweavers[] = {'w','w','w','.','c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0};
122
123     HANDLE ses, con, req;
124     DWORD size, status;
125     BOOL ret;
126     struct info info, *context = &info;
127
128     info.test  = cache_test;
129     info.count = sizeof(cache_test) / sizeof(cache_test[0]);
130     info.index = 0;
131     info.wait = NULL;
132
133     ses = WinHttpOpen( user_agent, 0, NULL, NULL, 0 );
134     ok(ses != NULL, "failed to open session %u\n", GetLastError());
135
136     WinHttpSetStatusCallback( ses, check_notification, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0 );
137
138     ret = WinHttpSetOption( ses, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
139     ok(ret, "failed to set context value %u\n", GetLastError());
140
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());
144
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());
148
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());
152
153     setup_test( &info, winhttp_receive_response, __LINE__ );
154     ret = WinHttpReceiveResponse( req, NULL );
155     ok(ret, "failed to receive response %u\n", GetLastError());
156
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);
161
162     setup_test( &info, winhttp_close_handle, __LINE__ );
163     WinHttpCloseHandle( req );
164     WinHttpCloseHandle( con );
165     WinHttpCloseHandle( ses );
166
167     Sleep(2000); /* make sure connection is evicted from cache */
168
169     info.index = 0;
170
171     ses = WinHttpOpen( user_agent, 0, NULL, NULL, 0 );
172     ok(ses != NULL, "failed to open session %u\n", GetLastError());
173
174     WinHttpSetStatusCallback( ses, check_notification, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0 );
175
176     ret = WinHttpSetOption( ses, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
177     ok(ret, "failed to set context value %u\n", GetLastError());
178
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());
182
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());
186
187     ret = WinHttpSetOption( req, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
188     ok(ret, "failed to set context value %u\n", GetLastError());
189
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());
193
194     setup_test( &info, winhttp_receive_response, __LINE__ );
195     ret = WinHttpReceiveResponse( req, NULL );
196     ok(ret, "failed to receive response %u\n", GetLastError());
197
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);
202
203     setup_test( &info, winhttp_close_handle, __LINE__ );
204     WinHttpCloseHandle( req );
205     WinHttpCloseHandle( con );
206     WinHttpCloseHandle( ses );
207 }
208
209 static const struct notification redirect_test[] =
210 {
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, 1 },
231     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED, 0, 1 },
232     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 0, 1 },
233     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1, 1 },
234     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1, 1 }
235 };
236
237 static void test_redirect( void )
238 {
239     static const WCHAR codeweavers[] = {'c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0};
240
241     HANDLE ses, con, req;
242     DWORD size, status;
243     BOOL ret;
244     struct info info, *context = &info;
245
246     info.test  = redirect_test;
247     info.count = sizeof(redirect_test) / sizeof(redirect_test[0]);
248     info.index = 0;
249     info.wait = NULL;
250
251     ses = WinHttpOpen( user_agent, 0, NULL, NULL, 0 );
252     ok(ses != NULL, "failed to open session %u\n", GetLastError());
253
254     WinHttpSetStatusCallback( ses, check_notification, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0 );
255
256     ret = WinHttpSetOption( ses, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
257     ok(ret, "failed to set context value %u\n", GetLastError());
258
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());
262
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());
266
267     setup_test( &info, winhttp_send_request, __LINE__ );
268     ret = WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 );
269
270     setup_test( &info, winhttp_receive_response, __LINE__ );
271     ret = WinHttpReceiveResponse( req, NULL );
272     ok(ret, "failed to receive response %u\n", GetLastError());
273
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);
278
279     setup_test( &info, winhttp_close_handle, __LINE__ );
280     WinHttpCloseHandle( req );
281     WinHttpCloseHandle( con );
282     WinHttpCloseHandle( ses );
283 }
284
285 static const struct notification async_test[] =
286 {
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, 1 },
313     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED, 0, 1 },
314     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 0, 1 },
315     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1, 1 },
316     { winhttp_close_handle,     WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1, 1 }
317 };
318
319 static void test_async( void )
320 {
321     static const WCHAR codeweavers[] = {'c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0};
322
323     HANDLE ses, con, req;
324     DWORD size, status;
325     BOOL ret;
326     struct info info, *context = &info;
327     char buffer[1024];
328
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 );
333
334     ses = WinHttpOpen( user_agent, 0, NULL, NULL, WINHTTP_FLAG_ASYNC );
335     ok(ses != NULL, "failed to open session %u\n", GetLastError());
336
337     WinHttpSetStatusCallback( ses, check_notification, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0 );
338
339     ret = WinHttpSetOption( ses, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
340     ok(ret, "failed to set context value %u\n", GetLastError());
341
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());
345
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());
349
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());
353
354     WaitForSingleObject( info.wait, INFINITE );
355
356     setup_test( &info, winhttp_receive_response, __LINE__ );
357     ret = WinHttpReceiveResponse( req, NULL );
358     ok(ret, "failed to receive response %u\n", GetLastError());
359
360     WaitForSingleObject( info.wait, INFINITE );
361
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);
366
367     setup_test( &info, winhttp_query_data, __LINE__ );
368     ret = WinHttpQueryDataAvailable( req, NULL );
369     ok(ret, "failed to query data available %u\n", GetLastError());
370
371     WaitForSingleObject( info.wait, INFINITE );
372
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());
376
377     WaitForSingleObject( info.wait, INFINITE );
378
379     setup_test( &info, winhttp_close_handle, __LINE__ );
380     WinHttpCloseHandle( req );
381     WinHttpCloseHandle( con );
382     WinHttpCloseHandle( ses );
383     CloseHandle( info.wait );
384 }
385
386 START_TEST (notification)
387 {
388     test_connection_cache();
389     test_redirect();
390     Sleep(2000); /* make sure previous connection is evicted from cache */
391     test_async();
392 }