wininet: Add some URL cache tests and make them pass.
[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;
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
147 static void test_RetrieveUrlCacheEntryA(void)
148 {
149     BOOL ret;
150     DWORD cbCacheEntryInfo;
151
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());
157
158     if (0)
159     {
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());
165     }
166
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());
172 }
173
174 static void test_urlcacheA(void)
175 {
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;
183
184     ret = CreateUrlCacheEntry(TEST_URL, 0, "html", filenameA, 0);
185     ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
186
187     ret = CreateUrlCacheEntry(TEST_URL, 0, "html", filenameA1, 0);
188     ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
189
190     ok(lstrcmpiA(filenameA, filenameA1), "expected a different file name\n");
191
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());
195
196     ret = WriteFile(hFile, &zero_byte, sizeof(zero_byte), &written, NULL);
197     ok(ret, "WriteFile failed with error %d\n", GetLastError());
198
199     CloseHandle(hFile);
200
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());
203
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());
206
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());
209
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());
216
217     lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
218     ret = RetrieveUrlCacheEntryFile(TEST_URL, lpCacheEntryInfo, &cbCacheEntryInfo, 0);
219     ok(ret, "RetrieveUrlCacheEntryFile failed with error %d\n", GetLastError());
220
221     check_cache_entry_infoA("RetrieveUrlCacheEntryFile", lpCacheEntryInfo);
222
223     HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
224
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());
231
232     if (pUnlockUrlCacheEntryFileA)
233     {
234         ret = pUnlockUrlCacheEntryFileA(TEST_URL, 0);
235         ok(ret, "UnlockUrlCacheEntryFileA failed with error %d\n", GetLastError());
236     }
237
238     /* test Find*UrlCacheEntry functions */
239     test_find_url_cache_entriesA();
240
241     test_GetUrlCacheEntryInfoExA();
242     test_RetrieveUrlCacheEntryA();
243
244     if (pDeleteUrlCacheEntryA)
245     {
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());
250     }
251
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");
256 }
257
258 static void test_FindCloseUrlCache(void)
259 {
260     BOOL r;
261     DWORD err;
262
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);
268 }
269
270 static void test_GetDiskInfoA(void)
271 {
272     BOOL ret;
273     DWORD error, cluster_size;
274     DWORDLONG free, total;
275     char path[MAX_PATH], *p;
276
277     GetSystemDirectoryA(path, MAX_PATH);
278     if ((p = strchr(path, '\\'))) *++p = 0;
279
280     ret = GetDiskInfoA(path, &cluster_size, &free, &total);
281     ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
282
283     ret = GetDiskInfoA(path, &cluster_size, &free, NULL);
284     ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
285
286     ret = GetDiskInfoA(path, &cluster_size, NULL, NULL);
287     ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
288
289     ret = GetDiskInfoA(path, NULL, NULL, NULL);
290     ok(ret, "GetDiskInfoA failed %u\n", GetLastError());
291
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);
302
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);
308 }
309
310 START_TEST(urlcache)
311 {
312     HMODULE hdll;
313     hdll = GetModuleHandleA("wininet.dll");
314     pDeleteUrlCacheEntryA = (void*)GetProcAddress(hdll, "DeleteUrlCacheEntryA");
315     pUnlockUrlCacheEntryFileA = (void*)GetProcAddress(hdll, "UnlockUrlCacheEntryFileA");
316     test_urlcacheA();
317     test_FindCloseUrlCache();
318     test_GetDiskInfoA();
319 }