msi: Set all folders' source paths to the root directory if the source type is compre...
[wine] / dlls / shell32 / tests / shlexec.c
1 /*
2  * Unit test of the ShellExecute function.
3  *
4  * Copyright 2005 Francois Gouget 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 /* TODO:
22  * - test the default verb selection
23  * - test selection of an alternate class
24  * - try running executables in more ways
25  * - try passing arguments to executables
26  * - ShellExecute("foo.shlexec") with no path should work if foo.shlexec is
27  *   in the PATH
28  * - test associations that use %l, %L or "%1" instead of %1
29  * - we may want to test ShellExecuteEx() instead of ShellExecute()
30  *   and then we could also check its return value
31  * - ShellExecuteEx() also calls SetLastError() with meaningful values which
32  *   we could check
33  */
34
35 #include <stdio.h>
36 #include <assert.h>
37
38 /* Needed to get SEE_MASK_NOZONECHECKS with the PSDK */
39 #define NTDDI_WINXPSP1 0x05010100
40 #define NTDDI_VERSION NTDDI_WINXPSP1
41 #define _WIN32_WINNT 0x0501
42
43 #include "wtypes.h"
44 #include "winbase.h"
45 #include "windef.h"
46 #include "shellapi.h"
47 #include "shlwapi.h"
48 #include "wine/test.h"
49
50 #include "shell32_test.h"
51
52
53 static char argv0[MAX_PATH];
54 static int myARGC;
55 static char** myARGV;
56 static char tmpdir[MAX_PATH];
57 static char child_file[MAX_PATH];
58 static DLLVERSIONINFO dllver;
59
60
61 /***
62  *
63  * ShellExecute wrappers
64  *
65  ***/
66 static void dump_child(void);
67
68 static HANDLE hEvent;
69 static void init_event(const char* child_file)
70 {
71     char* event_name;
72     event_name=strrchr(child_file, '\\')+1;
73     hEvent=CreateEvent(NULL, FALSE, FALSE, event_name);
74 }
75
76 static void strcat_param(char* str, const char* param)
77 {
78     if (param!=NULL)
79     {
80         strcat(str, "\"");
81         strcat(str, param);
82         strcat(str, "\"");
83     }
84     else
85     {
86         strcat(str, "null");
87     }
88 }
89
90 static char shell_call[2048]="";
91 static int shell_execute(LPCSTR operation, LPCSTR file, LPCSTR parameters, LPCSTR directory)
92 {
93     int rc;
94
95     strcpy(shell_call, "ShellExecute(");
96     strcat_param(shell_call, operation);
97     strcat(shell_call, ", ");
98     strcat_param(shell_call, file);
99     strcat(shell_call, ", ");
100     strcat_param(shell_call, parameters);
101     strcat(shell_call, ", ");
102     strcat_param(shell_call, directory);
103     strcat(shell_call, ")");
104     if (winetest_debug > 1)
105         trace("%s\n", shell_call);
106
107     DeleteFile(child_file);
108     SetLastError(0xcafebabe);
109
110     /* FIXME: We cannot use ShellExecuteEx() here because if there is no
111      * association it displays the 'Open With' dialog and I could not find
112      * a flag to prevent this.
113      */
114     rc=(int)ShellExecute(NULL, operation, file, parameters, directory,
115                          SW_SHOWNORMAL);
116
117     if (rc > 32)
118     {
119         int wait_rc;
120         wait_rc=WaitForSingleObject(hEvent, 5000);
121         ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
122     }
123     /* The child process may have changed the result file, so let profile
124      * functions know about it
125      */
126     WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
127     if (rc > 32)
128         dump_child();
129
130     return rc;
131 }
132
133 static int shell_execute_ex(DWORD mask, LPCSTR operation, LPCSTR file,
134                             LPCSTR parameters, LPCSTR directory)
135 {
136     SHELLEXECUTEINFO sei;
137     BOOL success;
138     int rc;
139
140     strcpy(shell_call, "ShellExecuteEx(");
141     strcat_param(shell_call, operation);
142     strcat(shell_call, ", ");
143     strcat_param(shell_call, file);
144     strcat(shell_call, ", ");
145     strcat_param(shell_call, parameters);
146     strcat(shell_call, ", ");
147     strcat_param(shell_call, directory);
148     strcat(shell_call, ")");
149     if (winetest_debug > 1)
150         trace("%s\n", shell_call);
151
152     sei.cbSize=sizeof(sei);
153     sei.fMask=SEE_MASK_NOCLOSEPROCESS | mask;
154     sei.hwnd=NULL;
155     sei.lpVerb=operation;
156     sei.lpFile=file;
157     sei.lpParameters=parameters;
158     sei.lpDirectory=directory;
159     sei.nShow=SW_SHOWNORMAL;
160     sei.hInstApp=NULL; /* Out */
161     sei.lpIDList=NULL;
162     sei.lpClass=NULL;
163     sei.hkeyClass=NULL;
164     sei.dwHotKey=0;
165     U(sei).hIcon=NULL;
166     sei.hProcess=NULL; /* Out */
167
168     DeleteFile(child_file);
169     SetLastError(0xcafebabe);
170     success=ShellExecuteEx(&sei);
171     rc=(int)sei.hInstApp;
172     ok((success && rc > 32) || (!success && rc <= 32),
173        "%s rc=%d and hInstApp=%d is not allowed\n", shell_call, success, rc);
174
175     if (rc > 32)
176     {
177         int wait_rc;
178         if (sei.hProcess!=NULL)
179         {
180             wait_rc=WaitForSingleObject(sei.hProcess, 5000);
181             ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject(hProcess) returned %d\n", wait_rc);
182         }
183         wait_rc=WaitForSingleObject(hEvent, 5000);
184         ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
185     }
186     /* The child process may have changed the result file, so let profile
187      * functions know about it
188      */
189     WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
190     if (rc > 32)
191         dump_child();
192
193     return rc;
194 }
195
196
197
198 /***
199  *
200  * Functions to create / delete associations wrappers
201  *
202  ***/
203
204 static void create_test_association(const char* extension)
205 {
206     HKEY hkey, hkey_shell;
207     char class[MAX_PATH];
208     LONG rc;
209
210     sprintf(class, "shlexec%s", extension);
211     rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, extension, 0, NULL, 0, KEY_SET_VALUE,
212                       NULL, &hkey, NULL);
213     assert(rc==ERROR_SUCCESS);
214     rc=RegSetValueEx(hkey, NULL, 0, REG_SZ, (LPBYTE) class, strlen(class)+1);
215     assert(rc==ERROR_SUCCESS);
216     CloseHandle(hkey);
217
218     rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, class, 0, NULL, 0,
219                       KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS, NULL, &hkey, NULL);
220     assert(rc==ERROR_SUCCESS);
221     rc=RegCreateKeyEx(hkey, "shell", 0, NULL, 0,
222                       KEY_CREATE_SUB_KEY, NULL, &hkey_shell, NULL);
223     assert(rc==ERROR_SUCCESS);
224     CloseHandle(hkey);
225     CloseHandle(hkey_shell);
226 }
227
228 /* Based on RegDeleteTreeW from dlls/advapi32/registry.c */
229 static LSTATUS myRegDeleteTreeA(HKEY hKey, LPCSTR lpszSubKey)
230 {
231     LONG ret;
232     DWORD dwMaxSubkeyLen, dwMaxValueLen;
233     DWORD dwMaxLen, dwSize;
234     CHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
235     HKEY hSubKey = hKey;
236
237     if(lpszSubKey)
238     {
239         ret = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
240         if (ret) return ret;
241     }
242
243     /* Get highest length for keys, values */
244     ret = RegQueryInfoKeyA(hSubKey, NULL, NULL, NULL, NULL,
245             &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
246     if (ret) goto cleanup;
247
248     dwMaxSubkeyLen++;
249     dwMaxValueLen++;
250     dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
251     if (dwMaxLen > sizeof(szNameBuf)/sizeof(CHAR))
252     {
253         /* Name too big: alloc a buffer for it */
254         if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(CHAR))))
255         {
256             ret = ERROR_NOT_ENOUGH_MEMORY;
257             goto cleanup;
258         }
259     }
260
261
262     /* Recursively delete all the subkeys */
263     while (TRUE)
264     {
265         dwSize = dwMaxLen;
266         if (RegEnumKeyExA(hSubKey, 0, lpszName, &dwSize, NULL,
267                           NULL, NULL, NULL)) break;
268
269         ret = myRegDeleteTreeA(hSubKey, lpszName);
270         if (ret) goto cleanup;
271     }
272
273     if (lpszSubKey)
274         ret = RegDeleteKeyA(hKey, lpszSubKey);
275     else
276         while (TRUE)
277         {
278             dwSize = dwMaxLen;
279             if (RegEnumValueA(hKey, 0, lpszName, &dwSize,
280                   NULL, NULL, NULL, NULL)) break;
281
282             ret = RegDeleteValueA(hKey, lpszName);
283             if (ret) goto cleanup;
284         }
285
286 cleanup:
287     /* Free buffer if allocated */
288     if (lpszName != szNameBuf)
289         HeapFree( GetProcessHeap(), 0, lpszName);
290     if(lpszSubKey)
291         RegCloseKey(hSubKey);
292     return ret;
293 }
294
295 static void delete_test_association(const char* extension)
296 {
297     char class[MAX_PATH];
298
299     sprintf(class, "shlexec%s", extension);
300     myRegDeleteTreeA(HKEY_CLASSES_ROOT, class);
301     myRegDeleteTreeA(HKEY_CLASSES_ROOT, extension);
302 }
303
304 static void create_test_verb_dde(const char* extension, const char* verb,
305                                  int rawcmd, const char* cmdtail, const char *ddeexec,
306                                  const char *application, const char *topic,
307                                  const char *ifexec)
308 {
309     HKEY hkey_shell, hkey_verb, hkey_cmd;
310     char shell[MAX_PATH];
311     char* cmd;
312     LONG rc;
313
314     sprintf(shell, "shlexec%s\\shell", extension);
315     rc=RegOpenKeyEx(HKEY_CLASSES_ROOT, shell, 0,
316                     KEY_CREATE_SUB_KEY, &hkey_shell);
317     assert(rc==ERROR_SUCCESS);
318     rc=RegCreateKeyEx(hkey_shell, verb, 0, NULL, 0, KEY_CREATE_SUB_KEY,
319                       NULL, &hkey_verb, NULL);
320     assert(rc==ERROR_SUCCESS);
321     rc=RegCreateKeyEx(hkey_verb, "command", 0, NULL, 0, KEY_SET_VALUE,
322                       NULL, &hkey_cmd, NULL);
323     assert(rc==ERROR_SUCCESS);
324
325     if (rawcmd)
326     {
327         rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmdtail, strlen(cmdtail)+1);
328     }
329     else
330     {
331         cmd=malloc(strlen(argv0)+10+strlen(child_file)+2+strlen(cmdtail)+1);
332         sprintf(cmd,"%s shlexec \"%s\" %s", argv0, child_file, cmdtail);
333         rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmd, strlen(cmd)+1);
334         assert(rc==ERROR_SUCCESS);
335         free(cmd);
336     }
337
338     if (ddeexec)
339     {
340         HKEY hkey_ddeexec, hkey_application, hkey_topic, hkey_ifexec;
341
342         rc=RegCreateKeyEx(hkey_verb, "ddeexec", 0, NULL, 0, KEY_SET_VALUE |
343                           KEY_CREATE_SUB_KEY, NULL, &hkey_ddeexec, NULL);
344         assert(rc==ERROR_SUCCESS);
345         rc=RegSetValueEx(hkey_ddeexec, NULL, 0, REG_SZ, (LPBYTE)ddeexec,
346                          strlen(ddeexec)+1);
347         assert(rc==ERROR_SUCCESS);
348         if (application)
349         {
350             rc=RegCreateKeyEx(hkey_ddeexec, "application", 0, NULL, 0, KEY_SET_VALUE,
351                               NULL, &hkey_application, NULL);
352             assert(rc==ERROR_SUCCESS);
353             rc=RegSetValueEx(hkey_application, NULL, 0, REG_SZ, (LPBYTE)application,
354                              strlen(application)+1);
355             assert(rc==ERROR_SUCCESS);
356             CloseHandle(hkey_application);
357         }
358         if (topic)
359         {
360             rc=RegCreateKeyEx(hkey_ddeexec, "topic", 0, NULL, 0, KEY_SET_VALUE,
361                               NULL, &hkey_topic, NULL);
362             assert(rc==ERROR_SUCCESS);
363             rc=RegSetValueEx(hkey_topic, NULL, 0, REG_SZ, (LPBYTE)topic,
364                              strlen(topic)+1);
365             assert(rc==ERROR_SUCCESS);
366             CloseHandle(hkey_topic);
367         }
368         if (ifexec)
369         {
370             rc=RegCreateKeyEx(hkey_ddeexec, "ifexec", 0, NULL, 0, KEY_SET_VALUE,
371                               NULL, &hkey_ifexec, NULL);
372             assert(rc==ERROR_SUCCESS);
373             rc=RegSetValueEx(hkey_ifexec, NULL, 0, REG_SZ, (LPBYTE)ifexec,
374                              strlen(ifexec)+1);
375             assert(rc==ERROR_SUCCESS);
376             CloseHandle(hkey_ifexec);
377         }
378         CloseHandle(hkey_ddeexec);
379     }
380
381     CloseHandle(hkey_shell);
382     CloseHandle(hkey_verb);
383     CloseHandle(hkey_cmd);
384 }
385
386 static void create_test_verb(const char* extension, const char* verb,
387                              int rawcmd, const char* cmdtail)
388 {
389     create_test_verb_dde(extension, verb, rawcmd, cmdtail, NULL, NULL,
390                          NULL, NULL);
391 }
392
393 /***
394  *
395  * Functions to check that the child process was started just right
396  * (borrowed from dlls/kernel32/tests/process.c)
397  *
398  ***/
399
400 static const char* encodeA(const char* str)
401 {
402     static char encoded[2*1024+1];
403     char*       ptr;
404     size_t      len,i;
405
406     if (!str) return "";
407     len = strlen(str) + 1;
408     if (len >= sizeof(encoded)/2)
409     {
410         fprintf(stderr, "string is too long!\n");
411         assert(0);
412     }
413     ptr = encoded;
414     for (i = 0; i < len; i++)
415         sprintf(&ptr[i * 2], "%02x", (unsigned char)str[i]);
416     ptr[2 * len] = '\0';
417     return ptr;
418 }
419
420 static unsigned decode_char(char c)
421 {
422     if (c >= '0' && c <= '9') return c - '0';
423     if (c >= 'a' && c <= 'f') return c - 'a' + 10;
424     assert(c >= 'A' && c <= 'F');
425     return c - 'A' + 10;
426 }
427
428 static char* decodeA(const char* str)
429 {
430     static char decoded[1024];
431     char*       ptr;
432     size_t      len,i;
433
434     len = strlen(str) / 2;
435     if (!len--) return NULL;
436     if (len >= sizeof(decoded))
437     {
438         fprintf(stderr, "string is too long!\n");
439         assert(0);
440     }
441     ptr = decoded;
442     for (i = 0; i < len; i++)
443         ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
444     ptr[len] = '\0';
445     return ptr;
446 }
447
448 static void     childPrintf(HANDLE h, const char* fmt, ...)
449 {
450     va_list     valist;
451     char        buffer[1024];
452     DWORD       w;
453
454     va_start(valist, fmt);
455     vsprintf(buffer, fmt, valist);
456     va_end(valist);
457     WriteFile(h, buffer, strlen(buffer), &w, NULL);
458 }
459
460 static void doChild(int argc, char** argv)
461 {
462     char* filename;
463     HANDLE hFile;
464     int i;
465
466     filename=argv[2];
467     hFile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
468     if (hFile == INVALID_HANDLE_VALUE)
469         return;
470
471     /* Arguments */
472     childPrintf(hFile, "[Arguments]\r\n");
473     if (winetest_debug > 2)
474         trace("argcA=%d\n", argc);
475     childPrintf(hFile, "argcA=%d\r\n", argc);
476     for (i = 0; i < argc; i++)
477     {
478         if (winetest_debug > 2)
479             trace("argvA%d=%s\n", i, argv[i]);
480         childPrintf(hFile, "argvA%d=%s\r\n", i, encodeA(argv[i]));
481     }
482     CloseHandle(hFile);
483
484     init_event(filename);
485     SetEvent(hEvent);
486     CloseHandle(hEvent);
487 }
488
489 static char* getChildString(const char* sect, const char* key)
490 {
491     char        buf[1024];
492     char*       ret;
493
494     GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), child_file);
495     if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
496     assert(!(strlen(buf) & 1));
497     ret = decodeA(buf);
498     return ret;
499 }
500
501 static void dump_child(void)
502 {
503     if (winetest_debug > 1)
504     {
505         char key[18];
506         char* str;
507         int i, c;
508
509         c=GetPrivateProfileIntA("Arguments", "argcA", -1, child_file);
510         trace("argcA=%d\n",c);
511         for (i=0;i<c;i++)
512         {
513             sprintf(key, "argvA%d", i);
514             str=getChildString("Arguments", key);
515             trace("%s=%s\n", key, str);
516         }
517     }
518 }
519
520 static int StrCmpPath(const char* s1, const char* s2)
521 {
522     if (!s1 && !s2) return 0;
523     if (!s2) return 1;
524     if (!s1) return -1;
525     while (*s1)
526     {
527         if (!*s2)
528         {
529             if (*s1=='.')
530                 s1++;
531             return (*s1-*s2);
532         }
533         if ((*s1=='/' || *s1=='\\') && (*s2=='/' || *s2=='\\'))
534         {
535             while (*s1=='/' || *s1=='\\')
536                 s1++;
537             while (*s2=='/' || *s2=='\\')
538                 s2++;
539         }
540         else if (toupper(*s1)==toupper(*s2))
541         {
542             s1++;
543             s2++;
544         }
545         else
546         {
547             return (*s1-*s2);
548         }
549     }
550     if (*s2=='.')
551         s2++;
552     if (*s2)
553         return -1;
554     return 0;
555 }
556
557 static int _okChildString(const char* file, int line, const char* key, const char* expected)
558 {
559     char* result;
560     result=getChildString("Arguments", key);
561     return ok_(file, line)(lstrcmpiA(result, expected) == 0,
562                "%s expected '%s', got '%s'\n", key, expected, result);
563 }
564
565 static int _okChildPath(const char* file, int line, const char* key, const char* expected)
566 {
567     char* result;
568     result=getChildString("Arguments", key);
569     return ok_(file, line)(StrCmpPath(result, expected) == 0,
570                "%s expected '%s', got '%s'\n", key, expected, result);
571 }
572
573 static int _okChildInt(const char* file, int line, const char* key, int expected)
574 {
575     INT result;
576     result=GetPrivateProfileIntA("Arguments", key, expected, child_file);
577     return ok_(file, line)(result == expected,
578                "%s expected %d, but got %d\n", key, expected, result);
579 }
580
581 #define okChildString(key, expected) _okChildString(__FILE__, __LINE__, (key), (expected))
582 #define okChildPath(key, expected) _okChildPath(__FILE__, __LINE__, (key), (expected))
583 #define okChildInt(key, expected)    _okChildInt(__FILE__, __LINE__, (key), (expected))
584
585
586
587 /***
588  *
589  * Tests
590  *
591  ***/
592
593 static const char* testfiles[]=
594 {
595     "%s\\test file.shlexec",
596     "%s\\%%nasty%% $file.shlexec",
597     "%s\\test file.noassoc",
598     "%s\\test file.noassoc.shlexec",
599     "%s\\test file.shlexec.noassoc",
600     "%s\\test_shortcut_shlexec.lnk",
601     "%s\\test_shortcut_exe.lnk",
602     "%s\\test file.shl",
603     "%s\\test file.shlfoo",
604     "%s\\test file.sfe",
605     "%s\\masked file.shlexec",
606     "%s\\masked",
607     "%s\\test file.sde",
608     "%s\\test file.exe",
609     "%s\\test2.exe",
610     NULL
611 };
612
613 typedef struct
614 {
615     const char* verb;
616     const char* basename;
617     int todo;
618     int rc;
619 } filename_tests_t;
620
621 static filename_tests_t filename_tests[]=
622 {
623     /* Test bad / nonexistent filenames */
624     {NULL,           "%s\\nonexistent.shlexec", 0x11, SE_ERR_FNF},
625     {NULL,           "%s\\nonexistent.noassoc", 0x11, SE_ERR_FNF},
626
627     /* Standard tests */
628     {NULL,           "%s\\test file.shlexec",   0x0, 33},
629     {NULL,           "%s\\test file.shlexec.",  0x0, 33},
630     {NULL,           "%s\\%%nasty%% $file.shlexec", 0x0, 33},
631     {NULL,           "%s/test file.shlexec",    0x0, 33},
632
633     /* Test filenames with no association */
634     {NULL,           "%s\\test file.noassoc",   0x0,  SE_ERR_NOASSOC},
635
636     /* Test double extensions */
637     {NULL,           "%s\\test file.noassoc.shlexec", 0x0, 33},
638     {NULL,           "%s\\test file.shlexec.noassoc", 0x0, SE_ERR_NOASSOC},
639
640     /* Test alternate verbs */
641     {"LowerL",       "%s\\nonexistent.shlexec", 0x11, SE_ERR_FNF},
642     {"LowerL",       "%s\\test file.noassoc",   0x0,  SE_ERR_NOASSOC},
643
644     {"QuotedLowerL", "%s\\test file.shlexec",   0x0, 33},
645     {"QuotedUpperL", "%s\\test file.shlexec",   0x0, 33},
646
647     /* Test file masked due to space */
648     {NULL,           "%s\\masked file.shlexec",   0x1, 33},
649     /* Test if quoting prevents the masking */
650     {NULL,           "%s\\masked file.shlexec",   0x40, 33},
651
652     {NULL, NULL, 0}
653 };
654
655 static filename_tests_t noquotes_tests[]=
656 {
657     /* Test unquoted '%1' thingies */
658     {"NoQuotes",     "%s\\test file.shlexec",   0xa, 33},
659     {"LowerL",       "%s\\test file.shlexec",   0xa, 33},
660     {"UpperL",       "%s\\test file.shlexec",   0xa, 33},
661
662     {NULL, NULL, 0}
663 };
664
665 static void test_filename(void)
666 {
667     char filename[MAX_PATH];
668     const filename_tests_t* test;
669     char* c;
670     int rc;
671
672     test=filename_tests;
673     while (test->basename)
674     {
675         sprintf(filename, test->basename, tmpdir);
676         if (strchr(filename, '/'))
677         {
678             c=filename;
679             while (*c)
680             {
681                 if (*c=='\\')
682                     *c='/';
683                 c++;
684             }
685         }
686         if ((test->todo & 0x40)==0)
687         {
688             rc=shell_execute(test->verb, filename, NULL, NULL);
689         }
690         else
691         {
692             char quoted[MAX_PATH + 2];
693             sprintf(quoted, "\"%s\"", filename);
694             rc=shell_execute(test->verb, quoted, NULL, NULL);
695         }
696         if (rc > 32)
697             rc=33;
698         if ((test->todo & 0x1)==0)
699         {
700             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
701                rc, GetLastError());
702         }
703         else todo_wine
704         {
705             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
706                rc, GetLastError());
707         }
708         if (rc == 33)
709         {
710             const char* verb;
711             if ((test->todo & 0x2)==0)
712             {
713                 okChildInt("argcA", 5);
714             }
715             else todo_wine
716             {
717                 okChildInt("argcA", 5);
718             }
719             verb=(test->verb ? test->verb : "Open");
720             if ((test->todo & 0x4)==0)
721             {
722                 okChildString("argvA3", verb);
723             }
724             else todo_wine
725             {
726                 okChildString("argvA3", verb);
727             }
728             if ((test->todo & 0x8)==0)
729             {
730                 okChildPath("argvA4", filename);
731             }
732             else todo_wine
733             {
734                 okChildPath("argvA4", filename);
735             }
736         }
737         test++;
738     }
739
740     test=noquotes_tests;
741     while (test->basename)
742     {
743         sprintf(filename, test->basename, tmpdir);
744         rc=shell_execute(test->verb, filename, NULL, NULL);
745         if (rc > 32)
746             rc=33;
747         if ((test->todo & 0x1)==0)
748         {
749             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
750                rc, GetLastError());
751         }
752         else todo_wine
753         {
754             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
755                rc, GetLastError());
756         }
757         if (rc==0)
758         {
759             int count;
760             const char* verb;
761             char* str;
762
763             verb=(test->verb ? test->verb : "Open");
764             if ((test->todo & 0x4)==0)
765             {
766                 okChildString("argvA3", verb);
767             }
768             else todo_wine
769             {
770                 okChildString("argvA3", verb);
771             }
772
773             count=4;
774             str=filename;
775             while (1)
776             {
777                 char attrib[18];
778                 char* space;
779                 space=strchr(str, ' ');
780                 if (space)
781                     *space='\0';
782                 sprintf(attrib, "argvA%d", count);
783                 if ((test->todo & 0x8)==0)
784                 {
785                     okChildPath(attrib, str);
786                 }
787                 else todo_wine
788                 {
789                     okChildPath(attrib, str);
790                 }
791                 count++;
792                 if (!space)
793                     break;
794                 str=space+1;
795             }
796             if ((test->todo & 0x2)==0)
797             {
798                 okChildInt("argcA", count);
799             }
800             else todo_wine
801             {
802                 okChildInt("argcA", count);
803             }
804         }
805         test++;
806     }
807
808     if (dllver.dwMajorVersion != 0)
809     {
810         /* The more recent versions of shell32.dll accept quoted filenames
811          * while older ones (e.g. 4.00) don't. Still we want to test this
812          * because IE 6 depends on the new behavior.
813          * One day we may need to check the exact version of the dll but for
814          * now making sure DllGetVersion() is present is sufficient.
815          */
816         sprintf(filename, "\"%s\\test file.shlexec\"", tmpdir);
817         rc=shell_execute(NULL, filename, NULL, NULL);
818         ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
819            GetLastError());
820         okChildInt("argcA", 5);
821         okChildString("argvA3", "Open");
822         sprintf(filename, "%s\\test file.shlexec", tmpdir);
823         okChildPath("argvA4", filename);
824     }
825 }
826
827 static void test_find_executable(void)
828 {
829     char filename[MAX_PATH];
830     char command[MAX_PATH];
831     const filename_tests_t* test;
832     int rc;
833
834     create_test_association(".sfe");
835     create_test_verb(".sfe", "Open", 1, "%1");
836
837     /* Don't test FindExecutable(..., NULL), it always crashes */
838
839     strcpy(command, "your word");
840     rc=(int)FindExecutableA(NULL, NULL, command);
841     ok(rc == SE_ERR_FNF || rc > 32 /* nt4 */, "FindExecutable(NULL) returned %d\n", rc);
842     ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
843
844     strcpy(command, "your word");
845     rc=(int)FindExecutableA(tmpdir, NULL, command);
846     ok(rc == SE_ERR_NOASSOC /* >= win2000 */ || rc > 32 /* win98, nt4 */, "FindExecutable(NULL) returned %d\n", rc);
847     ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
848
849     sprintf(filename, "%s\\test file.sfe", tmpdir);
850     rc=(int)FindExecutableA(filename, NULL, command);
851     ok(rc > 32, "FindExecutable(%s) returned %d\n", filename, rc);
852     /* Depending on the platform, command could be '%1' or 'test file.sfe' */
853
854     rc=(int)FindExecutableA("test file.sfe", tmpdir, command);
855     ok(rc > 32, "FindExecutable(%s) returned %d\n", filename, rc);
856
857     rc=(int)FindExecutableA("test file.sfe", NULL, command);
858     todo_wine ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %d\n", filename, rc);
859
860     delete_test_association(".sfe");
861
862     create_test_association(".shl");
863     create_test_verb(".shl", "Open", 0, "Open");
864
865     sprintf(filename, "%s\\test file.shl", tmpdir);
866     rc=(int)FindExecutableA(filename, NULL, command);
867     ok(rc == SE_ERR_FNF /* NT4 */ || rc > 32, "FindExecutable(%s) returned %d\n", filename, rc);
868
869     sprintf(filename, "%s\\test file.shlfoo", tmpdir);
870     rc=(int)FindExecutableA(filename, NULL, command);
871
872     delete_test_association(".shl");
873
874     if (rc > 32)
875     {
876         /* On Windows XP and 2003 FindExecutable() is completely broken.
877          * Probably what it does is convert the filename to 8.3 format,
878          * which as a side effect converts the '.shlfoo' extension to '.shl',
879          * and then tries to find an association for '.shl'. This means it
880          * will normally fail on most extensions with more than 3 characters,
881          * like '.mpeg', etc.
882          * Also it means we cannot do any other test.
883          */
884         trace("FindExecutable() is broken -> skipping 4+ character extension tests\n");
885         return;
886     }
887
888     test=filename_tests;
889     while (test->basename)
890     {
891         sprintf(filename, test->basename, tmpdir);
892         if (strchr(filename, '/'))
893         {
894             char* c;
895             c=filename;
896             while (*c)
897             {
898                 if (*c=='\\')
899                     *c='/';
900                 c++;
901             }
902         }
903         /* Win98 does not '\0'-terminate command! */
904         memset(command, '\0', sizeof(command));
905         rc=(int)FindExecutableA(filename, NULL, command);
906         if (rc > 32)
907             rc=33;
908         if ((test->todo & 0x10)==0)
909         {
910             ok(rc==test->rc, "FindExecutable(%s) failed: rc=%d\n", filename, rc);
911         }
912         else todo_wine
913         {
914             ok(rc==test->rc, "FindExecutable(%s) failed: rc=%d\n", filename, rc);
915         }
916         if (rc > 32)
917         {
918             int equal;
919             equal=strcmp(command, argv0) == 0 ||
920                 /* NT4 returns an extra 0x8 character! */
921                 (strlen(command) == strlen(argv0)+1 && strncmp(command, argv0, strlen(argv0)) == 0);
922             if ((test->todo & 0x20)==0)
923             {
924                 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
925                    filename, command, argv0);
926             }
927             else todo_wine
928             {
929                 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
930                    filename, command, argv0);
931             }
932         }
933         test++;
934     }
935 }
936
937
938 static filename_tests_t lnk_tests[]=
939 {
940     /* Pass bad / nonexistent filenames as a parameter */
941     {NULL, "%s\\nonexistent.shlexec",    0xa, 33},
942     {NULL, "%s\\nonexistent.noassoc",    0xa, 33},
943
944     /* Pass regular paths as a parameter */
945     {NULL, "%s\\test file.shlexec",      0xa, 33},
946     {NULL, "%s/%%nasty%% $file.shlexec", 0xa, 33},
947
948     /* Pass filenames with no association as a parameter */
949     {NULL, "%s\\test file.noassoc",      0xa, 33},
950
951     {NULL, NULL, 0}
952 };
953
954 static void test_lnks(void)
955 {
956     char filename[MAX_PATH];
957     char params[MAX_PATH];
958     const filename_tests_t* test;
959     int rc;
960
961     sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
962     rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
963     ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
964        GetLastError());
965     okChildInt("argcA", 5);
966     okChildString("argvA3", "Open");
967     sprintf(filename, "%s\\test file.shlexec", tmpdir);
968     okChildPath("argvA4", filename);
969
970     sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
971     rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
972     ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
973        GetLastError());
974     okChildInt("argcA", 4);
975     okChildString("argvA3", "Lnk");
976
977     if (dllver.dwMajorVersion>=6)
978     {
979         char* c;
980        /* Recent versions of shell32.dll accept '/'s in shortcut paths.
981          * Older versions don't or are quite buggy in this regard.
982          */
983         sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
984         c=filename;
985         while (*c)
986         {
987             if (*c=='\\')
988                 *c='/';
989             c++;
990         }
991         rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
992         ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
993            GetLastError());
994         okChildInt("argcA", 4);
995         okChildString("argvA3", "Lnk");
996     }
997
998     sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
999     test=lnk_tests;
1000     while (test->basename)
1001     {
1002         params[0]='\"';
1003         sprintf(params+1, test->basename, tmpdir);
1004         strcat(params,"\"");
1005         rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, params,
1006                             NULL);
1007         if (rc > 32)
1008             rc=33;
1009         if ((test->todo & 0x1)==0)
1010         {
1011             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1012                rc, GetLastError());
1013         }
1014         else todo_wine
1015         {
1016             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1017                rc, GetLastError());
1018         }
1019         if (rc==0)
1020         {
1021             if ((test->todo & 0x2)==0)
1022             {
1023                 okChildInt("argcA", 5);
1024             }
1025             else 
1026             {
1027                 okChildInt("argcA", 5);
1028             }
1029             if ((test->todo & 0x4)==0)
1030             {
1031                 okChildString("argvA3", "Lnk");
1032             }
1033             else todo_wine
1034             {
1035                 okChildString("argvA3", "Lnk");
1036             }
1037             sprintf(params, test->basename, tmpdir);
1038             if ((test->todo & 0x8)==0)
1039             {
1040                 okChildPath("argvA4", params);
1041             }
1042             else
1043             {
1044                 okChildPath("argvA4", params);
1045             }
1046         }
1047         test++;
1048     }
1049 }
1050
1051
1052 static void test_exes(void)
1053 {
1054     char filename[MAX_PATH];
1055     char params[1024];
1056     int rc;
1057
1058     sprintf(params, "shlexec \"%s\" Exec", child_file);
1059
1060     /* We need NOZONECHECKS on Win2003 to block a dialog */
1061     rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
1062                         NULL);
1063     ok(rc > 32, "%s returned %d\n", shell_call, rc);
1064     okChildInt("argcA", 4);
1065     okChildString("argvA3", "Exec");
1066
1067     sprintf(filename, "%s\\test file.noassoc", tmpdir);
1068     if (CopyFile(argv0, filename, FALSE))
1069     {
1070         rc=shell_execute(NULL, filename, params, NULL);
1071         todo_wine {
1072         ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
1073         }
1074     }
1075 }
1076
1077 static void test_exes_long(void)
1078 {
1079     char filename[MAX_PATH];
1080     char params[2024];
1081     char longparam[MAX_PATH];
1082     int rc;
1083
1084     for (rc = 0; rc < MAX_PATH; rc++)
1085         longparam[rc]='a'+rc%26;
1086     longparam[MAX_PATH-1]=0;
1087
1088
1089     sprintf(params, "shlexec \"%s\" %s", child_file,longparam);
1090
1091     /* We need NOZONECHECKS on Win2003 to block a dialog */
1092     rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
1093                         NULL);
1094     ok(rc > 32, "%s returned %d\n", shell_call, rc);
1095     okChildInt("argcA", 4);
1096     okChildString("argvA3", longparam);
1097
1098     sprintf(filename, "%s\\test file.noassoc", tmpdir);
1099     if (CopyFile(argv0, filename, FALSE))
1100     {
1101         rc=shell_execute(NULL, filename, params, NULL);
1102         todo_wine {
1103         ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
1104         }
1105     }
1106 }
1107
1108 typedef struct
1109 {
1110     const char* command;
1111     const char* ddeexec;
1112     const char* application;
1113     const char* topic;
1114     const char* ifexec;
1115     int expectedArgs;
1116     const char* expectedDdeExec;
1117     int todo;
1118     int rc;
1119 } dde_tests_t;
1120
1121 static dde_tests_t dde_tests[] =
1122 {
1123     /* Test passing and not passing command-line
1124      * argument, no DDE */
1125     {"", NULL, NULL, NULL, NULL, FALSE, "", 0x0, 33},
1126     {"\"%1\"", NULL, NULL, NULL, NULL, TRUE, "", 0x0, 33},
1127
1128     /* Test passing and not passing command-line
1129      * argument, with DDE */
1130     {"", "[open(\"%1\")]", "shlexec", "dde", NULL, FALSE, "[open(\"%s\")]", 0x0, 33},
1131     {"\"%1\"", "[open(\"%1\")]", "shlexec", "dde", NULL, TRUE, "[open(\"%s\")]", 0x0, 33},
1132
1133     /* Test unquoted %1 in command and ddeexec
1134      * (test filename has space) */
1135     {"%1", "[open(%1)]", "shlexec", "dde", NULL, 2, "[open(%s)]", 0x0, 33},
1136
1137     /* Test ifexec precedence over ddeexec */
1138     {"", "[open(\"%1\")]", "shlexec", "dde", "[ifexec(\"%1\")]", FALSE, "[ifexec(\"%s\")]", 0x0, 33},
1139
1140     /* Test default DDE topic */
1141     {"", "[open(\"%1\")]", "shlexec", NULL, NULL, FALSE, "[open(\"%s\")]", 0x0, 33},
1142
1143     /* Test default DDE application */
1144     {"", "[open(\"%1\")]", NULL, "dde", NULL, FALSE, "[open(\"%s\")]", 0x0, 33},
1145
1146     {NULL, NULL, NULL, NULL, NULL, 0, 0x0, 0}
1147 };
1148
1149 static DWORD ddeInst;
1150 static HSZ hszTopic;
1151 static char ddeExec[MAX_PATH], ddeApplication[MAX_PATH];
1152 static BOOL denyNextConnection;
1153
1154 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
1155                                 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
1156                                 ULONG_PTR dwData1, ULONG_PTR dwData2)
1157 {
1158     DWORD size = 0;
1159
1160     if (winetest_debug > 2)
1161         trace("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
1162               uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
1163
1164     switch (uType)
1165     {
1166         case XTYP_CONNECT:
1167             if (!DdeCmpStringHandles(hsz1, hszTopic))
1168             {
1169                 if (denyNextConnection)
1170                     denyNextConnection = FALSE;
1171                 else
1172                 {
1173                     size = DdeQueryString(ddeInst, hsz2, ddeApplication, MAX_PATH, CP_WINANSI);
1174                     assert(size < MAX_PATH);
1175                     return (HDDEDATA)TRUE;
1176                 }
1177             }
1178             return (HDDEDATA)FALSE;
1179
1180         case XTYP_EXECUTE:
1181             size = DdeGetData(hData, (LPBYTE)ddeExec, MAX_PATH, 0L);
1182             assert(size < MAX_PATH);
1183             DdeFreeDataHandle(hData);
1184             return (HDDEDATA)DDE_FACK;
1185
1186         default:
1187             return NULL;
1188     }
1189 }
1190
1191 typedef struct
1192 {
1193     char *filename;
1194     DWORD threadIdParent;
1195 } dde_thread_info_t;
1196
1197 static DWORD CALLBACK ddeThread(LPVOID arg)
1198 {
1199     dde_thread_info_t *info = (dde_thread_info_t *)arg;
1200     assert(info && info->filename);
1201     PostThreadMessage(info->threadIdParent,
1202                       WM_QUIT,
1203                       shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, info->filename, NULL, NULL),
1204                       0L);
1205     ExitThread(0);
1206 }
1207
1208 /* ShellExecute won't successfully send DDE commands to console applications after starting them,
1209  * so we run a DDE server in this application, deny the first connection request to make
1210  * ShellExecute start the application, and then process the next DDE connection in this application
1211  * to see the execute command that is sent. */
1212 static void test_dde(void)
1213 {
1214     char filename[MAX_PATH], defApplication[MAX_PATH];
1215     HSZ hszApplication;
1216     dde_thread_info_t info = { filename, GetCurrentThreadId() };
1217     const dde_tests_t* test;
1218     char params[1024];
1219     DWORD threadId;
1220     MSG msg;
1221     int rc;
1222
1223     ddeInst = 0;
1224     rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
1225                         CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
1226     assert(rc == DMLERR_NO_ERROR);
1227
1228     sprintf(filename, "%s\\test file.sde", tmpdir);
1229
1230     /* Default service is application name minus path and extension */
1231     strcpy(defApplication, strrchr(argv0, '\\')+1);
1232     *strchr(defApplication, '.') = 0;
1233
1234     test = dde_tests;
1235     while (test->command)
1236     {
1237         create_test_association(".sde");
1238         create_test_verb_dde(".sde", "Open", 0, test->command, test->ddeexec,
1239                              test->application, test->topic, test->ifexec);
1240         hszApplication = DdeCreateStringHandleA(ddeInst, test->application ?
1241                                                 test->application : defApplication, CP_WINANSI);
1242         hszTopic = DdeCreateStringHandleA(ddeInst, test->topic ? test->topic : SZDDESYS_TOPIC,
1243                                           CP_WINANSI);
1244         assert(hszApplication && hszTopic);
1245         assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER));
1246         denyNextConnection = TRUE;
1247         ddeExec[0] = 0;
1248
1249         assert(CreateThread(NULL, 0, ddeThread, (LPVOID)&info, 0, &threadId));
1250         while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);
1251         rc = msg.wParam > 32 ? 33 : msg.wParam;
1252         if ((test->todo & 0x1)==0)
1253         {
1254             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1255                rc, GetLastError());
1256         }
1257         else todo_wine
1258         {
1259             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1260                rc, GetLastError());
1261         }
1262         if (rc == 33)
1263         {
1264             if ((test->todo & 0x2)==0)
1265             {
1266                 okChildInt("argcA", test->expectedArgs + 3);
1267             }
1268             else todo_wine
1269             {
1270                 okChildInt("argcA", test->expectedArgs + 3);
1271             }
1272             if (test->expectedArgs == 1)
1273             {
1274                 if ((test->todo & 0x4) == 0)
1275                 {
1276                     okChildPath("argvA3", filename);
1277                 }
1278                 else todo_wine
1279                 {
1280                     okChildPath("argvA3", filename);
1281                 }
1282             }
1283             if ((test->todo & 0x8) == 0)
1284             {
1285                 sprintf(params, test->expectedDdeExec, filename);
1286                 ok(StrCmpPath(params, ddeExec) == 0,
1287                    "ddeexec expected '%s', got '%s'\n", params, ddeExec);
1288             }
1289             else todo_wine
1290             {
1291                 sprintf(params, test->expectedDdeExec, filename);
1292                 ok(StrCmpPath(params, ddeExec) == 0,
1293                    "ddeexec expected '%s', got '%s'\n", params, ddeExec);
1294             }
1295         }
1296
1297         assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
1298         assert(DdeFreeStringHandle(ddeInst, hszTopic));
1299         assert(DdeFreeStringHandle(ddeInst, hszApplication));
1300         delete_test_association(".sde");
1301         test++;
1302     }
1303
1304     assert(DdeUninitialize(ddeInst));
1305 }
1306
1307 #define DDE_DEFAULT_APP_VARIANTS 2
1308 typedef struct
1309 {
1310     const char* command;
1311     const char* expectedDdeApplication[DDE_DEFAULT_APP_VARIANTS];
1312     int todo;
1313     int rc[DDE_DEFAULT_APP_VARIANTS];
1314 } dde_default_app_tests_t;
1315
1316 static dde_default_app_tests_t dde_default_app_tests[] =
1317 {
1318     /* Windows XP and 98 handle default DDE app names in different ways.
1319      * The application name we see in the first test determines the pattern
1320      * of application names and return codes we will look for. */
1321
1322     /* Test unquoted existing filename with a space */
1323     {"%s\\test file.exe", {"test file", "test"}, 0x0, {33, 33}},
1324     {"%s\\test file.exe param", {"test file", "test"}, 0x0, {33, 33}},
1325
1326     /* Test quoted existing filename with a space */
1327     {"\"%s\\test file.exe\"", {"test file", "test file"}, 0x0, {33, 33}},
1328     {"\"%s\\test file.exe\" param", {"test file", "test file"}, 0x0, {33, 33}},
1329
1330     /* Test unquoted filename with a space that doesn't exist, but
1331      * test2.exe does */
1332     {"%s\\test2 file.exe", {"test2", "test2"}, 0x0, {33, 33}},
1333     {"%s\\test2 file.exe param", {"test2", "test2"}, 0x0, {33, 33}},
1334
1335     /* Test quoted filename with a space that does not exist */
1336     {"\"%s\\test2 file.exe\"", {"", "test2 file"}, 0x0, {5, 33}},
1337     {"\"%s\\test2 file.exe\" param", {"", "test2 file"}, 0x0, {5, 33}},
1338
1339     /* Test filename supplied without the extension */
1340     {"%s\\test2", {"test2", "test2"}, 0x0, {33, 33}},
1341     {"%s\\test2 param", {"test2", "test2"}, 0x0, {33, 33}},
1342
1343     /* Test an unquoted nonexistent filename */
1344     {"%s\\notexist.exe", {"", "notexist"}, 0x0, {5, 33}},
1345     {"%s\\notexist.exe param", {"", "notexist"}, 0x0, {5, 33}},
1346
1347     /* Test an application that will be found on the path */
1348     {"cmd", {"cmd", "cmd"}, 0x0, {33, 33}},
1349     {"cmd param", {"cmd", "cmd"}, 0x0, {33, 33}},
1350
1351     /* Test an application that will not be found on the path */
1352     {"xyzwxyzwxyz", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
1353     {"xyzwxyzwxyz param", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
1354
1355     {NULL, {NULL}, 0, {0}}
1356 };
1357
1358 static void test_dde_default_app(void)
1359 {
1360     char filename[MAX_PATH];
1361     HSZ hszApplication;
1362     dde_thread_info_t info = { filename, GetCurrentThreadId() };
1363     const dde_default_app_tests_t* test;
1364     char params[1024];
1365     DWORD threadId;
1366     MSG msg;
1367     int rc, which = 0;
1368
1369     ddeInst = 0;
1370     rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
1371                         CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
1372     assert(rc == DMLERR_NO_ERROR);
1373
1374     sprintf(filename, "%s\\test file.sde", tmpdir);
1375
1376     /* It is strictly not necessary to register an application name here, but wine's
1377      * DdeNameService implementation complains if 0L is passed instead of
1378      * hszApplication with DNS_FILTEROFF */
1379     hszApplication = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
1380     hszTopic = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
1381     assert(hszApplication && hszTopic);
1382     assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER | DNS_FILTEROFF));
1383
1384     test = dde_default_app_tests;
1385     while (test->command)
1386     {
1387         create_test_association(".sde");
1388         sprintf(params, test->command, tmpdir);
1389         create_test_verb_dde(".sde", "Open", 1, params, "[test]", NULL,
1390                              "shlexec", NULL);
1391         denyNextConnection = FALSE;
1392         ddeApplication[0] = 0;
1393
1394         /* No application will be run as we will respond to the first DDE event,
1395          * so don't wait for it */
1396         SetEvent(hEvent);
1397
1398         assert(CreateThread(NULL, 0, ddeThread, (LPVOID)&info, 0, &threadId));
1399         while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);
1400         rc = msg.wParam > 32 ? 33 : msg.wParam;
1401
1402         /* First test, find which set of test data we expect to see */
1403         if (test == dde_default_app_tests)
1404         {
1405             int i;
1406             for (i=0; i<DDE_DEFAULT_APP_VARIANTS; i++)
1407             {
1408                 if (!strcmp(ddeApplication, test->expectedDdeApplication[i]))
1409                 {
1410                     which = i;
1411                     break;
1412                 }
1413             }
1414             if (i == DDE_DEFAULT_APP_VARIANTS)
1415                 skip("Default DDE application test does not match any available results, using first expected data set.\n");
1416         }
1417
1418         if ((test->todo & 0x1)==0)
1419         {
1420             ok(rc==test->rc[which], "%s failed: rc=%d err=%d\n", shell_call,
1421                rc, GetLastError());
1422         }
1423         else todo_wine
1424         {
1425             ok(rc==test->rc[which], "%s failed: rc=%d err=%d\n", shell_call,
1426                rc, GetLastError());
1427         }
1428         if (rc == 33)
1429         {
1430             if ((test->todo & 0x2)==0)
1431             {
1432                 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
1433                    "Expected application '%s', got '%s'\n",
1434                    test->expectedDdeApplication[which], ddeApplication);
1435             }
1436             else todo_wine
1437             {
1438                 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
1439                    "Expected application '%s', got '%s'\n",
1440                    test->expectedDdeApplication[which], ddeApplication);
1441             }
1442         }
1443
1444         delete_test_association(".sde");
1445         test++;
1446     }
1447
1448     assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
1449     assert(DdeFreeStringHandle(ddeInst, hszTopic));
1450     assert(DdeFreeStringHandle(ddeInst, hszApplication));
1451     assert(DdeUninitialize(ddeInst));
1452 }
1453
1454 static void init_test(void)
1455 {
1456     HMODULE hdll;
1457     HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO*);
1458     char filename[MAX_PATH];
1459     WCHAR lnkfile[MAX_PATH];
1460     char params[1024];
1461     const char* const * testfile;
1462     lnk_desc_t desc;
1463     DWORD rc;
1464     HRESULT r;
1465
1466     hdll=GetModuleHandleA("shell32.dll");
1467     pDllGetVersion=(void*)GetProcAddress(hdll, "DllGetVersion");
1468     if (pDllGetVersion)
1469     {
1470         dllver.cbSize=sizeof(dllver);
1471         pDllGetVersion(&dllver);
1472         trace("major=%d minor=%d build=%d platform=%d\n",
1473               dllver.dwMajorVersion, dllver.dwMinorVersion,
1474               dllver.dwBuildNumber, dllver.dwPlatformID);
1475     }
1476     else
1477     {
1478         memset(&dllver, 0, sizeof(dllver));
1479     }
1480
1481     r = CoInitialize(NULL);
1482     ok(SUCCEEDED(r), "CoInitialize failed (0x%08x)\n", r);
1483     if (!SUCCEEDED(r))
1484         exit(1);
1485
1486     rc=GetModuleFileName(NULL, argv0, sizeof(argv0));
1487     assert(rc!=0 && rc<sizeof(argv0));
1488     if (GetFileAttributes(argv0)==INVALID_FILE_ATTRIBUTES)
1489     {
1490         strcat(argv0, ".so");
1491         ok(GetFileAttributes(argv0)!=INVALID_FILE_ATTRIBUTES,
1492            "unable to find argv0!\n");
1493     }
1494
1495     GetTempPathA(sizeof(tmpdir)/sizeof(*tmpdir), tmpdir);
1496     assert(GetTempFileNameA(tmpdir, "wt", 0, child_file)!=0);
1497     init_event(child_file);
1498
1499     /* Set up the test files */
1500     testfile=testfiles;
1501     while (*testfile)
1502     {
1503         HANDLE hfile;
1504
1505         sprintf(filename, *testfile, tmpdir);
1506         hfile=CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1507                      FILE_ATTRIBUTE_NORMAL, NULL);
1508         if (hfile==INVALID_HANDLE_VALUE)
1509         {
1510             trace("unable to create '%s': err=%d\n", filename, GetLastError());
1511             assert(0);
1512         }
1513         CloseHandle(hfile);
1514         testfile++;
1515     }
1516
1517     /* Setup the test shortcuts */
1518     sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
1519     MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
1520     desc.description=NULL;
1521     desc.workdir=NULL;
1522     sprintf(filename, "%s\\test file.shlexec", tmpdir);
1523     desc.path=filename;
1524     desc.pidl=NULL;
1525     desc.arguments="ignored";
1526     desc.showcmd=0;
1527     desc.icon=NULL;
1528     desc.icon_id=0;
1529     desc.hotkey=0;
1530     create_lnk(lnkfile, &desc, 0);
1531
1532     sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1533     MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
1534     desc.description=NULL;
1535     desc.workdir=NULL;
1536     desc.path=argv0;
1537     desc.pidl=NULL;
1538     sprintf(params, "shlexec \"%s\" Lnk", child_file);
1539     desc.arguments=params;
1540     desc.showcmd=0;
1541     desc.icon=NULL;
1542     desc.icon_id=0;
1543     desc.hotkey=0;
1544     create_lnk(lnkfile, &desc, 0);
1545
1546     /* Create a basic association suitable for most tests */
1547     create_test_association(".shlexec");
1548     create_test_verb(".shlexec", "Open", 0, "Open \"%1\"");
1549     create_test_verb(".shlexec", "NoQuotes", 0, "NoQuotes %1");
1550     create_test_verb(".shlexec", "LowerL", 0, "LowerL %l");
1551     create_test_verb(".shlexec", "QuotedLowerL", 0, "QuotedLowerL \"%l\"");
1552     create_test_verb(".shlexec", "UpperL", 0, "UpperL %L");
1553     create_test_verb(".shlexec", "QuotedUpperL", 0, "QuotedUpperL \"%L\"");
1554 }
1555
1556 static void cleanup_test(void)
1557 {
1558     char filename[MAX_PATH];
1559     const char* const * testfile;
1560
1561     /* Delete the test files */
1562     testfile=testfiles;
1563     while (*testfile)
1564     {
1565         sprintf(filename, *testfile, tmpdir);
1566         DeleteFile(filename);
1567         testfile++;
1568     }
1569     DeleteFile(child_file);
1570
1571     /* Delete the test association */
1572     delete_test_association(".shlexec");
1573
1574     CloseHandle(hEvent);
1575
1576     CoUninitialize();
1577 }
1578
1579 START_TEST(shlexec)
1580 {
1581
1582     myARGC = winetest_get_mainargs(&myARGV);
1583     if (myARGC >= 3)
1584     {
1585         doChild(myARGC, myARGV);
1586         exit(0);
1587     }
1588
1589     init_test();
1590
1591     test_filename();
1592     test_find_executable();
1593     test_lnks();
1594     test_exes();
1595     test_exes_long();
1596     test_dde();
1597     test_dde_default_app();
1598
1599     cleanup_test();
1600 }