wininet: Fix flags and SetLastError for InternetCanonicalizeUrlA/W.
[wine/gsoc_dplay.git] / dlls / wininet / tests / internet.c
blobf04c399ec4618c0f017ade9989d2f661fc066b09
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 /* buffer is larger as the required size */
77 memset(buffer, '#', sizeof(buffer)-1);
78 buffer[sizeof(buffer)-1] = '\0';
79 dwSize = urllen+2;
80 SetLastError(0xdeadbeef);
81 res = InternetCanonicalizeUrlA(url, buffer, &dwSize, 0);
82 /* dwSize is nr. of copied bytes without the terminating '\0' */
83 ok( res && (dwSize == urllen) && (lstrcmpA(url, buffer) == 0),
84 "got %u and %u with size %u for '%s' (%d)\n",
85 res, GetLastError(), dwSize, buffer, lstrlenA(buffer));
88 /* ############################### */
90 static void test_InternetQueryOptionA(void)
92 HINTERNET hinet,hurl;
93 DWORD len;
94 DWORD err;
95 static const char useragent[] = {"Wininet Test"};
96 char *buffer;
97 int retval;
99 hinet = InternetOpenA(useragent,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL, 0);
100 ok((hinet != 0x0),"InternetOpen Failed\n");
102 SetLastError(0xdeadbeef);
103 len=strlen(useragent)+1;
104 retval=InternetQueryOptionA(hinet,INTERNET_OPTION_USER_AGENT,NULL,&len);
105 err=GetLastError();
106 ok(len == strlen(useragent)+1,"Got wrong user agent length %d instead of %d\n",len,lstrlenA(useragent));
107 ok(retval == 0,"Got wrong return value %d\n",retval);
108 todo_wine ok(err == ERROR_INSUFFICIENT_BUFFER, "Got wrong error code%d\n",err);
110 SetLastError(0xdeadbeef);
111 len=strlen(useragent)+1;
112 buffer=HeapAlloc(GetProcessHeap(),0,len);
113 retval=InternetQueryOptionA(hinet,INTERNET_OPTION_USER_AGENT,buffer,&len);
114 err=GetLastError();
115 todo_wine ok(!strcmp(useragent,buffer),"Got wrong user agent string %s instead of %s\n",buffer,useragent);
116 todo_wine ok(len == strlen(useragent),"Got wrong user agent length %d instead of %d\n",len,lstrlenA(useragent));
117 todo_wine ok(retval == 1,"Got wrong return value %d\n",retval);
118 ok(err == 0xdeadbeef, "Got wrong error code %d\n",err);
119 HeapFree(GetProcessHeap(),0,buffer);
121 SetLastError(0xdeadbeef);
122 len=0;
123 buffer=HeapAlloc(GetProcessHeap(),0,100);
124 retval=InternetQueryOptionA(hinet,INTERNET_OPTION_USER_AGENT,buffer,&len);
125 err=GetLastError();
126 todo_wine ok(len == strlen(useragent) + 1,"Got wrong user agent length %d instead of %d\n", len, lstrlenA(useragent) + 1);
127 ok(!retval, "Got wrong return value %d\n", retval);
128 todo_wine ok(err == ERROR_INSUFFICIENT_BUFFER, "Got wrong error code %d\n", err);
129 HeapFree(GetProcessHeap(),0,buffer);
131 hurl = InternetConnectA(hinet,"www.winehq.com",INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
133 SetLastError(0xdeadbeef);
134 len=0;
135 retval = InternetQueryOptionA(hurl,INTERNET_OPTION_USER_AGENT,NULL,&len);
136 err=GetLastError();
137 ok(len == 0,"Got wrong user agent length %d instead of 0\n",len);
138 ok(retval == 0,"Got wrong return value %d\n",retval);
139 todo_wine ok(err == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "Got wrong error code %d\n",err);
141 InternetCloseHandle(hurl);
142 InternetCloseHandle(hinet);
144 hinet = InternetOpenA("",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL, 0);
145 ok((hinet != 0x0),"InternetOpen Failed\n");
147 SetLastError(0xdeadbeef);
148 len=0;
149 retval=InternetQueryOptionA(hinet,INTERNET_OPTION_USER_AGENT,NULL,&len);
150 err=GetLastError();
151 todo_wine ok(len == 1,"Got wrong user agent length %d instead of %d\n",len,1);
152 ok(retval == 0,"Got wrong return value %d\n",retval);
153 todo_wine ok(err == ERROR_INSUFFICIENT_BUFFER, "Got wrong error code%d\n",err);
155 InternetCloseHandle(hinet);
157 hinet = InternetOpenA(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL, 0);
158 ok((hinet != 0x0),"InternetOpen Failed\n");
159 SetLastError(0xdeadbeef);
160 len=0;
161 retval=InternetQueryOptionA(hinet,INTERNET_OPTION_USER_AGENT,NULL,&len);
162 err=GetLastError();
163 todo_wine ok(len == 1,"Got wrong user agent length %d instead of %d\n",len,1);
164 ok(retval == 0,"Got wrong return value %d\n",retval);
165 todo_wine ok(err == ERROR_INSUFFICIENT_BUFFER, "Got wrong error code%d\n",err);
167 InternetCloseHandle(hinet);
170 static void test_get_cookie(void)
172 DWORD len;
173 BOOL ret;
175 SetLastError(0xdeadbeef);
176 ret = InternetGetCookie("http://www.example.com", NULL, NULL, &len);
177 ok(!ret && GetLastError() == ERROR_NO_MORE_ITEMS,
178 "InternetGetCookie should have failed with %s and error %d\n",
179 ret ? "TRUE" : "FALSE", GetLastError());
182 static void test_null(void)
184 HINTERNET hi, hc;
185 static const WCHAR szServer[] = { 's','e','r','v','e','r',0 };
186 static const WCHAR szEmpty[] = { 0 };
187 static const WCHAR szUrl[] = { 'h','t','t','p',':','/','/','a','.','b','.','c',0 };
188 static const WCHAR szExpect[] = { 's','e','r','v','e','r',';',' ','s','e','r','v','e','r',0 };
189 WCHAR buffer[0x20];
190 BOOL r;
191 DWORD sz;
193 hi = InternetOpenW(NULL, 0, NULL, NULL, 0);
194 ok(hi != NULL, "open failed\n");
196 hc = InternetConnectW(hi, NULL, 0, NULL, NULL, 0, 0, 0);
197 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
198 ok(hc == NULL, "connect failed\n");
200 hc = InternetConnectW(hi, NULL, 0, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
201 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
202 ok(hc == NULL, "connect failed\n");
204 hc = InternetConnectW(hi, NULL, 0, NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
205 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
206 ok(hc == NULL, "connect failed\n");
208 hc = InternetConnectW(NULL, szServer, 0, NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
209 ok(GetLastError() == ERROR_INVALID_HANDLE, "wrong error\n");
210 ok(hc == NULL, "connect failed\n");
212 hc = InternetOpenUrlW(hi, NULL, NULL, 0, 0, 0);
213 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
214 ok(hc == NULL, "connect failed\n");
216 hc = InternetOpenUrlW(hi, szServer, NULL, 0, 0, 0);
217 ok(GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME, "wrong error\n");
218 ok(hc == NULL, "connect failed\n");
220 InternetCloseHandle(hi);
222 r = InternetSetCookieW(NULL, NULL, NULL);
223 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
224 ok(r == FALSE, "return wrong\n");
226 r = InternetSetCookieW(szServer, NULL, NULL);
227 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
228 ok(r == FALSE, "return wrong\n");
230 r = InternetSetCookieW(szUrl, szServer, NULL);
231 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
232 ok(r == FALSE, "return wrong\n");
234 r = InternetSetCookieW(szUrl, szServer, szServer);
235 ok(r == TRUE, "return wrong\n");
237 todo_wine {
238 r = InternetSetCookieW(szUrl, NULL, szServer);
239 ok(r == TRUE, "return wrong\n");
242 r = InternetSetCookieW(szUrl, szServer, szEmpty);
243 ok(r == TRUE, "return wrong\n");
245 r = InternetSetCookieW(szServer, NULL, szServer);
246 todo_wine {
247 ok(GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME, "wrong error\n");
249 ok(r == FALSE, "return wrong\n");
251 sz = 0;
252 r = InternetGetCookieW(NULL, NULL, NULL, &sz);
253 ok(GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME, "wrong error\n");
254 ok( r == FALSE, "return wrong\n");
256 r = InternetGetCookieW(szServer, NULL, NULL, &sz);
257 todo_wine {
258 ok(GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME, "wrong error\n");
260 ok( r == FALSE, "return wrong\n");
262 sz = 0;
263 r = InternetGetCookieW(szUrl, szServer, NULL, &sz);
264 ok( r == TRUE, "return wrong\n");
265 todo_wine {
266 ok( sz == 30, "sz wrong\n");
269 sz = 0x20;
270 memset(buffer, 0, sizeof buffer);
271 r = InternetGetCookieW(szUrl, szServer, buffer, &sz);
272 ok( r == TRUE, "return wrong\n");
273 todo_wine {
274 ok( sz == lstrlenW(buffer), "sz wrong\n");
275 ok( !lstrcmpW(szExpect, buffer), "cookie data wrong\n");
278 sz = sizeof(buffer);
279 r = InternetQueryOptionA(NULL, INTERNET_OPTION_CONNECTED_STATE, buffer, &sz);
280 ok(r == TRUE, "ret %d\n", r);
284 /* ############################### */
286 START_TEST(internet)
288 test_InternetCanonicalizeUrlA();
289 test_InternetQueryOptionA();
290 test_get_cookie();
291 test_null();