wininet: Fix a number of problems with InternetGetCookie.
[wine] / dlls / wininet / tests / internet.c
1 /*
2  * Wininet - internet tests
3  *
4  * Copyright 2005 Vijay Kiran Kamuju
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <string.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wininet.h"
26 #include "winerror.h"
27
28 #include "wine/test.h"
29
30 /* ############################### */
31
32 static void test_InternetCanonicalizeUrlA(void)
33 {
34     CHAR    buffer[256];
35     LPCSTR  url;
36     DWORD   urllen;
37     DWORD   dwSize;
38     DWORD   res;
39
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);
43
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));
52
53
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));
64
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));
75
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));
86
87
88     /* check NULL pointers */
89     memset(buffer, '#', urllen + 4);
90     buffer[urllen + 4] = '\0';
91     dwSize = urllen+1;
92     SetLastError(0xdeadbeef);
93     res = InternetCanonicalizeUrlA(NULL, buffer, &dwSize, 0);
94     ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
95         "got %u and %u with size %u for '%s' (%d)\n",
96         res, GetLastError(), dwSize, buffer, lstrlenA(buffer));
97
98     memset(buffer, '#', urllen + 4);
99     buffer[urllen + 4] = '\0';
100     dwSize = urllen+1;
101     SetLastError(0xdeadbeef);
102     res = InternetCanonicalizeUrlA(url, NULL, &dwSize, 0);
103     ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
104         "got %u and %u with size %u for '%s' (%d)\n",
105         res, GetLastError(), dwSize, buffer, lstrlenA(buffer));
106
107     memset(buffer, '#', urllen + 4);
108     buffer[urllen + 4] = '\0';
109     dwSize = urllen+1;
110     SetLastError(0xdeadbeef);
111     res = InternetCanonicalizeUrlA(url, buffer, NULL, 0);
112     ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
113         "got %u and %u with size %u for '%s' (%d)\n",
114         res, GetLastError(), dwSize, buffer, lstrlenA(buffer));
115
116 }
117
118 /* ############################### */
119
120 static void test_InternetQueryOptionA(void)
121 {
122   HINTERNET hinet,hurl;
123   DWORD len;
124   DWORD err;
125   static const char useragent[] = {"Wininet Test"};
126   char *buffer;
127   int retval;
128
129   hinet = InternetOpenA(useragent,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL, 0);
130   ok((hinet != 0x0),"InternetOpen Failed\n");
131
132   SetLastError(0xdeadbeef);
133   len=strlen(useragent)+1;
134   retval=InternetQueryOptionA(hinet,INTERNET_OPTION_USER_AGENT,NULL,&len);
135   err=GetLastError();
136   ok(len == strlen(useragent)+1,"Got wrong user agent length %d instead of %d\n",len,lstrlenA(useragent));
137   ok(retval == 0,"Got wrong return value %d\n",retval);
138   todo_wine ok(err == ERROR_INSUFFICIENT_BUFFER, "Got wrong error code%d\n",err);
139
140   SetLastError(0xdeadbeef);
141   len=strlen(useragent)+1;
142   buffer=HeapAlloc(GetProcessHeap(),0,len);
143   retval=InternetQueryOptionA(hinet,INTERNET_OPTION_USER_AGENT,buffer,&len);
144   err=GetLastError();
145   todo_wine ok(retval == 1,"Got wrong return value %d\n",retval);
146   if (retval)
147   {
148       todo_wine ok(!strcmp(useragent,buffer),"Got wrong user agent string %s instead of %s\n",buffer,useragent);
149       todo_wine ok(len == strlen(useragent),"Got wrong user agent length %d instead of %d\n",len,lstrlenA(useragent));
150   }
151   ok(err == 0xdeadbeef, "Got wrong error code %d\n",err);
152   HeapFree(GetProcessHeap(),0,buffer);
153
154   SetLastError(0xdeadbeef);
155   len=0;
156   buffer=HeapAlloc(GetProcessHeap(),0,100);
157   retval=InternetQueryOptionA(hinet,INTERNET_OPTION_USER_AGENT,buffer,&len);
158   err=GetLastError();
159   todo_wine ok(len == strlen(useragent) + 1,"Got wrong user agent length %d instead of %d\n", len, lstrlenA(useragent) + 1);
160   ok(!retval, "Got wrong return value %d\n", retval);
161   todo_wine ok(err == ERROR_INSUFFICIENT_BUFFER, "Got wrong error code %d\n", err);
162   HeapFree(GetProcessHeap(),0,buffer);
163
164   hurl = InternetConnectA(hinet,"www.winehq.com",INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
165
166   SetLastError(0xdeadbeef);
167   len=0;
168   retval = InternetQueryOptionA(hurl,INTERNET_OPTION_USER_AGENT,NULL,&len);
169   err=GetLastError();
170   ok(len == 0,"Got wrong user agent length %d instead of 0\n",len);
171   ok(retval == 0,"Got wrong return value %d\n",retval);
172   todo_wine ok(err == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "Got wrong error code %d\n",err);
173
174   InternetCloseHandle(hurl);
175   InternetCloseHandle(hinet);
176
177   hinet = InternetOpenA("",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL, 0);
178   ok((hinet != 0x0),"InternetOpen Failed\n");
179
180   SetLastError(0xdeadbeef);
181   len=0;
182   retval=InternetQueryOptionA(hinet,INTERNET_OPTION_USER_AGENT,NULL,&len);
183   err=GetLastError();
184   todo_wine ok(len == 1,"Got wrong user agent length %d instead of %d\n",len,1);
185   ok(retval == 0,"Got wrong return value %d\n",retval);
186   todo_wine ok(err == ERROR_INSUFFICIENT_BUFFER, "Got wrong error code%d\n",err);
187
188   InternetCloseHandle(hinet);
189
190   hinet = InternetOpenA(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL, 0);
191   ok((hinet != 0x0),"InternetOpen Failed\n");
192   SetLastError(0xdeadbeef);
193   len=0;
194   retval=InternetQueryOptionA(hinet,INTERNET_OPTION_USER_AGENT,NULL,&len);
195   err=GetLastError();
196   todo_wine ok(len == 1,"Got wrong user agent length %d instead of %d\n",len,1);
197   ok(retval == 0,"Got wrong return value %d\n",retval);
198   todo_wine ok(err == ERROR_INSUFFICIENT_BUFFER, "Got wrong error code%d\n",err);
199
200   InternetCloseHandle(hinet);
201 }
202
203 static void test_get_cookie(void)
204 {
205   DWORD len;
206   BOOL ret;
207
208   SetLastError(0xdeadbeef);
209   ret = InternetGetCookie("http://www.example.com", NULL, NULL, &len);
210   ok(!ret && GetLastError() == ERROR_NO_MORE_ITEMS,
211     "InternetGetCookie should have failed with %s and error %d\n",
212     ret ? "TRUE" : "FALSE", GetLastError());
213 }
214
215 static void test_null(void)
216 {
217   HINTERNET hi, hc;
218   static const WCHAR szServer[] = { 's','e','r','v','e','r',0 };
219   static const WCHAR szEmpty[] = { 0 };
220   static const WCHAR szUrl[] = { 'h','t','t','p',':','/','/','a','.','b','.','c',0 };
221   static const WCHAR szUrlEmpty[] = { 'h','t','t','p',':','/','/',0 };
222   static const WCHAR szExpect[] = { 's','e','r','v','e','r',';',' ','s','e','r','v','e','r',0 };
223   WCHAR buffer[0x20];
224   BOOL r;
225   DWORD sz;
226
227   hi = InternetOpenW(NULL, 0, NULL, NULL, 0);
228   ok(hi != NULL, "open failed\n");
229
230   hc = InternetConnectW(hi, NULL, 0, NULL, NULL, 0, 0, 0);
231   ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
232   ok(hc == NULL, "connect failed\n");
233
234   hc = InternetConnectW(hi, NULL, 0, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
235   ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
236   ok(hc == NULL, "connect failed\n");
237
238   hc = InternetConnectW(hi, NULL, 0, NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
239   ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
240   ok(hc == NULL, "connect failed\n");
241
242   hc = InternetConnectW(NULL, szServer, 0, NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
243   ok(GetLastError() == ERROR_INVALID_HANDLE, "wrong error\n");
244   ok(hc == NULL, "connect failed\n");
245
246   hc = InternetOpenUrlW(hi, NULL, NULL, 0, 0, 0);
247   ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
248   ok(hc == NULL, "connect failed\n");
249
250   hc = InternetOpenUrlW(hi, szServer, NULL, 0, 0, 0);
251   ok(GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME, "wrong error\n");
252   ok(hc == NULL, "connect failed\n");
253
254   InternetCloseHandle(hi);
255
256   r = InternetSetCookieW(NULL, NULL, NULL);
257   ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
258   ok(r == FALSE, "return wrong\n");
259
260   r = InternetSetCookieW(szServer, NULL, NULL);
261   ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
262   ok(r == FALSE, "return wrong\n");
263
264   r = InternetSetCookieW(szUrl, szServer, NULL);
265   ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
266   ok(r == FALSE, "return wrong\n");
267
268   r = InternetSetCookieW(szUrl, szServer, szServer);
269   ok(r == TRUE, "return wrong\n");
270
271   r = InternetSetCookieW(szUrl, NULL, szServer);
272   ok(r == TRUE, "return wrong\n");
273
274   r = InternetSetCookieW(szUrl, szServer, szEmpty);
275   ok(r == TRUE, "return wrong\n");
276
277   r = InternetSetCookieW(szUrlEmpty, szServer, szServer);
278   ok(r == FALSE, "return wrong\n");
279
280   r = InternetSetCookieW(szServer, NULL, szServer);
281   todo_wine {
282   ok(GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME, "wrong error\n");
283   }
284   ok(r == FALSE, "return wrong\n");
285
286   sz = 0;
287   r = InternetGetCookieW(NULL, NULL, NULL, &sz);
288   ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
289   ok( r == FALSE, "return wrong\n");
290
291   r = InternetGetCookieW(szServer, NULL, NULL, &sz);
292   todo_wine {
293   ok(GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME, "wrong error\n");
294   }
295   ok( r == FALSE, "return wrong\n");
296
297   sz = 0;
298   r = InternetGetCookieW(szUrlEmpty, szServer, NULL, &sz);
299   ok( r == FALSE, "return wrong\n");
300
301   sz = 0;
302   r = InternetGetCookieW(szUrl, szServer, NULL, &sz);
303   ok( r == TRUE, "return wrong\n");
304
305   /* sz is 14 on XP SP2 and beyond, 30 on XP SP1 and before */
306   ok( sz == 14 || sz == 30, "sz wrong, got %u, expected 14 or 30\n", sz);
307
308   sz = 0x20;
309   memset(buffer, 0, sizeof buffer);
310   r = InternetGetCookieW(szUrl, szServer, buffer, &sz);
311   ok( r == TRUE, "return wrong\n");
312
313   /* sz == lstrlenW(buffer) only in XP SP1 */
314   ok( sz == 1 + lstrlenW(buffer), "sz wrong\n");
315
316   /* before XP SP2, buffer is "server; server" */
317   ok( !lstrcmpW(szExpect, buffer) || !lstrcmpW(szServer, buffer), "cookie data wrong\n");
318
319   sz = sizeof(buffer);
320   r = InternetQueryOptionA(NULL, INTERNET_OPTION_CONNECTED_STATE, buffer, &sz);
321   ok(r == TRUE, "ret %d\n", r);
322 }
323
324 /* ############################### */
325
326 START_TEST(internet)
327 {
328   test_InternetCanonicalizeUrlA();
329   test_InternetQueryOptionA();
330   test_get_cookie();
331   test_null();
332 }