winhttp: Add status notification tests. Make them pass.
[wine/multimedia.git] / dlls / winhttp / tests / notification.c
blobda605b024ad4c072bc7fe8aad013193ec440c7bb
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_close_handle
40 struct notification
42 enum api function; /* api responsible for notification */
43 unsigned int status; /* status received */
44 int todo;
47 struct info
49 enum api function;
50 const struct notification *test;
51 unsigned int count;
52 unsigned int index;
55 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
57 struct info *info = (struct info *)context;
58 unsigned int i = info->index;
60 if (status == WINHTTP_CALLBACK_STATUS_HANDLE_CREATED)
62 DWORD size = sizeof(struct info *);
63 WinHttpQueryOption( handle, WINHTTP_OPTION_CONTEXT_VALUE, &info, &size );
65 ok(i < info->count, "unexpected notification 0x%08x\n", status);
66 if (i >= info->count) return;
67 if (!info->test[i].todo)
69 ok(info->test[i].status == status, "expected status 0x%08x got 0x%08x\n", info->test[i].status, status);
70 ok(info->test[i].function == info->function, "expected function %u got %u\n", info->test[i].function, info->function);
72 else todo_wine
74 ok(info->test[i].status == status, "expected status 0x%08x got 0x%08x\n", info->test[i].status, status);
75 ok(info->test[i].function == info->function, "expected function %u got %u\n", info->test[i].function, info->function);
77 info->index++;
80 static const struct notification cache_test[] =
82 { winhttp_connect, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, 0 },
83 { winhttp_open_request, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, 0 },
84 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_RESOLVING_NAME, 0 },
85 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_NAME_RESOLVED, 0 },
86 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER, 0 },
87 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER, 0 },
88 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_SENDING_REQUEST, 0 },
89 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_REQUEST_SENT, 0 },
90 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE, 0 },
91 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED, 0 },
92 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION, 0 },
93 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED, 0 },
94 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 0 },
95 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1 },
96 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1 }
99 static void test_connection_cache( void )
101 static const WCHAR codeweavers[] = {'w','w','w','.','c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0};
103 HANDLE ses, con, req;
104 DWORD size, status;
105 BOOL ret;
106 struct info info, *context = &info;
108 info.test = cache_test;
109 info.count = sizeof(cache_test) / sizeof(cache_test[0]);
110 info.index = 0;
112 ses = WinHttpOpen( user_agent, 0, NULL, NULL, 0 );
113 ok(ses != NULL, "failed to open session %u\n", GetLastError());
115 WinHttpSetStatusCallback( ses, check_notification, WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS | WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0 );
117 ret = WinHttpSetOption( ses, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
118 ok(ret, "failed to set context value %u\n", GetLastError());
120 info.function = winhttp_connect;
121 con = WinHttpConnect( ses, codeweavers, 0, 0 );
122 ok(con != NULL, "failed to open a connection %u\n", GetLastError());
124 info.function = winhttp_open_request;
125 req = WinHttpOpenRequest( con, NULL, NULL, NULL, NULL, NULL, 0 );
126 ok(req != NULL, "failed to open a request %u\n", GetLastError());
128 info.function = winhttp_send_request;
129 ret = WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 );
130 ok(ret, "failed to send request %u\n", GetLastError());
132 info.function = winhttp_receive_response;
133 ret = WinHttpReceiveResponse( req, NULL );
134 ok(ret, "failed to receive response %u\n", GetLastError());
136 size = sizeof(status);
137 ret = WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &status, &size, NULL );
138 ok(ret, "failed unexpectedly %u\n", GetLastError());
139 ok(status == 200, "request failed unexpectedly %u\n", status);
141 info.function = winhttp_close_handle;
142 WinHttpCloseHandle( req );
143 WinHttpCloseHandle( con );
144 WinHttpCloseHandle( ses );
146 Sleep(2000); /* make sure connection is evicted from cache */
148 info.index = 0;
150 ses = WinHttpOpen( user_agent, 0, NULL, NULL, 0 );
151 ok(ses != NULL, "failed to open session %u\n", GetLastError());
153 WinHttpSetStatusCallback( ses, check_notification, WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS | WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0 );
155 ret = WinHttpSetOption( ses, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
156 ok(ret, "failed to set context value %u\n", GetLastError());
158 info.function = winhttp_connect;
159 con = WinHttpConnect( ses, codeweavers, 0, 0 );
160 ok(con != NULL, "failed to open a connection %u\n", GetLastError());
162 info.function = winhttp_open_request;
163 req = WinHttpOpenRequest( con, NULL, NULL, NULL, NULL, NULL, 0 );
164 ok(req != NULL, "failed to open a request %u\n", GetLastError());
166 ret = WinHttpSetOption( req, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
167 ok(ret, "failed to set context value %u\n", GetLastError());
169 info.function = winhttp_send_request;
170 ret = WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 );
171 ok(ret, "failed to send request %u\n", GetLastError());
173 info.function = winhttp_receive_response;
174 ret = WinHttpReceiveResponse( req, NULL );
175 ok(ret, "failed to receive response %u\n", GetLastError());
177 size = sizeof(status);
178 ret = WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &status, &size, NULL );
179 ok(ret, "failed unexpectedly %u\n", GetLastError());
180 ok(status == 200, "request failed unexpectedly %u\n", status);
182 info.function = winhttp_close_handle;
183 WinHttpCloseHandle( req );
184 WinHttpCloseHandle( con );
185 WinHttpCloseHandle( ses );
188 static const struct notification redirect_test[] =
190 { winhttp_connect, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, 0 },
191 { winhttp_open_request, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, 0 },
192 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_RESOLVING_NAME, 0 },
193 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_NAME_RESOLVED, 0 },
194 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER, 0 },
195 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER, 0 },
196 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_SENDING_REQUEST, 0 },
197 { winhttp_send_request, WINHTTP_CALLBACK_STATUS_REQUEST_SENT, 0 },
198 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE, 0 },
199 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED, 0 },
200 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_REDIRECT, 0 },
201 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESOLVING_NAME, 0 },
202 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_NAME_RESOLVED, 0 },
203 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER, 0 },
204 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER, 0 },
205 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_SENDING_REQUEST, 0 },
206 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_REQUEST_SENT, 0 },
207 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE, 0 },
208 { winhttp_receive_response, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED, 0 },
209 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION, 0 },
210 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED, 0 },
211 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 0 },
212 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1 },
213 { winhttp_close_handle, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, 1 }
216 static void test_redirect( void )
218 static const WCHAR codeweavers[] = {'c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0};
220 HANDLE ses, con, req;
221 DWORD size, status;
222 BOOL ret;
223 struct info info, *context = &info;
225 info.test = redirect_test;
226 info.count = sizeof(redirect_test) / sizeof(redirect_test[0]);
227 info.index = 0;
229 ses = WinHttpOpen( user_agent, 0, NULL, NULL, 0 );
230 ok(ses != NULL, "failed to open session %u\n", GetLastError());
232 WinHttpSetStatusCallback( ses, check_notification, WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS | WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0 );
234 ret = WinHttpSetOption( ses, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(struct info *) );
235 ok(ret, "failed to set context value %u\n", GetLastError());
237 info.function = winhttp_connect;
238 con = WinHttpConnect( ses, codeweavers, 0, 0 );
239 ok(con != NULL, "failed to open a connection %u\n", GetLastError());
241 info.function = winhttp_open_request;
242 req = WinHttpOpenRequest( con, NULL, NULL, NULL, NULL, NULL, 0 );
243 ok(req != NULL, "failed to open a request %u\n", GetLastError());
245 info.function = winhttp_send_request;
246 ret = WinHttpSendRequest( req, NULL, 0, NULL, 0, 0, 0 );
248 info.function = winhttp_receive_response;
249 ret = WinHttpReceiveResponse( req, NULL );
250 ok(ret, "failed to receive response %u\n", GetLastError());
252 size = sizeof(status);
253 ret = WinHttpQueryHeaders( req, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &status, &size, NULL );
254 ok(ret, "failed unexpectedly %u\n", GetLastError());
255 ok(status == 200, "request failed unexpectedly %u\n", status);
257 info.function = winhttp_close_handle;
258 WinHttpCloseHandle( req );
259 WinHttpCloseHandle( con );
260 WinHttpCloseHandle( ses );
263 START_TEST (notification)
265 test_connection_cache();
266 test_redirect();