crypt32: Introduce function to encode an array of items as a set.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 <windows.h>
28 #include "shlguid.h"
29 #include "shobjidl.h"
30 #include "shlobj.h"
31 #include "wine/test.h"
32
33 #include "shell32_test.h"
34
35
36 typedef void (WINAPI *fnILFree)(LPITEMIDLIST);
37 typedef BOOL (WINAPI *fnILIsEqual)(LPCITEMIDLIST, LPCITEMIDLIST);
38 typedef HRESULT (WINAPI *fnSHILCreateFromPath)(LPCWSTR, LPITEMIDLIST *,DWORD*);
39
40 static fnILFree pILFree;
41 static fnILIsEqual pILIsEqual;
42 static fnSHILCreateFromPath pSHILCreateFromPath;
43
44 static const GUID _IID_IShellLinkDataList = {
45     0x45e2b4ae, 0xb1c3, 0x11d0,
46     { 0xb9, 0x2f, 0x00, 0xa0, 0xc9, 0x03, 0x12, 0xe1 }
47 };
48
49 static const WCHAR lnkfile[]= { 'C',':','\\','t','e','s','t','.','l','n','k',0 };
50 static const WCHAR notafile[]= { 'C',':','\\','n','o','n','e','x','i','s','t','e','n','t','\\','f','i','l','e',0 };
51
52
53 /* For some reason SHILCreateFromPath does not work on Win98 and
54  * SHSimpleIDListFromPathA does not work on NT4. But if we call both we
55  * get what we want on all platforms.
56  */
57 static LPITEMIDLIST (WINAPI *pSHSimpleIDListFromPathAW)(LPCVOID);
58
59 static LPITEMIDLIST path_to_pidl(const char* path)
60 {
61     LPITEMIDLIST pidl;
62
63     if (!pSHSimpleIDListFromPathAW)
64     {
65         HMODULE hdll=LoadLibraryA("shell32.dll");
66         pSHSimpleIDListFromPathAW=(void*)GetProcAddress(hdll, (char*)162);
67         if (!pSHSimpleIDListFromPathAW)
68             trace("SHSimpleIDListFromPathAW not found in shell32.dll\n");
69     }
70
71     pidl=NULL;
72     /* pSHSimpleIDListFromPathAW maps to A on non NT platforms */
73     if (pSHSimpleIDListFromPathAW && (GetVersion() & 0x80000000))
74         pidl=pSHSimpleIDListFromPathAW(path);
75
76     if (!pidl)
77     {
78         WCHAR* pathW;
79         HRESULT r;
80         int len;
81
82         len=MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
83         pathW=HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
84         MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, len);
85
86         r=pSHILCreateFromPath(pathW, &pidl, NULL);
87         todo_wine {
88         ok(SUCCEEDED(r), "SHILCreateFromPath failed (0x%08x)\n", r);
89         }
90         HeapFree(GetProcessHeap(), 0, pathW);
91     }
92     return pidl;
93 }
94
95
96 /*
97  * Test manipulation of an IShellLink's properties.
98  */
99
100 static void test_get_set(void)
101 {
102     HRESULT r;
103     IShellLinkA *sl;
104     char mypath[MAX_PATH];
105     char buffer[INFOTIPSIZE];
106     LPITEMIDLIST pidl, tmp_pidl;
107     const char * str;
108     int i;
109     WORD w;
110
111     r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
112                          &IID_IShellLinkA, (LPVOID*)&sl);
113     ok(SUCCEEDED(r), "no IID_IShellLinkA (0x%08x)\n", r);
114     if (!SUCCEEDED(r))
115         return;
116
117     /* Test Getting / Setting the description */
118     strcpy(buffer,"garbage");
119     r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
120     ok(SUCCEEDED(r), "GetDescription failed (0x%08x)\n", r);
121     ok(*buffer=='\0', "GetDescription returned '%s'\n", buffer);
122
123     str="Some description";
124     r = IShellLinkA_SetDescription(sl, str);
125     ok(SUCCEEDED(r), "SetDescription failed (0x%08x)\n", r);
126
127     strcpy(buffer,"garbage");
128     r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
129     ok(SUCCEEDED(r), "GetDescription failed (0x%08x)\n", r);
130     ok(lstrcmp(buffer,str)==0, "GetDescription returned '%s'\n", buffer);
131
132     /* Test Getting / Setting the work directory */
133     strcpy(buffer,"garbage");
134     r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
135     ok(SUCCEEDED(r), "GetWorkingDirectory failed (0x%08x)\n", r);
136     ok(*buffer=='\0', "GetWorkingDirectory returned '%s'\n", buffer);
137
138     str="c:\\nonexistent\\directory";
139     r = IShellLinkA_SetWorkingDirectory(sl, str);
140     ok(SUCCEEDED(r), "SetWorkingDirectory failed (0x%08x)\n", r);
141
142     strcpy(buffer,"garbage");
143     r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
144     ok(SUCCEEDED(r), "GetWorkingDirectory failed (0x%08x)\n", r);
145     ok(lstrcmpi(buffer,str)==0, "GetWorkingDirectory returned '%s'\n", buffer);
146
147     /* Test Getting / Setting the work directory */
148     strcpy(buffer,"garbage");
149     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
150     ok(SUCCEEDED(r), "GetPath failed (0x%08x)\n", r);
151     ok(*buffer=='\0', "GetPath returned '%s'\n", buffer);
152
153     r = IShellLinkA_SetPath(sl, "");
154     ok(r==S_OK, "SetPath failed (0x%08x)\n", r);
155
156     strcpy(buffer,"garbage");
157     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
158     ok(SUCCEEDED(r), "GetPath failed (0x%08x)\n", r);
159     ok(*buffer=='\0', "GetPath returned '%s'\n", buffer);
160
161     /* Win98 returns S_FALSE, but WinXP returns S_OK */
162     str="c:\\nonexistent\\file";
163     r = IShellLinkA_SetPath(sl, str);
164     ok(r==S_FALSE || r==S_OK, "SetPath failed (0x%08x)\n", r);
165
166     strcpy(buffer,"garbage");
167     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
168     ok(SUCCEEDED(r), "GetPath failed (0x%08x)\n", r);
169     ok(lstrcmpi(buffer,str)==0, "GetPath returned '%s'\n", buffer);
170
171     /* Get some a real path to play with */
172     r=GetModuleFileName(NULL, mypath, sizeof(mypath));
173     ok(r>=0 && r<sizeof(mypath), "GetModuleFileName failed (%d)\n", r);
174
175     /* Test the interaction of SetPath and SetIDList */
176     tmp_pidl=NULL;
177     r = IShellLinkA_GetIDList(sl, &tmp_pidl);
178     ok(SUCCEEDED(r), "GetIDList failed (0x%08x)\n", r);
179     if (SUCCEEDED(r))
180     {
181         strcpy(buffer,"garbage");
182         r=SHGetPathFromIDListA(tmp_pidl, buffer);
183         todo_wine {
184         ok(r, "SHGetPathFromIDListA failed\n");
185         }
186         if (r)
187             ok(lstrcmpi(buffer,str)==0, "GetIDList returned '%s'\n", buffer);
188     }
189
190     pidl=path_to_pidl(mypath);
191     todo_wine {
192     ok(pidl!=NULL, "path_to_pidl returned a NULL pidl\n");
193     }
194
195     if (pidl)
196     {
197         r = IShellLinkA_SetIDList(sl, pidl);
198         ok(SUCCEEDED(r), "SetIDList failed (0x%08x)\n", r);
199
200         tmp_pidl=NULL;
201         r = IShellLinkA_GetIDList(sl, &tmp_pidl);
202         ok(SUCCEEDED(r), "GetIDList failed (0x%08x)\n", r);
203         ok(tmp_pidl && pILIsEqual(pidl, tmp_pidl),
204            "GetIDList returned an incorrect pidl\n");
205
206         /* tmp_pidl is owned by IShellLink so we don't free it */
207         pILFree(pidl);
208
209         strcpy(buffer,"garbage");
210         r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
211         ok(SUCCEEDED(r), "GetPath failed (0x%08x)\n", r);
212         ok(lstrcmpi(buffer, mypath)==0, "GetPath returned '%s'\n", buffer);
213     }
214
215     /* test path with quotes (Win98 IShellLinkA_SetPath returns S_FALSE, WinXP returns S_OK) */
216     r = IShellLinkA_SetPath(sl, "\"c:\\nonexistent\\file\"");
217     ok(r==S_FALSE || r == S_OK, "SetPath failed (0x%08x)\n", r);
218
219     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
220     ok(r==S_OK, "GetPath failed (0x%08x)\n", r);
221     ok(!lstrcmp(buffer, "C:\\nonexistent\\file"), "case doesn't match\n");
222
223     r = IShellLinkA_SetPath(sl, "\"c:\\foo");
224     ok(r==S_FALSE || r == S_OK, "SetPath failed (0x%08x)\n", r);
225
226     r = IShellLinkA_SetPath(sl, "\"\"c:\\foo");
227     ok(r==S_FALSE || r == S_OK, "SetPath failed (0x%08x)\n", r);
228
229     r = IShellLinkA_SetPath(sl, "c:\\foo\"");
230     ok(r==S_FALSE || r == S_OK, "SetPath failed (0x%08x)\n", r);
231
232     r = IShellLinkA_SetPath(sl, "\"\"c:\\foo\"");
233     ok(r==S_FALSE || r == S_OK, "SetPath failed (0x%08x)\n", r);
234
235     r = IShellLinkA_SetPath(sl, "\"\"c:\\foo\"\"");
236     ok(r==S_FALSE || r == S_OK, "SetPath failed (0x%08x)\n", r);
237
238     /* Test Getting / Setting the arguments */
239     strcpy(buffer,"garbage");
240     r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
241     ok(SUCCEEDED(r), "GetArguments failed (0x%08x)\n", r);
242     ok(*buffer=='\0', "GetArguments returned '%s'\n", buffer);
243
244     str="param1 \"spaced param2\"";
245     r = IShellLinkA_SetArguments(sl, str);
246     ok(SUCCEEDED(r), "SetArguments failed (0x%08x)\n", r);
247
248     strcpy(buffer,"garbage");
249     r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
250     ok(SUCCEEDED(r), "GetArguments failed (0x%08x)\n", r);
251     ok(lstrcmp(buffer,str)==0, "GetArguments returned '%s'\n", buffer);
252
253     /* Test Getting / Setting showcmd */
254     i=0xdeadbeef;
255     r = IShellLinkA_GetShowCmd(sl, &i);
256     ok(SUCCEEDED(r), "GetShowCmd failed (0x%08x)\n", r);
257     ok(i==SW_SHOWNORMAL, "GetShowCmd returned %d\n", i);
258
259     r = IShellLinkA_SetShowCmd(sl, SW_SHOWMAXIMIZED);
260     ok(SUCCEEDED(r), "SetShowCmd failed (0x%08x)\n", r);
261
262     i=0xdeadbeef;
263     r = IShellLinkA_GetShowCmd(sl, &i);
264     ok(SUCCEEDED(r), "GetShowCmd failed (0x%08x)\n", r);
265     ok(i==SW_SHOWMAXIMIZED, "GetShowCmd returned %d'\n", i);
266
267     /* Test Getting / Setting the icon */
268     i=0xdeadbeef;
269     strcpy(buffer,"garbage");
270     r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
271     todo_wine {
272     ok(SUCCEEDED(r), "GetIconLocation failed (0x%08x)\n", r);
273     }
274     ok(*buffer=='\0', "GetIconLocation returned '%s'\n", buffer);
275     ok(i==0, "GetIconLocation returned %d\n", i);
276
277     str="c:\\nonexistent\\file";
278     r = IShellLinkA_SetIconLocation(sl, str, 0xbabecafe);
279     ok(SUCCEEDED(r), "SetIconLocation failed (0x%08x)\n", r);
280
281     i=0xdeadbeef;
282     r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
283     ok(SUCCEEDED(r), "GetIconLocation failed (0x%08x)\n", r);
284     ok(lstrcmpi(buffer,str)==0, "GetArguments returned '%s'\n", buffer);
285     ok(i==0xbabecafe, "GetIconLocation returned %d'\n", i);
286
287     /* Test Getting / Setting the hot key */
288     w=0xbeef;
289     r = IShellLinkA_GetHotkey(sl, &w);
290     ok(SUCCEEDED(r), "GetHotkey failed (0x%08x)\n", r);
291     ok(w==0, "GetHotkey returned %d\n", w);
292
293     r = IShellLinkA_SetHotkey(sl, 0x5678);
294     ok(SUCCEEDED(r), "SetHotkey failed (0x%08x)\n", r);
295
296     w=0xbeef;
297     r = IShellLinkA_GetHotkey(sl, &w);
298     ok(SUCCEEDED(r), "GetHotkey failed (0x%08x)\n", r);
299     ok(w==0x5678, "GetHotkey returned %d'\n", w);
300
301     IShellLinkA_Release(sl);
302 }
303
304
305 /*
306  * Test saving and loading .lnk files
307  */
308
309 #define lok                   ok_(__FILE__, line)
310 #define lok_todo_4(todo_flag,a,b,c,d) \
311     if ((todo & todo_flag) == 0) lok((a), (b), (c), (d)); \
312     else todo_wine lok((a), (b), (c), (d));
313 #define lok_todo_2(todo_flag,a,b) \
314     if ((todo & todo_flag) == 0) lok((a), (b)); \
315     else todo_wine lok((a), (b));
316 #define check_lnk(a,b,c)        check_lnk_(__LINE__, (a), (b), (c))
317
318 void create_lnk_(int line, const WCHAR* path, lnk_desc_t* desc, int save_fails)
319 {
320     HRESULT r;
321     IShellLinkA *sl;
322     IPersistFile *pf;
323
324     r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
325                          &IID_IShellLinkA, (LPVOID*)&sl);
326     lok(SUCCEEDED(r), "no IID_IShellLinkA (0x%08x)\n", r);
327     if (!SUCCEEDED(r))
328         return;
329
330     if (desc->description)
331     {
332         r = IShellLinkA_SetDescription(sl, desc->description);
333         lok(SUCCEEDED(r), "SetDescription failed (0x%08x)\n", r);
334     }
335     if (desc->workdir)
336     {
337         r = IShellLinkA_SetWorkingDirectory(sl, desc->workdir);
338         lok(SUCCEEDED(r), "SetWorkingDirectory failed (0x%08x)\n", r);
339     }
340     if (desc->path)
341     {
342         r = IShellLinkA_SetPath(sl, desc->path);
343         lok(SUCCEEDED(r), "SetPath failed (0x%08x)\n", r);
344     }
345     if (desc->pidl)
346     {
347         r = IShellLinkA_SetIDList(sl, desc->pidl);
348         lok(SUCCEEDED(r), "SetIDList failed (0x%08x)\n", r);
349     }
350     if (desc->arguments)
351     {
352         r = IShellLinkA_SetArguments(sl, desc->arguments);
353         lok(SUCCEEDED(r), "SetArguments failed (0x%08x)\n", r);
354     }
355     if (desc->showcmd)
356     {
357         r = IShellLinkA_SetShowCmd(sl, desc->showcmd);
358         lok(SUCCEEDED(r), "SetShowCmd failed (0x%08x)\n", r);
359     }
360     if (desc->icon)
361     {
362         r = IShellLinkA_SetIconLocation(sl, desc->icon, desc->icon_id);
363         lok(SUCCEEDED(r), "SetIconLocation failed (0x%08x)\n", r);
364     }
365     if (desc->hotkey)
366     {
367         r = IShellLinkA_SetHotkey(sl, desc->hotkey);
368         lok(SUCCEEDED(r), "SetHotkey failed (0x%08x)\n", r);
369     }
370
371     r = IShellLinkW_QueryInterface(sl, &IID_IPersistFile, (LPVOID*)&pf);
372     lok(SUCCEEDED(r), "no IID_IPersistFile (0x%08x)\n", r);
373     if (SUCCEEDED(r))
374     {
375         r = IPersistFile_Save(pf, path, TRUE);
376         if (save_fails)
377         {
378             todo_wine {
379             lok(SUCCEEDED(r), "save failed (0x%08x)\n", r);
380             }
381         }
382         else
383         {
384             lok(SUCCEEDED(r), "save failed (0x%08x)\n", r);
385         }
386         IPersistFile_Release(pf);
387     }
388
389     IShellLinkA_Release(sl);
390 }
391
392 static void check_lnk_(int line, const WCHAR* path, lnk_desc_t* desc, int todo)
393 {
394     HRESULT r;
395     IShellLinkA *sl;
396     IPersistFile *pf;
397     char buffer[INFOTIPSIZE];
398
399     r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
400                          &IID_IShellLinkA, (LPVOID*)&sl);
401     lok(SUCCEEDED(r), "no IID_IShellLinkA (0x%08x)\n", r);
402     if (!SUCCEEDED(r))
403         return;
404
405     r = IShellLinkA_QueryInterface(sl, &IID_IPersistFile, (LPVOID*)&pf);
406     lok(SUCCEEDED(r), "no IID_IPersistFile (0x%08x)\n", r);
407     if (!SUCCEEDED(r))
408     {
409         IShellLinkA_Release(sl);
410         return;
411     }
412
413     r = IPersistFile_Load(pf, path, STGM_READ);
414     lok(SUCCEEDED(r), "load failed (0x%08x)\n", r);
415     IPersistFile_Release(pf);
416     if (!SUCCEEDED(r))
417     {
418         IShellLinkA_Release(sl);
419         return;
420     }
421
422     if (desc->description)
423     {
424         strcpy(buffer,"garbage");
425         r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
426         lok(SUCCEEDED(r), "GetDescription failed (0x%08x)\n", r);
427         lok_todo_4(0x1, lstrcmp(buffer, desc->description)==0,
428            "GetDescription returned '%s' instead of '%s'\n",
429            buffer, desc->description);
430     }
431     if (desc->workdir)
432     {
433         strcpy(buffer,"garbage");
434         r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
435         lok(SUCCEEDED(r), "GetWorkingDirectory failed (0x%08x)\n", r);
436         lok_todo_4(0x2, lstrcmpi(buffer, desc->workdir)==0,
437            "GetWorkingDirectory returned '%s' instead of '%s'\n",
438            buffer, desc->workdir);
439     }
440     if (desc->path)
441     {
442         strcpy(buffer,"garbage");
443         r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
444         lok(SUCCEEDED(r), "GetPath failed (0x%08x)\n", r);
445         lok_todo_4(0x4, lstrcmpi(buffer, desc->path)==0,
446            "GetPath returned '%s' instead of '%s'\n",
447            buffer, desc->path);
448     }
449     if (desc->pidl)
450     {
451         LPITEMIDLIST pidl=NULL;
452         r = IShellLinkA_GetIDList(sl, &pidl);
453         lok(SUCCEEDED(r), "GetIDList failed (0x%08x)\n", r);
454         lok_todo_2(0x8, pILIsEqual(pidl, desc->pidl),
455            "GetIDList returned an incorrect pidl\n");
456     }
457     if (desc->showcmd)
458     {
459         int i=0xdeadbeef;
460         r = IShellLinkA_GetShowCmd(sl, &i);
461         lok(SUCCEEDED(r), "GetShowCmd failed (0x%08x)\n", r);
462         lok_todo_4(0x10, i==desc->showcmd,
463            "GetShowCmd returned 0x%0x instead of 0x%0x\n",
464            i, desc->showcmd);
465     }
466     if (desc->icon)
467     {
468         int i=0xdeadbeef;
469         strcpy(buffer,"garbage");
470         r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
471         lok(SUCCEEDED(r), "GetIconLocation failed (0x%08x)\n", r);
472         lok_todo_4(0x20, lstrcmpi(buffer, desc->icon)==0,
473            "GetIconLocation returned '%s' instead of '%s'\n",
474            buffer, desc->icon);
475         lok_todo_4(0x20, i==desc->icon_id,
476            "GetIconLocation returned 0x%0x instead of 0x%0x\n",
477            i, desc->icon_id);
478     }
479     if (desc->hotkey)
480     {
481         WORD i=0xbeef;
482         r = IShellLinkA_GetHotkey(sl, &i);
483         lok(SUCCEEDED(r), "GetHotkey failed (0x%08x)\n", r);
484         lok_todo_4(0x40, i==desc->hotkey,
485            "GetHotkey returned 0x%04x instead of 0x%04x\n",
486            i, desc->hotkey);
487     }
488
489     IShellLinkA_Release(sl);
490 }
491
492 static void test_load_save(void)
493 {
494     lnk_desc_t desc;
495     char mypath[MAX_PATH];
496     char mydir[MAX_PATH];
497     char realpath[MAX_PATH];
498     char* p;
499     HANDLE hf;
500     DWORD r;
501
502     /* Save an empty .lnk file */
503     memset(&desc, 0, sizeof(desc));
504     create_lnk(lnkfile, &desc, 0);
505
506     /* It should come back as a bunch of empty strings */
507     desc.description="";
508     desc.workdir="";
509     desc.path="";
510     desc.arguments="";
511     desc.icon="";
512     check_lnk(lnkfile, &desc, 0x0);
513
514     /* Point a .lnk file to nonexistent files */
515     desc.description="";
516     desc.workdir="c:\\Nonexitent\\work\\directory";
517     desc.path="c:\\nonexistent\\path";
518     desc.pidl=NULL;
519     desc.arguments="";
520     desc.showcmd=0;
521     desc.icon="c:\\nonexistent\\icon\\file";
522     desc.icon_id=1234;
523     desc.hotkey=0;
524     create_lnk(lnkfile, &desc, 0);
525     check_lnk(lnkfile, &desc, 0x0);
526
527     r=GetModuleFileName(NULL, mypath, sizeof(mypath));
528     ok(r>=0 && r<sizeof(mypath), "GetModuleFileName failed (%d)\n", r);
529     strcpy(mydir, mypath);
530     p=strrchr(mydir, '\\');
531     if (p)
532         *p='\0';
533
534     /* Overwrite the existing lnk file and point it to existing files */
535     desc.description="test 2";
536     desc.workdir=mydir;
537     desc.path=mypath;
538     desc.pidl=NULL;
539     desc.arguments="/option1 /option2 \"Some string\"";
540     desc.showcmd=SW_SHOWNORMAL;
541     desc.icon=mypath;
542     desc.icon_id=0;
543     desc.hotkey=0x1234;
544     create_lnk(lnkfile, &desc, 0);
545     check_lnk(lnkfile, &desc, 0x0);
546
547     /* Overwrite the existing lnk file and test link to a command on the path */
548     desc.description="command on path";
549     desc.workdir=mypath;
550     desc.path="rundll32.exe";
551     desc.pidl=NULL;
552     desc.arguments="/option1 /option2 \"Some string\"";
553     desc.showcmd=SW_SHOWNORMAL;
554     desc.icon=mypath;
555     desc.icon_id=0;
556     desc.hotkey=0x1234;
557     create_lnk(lnkfile, &desc, 0);
558     /* Check that link is created to proper location */
559     SearchPathA( NULL, desc.path, NULL, MAX_PATH, realpath, NULL);
560     desc.path=realpath;
561     check_lnk(lnkfile, &desc, 0x0);
562
563     /* Create a temporary non-executable file */
564     r=GetTempPath(sizeof(mypath), mypath);
565     ok(r>=0 && r<sizeof(mypath), "GetTempPath failed (%d), err %d\n", r, GetLastError());
566     r=GetLongPathName(mypath, mydir, sizeof(mydir));
567     ok(r>=0 && r<sizeof(mydir), "GetLongPathName failed (%d), err %d\n", r, GetLastError());
568     p=strrchr(mydir, '\\');
569     if (p)
570         *p='\0';
571
572     strcpy(mypath, mydir);
573     strcat(mypath, "\\test.txt");
574     hf = CreateFile(mypath, GENERIC_WRITE, 0, NULL,
575                     CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
576     CloseHandle(hf);
577
578     /* Overwrite the existing lnk file and test link to an existing non-executable file */
579     desc.description="non-executable file";
580     desc.workdir=mydir;
581     desc.path=mypath;
582     desc.pidl=NULL;
583     desc.arguments="";
584     desc.showcmd=SW_SHOWNORMAL;
585     desc.icon=mypath;
586     desc.icon_id=0;
587     desc.hotkey=0x1234;
588     create_lnk(lnkfile, &desc, 0);
589     check_lnk(lnkfile, &desc, 0x0);
590
591     r = DeleteFileA(mypath);
592     ok(r, "failed to delete file %s (%d)\n", mypath, GetLastError());
593
594     /* FIXME: Also test saving a .lnk pointing to a pidl that cannot be
595      * represented as a path.
596      */
597
598     /* DeleteFileW is not implemented on Win9x */
599     r=DeleteFileA("c:\\test.lnk");
600     ok(r, "failed to delete link (%d)\n", GetLastError());
601 }
602
603 static void test_datalink(void)
604 {
605     static const WCHAR lnk[] = {
606       ':',':','{','9','d','b','1','1','8','6','f','-','4','0','d','f','-','1',
607       '1','d','1','-','a','a','8','c','-','0','0','c','0','4','f','b','6','7',
608       '8','6','3','}',':','{','0','0','0','1','0','4','0','9','-','7','8','E',
609       '1','-','1','1','D','2','-','B','6','0','F','-','0','0','6','0','9','7',
610       'C','9','9','8','E','7','}',':',':','{','9','d','b','1','1','8','6','e',
611       '-','4','0','d','f','-','1','1','d','1','-','a','a','8','c','-','0','0',
612       'c','0','4','f','b','6','7','8','6','3','}',':','2','6',',','!','!','g',
613       'x','s','f','(','N','g',']','q','F','`','H','{','L','s','A','C','C','E',
614       'S','S','F','i','l','e','s','>','p','l','T',']','j','I','{','j','f','(',
615       '=','1','&','L','[','-','8','1','-',']',':',':',0 };
616     static const WCHAR comp[] = {
617       '2','6',',','!','!','g','x','s','f','(','N','g',']','q','F','`','H','{',
618       'L','s','A','C','C','E','S','S','F','i','l','e','s','>','p','l','T',']',
619       'j','I','{','j','f','(','=','1','&','L','[','-','8','1','-',']',0 };
620     IShellLinkDataList *dl = NULL;
621     IShellLinkW *sl = NULL;
622     HRESULT r;
623     DWORD flags = 0;
624     EXP_DARWIN_LINK *dar;
625
626     r = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
627                             &IID_IShellLinkW, (LPVOID*)&sl );
628     ok( r == S_OK || r == E_NOINTERFACE, "CoCreateInstance failed (0x%08x)\n", r);
629     if (!sl)
630     {
631         skip("no shelllink\n");
632         return;
633     }
634
635     r = IShellLinkW_QueryInterface( sl, &_IID_IShellLinkDataList, (LPVOID*) &dl );
636     ok(r == S_OK, "IShellLinkW_QueryInterface failed (0x%08x)\n", r);
637
638     if (!dl)
639     {
640         skip("no datalink interface\n");
641         return;
642     }
643
644     flags = 0;
645     r = dl->lpVtbl->GetFlags( dl, &flags );
646     ok( r == S_OK, "GetFlags failed\n");
647     ok( flags == 0, "GetFlags returned wrong flags\n");
648
649     dar = (void*)-1;
650     r = dl->lpVtbl->CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
651     ok( r == E_FAIL, "CopyDataBlock failed\n");
652     ok( dar == NULL, "should be null\n");
653
654     r = IShellLinkW_SetPath(sl, lnk);
655     ok(r == S_OK, "set path failed\n");
656
657     /*
658      * The following crashes:
659      * r = dl->lpVtbl->GetFlags( dl, NULL );
660      */
661
662     flags = 0;
663     r = dl->lpVtbl->GetFlags( dl, &flags );
664     ok( r == S_OK, "GetFlags failed\n");
665     ok( flags == (SLDF_HAS_DARWINID|SLDF_HAS_LOGO3ID),
666         "GetFlags returned wrong flags\n");
667
668     dar = NULL;
669     r = dl->lpVtbl->CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
670     ok( r == S_OK, "CopyDataBlock failed\n");
671
672     ok( dar && ((DATABLOCK_HEADER*)dar)->dwSignature == EXP_DARWIN_ID_SIG, "signature wrong\n");
673     ok( dar && 0==lstrcmpW(dar->szwDarwinID, comp ), "signature wrong\n");
674
675     LocalFree( dar );
676
677     IUnknown_Release( dl );
678     IShellLinkW_Release( sl );
679 }
680
681 START_TEST(shelllink)
682 {
683     HRESULT r;
684     HMODULE hmod;
685
686     hmod = GetModuleHandle("shell32");
687     pILFree = (fnILFree) GetProcAddress(hmod, (LPSTR)155);
688     pILIsEqual = (fnILIsEqual) GetProcAddress(hmod, (LPSTR)21);
689     pSHILCreateFromPath = (fnSHILCreateFromPath) GetProcAddress(hmod, (LPSTR)28);
690
691     r = CoInitialize(NULL);
692     ok(SUCCEEDED(r), "CoInitialize failed (0x%08x)\n", r);
693     if (!SUCCEEDED(r))
694         return;
695
696     test_get_set();
697     test_load_save();
698     test_datalink();
699
700     CoUninitialize();
701 }