[WININET_WINETEST] Sync with Wine Staging 4.18. CORE-16441
[reactos.git] / modules / rostests / winetests / wininet / 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 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winnls.h"
31 #include "wininet.h"
32 #include "winineti.h"
33 #include "shlobj.h"
34
35 #include "wine/test.h"
36
37 static const char test_url[] = "http://urlcachetest.winehq.org/index.html";
38 static const WCHAR test_urlW[] = {'h','t','t','p',':','/','/','u','r','l','c','a','c','h','e','t','e','s','t','.',
39 'w','i','n','e','h','q','.','o','r','g','/','i','n','d','e','x','.','h','t','m','l',0};
40 static const char test_url1[] = "Visited: user@http://urlcachetest.winehq.org/index.html";
41 static const char test_hash_collisions1[] = "Visited: http://winehq.org/doc0.html";
42 static const char test_hash_collisions2[] = "Visited: http://winehq.org/doc75651909.html";
43
44 static BOOL (WINAPI *pDeleteUrlCacheEntryA)(LPCSTR);
45 static BOOL (WINAPI *pUnlockUrlCacheEntryFileA)(LPCSTR,DWORD);
46
47 static char filenameA[MAX_PATH + 1];
48 static char filenameA1[MAX_PATH + 1];
49 static BOOL old_ie = FALSE;
50 static BOOL ie10_cache = FALSE;
51
52 static void check_cache_entry_infoA(const char *returnedfrom, INTERNET_CACHE_ENTRY_INFOA *lpCacheEntryInfo)
53 {
54 ok(lpCacheEntryInfo->dwStructSize == sizeof(*lpCacheEntryInfo), "%s: dwStructSize was %d\n", returnedfrom, lpCacheEntryInfo->dwStructSize);
55 ok(!strcmp(lpCacheEntryInfo->lpszSourceUrlName, test_url), "%s: lpszSourceUrlName should be %s instead of %s\n", returnedfrom, test_url, lpCacheEntryInfo->lpszSourceUrlName);
56 ok(!strcmp(lpCacheEntryInfo->lpszLocalFileName, filenameA), "%s: lpszLocalFileName should be %s instead of %s\n", returnedfrom, filenameA, lpCacheEntryInfo->lpszLocalFileName);
57 ok(!strcmp(lpCacheEntryInfo->lpszFileExtension, "html"), "%s: lpszFileExtension should be html instead of %s\n", returnedfrom, lpCacheEntryInfo->lpszFileExtension);
58 }
59
60 static void test_find_url_cache_entriesA(void)
61 {
62 BOOL ret;
63 HANDLE hEnumHandle;
64 BOOL found = FALSE;
65 DWORD cbCacheEntryInfo;
66 DWORD cbCacheEntryInfoSaved;
67 INTERNET_CACHE_ENTRY_INFOA *lpCacheEntryInfo;
68
69 cbCacheEntryInfo = 0;
70 SetLastError(0xdeadbeef);
71 hEnumHandle = FindFirstUrlCacheEntryA(NULL, NULL, &cbCacheEntryInfo);
72 ok(!hEnumHandle, "FindFirstUrlCacheEntry should have failed\n");
73 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "FindFirstUrlCacheEntry should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
74 lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo * sizeof(char));
75 cbCacheEntryInfoSaved = cbCacheEntryInfo;
76 hEnumHandle = FindFirstUrlCacheEntryA(NULL, lpCacheEntryInfo, &cbCacheEntryInfo);
77 ok(hEnumHandle != NULL, "FindFirstUrlCacheEntry failed with error %d\n", GetLastError());
78 while (TRUE)
79 {
80 if (!strcmp(lpCacheEntryInfo->lpszSourceUrlName, test_url))
81 {
82 found = TRUE;
83 ret = TRUE;
84 break;
85 }
86 SetLastError(0xdeadbeef);
87 cbCacheEntryInfo = cbCacheEntryInfoSaved;
88 ret = FindNextUrlCacheEntryA(hEnumHandle, lpCacheEntryInfo, &cbCacheEntryInfo);
89 if (!ret)
90 {
91 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
92 {
93 lpCacheEntryInfo = HeapReAlloc(GetProcessHeap(), 0, lpCacheEntryInfo, cbCacheEntryInfo);
94 cbCacheEntryInfoSaved = cbCacheEntryInfo;
95 ret = FindNextUrlCacheEntryA(hEnumHandle, lpCacheEntryInfo, &cbCacheEntryInfo);
96 }
97 }
98 if (!ret)
99 break;
100 }
101 ok(ret, "FindNextUrlCacheEntry failed with error %d\n", GetLastError());
102 ok(found, "Committed url cache entry not found during enumeration\n");
103
104 ret = FindCloseUrlCache(hEnumHandle);
105 ok(ret, "FindCloseUrlCache failed with error %d\n", GetLastError());
106 HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
107 }
108
109 static void test_GetUrlCacheEntryInfoExA(void)
110 {
111 BOOL ret;
112 DWORD cbCacheEntryInfo, cbRedirectUrl;
113 INTERNET_CACHE_ENTRY_INFOA *lpCacheEntryInfo;
114
115 SetLastError(0xdeadbeef);
116 ret = GetUrlCacheEntryInfoExA(NULL, NULL, NULL, NULL, NULL, NULL, 0);
117 ok(!ret, "GetUrlCacheEntryInfoEx with NULL URL and NULL args should have failed\n");
118 ok(GetLastError() == ERROR_INVALID_PARAMETER,
119 "GetUrlCacheEntryInfoEx with NULL URL and NULL args should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
120
121 cbCacheEntryInfo = sizeof(INTERNET_CACHE_ENTRY_INFOA);
122 SetLastError(0xdeadbeef);
123 ret = GetUrlCacheEntryInfoExA("", NULL, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
124 ok(!ret, "GetUrlCacheEntryInfoEx with zero-length buffer should fail\n");
125 ok(GetLastError() == ERROR_FILE_NOT_FOUND,
126 "GetUrlCacheEntryInfoEx should have set last error to ERROR_FILE_NOT_FOUND instead of %d\n", GetLastError());
127
128 ret = GetUrlCacheEntryInfoExA(test_url, NULL, NULL, NULL, NULL, NULL, 0);
129 ok(ret, "GetUrlCacheEntryInfoEx with NULL args failed with error %d\n", GetLastError());
130
131 cbCacheEntryInfo = 0;
132 SetLastError(0xdeadbeef);
133 ret = GetUrlCacheEntryInfoExA(test_url, NULL, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
134 ok(!ret, "GetUrlCacheEntryInfoEx with zero-length buffer should fail\n");
135 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
136 "GetUrlCacheEntryInfoEx should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
137
138 lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
139
140 SetLastError(0xdeadbeef);
141 ret = GetUrlCacheEntryInfoExA(test_url, NULL, NULL, NULL, NULL, NULL, 0x200 /*GET_INSTALLED_ENTRY*/);
142 ok(ret == ie10_cache, "GetUrlCacheEntryInfoEx returned %x\n", ret);
143 if (!ret) ok(GetLastError() == ERROR_FILE_NOT_FOUND,
144 "GetUrlCacheEntryInfoEx should have set last error to ERROR_FILE_NOT_FOUND instead of %d\n", GetLastError());
145
146 /* Unicode version of function seems to ignore 0x200 flag */
147 ret = GetUrlCacheEntryInfoExW(test_urlW, NULL, NULL, NULL, NULL, NULL, 0x200 /*GET_INSTALLED_ENTRY*/);
148 ok(ret || broken(old_ie && !ret), "GetUrlCacheEntryInfoExW failed with error %d\n", GetLastError());
149
150 ret = GetUrlCacheEntryInfoExA(test_url, lpCacheEntryInfo, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
151 ok(ret, "GetUrlCacheEntryInfoEx failed with error %d\n", GetLastError());
152
153 if (ret) check_cache_entry_infoA("GetUrlCacheEntryInfoEx", lpCacheEntryInfo);
154
155 lpCacheEntryInfo->CacheEntryType |= 0x10000000; /* INSTALLED_CACHE_ENTRY */
156 ret = SetUrlCacheEntryInfoA(test_url, lpCacheEntryInfo, CACHE_ENTRY_ATTRIBUTE_FC);
157 ok(ret, "SetUrlCacheEntryInfoA failed with error %d\n", GetLastError());
158
159 SetLastError(0xdeadbeef);
160 ret = GetUrlCacheEntryInfoExA(test_url, NULL, NULL, NULL, NULL, NULL, 0x200 /*GET_INSTALLED_ENTRY*/);
161 ok(ret, "GetUrlCacheEntryInfoEx failed with error %d\n", GetLastError());
162
163 cbCacheEntryInfo = 100000;
164 SetLastError(0xdeadbeef);
165 ret = GetUrlCacheEntryInfoExA(test_url, NULL, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
166 ok(!ret, "GetUrlCacheEntryInfoEx with zero-length buffer should fail\n");
167 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetUrlCacheEntryInfoEx should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
168
169 HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
170
171 /* Querying the redirect URL fails with ERROR_INVALID_PARAMETER */
172 SetLastError(0xdeadbeef);
173 ret = GetUrlCacheEntryInfoExA(test_url, NULL, NULL, NULL, &cbRedirectUrl, NULL, 0);
174 ok(!ret, "GetUrlCacheEntryInfoEx should have failed\n");
175 ok(GetLastError() == ERROR_INVALID_PARAMETER,
176 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
177 SetLastError(0xdeadbeef);
178 ret = GetUrlCacheEntryInfoExA(test_url, NULL, &cbCacheEntryInfo, NULL, &cbRedirectUrl, NULL, 0);
179 ok(!ret, "GetUrlCacheEntryInfoEx should have failed\n");
180 ok(GetLastError() == ERROR_INVALID_PARAMETER,
181 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
182 }
183
184 static void test_RetrieveUrlCacheEntryA(void)
185 {
186 BOOL ret;
187 DWORD cbCacheEntryInfo;
188
189 cbCacheEntryInfo = 0;
190 SetLastError(0xdeadbeef);
191 ret = RetrieveUrlCacheEntryFileA(NULL, NULL, &cbCacheEntryInfo, 0);
192 ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
193 ok(GetLastError() == ERROR_INVALID_PARAMETER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
194
195 if (0)
196 {
197 /* Crashes on Win9x, NT4 and W2K */
198 SetLastError(0xdeadbeef);
199 ret = RetrieveUrlCacheEntryFileA(test_url, NULL, NULL, 0);
200 ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
201 ok(GetLastError() == ERROR_INVALID_PARAMETER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
202 }
203
204 SetLastError(0xdeadbeef);
205 cbCacheEntryInfo = 100000;
206 ret = RetrieveUrlCacheEntryFileA(NULL, NULL, &cbCacheEntryInfo, 0);
207 ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
208 ok(GetLastError() == ERROR_INVALID_PARAMETER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
209 }
210
211 static void test_IsUrlCacheEntryExpiredA(void)
212 {
213 static const char uncached_url[] =
214 "What's the airspeed velocity of an unladen swallow?";
215 BOOL ret;
216 FILETIME ft;
217 DWORD size;
218 INTERNET_CACHE_ENTRY_INFOA *info;
219 ULARGE_INTEGER exp_time;
220
221 /* The function returns TRUE when the output time is NULL or the tested URL
222 * is NULL.
223 */
224 ret = IsUrlCacheEntryExpiredA(NULL, 0, NULL);
225 ok(ret != ie10_cache, "IsUrlCacheEntryExpiredA returned %x\n", ret);
226 ft.dwLowDateTime = 0xdeadbeef;
227 ft.dwHighDateTime = 0xbaadf00d;
228 ret = IsUrlCacheEntryExpiredA(NULL, 0, &ft);
229 ok(ret != ie10_cache, "IsUrlCacheEntryExpiredA returned %x\n", ret);
230 ok(ft.dwLowDateTime == 0xdeadbeef && ft.dwHighDateTime == 0xbaadf00d,
231 "expected time to be unchanged, got (%u,%u)\n",
232 ft.dwLowDateTime, ft.dwHighDateTime);
233 ret = IsUrlCacheEntryExpiredA(test_url, 0, NULL);
234 ok(ret != ie10_cache, "IsUrlCacheEntryExpiredA returned %x\n", ret);
235
236 /* The return value should indicate whether the URL is expired,
237 * and the filetime indicates the last modified time, but a cache entry
238 * with a zero expire time is "not expired".
239 */
240 ft.dwLowDateTime = 0xdeadbeef;
241 ft.dwHighDateTime = 0xbaadf00d;
242 ret = IsUrlCacheEntryExpiredA(test_url, 0, &ft);
243 ok(!ret, "expected FALSE\n");
244 ok(!ft.dwLowDateTime && !ft.dwHighDateTime,
245 "expected time (0,0), got (%u,%u)\n",
246 ft.dwLowDateTime, ft.dwHighDateTime);
247
248 /* Same behavior with bogus flags. */
249 ft.dwLowDateTime = 0xdeadbeef;
250 ft.dwHighDateTime = 0xbaadf00d;
251 ret = IsUrlCacheEntryExpiredA(test_url, 0xffffffff, &ft);
252 ok(!ret, "expected FALSE\n");
253 ok(!ft.dwLowDateTime && !ft.dwHighDateTime,
254 "expected time (0,0), got (%u,%u)\n",
255 ft.dwLowDateTime, ft.dwHighDateTime);
256
257 /* Set the expire time to a point in the past.. */
258 ret = GetUrlCacheEntryInfoA(test_url, NULL, &size);
259 ok(!ret, "GetUrlCacheEntryInfo should have failed\n");
260 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
261 "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
262 info = HeapAlloc(GetProcessHeap(), 0, size);
263 ret = GetUrlCacheEntryInfoA(test_url, info, &size);
264 ok(ret, "GetUrlCacheEntryInfo failed: %d\n", GetLastError());
265 GetSystemTimeAsFileTime(&info->ExpireTime);
266 exp_time.u.LowPart = info->ExpireTime.dwLowDateTime;
267 exp_time.u.HighPart = info->ExpireTime.dwHighDateTime;
268 exp_time.QuadPart -= 10 * 60 * (ULONGLONG)10000000;
269 info->ExpireTime.dwLowDateTime = exp_time.u.LowPart;
270 info->ExpireTime.dwHighDateTime = exp_time.u.HighPart;
271 ret = SetUrlCacheEntryInfoA(test_url, info, CACHE_ENTRY_EXPTIME_FC);
272 ok(ret, "SetUrlCacheEntryInfo failed: %d\n", GetLastError());
273 ft.dwLowDateTime = 0xdeadbeef;
274 ft.dwHighDateTime = 0xbaadf00d;
275 /* and the entry should be expired. */
276 ret = IsUrlCacheEntryExpiredA(test_url, 0, &ft);
277 ok(ret, "expected TRUE\n");
278 /* The modified time returned is 0. */
279 ok(!ft.dwLowDateTime && !ft.dwHighDateTime,
280 "expected time (0,0), got (%u,%u)\n",
281 ft.dwLowDateTime, ft.dwHighDateTime);
282 /* Set the expire time to a point in the future.. */
283 exp_time.QuadPart += 20 * 60 * (ULONGLONG)10000000;
284 info->ExpireTime.dwLowDateTime = exp_time.u.LowPart;
285 info->ExpireTime.dwHighDateTime = exp_time.u.HighPart;
286 ret = SetUrlCacheEntryInfoA(test_url, info, CACHE_ENTRY_EXPTIME_FC);
287 ok(ret, "SetUrlCacheEntryInfo failed: %d\n", GetLastError());
288 ft.dwLowDateTime = 0xdeadbeef;
289 ft.dwHighDateTime = 0xbaadf00d;
290 /* and the entry should no longer be expired. */
291 ret = IsUrlCacheEntryExpiredA(test_url, 0, &ft);
292 ok(!ret, "expected FALSE\n");
293 /* The modified time returned is still 0. */
294 ok(!ft.dwLowDateTime && !ft.dwHighDateTime,
295 "expected time (0,0), got (%u,%u)\n",
296 ft.dwLowDateTime, ft.dwHighDateTime);
297 /* Set the modified time... */
298 GetSystemTimeAsFileTime(&info->LastModifiedTime);
299 ret = SetUrlCacheEntryInfoA(test_url, info, CACHE_ENTRY_MODTIME_FC);
300 ok(ret, "SetUrlCacheEntryInfo failed: %d\n", GetLastError());
301 /* and the entry should still be unexpired.. */
302 ret = IsUrlCacheEntryExpiredA(test_url, 0, &ft);
303 ok(!ret, "expected FALSE\n");
304 /* but the modified time returned is the last modified time just set. */
305 ok(ft.dwLowDateTime == info->LastModifiedTime.dwLowDateTime &&
306 ft.dwHighDateTime == info->LastModifiedTime.dwHighDateTime,
307 "expected time (%u,%u), got (%u,%u)\n",
308 info->LastModifiedTime.dwLowDateTime,
309 info->LastModifiedTime.dwHighDateTime,
310 ft.dwLowDateTime, ft.dwHighDateTime);
311 HeapFree(GetProcessHeap(), 0, info);
312
313 /* An uncached URL is implicitly expired, but with unknown time. */
314 ft.dwLowDateTime = 0xdeadbeef;
315 ft.dwHighDateTime = 0xbaadf00d;
316 ret = IsUrlCacheEntryExpiredA(uncached_url, 0, &ft);
317 ok(ret != ie10_cache, "IsUrlCacheEntryExpiredA returned %x\n", ret);
318 ok(!ft.dwLowDateTime && !ft.dwHighDateTime,
319 "expected time (0,0), got (%u,%u)\n",
320 ft.dwLowDateTime, ft.dwHighDateTime);
321 }
322
323 static void _check_file_exists(LONG l, LPCSTR filename)
324 {
325 HANDLE file;
326
327 file = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
328 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
329 ok_(__FILE__,l)(file != INVALID_HANDLE_VALUE,
330 "expected file to exist, CreateFile failed with error %d\n",
331 GetLastError());
332 CloseHandle(file);
333 }
334
335 #define check_file_exists(f) _check_file_exists(__LINE__, f)
336
337 static void _check_file_not_exists(LONG l, LPCSTR filename)
338 {
339 HANDLE file;
340
341 file = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
342 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
343 ok_(__FILE__,l)(file == INVALID_HANDLE_VALUE,
344 "expected file not to exist\n");
345 if (file != INVALID_HANDLE_VALUE)
346 CloseHandle(file);
347 }
348
349 #define check_file_not_exists(f) _check_file_not_exists(__LINE__, f)
350
351 static void create_and_write_file(LPCSTR filename, void *data, DWORD len)
352 {
353 HANDLE file;
354 DWORD written;
355 BOOL ret;
356
357 file = CreateFileA(filename, GENERIC_WRITE,
358 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
359 FILE_ATTRIBUTE_NORMAL, NULL);
360 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed with error %d\n", GetLastError());
361
362 ret = WriteFile(file, data, len, &written, NULL);
363 ok(ret, "WriteFile failed with error %d\n", GetLastError());
364
365 CloseHandle(file);
366 }
367
368 static void test_urlcacheA(void)
369 {
370 static char long_url[300] = "http://www.winehq.org/";
371 static char ok_header[] = "HTTP/1.0 200 OK\r\n\r\n";
372 BOOL ret;
373 HANDLE hFile;
374 BYTE zero_byte = 0;
375 INTERNET_CACHE_ENTRY_INFOA *lpCacheEntryInfo;
376 INTERNET_CACHE_ENTRY_INFOA *lpCacheEntryInfo2;
377 DWORD cbCacheEntryInfo;
378 static const FILETIME filetime_zero;
379 FILETIME now;
380 int len;
381
382 ret = CreateUrlCacheEntryA(test_url, 0, "html", filenameA, 0);
383 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
384
385 ret = CreateUrlCacheEntryA(test_url, 0, "html", filenameA1, 0);
386 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
387 check_file_exists(filenameA1);
388 DeleteFileA(filenameA1);
389
390 ok(lstrcmpiA(filenameA, filenameA1), "expected a different file name\n");
391
392 create_and_write_file(filenameA, &zero_byte, sizeof(zero_byte));
393
394 ret = CommitUrlCacheEntryA(test_url1, NULL, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY, NULL, 0, "html", NULL);
395 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
396 cbCacheEntryInfo = 0;
397 ret = GetUrlCacheEntryInfoA(test_url1, NULL, &cbCacheEntryInfo);
398 ok(!ret, "GetUrlCacheEntryInfo should have failed\n");
399 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
400 "GetUrlCacheEntryInfo should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
401 lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
402 ret = GetUrlCacheEntryInfoA(test_url1, lpCacheEntryInfo, &cbCacheEntryInfo);
403 ok(ret, "GetUrlCacheEntryInfo failed with error %d\n", GetLastError());
404 ok(!memcmp(&lpCacheEntryInfo->ExpireTime, &filetime_zero, sizeof(FILETIME)),
405 "expected zero ExpireTime\n");
406 ok(!memcmp(&lpCacheEntryInfo->LastModifiedTime, &filetime_zero, sizeof(FILETIME)),
407 "expected zero LastModifiedTime\n");
408 ok(lpCacheEntryInfo->CacheEntryType == (NORMAL_CACHE_ENTRY|URLHISTORY_CACHE_ENTRY) ||
409 broken(lpCacheEntryInfo->CacheEntryType == NORMAL_CACHE_ENTRY /* NT4/W2k */),
410 "expected type NORMAL_CACHE_ENTRY|URLHISTORY_CACHE_ENTRY, got %08x\n",
411 lpCacheEntryInfo->CacheEntryType);
412 ok(!U(*lpCacheEntryInfo).dwExemptDelta, "expected dwExemptDelta 0, got %d\n",
413 U(*lpCacheEntryInfo).dwExemptDelta);
414
415 /* Make sure there is a notable change in timestamps */
416 Sleep(1000);
417
418 /* A subsequent commit with a different time/type doesn't change most of the entry */
419 GetSystemTimeAsFileTime(&now);
420 ret = CommitUrlCacheEntryA(test_url1, NULL, now, now, NORMAL_CACHE_ENTRY,
421 (LPBYTE)ok_header, strlen(ok_header), NULL, NULL);
422 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
423 cbCacheEntryInfo = 0;
424 ret = GetUrlCacheEntryInfoA(test_url1, NULL, &cbCacheEntryInfo);
425 ok(!ret, "GetUrlCacheEntryInfo should have failed\n");
426 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
427 "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
428 lpCacheEntryInfo2 = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
429 ret = GetUrlCacheEntryInfoA(test_url1, lpCacheEntryInfo2, &cbCacheEntryInfo);
430 ok(ret, "GetUrlCacheEntryInfo failed with error %d\n", GetLastError());
431 /* but it does change the time.. */
432 ok(memcmp(&lpCacheEntryInfo2->ExpireTime, &filetime_zero, sizeof(FILETIME)),
433 "expected positive ExpireTime\n");
434 ok(memcmp(&lpCacheEntryInfo2->LastModifiedTime, &filetime_zero, sizeof(FILETIME)),
435 "expected positive LastModifiedTime\n");
436 ok(lpCacheEntryInfo2->CacheEntryType == (NORMAL_CACHE_ENTRY|URLHISTORY_CACHE_ENTRY) ||
437 broken(lpCacheEntryInfo2->CacheEntryType == NORMAL_CACHE_ENTRY /* NT4/W2k */),
438 "expected type NORMAL_CACHE_ENTRY|URLHISTORY_CACHE_ENTRY, got %08x\n",
439 lpCacheEntryInfo2->CacheEntryType);
440 /* and set the headers. */
441 ok(lpCacheEntryInfo2->dwHeaderInfoSize == 19,
442 "expected headers size 19, got %d\n",
443 lpCacheEntryInfo2->dwHeaderInfoSize);
444 /* Hit rate gets incremented by 1 */
445 ok((lpCacheEntryInfo->dwHitRate + 1) == lpCacheEntryInfo2->dwHitRate,
446 "HitRate not incremented by one on commit\n");
447 /* Last access time should be updated */
448 ok(!(lpCacheEntryInfo->LastAccessTime.dwHighDateTime == lpCacheEntryInfo2->LastAccessTime.dwHighDateTime &&
449 lpCacheEntryInfo->LastAccessTime.dwLowDateTime == lpCacheEntryInfo2->LastAccessTime.dwLowDateTime),
450 "Last accessed time was not updated by commit\n");
451 /* File extension should be unset */
452 ok(lpCacheEntryInfo2->lpszFileExtension == NULL,
453 "Fileextension isn't unset: %s\n",
454 lpCacheEntryInfo2->lpszFileExtension);
455 HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
456 HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo2);
457
458 ret = CommitUrlCacheEntryA(test_url, filenameA, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY, NULL, 0, "html", NULL);
459 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
460
461 cbCacheEntryInfo = 0;
462 SetLastError(0xdeadbeef);
463 ret = RetrieveUrlCacheEntryFileA(test_url, NULL, &cbCacheEntryInfo, 0);
464 ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
465 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
466 "RetrieveUrlCacheEntryFile should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
467
468 lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
469 ret = RetrieveUrlCacheEntryFileA(test_url, lpCacheEntryInfo, &cbCacheEntryInfo, 0);
470 ok(ret, "RetrieveUrlCacheEntryFile failed with error %d\n", GetLastError());
471
472 if (ret) check_cache_entry_infoA("RetrieveUrlCacheEntryFile", lpCacheEntryInfo);
473
474 HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
475
476 cbCacheEntryInfo = 0;
477 SetLastError(0xdeadbeef);
478 ret = RetrieveUrlCacheEntryFileA(test_url1, NULL, &cbCacheEntryInfo, 0);
479 ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
480 ok(GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_INSUFFICIENT_BUFFER,
481 "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_DATA instead of %d\n", GetLastError());
482
483 if (pUnlockUrlCacheEntryFileA)
484 {
485 ret = pUnlockUrlCacheEntryFileA(test_url, 0);
486 ok(ret, "UnlockUrlCacheEntryFileA failed with error %d\n", GetLastError());
487 }
488
489 /* test Find*UrlCacheEntry functions */
490 test_find_url_cache_entriesA();
491
492 test_GetUrlCacheEntryInfoExA();
493 test_RetrieveUrlCacheEntryA();
494 test_IsUrlCacheEntryExpiredA();
495
496 if (pDeleteUrlCacheEntryA)
497 {
498 ret = pDeleteUrlCacheEntryA(test_url);
499 ok(ret, "DeleteUrlCacheEntryA failed with error %d\n", GetLastError());
500 ret = pDeleteUrlCacheEntryA(test_url1);
501 ok(ret, "DeleteUrlCacheEntryA failed with error %d\n", GetLastError());
502 }
503
504 SetLastError(0xdeadbeef);
505 ret = DeleteFileA(filenameA);
506 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "local file should no longer exist\n");
507
508 /* Creating two entries with the same URL */
509 ret = CreateUrlCacheEntryA(test_url, 0, "html", filenameA, 0);
510 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
511
512 ret = CreateUrlCacheEntryA(test_url, 0, "html", filenameA1, 0);
513 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
514
515 ok(lstrcmpiA(filenameA, filenameA1), "expected a different file name\n");
516
517 create_and_write_file(filenameA, &zero_byte, sizeof(zero_byte));
518 create_and_write_file(filenameA1, &zero_byte, sizeof(zero_byte));
519 check_file_exists(filenameA);
520 check_file_exists(filenameA1);
521
522 ret = CommitUrlCacheEntryA(test_url, filenameA, filetime_zero,
523 filetime_zero, NORMAL_CACHE_ENTRY, (LPBYTE)ok_header,
524 strlen(ok_header), "html", NULL);
525 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
526 check_file_exists(filenameA);
527 check_file_exists(filenameA1);
528 ret = CommitUrlCacheEntryA(test_url, filenameA1, filetime_zero,
529 filetime_zero, COOKIE_CACHE_ENTRY, NULL, 0, "html", NULL);
530 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
531 /* By committing the same URL a second time, the prior entry is
532 * overwritten...
533 */
534 cbCacheEntryInfo = 0;
535 SetLastError(0xdeadbeef);
536 ret = GetUrlCacheEntryInfoA(test_url, NULL, &cbCacheEntryInfo);
537 ok(!ret, "GetUrlCacheEntryInfo should have failed\n");
538 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
539 "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
540 lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
541 ret = GetUrlCacheEntryInfoA(test_url, lpCacheEntryInfo, &cbCacheEntryInfo);
542 ok(ret, "GetUrlCacheEntryInfo failed with error %d\n", GetLastError());
543 /* with the previous entry type retained.. */
544 ok(lpCacheEntryInfo->CacheEntryType & NORMAL_CACHE_ENTRY,
545 "expected cache entry type NORMAL_CACHE_ENTRY, got %d (0x%08x)\n",
546 lpCacheEntryInfo->CacheEntryType, lpCacheEntryInfo->CacheEntryType);
547 /* and the headers overwritten.. */
548 ok(!lpCacheEntryInfo->dwHeaderInfoSize, "expected headers size 0, got %d\n",
549 lpCacheEntryInfo->dwHeaderInfoSize);
550 HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
551 /* and the previous filename shouldn't exist. */
552 check_file_not_exists(filenameA);
553 check_file_exists(filenameA1);
554
555 if (pDeleteUrlCacheEntryA)
556 {
557 ret = pDeleteUrlCacheEntryA(test_url);
558 ok(ret, "DeleteUrlCacheEntryA failed with error %d\n", GetLastError());
559 check_file_not_exists(filenameA);
560 check_file_not_exists(filenameA1);
561 /* Just in case, clean up files */
562 DeleteFileA(filenameA1);
563 DeleteFileA(filenameA);
564 }
565
566 /* Check whether a retrieved cache entry can be deleted before it's
567 * unlocked:
568 */
569 ret = CreateUrlCacheEntryA(test_url, 0, "html", filenameA, 0);
570 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
571 ret = CommitUrlCacheEntryA(test_url, filenameA, filetime_zero, filetime_zero,
572 NORMAL_CACHE_ENTRY, NULL, 0, "html", NULL);
573 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
574
575 cbCacheEntryInfo = 0;
576 SetLastError(0xdeadbeef);
577 ret = RetrieveUrlCacheEntryFileA(test_url, NULL, &cbCacheEntryInfo, 0);
578 ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
579 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
580 "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
581
582 lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
583 ret = RetrieveUrlCacheEntryFileA(test_url, lpCacheEntryInfo,
584 &cbCacheEntryInfo, 0);
585 ok(ret, "RetrieveUrlCacheEntryFile failed with error %d\n", GetLastError());
586
587 HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
588
589 if (pDeleteUrlCacheEntryA)
590 {
591 ret = pDeleteUrlCacheEntryA(test_url);
592 ok(!ret, "Expected failure\n");
593 ok(GetLastError() == ERROR_SHARING_VIOLATION,
594 "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
595 check_file_exists(filenameA);
596 }
597
598 lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
599 memset(lpCacheEntryInfo, 0, cbCacheEntryInfo);
600 ret = GetUrlCacheEntryInfoA(test_url, lpCacheEntryInfo, &cbCacheEntryInfo);
601 ok(ret, "GetUrlCacheEntryInfo failed with error %d\n", GetLastError());
602 ok(lpCacheEntryInfo->CacheEntryType & 0x400000,
603 "CacheEntryType hasn't PENDING_DELETE_CACHE_ENTRY set, (flags %08x)\n",
604 lpCacheEntryInfo->CacheEntryType);
605 HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
606
607 if (pUnlockUrlCacheEntryFileA)
608 {
609 check_file_exists(filenameA);
610 ret = pUnlockUrlCacheEntryFileA(test_url, 0);
611 ok(ret, "UnlockUrlCacheEntryFileA failed: %d\n", GetLastError());
612 /* By unlocking the already-deleted cache entry, the file associated
613 * with it is deleted..
614 */
615 check_file_not_exists(filenameA);
616 /* (just in case, delete file) */
617 DeleteFileA(filenameA);
618 }
619 if (pDeleteUrlCacheEntryA)
620 {
621 /* and a subsequent deletion should fail. */
622 ret = pDeleteUrlCacheEntryA(test_url);
623 ok(!ret, "Expected failure\n");
624 ok(GetLastError() == ERROR_FILE_NOT_FOUND,
625 "expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
626 }
627
628 /* Test whether preventing a file from being deleted causes
629 * DeleteUrlCacheEntryA to fail.
630 */
631 ret = CreateUrlCacheEntryA(test_url, 0, "html", filenameA, 0);
632 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
633
634 create_and_write_file(filenameA, &zero_byte, sizeof(zero_byte));
635 check_file_exists(filenameA);
636
637 ret = CommitUrlCacheEntryA(test_url, filenameA, filetime_zero,
638 filetime_zero, NORMAL_CACHE_ENTRY, (LPBYTE)ok_header,
639 strlen(ok_header), "html", NULL);
640 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
641 check_file_exists(filenameA);
642 hFile = CreateFileA(filenameA, GENERIC_READ, 0, NULL, OPEN_EXISTING,
643 FILE_ATTRIBUTE_NORMAL, NULL);
644 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA failed: %d\n",
645 GetLastError());
646 if (pDeleteUrlCacheEntryA)
647 {
648 /* DeleteUrlCacheEntryA should succeed.. */
649 ret = pDeleteUrlCacheEntryA(test_url);
650 ok(ret, "DeleteUrlCacheEntryA failed with error %d\n", GetLastError());
651 }
652 CloseHandle(hFile);
653 if (pDeleteUrlCacheEntryA)
654 {
655 /* and a subsequent deletion should fail.. */
656 ret = pDeleteUrlCacheEntryA(test_url);
657 ok(!ret, "Expected failure\n");
658 ok(GetLastError() == ERROR_FILE_NOT_FOUND,
659 "expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
660 }
661 /* and the file should be untouched. */
662 check_file_exists(filenameA);
663 DeleteFileA(filenameA);
664
665 /* Try creating a sticky entry. Unlike non-sticky entries, the filename
666 * must have been set already.
667 */
668 SetLastError(0xdeadbeef);
669 ret = CommitUrlCacheEntryA(test_url, NULL, filetime_zero, filetime_zero,
670 STICKY_CACHE_ENTRY, (LPBYTE)ok_header, strlen(ok_header), "html",
671 NULL);
672 ok(ret == ie10_cache, "CommitUrlCacheEntryA returned %x\n", ret);
673 if (!ret) ok(GetLastError() == ERROR_INVALID_PARAMETER,
674 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
675 SetLastError(0xdeadbeef);
676 ret = CommitUrlCacheEntryA(test_url, NULL, filetime_zero, filetime_zero,
677 NORMAL_CACHE_ENTRY|STICKY_CACHE_ENTRY,
678 (LPBYTE)ok_header, strlen(ok_header), "html", NULL);
679 ok(ret == ie10_cache, "CommitUrlCacheEntryA returned %x\n", ret);
680 if (!ret) ok(GetLastError() == ERROR_INVALID_PARAMETER,
681 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
682
683 ret = CreateUrlCacheEntryA(test_url, 0, "html", filenameA, 0);
684 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
685 create_and_write_file(filenameA, &zero_byte, sizeof(zero_byte));
686 ret = CommitUrlCacheEntryA(test_url, filenameA, filetime_zero, filetime_zero,
687 NORMAL_CACHE_ENTRY|STICKY_CACHE_ENTRY,
688 (LPBYTE)ok_header, strlen(ok_header), "html", NULL);
689 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
690 cbCacheEntryInfo = 0;
691 SetLastError(0xdeadbeef);
692 ret = GetUrlCacheEntryInfoA(test_url, NULL, &cbCacheEntryInfo);
693 ok(!ret, "GetUrlCacheEntryInfo should have failed\n");
694 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
695 "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
696 lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
697 ret = GetUrlCacheEntryInfoA(test_url, lpCacheEntryInfo, &cbCacheEntryInfo);
698 ok(ret, "GetUrlCacheEntryInfo failed with error %d\n", GetLastError());
699 ok(lpCacheEntryInfo->CacheEntryType & (NORMAL_CACHE_ENTRY|STICKY_CACHE_ENTRY),
700 "expected cache entry type NORMAL_CACHE_ENTRY | STICKY_CACHE_ENTRY, got %d (0x%08x)\n",
701 lpCacheEntryInfo->CacheEntryType, lpCacheEntryInfo->CacheEntryType);
702 ok(U(*lpCacheEntryInfo).dwExemptDelta == 86400,
703 "expected dwExemptDelta 86400, got %d\n",
704 U(*lpCacheEntryInfo).dwExemptDelta);
705 HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
706 if (pDeleteUrlCacheEntryA)
707 {
708 ret = pDeleteUrlCacheEntryA(test_url);
709 ok(ret, "DeleteUrlCacheEntryA failed with error %d\n", GetLastError());
710 /* When explicitly deleting the cache entry, the file is also deleted */
711 check_file_not_exists(filenameA);
712 }
713 /* Test once again, setting the exempt delta via SetUrlCacheEntryInfo */
714 ret = CreateUrlCacheEntryA(test_url, 0, "html", filenameA, 0);
715 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
716 create_and_write_file(filenameA, &zero_byte, sizeof(zero_byte));
717 ret = CommitUrlCacheEntryA(test_url, filenameA, filetime_zero, filetime_zero,
718 NORMAL_CACHE_ENTRY|STICKY_CACHE_ENTRY,
719 (LPBYTE)ok_header, strlen(ok_header), "html", NULL);
720 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
721 cbCacheEntryInfo = 0;
722 SetLastError(0xdeadbeef);
723 ret = GetUrlCacheEntryInfoA(test_url, NULL, &cbCacheEntryInfo);
724 ok(!ret, "GetUrlCacheEntryInfo should have failed\n");
725 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
726 "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
727 lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
728 ret = GetUrlCacheEntryInfoA(test_url, lpCacheEntryInfo, &cbCacheEntryInfo);
729 ok(ret, "GetUrlCacheEntryInfo failed with error %d\n", GetLastError());
730 ok(lpCacheEntryInfo->CacheEntryType & (NORMAL_CACHE_ENTRY|STICKY_CACHE_ENTRY),
731 "expected cache entry type NORMAL_CACHE_ENTRY | STICKY_CACHE_ENTRY, got %d (0x%08x)\n",
732 lpCacheEntryInfo->CacheEntryType, lpCacheEntryInfo->CacheEntryType);
733 ok(U(*lpCacheEntryInfo).dwExemptDelta == 86400,
734 "expected dwExemptDelta 86400, got %d\n",
735 U(*lpCacheEntryInfo).dwExemptDelta);
736 U(*lpCacheEntryInfo).dwExemptDelta = 0;
737 ret = SetUrlCacheEntryInfoA(test_url, lpCacheEntryInfo,
738 CACHE_ENTRY_EXEMPT_DELTA_FC);
739 ok(ret, "SetUrlCacheEntryInfo failed: %d\n", GetLastError());
740 ret = GetUrlCacheEntryInfoA(test_url, lpCacheEntryInfo, &cbCacheEntryInfo);
741 ok(ret, "GetUrlCacheEntryInfo failed with error %d\n", GetLastError());
742 ok(!U(*lpCacheEntryInfo).dwExemptDelta, "expected dwExemptDelta 0, got %d\n",
743 U(*lpCacheEntryInfo).dwExemptDelta);
744 /* See whether a sticky cache entry has the flag cleared once the exempt
745 * delta is meaningless.
746 */
747 ok(lpCacheEntryInfo->CacheEntryType & (NORMAL_CACHE_ENTRY|STICKY_CACHE_ENTRY),
748 "expected cache entry type NORMAL_CACHE_ENTRY | STICKY_CACHE_ENTRY, got %d (0x%08x)\n",
749 lpCacheEntryInfo->CacheEntryType, lpCacheEntryInfo->CacheEntryType);
750
751 /* Recommit of Url entry keeps dwExemptDelta */
752 U(*lpCacheEntryInfo).dwExemptDelta = 8600;
753 ret = SetUrlCacheEntryInfoA(test_url, lpCacheEntryInfo,
754 CACHE_ENTRY_EXEMPT_DELTA_FC);
755 ok(ret, "SetUrlCacheEntryInfo failed: %d\n", GetLastError());
756
757 ret = CreateUrlCacheEntryA(test_url, 0, "html", filenameA1, 0);
758 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
759 create_and_write_file(filenameA1, &zero_byte, sizeof(zero_byte));
760
761 ret = CommitUrlCacheEntryA(test_url, filenameA1, filetime_zero, filetime_zero,
762 NORMAL_CACHE_ENTRY|STICKY_CACHE_ENTRY,
763 (LPBYTE)ok_header, strlen(ok_header), "html", NULL);
764 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
765
766 ret = GetUrlCacheEntryInfoA(test_url, lpCacheEntryInfo, &cbCacheEntryInfo);
767 ok(ret, "GetUrlCacheEntryInfo failed with error %d\n", GetLastError());
768 ok(U(*lpCacheEntryInfo).dwExemptDelta == 8600 || (ie10_cache && U(*lpCacheEntryInfo).dwExemptDelta == 86400),
769 "expected dwExemptDelta 8600, got %d\n", U(*lpCacheEntryInfo).dwExemptDelta);
770
771 HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
772
773 if (pDeleteUrlCacheEntryA)
774 {
775 ret = pDeleteUrlCacheEntryA(test_url);
776 ok(ret, "DeleteUrlCacheEntryA failed with error %d\n", GetLastError());
777 check_file_not_exists(filenameA);
778 }
779
780 /* Test if files with identical hash keys are handled correctly */
781 ret = CommitUrlCacheEntryA(test_hash_collisions1, NULL, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY, NULL, 0, "html", NULL);
782 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
783 ret = CommitUrlCacheEntryA(test_hash_collisions2, NULL, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY, NULL, 0, "html", NULL);
784 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
785
786 cbCacheEntryInfo = 0;
787 ret = GetUrlCacheEntryInfoA(test_hash_collisions1, NULL, &cbCacheEntryInfo);
788 ok(!ret, "GetUrlCacheEntryInfo should have failed\n");
789 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
790 "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
791 lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
792 ret = GetUrlCacheEntryInfoA(test_hash_collisions1, lpCacheEntryInfo, &cbCacheEntryInfo);
793 ok(ret, "GetUrlCacheEntryInfo failed with error %d\n", GetLastError());
794 ok(!strcmp(lpCacheEntryInfo->lpszSourceUrlName, test_hash_collisions1),
795 "got incorrect entry: %s\n", lpCacheEntryInfo->lpszSourceUrlName);
796 HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
797
798 cbCacheEntryInfo = 0;
799 ret = GetUrlCacheEntryInfoA(test_hash_collisions2, NULL, &cbCacheEntryInfo);
800 ok(!ret, "GetUrlCacheEntryInfo should have failed\n");
801 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
802 "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
803 lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
804 ret = GetUrlCacheEntryInfoA(test_hash_collisions2, lpCacheEntryInfo, &cbCacheEntryInfo);
805 ok(ret, "GetUrlCacheEntryInfo failed with error %d\n", GetLastError());
806 ok(!strcmp(lpCacheEntryInfo->lpszSourceUrlName, test_hash_collisions2),
807 "got incorrect entry: %s\n", lpCacheEntryInfo->lpszSourceUrlName);
808 HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
809
810 if (pDeleteUrlCacheEntryA) {
811 ret = pDeleteUrlCacheEntryA(test_hash_collisions1);
812 ok(ret, "DeleteUrlCacheEntry failed: %d\n", GetLastError());
813 ret = pDeleteUrlCacheEntryA(test_hash_collisions2);
814 ok(ret, "DeleteUrlCacheEntry failed: %d\n", GetLastError());
815 }
816
817 len = strlen(long_url);
818 memset(long_url+len, 'a', sizeof(long_url)-len);
819 long_url[sizeof(long_url)-1] = 0;
820 ret = CreateUrlCacheEntryA(long_url, 0, NULL, filenameA, 0);
821 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
822 check_file_exists(filenameA);
823 DeleteFileA(filenameA);
824
825 ret = CreateUrlCacheEntryA(long_url, 0, "extension", filenameA, 0);
826 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
827 check_file_exists(filenameA);
828 DeleteFileA(filenameA);
829
830 long_url[250] = 0;
831 ret = CreateUrlCacheEntryA(long_url, 0, NULL, filenameA, 0);
832 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
833 check_file_exists(filenameA);
834 DeleteFileA(filenameA);
835
836 ret = CreateUrlCacheEntryA(long_url, 0, "extension", filenameA, 0);
837 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
838 check_file_exists(filenameA);
839 DeleteFileA(filenameA);
840 }
841
842 static void test_urlcacheW(void)
843 {
844 static struct test_data
845 {
846 DWORD err;
847 WCHAR url[128];
848 char encoded_url[128];
849 WCHAR extension[32];
850 WCHAR header_info[128];
851 }urls[] = {
852 {
853 0, {'h','t','t','p',':','/','/','T','.','p','l','/','t',0},
854 "http://T.pl/t", {0}, {0}
855 },
856 {
857 0, {'w','w','w','.','T','.','p','l','/','t',0},
858 "www.T.pl/t", {0}, {0}
859 },
860 {
861 0, {'h','t','t','p',':','/','/','w','w','w','.','t','e','s','t',0x15b,0x107,
862 '.','o','r','g','/','t','e','s','t','.','h','t','m','l',0},
863 "http://www.xn--test-ota71c.org/test.html", {'t','x','t',0}, {0}
864 },
865 {
866 0, {'w','w','w','.','T','e','s','t',0x15b,0x107,'.','o','r','g',
867 '/','t','e','s','t','.','h','t','m','l',0},
868 "www.Test\xc5\x9b\xc4\x87.org/test.html", {'a',0x106,'a',0}, {'b',0x106,'b',0}
869 },
870 {
871 0, {'H','t','t','p','s',':','/','/',0x15b,0x15b,0x107,'/','t',0x107,'/',
872 't','e','s','t','?','a','=','%','2','0',0x106,0},
873 "Https://xn--4da1oa/t\xc4\x87/test?a=%20\xc4\x86", {'a',0x15b,'a',0}, {'b',0x15b,'b',0}
874 },
875 {
876 12005, {'h','t','t','p','s',':','/','/','/','/',0x107,'.','o','r','g','/','t','e','s','t',0},
877 "", {0}, {0}
878 },
879 {
880 0, {'C','o','o','k','i','e',':',' ','u','s','e','r','@','h','t','t','p',
881 ':','/','/','t',0x15b,0x107,'.','o','r','g','/',0},
882 "Cookie: user@http://t\xc5\x9b\xc4\x87.org/", {0}, {0}
883 }
884 };
885 static const FILETIME filetime_zero;
886
887 WCHAR bufW[MAX_PATH];
888 DWORD i;
889 BOOL ret;
890
891 if(old_ie) {
892 win_skip("urlcache unicode functions\n");
893 return;
894 }
895
896 if(ie10_cache) {
897 if(!MultiByteToWideChar(CP_ACP, 0, urls[6].encoded_url, -1,
898 urls[6].url, ARRAY_SIZE(urls[6].url)))
899 urls[6].url[0] = 0;
900
901 trace("converted url in test 6: %s\n", wine_dbgstr_w(urls[6].url));
902 }
903
904 for(i=0; i<ARRAY_SIZE(urls); i++) {
905 INTERNET_CACHE_ENTRY_INFOA *entry_infoA;
906 INTERNET_CACHE_ENTRY_INFOW *entry_infoW;
907 DWORD size;
908
909 if(!urls[i].url[0]) {
910 win_skip("No UTF16 version of url (%d)\n", i);
911 continue;
912 }
913
914 SetLastError(0xdeadbeef);
915 ret = CreateUrlCacheEntryW(urls[i].url, 0, NULL, bufW, 0);
916 if(urls[i].err != 0) {
917 ok(!ret, "%d) CreateUrlCacheEntryW succeeded\n", i);
918 ok(urls[i].err == GetLastError(), "%d) GetLastError() = %d\n", i, GetLastError());
919 continue;
920 }
921 ok(ret, "%d) CreateUrlCacheEntryW failed: %d\n", i, GetLastError());
922
923 /* dwHeaderSize is ignored, pass 0 to prove it */
924 ret = CommitUrlCacheEntryW(urls[i].url, bufW, filetime_zero, filetime_zero,
925 NORMAL_CACHE_ENTRY, urls[i].header_info, 0, urls[i].extension, NULL);
926 ok(ret, "%d) CommitUrlCacheEntryW failed: %d\n", i, GetLastError());
927
928 SetLastError(0xdeadbeef);
929 size = 0;
930 ret = GetUrlCacheEntryInfoW(urls[i].url, NULL, &size);
931 ok(!ret && GetLastError()==ERROR_INSUFFICIENT_BUFFER,
932 "%d) GetLastError() = %d\n", i, GetLastError());
933 entry_infoW = HeapAlloc(GetProcessHeap(), 0, size);
934 ret = GetUrlCacheEntryInfoW(urls[i].url, entry_infoW, &size);
935 ok(ret, "%d) GetUrlCacheEntryInfoW failed: %d\n", i, GetLastError());
936
937 ret = GetUrlCacheEntryInfoA(urls[i].encoded_url, NULL, &size);
938 ok(!ret && GetLastError()==ERROR_INSUFFICIENT_BUFFER,
939 "%d) GetLastError() = %d\n", i, GetLastError());
940 if(!ret && GetLastError()!=ERROR_INSUFFICIENT_BUFFER) {
941 win_skip("ANSI version of url is incorrect\n");
942 continue;
943 }
944 entry_infoA = HeapAlloc(GetProcessHeap(), 0, size);
945 ret = GetUrlCacheEntryInfoA(urls[i].encoded_url, entry_infoA, &size);
946 ok(ret, "%d) GetUrlCacheEntryInfoA failed: %d\n", i, GetLastError());
947
948 ok(entry_infoW->dwStructSize == entry_infoA->dwStructSize,
949 "%d) entry_infoW->dwStructSize = %d, expected %d\n",
950 i, entry_infoW->dwStructSize, entry_infoA->dwStructSize);
951 ok(!lstrcmpW(urls[i].url, entry_infoW->lpszSourceUrlName),
952 "%d) entry_infoW->lpszSourceUrlName = %s\n",
953 i, wine_dbgstr_w(entry_infoW->lpszSourceUrlName));
954 ok(!lstrcmpA(urls[i].encoded_url, entry_infoA->lpszSourceUrlName),
955 "%d) entry_infoA->lpszSourceUrlName = %s\n",
956 i, entry_infoA->lpszSourceUrlName);
957 ok(entry_infoW->CacheEntryType == entry_infoA->CacheEntryType,
958 "%d) entry_infoW->CacheEntryType = %x, expected %x\n",
959 i, entry_infoW->CacheEntryType, entry_infoA->CacheEntryType);
960 ok(entry_infoW->dwUseCount == entry_infoA->dwUseCount,
961 "%d) entry_infoW->dwUseCount = %d, expected %d\n",
962 i, entry_infoW->dwUseCount, entry_infoA->dwUseCount);
963 ok(entry_infoW->dwHitRate == entry_infoA->dwHitRate,
964 "%d) entry_infoW->dwHitRate = %d, expected %d\n",
965 i, entry_infoW->dwHitRate, entry_infoA->dwHitRate);
966 ok(entry_infoW->dwSizeLow == entry_infoA->dwSizeLow,
967 "%d) entry_infoW->dwSizeLow = %d, expected %d\n",
968 i, entry_infoW->dwSizeLow, entry_infoA->dwSizeLow);
969 ok(entry_infoW->dwSizeHigh == entry_infoA->dwSizeHigh,
970 "%d) entry_infoW->dwSizeHigh = %d, expected %d\n",
971 i, entry_infoW->dwSizeHigh, entry_infoA->dwSizeHigh);
972 ok(!memcmp(&entry_infoW->LastModifiedTime, &entry_infoA->LastModifiedTime, sizeof(FILETIME)),
973 "%d) entry_infoW->LastModifiedTime is incorrect\n", i);
974 ok(!memcmp(&entry_infoW->ExpireTime, &entry_infoA->ExpireTime, sizeof(FILETIME)),
975 "%d) entry_infoW->ExpireTime is incorrect\n", i);
976 ok(!memcmp(&entry_infoW->LastAccessTime, &entry_infoA->LastAccessTime, sizeof(FILETIME)),
977 "%d) entry_infoW->LastAccessTime is incorrect\n", i);
978 ok(!memcmp(&entry_infoW->LastSyncTime, &entry_infoA->LastSyncTime, sizeof(FILETIME)),
979 "%d) entry_infoW->LastSyncTime is incorrect\n", i);
980
981 MultiByteToWideChar(CP_ACP, 0, entry_infoA->lpszLocalFileName, -1, bufW, MAX_PATH);
982 ok(!lstrcmpW(entry_infoW->lpszLocalFileName, bufW),
983 "%d) entry_infoW->lpszLocalFileName = %s, expected %s\n",
984 i, wine_dbgstr_w(entry_infoW->lpszLocalFileName), wine_dbgstr_w(bufW));
985
986 if(!urls[i].header_info[0]) {
987 ok(!entry_infoW->lpHeaderInfo, "entry_infoW->lpHeaderInfo != NULL\n");
988 }else {
989 ok(!lstrcmpW((WCHAR*)entry_infoW->lpHeaderInfo, urls[i].header_info),
990 "%d) entry_infoW->lpHeaderInfo = %s\n",
991 i, wine_dbgstr_w((WCHAR*)entry_infoW->lpHeaderInfo));
992 }
993
994 if(!urls[i].extension[0]) {
995 ok(!entry_infoW->lpszFileExtension || (ie10_cache && !entry_infoW->lpszFileExtension[0]),
996 "%d) entry_infoW->lpszFileExtension = %s\n",
997 i, wine_dbgstr_w(entry_infoW->lpszFileExtension));
998 }else {
999 MultiByteToWideChar(CP_ACP, 0, entry_infoA->lpszFileExtension, -1, bufW, MAX_PATH);
1000 ok(!lstrcmpW(entry_infoW->lpszFileExtension, bufW) ||
1001 (ie10_cache && !lstrcmpW(entry_infoW->lpszFileExtension, urls[i].extension)),
1002 "%d) entry_infoW->lpszFileExtension = %s, expected %s\n",
1003 i, wine_dbgstr_w(entry_infoW->lpszFileExtension), wine_dbgstr_w(bufW));
1004 }
1005
1006 HeapFree(GetProcessHeap(), 0, entry_infoW);
1007 HeapFree(GetProcessHeap(), 0, entry_infoA);
1008
1009 if(pDeleteUrlCacheEntryA) {
1010 ret = pDeleteUrlCacheEntryA(urls[i].encoded_url);
1011 ok(ret, "%d) DeleteUrlCacheEntryW failed: %d\n", i, GetLastError());
1012 }
1013 }
1014 }
1015
1016 static void test_FindCloseUrlCache(void)
1017 {
1018 BOOL r;
1019 DWORD err;
1020
1021 SetLastError(0xdeadbeef);
1022 r = FindCloseUrlCache(NULL);
1023 err = GetLastError();
1024 ok(0 == r, "expected 0, got %d\n", r);
1025 ok(ERROR_INVALID_HANDLE == err, "expected %d, got %d\n", ERROR_INVALID_HANDLE, err);
1026 }
1027
1028 static void test_GetDiskInfoA(void)
1029 {
1030 BOOL ret;
1031 DWORD error, cluster_size;
1032 DWORDLONG free, total;
1033 char path[MAX_PATH], *p;
1034
1035 GetSystemDirectoryA(path, MAX_PATH);
1036 if ((p = strchr(path, '\\'))) *++p = 0;
1037
1038 ret = GetDiskInfoA(path, &cluster_size, &free, &total);
1039 ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
1040
1041 ret = GetDiskInfoA(path, &cluster_size, &free, NULL);
1042 ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
1043
1044 ret = GetDiskInfoA(path, &cluster_size, NULL, NULL);
1045 ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
1046
1047 ret = GetDiskInfoA(path, NULL, NULL, NULL);
1048 ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
1049
1050 SetLastError(0xdeadbeef);
1051 strcpy(p, "\\non\\existing\\path");
1052 ret = GetDiskInfoA(path, NULL, NULL, NULL);
1053 error = GetLastError();
1054 ok(!ret ||
1055 broken(old_ie && ret), /* < IE7 */
1056 "GetDiskInfoA succeeded\n");
1057 ok(error == ERROR_PATH_NOT_FOUND ||
1058 broken(old_ie && error == 0xdeadbeef), /* < IE7 */
1059 "got %u expected ERROR_PATH_NOT_FOUND\n", error);
1060
1061 SetLastError(0xdeadbeef);
1062 ret = GetDiskInfoA(NULL, NULL, NULL, NULL);
1063 error = GetLastError();
1064 ok(!ret, "GetDiskInfoA succeeded\n");
1065 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
1066 }
1067
1068 static BOOL cache_entry_exists(const char *url)
1069 {
1070 static char buf[10000];
1071 DWORD size = sizeof(buf);
1072 BOOL ret;
1073
1074 ret = GetUrlCacheEntryInfoA(url, (void*)buf, &size);
1075 ok(ret || GetLastError() == ERROR_FILE_NOT_FOUND, "GetUrlCacheEntryInfoA returned %x (%u)\n", ret, GetLastError());
1076
1077 return ret;
1078 }
1079
1080 static void test_trailing_slash(void)
1081 {
1082 char filename[MAX_PATH];
1083 BYTE zero_byte = 0;
1084 BOOL ret;
1085
1086 static const FILETIME filetime_zero;
1087 static char url_with_slash[] = "http://testing.cache.com/";
1088
1089
1090 ret = CreateUrlCacheEntryA(url_with_slash, 0, "html", filename, 0);
1091 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
1092
1093 create_and_write_file(filename, &zero_byte, sizeof(zero_byte));
1094
1095 ret = CommitUrlCacheEntryA("Visited: http://testing.cache.com/", NULL, filetime_zero, filetime_zero,
1096 NORMAL_CACHE_ENTRY, NULL, 0, "html", NULL);
1097 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
1098
1099 ok(cache_entry_exists("Visited: http://testing.cache.com/"), "cache entry does not exist\n");
1100 ok(!cache_entry_exists("Visited: http://testing.cache.com"), "cache entry exists\n");
1101
1102 ret = DeleteUrlCacheEntryA("Visited: http://testing.cache.com/");
1103 ok(ret, "DeleteCacheEntryA failed\n");
1104 DeleteFileA(filename);
1105 }
1106
1107 static void get_cache_path(DWORD flags, char path[MAX_PATH], char path_win8[MAX_PATH])
1108 {
1109 BOOL ret;
1110 int folder = -1;
1111 const char *suffix = "";
1112 const char *suffix_win8 = "";
1113
1114 switch (flags)
1115 {
1116 case 0:
1117 case CACHE_CONFIG_CONTENT_PATHS_FC:
1118 folder = CSIDL_INTERNET_CACHE;
1119 suffix = "\\Content.IE5\\";
1120 suffix_win8 = "\\IE\\";
1121 break;
1122
1123 case CACHE_CONFIG_COOKIES_PATHS_FC:
1124 folder = CSIDL_COOKIES;
1125 suffix = "\\";
1126 suffix_win8 = "\\";
1127 break;
1128
1129 case CACHE_CONFIG_HISTORY_PATHS_FC:
1130 folder = CSIDL_HISTORY;
1131 suffix = "\\History.IE5\\";
1132 suffix_win8 = "\\History.IE5\\";
1133 break;
1134
1135 default:
1136 ok(0, "unexpected flags %#x\n", flags);
1137 break;
1138 }
1139
1140 ret = SHGetSpecialFolderPathA(0, path, folder, FALSE);
1141 ok(ret, "SHGetSpecialFolderPath error %u\n", GetLastError());
1142
1143 strcpy(path_win8, path);
1144 strcat(path_win8, suffix_win8);
1145
1146 strcat(path, suffix);
1147 }
1148
1149 static void test_GetUrlCacheConfigInfo(void)
1150 {
1151 INTERNET_CACHE_CONFIG_INFOA info;
1152 struct
1153 {
1154 INTERNET_CACHE_CONFIG_INFOA *info;
1155 DWORD dwStructSize;
1156 DWORD flags;
1157 BOOL ret;
1158 DWORD error;
1159 } td[] =
1160 {
1161 #if 0 /* crashes under Vista */
1162 { NULL, 0, 0, FALSE, ERROR_INVALID_PARAMETER },
1163 #endif
1164 { &info, 0, 0, TRUE },
1165 { &info, sizeof(info) - 1, 0, TRUE },
1166 { &info, sizeof(info) + 1, 0, TRUE },
1167 { &info, 0, CACHE_CONFIG_CONTENT_PATHS_FC, TRUE },
1168 { &info, sizeof(info), CACHE_CONFIG_CONTENT_PATHS_FC, TRUE },
1169 { &info, 0, CACHE_CONFIG_COOKIES_PATHS_FC, TRUE },
1170 { &info, sizeof(info), CACHE_CONFIG_COOKIES_PATHS_FC, TRUE },
1171 { &info, 0, CACHE_CONFIG_HISTORY_PATHS_FC, TRUE },
1172 { &info, sizeof(info), CACHE_CONFIG_HISTORY_PATHS_FC, TRUE },
1173 };
1174 int i;
1175 BOOL ret;
1176
1177 for (i = 0; i < ARRAY_SIZE(td); i++)
1178 {
1179 if (td[i].info)
1180 {
1181 memset(&info, 0, sizeof(*td[i].info));
1182 info.dwStructSize = td[i].dwStructSize;
1183 }
1184
1185 SetLastError(0xdeadbeef);
1186 ret = GetUrlCacheConfigInfoA(td[i].info, NULL, td[i].flags);
1187 ok(ret == td[i].ret, "%d: expected %d, got %d\n", i, td[i].ret, ret);
1188 if (!ret)
1189 ok(GetLastError() == td[i].error, "%d: expected %u, got %u\n", i, td[i].error, GetLastError());
1190 else
1191 {
1192 char path[MAX_PATH], path_win8[MAX_PATH];
1193
1194 get_cache_path(td[i].flags, path, path_win8);
1195
1196 ok(info.dwStructSize == td[i].dwStructSize, "got %u\n", info.dwStructSize);
1197 ok(!lstrcmpA(info.CachePath, path) || !lstrcmpA(info.CachePath, path_win8),
1198 "%d: expected %s or %s, got %s\n", i, path, path_win8, info.CachePath);
1199 }
1200 }
1201 }
1202
1203 START_TEST(urlcache)
1204 {
1205 HMODULE hdll;
1206 hdll = GetModuleHandleA("wininet.dll");
1207
1208 if(!GetProcAddress(hdll, "InternetGetCookieExW")) {
1209 win_skip("Too old IE (older than 6.0)\n");
1210 return;
1211 }
1212 if(!GetProcAddress(hdll, "InternetGetSecurityInfoByURL")) /* < IE7 */
1213 old_ie = TRUE;
1214
1215 if(GetProcAddress(hdll, "CreateUrlCacheEntryExW")) {
1216 trace("Running tests on IE10 or newer\n");
1217 ie10_cache = TRUE;
1218 }
1219
1220 pDeleteUrlCacheEntryA = (void*)GetProcAddress(hdll, "DeleteUrlCacheEntryA");
1221 pUnlockUrlCacheEntryFileA = (void*)GetProcAddress(hdll, "UnlockUrlCacheEntryFileA");
1222 test_urlcacheA();
1223 test_urlcacheW();
1224 test_FindCloseUrlCache();
1225 test_GetDiskInfoA();
1226 test_trailing_slash();
1227 test_GetUrlCacheConfigInfo();
1228 }