wined3d: Handle the sampler type shift in the frontend.
[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=malloc(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         free(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         sprintf(filename, test->basename, tmpdir);
681         if (strchr(filename, '/'))
682         {
683             c=filename;
684             while (*c)
685             {
686                 if (*c=='\\')
687                     *c='/';
688                 c++;
689             }
690         }
691         if ((test->todo & 0x40)==0)
692         {
693             rc=shell_execute(test->verb, filename, NULL, NULL);
694         }
695         else
696         {
697             char quoted[MAX_PATH + 2];
698             sprintf(quoted, "\"%s\"", filename);
699             rc=shell_execute(test->verb, quoted, NULL, NULL);
700         }
701         if (rc > 32)
702             rc=33;
703         if ((test->todo & 0x1)==0)
704         {
705             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
706                rc, GetLastError());
707         }
708         else todo_wine
709         {
710             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
711                rc, GetLastError());
712         }
713         if (rc == 33)
714         {
715             const char* verb;
716             if ((test->todo & 0x2)==0)
717             {
718                 okChildInt("argcA", 5);
719             }
720             else todo_wine
721             {
722                 okChildInt("argcA", 5);
723             }
724             verb=(test->verb ? test->verb : "Open");
725             if ((test->todo & 0x4)==0)
726             {
727                 okChildString("argvA3", verb);
728             }
729             else todo_wine
730             {
731                 okChildString("argvA3", verb);
732             }
733             if ((test->todo & 0x8)==0)
734             {
735                 okChildPath("argvA4", filename);
736             }
737             else todo_wine
738             {
739                 okChildPath("argvA4", filename);
740             }
741         }
742         test++;
743     }
744
745     test=noquotes_tests;
746     while (test->basename)
747     {
748         sprintf(filename, test->basename, tmpdir);
749         rc=shell_execute(test->verb, filename, NULL, NULL);
750         if (rc > 32)
751             rc=33;
752         if ((test->todo & 0x1)==0)
753         {
754             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
755                rc, GetLastError());
756         }
757         else todo_wine
758         {
759             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
760                rc, GetLastError());
761         }
762         if (rc==0)
763         {
764             int count;
765             const char* verb;
766             char* str;
767
768             verb=(test->verb ? test->verb : "Open");
769             if ((test->todo & 0x4)==0)
770             {
771                 okChildString("argvA3", verb);
772             }
773             else todo_wine
774             {
775                 okChildString("argvA3", verb);
776             }
777
778             count=4;
779             str=filename;
780             while (1)
781             {
782                 char attrib[18];
783                 char* space;
784                 space=strchr(str, ' ');
785                 if (space)
786                     *space='\0';
787                 sprintf(attrib, "argvA%d", count);
788                 if ((test->todo & 0x8)==0)
789                 {
790                     okChildPath(attrib, str);
791                 }
792                 else todo_wine
793                 {
794                     okChildPath(attrib, str);
795                 }
796                 count++;
797                 if (!space)
798                     break;
799                 str=space+1;
800             }
801             if ((test->todo & 0x2)==0)
802             {
803                 okChildInt("argcA", count);
804             }
805             else todo_wine
806             {
807                 okChildInt("argcA", count);
808             }
809         }
810         test++;
811     }
812
813     if (dllver.dwMajorVersion != 0)
814     {
815         /* The more recent versions of shell32.dll accept quoted filenames
816          * while older ones (e.g. 4.00) don't. Still we want to test this
817          * because IE 6 depends on the new behavior.
818          * One day we may need to check the exact version of the dll but for
819          * now making sure DllGetVersion() is present is sufficient.
820          */
821         sprintf(filename, "\"%s\\test file.shlexec\"", tmpdir);
822         rc=shell_execute(NULL, filename, NULL, NULL);
823         ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
824            GetLastError());
825         okChildInt("argcA", 5);
826         okChildString("argvA3", "Open");
827         sprintf(filename, "%s\\test file.shlexec", tmpdir);
828         okChildPath("argvA4", filename);
829     }
830 }
831
832 static void test_find_executable(void)
833 {
834     char filename[MAX_PATH];
835     char command[MAX_PATH];
836     const filename_tests_t* test;
837     INT_PTR rc;
838
839     if (!create_test_association(".sfe"))
840     {
841         skip("Unable to create association for '.sfe'\n");
842         return;
843     }
844     create_test_verb(".sfe", "Open", 1, "%1");
845
846     /* Don't test FindExecutable(..., NULL), it always crashes */
847
848     strcpy(command, "your word");
849     if (0) /* Can crash on Vista! */
850     {
851     rc=(INT_PTR)FindExecutableA(NULL, NULL, command);
852     ok(rc == SE_ERR_FNF || rc > 32 /* nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
853     ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
854     }
855
856     strcpy(command, "your word");
857     rc=(INT_PTR)FindExecutableA(tmpdir, NULL, command);
858     ok(rc == SE_ERR_NOASSOC /* >= win2000 */ || rc > 32 /* win98, nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
859     ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
860
861     sprintf(filename, "%s\\test file.sfe", tmpdir);
862     rc=(INT_PTR)FindExecutableA(filename, NULL, command);
863     ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
864     /* Depending on the platform, command could be '%1' or 'test file.sfe' */
865
866     rc=(INT_PTR)FindExecutableA("test file.sfe", tmpdir, command);
867     ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
868
869     rc=(INT_PTR)FindExecutableA("test file.sfe", NULL, command);
870     ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %ld\n", filename, rc);
871
872     delete_test_association(".sfe");
873
874     if (!create_test_association(".shl"))
875     {
876         skip("Unable to create association for '.shl'\n");
877         return;
878     }
879     create_test_verb(".shl", "Open", 0, "Open");
880
881     sprintf(filename, "%s\\test file.shl", tmpdir);
882     rc=(INT_PTR)FindExecutableA(filename, NULL, command);
883     ok(rc == SE_ERR_FNF /* NT4 */ || rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
884
885     sprintf(filename, "%s\\test file.shlfoo", tmpdir);
886     rc=(INT_PTR)FindExecutableA(filename, NULL, command);
887
888     delete_test_association(".shl");
889
890     if (rc > 32)
891     {
892         /* On Windows XP and 2003 FindExecutable() is completely broken.
893          * Probably what it does is convert the filename to 8.3 format,
894          * which as a side effect converts the '.shlfoo' extension to '.shl',
895          * and then tries to find an association for '.shl'. This means it
896          * will normally fail on most extensions with more than 3 characters,
897          * like '.mpeg', etc.
898          * Also it means we cannot do any other test.
899          */
900         trace("FindExecutable() is broken -> skipping 4+ character extension tests\n");
901         return;
902     }
903
904     test=filename_tests;
905     while (test->basename)
906     {
907         sprintf(filename, test->basename, tmpdir);
908         if (strchr(filename, '/'))
909         {
910             char* c;
911             c=filename;
912             while (*c)
913             {
914                 if (*c=='\\')
915                     *c='/';
916                 c++;
917             }
918         }
919         /* Win98 does not '\0'-terminate command! */
920         memset(command, '\0', sizeof(command));
921         rc=(INT_PTR)FindExecutableA(filename, NULL, command);
922         if (rc > 32)
923             rc=33;
924         if ((test->todo & 0x10)==0)
925         {
926             ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
927         }
928         else todo_wine
929         {
930             ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
931         }
932         if (rc > 32)
933         {
934             int equal;
935             equal=strcmp(command, argv0) == 0 ||
936                 /* NT4 returns an extra 0x8 character! */
937                 (strlen(command) == strlen(argv0)+1 && strncmp(command, argv0, strlen(argv0)) == 0);
938             if ((test->todo & 0x20)==0)
939             {
940                 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
941                    filename, command, argv0);
942             }
943             else todo_wine
944             {
945                 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
946                    filename, command, argv0);
947             }
948         }
949         test++;
950     }
951 }
952
953
954 static filename_tests_t lnk_tests[]=
955 {
956     /* Pass bad / nonexistent filenames as a parameter */
957     {NULL, "%s\\nonexistent.shlexec",    0xa, 33},
958     {NULL, "%s\\nonexistent.noassoc",    0xa, 33},
959
960     /* Pass regular paths as a parameter */
961     {NULL, "%s\\test file.shlexec",      0xa, 33},
962     {NULL, "%s/%%nasty%% $file.shlexec", 0xa, 33},
963
964     /* Pass filenames with no association as a parameter */
965     {NULL, "%s\\test file.noassoc",      0xa, 33},
966
967     {NULL, NULL, 0}
968 };
969
970 static void test_lnks(void)
971 {
972     char filename[MAX_PATH];
973     char params[MAX_PATH];
974     const filename_tests_t* test;
975     int rc;
976
977     sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
978     rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
979     ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
980        GetLastError());
981     okChildInt("argcA", 5);
982     okChildString("argvA3", "Open");
983     sprintf(filename, "%s\\test file.shlexec", tmpdir);
984     okChildPath("argvA4", filename);
985
986     sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
987     rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
988     ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
989        GetLastError());
990     okChildInt("argcA", 4);
991     okChildString("argvA3", "Lnk");
992
993     if (dllver.dwMajorVersion>=6)
994     {
995         char* c;
996        /* Recent versions of shell32.dll accept '/'s in shortcut paths.
997          * Older versions don't or are quite buggy in this regard.
998          */
999         sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1000         c=filename;
1001         while (*c)
1002         {
1003             if (*c=='\\')
1004                 *c='/';
1005             c++;
1006         }
1007         rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
1008         ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
1009            GetLastError());
1010         okChildInt("argcA", 4);
1011         okChildString("argvA3", "Lnk");
1012     }
1013
1014     sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1015     test=lnk_tests;
1016     while (test->basename)
1017     {
1018         params[0]='\"';
1019         sprintf(params+1, test->basename, tmpdir);
1020         strcat(params,"\"");
1021         rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, params,
1022                             NULL);
1023         if (rc > 32)
1024             rc=33;
1025         if ((test->todo & 0x1)==0)
1026         {
1027             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1028                rc, GetLastError());
1029         }
1030         else todo_wine
1031         {
1032             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1033                rc, GetLastError());
1034         }
1035         if (rc==0)
1036         {
1037             if ((test->todo & 0x2)==0)
1038             {
1039                 okChildInt("argcA", 5);
1040             }
1041             else 
1042             {
1043                 okChildInt("argcA", 5);
1044             }
1045             if ((test->todo & 0x4)==0)
1046             {
1047                 okChildString("argvA3", "Lnk");
1048             }
1049             else todo_wine
1050             {
1051                 okChildString("argvA3", "Lnk");
1052             }
1053             sprintf(params, test->basename, tmpdir);
1054             if ((test->todo & 0x8)==0)
1055             {
1056                 okChildPath("argvA4", params);
1057             }
1058             else
1059             {
1060                 okChildPath("argvA4", params);
1061             }
1062         }
1063         test++;
1064     }
1065 }
1066
1067
1068 static void test_exes(void)
1069 {
1070     char filename[MAX_PATH];
1071     char params[1024];
1072     int rc;
1073
1074     sprintf(params, "shlexec \"%s\" Exec", child_file);
1075
1076     /* We need NOZONECHECKS on Win2003 to block a dialog */
1077     rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
1078                         NULL);
1079     ok(rc > 32, "%s returned %d\n", shell_call, rc);
1080     okChildInt("argcA", 4);
1081     okChildString("argvA3", "Exec");
1082
1083     sprintf(filename, "%s\\test file.noassoc", tmpdir);
1084     if (CopyFile(argv0, filename, FALSE))
1085     {
1086         rc=shell_execute(NULL, filename, params, NULL);
1087         todo_wine {
1088         ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
1089         }
1090     }
1091 }
1092
1093 static void test_exes_long(void)
1094 {
1095     char filename[MAX_PATH];
1096     char params[2024];
1097     char longparam[MAX_PATH];
1098     int rc;
1099
1100     for (rc = 0; rc < MAX_PATH; rc++)
1101         longparam[rc]='a'+rc%26;
1102     longparam[MAX_PATH-1]=0;
1103
1104
1105     sprintf(params, "shlexec \"%s\" %s", child_file,longparam);
1106
1107     /* We need NOZONECHECKS on Win2003 to block a dialog */
1108     rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
1109                         NULL);
1110     ok(rc > 32, "%s returned %d\n", shell_call, rc);
1111     okChildInt("argcA", 4);
1112     okChildString("argvA3", longparam);
1113
1114     sprintf(filename, "%s\\test file.noassoc", tmpdir);
1115     if (CopyFile(argv0, filename, FALSE))
1116     {
1117         rc=shell_execute(NULL, filename, params, NULL);
1118         todo_wine {
1119         ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
1120         }
1121     }
1122 }
1123
1124 typedef struct
1125 {
1126     const char* command;
1127     const char* ddeexec;
1128     const char* application;
1129     const char* topic;
1130     const char* ifexec;
1131     int expectedArgs;
1132     const char* expectedDdeExec;
1133     int todo;
1134     int rc;
1135 } dde_tests_t;
1136
1137 static dde_tests_t dde_tests[] =
1138 {
1139     /* Test passing and not passing command-line
1140      * argument, no DDE */
1141     {"", NULL, NULL, NULL, NULL, FALSE, "", 0x0, 33},
1142     {"\"%1\"", NULL, NULL, NULL, NULL, TRUE, "", 0x0, 33},
1143
1144     /* Test passing and not passing command-line
1145      * argument, with DDE */
1146     {"", "[open(\"%1\")]", "shlexec", "dde", NULL, FALSE, "[open(\"%s\")]", 0x0, 33},
1147     {"\"%1\"", "[open(\"%1\")]", "shlexec", "dde", NULL, TRUE, "[open(\"%s\")]", 0x0, 33},
1148
1149     /* Test unquoted %1 in command and ddeexec
1150      * (test filename has space) */
1151     {"%1", "[open(%1)]", "shlexec", "dde", NULL, 2, "[open(%s)]", 0x0, 33},
1152
1153     /* Test ifexec precedence over ddeexec */
1154     {"", "[open(\"%1\")]", "shlexec", "dde", "[ifexec(\"%1\")]", FALSE, "[ifexec(\"%s\")]", 0x0, 33},
1155
1156     /* Test default DDE topic */
1157     {"", "[open(\"%1\")]", "shlexec", NULL, NULL, FALSE, "[open(\"%s\")]", 0x0, 33},
1158
1159     /* Test default DDE application */
1160     {"", "[open(\"%1\")]", NULL, "dde", NULL, FALSE, "[open(\"%s\")]", 0x0, 33},
1161
1162     {NULL, NULL, NULL, NULL, NULL, 0, 0x0, 0}
1163 };
1164
1165 static DWORD ddeInst;
1166 static HSZ hszTopic;
1167 static char ddeExec[MAX_PATH], ddeApplication[MAX_PATH];
1168 static BOOL denyNextConnection;
1169
1170 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
1171                                 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
1172                                 ULONG_PTR dwData1, ULONG_PTR dwData2)
1173 {
1174     DWORD size = 0;
1175
1176     if (winetest_debug > 2)
1177         trace("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
1178               uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
1179
1180     switch (uType)
1181     {
1182         case XTYP_CONNECT:
1183             if (!DdeCmpStringHandles(hsz1, hszTopic))
1184             {
1185                 if (denyNextConnection)
1186                     denyNextConnection = FALSE;
1187                 else
1188                 {
1189                     size = DdeQueryString(ddeInst, hsz2, ddeApplication, MAX_PATH, CP_WINANSI);
1190                     assert(size < MAX_PATH);
1191                     return (HDDEDATA)TRUE;
1192                 }
1193             }
1194             return (HDDEDATA)FALSE;
1195
1196         case XTYP_EXECUTE:
1197             size = DdeGetData(hData, (LPBYTE)ddeExec, MAX_PATH, 0L);
1198             assert(size < MAX_PATH);
1199             DdeFreeDataHandle(hData);
1200             return (HDDEDATA)DDE_FACK;
1201
1202         default:
1203             return NULL;
1204     }
1205 }
1206
1207 typedef struct
1208 {
1209     char *filename;
1210     DWORD threadIdParent;
1211 } dde_thread_info_t;
1212
1213 static DWORD CALLBACK ddeThread(LPVOID arg)
1214 {
1215     dde_thread_info_t *info = arg;
1216     assert(info && info->filename);
1217     PostThreadMessage(info->threadIdParent,
1218                       WM_QUIT,
1219                       shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, info->filename, NULL, NULL),
1220                       0L);
1221     ExitThread(0);
1222 }
1223
1224 /* ShellExecute won't successfully send DDE commands to console applications after starting them,
1225  * so we run a DDE server in this application, deny the first connection request to make
1226  * ShellExecute start the application, and then process the next DDE connection in this application
1227  * to see the execute command that is sent. */
1228 static void test_dde(void)
1229 {
1230     char filename[MAX_PATH], defApplication[MAX_PATH];
1231     HSZ hszApplication;
1232     dde_thread_info_t info = { filename, GetCurrentThreadId() };
1233     const dde_tests_t* test;
1234     char params[1024];
1235     DWORD threadId;
1236     MSG msg;
1237     int rc;
1238
1239     ddeInst = 0;
1240     rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
1241                         CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
1242     assert(rc == DMLERR_NO_ERROR);
1243
1244     sprintf(filename, "%s\\test file.sde", tmpdir);
1245
1246     /* Default service is application name minus path and extension */
1247     strcpy(defApplication, strrchr(argv0, '\\')+1);
1248     *strchr(defApplication, '.') = 0;
1249
1250     test = dde_tests;
1251     while (test->command)
1252     {
1253         if (!create_test_association(".sde"))
1254         {
1255             skip("Unable to create association for '.sfe'\n");
1256             return;
1257         }
1258         create_test_verb_dde(".sde", "Open", 0, test->command, test->ddeexec,
1259                              test->application, test->topic, test->ifexec);
1260         hszApplication = DdeCreateStringHandleA(ddeInst, test->application ?
1261                                                 test->application : defApplication, CP_WINANSI);
1262         hszTopic = DdeCreateStringHandleA(ddeInst, test->topic ? test->topic : SZDDESYS_TOPIC,
1263                                           CP_WINANSI);
1264         assert(hszApplication && hszTopic);
1265         assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER));
1266         denyNextConnection = TRUE;
1267         ddeExec[0] = 0;
1268
1269         assert(CreateThread(NULL, 0, ddeThread, &info, 0, &threadId));
1270         while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);
1271         rc = msg.wParam > 32 ? 33 : msg.wParam;
1272         if ((test->todo & 0x1)==0)
1273         {
1274             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1275                rc, GetLastError());
1276         }
1277         else todo_wine
1278         {
1279             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1280                rc, GetLastError());
1281         }
1282         if (rc == 33)
1283         {
1284             if ((test->todo & 0x2)==0)
1285             {
1286                 okChildInt("argcA", test->expectedArgs + 3);
1287             }
1288             else todo_wine
1289             {
1290                 okChildInt("argcA", test->expectedArgs + 3);
1291             }
1292             if (test->expectedArgs == 1)
1293             {
1294                 if ((test->todo & 0x4) == 0)
1295                 {
1296                     okChildPath("argvA3", filename);
1297                 }
1298                 else todo_wine
1299                 {
1300                     okChildPath("argvA3", filename);
1301                 }
1302             }
1303             if ((test->todo & 0x8) == 0)
1304             {
1305                 sprintf(params, test->expectedDdeExec, filename);
1306                 ok(StrCmpPath(params, ddeExec) == 0,
1307                    "ddeexec expected '%s', got '%s'\n", params, ddeExec);
1308             }
1309             else todo_wine
1310             {
1311                 sprintf(params, test->expectedDdeExec, filename);
1312                 ok(StrCmpPath(params, ddeExec) == 0,
1313                    "ddeexec expected '%s', got '%s'\n", params, ddeExec);
1314             }
1315         }
1316
1317         assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
1318         assert(DdeFreeStringHandle(ddeInst, hszTopic));
1319         assert(DdeFreeStringHandle(ddeInst, hszApplication));
1320         delete_test_association(".sde");
1321         test++;
1322     }
1323
1324     assert(DdeUninitialize(ddeInst));
1325 }
1326
1327 #define DDE_DEFAULT_APP_VARIANTS 2
1328 typedef struct
1329 {
1330     const char* command;
1331     const char* expectedDdeApplication[DDE_DEFAULT_APP_VARIANTS];
1332     int todo;
1333     int rc[DDE_DEFAULT_APP_VARIANTS];
1334 } dde_default_app_tests_t;
1335
1336 static dde_default_app_tests_t dde_default_app_tests[] =
1337 {
1338     /* Windows XP and 98 handle default DDE app names in different ways.
1339      * The application name we see in the first test determines the pattern
1340      * of application names and return codes we will look for. */
1341
1342     /* Test unquoted existing filename with a space */
1343     {"%s\\test file.exe", {"test file", "test"}, 0x0, {33, 33}},
1344     {"%s\\test file.exe param", {"test file", "test"}, 0x0, {33, 33}},
1345
1346     /* Test quoted existing filename with a space */
1347     {"\"%s\\test file.exe\"", {"test file", "test file"}, 0x0, {33, 33}},
1348     {"\"%s\\test file.exe\" param", {"test file", "test file"}, 0x0, {33, 33}},
1349
1350     /* Test unquoted filename with a space that doesn't exist, but
1351      * test2.exe does */
1352     {"%s\\test2 file.exe", {"test2", "test2"}, 0x0, {33, 33}},
1353     {"%s\\test2 file.exe param", {"test2", "test2"}, 0x0, {33, 33}},
1354
1355     /* Test quoted filename with a space that does not exist */
1356     {"\"%s\\test2 file.exe\"", {"", "test2 file"}, 0x0, {5, 33}},
1357     {"\"%s\\test2 file.exe\" param", {"", "test2 file"}, 0x0, {5, 33}},
1358
1359     /* Test filename supplied without the extension */
1360     {"%s\\test2", {"test2", "test2"}, 0x0, {33, 33}},
1361     {"%s\\test2 param", {"test2", "test2"}, 0x0, {33, 33}},
1362
1363     /* Test an unquoted nonexistent filename */
1364     {"%s\\notexist.exe", {"", "notexist"}, 0x0, {5, 33}},
1365     {"%s\\notexist.exe param", {"", "notexist"}, 0x0, {5, 33}},
1366
1367     /* Test an application that will be found on the path */
1368     {"cmd", {"cmd", "cmd"}, 0x0, {33, 33}},
1369     {"cmd param", {"cmd", "cmd"}, 0x0, {33, 33}},
1370
1371     /* Test an application that will not be found on the path */
1372     {"xyzwxyzwxyz", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
1373     {"xyzwxyzwxyz param", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
1374
1375     {NULL, {NULL}, 0, {0}}
1376 };
1377
1378 static void test_dde_default_app(void)
1379 {
1380     char filename[MAX_PATH];
1381     HSZ hszApplication;
1382     dde_thread_info_t info = { filename, GetCurrentThreadId() };
1383     const dde_default_app_tests_t* test;
1384     char params[1024];
1385     DWORD threadId;
1386     MSG msg;
1387     int rc, which = 0;
1388
1389     ddeInst = 0;
1390     rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
1391                         CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
1392     assert(rc == DMLERR_NO_ERROR);
1393
1394     sprintf(filename, "%s\\test file.sde", tmpdir);
1395
1396     /* It is strictly not necessary to register an application name here, but wine's
1397      * DdeNameService implementation complains if 0L is passed instead of
1398      * hszApplication with DNS_FILTEROFF */
1399     hszApplication = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
1400     hszTopic = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
1401     assert(hszApplication && hszTopic);
1402     assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER | DNS_FILTEROFF));
1403
1404     test = dde_default_app_tests;
1405     while (test->command)
1406     {
1407         if (!create_test_association(".sde"))
1408         {
1409             skip("Unable to create association for '.sde'\n");
1410             return;
1411         }
1412         sprintf(params, test->command, tmpdir);
1413         create_test_verb_dde(".sde", "Open", 1, params, "[test]", NULL,
1414                              "shlexec", NULL);
1415         denyNextConnection = FALSE;
1416         ddeApplication[0] = 0;
1417
1418         /* No application will be run as we will respond to the first DDE event,
1419          * so don't wait for it */
1420         SetEvent(hEvent);
1421
1422         assert(CreateThread(NULL, 0, ddeThread, &info, 0, &threadId));
1423         while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);
1424         rc = msg.wParam > 32 ? 33 : msg.wParam;
1425
1426         /* First test, find which set of test data we expect to see */
1427         if (test == dde_default_app_tests)
1428         {
1429             int i;
1430             for (i=0; i<DDE_DEFAULT_APP_VARIANTS; i++)
1431             {
1432                 if (!strcmp(ddeApplication, test->expectedDdeApplication[i]))
1433                 {
1434                     which = i;
1435                     break;
1436                 }
1437             }
1438             if (i == DDE_DEFAULT_APP_VARIANTS)
1439                 skip("Default DDE application test does not match any available results, using first expected data set.\n");
1440         }
1441
1442         if ((test->todo & 0x1)==0)
1443         {
1444             ok(rc==test->rc[which], "%s failed: rc=%d err=%d\n", shell_call,
1445                rc, GetLastError());
1446         }
1447         else todo_wine
1448         {
1449             ok(rc==test->rc[which], "%s failed: rc=%d err=%d\n", shell_call,
1450                rc, GetLastError());
1451         }
1452         if (rc == 33)
1453         {
1454             if ((test->todo & 0x2)==0)
1455             {
1456                 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
1457                    "Expected application '%s', got '%s'\n",
1458                    test->expectedDdeApplication[which], ddeApplication);
1459             }
1460             else todo_wine
1461             {
1462                 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
1463                    "Expected application '%s', got '%s'\n",
1464                    test->expectedDdeApplication[which], ddeApplication);
1465             }
1466         }
1467
1468         delete_test_association(".sde");
1469         test++;
1470     }
1471
1472     assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
1473     assert(DdeFreeStringHandle(ddeInst, hszTopic));
1474     assert(DdeFreeStringHandle(ddeInst, hszApplication));
1475     assert(DdeUninitialize(ddeInst));
1476 }
1477
1478 static void init_test(void)
1479 {
1480     HMODULE hdll;
1481     HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO*);
1482     char filename[MAX_PATH];
1483     WCHAR lnkfile[MAX_PATH];
1484     char params[1024];
1485     const char* const * testfile;
1486     lnk_desc_t desc;
1487     DWORD rc;
1488     HRESULT r;
1489
1490     hdll=GetModuleHandleA("shell32.dll");
1491     pDllGetVersion=(void*)GetProcAddress(hdll, "DllGetVersion");
1492     if (pDllGetVersion)
1493     {
1494         dllver.cbSize=sizeof(dllver);
1495         pDllGetVersion(&dllver);
1496         trace("major=%d minor=%d build=%d platform=%d\n",
1497               dllver.dwMajorVersion, dllver.dwMinorVersion,
1498               dllver.dwBuildNumber, dllver.dwPlatformID);
1499     }
1500     else
1501     {
1502         memset(&dllver, 0, sizeof(dllver));
1503     }
1504
1505     r = CoInitialize(NULL);
1506     ok(SUCCEEDED(r), "CoInitialize failed (0x%08x)\n", r);
1507     if (FAILED(r))
1508         exit(1);
1509
1510     rc=GetModuleFileName(NULL, argv0, sizeof(argv0));
1511     assert(rc!=0 && rc<sizeof(argv0));
1512     if (GetFileAttributes(argv0)==INVALID_FILE_ATTRIBUTES)
1513     {
1514         strcat(argv0, ".so");
1515         ok(GetFileAttributes(argv0)!=INVALID_FILE_ATTRIBUTES,
1516            "unable to find argv0!\n");
1517     }
1518
1519     GetTempPathA(sizeof(tmpdir)/sizeof(*tmpdir), tmpdir);
1520     assert(GetTempFileNameA(tmpdir, "wt", 0, child_file)!=0);
1521     init_event(child_file);
1522
1523     /* Set up the test files */
1524     testfile=testfiles;
1525     while (*testfile)
1526     {
1527         HANDLE hfile;
1528
1529         sprintf(filename, *testfile, tmpdir);
1530         hfile=CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1531                      FILE_ATTRIBUTE_NORMAL, NULL);
1532         if (hfile==INVALID_HANDLE_VALUE)
1533         {
1534             trace("unable to create '%s': err=%d\n", filename, GetLastError());
1535             assert(0);
1536         }
1537         CloseHandle(hfile);
1538         testfile++;
1539     }
1540
1541     /* Setup the test shortcuts */
1542     sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
1543     MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
1544     desc.description=NULL;
1545     desc.workdir=NULL;
1546     sprintf(filename, "%s\\test file.shlexec", tmpdir);
1547     desc.path=filename;
1548     desc.pidl=NULL;
1549     desc.arguments="ignored";
1550     desc.showcmd=0;
1551     desc.icon=NULL;
1552     desc.icon_id=0;
1553     desc.hotkey=0;
1554     create_lnk(lnkfile, &desc, 0);
1555
1556     sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1557     MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
1558     desc.description=NULL;
1559     desc.workdir=NULL;
1560     desc.path=argv0;
1561     desc.pidl=NULL;
1562     sprintf(params, "shlexec \"%s\" Lnk", child_file);
1563     desc.arguments=params;
1564     desc.showcmd=0;
1565     desc.icon=NULL;
1566     desc.icon_id=0;
1567     desc.hotkey=0;
1568     create_lnk(lnkfile, &desc, 0);
1569
1570     /* Create a basic association suitable for most tests */
1571     if (!create_test_association(".shlexec"))
1572     {
1573         skip("Unable to create association for '.shlexec'\n");
1574         return;
1575     }
1576     create_test_verb(".shlexec", "Open", 0, "Open \"%1\"");
1577     create_test_verb(".shlexec", "NoQuotes", 0, "NoQuotes %1");
1578     create_test_verb(".shlexec", "LowerL", 0, "LowerL %l");
1579     create_test_verb(".shlexec", "QuotedLowerL", 0, "QuotedLowerL \"%l\"");
1580     create_test_verb(".shlexec", "UpperL", 0, "UpperL %L");
1581     create_test_verb(".shlexec", "QuotedUpperL", 0, "QuotedUpperL \"%L\"");
1582 }
1583
1584 static void cleanup_test(void)
1585 {
1586     char filename[MAX_PATH];
1587     const char* const * testfile;
1588
1589     /* Delete the test files */
1590     testfile=testfiles;
1591     while (*testfile)
1592     {
1593         sprintf(filename, *testfile, tmpdir);
1594         DeleteFile(filename);
1595         testfile++;
1596     }
1597     DeleteFile(child_file);
1598
1599     /* Delete the test association */
1600     delete_test_association(".shlexec");
1601
1602     CloseHandle(hEvent);
1603
1604     CoUninitialize();
1605 }
1606
1607 static void test_commandline(void)
1608 {
1609     static const WCHAR one[] = {'o','n','e',0};
1610     static const WCHAR two[] = {'t','w','o',0};
1611     static const WCHAR three[] = {'t','h','r','e','e',0};
1612     static const WCHAR four[] = {'f','o','u','r',0};
1613
1614     static const WCHAR fmt1[] = {'%','s',' ','%','s',' ','%','s',' ','%','s',0};
1615     static const WCHAR fmt2[] = {' ','%','s',' ','%','s',' ','%','s',' ','%','s',0};
1616     static const WCHAR fmt3[] = {'%','s','=','%','s',' ','%','s','=','\"','%','s','\"',0};
1617     static const WCHAR fmt4[] = {'\"','%','s','\"',' ','\"','%','s',' ','%','s','\"',' ','%','s',0};
1618     static const WCHAR fmt5[] = {'\\','\"','%','s','\"',' ','%','s','=','\"','%','s','\\','\"',' ','\"','%','s','\\','\"',0};
1619     static const WCHAR fmt6[] = {0};
1620
1621     static const WCHAR chkfmt1[] = {'%','s','=','%','s',0};
1622     static const WCHAR chkfmt2[] = {'%','s',' ','%','s',0};
1623     static const WCHAR chkfmt3[] = {'\\','\"','%','s','\"',0};
1624     static const WCHAR chkfmt4[] = {'%','s','=','%','s','\"',' ','%','s','\"',0};
1625     WCHAR cmdline[255];
1626     LPWSTR *args = (LPWSTR*)0xdeadcafe;
1627     INT numargs = -1;
1628
1629     wsprintfW(cmdline,fmt1,one,two,three,four);
1630     args=CommandLineToArgvW(cmdline,&numargs);
1631     if (args == NULL && numargs == -1)
1632     {
1633         win_skip("CommandLineToArgvW not implemented, skipping\n");
1634         return;
1635     }
1636     ok(numargs == 4, "expected 4 args, got %i\n",numargs);
1637     ok(lstrcmpW(args[0],one)==0,"arg0 is not as expected\n");
1638     ok(lstrcmpW(args[1],two)==0,"arg1 is not as expected\n");
1639     ok(lstrcmpW(args[2],three)==0,"arg2 is not as expected\n");
1640     ok(lstrcmpW(args[3],four)==0,"arg3 is not as expected\n");
1641
1642     wsprintfW(cmdline,fmt2,one,two,three,four);
1643     args=CommandLineToArgvW(cmdline,&numargs);
1644     ok(numargs == 5, "expected 5 args, got %i\n",numargs);
1645     ok(args[0][0]==0,"arg0 is not as expected\n");
1646     ok(lstrcmpW(args[1],one)==0,"arg1 is not as expected\n");
1647     ok(lstrcmpW(args[2],two)==0,"arg2 is not as expected\n");
1648     ok(lstrcmpW(args[3],three)==0,"arg3 is not as expected\n");
1649     ok(lstrcmpW(args[4],four)==0,"arg4 is not as expected\n");
1650
1651     wsprintfW(cmdline,fmt3,one,two,three,four);
1652     args=CommandLineToArgvW(cmdline,&numargs);
1653     ok(numargs == 2, "expected 2 args, got %i\n",numargs);
1654     wsprintfW(cmdline,chkfmt1,one,two);
1655     ok(lstrcmpW(args[0],cmdline)==0,"arg0 is not as expected\n");
1656     wsprintfW(cmdline,chkfmt1,three,four);
1657     ok(lstrcmpW(args[1],cmdline)==0,"arg1 is not as expected\n");
1658
1659     wsprintfW(cmdline,fmt4,one,two,three,four);
1660     args=CommandLineToArgvW(cmdline,&numargs);
1661     ok(numargs == 3, "expected 3 args, got %i\n",numargs);
1662     ok(lstrcmpW(args[0],one)==0,"arg0 is not as expected\n");
1663     wsprintfW(cmdline,chkfmt2,two,three);
1664     ok(lstrcmpW(args[1],cmdline)==0,"arg1 is not as expected\n");
1665     ok(lstrcmpW(args[2],four)==0,"arg2 is not as expected\n");
1666
1667     wsprintfW(cmdline,fmt5,one,two,three,four);
1668     args=CommandLineToArgvW(cmdline,&numargs);
1669     ok(numargs == 2, "expected 2 args, got %i\n",numargs);
1670     wsprintfW(cmdline,chkfmt3,one);
1671     todo_wine ok(lstrcmpW(args[0],cmdline)==0,"arg0 is not as expected\n");
1672     wsprintfW(cmdline,chkfmt4,two,three,four);
1673     todo_wine ok(lstrcmpW(args[1],cmdline)==0,"arg1 is not as expected\n");
1674
1675     wsprintfW(cmdline,fmt6);
1676     args=CommandLineToArgvW(cmdline,&numargs);
1677     ok(numargs == 1, "expected 1 args, got %i\n",numargs);
1678 }
1679
1680 START_TEST(shlexec)
1681 {
1682
1683     myARGC = winetest_get_mainargs(&myARGV);
1684     if (myARGC >= 3)
1685     {
1686         doChild(myARGC, myARGV);
1687         exit(0);
1688     }
1689
1690     init_test();
1691
1692     test_filename();
1693     test_find_executable();
1694     test_lnks();
1695     test_exes();
1696     test_exes_long();
1697     test_dde();
1698     test_dde_default_app();
1699     test_commandline();
1700
1701     cleanup_test();
1702 }