rsaenh/tests: Fix a test failure on NT4 and below.
[wine/multimedia.git] / dlls / winhttp / tests / url.c
blob20ff7f630d0c3ed4f9c99eca31a781afc1b8d5f1
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 void fill_url_components( URL_COMPONENTS *uc )
65 uc->dwStructSize = sizeof(URL_COMPONENTS);
66 uc->lpszScheme = http;
67 uc->dwSchemeLength = lstrlenW( uc->lpszScheme );
68 uc->nScheme = INTERNET_SCHEME_HTTP;
69 uc->lpszHostName = winehq;
70 uc->dwHostNameLength = lstrlenW( uc->lpszHostName );
71 uc->nPort = 80;
72 uc->lpszUserName = username;
73 uc->dwUserNameLength = lstrlenW( uc->lpszUserName );
74 uc->lpszPassword = password;
75 uc->dwPasswordLength = lstrlenW( uc->lpszPassword );
76 uc->lpszUrlPath = about;
77 uc->dwUrlPathLength = lstrlenW( uc->lpszUrlPath );
78 uc->lpszExtraInfo = query;
79 uc->dwExtraInfoLength = lstrlenW( uc->lpszExtraInfo );
82 static void WinHttpCreateUrl_test( void )
84 URL_COMPONENTS uc;
85 WCHAR *url;
86 DWORD len;
87 BOOL ret;
89 /* NULL components */
90 len = ~0UL;
91 SetLastError( 0xdeadbeef );
92 ret = WinHttpCreateUrl( NULL, 0, NULL, &len );
93 ok( !ret, "expected failure\n" );
94 ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", GetLastError() );
95 ok( len == ~0UL, "expected len ~0UL got %u\n", len );
97 /* zero'ed components */
98 memset( &uc, 0, sizeof(URL_COMPONENTS) );
99 SetLastError( 0xdeadbeef );
100 ret = WinHttpCreateUrl( &uc, 0, NULL, &len );
101 ok( !ret, "expected failure\n" );
102 ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", GetLastError() );
103 ok( len == ~0UL, "expected len ~0UL got %u\n", len );
105 /* valid components, NULL url, NULL length */
106 fill_url_components( &uc );
107 SetLastError( 0xdeadbeef );
108 ret = WinHttpCreateUrl( &uc, 0, NULL, NULL );
109 ok( !ret, "expected failure\n" );
110 ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", GetLastError() );
111 ok( len == ~0UL, "expected len ~0UL got %u\n", len );
113 /* valid components, NULL url */
114 SetLastError( 0xdeadbeef );
115 ret = WinHttpCreateUrl( &uc, 0, NULL, &len );
116 ok( !ret, "expected failure\n" );
117 ok( GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError() );
118 ok( len == 57, "expected len 57 got %u\n", len );
120 /* correct size, NULL url */
121 fill_url_components( &uc );
122 SetLastError( 0xdeadbeef );
123 ret = WinHttpCreateUrl( &uc, 0, NULL, &len );
124 ok( !ret, "expected failure\n" );
125 ok( GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError() );
126 ok( len == 57, "expected len 57 got %u\n", len );
128 /* valid components, allocated url, short length */
129 SetLastError( 0xdeadbeef );
130 url = HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(WCHAR) );
131 url[0] = 0;
132 len = 2;
133 ret = WinHttpCreateUrl( &uc, 0, url, &len );
134 ok( !ret, "expected failure\n" );
135 ok( GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER got %u\n", GetLastError() );
136 ok( len == 57, "expected len 57 got %u\n", len );
138 /* allocated url, NULL scheme */
139 uc.lpszScheme = NULL;
140 url[0] = 0;
141 len = 256;
142 ret = WinHttpCreateUrl( &uc, 0, url, &len );
143 ok( ret, "expected success\n" );
144 ok( len == 56, "expected len 56 got %u\n", len );
145 ok( !lstrcmpW( url, url1 ), "url doesn't match\n" );
147 /* allocated url, 0 scheme */
148 fill_url_components( &uc );
149 uc.nScheme = 0;
150 url[0] = 0;
151 len = 256;
152 ret = WinHttpCreateUrl( &uc, 0, url, &len );
153 ok( ret, "expected success\n" );
154 ok( len == 56, "expected len 56 got %u\n", len );
156 /* valid components, allocated url */
157 fill_url_components( &uc );
158 url[0] = 0;
159 len = 256;
160 ret = WinHttpCreateUrl( &uc, 0, url, &len );
161 ok( ret, "expected success\n" );
162 ok( len == 56, "expected len 56 got %d\n", len );
163 ok( !lstrcmpW( url, url1 ), "url doesn't match\n" );
165 /* valid username, NULL password */
166 fill_url_components( &uc );
167 uc.lpszPassword = NULL;
168 url[0] = 0;
169 len = 256;
170 ret = WinHttpCreateUrl( &uc, 0, url, &len );
171 ok( ret, "expected success\n" );
173 /* valid username, empty password */
174 fill_url_components( &uc );
175 uc.lpszPassword = empty;
176 url[0] = 0;
177 len = 256;
178 ret = WinHttpCreateUrl( &uc, 0, url, &len );
179 ok( ret, "expected success\n" );
180 ok( len == 56, "expected len 56 got %u\n", len );
181 ok( !lstrcmpW( url, url3 ), "url doesn't match\n" );
183 /* valid password, NULL username */
184 fill_url_components( &uc );
185 SetLastError( 0xdeadbeef );
186 uc.lpszUserName = NULL;
187 url[0] = 0;
188 len = 256;
189 ret = WinHttpCreateUrl( &uc, 0, url, &len );
190 ok( !ret, "expected failure\n" );
191 ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", GetLastError() );
193 /* valid password, empty username */
194 fill_url_components( &uc );
195 uc.lpszUserName = empty;
196 url[0] = 0;
197 len = 256;
198 ret = WinHttpCreateUrl( &uc, 0, url, &len );
199 ok( ret, "expected success\n");
201 /* NULL username, NULL password */
202 fill_url_components( &uc );
203 uc.lpszUserName = NULL;
204 uc.lpszPassword = NULL;
205 url[0] = 0;
206 len = 256;
207 ret = WinHttpCreateUrl( &uc, 0, url, &len );
208 ok( ret, "expected success\n" );
209 ok( len == 38, "expected len 38 got %u\n", len );
210 ok( !lstrcmpW( url, url4 ), "url doesn't match\n" );
212 /* empty username, empty password */
213 fill_url_components( &uc );
214 uc.lpszUserName = empty;
215 uc.lpszPassword = empty;
216 url[0] = 0;
217 len = 256;
218 ret = WinHttpCreateUrl( &uc, 0, url, &len );
219 ok( ret, "expected success\n" );
220 ok( len == 56, "expected len 56 got %u\n", len );
221 ok( !lstrcmpW( url, url5 ), "url doesn't match\n" );
223 /* nScheme has lower precedence than lpszScheme */
224 fill_url_components( &uc );
225 uc.lpszScheme = ftp;
226 uc.dwSchemeLength = lstrlenW( uc.lpszScheme );
227 url[0] = 0;
228 len = 256;
229 ret = WinHttpCreateUrl( &uc, 0, url, &len );
230 ok( ret, "expected success\n" );
231 ok( len == lstrlenW( url6 ), "expected len %d got %u\n", lstrlenW( url6 ) + 1, len );
232 ok( !lstrcmpW( url, url6 ), "url doesn't match\n" );
234 /* non-standard port */
235 uc.lpszScheme = http;
236 uc.dwSchemeLength = lstrlenW( uc.lpszScheme );
237 uc.nPort = 42;
238 url[0] = 0;
239 len = 256;
240 ret = WinHttpCreateUrl( &uc, 0, url, &len );
241 ok( ret, "expected success\n" );
242 ok( len == 59, "expected len 59 got %u\n", len );
243 ok( !lstrcmpW( url, url7 ), "url doesn't match\n" );
245 /* escape extra info */
246 fill_url_components( &uc );
247 uc.lpszExtraInfo = escape;
248 uc.dwExtraInfoLength = lstrlenW( uc.lpszExtraInfo );
249 url[0] = 0;
250 len = 256;
251 ret = WinHttpCreateUrl( &uc, ICU_ESCAPE, url, &len );
252 ok( ret, "expected success\n" );
253 ok( len == 113, "expected len 113 got %u\n", len );
254 ok( !lstrcmpW( url, url8 ), "url doesn't match\n" );
256 /* NULL lpszScheme, 0 nScheme and nPort */
257 fill_url_components( &uc );
258 uc.lpszScheme = NULL;
259 uc.dwSchemeLength = 0;
260 uc.nScheme = 0;
261 uc.nPort = 0;
262 url[0] = 0;
263 len = 256;
264 ret = WinHttpCreateUrl( &uc, 0, url, &len );
265 ok( ret, "expected success\n" );
266 ok( len == 58, "expected len 58 got %u\n", len );
267 ok( !lstrcmpW( url, url9 ), "url doesn't match\n" );
269 HeapFree( GetProcessHeap(), 0, url );
272 START_TEST(url)
274 WinHttpCreateUrl_test();