push 52cba0a224aad01bcbdb26d79e43229ba950650e
[wine/hacks.git] / dlls / wininet / tests / urlcache.c
blobb7df03e3120e53a11212a2c86354c8de6ac179b3
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"
34 static BOOL (WINAPI *pDeleteUrlCacheEntryA)(LPCSTR);
35 static BOOL (WINAPI *pUnlockUrlCacheEntryFileA)(LPCSTR,DWORD);
37 static char filenameA[MAX_PATH + 1];
39 static void check_cache_entry_infoA(const char *returnedfrom, LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo)
41 ok(lpCacheEntryInfo->dwStructSize == sizeof(*lpCacheEntryInfo), "%s: dwStructSize was %d\n", returnedfrom, lpCacheEntryInfo->dwStructSize);
42 ok(!strcmp(lpCacheEntryInfo->lpszSourceUrlName, TEST_URL), "%s: lpszSourceUrlName should be %s instead of %s\n", returnedfrom, TEST_URL, lpCacheEntryInfo->lpszSourceUrlName);
43 ok(!strcmp(lpCacheEntryInfo->lpszLocalFileName, filenameA), "%s: lpszLocalFileName should be %s instead of %s\n", returnedfrom, filenameA, lpCacheEntryInfo->lpszLocalFileName);
44 ok(!strcmp(lpCacheEntryInfo->lpszFileExtension, "html"), "%s: lpszFileExtension should be html instead of %s\n", returnedfrom, lpCacheEntryInfo->lpszFileExtension);
47 static void test_find_url_cache_entriesA(void)
49 BOOL ret;
50 HANDLE hEnumHandle;
51 BOOL found = FALSE;
52 DWORD cbCacheEntryInfo;
53 DWORD cbCacheEntryInfoSaved;
54 LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo;
56 cbCacheEntryInfo = 0;
57 hEnumHandle = FindFirstUrlCacheEntry(NULL, NULL, &cbCacheEntryInfo);
58 ok(!hEnumHandle, "FindFirstUrlCacheEntry should have failed\n");
59 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "FindFirstUrlCacheEntry should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
60 lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo * sizeof(char));
61 cbCacheEntryInfoSaved = cbCacheEntryInfo;
62 hEnumHandle = FindFirstUrlCacheEntry(NULL, lpCacheEntryInfo, &cbCacheEntryInfo);
63 ok(hEnumHandle != NULL, "FindFirstUrlCacheEntry failed with error %d\n", GetLastError());
64 while (TRUE)
66 if (!strcmp(lpCacheEntryInfo->lpszSourceUrlName, TEST_URL))
68 found = TRUE;
69 break;
71 cbCacheEntryInfo = cbCacheEntryInfoSaved;
72 ret = FindNextUrlCacheEntry(hEnumHandle, lpCacheEntryInfo, &cbCacheEntryInfo);
73 if (!ret)
75 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
77 lpCacheEntryInfo = HeapReAlloc(GetProcessHeap(), 0, lpCacheEntryInfo, cbCacheEntryInfo);
78 cbCacheEntryInfoSaved = cbCacheEntryInfo;
79 ret = FindNextUrlCacheEntry(hEnumHandle, lpCacheEntryInfo, &cbCacheEntryInfo);
82 ok(ret, "FindNextUrlCacheEntry failed with error %d\n", GetLastError());
83 if (!ret)
84 break;
86 ok(found, "committed url cache entry not found during enumeration\n");
88 ret = FindCloseUrlCache(hEnumHandle);
89 ok(ret, "FindCloseUrlCache failed with error %d\n", GetLastError());
92 static void test_GetUrlCacheEntryInfoExA(void)
94 BOOL ret;
95 DWORD cbCacheEntryInfo;
96 LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo;
98 ret = GetUrlCacheEntryInfoEx(NULL, NULL, NULL, NULL, NULL, NULL, 0);
99 ok(!ret, "GetUrlCacheEntryInfoEx with NULL URL and NULL args should have failed\n");
100 ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetUrlCacheEntryInfoEx with NULL URL and NULL args should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
102 ret = GetUrlCacheEntryInfoEx(TEST_URL, NULL, NULL, NULL, NULL, NULL, 0);
103 ok(ret, "GetUrlCacheEntryInfoEx with NULL args failed with error %d\n", GetLastError());
105 cbCacheEntryInfo = 0;
106 ret = GetUrlCacheEntryInfoEx(TEST_URL, NULL, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
107 ok(!ret, "GetUrlCacheEntryInfoEx with zero-length buffer should fail\n");
108 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetUrlCacheEntryInfoEx should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
110 lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
111 ret = GetUrlCacheEntryInfoEx(TEST_URL, lpCacheEntryInfo, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
112 ok(ret, "GetUrlCacheEntryInfoEx failed with error %d\n", GetLastError());
114 check_cache_entry_infoA("GetUrlCacheEntryInfoEx", lpCacheEntryInfo);
116 cbCacheEntryInfo = 100000;
117 SetLastError(0xdeadbeef);
118 ret = GetUrlCacheEntryInfoEx(TEST_URL, NULL, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
119 ok(!ret, "GetUrlCacheEntryInfoEx with zero-length buffer should fail\n");
120 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetUrlCacheEntryInfoEx should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
122 HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
125 static void test_RetrieveUrlCacheEntryA(void)
127 BOOL ret;
128 DWORD cbCacheEntryInfo;
130 cbCacheEntryInfo = 0;
131 SetLastError(0xdeadbeef);
132 ret = RetrieveUrlCacheEntryFile(NULL, NULL, &cbCacheEntryInfo, 0);
133 ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
134 ok(GetLastError() == ERROR_INVALID_PARAMETER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
136 if (0)
138 /* Crashes on Win9x, NT4 and W2K */
139 SetLastError(0xdeadbeef);
140 ret = RetrieveUrlCacheEntryFile(TEST_URL, NULL, NULL, 0);
141 ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
142 ok(GetLastError() == ERROR_INVALID_PARAMETER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
145 SetLastError(0xdeadbeef);
146 cbCacheEntryInfo = 100000;
147 ret = RetrieveUrlCacheEntryFile(NULL, NULL, &cbCacheEntryInfo, 0);
148 ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
149 ok(GetLastError() == ERROR_INVALID_PARAMETER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
152 static void test_urlcacheA(void)
154 BOOL ret;
155 HANDLE hFile;
156 DWORD written;
157 BYTE zero_byte = 0;
158 LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo;
159 DWORD cbCacheEntryInfo;
160 static const FILETIME filetime_zero;
162 ret = CreateUrlCacheEntry(TEST_URL, 0, "html", filenameA, 0);
163 ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
165 hFile = CreateFileA(filenameA, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
166 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
167 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA failed with error %d\n", GetLastError());
169 ret = WriteFile(hFile, &zero_byte, sizeof(zero_byte), &written, NULL);
170 ok(ret, "WriteFile failed with error %d\n", GetLastError());
172 CloseHandle(hFile);
174 ret = CommitUrlCacheEntry(TEST_URL, filenameA, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY, NULL, 0, "html", NULL);
175 ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
177 cbCacheEntryInfo = 0;
178 ret = RetrieveUrlCacheEntryFile(TEST_URL, NULL, &cbCacheEntryInfo, 0);
179 ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
180 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
182 lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
183 ret = RetrieveUrlCacheEntryFile(TEST_URL, lpCacheEntryInfo, &cbCacheEntryInfo, 0);
184 ok(ret, "RetrieveUrlCacheEntryFile failed with error %d\n", GetLastError());
186 check_cache_entry_infoA("RetrieveUrlCacheEntryFile", lpCacheEntryInfo);
188 HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
190 if (pUnlockUrlCacheEntryFileA)
192 ret = pUnlockUrlCacheEntryFileA(TEST_URL, 0);
193 ok(ret, "UnlockUrlCacheEntryFileA failed with error %d\n", GetLastError());
196 /* test Find*UrlCacheEntry functions */
197 test_find_url_cache_entriesA();
199 test_GetUrlCacheEntryInfoExA();
200 test_RetrieveUrlCacheEntryA();
202 if (pDeleteUrlCacheEntryA)
204 ret = pDeleteUrlCacheEntryA(TEST_URL);
205 ok(ret, "DeleteUrlCacheEntryA failed with error %d\n", GetLastError());
208 ret = DeleteFile(filenameA);
209 todo_wine
210 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "local file should no longer exist\n");
213 static void test_FindCloseUrlCache(void)
215 BOOL r;
216 DWORD err;
217 r = FindCloseUrlCache(NULL);
218 err = GetLastError();
219 ok(0 == r, "expected 0, got %d\n", r);
220 ok(ERROR_INVALID_HANDLE == err, "expected %d, got %d\n", ERROR_INVALID_HANDLE, err);
223 static void test_GetDiskInfoA(void)
225 BOOL ret;
226 DWORD error, cluster_size;
227 DWORDLONG free, total;
228 char path[MAX_PATH], *p;
230 GetSystemDirectoryA(path, MAX_PATH);
231 if ((p = strchr(path, '\\'))) *++p = 0;
233 ret = GetDiskInfoA(path, &cluster_size, &free, &total);
234 ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
236 ret = GetDiskInfoA(path, &cluster_size, &free, NULL);
237 ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
239 ret = GetDiskInfoA(path, &cluster_size, NULL, NULL);
240 ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
242 ret = GetDiskInfoA(path, NULL, NULL, NULL);
243 ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
245 ret = GetDiskInfoA(path, NULL, NULL, NULL);
246 ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
248 SetLastError(0xdeadbeef);
249 strcpy(p, "\\non\\existing\\path");
250 ret = GetDiskInfoA(path, NULL, NULL, NULL);
251 error = GetLastError();
252 ok(!ret ||
253 broken(ret), /* < IE7 */
254 "GetDiskInfoA succeeded\n");
255 ok(error == ERROR_PATH_NOT_FOUND ||
256 broken(error == 0xdeadbeef), /* < IE7 */
257 "got %u expected ERROR_PATH_NOT_FOUND\n", error);
259 SetLastError(0xdeadbeef);
260 ret = GetDiskInfoA(NULL, NULL, NULL, NULL);
261 error = GetLastError();
262 ok(!ret, "GetDiskInfoA succeeded\n");
263 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
266 START_TEST(urlcache)
268 HMODULE hdll;
269 hdll = GetModuleHandleA("wininet.dll");
270 pDeleteUrlCacheEntryA = (void*)GetProcAddress(hdll, "DeleteUrlCacheEntryA");
271 pUnlockUrlCacheEntryFileA = (void*)GetProcAddress(hdll, "UnlockUrlCacheEntryFileA");
272 test_urlcacheA();
273 test_FindCloseUrlCache();
274 test_GetDiskInfoA();