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