wininet: Support the Cache-Control max-age directive for setting url cache entry...
[wine/testsucceed.git] / dlls / wininet / tests / urlcache.c
blob630b5cc68871f14b993f1510f124eb3caf46fdbf
1 /*
2 * URL Cache Tests
4 * Copyright 2008 Robert Shearman for CodeWeavers
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 <stdio.h>
23 #include <stdlib.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wininet.h"
28 #include "winineti.h"
30 #include "wine/test.h"
32 #define TEST_URL "http://urlcachetest.winehq.org/index.html"
33 #define TEST_URL1 "Visited: user@http://urlcachetest.winehq.org/index.html"
35 static BOOL (WINAPI *pDeleteUrlCacheEntryA)(LPCSTR);
36 static BOOL (WINAPI *pUnlockUrlCacheEntryFileA)(LPCSTR,DWORD);
38 static char filenameA[MAX_PATH + 1];
39 static char filenameA1[MAX_PATH + 1];
41 static void check_cache_entry_infoA(const char *returnedfrom, LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo)
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);
49 static void test_find_url_cache_entriesA(void)
51 BOOL ret;
52 HANDLE hEnumHandle;
53 BOOL found = FALSE;
54 DWORD cbCacheEntryInfo;
55 DWORD cbCacheEntryInfoSaved;
56 LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo;
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)
69 if (!strcmp(lpCacheEntryInfo->lpszSourceUrlName, TEST_URL))
71 found = TRUE;
72 break;
74 SetLastError(0xdeadbeef);
75 cbCacheEntryInfo = cbCacheEntryInfoSaved;
76 ret = FindNextUrlCacheEntry(hEnumHandle, lpCacheEntryInfo, &cbCacheEntryInfo);
77 if (!ret)
79 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
81 lpCacheEntryInfo = HeapReAlloc(GetProcessHeap(), 0, lpCacheEntryInfo, cbCacheEntryInfo);
82 cbCacheEntryInfoSaved = cbCacheEntryInfo;
83 ret = FindNextUrlCacheEntry(hEnumHandle, lpCacheEntryInfo, &cbCacheEntryInfo);
86 ok(ret, "FindNextUrlCacheEntry failed with error %d\n", GetLastError());
87 if (!ret)
88 break;
90 ok(found, "committed url cache entry not found during enumeration\n");
92 ret = FindCloseUrlCache(hEnumHandle);
93 ok(ret, "FindCloseUrlCache failed with error %d\n", GetLastError());
96 static void test_GetUrlCacheEntryInfoExA(void)
98 BOOL ret;
99 DWORD cbCacheEntryInfo;
100 LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo;
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());
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());
115 ret = GetUrlCacheEntryInfoEx(TEST_URL, NULL, NULL, NULL, NULL, NULL, 0);
116 ok(ret, "GetUrlCacheEntryInfoEx with NULL args failed with error %d\n", GetLastError());
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());
125 lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
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());
133 ret = GetUrlCacheEntryInfoEx(TEST_URL, lpCacheEntryInfo, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
134 ok(ret, "GetUrlCacheEntryInfoEx failed with error %d\n", GetLastError());
136 check_cache_entry_infoA("GetUrlCacheEntryInfoEx", lpCacheEntryInfo);
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());
144 HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
147 static void test_RetrieveUrlCacheEntryA(void)
149 BOOL ret;
150 DWORD cbCacheEntryInfo;
152 cbCacheEntryInfo = 0;
153 SetLastError(0xdeadbeef);
154 ret = RetrieveUrlCacheEntryFile(NULL, NULL, &cbCacheEntryInfo, 0);
155 ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
156 ok(GetLastError() == ERROR_INVALID_PARAMETER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
158 if (0)
160 /* Crashes on Win9x, NT4 and W2K */
161 SetLastError(0xdeadbeef);
162 ret = RetrieveUrlCacheEntryFile(TEST_URL, NULL, NULL, 0);
163 ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
164 ok(GetLastError() == ERROR_INVALID_PARAMETER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
167 SetLastError(0xdeadbeef);
168 cbCacheEntryInfo = 100000;
169 ret = RetrieveUrlCacheEntryFile(NULL, NULL, &cbCacheEntryInfo, 0);
170 ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
171 ok(GetLastError() == ERROR_INVALID_PARAMETER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
174 static void test_urlcacheA(void)
176 BOOL ret;
177 HANDLE hFile;
178 DWORD written;
179 BYTE zero_byte = 0;
180 LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo;
181 DWORD cbCacheEntryInfo;
182 static const FILETIME filetime_zero;
184 ret = CreateUrlCacheEntry(TEST_URL, 0, "html", filenameA, 0);
185 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
187 ret = CreateUrlCacheEntry(TEST_URL, 0, "html", filenameA1, 0);
188 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
190 ok(lstrcmpiA(filenameA, filenameA1), "expected a different file name\n");
192 hFile = CreateFileA(filenameA, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
193 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
194 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA failed with error %d\n", GetLastError());
196 ret = WriteFile(hFile, &zero_byte, sizeof(zero_byte), &written, NULL);
197 ok(ret, "WriteFile failed with error %d\n", GetLastError());
199 CloseHandle(hFile);
201 ret = CommitUrlCacheEntry(TEST_URL1, NULL, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY|URLHISTORY_CACHE_ENTRY, NULL, 0, NULL, NULL);
202 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
204 ret = CommitUrlCacheEntry(TEST_URL1, NULL, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY|URLHISTORY_CACHE_ENTRY, NULL, 0, NULL, NULL);
205 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
207 ret = CommitUrlCacheEntry(TEST_URL, filenameA, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY, NULL, 0, "html", NULL);
208 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
210 cbCacheEntryInfo = 0;
211 SetLastError(0xdeadbeef);
212 ret = RetrieveUrlCacheEntryFile(TEST_URL, NULL, &cbCacheEntryInfo, 0);
213 ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
214 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
215 "RetrieveUrlCacheEntryFile should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
217 lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
218 ret = RetrieveUrlCacheEntryFile(TEST_URL, lpCacheEntryInfo, &cbCacheEntryInfo, 0);
219 ok(ret, "RetrieveUrlCacheEntryFile failed with error %d\n", GetLastError());
221 check_cache_entry_infoA("RetrieveUrlCacheEntryFile", lpCacheEntryInfo);
223 HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
225 cbCacheEntryInfo = 0;
226 SetLastError(0xdeadbeef);
227 ret = RetrieveUrlCacheEntryFile(TEST_URL1, NULL, &cbCacheEntryInfo, 0);
228 ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
229 ok(GetLastError() == ERROR_INVALID_DATA,
230 "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_DATA instead of %d\n", GetLastError());
232 if (pUnlockUrlCacheEntryFileA)
234 ret = pUnlockUrlCacheEntryFileA(TEST_URL, 0);
235 ok(ret, "UnlockUrlCacheEntryFileA failed with error %d\n", GetLastError());
238 /* test Find*UrlCacheEntry functions */
239 test_find_url_cache_entriesA();
241 test_GetUrlCacheEntryInfoExA();
242 test_RetrieveUrlCacheEntryA();
244 if (pDeleteUrlCacheEntryA)
246 ret = pDeleteUrlCacheEntryA(TEST_URL);
247 ok(ret, "DeleteUrlCacheEntryA failed with error %d\n", GetLastError());
248 ret = pDeleteUrlCacheEntryA(TEST_URL1);
249 ok(ret, "DeleteUrlCacheEntryA failed with error %d\n", GetLastError());
252 SetLastError(0xdeadbeef);
253 ret = DeleteFile(filenameA);
254 todo_wine
255 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "local file should no longer exist\n");
258 static void test_FindCloseUrlCache(void)
260 BOOL r;
261 DWORD err;
263 SetLastError(0xdeadbeef);
264 r = FindCloseUrlCache(NULL);
265 err = GetLastError();
266 ok(0 == r, "expected 0, got %d\n", r);
267 ok(ERROR_INVALID_HANDLE == err, "expected %d, got %d\n", ERROR_INVALID_HANDLE, err);
270 static void test_GetDiskInfoA(void)
272 BOOL ret;
273 DWORD error, cluster_size;
274 DWORDLONG free, total;
275 char path[MAX_PATH], *p;
277 GetSystemDirectoryA(path, MAX_PATH);
278 if ((p = strchr(path, '\\'))) *++p = 0;
280 ret = GetDiskInfoA(path, &cluster_size, &free, &total);
281 ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
283 ret = GetDiskInfoA(path, &cluster_size, &free, NULL);
284 ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
286 ret = GetDiskInfoA(path, &cluster_size, NULL, NULL);
287 ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
289 ret = GetDiskInfoA(path, NULL, NULL, NULL);
290 ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
292 SetLastError(0xdeadbeef);
293 strcpy(p, "\\non\\existing\\path");
294 ret = GetDiskInfoA(path, NULL, NULL, NULL);
295 error = GetLastError();
296 ok(!ret ||
297 broken(ret), /* < IE7 */
298 "GetDiskInfoA succeeded\n");
299 ok(error == ERROR_PATH_NOT_FOUND ||
300 broken(error == 0xdeadbeef), /* < IE7 */
301 "got %u expected ERROR_PATH_NOT_FOUND\n", error);
303 SetLastError(0xdeadbeef);
304 ret = GetDiskInfoA(NULL, NULL, NULL, NULL);
305 error = GetLastError();
306 ok(!ret, "GetDiskInfoA succeeded\n");
307 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
310 START_TEST(urlcache)
312 HMODULE hdll;
313 hdll = GetModuleHandleA("wininet.dll");
315 if(!GetProcAddress(hdll, "InternetGetCookieExW")) {
316 win_skip("Too old IE (older than 6.0)\n");
317 return;
320 pDeleteUrlCacheEntryA = (void*)GetProcAddress(hdll, "DeleteUrlCacheEntryA");
321 pUnlockUrlCacheEntryFileA = (void*)GetProcAddress(hdll, "UnlockUrlCacheEntryFileA");
322 test_urlcacheA();
323 test_FindCloseUrlCache();
324 test_GetDiskInfoA();