wininet/tests: Add a couple more tests for GetUrlCacheEntryInfoEx.
[wine] / dlls / wininet / tests / urlcache.c
1 /*
2  * URL Cache Tests
3  *
4  * Copyright 2008 Robert Shearman for CodeWeavers
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 <stdio.h>
23 #include <stdlib.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wininet.h"
28 #include "winineti.h"
29
30 #include "wine/test.h"
31
32 #define TEST_URL    "http://urlcachetest.winehq.org/index.html"
33 #define TEST_URL1   "Visited: user@http://urlcachetest.winehq.org/index.html"
34
35 static BOOL (WINAPI *pDeleteUrlCacheEntryA)(LPCSTR);
36 static BOOL (WINAPI *pUnlockUrlCacheEntryFileA)(LPCSTR,DWORD);
37
38 static char filenameA[MAX_PATH + 1];
39 static char filenameA1[MAX_PATH + 1];
40
41 static void check_cache_entry_infoA(const char *returnedfrom, LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo)
42 {
43     ok(lpCacheEntryInfo->dwStructSize == sizeof(*lpCacheEntryInfo), "%s: dwStructSize was %d\n", returnedfrom, lpCacheEntryInfo->dwStructSize);
44     ok(!strcmp(lpCacheEntryInfo->lpszSourceUrlName, TEST_URL), "%s: lpszSourceUrlName should be %s instead of %s\n", returnedfrom, TEST_URL, lpCacheEntryInfo->lpszSourceUrlName);
45     ok(!strcmp(lpCacheEntryInfo->lpszLocalFileName, filenameA), "%s: lpszLocalFileName should be %s instead of %s\n", returnedfrom, filenameA, lpCacheEntryInfo->lpszLocalFileName);
46     ok(!strcmp(lpCacheEntryInfo->lpszFileExtension, "html"), "%s: lpszFileExtension should be html instead of %s\n", returnedfrom, lpCacheEntryInfo->lpszFileExtension);
47 }
48
49 static void test_find_url_cache_entriesA(void)
50 {
51     BOOL ret;
52     HANDLE hEnumHandle;
53     BOOL found = FALSE;
54     DWORD cbCacheEntryInfo;
55     DWORD cbCacheEntryInfoSaved;
56     LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo;
57
58     cbCacheEntryInfo = 0;
59     SetLastError(0xdeadbeef);
60     hEnumHandle = FindFirstUrlCacheEntry(NULL, NULL, &cbCacheEntryInfo);
61     ok(!hEnumHandle, "FindFirstUrlCacheEntry should have failed\n");
62     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "FindFirstUrlCacheEntry should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
63     lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo * sizeof(char));
64     cbCacheEntryInfoSaved = cbCacheEntryInfo;
65     hEnumHandle = FindFirstUrlCacheEntry(NULL, lpCacheEntryInfo, &cbCacheEntryInfo);
66     ok(hEnumHandle != NULL, "FindFirstUrlCacheEntry failed with error %d\n", GetLastError());
67     while (TRUE)
68     {
69         if (!strcmp(lpCacheEntryInfo->lpszSourceUrlName, TEST_URL))
70         {
71             found = TRUE;
72             break;
73         }
74         SetLastError(0xdeadbeef);
75         cbCacheEntryInfo = cbCacheEntryInfoSaved;
76         ret = FindNextUrlCacheEntry(hEnumHandle, lpCacheEntryInfo, &cbCacheEntryInfo);
77         if (!ret)
78         {
79             if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
80             {
81                 lpCacheEntryInfo = HeapReAlloc(GetProcessHeap(), 0, lpCacheEntryInfo, cbCacheEntryInfo);
82                 cbCacheEntryInfoSaved = cbCacheEntryInfo;
83                 ret = FindNextUrlCacheEntry(hEnumHandle, lpCacheEntryInfo, &cbCacheEntryInfo);
84             }
85         }
86         ok(ret, "FindNextUrlCacheEntry failed with error %d\n", GetLastError());
87         if (!ret)
88             break;
89     }
90     ok(found, "committed url cache entry not found during enumeration\n");
91
92     ret = FindCloseUrlCache(hEnumHandle);
93     ok(ret, "FindCloseUrlCache failed with error %d\n", GetLastError());
94 }
95
96 static void test_GetUrlCacheEntryInfoExA(void)
97 {
98     BOOL ret;
99     DWORD cbCacheEntryInfo, cbRedirectUrl;
100     LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo;
101
102     SetLastError(0xdeadbeef);
103     ret = GetUrlCacheEntryInfoEx(NULL, NULL, NULL, NULL, NULL, NULL, 0);
104     ok(!ret, "GetUrlCacheEntryInfoEx with NULL URL and NULL args should have failed\n");
105     ok(GetLastError() == ERROR_INVALID_PARAMETER,
106        "GetUrlCacheEntryInfoEx with NULL URL and NULL args should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
107
108     cbCacheEntryInfo = sizeof(INTERNET_CACHE_ENTRY_INFO);
109     SetLastError(0xdeadbeef);
110     ret = GetUrlCacheEntryInfoEx("", NULL, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
111     ok(!ret, "GetUrlCacheEntryInfoEx with zero-length buffer should fail\n");
112     ok(GetLastError() == ERROR_FILE_NOT_FOUND,
113        "GetUrlCacheEntryInfoEx should have set last error to ERROR_FILE_NOT_FOUND instead of %d\n", GetLastError());
114
115     ret = GetUrlCacheEntryInfoEx(TEST_URL, NULL, NULL, NULL, NULL, NULL, 0);
116     ok(ret, "GetUrlCacheEntryInfoEx with NULL args failed with error %d\n", GetLastError());
117
118     cbCacheEntryInfo = 0;
119     SetLastError(0xdeadbeef);
120     ret = GetUrlCacheEntryInfoEx(TEST_URL, NULL, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
121     ok(!ret, "GetUrlCacheEntryInfoEx with zero-length buffer should fail\n");
122     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
123        "GetUrlCacheEntryInfoEx should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
124
125     lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
126
127     SetLastError(0xdeadbeef);
128     ret = GetUrlCacheEntryInfoEx(TEST_URL, lpCacheEntryInfo, &cbCacheEntryInfo, NULL, NULL, NULL, 0x200);
129     ok(!ret, "GetUrlCacheEntryInfoEx succeeded\n");
130     ok(GetLastError() == ERROR_FILE_NOT_FOUND,
131        "GetUrlCacheEntryInfoEx should have set last error to ERROR_FILE_NOT_FOUND instead of %d\n", GetLastError());
132
133     ret = GetUrlCacheEntryInfoEx(TEST_URL, lpCacheEntryInfo, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
134     ok(ret, "GetUrlCacheEntryInfoEx failed with error %d\n", GetLastError());
135
136     check_cache_entry_infoA("GetUrlCacheEntryInfoEx", lpCacheEntryInfo);
137
138     cbCacheEntryInfo = 100000;
139     SetLastError(0xdeadbeef);
140     ret = GetUrlCacheEntryInfoEx(TEST_URL, NULL, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
141     ok(!ret, "GetUrlCacheEntryInfoEx with zero-length buffer should fail\n");
142     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetUrlCacheEntryInfoEx should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
143
144     HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
145
146     /* Querying the redirect URL fails with ERROR_INVALID_PARAMETER */
147     SetLastError(0xdeadbeef);
148     ret = GetUrlCacheEntryInfoEx(TEST_URL, NULL, NULL, NULL, &cbRedirectUrl, NULL, 0);
149     ok(GetLastError() == ERROR_INVALID_PARAMETER,
150        "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
151     SetLastError(0xdeadbeef);
152     ret = GetUrlCacheEntryInfoEx(TEST_URL, NULL, &cbCacheEntryInfo, NULL, &cbRedirectUrl, NULL, 0);
153     ok(GetLastError() == ERROR_INVALID_PARAMETER,
154        "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
155 }
156
157 static void test_RetrieveUrlCacheEntryA(void)
158 {
159     BOOL ret;
160     DWORD cbCacheEntryInfo;
161
162     cbCacheEntryInfo = 0;
163     SetLastError(0xdeadbeef);
164     ret = RetrieveUrlCacheEntryFile(NULL, NULL, &cbCacheEntryInfo, 0);
165     ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
166     ok(GetLastError() == ERROR_INVALID_PARAMETER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
167
168     if (0)
169     {
170         /* Crashes on Win9x, NT4 and W2K */
171         SetLastError(0xdeadbeef);
172         ret = RetrieveUrlCacheEntryFile(TEST_URL, NULL, NULL, 0);
173         ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
174         ok(GetLastError() == ERROR_INVALID_PARAMETER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
175     }
176
177     SetLastError(0xdeadbeef);
178     cbCacheEntryInfo = 100000;
179     ret = RetrieveUrlCacheEntryFile(NULL, NULL, &cbCacheEntryInfo, 0);
180     ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
181     ok(GetLastError() == ERROR_INVALID_PARAMETER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
182 }
183
184 static void test_urlcacheA(void)
185 {
186     BOOL ret;
187     HANDLE hFile;
188     DWORD written;
189     BYTE zero_byte = 0;
190     LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo;
191     DWORD cbCacheEntryInfo;
192     static const FILETIME filetime_zero;
193
194     ret = CreateUrlCacheEntry(TEST_URL, 0, "html", filenameA, 0);
195     ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
196
197     ret = CreateUrlCacheEntry(TEST_URL, 0, "html", filenameA1, 0);
198     ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
199
200     ok(lstrcmpiA(filenameA, filenameA1), "expected a different file name\n");
201
202     hFile = CreateFileA(filenameA, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
203                         NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
204     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA failed with error %d\n", GetLastError());
205
206     ret = WriteFile(hFile, &zero_byte, sizeof(zero_byte), &written, NULL);
207     ok(ret, "WriteFile failed with error %d\n", GetLastError());
208
209     CloseHandle(hFile);
210
211     ret = CommitUrlCacheEntry(TEST_URL1, NULL, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY|URLHISTORY_CACHE_ENTRY, NULL, 0, NULL, NULL);
212     ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
213
214     ret = CommitUrlCacheEntry(TEST_URL1, NULL, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY|URLHISTORY_CACHE_ENTRY, NULL, 0, NULL, NULL);
215     ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
216
217     ret = CommitUrlCacheEntry(TEST_URL, filenameA, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY, NULL, 0, "html", NULL);
218     ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
219
220     cbCacheEntryInfo = 0;
221     SetLastError(0xdeadbeef);
222     ret = RetrieveUrlCacheEntryFile(TEST_URL, NULL, &cbCacheEntryInfo, 0);
223     ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
224     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
225        "RetrieveUrlCacheEntryFile should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
226
227     lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
228     ret = RetrieveUrlCacheEntryFile(TEST_URL, lpCacheEntryInfo, &cbCacheEntryInfo, 0);
229     ok(ret, "RetrieveUrlCacheEntryFile failed with error %d\n", GetLastError());
230
231     check_cache_entry_infoA("RetrieveUrlCacheEntryFile", lpCacheEntryInfo);
232
233     HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
234
235     cbCacheEntryInfo = 0;
236     SetLastError(0xdeadbeef);
237     ret = RetrieveUrlCacheEntryFile(TEST_URL1, NULL, &cbCacheEntryInfo, 0);
238     ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
239     ok(GetLastError() == ERROR_INVALID_DATA,
240        "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_DATA instead of %d\n", GetLastError());
241
242     if (pUnlockUrlCacheEntryFileA)
243     {
244         ret = pUnlockUrlCacheEntryFileA(TEST_URL, 0);
245         ok(ret, "UnlockUrlCacheEntryFileA failed with error %d\n", GetLastError());
246     }
247
248     /* test Find*UrlCacheEntry functions */
249     test_find_url_cache_entriesA();
250
251     test_GetUrlCacheEntryInfoExA();
252     test_RetrieveUrlCacheEntryA();
253
254     if (pDeleteUrlCacheEntryA)
255     {
256         ret = pDeleteUrlCacheEntryA(TEST_URL);
257         ok(ret, "DeleteUrlCacheEntryA failed with error %d\n", GetLastError());
258         ret = pDeleteUrlCacheEntryA(TEST_URL1);
259         ok(ret, "DeleteUrlCacheEntryA failed with error %d\n", GetLastError());
260     }
261
262     SetLastError(0xdeadbeef);
263     ret = DeleteFile(filenameA);
264     todo_wine
265     ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "local file should no longer exist\n");
266 }
267
268 static void test_FindCloseUrlCache(void)
269 {
270     BOOL r;
271     DWORD err;
272
273     SetLastError(0xdeadbeef);
274     r = FindCloseUrlCache(NULL);
275     err = GetLastError();
276     ok(0 == r, "expected 0, got %d\n", r);
277     ok(ERROR_INVALID_HANDLE == err, "expected %d, got %d\n", ERROR_INVALID_HANDLE, err);
278 }
279
280 static void test_GetDiskInfoA(void)
281 {
282     BOOL ret;
283     DWORD error, cluster_size;
284     DWORDLONG free, total;
285     char path[MAX_PATH], *p;
286
287     GetSystemDirectoryA(path, MAX_PATH);
288     if ((p = strchr(path, '\\'))) *++p = 0;
289
290     ret = GetDiskInfoA(path, &cluster_size, &free, &total);
291     ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
292
293     ret = GetDiskInfoA(path, &cluster_size, &free, NULL);
294     ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
295
296     ret = GetDiskInfoA(path, &cluster_size, NULL, NULL);
297     ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
298
299     ret = GetDiskInfoA(path, NULL, NULL, NULL);
300     ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
301
302     SetLastError(0xdeadbeef);
303     strcpy(p, "\\non\\existing\\path");
304     ret = GetDiskInfoA(path, NULL, NULL, NULL);
305     error = GetLastError();
306     ok(!ret ||
307        broken(ret), /* < IE7 */
308        "GetDiskInfoA succeeded\n");
309     ok(error == ERROR_PATH_NOT_FOUND ||
310        broken(error == 0xdeadbeef), /* < IE7 */
311        "got %u expected ERROR_PATH_NOT_FOUND\n", error);
312
313     SetLastError(0xdeadbeef);
314     ret = GetDiskInfoA(NULL, NULL, NULL, NULL);
315     error = GetLastError();
316     ok(!ret, "GetDiskInfoA succeeded\n");
317     ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
318 }
319
320 START_TEST(urlcache)
321 {
322     HMODULE hdll;
323     hdll = GetModuleHandleA("wininet.dll");
324
325     if(!GetProcAddress(hdll, "InternetGetCookieExW")) {
326         win_skip("Too old IE (older than 6.0)\n");
327         return;
328     }
329
330     pDeleteUrlCacheEntryA = (void*)GetProcAddress(hdll, "DeleteUrlCacheEntryA");
331     pUnlockUrlCacheEntryFileA = (void*)GetProcAddress(hdll, "UnlockUrlCacheEntryFileA");
332     test_urlcacheA();
333     test_FindCloseUrlCache();
334     test_GetDiskInfoA();
335 }