Prevent crash when no URL is specified.
[wine] / dlls / shell32 / tests / shelllink.c
1 /*
2  * Unit tests for shelllinks
3  *
4  * Copyright 2004 Mike McCormack
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  * This is a test program for the SHGet{Special}Folder{Path|Location} functions
20  * of shell32, that get either a filesytem path or a LPITEMIDLIST (shell
21  * namespace) path for a given folder (CSIDL value).
22  *
23  */
24
25 #define COBJMACROS
26
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "shlguid.h"
32 #include "shobjidl.h"
33 #include "shlobj.h"
34 #include "wine/test.h"
35
36 static const WCHAR lnkfile[]= { 'C',':','\\','t','e','s','t','.','l','n','k',0 };
37 static const WCHAR notafile[]= { 'C',':','\\','n','o','n','e','x','i','s','t','e','n','t','\\','f','i','l','e',0 };
38
39
40 /* For some reason SHILCreateFromPath does not work on Win98 and
41  * SHSimpleIDListFromPathA does not work on NT4. But if we call both we
42  * get what we want on all platforms.
43  */
44 static LPITEMIDLIST (WINAPI *pSHSimpleIDListFromPathA)(LPCSTR)=NULL;
45
46 static LPITEMIDLIST path_to_pidl(const char* path)
47 {
48     LPITEMIDLIST pidl;
49
50     if (!pSHSimpleIDListFromPathA)
51     {
52         HMODULE hdll=LoadLibraryA("shell32.dll");
53         pSHSimpleIDListFromPathA=(void*)GetProcAddress(hdll, (char*)162);
54         if (!pSHSimpleIDListFromPathA)
55             trace("SHSimpleIDListFromPathA not found in shell32.dll");
56     }
57
58     pidl=NULL;
59     if (pSHSimpleIDListFromPathA)
60         pidl=pSHSimpleIDListFromPathA(path);
61
62     if (!pidl)
63     {
64         WCHAR* pathW;
65         HRESULT r;
66         int len;
67
68         len=MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
69         pathW=HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
70         MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, len);
71
72         r=SHILCreateFromPath(pathW, &pidl, NULL);
73         todo_wine {
74         ok(SUCCEEDED(r), "SHILCreateFromPath failed (0x%08lx)\n", r);
75         }
76         HeapFree(GetProcessHeap(), 0, pathW);
77     }
78     return pidl;
79 }
80
81
82 /*
83  * Test manipulation of an IShellLink's properties.
84  */
85
86 static void test_get_set()
87 {
88     HRESULT r;
89     IShellLinkA *sl;
90     char mypath[MAX_PATH];
91     char buffer[INFOTIPSIZE];
92     LPITEMIDLIST pidl, tmp_pidl;
93     char * str;
94     int i;
95     WORD w;
96
97     r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
98                          &IID_IShellLinkA, (LPVOID*)&sl);
99     ok(SUCCEEDED(r), "no IID_IShellLinkA (0x%08lx)\n", r);
100     if (!SUCCEEDED(r))
101         return;
102
103     /* Test Getting / Setting the description */
104     strcpy(buffer,"garbage");
105     r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
106     ok(SUCCEEDED(r), "GetDescription failed (0x%08lx)\n", r);
107     ok(*buffer=='\0', "GetDescription returned '%s'\n", buffer);
108
109     str="Some description";
110     r = IShellLinkA_SetDescription(sl, str);
111     ok(SUCCEEDED(r), "SetDescription failed (0x%08lx)\n", r);
112
113     strcpy(buffer,"garbage");
114     r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
115     ok(SUCCEEDED(r), "GetDescription failed (0x%08lx)\n", r);
116     ok(lstrcmp(buffer,str)==0, "GetDescription returned '%s'\n", buffer);
117
118     /* Test Getting / Setting the work directory */
119     strcpy(buffer,"garbage");
120     r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
121     ok(SUCCEEDED(r), "GetWorkingDirectory failed (0x%08lx)\n", r);
122     ok(*buffer=='\0', "GetWorkingDirectory returned '%s'\n", buffer);
123
124     str="c:\\nonexistent\\directory";
125     r = IShellLinkA_SetWorkingDirectory(sl, str);
126     ok(SUCCEEDED(r), "SetWorkingDirectory failed (0x%08lx)\n", r);
127
128     strcpy(buffer,"garbage");
129     r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
130     ok(SUCCEEDED(r), "GetWorkingDirectory failed (0x%08lx)\n", r);
131     ok(lstrcmpi(buffer,str)==0, "GetWorkingDirectory returned '%s'\n", buffer);
132
133     /* Test Getting / Setting the work directory */
134     strcpy(buffer,"garbage");
135     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
136     ok(SUCCEEDED(r), "GetPath failed (0x%08lx)\n", r);
137     ok(*buffer=='\0', "GetPath returned '%s'\n", buffer);
138
139     r = IShellLinkA_SetPath(sl, "");
140     ok(r==S_OK, "SetPath failed (0x%08lx)\n", r);
141
142     strcpy(buffer,"garbage");
143     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
144     ok(SUCCEEDED(r), "GetPath failed (0x%08lx)\n", r);
145     ok(*buffer=='\0', "GetPath returned '%s'\n", buffer);
146
147     str="c:\\nonexistent\\file";
148     r = IShellLinkA_SetPath(sl, str);
149     ok(r==S_FALSE, "SetPath failed (0x%08lx)\n", r);
150
151     strcpy(buffer,"garbage");
152     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
153     ok(SUCCEEDED(r), "GetPath failed (0x%08lx)\n", r);
154     ok(lstrcmpi(buffer,str)==0, "GetPath returned '%s'\n", buffer);
155
156     /* Get some a real path to play with */
157     r=GetModuleFileName(NULL, mypath, sizeof(mypath));
158     ok(r>=0 && r<sizeof(mypath), "GetModuleFileName failed (%ld)\n", r);
159
160     /* Test the interaction of SetPath and SetIDList */
161     tmp_pidl=NULL;
162     r = IShellLinkA_GetIDList(sl, &tmp_pidl);
163     ok(SUCCEEDED(r), "GetIDList failed (0x%08lx)\n", r);
164     if (SUCCEEDED(r))
165     {
166         strcpy(buffer,"garbage");
167         r=SHGetPathFromIDListA(tmp_pidl, buffer);
168         todo_wine {
169         ok(r, "SHGetPathFromIDListA failed\n");
170         }
171         if (r)
172             ok(lstrcmpi(buffer,str)==0, "GetIDList returned '%s'\n", buffer);
173     }
174
175     pidl=path_to_pidl(mypath);
176     todo_wine {
177     ok(pidl!=NULL, "path_to_pidl returned a NULL pidl\n");
178     }
179
180     if (pidl)
181     {
182         r = IShellLinkA_SetIDList(sl, pidl);
183         ok(SUCCEEDED(r), "SetIDList failed (0x%08lx)\n", r);
184
185         tmp_pidl=NULL;
186         r = IShellLinkA_GetIDList(sl, &tmp_pidl);
187         ok(SUCCEEDED(r), "GetIDList failed (0x%08lx)\n", r);
188         ok(tmp_pidl && ILIsEqual(pidl, tmp_pidl),
189            "GetIDList returned an incorrect pidl\n");
190
191         /* tmp_pidl is owned by IShellLink so we don't free it */
192         ILFree(pidl);
193
194         strcpy(buffer,"garbage");
195         r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
196         ok(SUCCEEDED(r), "GetPath failed (0x%08lx)\n", r);
197         ok(lstrcmpi(buffer, mypath)==0, "GetPath returned '%s'\n", buffer);
198     }
199
200     /* Test Getting / Setting the arguments */
201     strcpy(buffer,"garbage");
202     r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
203     ok(SUCCEEDED(r), "GetArguments failed (0x%08lx)\n", r);
204     ok(*buffer=='\0', "GetArguments returned '%s'\n", buffer);
205
206     str="param1 \"spaced param2\"";
207     r = IShellLinkA_SetArguments(sl, str);
208     ok(SUCCEEDED(r), "SetArguments failed (0x%08lx)\n", r);
209
210     strcpy(buffer,"garbage");
211     r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
212     ok(SUCCEEDED(r), "GetArguments failed (0x%08lx)\n", r);
213     ok(lstrcmp(buffer,str)==0, "GetArguments returned '%s'\n", buffer);
214
215     /* Test Getting / Setting showcmd */
216     i=0xdeadbeef;
217     r = IShellLinkA_GetShowCmd(sl, &i);
218     ok(SUCCEEDED(r), "GetShowCmd failed (0x%08lx)\n", r);
219     ok(i==SW_SHOWNORMAL, "GetShowCmd returned %d\n", i);
220
221     r = IShellLinkA_SetShowCmd(sl, SW_SHOWMAXIMIZED);
222     ok(SUCCEEDED(r), "SetShowCmd failed (0x%08lx)\n", r);
223
224     i=0xdeadbeef;
225     r = IShellLinkA_GetShowCmd(sl, &i);
226     ok(SUCCEEDED(r), "GetShowCmd failed (0x%08lx)\n", r);
227     ok(i==SW_SHOWMAXIMIZED, "GetShowCmd returned %d'\n", i);
228
229     /* Test Getting / Setting the icon */
230     i=0xdeadbeef;
231     strcpy(buffer,"garbage");
232     r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
233     todo_wine {
234     ok(SUCCEEDED(r), "GetIconLocation failed (0x%08lx)\n", r);
235     }
236     ok(*buffer=='\0', "GetIconLocation returned '%s'\n", buffer);
237     ok(i==0, "GetIconLocation returned %d\n", i);
238
239     str="c:\\nonexistent\\file";
240     r = IShellLinkA_SetIconLocation(sl, str, 0xbabecafe);
241     ok(SUCCEEDED(r), "SetIconLocation failed (0x%08lx)\n", r);
242
243     i=0xdeadbeef;
244     r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
245     ok(SUCCEEDED(r), "GetIconLocation failed (0x%08lx)\n", r);
246     ok(lstrcmpi(buffer,str)==0, "GetArguments returned '%s'\n", buffer);
247     ok(i==0xbabecafe, "GetIconLocation returned %d'\n", i);
248
249     /* Test Getting / Setting the hot key */
250     w=0xbeef;
251     r = IShellLinkA_GetHotkey(sl, &w);
252     ok(SUCCEEDED(r), "GetHotkey failed (0x%08lx)\n", r);
253     ok(w==0, "GetHotkey returned %d\n", w);
254
255     r = IShellLinkA_SetHotkey(sl, 0x5678);
256     ok(SUCCEEDED(r), "SetHotkey failed (0x%08lx)\n", r);
257
258     w=0xbeef;
259     r = IShellLinkA_GetHotkey(sl, &w);
260     ok(SUCCEEDED(r), "GetHotkey failed (0x%08lx)\n", r);
261     ok(w==0x5678, "GetHotkey returned %d'\n", w);
262
263     IShellLinkA_Release(sl);
264 }
265
266
267 /*
268  * Test saving and loading .lnk files
269  */
270
271 typedef struct
272 {
273     char* description;
274     char* workdir;
275     char* path;
276     LPITEMIDLIST pidl;
277     char* arguments;
278     int   showcmd;
279     char* icon;
280     int   icon_id;
281     WORD  hotkey;
282 } lnk_desc_t;
283
284 #define lok                   ok_(__FILE__, line)
285 #define create_lnk(a,b,c)     create_lnk_(__LINE__, (a), (b), (c))
286 #define check_lnk(a,b)        check_lnk_(__LINE__, (a), (b))
287
288 static void create_lnk_(int line, const WCHAR* path, lnk_desc_t* desc, int save_fails)
289 {
290     HRESULT r;
291     IShellLinkA *sl;
292     IPersistFile *pf;
293
294     r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
295                          &IID_IShellLinkA, (LPVOID*)&sl);
296     lok(SUCCEEDED(r), "no IID_IShellLinkA (0x%08lx)\n", r);
297     if (!SUCCEEDED(r))
298         return;
299
300     if (desc->description)
301     {
302         r = IShellLinkA_SetDescription(sl, desc->description);
303         lok(SUCCEEDED(r), "SetDescription failed (0x%08lx)\n", r);
304     }
305     if (desc->workdir)
306     {
307         r = IShellLinkA_SetWorkingDirectory(sl, desc->workdir);
308         lok(SUCCEEDED(r), "SetWorkingDirectory failed (0x%08lx)\n", r);
309     }
310     if (desc->path)
311     {
312         r = IShellLinkA_SetPath(sl, desc->path);
313         lok(SUCCEEDED(r), "SetPath failed (0x%08lx)\n", r);
314     }
315     if (desc->pidl)
316     {
317         r = IShellLinkA_SetIDList(sl, desc->pidl);
318         lok(SUCCEEDED(r), "SetIDList failed (0x%08lx)\n", r);
319     }
320     if (desc->arguments)
321     {
322         r = IShellLinkA_SetArguments(sl, desc->arguments);
323         lok(SUCCEEDED(r), "SetArguments failed (0x%08lx)\n", r);
324     }
325     if (desc->showcmd)
326     {
327         r = IShellLinkA_SetShowCmd(sl, desc->showcmd);
328         lok(SUCCEEDED(r), "SetShowCmd failed (0x%08lx)\n", r);
329     }
330     if (desc->icon)
331     {
332         r = IShellLinkA_SetIconLocation(sl, desc->icon, desc->icon_id);
333         lok(SUCCEEDED(r), "SetIconLocation failed (0x%08lx)\n", r);
334     }
335     if (desc->hotkey)
336     {
337         r = IShellLinkA_SetHotkey(sl, desc->hotkey);
338         lok(SUCCEEDED(r), "SetHotkey failed (0x%08lx)\n", r);
339     }
340
341     r = IShellLinkW_QueryInterface(sl, &IID_IPersistFile, (LPVOID*)&pf);
342     lok(SUCCEEDED(r), "no IID_IPersistFile (0x%08lx)\n", r);
343     if (SUCCEEDED(r))
344     {
345         r = IPersistFile_Save(pf, path, TRUE);
346         if (save_fails)
347         {
348             todo_wine {
349             lok(SUCCEEDED(r), "save failed (0x%08lx)\n", r);
350             }
351         }
352         else
353         {
354             lok(SUCCEEDED(r), "save failed (0x%08lx)\n", r);
355         }
356         IPersistFile_Release(pf);
357     }
358
359     IShellLinkA_Release(sl);
360 }
361
362 static void check_lnk_(int line, const WCHAR* path, lnk_desc_t* desc)
363 {
364     HRESULT r;
365     IShellLinkA *sl;
366     IPersistFile *pf;
367     char buffer[INFOTIPSIZE];
368
369     r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
370                          &IID_IShellLinkA, (LPVOID*)&sl);
371     lok(SUCCEEDED(r), "no IID_IShellLinkA (0x%08lx)\n", r);
372     if (!SUCCEEDED(r))
373         return;
374
375     r = IShellLinkA_QueryInterface(sl, &IID_IPersistFile, (LPVOID*)&pf);
376     lok(SUCCEEDED(r), "no IID_IPersistFile (0x%08lx)\n", r);
377     if (!SUCCEEDED(r))
378     {
379         IShellLinkA_Release(sl);
380         return;
381     }
382
383     r = IPersistFile_Load(pf, path, STGM_READ);
384     lok(SUCCEEDED(r), "load failed (0x%08lx)\n", r);
385     IPersistFile_Release(pf);
386     if (!SUCCEEDED(r))
387     {
388         IShellLinkA_Release(sl);
389         return;
390     }
391
392     if (desc->description)
393     {
394         strcpy(buffer,"garbage");
395         r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
396         lok(SUCCEEDED(r), "GetDescription failed (0x%08lx)\n", r);
397         lok(lstrcmp(buffer, desc->description)==0,
398            "GetDescription returned '%s' instead of '%s'\n",
399            buffer, desc->description);
400     }
401     if (desc->workdir)
402     {
403         strcpy(buffer,"garbage");
404         r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
405         lok(SUCCEEDED(r), "GetWorkingDirectory failed (0x%08lx)\n", r);
406         lok(lstrcmpi(buffer, desc->workdir)==0,
407            "GetWorkingDirectory returned '%s' instead of '%s'\n",
408            buffer, desc->workdir);
409     }
410     if (desc->path)
411     {
412         strcpy(buffer,"garbage");
413         r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
414         lok(SUCCEEDED(r), "GetPath failed (0x%08lx)\n", r);
415         lok(lstrcmpi(buffer, desc->path)==0,
416            "GetPath returned '%s' instead of '%s'\n",
417            buffer, desc->path);
418     }
419     if (desc->pidl)
420     {
421         LPITEMIDLIST pidl=NULL;
422         r = IShellLinkA_GetIDList(sl, &pidl);
423         lok(SUCCEEDED(r), "GetIDList failed (0x%08lx)\n", r);
424         lok(ILIsEqual(pidl, desc->pidl),
425            "GetIDList returned an incorrect pidl\n");
426     }
427     if (desc->showcmd)
428     {
429         int i=0xdeadbeef;
430         r = IShellLinkA_GetShowCmd(sl, &i);
431         lok(SUCCEEDED(r), "GetShowCmd failed (0x%08lx)\n", r);
432         lok(i==desc->showcmd,
433            "GetShowCmd returned 0x%0x instead of 0x%0x\n",
434            i, desc->showcmd);
435     }
436     if (desc->icon)
437     {
438         int i=0xdeadbeef;
439         strcpy(buffer,"garbage");
440         r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
441         lok(SUCCEEDED(r), "GetIconLocation failed (0x%08lx)\n", r);
442         lok(lstrcmpi(buffer, desc->icon)==0,
443            "GetIconLocation returned '%s' instead of '%s'\n",
444            buffer, desc->icon);
445         lok(i==desc->icon_id,
446            "GetIconLocation returned 0x%0x instead of 0x%0x\n",
447            i, desc->icon_id);
448     }
449     if (desc->hotkey)
450     {
451         WORD i=0xbeef;
452         r = IShellLinkA_GetHotkey(sl, &i);
453         lok(SUCCEEDED(r), "GetHotkey failed (0x%08lx)\n", r);
454         lok(i==desc->hotkey,
455            "GetHotkey returned 0x%04x instead of 0x%04x\n",
456            i, desc->hotkey);
457     }
458
459     IShellLinkA_Release(sl);
460 }
461
462 static void test_load_save()
463 {
464     lnk_desc_t desc;
465     char mypath[MAX_PATH];
466     char mydir[MAX_PATH];
467     char* p;
468     DWORD r;
469
470     /* Save an empty .lnk file */
471     memset(&desc, 0, sizeof(desc));
472     create_lnk(lnkfile, &desc, 0);
473
474     /* It should come back as a bunch of empty strings */
475     desc.description="";
476     desc.workdir="";
477     desc.path="";
478     desc.arguments="";
479     desc.icon="";
480     check_lnk(lnkfile, &desc);
481
482
483     /* Point a .lnk file to nonexistent files */
484     desc.description="";
485     desc.workdir="c:\\Nonexitent\\work\\directory";
486     desc.path="c:\\nonexistent\\path";
487     desc.pidl=NULL;
488     desc.arguments="";
489     desc.showcmd=0;
490     desc.icon="c:\\nonexistent\\icon\\file";
491     desc.icon_id=1234;
492     desc.hotkey=0;
493     create_lnk(lnkfile, &desc, 0);
494     check_lnk(lnkfile, &desc);
495
496     r=GetModuleFileName(NULL, mypath, sizeof(mypath));
497     ok(r>=0 && r<sizeof(mypath), "GetModuleFileName failed (%ld)\n", r);
498     strcpy(mydir, mypath);
499     p=strrchr(mydir, '\\');
500     if (p)
501         *p='\0';
502
503
504     /* Overwrite the existing lnk file and point it to existing files */
505     desc.description="test 2";
506     desc.workdir=mydir;
507     desc.path=mypath;
508     desc.pidl=NULL;
509     desc.arguments="/option1 /option2 \"Some string\"";
510     desc.showcmd=SW_SHOWNORMAL;
511     desc.icon=mypath;
512     desc.icon_id=0;
513     desc.hotkey=0x1234;
514     create_lnk(lnkfile, &desc, 0);
515     check_lnk(lnkfile, &desc);
516
517     /* FIXME: Also test saving a .lnk pointing to a pidl that cannot be
518      * represented as a path.
519      */
520
521     /* DeleteFileW is not implemented on Win9x */
522     r=DeleteFileA("c:\\test.lnk");
523     ok(r, "failed to delete link (%ld)\n", GetLastError());
524 }
525
526 START_TEST(shelllink)
527 {
528     HRESULT r;
529
530     r = CoInitialize(NULL);
531     ok(SUCCEEDED(r), "CoInitialize failed (0x%08lx)\b", r);
532     if (!SUCCEEDED(r))
533         return;
534
535     test_get_set();
536     test_load_save();
537
538     CoUninitialize();
539 }