push ada2d4f150af430b0cf01adcbe8b6d3e34deeeb1
[wine/hacks.git] / dlls / winhttp / tests / url.c
blob5d88614bed0e88bc516a83fe8f2f6cef4e9bb250
1 /*
2 * Copyright 2008 Hans Leidekker
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winnls.h"
24 #include "winhttp.h"
26 #include "wine/test.h"
28 static WCHAR empty[] = {0};
29 static WCHAR ftp[] = {'f','t','p',0};
30 static WCHAR http[] = {'h','t','t','p',0};
31 static WCHAR winehq[] = {'w','w','w','.','w','i','n','e','h','q','.','o','r','g',0};
32 static WCHAR username[] = {'u','s','e','r','n','a','m','e',0};
33 static WCHAR password[] = {'p','a','s','s','w','o','r','d',0};
34 static WCHAR about[] = {'/','s','i','t','e','/','a','b','o','u','t',0};
35 static WCHAR query[] = {'?','q','u','e','r','y',0};
36 static WCHAR escape[] = {' ','!','"','#','$','%','&','\'','(',')','*','+',',','-','.','/',':',';','<','=','>','?','@','[','\\',']','^','_','`','{','|','}','~',0};
38 static const WCHAR url1[] =
39 {'h','t','t','p',':','/','/','u','s','e','r','n','a','m','e',':','p','a','s','s','w','o','r','d',
40 '@','w','w','w','.','w','i','n','e','h','q','.','o','r','g','/','s','i','t','e','/','a','b','o','u','t','?','q','u','e','r','y',0};
41 static const WCHAR url2[] =
42 {'h','t','t','p',':','/','/','u','s','e','r','n','a','m','e',
43 '@','w','w','w','.','w','i','n','e','h','q','.','o','r','g','/','s','i','t','e','/','a','b','o','u','t','?','q','u','e','r','y',0};
44 static const WCHAR url3[] = {'h','t','t','p',':','/','/','u','s','e','r','n','a','m','e',':',0};
45 static const WCHAR url4[] =
46 {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q','.','o','r','g','/','s','i','t','e','/','a','b','o','u','t','?','q','u','e','r','y',0};
47 static const WCHAR url5[] = {'h','t','t','p',':','/','/',0};
48 static const WCHAR url6[] =
49 {'f','t','p',':','/','/','u','s','e','r','n','a','m','e',':','p','a','s','s','w','o','r','d',
50 '@','w','w','w','.','w','i','n','e','h','q','.','o','r','g',':','8','0','/','s','i','t','e','/','a','b','o','u','t','?','q','u','e','r','y',0};
51 static const WCHAR url7[] =
52 {'h','t','t','p',':','/','/','u','s','e','r','n','a','m','e',':','p','a','s','s','w','o','r','d',
53 '@','w','w','w','.','w','i','n','e','h','q','.','o','r','g',':','4','2','/','s','i','t','e','/','a','b','o','u','t','?','q','u','e','r','y',0};
54 static const WCHAR url8[] =
55 {'h','t','t','p',':','/','/','u','s','e','r','n','a','m','e',':','p','a','s','s','w','o','r','d',
56 '@','w','w','w','.','w','i','n','e','h','q','.','o','r','g','/','s','i','t','e','/','a','b','o','u','t',
57 '%','2','0','!','%','2','2','%','2','3','$','%','2','5','&','\'','(',')','*','+',',','-','.','/',':',';','%','3','C','=','%','3','E','?','@','%',
58 '5','B','%','5','C','%','5','D','%','5','E','_','%','6','0','%','7','B','%','7','C','%','7','D','%','7','E',0};
59 static const WCHAR url9[] =
60 {'h','t','t','p',':','/','/','u','s','e','r','n','a','m','e',':','p','a','s','s','w','o','r','d',
61 '@','w','w','w','.','w','i','n','e','h','q','.','o','r','g',':','0','/','s','i','t','e','/','a','b','o','u','t','?','q','u','e','r','y',0};
63 static const WCHAR url_k1[] =
64 {'h','t','t','p',':','/','/','u','s','e','r','n','a','m','e',':','p','a','s','s','w','o','r','d',
65 '@','w','w','w','.','w','i','n','e','h','q','.','o','r','g','/','s','i','t','e','/','a','b','o','u','t',0};
67 static const WCHAR url_k2[] =
68 {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q','.','o','r','g',0};
70 static void fill_url_components( URL_COMPONENTS *uc )
72 uc->dwStructSize = sizeof(URL_COMPONENTS);
73 uc->lpszScheme = http;
74 uc->dwSchemeLength = lstrlenW( uc->lpszScheme );
75 uc->nScheme = INTERNET_SCHEME_HTTP;
76 uc->lpszHostName = winehq;
77 uc->dwHostNameLength = lstrlenW( uc->lpszHostName );
78 uc->nPort = 80;
79 uc->lpszUserName = username;
80 uc->dwUserNameLength = lstrlenW( uc->lpszUserName );
81 uc->lpszPassword = password;
82 uc->dwPasswordLength = lstrlenW( uc->lpszPassword );
83 uc->lpszUrlPath = about;
84 uc->dwUrlPathLength = lstrlenW( uc->lpszUrlPath );
85 uc->lpszExtraInfo = query;
86 uc->dwExtraInfoLength = lstrlenW( uc->lpszExtraInfo );
89 static void WinHttpCreateUrl_test( void )
91 URL_COMPONENTS uc;
92 WCHAR *url;
93 DWORD len;
94 BOOL ret;
96 /* NULL components */
97 len = ~0UL;
98 SetLastError( 0xdeadbeef );
99 ret = WinHttpCreateUrl( NULL, 0, NULL, &len );
100 ok( !ret, "expected failure\n" );
101 ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", GetLastError() );
102 ok( len == ~0UL, "expected len ~0UL got %u\n", len );
104 /* zero'ed components */
105 memset( &uc, 0, sizeof(URL_COMPONENTS) );
106 SetLastError( 0xdeadbeef );
107 ret = WinHttpCreateUrl( &uc, 0, NULL, &len );
108 ok( !ret, "expected failure\n" );
109 ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", GetLastError() );
110 ok( len == ~0UL, "expected len ~0UL got %u\n", len );
112 /* valid components, NULL url, NULL length */
113 fill_url_components( &uc );
114 SetLastError( 0xdeadbeef );
115 ret = WinHttpCreateUrl( &uc, 0, NULL, NULL );
116 ok( !ret, "expected failure\n" );
117 ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", GetLastError() );
118 ok( len == ~0UL, "expected len ~0UL got %u\n", len );
120 /* valid components, NULL url */
121 SetLastError( 0xdeadbeef );
122 ret = WinHttpCreateUrl( &uc, 0, NULL, &len );
123 ok( !ret, "expected failure\n" );
124 ok( GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError() );
125 ok( len == 57, "expected len 57 got %u\n", len );
127 /* correct size, NULL url */
128 fill_url_components( &uc );
129 SetLastError( 0xdeadbeef );
130 ret = WinHttpCreateUrl( &uc, 0, NULL, &len );
131 ok( !ret, "expected failure\n" );
132 ok( GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError() );
133 ok( len == 57, "expected len 57 got %u\n", len );
135 /* valid components, allocated url, short length */
136 SetLastError( 0xdeadbeef );
137 url = HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(WCHAR) );
138 url[0] = 0;
139 len = 2;
140 ret = WinHttpCreateUrl( &uc, 0, url, &len );
141 ok( !ret, "expected failure\n" );
142 ok( GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError() );
143 ok( len == 57, "expected len 57 got %u\n", len );
145 /* allocated url, NULL scheme */
146 uc.lpszScheme = NULL;
147 url[0] = 0;
148 len = 256;
149 ret = WinHttpCreateUrl( &uc, 0, url, &len );
150 ok( ret, "expected success\n" );
151 ok( len == 56, "expected len 56 got %u\n", len );
152 ok( !lstrcmpW( url, url1 ), "url doesn't match\n" );
154 /* allocated url, 0 scheme */
155 fill_url_components( &uc );
156 uc.nScheme = 0;
157 url[0] = 0;
158 len = 256;
159 ret = WinHttpCreateUrl( &uc, 0, url, &len );
160 ok( ret, "expected success\n" );
161 ok( len == 56, "expected len 56 got %u\n", len );
163 /* valid components, allocated url */
164 fill_url_components( &uc );
165 url[0] = 0;
166 len = 256;
167 ret = WinHttpCreateUrl( &uc, 0, url, &len );
168 ok( ret, "expected success\n" );
169 ok( len == 56, "expected len 56 got %d\n", len );
170 ok( !lstrcmpW( url, url1 ), "url doesn't match\n" );
172 /* valid username, NULL password */
173 fill_url_components( &uc );
174 uc.lpszPassword = NULL;
175 url[0] = 0;
176 len = 256;
177 ret = WinHttpCreateUrl( &uc, 0, url, &len );
178 ok( ret, "expected success\n" );
180 /* valid username, empty password */
181 fill_url_components( &uc );
182 uc.lpszPassword = empty;
183 url[0] = 0;
184 len = 256;
185 ret = WinHttpCreateUrl( &uc, 0, url, &len );
186 ok( ret, "expected success\n" );
187 ok( len == 56, "expected len 56 got %u\n", len );
188 ok( !lstrcmpW( url, url3 ), "url doesn't match\n" );
190 /* valid password, NULL username */
191 fill_url_components( &uc );
192 SetLastError( 0xdeadbeef );
193 uc.lpszUserName = NULL;
194 url[0] = 0;
195 len = 256;
196 ret = WinHttpCreateUrl( &uc, 0, url, &len );
197 ok( !ret, "expected failure\n" );
198 ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", GetLastError() );
200 /* valid password, empty username */
201 fill_url_components( &uc );
202 uc.lpszUserName = empty;
203 url[0] = 0;
204 len = 256;
205 ret = WinHttpCreateUrl( &uc, 0, url, &len );
206 ok( ret, "expected success\n");
208 /* NULL username, NULL password */
209 fill_url_components( &uc );
210 uc.lpszUserName = NULL;
211 uc.lpszPassword = NULL;
212 url[0] = 0;
213 len = 256;
214 ret = WinHttpCreateUrl( &uc, 0, url, &len );
215 ok( ret, "expected success\n" );
216 ok( len == 38, "expected len 38 got %u\n", len );
217 ok( !lstrcmpW( url, url4 ), "url doesn't match\n" );
219 /* empty username, empty password */
220 fill_url_components( &uc );
221 uc.lpszUserName = empty;
222 uc.lpszPassword = empty;
223 url[0] = 0;
224 len = 256;
225 ret = WinHttpCreateUrl( &uc, 0, url, &len );
226 ok( ret, "expected success\n" );
227 ok( len == 56, "expected len 56 got %u\n", len );
228 ok( !lstrcmpW( url, url5 ), "url doesn't match\n" );
230 /* nScheme has lower precedence than lpszScheme */
231 fill_url_components( &uc );
232 uc.lpszScheme = ftp;
233 uc.dwSchemeLength = lstrlenW( uc.lpszScheme );
234 url[0] = 0;
235 len = 256;
236 ret = WinHttpCreateUrl( &uc, 0, url, &len );
237 ok( ret, "expected success\n" );
238 ok( len == lstrlenW( url6 ), "expected len %d got %u\n", lstrlenW( url6 ) + 1, len );
239 ok( !lstrcmpW( url, url6 ), "url doesn't match\n" );
241 /* non-standard port */
242 uc.lpszScheme = http;
243 uc.dwSchemeLength = lstrlenW( uc.lpszScheme );
244 uc.nPort = 42;
245 url[0] = 0;
246 len = 256;
247 ret = WinHttpCreateUrl( &uc, 0, url, &len );
248 ok( ret, "expected success\n" );
249 ok( len == 59, "expected len 59 got %u\n", len );
250 ok( !lstrcmpW( url, url7 ), "url doesn't match\n" );
252 /* escape extra info */
253 fill_url_components( &uc );
254 uc.lpszExtraInfo = escape;
255 uc.dwExtraInfoLength = lstrlenW( uc.lpszExtraInfo );
256 url[0] = 0;
257 len = 256;
258 ret = WinHttpCreateUrl( &uc, ICU_ESCAPE, url, &len );
259 ok( ret, "expected success\n" );
260 ok( len == 113, "expected len 113 got %u\n", len );
261 ok( !lstrcmpW( url, url8 ), "url doesn't match\n" );
263 /* NULL lpszScheme, 0 nScheme and nPort */
264 fill_url_components( &uc );
265 uc.lpszScheme = NULL;
266 uc.dwSchemeLength = 0;
267 uc.nScheme = 0;
268 uc.nPort = 0;
269 url[0] = 0;
270 len = 256;
271 ret = WinHttpCreateUrl( &uc, 0, url, &len );
272 ok( ret, "expected success\n" );
273 ok( len == 58, "expected len 58 got %u\n", len );
274 ok( !lstrcmpW( url, url9 ), "url doesn't match\n" );
276 HeapFree( GetProcessHeap(), 0, url );
279 static void WinHttpCrackUrl_test( void )
281 URL_COMPONENTSW uc;
282 DWORD Len = 0;
283 BOOL ret;
285 /* NULL components */
286 SetLastError( 0xdeadbeef );
287 memset(&uc,0,sizeof(uc));
288 uc.dwStructSize = sizeof(URL_COMPONENTSW);
289 uc.dwSchemeLength = -1;
290 uc.dwHostNameLength = -1;
291 uc.dwUserNameLength = -1;
292 uc.dwPasswordLength = -1;
293 uc.dwUrlPathLength = -1;
294 uc.dwExtraInfoLength = -1;
296 ret = WinHttpCrackUrl( url_k1, 0, 0,&uc);
298 Len = 0;
299 ok (ret!=0,"WinHttpCrackUrl failed\n");
300 ok (uc.lpszScheme == url_k1,"Failed to get uc.lpszScheme\n");
301 ok (uc.dwSchemeLength == 4, "Unexpected dwSchemeLength\n");
302 Len += uc.dwSchemeLength + 3;
303 ok (uc.lpszUserName== &url_k1[Len],"Failed to get uc.lpszUserName\n");
304 ok (uc.dwUserNameLength == 8, "Unexpected dwUserNameLength\n");
305 Len +=uc.dwUserNameLength + 1;
306 ok (uc.lpszPassword==&url_k1[Len],"Failed to get uc.lpszPassword\n");
307 ok (uc.dwPasswordLength == 8, "Unexpected dwPasswordLength\n");
308 Len +=uc.dwPasswordLength + 1;
309 ok (uc.lpszHostName == &url_k1[Len],"Failed to get uc.lpszHostName\n");
310 ok (uc.dwHostNameLength == 14, "Unexpected dwHostNameLength\n");
311 Len += uc.dwHostNameLength;
312 ok (uc.lpszUrlPath == &url_k1[Len],"Failed to get uc.lpszUrlPath\n");
313 ok (uc.dwUrlPathLength == 11, "Unexpected dwUrlPathLength\n");
314 Len += uc.dwUrlPathLength;
315 ok (uc.lpszExtraInfo == &url_k1[Len],"Failed to get uc.lpszExtraInfo\n");
316 ok (uc.dwExtraInfoLength == 0, "Unexpected dwExtraInfoLength\n");
318 memset(&uc,0,sizeof(uc));
319 uc.dwStructSize = sizeof(URL_COMPONENTSW);
320 uc.dwSchemeLength = -1;
321 uc.dwHostNameLength = -1;
322 uc.dwUserNameLength = -1;
323 uc.dwPasswordLength = -1;
324 uc.dwUrlPathLength = -1;
325 uc.dwExtraInfoLength = -1;
327 ret = WinHttpCrackUrl( url_k2, 0, 0,&uc);
329 Len = 0;
330 ok (ret!=0,"WinHttpCrackUrl failed\n");
331 ok (uc.lpszScheme == url_k2,"Failed to get uc.lpszScheme\n");
332 ok (uc.dwSchemeLength == 4, "Unexpected dwSchemeLength\n");
333 Len += uc.dwSchemeLength + 3;
334 ok (uc.lpszUserName == NULL ,"Got uc.lpszUserName\n");
335 ok (uc.dwUserNameLength == 0, "Unexpected dwUserNameLength\n");
336 ok (uc.lpszPassword == NULL,"Got uc.lpszPassword\n");
337 ok (uc.dwPasswordLength == 0, "Unexpected dwPasswordLength\n");
338 ok (uc.lpszHostName == &url_k2[Len],"Failed to get uc.lpszHostName\n");
339 ok (uc.dwHostNameLength == 14, "Unexpected dwHostNameLength\n");
340 Len += uc.dwHostNameLength;
341 ok (uc.lpszUrlPath == &url_k2[Len],"Failed to get uc.lpszUrlPath\n");
342 ok (uc.dwUrlPathLength == 0, "Unexpected dwUrlPathLength\n");
343 Len += uc.dwUrlPathLength;
344 ok (uc.lpszExtraInfo == &url_k2[Len],"Failed to get uc.lpszExtraInfo\n");
345 ok (uc.dwExtraInfoLength == 0, "Unexpected dwExtraInfoLength\n");
348 START_TEST(url)
350 WinHttpCreateUrl_test();
351 WinHttpCrackUrl_test();