wnaspi32: Make winaspi.dll into a stand-alone 16-bit module.
[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
29 #include "wine/test.h"
30
31 #define TEST_URL    "http://urlcachetest.winehq.org/index.html"
32
33 static BOOL (WINAPI *pDeleteUrlCacheEntryA)(LPCSTR);
34 static BOOL (WINAPI *pUnlockUrlCacheEntryFileA)(LPCSTR,DWORD);
35
36 static char filenameA[MAX_PATH + 1];
37
38 static void check_cache_entry_infoA(const char *returnedfrom, LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo)
39 {
40     ok(lpCacheEntryInfo->dwStructSize == sizeof(*lpCacheEntryInfo), "%s: dwStructSize was %d\n", returnedfrom, lpCacheEntryInfo->dwStructSize);
41     ok(!strcmp(lpCacheEntryInfo->lpszSourceUrlName, TEST_URL), "%s: lpszSourceUrlName should be %s instead of %s\n", returnedfrom, TEST_URL, lpCacheEntryInfo->lpszSourceUrlName);
42     ok(!strcmp(lpCacheEntryInfo->lpszLocalFileName, filenameA), "%s: lpszLocalFileName should be %s instead of %s\n", returnedfrom, filenameA, lpCacheEntryInfo->lpszLocalFileName);
43     ok(!strcmp(lpCacheEntryInfo->lpszFileExtension, "html"), "%s: lpszFileExtension should be html instead of %s\n", returnedfrom, lpCacheEntryInfo->lpszFileExtension);
44 }
45
46 static void test_find_url_cache_entriesA(void)
47 {
48     BOOL ret;
49     HANDLE hEnumHandle;
50     BOOL found = FALSE;
51     DWORD cbCacheEntryInfo;
52     DWORD cbCacheEntryInfoSaved;
53     LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo;
54
55     cbCacheEntryInfo = 0;
56     hEnumHandle = FindFirstUrlCacheEntry(NULL, NULL, &cbCacheEntryInfo);
57     ok(!hEnumHandle, "FindFirstUrlCacheEntry should have failed\n");
58     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "FindFirstUrlCacheEntry should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
59     lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo * sizeof(char));
60     cbCacheEntryInfoSaved = cbCacheEntryInfo;
61     hEnumHandle = FindFirstUrlCacheEntry(NULL, lpCacheEntryInfo, &cbCacheEntryInfo);
62     ok(hEnumHandle != NULL, "FindFirstUrlCacheEntry failed with error %d\n", GetLastError());
63     while (TRUE)
64     {
65         if (!strcmp(lpCacheEntryInfo->lpszSourceUrlName, TEST_URL))
66         {
67             found = TRUE;
68             break;
69         }
70         cbCacheEntryInfo = cbCacheEntryInfoSaved;
71         ret = FindNextUrlCacheEntry(hEnumHandle, lpCacheEntryInfo, &cbCacheEntryInfo);
72         if (!ret)
73         {
74             if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
75             {
76                 lpCacheEntryInfo = HeapReAlloc(GetProcessHeap(), 0, lpCacheEntryInfo, cbCacheEntryInfo);
77                 cbCacheEntryInfoSaved = cbCacheEntryInfo;
78                 ret = FindNextUrlCacheEntry(hEnumHandle, lpCacheEntryInfo, &cbCacheEntryInfo);
79             }
80         }
81         ok(ret, "FindNextUrlCacheEntry failed with error %d\n", GetLastError());
82         if (!ret)
83             break;
84     }
85     ok(found, "committed url cache entry not found during enumeration\n");
86
87     ret = FindCloseUrlCache(hEnumHandle);
88     ok(ret, "FindCloseUrlCache failed with error %d\n", GetLastError());
89 }
90
91 static void test_GetUrlCacheEntryInfoExA(void)
92 {
93     BOOL ret;
94     DWORD cbCacheEntryInfo;
95     LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo;
96
97     ret = GetUrlCacheEntryInfoEx(NULL, NULL, NULL, NULL, NULL, NULL, 0);
98     ok(!ret, "GetUrlCacheEntryInfoEx with NULL URL and NULL args should have failed\n");
99     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());
100
101     ret = GetUrlCacheEntryInfoEx(TEST_URL, NULL, NULL, NULL, NULL, NULL, 0);
102     ok(ret, "GetUrlCacheEntryInfoEx with NULL args failed with error %d\n", GetLastError());
103
104     cbCacheEntryInfo = 0;
105     ret = GetUrlCacheEntryInfoEx(TEST_URL, NULL, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
106     ok(!ret, "GetUrlCacheEntryInfoEx with zero-length buffer should fail\n");
107     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetUrlCacheEntryInfoEx should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
108
109     lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
110     ret = GetUrlCacheEntryInfoEx(TEST_URL, lpCacheEntryInfo, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
111     ok(ret, "GetUrlCacheEntryInfoEx failed with error %d\n", GetLastError());
112
113     check_cache_entry_infoA("GetUrlCacheEntryInfoEx", lpCacheEntryInfo);
114
115     cbCacheEntryInfo = 100000;
116     SetLastError(0xdeadbeef);
117     ret = GetUrlCacheEntryInfoEx(TEST_URL, NULL, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
118     ok(!ret, "GetUrlCacheEntryInfoEx with zero-length buffer should fail\n");
119     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetUrlCacheEntryInfoEx should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
120
121     HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
122 }
123
124 static void test_RetrieveUrlCacheEntryA(void)
125 {
126     BOOL ret;
127     DWORD cbCacheEntryInfo;
128
129     cbCacheEntryInfo = 0;
130     SetLastError(0xdeadbeef);
131     ret = RetrieveUrlCacheEntryFile(NULL, NULL, &cbCacheEntryInfo, 0);
132     ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
133     ok(GetLastError() == ERROR_INVALID_PARAMETER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
134
135     if (0)
136     {
137         /* Crashes on Win9x, NT4 and W2K */
138         SetLastError(0xdeadbeef);
139         ret = RetrieveUrlCacheEntryFile(TEST_URL, NULL, NULL, 0);
140         ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
141         ok(GetLastError() == ERROR_INVALID_PARAMETER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
142     }
143
144     SetLastError(0xdeadbeef);
145     cbCacheEntryInfo = 100000;
146     ret = RetrieveUrlCacheEntryFile(NULL, NULL, &cbCacheEntryInfo, 0);
147     ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
148     ok(GetLastError() == ERROR_INVALID_PARAMETER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
149 }
150
151 static void test_urlcacheA(void)
152 {
153     BOOL ret;
154     HANDLE hFile;
155     DWORD written;
156     BYTE zero_byte = 0;
157     LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo;
158     DWORD cbCacheEntryInfo;
159     static const FILETIME filetime_zero;
160
161     ret = CreateUrlCacheEntry(TEST_URL, 0, "html", filenameA, 0);
162     ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
163
164     hFile = CreateFileA(filenameA, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
165                         NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
166     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA failed with error %d\n", GetLastError());
167
168     ret = WriteFile(hFile, &zero_byte, sizeof(zero_byte), &written, NULL);
169     ok(ret, "WriteFile failed with error %d\n", GetLastError());
170
171     CloseHandle(hFile);
172
173     ret = CommitUrlCacheEntry(TEST_URL, filenameA, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY, NULL, 0, "html", NULL);
174     ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
175
176     cbCacheEntryInfo = 0;
177     ret = RetrieveUrlCacheEntryFile(TEST_URL, NULL, &cbCacheEntryInfo, 0);
178     ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
179     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
180
181     lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
182     ret = RetrieveUrlCacheEntryFile(TEST_URL, lpCacheEntryInfo, &cbCacheEntryInfo, 0);
183     ok(ret, "RetrieveUrlCacheEntryFile failed with error %d\n", GetLastError());
184
185     check_cache_entry_infoA("RetrieveUrlCacheEntryFile", lpCacheEntryInfo);
186
187     HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
188
189     if (pUnlockUrlCacheEntryFileA)
190     {
191         ret = pUnlockUrlCacheEntryFileA(TEST_URL, 0);
192         ok(ret, "UnlockUrlCacheEntryFileA failed with error %d\n", GetLastError());
193     }
194
195     /* test Find*UrlCacheEntry functions */
196     test_find_url_cache_entriesA();
197
198     test_GetUrlCacheEntryInfoExA();
199     test_RetrieveUrlCacheEntryA();
200
201     if (pDeleteUrlCacheEntryA)
202     {
203         ret = pDeleteUrlCacheEntryA(TEST_URL);
204         ok(ret, "DeleteUrlCacheEntryA failed with error %d\n", GetLastError());
205     }
206
207     ret = DeleteFile(filenameA);
208     todo_wine
209     ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "local file should no longer exist\n");
210 }
211
212 static void test_FindCloseUrlCache(void)
213 {
214     BOOL r;
215     DWORD err;
216     r = FindCloseUrlCache(NULL);
217     err = GetLastError();
218     ok(0 == r, "expected 0, got %d\n", r);
219     ok(ERROR_INVALID_HANDLE == err, "expected %d, got %d\n", ERROR_INVALID_HANDLE, err);
220 }
221
222 START_TEST(urlcache)
223 {
224     HMODULE hdll;
225     hdll = GetModuleHandleA("wininet.dll");
226     pDeleteUrlCacheEntryA = (void*)GetProcAddress(hdll, "DeleteUrlCacheEntryA");
227     pUnlockUrlCacheEntryFileA = (void*)GetProcAddress(hdll, "UnlockUrlCacheEntryFileA");
228     test_urlcacheA();
229     test_FindCloseUrlCache();
230 }