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