ieframe: Allow STGM_WRITE in IPropertyStorage::Open.
[wine.git] / dlls / ieframe / tests / intshcut.c
blobb54874914140a52f9cdac5feeb290d0fe03f2c8e
1 /*
2 * Unit tests to document InternetShortcut's behaviour
4 * Copyright 2008 Damjan Jovanovic
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>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winerror.h"
31 #include "shlobj.h"
32 #include "shobjidl.h"
33 #include "shlguid.h"
34 #include "ole2.h"
35 #include "mshtml.h"
36 #include "initguid.h"
37 #include "isguids.h"
38 #include "intshcut.h"
40 #include "wine/test.h"
42 static HRESULT WINAPI Unknown_QueryInterface(IUnknown *pUnknown, REFIID riid, void **ppvObject)
44 if (IsEqualGUID(&IID_IUnknown, riid))
46 *ppvObject = pUnknown;
47 return S_OK;
49 return E_NOINTERFACE;
52 static ULONG WINAPI Unknown_AddRef(IUnknown *pUnknown)
54 return 2;
57 static ULONG WINAPI Unknown_Release(IUnknown *pUnknown)
59 return 1;
62 static IUnknownVtbl unknownVtbl = {
63 Unknown_QueryInterface,
64 Unknown_AddRef,
65 Unknown_Release
68 static IUnknown unknown = {
69 &unknownVtbl
72 static void test_Aggregability(void)
74 HRESULT hr;
75 IUnknown *pUnknown = NULL;
77 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUnknown, (void**)&pUnknown);
78 ok(hr == S_OK, "could not create instance of CLSID_InternetShortcut with IID_IUnknown, hr = 0x%lx\n", hr);
79 if (pUnknown)
80 IUnknown_Release(pUnknown);
82 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&pUnknown);
83 ok(hr == S_OK, "could not create instance of CLSID_InternetShortcut with IID_IUniformResourceLocatorA, hr = 0x%lx\n", hr);
84 if (pUnknown)
85 IUnknown_Release(pUnknown);
87 hr = CoCreateInstance(&CLSID_InternetShortcut, &unknown, CLSCTX_ALL, &IID_IUnknown, (void**)&pUnknown);
88 ok(hr == CLASS_E_NOAGGREGATION, "aggregation didn't fail like it should, hr = 0x%lx\n", hr);
89 if (pUnknown)
90 IUnknown_Release(pUnknown);
93 static void can_query_interface(IUnknown *pUnknown, REFIID riid)
95 HRESULT hr;
96 IUnknown *newInterface;
97 hr = IUnknown_QueryInterface(pUnknown, riid, (void**)&newInterface);
98 ok(hr == S_OK, "interface %s could not be queried\n", wine_dbgstr_guid(riid));
99 if (SUCCEEDED(hr))
100 IUnknown_Release(newInterface);
103 static void test_QueryInterface(void)
105 HRESULT hr;
106 IUnknown *pUnknown;
108 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUnknown, (void**)&pUnknown);
109 ok(hr == S_OK, "Could not create InternetShortcut object: %08lx\n", hr);
111 can_query_interface(pUnknown, &IID_IUniformResourceLocatorA);
112 can_query_interface(pUnknown, &IID_IUniformResourceLocatorW);
113 can_query_interface(pUnknown, &IID_IPersistFile);
114 IUnknown_Release(pUnknown);
117 #define test_shortcut_url(a,b) _test_shortcut_url(__LINE__,a,b)
118 static void _test_shortcut_url(unsigned line, IUnknown *unk, const char *exurl)
120 IUniformResourceLocatorA *locator_a;
121 char *url_a;
122 HRESULT hres;
124 hres = IUnknown_QueryInterface(unk, &IID_IUniformResourceLocatorA, (void**)&locator_a);
125 ok_(__FILE__,line)(hres == S_OK, "Could not get IUniformResourceLocatorA iface: %08lx\n", hres);
127 hres = locator_a->lpVtbl->GetURL(locator_a, &url_a);
128 ok_(__FILE__,line)(hres == S_OK, "GetURL failed: %08lx\n", hres);
129 ok_(__FILE__,line)(!strcmp(url_a, exurl), "unexpected URL, got %s, expected %s\n", url_a, exurl);
130 CoTaskMemFree(url_a);
132 IUnknown_Release((IUnknown*)locator_a);
135 #define check_string_transform(a,b,c,d,e) _check_string_transform(__LINE__,a,b,c,d,e)
136 static void _check_string_transform(unsigned line, IUniformResourceLocatorA *urlA, LPCSTR input, DWORD flags,
137 LPCSTR expectedOutput, BOOL is_todo)
139 CHAR *output;
140 HRESULT hr;
142 hr = urlA->lpVtbl->SetURL(urlA, input, flags);
143 ok_(__FILE__,line)(hr == S_OK, "SetUrl failed, hr=0x%lx\n", hr);
144 if (FAILED(hr))
145 return;
147 output = (void*)0xdeadbeef;
148 hr = urlA->lpVtbl->GetURL(urlA, &output);
149 if(expectedOutput) {
150 todo_wine_if(is_todo) {
151 ok_(__FILE__,line)(hr == S_OK, "GetUrl failed, hr=0x%lx\n", hr);
153 todo_wine
154 ok_(__FILE__,line)(!lstrcmpA(output, expectedOutput), "unexpected URL change %s -> %s (expected %s)\n",
155 input, output, expectedOutput);
156 }else {
157 todo_wine
158 ok_(__FILE__,line)(hr == S_FALSE, "GetUrl failed, hr=0x%lx\n", hr);
159 todo_wine
160 ok_(__FILE__,line)(!output, "GetUrl returned %s\n", output);
162 if (hr == S_OK)
163 CoTaskMemFree(output);
166 static void test_ReadAndWriteProperties(void)
168 int iconIndex = 7;
169 PROPSPEC ps[2];
170 HRESULT hr;
171 IUniformResourceLocatorA *urlA;
172 IUniformResourceLocatorA *urlAFromFile;
173 WCHAR fileNameW[MAX_PATH];
174 static WCHAR iconPath[] = L"file:///C:/arbitrary/icon/path";
175 char testurl[] = "http://some/bogus/url.html";
176 PROPVARIANT pv[2], pvread[2];
178 ps[0].ulKind = PRSPEC_PROPID;
179 U(ps[0]).propid = PID_IS_ICONFILE;
180 ps[1].ulKind = PRSPEC_PROPID;
181 U(ps[1]).propid = PID_IS_ICONINDEX;
183 pv[0].vt = VT_LPWSTR;
184 U(pv[0]).pwszVal = iconPath;
185 pv[1].vt = VT_I4;
186 U(pv[1]).lVal = iconIndex;
188 /* Make sure we have a valid temporary directory */
189 GetTempPathW(MAX_PATH, fileNameW);
190 lstrcatW(fileNameW, L"testshortcut.url");
192 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlA);
193 ok(hr == S_OK, "Could not create CLSID_InternetShortcut instance: %08lx\n", hr);
194 if (hr == S_OK)
196 IPersistFile *pf;
197 IPropertyStorage *pPropStgWrite;
198 IPropertySetStorage *pPropSetStg;
200 /* We need to set a URL -- IPersistFile refuses to save without one. */
201 hr = urlA->lpVtbl->SetURL(urlA, testurl, 0);
202 ok(hr == S_OK, "Failed to set a URL. hr=0x%lx\n", hr);
204 /* Write this shortcut out to a file so that we can test reading it in again. */
205 hr = urlA->lpVtbl->QueryInterface(urlA, &IID_IPersistFile, (void **) &pf);
206 ok(hr == S_OK, "Failed to get the IPersistFile for writing. hr=0x%lx\n", hr);
208 hr = IPersistFile_Save(pf, fileNameW, TRUE);
209 ok(hr == S_OK, "Failed to save via IPersistFile. hr=0x%lx\n", hr);
211 IPersistFile_Release(pf);
213 hr = urlA->lpVtbl->QueryInterface(urlA, &IID_IPropertySetStorage, (void **) &pPropSetStg);
214 ok(hr == S_OK, "Unable to get an IPropertySetStorage, hr=0x%lx\n", hr);
216 hr = IPropertySetStorage_Open(pPropSetStg, &FMTID_Intshcut, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, &pPropStgWrite);
217 ok(hr == S_OK, "Unable to get an IPropertyStorage for writing, hr=0x%lx\n", hr);
219 hr = IPropertyStorage_WriteMultiple(pPropStgWrite, 2, ps, pv, 0);
220 ok(hr == S_OK, "Unable to set properties, hr=0x%lx\n", hr);
222 memset(pvread, 0, sizeof(pvread));
223 hr = IPropertyStorage_ReadMultiple(pPropStgWrite, 2, ps, pvread);
224 ok(hr == S_OK, "Unable to read properties, hr=0x%lx\n", hr);
225 ok(pvread[1].vt == VT_I4, "got %d\n", pvread[1].vt);
226 ok(U(pvread[1]).lVal == iconIndex, "Read wrong icon index: %d\n", U(pvread[1]).iVal);
227 ok(pvread[0].vt == VT_LPWSTR, "got %d\n", pvread[0].vt);
228 ok(lstrcmpW(U(pvread[0]).pwszVal, iconPath) == 0, "Wrong icon path read: %s\n", wine_dbgstr_w(U(pvread[0]).pwszVal));
229 PropVariantClear(&pvread[0]);
230 PropVariantClear(&pvread[1]);
232 hr = IPropertyStorage_Commit(pPropStgWrite, STGC_DEFAULT);
233 ok(hr == S_OK, "Failed to commit properties, hr=0x%lx\n", hr);
235 pPropStgWrite->lpVtbl->Release(pPropStgWrite);
237 /* Test with STGM_WRITE */
238 hr = IPropertySetStorage_Open(pPropSetStg, &FMTID_Intshcut, STGM_WRITE, &pPropStgWrite);
239 ok(hr == S_OK, "Unable to get an IPropertyStorage for writing, hr=0x%lx\n", hr);
241 if (hr == S_OK)
243 memset(pvread, 0, sizeof(pvread));
244 hr = IPropertyStorage_ReadMultiple(pPropStgWrite, 2, ps, pvread);
245 ok(hr == S_OK, "Unable to read properties, hr=0x%lx\n", hr);
246 ok(pvread[1].vt == VT_I4, "got %d\n", pvread[1].vt);
247 ok(U(pvread[1]).lVal == iconIndex, "Read wrong icon index: %d\n", U(pvread[1]).iVal);
248 ok(pvread[0].vt == VT_LPWSTR, "got %d\n", pvread[0].vt);
249 ok(lstrcmpW(U(pvread[0]).pwszVal, iconPath) == 0, "Wrong icon path read: %s\n", wine_dbgstr_w(U(pvread[0]).pwszVal));
250 PropVariantClear(&pvread[0]);
251 PropVariantClear(&pvread[1]);
253 IPropertyStorage_Release(pPropStgWrite);
255 urlA->lpVtbl->Release(urlA);
256 IPropertySetStorage_Release(pPropSetStg);
259 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlAFromFile);
260 ok(hr == S_OK, "Could not create CLSID_InternetShortcut instance: %08lx\n", hr);
261 if (hr == S_OK)
263 IPropertySetStorage *pPropSetStg;
264 IPropertyStorage *pPropStgRead;
265 PROPVARIANT pvread[2];
266 IPersistFile *pf;
267 LPSTR url = NULL;
269 /* Now read that .url file back in. */
270 hr = urlAFromFile->lpVtbl->QueryInterface(urlAFromFile, &IID_IPersistFile, (void **) &pf);
271 ok(hr == S_OK, "Failed to get the IPersistFile for reading. hr=0x%lx\n", hr);
273 hr = IPersistFile_Load(pf, fileNameW, 0);
274 ok(hr == S_OK, "Failed to load via IPersistFile. hr=0x%lx\n", hr);
275 IPersistFile_Release(pf);
278 hr = urlAFromFile->lpVtbl->GetURL(urlAFromFile, &url);
279 ok(hr == S_OK, "Unable to get url from file, hr=0x%lx\n", hr);
280 ok(lstrcmpA(url, testurl) == 0, "Wrong url read from file: %s\n",url);
281 CoTaskMemFree(url);
283 hr = urlAFromFile->lpVtbl->QueryInterface(urlAFromFile, &IID_IPropertySetStorage, (void **) &pPropSetStg);
284 ok(hr == S_OK, "Unable to get an IPropertySetStorage, hr=0x%lx\n", hr);
286 hr = IPropertySetStorage_Open(pPropSetStg, &FMTID_Intshcut, STGM_READ | STGM_SHARE_EXCLUSIVE, &pPropStgRead);
287 ok(hr == S_OK, "Unable to get an IPropertyStorage for reading, hr=0x%lx\n", hr);
289 memset(pvread, 0, sizeof(pvread));
290 hr = IPropertyStorage_ReadMultiple(pPropStgRead, 2, ps, pvread);
291 todo_wine /* Wine doesn't yet support setting properties after save */
293 ok(hr == S_OK, "Unable to read properties, hr=0x%lx\n", hr);
294 ok(pvread[1].vt == VT_I4, "got %d\n", pvread[1].vt);
295 ok(U(pvread[1]).lVal == iconIndex, "Read wrong icon index: %d\n", U(pvread[1]).iVal);
296 ok(pvread[0].vt == VT_LPWSTR, "got %d\n", pvread[0].vt);
297 ok(lstrcmpW(U(pvread[0]).pwszVal, iconPath) == 0, "Wrong icon path read: %s\n", wine_dbgstr_w(U(pvread[0]).pwszVal));
299 PropVariantClear(&pvread[0]);
300 PropVariantClear(&pvread[1]);
302 hr = IPropertyStorage_WriteMultiple(pPropStgRead, 2, ps, pv, 0);
303 ok(hr == STG_E_ACCESSDENIED, "setting properties should return STG_E_ACCESSDENIED instead of 0x%lx\n", hr);
305 IPropertyStorage_Release(pPropStgRead);
306 IPropertySetStorage_Release(pPropSetStg);
307 urlAFromFile->lpVtbl->Release(urlAFromFile);
308 DeleteFileW(fileNameW);
312 static void test_NullURLs(void)
314 LPSTR url = NULL;
315 HRESULT hr;
316 IUniformResourceLocatorA *urlA;
318 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlA);
319 ok(hr == S_OK, "Could not create InternetShortcut object: %08lx\n", hr);
321 hr = urlA->lpVtbl->GetURL(urlA, &url);
322 ok(hr == S_FALSE, "getting uninitialized URL unexpectedly failed, hr=0x%lx\n", hr);
323 ok(url == NULL, "uninitialized URL is not NULL but %s\n", url);
325 hr = urlA->lpVtbl->SetURL(urlA, NULL, 0);
326 ok(hr == S_OK, "setting NULL URL unexpectedly failed, hr=0x%lx\n", hr);
328 hr = urlA->lpVtbl->GetURL(urlA, &url);
329 ok(hr == S_FALSE, "getting NULL URL unexpectedly failed, hr=0x%lx\n", hr);
330 ok(url == NULL, "URL unexpectedly not NULL but %s\n", url);
332 urlA->lpVtbl->Release(urlA);
335 typedef struct {
336 const char *data;
337 const char *url;
338 } load_test_t;
340 static const load_test_t load_tests[] = {
341 {"[InternetShortcut]\n"
342 "URL=http://www.winehq.org/\n"
343 "HotKey=0\n"
344 "IDList=\n"
345 "[{000214A0-0000-0000-C000-000000000046}]\n"
346 "Prop0=1,2\n",
348 "http://www.winehq.org/"
352 static void test_Load(void)
354 IPersistFile *persist_file;
355 const load_test_t *test;
356 WCHAR file_path[MAX_PATH];
357 DWORD size;
358 HANDLE file;
359 HRESULT hres;
361 GetTempPathW(MAX_PATH, file_path);
362 lstrcatW(file_path, L"test.url");
364 for(test = load_tests; test < load_tests + ARRAY_SIZE(load_tests); test++) {
365 IPropertySetStorage *propsetstorage;
366 IPropertyStorage *propstorage;
367 PROPVARIANT v;
368 PROPSPEC ps;
370 file = CreateFileW(file_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
371 ok(file != INVALID_HANDLE_VALUE, "could not create test file\n");
372 if(file == INVALID_HANDLE_VALUE)
373 continue;
375 WriteFile(file, test->data, strlen(test->data), &size, NULL);
376 CloseHandle(file);
378 hres = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IPersistFile, (void**)&persist_file);
379 ok(hres == S_OK, "Could not create InternetShortcut instance: %08lx\n", hres);
381 hres = IPersistFile_Load(persist_file, file_path, 0);
382 ok(hres == S_OK, "Load failed: %08lx\n", hres);
384 test_shortcut_url((IUnknown*)persist_file, test->url);
386 hres = IPersistFile_QueryInterface(persist_file, &IID_IPropertySetStorage, (void **)&propsetstorage);
387 ok(hres == S_OK, "Unable to get an IPropertySetStorage, hr=0x%lx\n", hres);
389 hres = IPropertySetStorage_Open(propsetstorage, &FMTID_Intshcut, STGM_READ | STGM_SHARE_EXCLUSIVE, &propstorage);
390 ok(hres == S_OK, "Unable to get an IPropertyStorage for reading, hr=0x%lx\n", hres);
392 ps.ulKind = PRSPEC_PROPID;
393 U(ps).propid = PID_IS_ICONFILE;
394 v.vt = VT_NULL;
395 hres = IPropertyStorage_ReadMultiple(propstorage, 1, &ps, &v);
396 ok(hres == S_FALSE, "got 0x%08lx\n", hres);
397 ok(v.vt == VT_EMPTY, "got %d\n", v.vt);
399 ps.ulKind = PRSPEC_PROPID;
400 U(ps).propid = PID_IS_ICONINDEX;
401 v.vt = VT_EMPTY;
402 hres = IPropertyStorage_ReadMultiple(propstorage, 1, &ps, &v);
403 ok(hres == S_FALSE, "got 0x%08lx\n", hres);
404 ok(v.vt == VT_EMPTY, "got %d\n", v.vt);
406 IPropertyStorage_Release(propstorage);
407 IPropertySetStorage_Release(propsetstorage);
409 IPersistFile_Release(persist_file);
410 DeleteFileW(file_path);
414 static void test_SetURLFlags(void)
416 HRESULT hr;
417 IUniformResourceLocatorA *urlA;
419 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlA);
420 ok(hr == S_OK, "Could not create InternetShortcut object: %08lx\n", hr);
422 check_string_transform(urlA, "somerandomstring", 0, NULL, TRUE);
423 check_string_transform(urlA, "www.winehq.org", 0, NULL, TRUE);
425 check_string_transform(urlA, "www.winehq.org", IURL_SETURL_FL_GUESS_PROTOCOL, "http://www.winehq.org/", FALSE);
426 check_string_transform(urlA, "ftp.winehq.org", IURL_SETURL_FL_GUESS_PROTOCOL, "ftp://ftp.winehq.org/", FALSE);
428 urlA->lpVtbl->Release(urlA);
431 static void test_InternetShortcut(void)
433 IUniformResourceLocatorA *url;
434 HRESULT hres;
436 hres = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&url);
437 ok(hres == S_OK, "Could not create CLSID_InternetShortcut instance: %08lx\n", hres);
438 if(FAILED(hres))
439 return;
440 url->lpVtbl->Release(url);
442 test_Aggregability();
443 test_QueryInterface();
444 test_NullURLs();
445 test_SetURLFlags();
446 test_ReadAndWriteProperties();
447 test_Load();
450 START_TEST(intshcut)
452 OleInitialize(NULL);
454 test_InternetShortcut();
456 OleUninitialize();