wininet: Avoid a crash on an invalid handle in InternetQueryOption.
[wine/hacks.git] / dlls / wininet / tests / internet.c
blob8eb7cc63f3249dc24c58c12403eff242ee3bbeda
1 /*
2 * Wininet - internet tests
4 * Copyright 2005 Vijay Kiran Kamuju
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 <string.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wininet.h"
26 #include "winerror.h"
28 #include "wine/test.h"
30 /* ############################### */
32 static void test_InternetCanonicalizeUrlA(void)
34 CHAR buffer[256];
35 LPCSTR url;
36 DWORD urllen;
37 DWORD dwSize;
38 DWORD res;
40 /* Acrobat Updater 5 calls this for Adobe Reader 8.1 */
41 url = "http://swupmf.adobe.com/manifest/50/win/AdobeUpdater.upd";
42 urllen = lstrlenA(url);
44 memset(buffer, '#', sizeof(buffer)-1);
45 buffer[sizeof(buffer)-1] = '\0';
46 dwSize = 1; /* Acrobat Updater use this size */
47 SetLastError(0xdeadbeef);
48 res = InternetCanonicalizeUrlA(url, buffer, &dwSize, 0);
49 ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER) && (dwSize == (urllen+1)),
50 "got %u and %u with size %u for '%s' (%d)\n",
51 res, GetLastError(), dwSize, buffer, lstrlenA(buffer));
54 /* buffer has no space for the terminating '\0' */
55 memset(buffer, '#', sizeof(buffer)-1);
56 buffer[sizeof(buffer)-1] = '\0';
57 dwSize = urllen;
58 SetLastError(0xdeadbeef);
59 res = InternetCanonicalizeUrlA(url, buffer, &dwSize, 0);
60 /* dwSize is nr. of needed bytes with the terminating '\0' */
61 ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER) && (dwSize == (urllen+1)),
62 "got %u and %u with size %u for '%s' (%d)\n",
63 res, GetLastError(), dwSize, buffer, lstrlenA(buffer));
65 /* buffer has the required size */
66 memset(buffer, '#', sizeof(buffer)-1);
67 buffer[sizeof(buffer)-1] = '\0';
68 dwSize = urllen+1;
69 SetLastError(0xdeadbeef);
70 res = InternetCanonicalizeUrlA(url, buffer, &dwSize, 0);
71 /* dwSize is nr. of copied bytes without the terminating '\0' */
72 ok( res && (dwSize == urllen) && (lstrcmpA(url, buffer) == 0),
73 "got %u and %u with size %u for '%s' (%d)\n",
74 res, GetLastError(), dwSize, buffer, lstrlenA(buffer));
76 memset(buffer, '#', sizeof(buffer)-1);
77 buffer[sizeof(buffer)-1] = '\0';
78 dwSize = sizeof(buffer);
79 SetLastError(0xdeadbeef);
80 res = InternetCanonicalizeUrlA("file:///C:/Program%20Files/Atmel/AVR%20Tools/STK500/STK500.xml", buffer, &dwSize, ICU_DECODE | ICU_NO_ENCODE);
81 ok(res, "InternetCanonicalizeUrlA failed %u\n", GetLastError());
82 ok(dwSize == lstrlenA(buffer), "got %d expected %d\n", dwSize, lstrlenA(buffer));
83 todo_wine ok(!lstrcmpA("file://C:\\Program Files\\Atmel\\AVR Tools\\STK500\\STK500.xml", buffer),
84 "got %s expected 'file://C:\\Program Files\\Atmel\\AVR Tools\\STK500\\STK500.xml'\n", buffer);
86 /* buffer is larger as the required size */
87 memset(buffer, '#', sizeof(buffer)-1);
88 buffer[sizeof(buffer)-1] = '\0';
89 dwSize = urllen+2;
90 SetLastError(0xdeadbeef);
91 res = InternetCanonicalizeUrlA(url, buffer, &dwSize, 0);
92 /* dwSize is nr. of copied bytes without the terminating '\0' */
93 ok( res && (dwSize == urllen) && (lstrcmpA(url, buffer) == 0),
94 "got %u and %u with size %u for '%s' (%d)\n",
95 res, GetLastError(), dwSize, buffer, lstrlenA(buffer));
98 /* check NULL pointers */
99 memset(buffer, '#', urllen + 4);
100 buffer[urllen + 4] = '\0';
101 dwSize = urllen+1;
102 SetLastError(0xdeadbeef);
103 res = InternetCanonicalizeUrlA(NULL, buffer, &dwSize, 0);
104 ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
105 "got %u and %u with size %u for '%s' (%d)\n",
106 res, GetLastError(), dwSize, buffer, lstrlenA(buffer));
108 memset(buffer, '#', urllen + 4);
109 buffer[urllen + 4] = '\0';
110 dwSize = urllen+1;
111 SetLastError(0xdeadbeef);
112 res = InternetCanonicalizeUrlA(url, NULL, &dwSize, 0);
113 ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
114 "got %u and %u with size %u for '%s' (%d)\n",
115 res, GetLastError(), dwSize, buffer, lstrlenA(buffer));
117 memset(buffer, '#', urllen + 4);
118 buffer[urllen + 4] = '\0';
119 dwSize = urllen+1;
120 SetLastError(0xdeadbeef);
121 res = InternetCanonicalizeUrlA(url, buffer, NULL, 0);
122 ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
123 "got %u and %u with size %u for '%s' (%d)\n",
124 res, GetLastError(), dwSize, buffer, lstrlenA(buffer));
126 /* test with trailing space */
127 dwSize = 256;
128 res = InternetCanonicalizeUrlA("http://www.winehq.org/index.php?x= ", buffer, &dwSize, ICU_BROWSER_MODE);
129 ok(res == 1, "InternetCanonicalizeUrlA failed\n");
130 ok(!strcmp(buffer, "http://www.winehq.org/index.php?x="), "Trailing space should have been stripped even in ICU_BROWSER_MODE (%s)\n", buffer);
132 res = InternetSetOptionA(NULL, 0xdeadbeef, buffer, sizeof(buffer));
133 ok(!res, "InternetSetOptionA succeeded\n");
134 ok(GetLastError() == ERROR_INTERNET_INVALID_OPTION,
135 "InternetSetOptionA failed %u, expected ERROR_INTERNET_INVALID_OPTION\n", GetLastError());
138 /* ############################### */
140 static void test_InternetQueryOptionA(void)
142 HINTERNET hinet,hurl;
143 DWORD len;
144 DWORD err;
145 static const char useragent[] = {"Wininet Test"};
146 char *buffer;
147 int retval;
149 hinet = InternetOpenA(useragent,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL, 0);
150 ok((hinet != 0x0),"InternetOpen Failed\n");
152 SetLastError(0xdeadbeef);
153 retval=InternetQueryOptionA(NULL,INTERNET_OPTION_USER_AGENT,NULL,&len);
154 err=GetLastError();
155 ok(retval == 0,"Got wrong return value %d\n",retval);
156 ok(err == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "Got wrong error code%d\n",err);
158 SetLastError(0xdeadbeef);
159 len=strlen(useragent)+1;
160 retval=InternetQueryOptionA(hinet,INTERNET_OPTION_USER_AGENT,NULL,&len);
161 err=GetLastError();
162 ok(len == strlen(useragent)+1,"Got wrong user agent length %d instead of %d\n",len,lstrlenA(useragent));
163 ok(retval == 0,"Got wrong return value %d\n",retval);
164 todo_wine ok(err == ERROR_INSUFFICIENT_BUFFER, "Got wrong error code%d\n",err);
166 SetLastError(0xdeadbeef);
167 len=strlen(useragent)+1;
168 buffer=HeapAlloc(GetProcessHeap(),0,len);
169 retval=InternetQueryOptionA(hinet,INTERNET_OPTION_USER_AGENT,buffer,&len);
170 err=GetLastError();
171 ok(retval == 1,"Got wrong return value %d\n",retval);
172 if (retval)
174 ok(!strcmp(useragent,buffer),"Got wrong user agent string %s instead of %s\n",buffer,useragent);
175 todo_wine ok(len == strlen(useragent),"Got wrong user agent length %d instead of %d\n",len,lstrlenA(useragent));
177 ok(err == 0xdeadbeef, "Got wrong error code %d\n",err);
178 HeapFree(GetProcessHeap(),0,buffer);
180 SetLastError(0xdeadbeef);
181 len=0;
182 buffer=HeapAlloc(GetProcessHeap(),0,100);
183 retval=InternetQueryOptionA(hinet,INTERNET_OPTION_USER_AGENT,buffer,&len);
184 err=GetLastError();
185 ok(len == strlen(useragent) + 1,"Got wrong user agent length %d instead of %d\n", len, lstrlenA(useragent) + 1);
186 ok(!retval, "Got wrong return value %d\n", retval);
187 ok(err == ERROR_INSUFFICIENT_BUFFER, "Got wrong error code %d\n", err);
188 HeapFree(GetProcessHeap(),0,buffer);
190 hurl = InternetConnectA(hinet,"www.winehq.com",INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
192 SetLastError(0xdeadbeef);
193 len=0;
194 retval = InternetQueryOptionA(hurl,INTERNET_OPTION_USER_AGENT,NULL,&len);
195 err=GetLastError();
196 ok(len == 0,"Got wrong user agent length %d instead of 0\n",len);
197 ok(retval == 0,"Got wrong return value %d\n",retval);
198 ok(err == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "Got wrong error code %d\n",err);
200 InternetCloseHandle(hurl);
201 InternetCloseHandle(hinet);
203 hinet = InternetOpenA("",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL, 0);
204 ok((hinet != 0x0),"InternetOpen Failed\n");
206 SetLastError(0xdeadbeef);
207 len=0;
208 retval=InternetQueryOptionA(hinet,INTERNET_OPTION_USER_AGENT,NULL,&len);
209 err=GetLastError();
210 ok(len == 1,"Got wrong user agent length %d instead of %d\n",len,1);
211 ok(retval == 0,"Got wrong return value %d\n",retval);
212 ok(err == ERROR_INSUFFICIENT_BUFFER, "Got wrong error code%d\n",err);
214 InternetCloseHandle(hinet);
216 hinet = InternetOpenA(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL, 0);
217 ok((hinet != 0x0),"InternetOpen Failed\n");
218 SetLastError(0xdeadbeef);
219 len=0;
220 retval=InternetQueryOptionA(hinet,INTERNET_OPTION_USER_AGENT,NULL,&len);
221 err=GetLastError();
222 todo_wine ok(len == 1,"Got wrong user agent length %d instead of %d\n",len,1);
223 ok(retval == 0,"Got wrong return value %d\n",retval);
224 todo_wine ok(err == ERROR_INSUFFICIENT_BUFFER, "Got wrong error code%d\n",err);
226 InternetCloseHandle(hinet);
229 static void test_get_cookie(void)
231 DWORD len;
232 BOOL ret;
234 SetLastError(0xdeadbeef);
235 ret = InternetGetCookie("http://www.example.com", NULL, NULL, &len);
236 ok(!ret && GetLastError() == ERROR_NO_MORE_ITEMS,
237 "InternetGetCookie should have failed with %s and error %d\n",
238 ret ? "TRUE" : "FALSE", GetLastError());
241 static void test_null(void)
243 HINTERNET hi, hc;
244 static const WCHAR szServer[] = { 's','e','r','v','e','r',0 };
245 static const WCHAR szEmpty[] = { 0 };
246 static const WCHAR szUrl[] = { 'h','t','t','p',':','/','/','a','.','b','.','c',0 };
247 static const WCHAR szUrlEmpty[] = { 'h','t','t','p',':','/','/',0 };
248 static const WCHAR szExpect[] = { 's','e','r','v','e','r',';',' ','s','e','r','v','e','r',0 };
249 WCHAR buffer[0x20];
250 BOOL r;
251 DWORD sz;
253 hi = InternetOpenW(NULL, 0, NULL, NULL, 0);
254 ok(hi != NULL, "open failed\n");
256 hc = InternetConnectW(hi, NULL, 0, NULL, NULL, 0, 0, 0);
257 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
258 ok(hc == NULL, "connect failed\n");
260 hc = InternetConnectW(hi, NULL, 0, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
261 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
262 ok(hc == NULL, "connect failed\n");
264 hc = InternetConnectW(hi, NULL, 0, NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
265 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
266 ok(hc == NULL, "connect failed\n");
268 hc = InternetConnectW(NULL, szServer, 0, NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
269 ok(GetLastError() == ERROR_INVALID_HANDLE, "wrong error\n");
270 ok(hc == NULL, "connect failed\n");
272 hc = InternetOpenUrlW(hi, NULL, NULL, 0, 0, 0);
273 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
274 ok(hc == NULL, "connect failed\n");
276 hc = InternetOpenUrlW(hi, szServer, NULL, 0, 0, 0);
277 ok(GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME, "wrong error\n");
278 ok(hc == NULL, "connect failed\n");
280 InternetCloseHandle(hi);
282 r = InternetSetCookieW(NULL, NULL, NULL);
283 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
284 ok(r == FALSE, "return wrong\n");
286 r = InternetSetCookieW(szServer, NULL, NULL);
287 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
288 ok(r == FALSE, "return wrong\n");
290 r = InternetSetCookieW(szUrl, szServer, NULL);
291 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
292 ok(r == FALSE, "return wrong\n");
294 r = InternetSetCookieW(szUrl, szServer, szServer);
295 ok(r == TRUE, "return wrong\n");
297 r = InternetSetCookieW(szUrl, NULL, szServer);
298 ok(r == TRUE, "return wrong\n");
300 r = InternetSetCookieW(szUrl, szServer, szEmpty);
301 ok(r == TRUE, "return wrong\n");
303 r = InternetSetCookieW(szUrlEmpty, szServer, szServer);
304 ok(r == FALSE, "return wrong\n");
306 r = InternetSetCookieW(szServer, NULL, szServer);
307 todo_wine {
308 ok(GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME, "wrong error\n");
310 ok(r == FALSE, "return wrong\n");
312 sz = 0;
313 r = InternetGetCookieW(NULL, NULL, NULL, &sz);
314 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME,
315 "wrong error %u\n", GetLastError());
316 ok( r == FALSE, "return wrong\n");
318 r = InternetGetCookieW(szServer, NULL, NULL, &sz);
319 todo_wine {
320 ok(GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME, "wrong error\n");
322 ok( r == FALSE, "return wrong\n");
324 sz = 0;
325 r = InternetGetCookieW(szUrlEmpty, szServer, NULL, &sz);
326 ok( r == FALSE, "return wrong\n");
328 sz = 0;
329 r = InternetGetCookieW(szUrl, szServer, NULL, &sz);
330 ok( r == TRUE, "return wrong\n");
332 /* sz is 14 on XP SP2 and beyond, 30 on XP SP1 and before */
333 ok( sz == 14 || sz == 30, "sz wrong, got %u, expected 14 or 30\n", sz);
335 sz = 0x20;
336 memset(buffer, 0, sizeof buffer);
337 r = InternetGetCookieW(szUrl, szServer, buffer, &sz);
338 ok( r == TRUE, "return wrong\n");
340 /* sz == lstrlenW(buffer) only in XP SP1 */
341 ok( sz == 1 + lstrlenW(buffer) || sz == lstrlenW(buffer), "sz wrong %d\n", sz);
343 /* before XP SP2, buffer is "server; server" */
344 ok( !lstrcmpW(szExpect, buffer) || !lstrcmpW(szServer, buffer), "cookie data wrong\n");
346 sz = sizeof(buffer);
347 r = InternetQueryOptionA(NULL, INTERNET_OPTION_CONNECTED_STATE, buffer, &sz);
348 ok(r == TRUE, "ret %d\n", r);
351 static void test_version(void)
353 INTERNET_VERSION_INFO version;
354 DWORD size;
355 BOOL res;
357 size = sizeof(version);
358 res = InternetQueryOptionA(NULL, INTERNET_OPTION_VERSION, &version, &size);
359 ok(res, "Could not get version: %u\n", GetLastError());
360 ok(version.dwMajorVersion == 1, "dwMajorVersion=%d, expected 1\n", version.dwMajorVersion);
361 ok(version.dwMinorVersion == 2, "dwMinorVersion=%d, expected 2\n", version.dwMinorVersion);
364 /* ############################### */
366 START_TEST(internet)
368 test_InternetCanonicalizeUrlA();
369 test_InternetQueryOptionA();
370 test_get_cookie();
371 test_version();
372 test_null();