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