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