msi: Fix a typo.
[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 char filenameA[MAX_PATH + 1];
34
35 static void check_cache_entry_infoA(const char *returnedfrom, LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo)
36 {
37     ok(lpCacheEntryInfo->dwStructSize == sizeof(*lpCacheEntryInfo), "%s: dwStructSize was %d\n", returnedfrom, lpCacheEntryInfo->dwStructSize);
38     ok(!strcmp(lpCacheEntryInfo->lpszSourceUrlName, TEST_URL), "%s: lpszSourceUrlName should be %s instead of %s\n", returnedfrom, TEST_URL, lpCacheEntryInfo->lpszSourceUrlName);
39     ok(!strcmp(lpCacheEntryInfo->lpszLocalFileName, filenameA), "%s: lpszLocalFileName should be %s instead of %s\n", returnedfrom, filenameA, lpCacheEntryInfo->lpszLocalFileName);
40     ok(!strcmp(lpCacheEntryInfo->lpszFileExtension, "html"), "%s: lpszFileExtension should be html instead of %s\n", returnedfrom, lpCacheEntryInfo->lpszFileExtension);
41 }
42
43 static void test_find_url_cache_entriesA(void)
44 {
45     BOOL ret;
46     HANDLE hEnumHandle;
47     BOOL found = FALSE;
48     DWORD cbCacheEntryInfo;
49     DWORD cbCacheEntryInfoSaved;
50     LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo;
51
52     cbCacheEntryInfo = 0;
53     hEnumHandle = FindFirstUrlCacheEntry(NULL, NULL, &cbCacheEntryInfo);
54     ok(!hEnumHandle, "FindFirstUrlCacheEntry should have failed\n");
55     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "FindFirstUrlCacheEntry should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
56     lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo * sizeof(char));
57     cbCacheEntryInfoSaved = cbCacheEntryInfo;
58     hEnumHandle = FindFirstUrlCacheEntry(NULL, lpCacheEntryInfo, &cbCacheEntryInfo);
59     ok(hEnumHandle != NULL, "FindFirstUrlCacheEntry failed with error %d\n", GetLastError());
60     while (TRUE)
61     {
62         if (!strcmp(lpCacheEntryInfo->lpszSourceUrlName, TEST_URL))
63         {
64             found = TRUE;
65             break;
66         }
67         cbCacheEntryInfo = cbCacheEntryInfoSaved;
68         ret = FindNextUrlCacheEntry(hEnumHandle, lpCacheEntryInfo, &cbCacheEntryInfo);
69         if (!ret)
70         {
71             if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
72             {
73                 lpCacheEntryInfo = HeapReAlloc(GetProcessHeap(), 0, lpCacheEntryInfo, cbCacheEntryInfo);
74                 cbCacheEntryInfoSaved = cbCacheEntryInfo;
75                 ret = FindNextUrlCacheEntry(hEnumHandle, lpCacheEntryInfo, &cbCacheEntryInfo);
76             }
77         }
78         ok(ret, "FindNextUrlCacheEntry failed with error %d\n", GetLastError());
79         if (!ret)
80             break;
81     }
82     ok(found, "committed url cache entry not found during enumeration\n");
83
84     ret = FindCloseUrlCache(hEnumHandle);
85     ok(ret, "FindCloseUrlCache failed with error %d\n", GetLastError());
86 }
87
88 static void test_GetUrlCacheEntryInfoExA(void)
89 {
90     BOOL ret;
91     DWORD cbCacheEntryInfo;
92     LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo;
93
94     ret = GetUrlCacheEntryInfoEx(NULL, NULL, NULL, NULL, NULL, NULL, 0);
95     ok(!ret, "GetUrlCacheEntryInfoEx with NULL URL and NULL args should have failed\n");
96     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());
97
98     ret = GetUrlCacheEntryInfoEx(TEST_URL, NULL, NULL, NULL, NULL, NULL, 0);
99     ok(ret, "GetUrlCacheEntryInfoEx with NULL args failed with error %d\n", GetLastError());
100
101     cbCacheEntryInfo = 0;
102     ret = GetUrlCacheEntryInfoEx(TEST_URL, NULL, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
103     ok(!ret, "GetUrlCacheEntryInfoEx with zero-length buffer should fail\n");
104     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetUrlCacheEntryInfoEx should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
105
106     lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
107     ret = GetUrlCacheEntryInfoEx(TEST_URL, lpCacheEntryInfo, &cbCacheEntryInfo, NULL, NULL, NULL, 0);
108     ok(ret, "GetUrlCacheEntryInfoEx failed with error %d\n", GetLastError());
109
110     check_cache_entry_infoA("GetUrlCacheEntryInfoEx", lpCacheEntryInfo);
111
112     HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
113 }
114
115 static void test_urlcacheA(void)
116 {
117     BOOL ret;
118     HANDLE hFile;
119     DWORD written;
120     BYTE zero_byte = 0;
121     LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo;
122     DWORD cbCacheEntryInfo;
123     static const FILETIME filetime_zero;
124
125     ret = CreateUrlCacheEntry(TEST_URL, 0, "html", filenameA, 0);
126     ok(ret, "CreateUrlCacheEntry failed with error %d\n", GetLastError());
127
128     hFile = CreateFileA(filenameA, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
129                         NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
130     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA failed with error %d\n", GetLastError());
131
132     ret = WriteFile(hFile, &zero_byte, sizeof(zero_byte), &written, NULL);
133     ok(ret, "WriteFile failed with error %d\n", GetLastError());
134
135     CloseHandle(hFile);
136
137     ret = CommitUrlCacheEntry(TEST_URL, filenameA, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY, NULL, 0, "html", NULL);
138     ok(ret, "CommitUrlCacheEntry failed with error %d\n", GetLastError());
139
140     cbCacheEntryInfo = 0;
141     ret = RetrieveUrlCacheEntryFile(TEST_URL, NULL, &cbCacheEntryInfo, 0);
142     ok(!ret, "RetrieveUrlCacheEntryFile should have failed\n");
143     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "RetrieveUrlCacheEntryFile should have set last error to ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
144
145     lpCacheEntryInfo = HeapAlloc(GetProcessHeap(), 0, cbCacheEntryInfo);
146     ret = RetrieveUrlCacheEntryFile(TEST_URL, lpCacheEntryInfo, &cbCacheEntryInfo, 0);
147     ok(ret, "RetrieveUrlCacheEntryFile failed with error %d\n", GetLastError());
148
149     check_cache_entry_infoA("RetrieveUrlCacheEntryFile", lpCacheEntryInfo);
150
151     HeapFree(GetProcessHeap(), 0, lpCacheEntryInfo);
152
153     ret = UnlockUrlCacheEntryFile(TEST_URL, 0);
154     ok(ret, "UnlockUrlCacheEntryFile failed with error %d\n", GetLastError());
155
156     /* test Find*UrlCacheEntry functions */
157     test_find_url_cache_entriesA();
158
159     test_GetUrlCacheEntryInfoExA();
160
161     ret = DeleteUrlCacheEntry(TEST_URL);
162     ok(ret, "DeleteUrlCacheEntry failed with error %d\n", GetLastError());
163
164     ret = DeleteFile(filenameA);
165     todo_wine
166     ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "local file should no longer exist\n");
167 }
168
169 START_TEST(urlcache)
170 {
171     test_urlcacheA();
172 }