mcicda: Exclude unused headers.
[wine] / dlls / shell32 / tests / shlexec.c
1 /*
2  * Unit test of the ShellExecute function.
3  *
4  * Copyright 2005 Francois Gouget for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 /* TODO:
22  * - test the default verb selection
23  * - test selection of an alternate class
24  * - try running executables in more ways
25  * - try passing arguments to executables
26  * - ShellExecute("foo.shlexec") with no path should work if foo.shlexec is
27  *   in the PATH
28  * - test associations that use %l, %L or "%1" instead of %1
29  * - we may want to test ShellExecuteEx() instead of ShellExecute()
30  *   and then we could also check its return value
31  * - ShellExecuteEx() also calls SetLastError() with meaningful values which
32  *   we could check
33  */
34
35 #include <stdio.h>
36 #include <assert.h>
37
38 #include "wtypes.h"
39 #include "winbase.h"
40 #include "windef.h"
41 #include "shellapi.h"
42 #include "shlwapi.h"
43 #include "wine/test.h"
44
45 #include "shell32_test.h"
46
47
48 static char argv0[MAX_PATH];
49 static int myARGC;
50 static char** myARGV;
51 static char tmpdir[MAX_PATH];
52 static char child_file[MAX_PATH];
53 static DLLVERSIONINFO dllver;
54
55
56 /***
57  *
58  * ShellExecute wrappers
59  *
60  ***/
61 static void dump_child(void);
62
63 static HANDLE hEvent;
64 static void init_event(const char* child_file)
65 {
66     char* event_name;
67     event_name=strrchr(child_file, '\\')+1;
68     hEvent=CreateEvent(NULL, FALSE, FALSE, event_name);
69 }
70
71 static void strcat_param(char* str, const char* param)
72 {
73     if (param!=NULL)
74     {
75         strcat(str, "\"");
76         strcat(str, param);
77         strcat(str, "\"");
78     }
79     else
80     {
81         strcat(str, "null");
82     }
83 }
84
85 static char shell_call[2048]="";
86 static int shell_execute(LPCSTR operation, LPCSTR file, LPCSTR parameters, LPCSTR directory)
87 {
88     int rc;
89
90     strcpy(shell_call, "ShellExecute(");
91     strcat_param(shell_call, operation);
92     strcat(shell_call, ", ");
93     strcat_param(shell_call, file);
94     strcat(shell_call, ", ");
95     strcat_param(shell_call, parameters);
96     strcat(shell_call, ", ");
97     strcat_param(shell_call, directory);
98     strcat(shell_call, ")");
99     if (winetest_debug > 1)
100         trace("%s\n", shell_call);
101
102     DeleteFile(child_file);
103     SetLastError(0xcafebabe);
104
105     /* FIXME: We cannot use ShellExecuteEx() here because if there is no
106      * association it displays the 'Open With' dialog and I could not find
107      * a flag to prevent this.
108      */
109     rc=(int)ShellExecute(NULL, operation, file, parameters, directory,
110                          SW_SHOWNORMAL);
111
112     if (rc > 32)
113     {
114         int wait_rc;
115         wait_rc=WaitForSingleObject(hEvent, 5000);
116         ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
117     }
118     /* The child process may have changed the result file, so let profile
119      * functions know about it
120      */
121     WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
122     if (rc > 32)
123         dump_child();
124
125     return rc;
126 }
127
128 static int shell_execute_ex(DWORD mask, LPCSTR operation, LPCSTR file,
129                             LPCSTR parameters, LPCSTR directory)
130 {
131     SHELLEXECUTEINFO sei;
132     BOOL success;
133     int rc;
134
135     strcpy(shell_call, "ShellExecuteEx(");
136     strcat_param(shell_call, operation);
137     strcat(shell_call, ", ");
138     strcat_param(shell_call, file);
139     strcat(shell_call, ", ");
140     strcat_param(shell_call, parameters);
141     strcat(shell_call, ", ");
142     strcat_param(shell_call, directory);
143     strcat(shell_call, ")");
144     if (winetest_debug > 1)
145         trace("%s\n", shell_call);
146
147     sei.cbSize=sizeof(sei);
148     sei.fMask=SEE_MASK_NOCLOSEPROCESS | mask;
149     sei.hwnd=NULL;
150     sei.lpVerb=operation;
151     sei.lpFile=file;
152     sei.lpParameters=parameters;
153     sei.lpDirectory=directory;
154     sei.nShow=SW_SHOWNORMAL;
155     sei.hInstApp=NULL; /* Out */
156     sei.lpIDList=NULL;
157     sei.lpClass=NULL;
158     sei.hkeyClass=NULL;
159     sei.dwHotKey=0;
160     U(sei).hIcon=NULL;
161     sei.hProcess=NULL; /* Out */
162
163     DeleteFile(child_file);
164     SetLastError(0xcafebabe);
165     success=ShellExecuteEx(&sei);
166     rc=(int)sei.hInstApp;
167     ok((success && rc > 32) || (!success && rc <= 32),
168        "%s rc=%d and hInstApp=%d is not allowed\n", shell_call, success, rc);
169
170     if (rc > 32)
171     {
172         int wait_rc;
173         if (sei.hProcess!=NULL)
174         {
175             wait_rc=WaitForSingleObject(sei.hProcess, 5000);
176             ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject(hProcess) returned %d\n", wait_rc);
177         }
178         wait_rc=WaitForSingleObject(hEvent, 5000);
179         ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
180     }
181     /* The child process may have changed the result file, so let profile
182      * functions know about it
183      */
184     WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
185     if (rc > 32)
186         dump_child();
187
188     return rc;
189 }
190
191
192
193 /***
194  *
195  * Functions to create / delete associations wrappers
196  *
197  ***/
198
199 static void create_test_association(const char* extension)
200 {
201     HKEY hkey, hkey_shell;
202     char class[MAX_PATH];
203     LONG rc;
204
205     sprintf(class, "shlexec%s", extension);
206     rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, extension, 0, NULL, 0, KEY_SET_VALUE,
207                       NULL, &hkey, NULL);
208     assert(rc==ERROR_SUCCESS);
209     rc=RegSetValueEx(hkey, NULL, 0, REG_SZ, (LPBYTE) class, strlen(class)+1);
210     assert(rc==ERROR_SUCCESS);
211     CloseHandle(hkey);
212
213     rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, class, 0, NULL, 0,
214                       KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS, NULL, &hkey, NULL);
215     assert(rc==ERROR_SUCCESS);
216     rc=RegCreateKeyEx(hkey, "shell", 0, NULL, 0,
217                       KEY_CREATE_SUB_KEY, NULL, &hkey_shell, NULL);
218     assert(rc==ERROR_SUCCESS);
219     CloseHandle(hkey);
220     CloseHandle(hkey_shell);
221 }
222
223 static void delete_test_association(const char* extension)
224 {
225     char class[MAX_PATH];
226
227     sprintf(class, "shlexec%s", extension);
228     SHDeleteKey(HKEY_CLASSES_ROOT, class);
229     SHDeleteKey(HKEY_CLASSES_ROOT, extension);
230 }
231
232 static void create_test_verb(const char* extension, const char* verb,
233                              int rawcmd, const char* cmdtail)
234 {
235     HKEY hkey_shell, hkey_verb, hkey_cmd;
236     char shell[MAX_PATH];
237     char* cmd;
238     LONG rc;
239
240     sprintf(shell, "shlexec%s\\shell", extension);
241     rc=RegOpenKeyEx(HKEY_CLASSES_ROOT, shell, 0,
242                     KEY_CREATE_SUB_KEY, &hkey_shell);
243     assert(rc==ERROR_SUCCESS);
244     rc=RegCreateKeyEx(hkey_shell, verb, 0, NULL, 0, KEY_CREATE_SUB_KEY,
245                       NULL, &hkey_verb, NULL);
246     assert(rc==ERROR_SUCCESS);
247     rc=RegCreateKeyEx(hkey_verb, "command", 0, NULL, 0, KEY_SET_VALUE,
248                       NULL, &hkey_cmd, NULL);
249     assert(rc==ERROR_SUCCESS);
250
251     if (rawcmd)
252     {
253         rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmdtail, strlen(cmdtail)+1);
254     }
255     else
256     {
257         cmd=malloc(strlen(argv0)+10+strlen(child_file)+2+strlen(cmdtail)+1);
258         sprintf(cmd,"%s shlexec \"%s\" %s", argv0, child_file, cmdtail);
259         rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmd, strlen(cmd)+1);
260         assert(rc==ERROR_SUCCESS);
261         free(cmd);
262     }
263
264     CloseHandle(hkey_shell);
265     CloseHandle(hkey_verb);
266     CloseHandle(hkey_cmd);
267 }
268
269
270
271 /***
272  *
273  * Functions to check that the child process was started just right
274  * (borrowed from dlls/kernel32/tests/process.c)
275  *
276  ***/
277
278 static const char* encodeA(const char* str)
279 {
280     static char encoded[2*1024+1];
281     char*       ptr;
282     size_t      len,i;
283
284     if (!str) return "";
285     len = strlen(str) + 1;
286     if (len >= sizeof(encoded)/2)
287     {
288         fprintf(stderr, "string is too long!\n");
289         assert(0);
290     }
291     ptr = encoded;
292     for (i = 0; i < len; i++)
293         sprintf(&ptr[i * 2], "%02x", (unsigned char)str[i]);
294     ptr[2 * len] = '\0';
295     return ptr;
296 }
297
298 static unsigned decode_char(char c)
299 {
300     if (c >= '0' && c <= '9') return c - '0';
301     if (c >= 'a' && c <= 'f') return c - 'a' + 10;
302     assert(c >= 'A' && c <= 'F');
303     return c - 'A' + 10;
304 }
305
306 static char* decodeA(const char* str)
307 {
308     static char decoded[1024];
309     char*       ptr;
310     size_t      len,i;
311
312     len = strlen(str) / 2;
313     if (!len--) return NULL;
314     if (len >= sizeof(decoded))
315     {
316         fprintf(stderr, "string is too long!\n");
317         assert(0);
318     }
319     ptr = decoded;
320     for (i = 0; i < len; i++)
321         ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
322     ptr[len] = '\0';
323     return ptr;
324 }
325
326 static void     childPrintf(HANDLE h, const char* fmt, ...)
327 {
328     va_list     valist;
329     char        buffer[1024];
330     DWORD       w;
331
332     va_start(valist, fmt);
333     vsprintf(buffer, fmt, valist);
334     va_end(valist);
335     WriteFile(h, buffer, strlen(buffer), &w, NULL);
336 }
337
338 static void doChild(int argc, char** argv)
339 {
340     char* filename;
341     HANDLE hFile;
342     int i;
343
344     filename=argv[2];
345     hFile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
346     if (hFile == INVALID_HANDLE_VALUE)
347         return;
348
349     /* Arguments */
350     childPrintf(hFile, "[Arguments]\r\n");
351     if (winetest_debug > 2)
352         trace("argcA=%d\n", argc);
353     childPrintf(hFile, "argcA=%d\r\n", argc);
354     for (i = 0; i < argc; i++)
355     {
356         if (winetest_debug > 2)
357             trace("argvA%d=%s\n", i, argv[i]);
358         childPrintf(hFile, "argvA%d=%s\r\n", i, encodeA(argv[i]));
359     }
360     CloseHandle(hFile);
361
362     init_event(filename);
363     SetEvent(hEvent);
364     CloseHandle(hEvent);
365 }
366
367 static char* getChildString(const char* sect, const char* key)
368 {
369     char        buf[1024];
370     char*       ret;
371
372     GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), child_file);
373     if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
374     assert(!(strlen(buf) & 1));
375     ret = decodeA(buf);
376     return ret;
377 }
378
379 static void dump_child(void)
380 {
381     if (winetest_debug > 1)
382     {
383         char key[18];
384         char* str;
385         int i, c;
386
387         c=GetPrivateProfileIntA("Arguments", "argcA", -1, child_file);
388         trace("argcA=%d\n",c);
389         for (i=0;i<c;i++)
390         {
391             sprintf(key, "argvA%d", i);
392             str=getChildString("Arguments", key);
393             trace("%s=%s\n", key, str);
394         }
395     }
396 }
397
398 static int StrCmpPath(const char* s1, const char* s2)
399 {
400     if (!s1 && !s2) return 0;
401     if (!s2) return 1;
402     if (!s1) return -1;
403     while (*s1)
404     {
405         if (!*s2)
406         {
407             if (*s1=='.')
408                 s1++;
409             return (*s1-*s2);
410         }
411         if ((*s1=='/' || *s1=='\\') && (*s2=='/' || *s2=='\\'))
412         {
413             while (*s1=='/' || *s1=='\\')
414                 s1++;
415             while (*s2=='/' || *s2=='\\')
416                 s2++;
417         }
418         else if (toupper(*s1)==toupper(*s2))
419         {
420             s1++;
421             s2++;
422         }
423         else
424         {
425             return (*s1-*s2);
426         }
427     }
428     if (*s2=='.')
429         s2++;
430     if (*s2)
431         return -1;
432     return 0;
433 }
434
435 static int _okChildString(const char* file, int line, const char* key, const char* expected)
436 {
437     char* result;
438     result=getChildString("Arguments", key);
439     return ok_(file, line)(lstrcmpiA(result, expected) == 0,
440                "%s expected '%s', got '%s'\n", key, expected, result);
441 }
442
443 static int _okChildPath(const char* file, int line, const char* key, const char* expected)
444 {
445     char* result;
446     result=getChildString("Arguments", key);
447     return ok_(file, line)(StrCmpPath(result, expected) == 0,
448                "%s expected '%s', got '%s'\n", key, expected, result);
449 }
450
451 static int _okChildInt(const char* file, int line, const char* key, int expected)
452 {
453     INT result;
454     result=GetPrivateProfileIntA("Arguments", key, expected, child_file);
455     return ok_(file, line)(result == expected,
456                "%s expected %d, but got %d\n", key, expected, result);
457 }
458
459 #define okChildString(key, expected) _okChildString(__FILE__, __LINE__, (key), (expected))
460 #define okChildPath(key, expected) _okChildPath(__FILE__, __LINE__, (key), (expected))
461 #define okChildInt(key, expected)    _okChildInt(__FILE__, __LINE__, (key), (expected))
462
463
464
465 /***
466  *
467  * Tests
468  *
469  ***/
470
471 static const char* testfiles[]=
472 {
473     "%s\\test file.shlexec",
474     "%s\\%%nasty%% $file.shlexec",
475     "%s\\test file.noassoc",
476     "%s\\test file.noassoc.shlexec",
477     "%s\\test file.shlexec.noassoc",
478     "%s\\test_shortcut_shlexec.lnk",
479     "%s\\test_shortcut_exe.lnk",
480     "%s\\test file.shl",
481     "%s\\test file.shlfoo",
482     "%s\\test file.sfe",
483     "%s\\masked file.shlexec",
484     "%s\\masked",
485     NULL
486 };
487
488 typedef struct
489 {
490     const char* verb;
491     const char* basename;
492     int todo;
493     int rc;
494 } filename_tests_t;
495
496 static filename_tests_t filename_tests[]=
497 {
498     /* Test bad / nonexistent filenames */
499     {NULL,           "%s\\nonexistent.shlexec", 0x11, SE_ERR_FNF},
500     {NULL,           "%s\\nonexistent.noassoc", 0x11, SE_ERR_FNF},
501
502     /* Standard tests */
503     {NULL,           "%s\\test file.shlexec",   0x0, 33},
504     {NULL,           "%s\\test file.shlexec.",  0x0, 33},
505     {NULL,           "%s\\%%nasty%% $file.shlexec", 0x0, 33},
506     {NULL,           "%s/test file.shlexec",    0x0, 33},
507
508     /* Test filenames with no association */
509     {NULL,           "%s\\test file.noassoc",   0x0,  SE_ERR_NOASSOC},
510
511     /* Test double extensions */
512     {NULL,           "%s\\test file.noassoc.shlexec", 0x0, 33},
513     {NULL,           "%s\\test file.shlexec.noassoc", 0x0, SE_ERR_NOASSOC},
514
515     /* Test alternate verbs */
516     {"LowerL",       "%s\\nonexistent.shlexec", 0x11, SE_ERR_FNF},
517     {"LowerL",       "%s\\test file.noassoc",   0x0,  SE_ERR_NOASSOC},
518
519     {"QuotedLowerL", "%s\\test file.shlexec",   0x0, 33},
520     {"QuotedUpperL", "%s\\test file.shlexec",   0x0, 33},
521
522     /* Test file masked due to space */
523     {NULL,           "%s\\masked file.shlexec",   0x1, 33},
524     /* Test if quoting prevents the masking */
525     {NULL,           "%s\\masked file.shlexec",   0x40, 33},
526
527     {NULL, NULL, 0}
528 };
529
530 static filename_tests_t noquotes_tests[]=
531 {
532     /* Test unquoted '%1' thingies */
533     {"NoQuotes",     "%s\\test file.shlexec",   0xa, 33},
534     {"LowerL",       "%s\\test file.shlexec",   0xa, 33},
535     {"UpperL",       "%s\\test file.shlexec",   0xa, 33},
536
537     {NULL, NULL, 0}
538 };
539
540 static void test_filename(void)
541 {
542     char filename[MAX_PATH];
543     const filename_tests_t* test;
544     char* c;
545     int rc;
546
547     test=filename_tests;
548     while (test->basename)
549     {
550         sprintf(filename, test->basename, tmpdir);
551         if (strchr(filename, '/'))
552         {
553             c=filename;
554             while (*c)
555             {
556                 if (*c=='\\')
557                     *c='/';
558                 c++;
559             }
560         }
561         if ((test->todo & 0x40)==0)
562         {
563             rc=shell_execute(test->verb, filename, NULL, NULL);
564         }
565         else
566         {
567             char quoted[MAX_PATH + 2];
568             sprintf(quoted, "\"%s\"", filename);
569             rc=shell_execute(test->verb, quoted, NULL, NULL);
570         }
571         if (rc > 32)
572             rc=33;
573         if ((test->todo & 0x1)==0)
574         {
575             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
576                rc, GetLastError());
577         }
578         else todo_wine
579         {
580             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
581                rc, GetLastError());
582         }
583         if (rc == 33)
584         {
585             const char* verb;
586             if ((test->todo & 0x2)==0)
587             {
588                 okChildInt("argcA", 5);
589             }
590             else todo_wine
591             {
592                 okChildInt("argcA", 5);
593             }
594             verb=(test->verb ? test->verb : "Open");
595             if ((test->todo & 0x4)==0)
596             {
597                 okChildString("argvA3", verb);
598             }
599             else todo_wine
600             {
601                 okChildString("argvA3", verb);
602             }
603             if ((test->todo & 0x8)==0)
604             {
605                 okChildPath("argvA4", filename);
606             }
607             else todo_wine
608             {
609                 okChildPath("argvA4", filename);
610             }
611         }
612         test++;
613     }
614
615     test=noquotes_tests;
616     while (test->basename)
617     {
618         sprintf(filename, test->basename, tmpdir);
619         rc=shell_execute(test->verb, filename, NULL, NULL);
620         if (rc > 32)
621             rc=33;
622         if ((test->todo & 0x1)==0)
623         {
624             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
625                rc, GetLastError());
626         }
627         else todo_wine
628         {
629             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
630                rc, GetLastError());
631         }
632         if (rc==0)
633         {
634             int count;
635             const char* verb;
636             char* str;
637
638             verb=(test->verb ? test->verb : "Open");
639             if ((test->todo & 0x4)==0)
640             {
641                 okChildString("argvA3", verb);
642             }
643             else todo_wine
644             {
645                 okChildString("argvA3", verb);
646             }
647
648             count=4;
649             str=filename;
650             while (1)
651             {
652                 char attrib[18];
653                 char* space;
654                 space=strchr(str, ' ');
655                 if (space)
656                     *space='\0';
657                 sprintf(attrib, "argvA%d", count);
658                 if ((test->todo & 0x8)==0)
659                 {
660                     okChildPath(attrib, str);
661                 }
662                 else todo_wine
663                 {
664                     okChildPath(attrib, str);
665                 }
666                 count++;
667                 if (!space)
668                     break;
669                 str=space+1;
670             }
671             if ((test->todo & 0x2)==0)
672             {
673                 okChildInt("argcA", count);
674             }
675             else todo_wine
676             {
677                 okChildInt("argcA", count);
678             }
679         }
680         test++;
681     }
682
683     if (dllver.dwMajorVersion != 0)
684     {
685         /* The more recent versions of shell32.dll accept quoted filenames
686          * while older ones (e.g. 4.00) don't. Still we want to test this
687          * because IE 6 depends on the new behavior.
688          * One day we may need to check the exact version of the dll but for
689          * now making sure DllGetVersion() is present is sufficient.
690          */
691         sprintf(filename, "\"%s\\test file.shlexec\"", tmpdir);
692         rc=shell_execute(NULL, filename, NULL, NULL);
693         ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
694            GetLastError());
695         okChildInt("argcA", 5);
696         okChildString("argvA3", "Open");
697         sprintf(filename, "%s\\test file.shlexec", tmpdir);
698         okChildPath("argvA4", filename);
699     }
700 }
701
702 static void test_find_executable(void)
703 {
704     char filename[MAX_PATH];
705     char command[MAX_PATH];
706     const filename_tests_t* test;
707     int rc;
708
709     create_test_association(".sfe");
710     create_test_verb(".sfe", "Open", 1, "%1");
711
712     /* Don't test FindExecutable(..., NULL), it always crashes */
713
714     strcpy(command, "your word");
715     rc=(int)FindExecutableA(NULL, NULL, command);
716     ok(rc == SE_ERR_FNF || rc > 32 /* nt4 */, "FindExecutable(NULL) returned %d\n", rc);
717     ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
718
719     strcpy(command, "your word");
720     rc=(int)FindExecutableA(tmpdir, NULL, command);
721     ok(rc == SE_ERR_NOASSOC /* >= win2000 */ || rc > 32 /* win98, nt4 */, "FindExecutable(NULL) returned %d\n", rc);
722     ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
723
724     sprintf(filename, "%s\\test file.sfe", tmpdir);
725     rc=(int)FindExecutableA(filename, NULL, command);
726     ok(rc > 32, "FindExecutable(%s) returned %d\n", filename, rc);
727     /* Depending on the platform, command could be '%1' or 'test file.sfe' */
728
729     rc=(int)FindExecutableA("test file.sfe", tmpdir, command);
730     ok(rc > 32, "FindExecutable(%s) returned %d\n", filename, rc);
731
732     rc=(int)FindExecutableA("test file.sfe", NULL, command);
733     todo_wine ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %d\n", filename, rc);
734
735     delete_test_association(".sfe");
736
737     create_test_association(".shl");
738     create_test_verb(".shl", "Open", 0, "Open");
739
740     sprintf(filename, "%s\\test file.shl", tmpdir);
741     rc=(int)FindExecutableA(filename, NULL, command);
742     ok(rc == SE_ERR_FNF /* NT4 */ || rc > 32, "FindExecutable(%s) returned %d\n", filename, rc);
743
744     sprintf(filename, "%s\\test file.shlfoo", tmpdir);
745     rc=(int)FindExecutableA(filename, NULL, command);
746
747     delete_test_association(".shl");
748
749     if (rc > 32)
750     {
751         /* On Windows XP and 2003 FindExecutable() is completely broken.
752          * Probably what it does is convert the filename to 8.3 format,
753          * which as a side effect converts the '.shlfoo' extension to '.shl',
754          * and then tries to find an association for '.shl'. This means it
755          * will normally fail on most extensions with more than 3 characters,
756          * like '.mpeg', etc.
757          * Also it means we cannot do any other test.
758          */
759         trace("FindExecutable() is broken -> skipping 4+ character extension tests\n");
760         return;
761     }
762
763     test=filename_tests;
764     while (test->basename)
765     {
766         sprintf(filename, test->basename, tmpdir);
767         if (strchr(filename, '/'))
768         {
769             char* c;
770             c=filename;
771             while (*c)
772             {
773                 if (*c=='\\')
774                     *c='/';
775                 c++;
776             }
777         }
778         /* Win98 does not '\0'-terminate command! */
779         memset(command, '\0', sizeof(command));
780         rc=(int)FindExecutableA(filename, NULL, command);
781         if (rc > 32)
782             rc=33;
783         if ((test->todo & 0x10)==0)
784         {
785             ok(rc==test->rc, "FindExecutable(%s) failed: rc=%d\n", filename, rc);
786         }
787         else todo_wine
788         {
789             ok(rc==test->rc, "FindExecutable(%s) failed: rc=%d\n", filename, rc);
790         }
791         if (rc > 32)
792         {
793             int equal;
794             equal=strcmp(command, argv0) == 0 ||
795                 /* NT4 returns an extra 0x8 character! */
796                 (strlen(command) == strlen(argv0)+1 && strncmp(command, argv0, strlen(argv0)) == 0);
797             if ((test->todo & 0x20)==0)
798             {
799                 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
800                    filename, command, argv0);
801             }
802             else todo_wine
803             {
804                 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
805                    filename, command, argv0);
806             }
807         }
808         test++;
809     }
810 }
811
812
813 static filename_tests_t lnk_tests[]=
814 {
815     /* Pass bad / nonexistent filenames as a parameter */
816     {NULL, "%s\\nonexistent.shlexec",    0xa, 33},
817     {NULL, "%s\\nonexistent.noassoc",    0xa, 33},
818
819     /* Pass regular paths as a parameter */
820     {NULL, "%s\\test file.shlexec",      0xa, 33},
821     {NULL, "%s/%%nasty%% $file.shlexec", 0xa, 33},
822
823     /* Pass filenames with no association as a parameter */
824     {NULL, "%s\\test file.noassoc",      0xa, 33},
825
826     {NULL, NULL, 0}
827 };
828
829 static void test_lnks(void)
830 {
831     char filename[MAX_PATH];
832     char params[MAX_PATH];
833     const filename_tests_t* test;
834     int rc;
835
836     sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
837     rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
838     ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
839        GetLastError());
840     okChildInt("argcA", 5);
841     okChildString("argvA3", "Open");
842     sprintf(filename, "%s\\test file.shlexec", tmpdir);
843     okChildPath("argvA4", filename);
844
845     sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
846     rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
847     ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
848        GetLastError());
849     okChildInt("argcA", 4);
850     okChildString("argvA3", "Lnk");
851
852     if (dllver.dwMajorVersion>=6)
853     {
854         char* c;
855        /* Recent versions of shell32.dll accept '/'s in shortcut paths.
856          * Older versions don't or are quite buggy in this regard.
857          */
858         sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
859         c=filename;
860         while (*c)
861         {
862             if (*c=='\\')
863                 *c='/';
864             c++;
865         }
866         rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
867         ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
868            GetLastError());
869         okChildInt("argcA", 4);
870         okChildString("argvA3", "Lnk");
871     }
872
873     sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
874     test=lnk_tests;
875     while (test->basename)
876     {
877         params[0]='\"';
878         sprintf(params+1, test->basename, tmpdir);
879         strcat(params,"\"");
880         rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, params,
881                             NULL);
882         if (rc > 32)
883             rc=33;
884         if ((test->todo & 0x1)==0)
885         {
886             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
887                rc, GetLastError());
888         }
889         else todo_wine
890         {
891             ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
892                rc, GetLastError());
893         }
894         if (rc==0)
895         {
896             if ((test->todo & 0x2)==0)
897             {
898                 okChildInt("argcA", 5);
899             }
900             else 
901             {
902                 okChildInt("argcA", 5);
903             }
904             if ((test->todo & 0x4)==0)
905             {
906                 okChildString("argvA3", "Lnk");
907             }
908             else todo_wine
909             {
910                 okChildString("argvA3", "Lnk");
911             }
912             sprintf(params, test->basename, tmpdir);
913             if ((test->todo & 0x8)==0)
914             {
915                 okChildPath("argvA4", params);
916             }
917             else
918             {
919                 okChildPath("argvA4", params);
920             }
921         }
922         test++;
923     }
924 }
925
926
927 static void test_exes(void)
928 {
929     char filename[MAX_PATH];
930     char params[1024];
931     int rc;
932
933     sprintf(params, "shlexec \"%s\" Exec", child_file);
934
935     /* We need NOZONECHECKS on Win2003 to block a dialog */
936     rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
937                         NULL);
938     ok(rc > 32, "%s returned %d\n", shell_call, rc);
939     okChildInt("argcA", 4);
940     okChildString("argvA3", "Exec");
941
942     sprintf(filename, "%s\\test file.noassoc", tmpdir);
943     if (CopyFile(argv0, filename, FALSE))
944     {
945         rc=shell_execute(NULL, filename, params, NULL);
946         todo_wine {
947         ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
948         }
949     }
950 }
951
952 static void test_exes_long(void)
953 {
954     char filename[MAX_PATH];
955     char params[2024];
956     char longparam[MAX_PATH];
957     int rc;
958
959     for (rc = 0; rc < MAX_PATH; rc++)
960         longparam[rc]='a'+rc%26;
961     longparam[MAX_PATH-1]=0;
962
963
964     sprintf(params, "shlexec \"%s\" %s", child_file,longparam);
965
966     /* We need NOZONECHECKS on Win2003 to block a dialog */
967     rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
968                         NULL);
969     ok(rc > 32, "%s returned %d\n", shell_call, rc);
970     okChildInt("argcA", 4);
971     okChildString("argvA3", longparam);
972
973     sprintf(filename, "%s\\test file.noassoc", tmpdir);
974     if (CopyFile(argv0, filename, FALSE))
975     {
976         rc=shell_execute(NULL, filename, params, NULL);
977         todo_wine {
978         ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
979         }
980     }
981 }
982
983
984 static void init_test(void)
985 {
986     HMODULE hdll;
987     HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO*);
988     char filename[MAX_PATH];
989     WCHAR lnkfile[MAX_PATH];
990     char params[1024];
991     const char* const * testfile;
992     lnk_desc_t desc;
993     DWORD rc;
994     HRESULT r;
995
996     hdll=GetModuleHandleA("shell32.dll");
997     pDllGetVersion=(void*)GetProcAddress(hdll, "DllGetVersion");
998     if (pDllGetVersion)
999     {
1000         dllver.cbSize=sizeof(dllver);
1001         pDllGetVersion(&dllver);
1002         trace("major=%d minor=%d build=%d platform=%d\n",
1003               dllver.dwMajorVersion, dllver.dwMinorVersion,
1004               dllver.dwBuildNumber, dllver.dwPlatformID);
1005     }
1006     else
1007     {
1008         memset(&dllver, 0, sizeof(dllver));
1009     }
1010
1011     r = CoInitialize(NULL);
1012     ok(SUCCEEDED(r), "CoInitialize failed (0x%08x)\n", r);
1013     if (!SUCCEEDED(r))
1014         exit(1);
1015
1016     rc=GetModuleFileName(NULL, argv0, sizeof(argv0));
1017     assert(rc!=0 && rc<sizeof(argv0));
1018     if (GetFileAttributes(argv0)==INVALID_FILE_ATTRIBUTES)
1019     {
1020         strcat(argv0, ".so");
1021         ok(GetFileAttributes(argv0)!=INVALID_FILE_ATTRIBUTES,
1022            "unable to find argv0!\n");
1023     }
1024
1025     GetTempPathA(sizeof(tmpdir)/sizeof(*tmpdir), tmpdir);
1026     assert(GetTempFileNameA(tmpdir, "wt", 0, child_file)!=0);
1027     init_event(child_file);
1028
1029     /* Set up the test files */
1030     testfile=testfiles;
1031     while (*testfile)
1032     {
1033         HANDLE hfile;
1034
1035         sprintf(filename, *testfile, tmpdir);
1036         hfile=CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1037                      FILE_ATTRIBUTE_NORMAL, NULL);
1038         if (hfile==INVALID_HANDLE_VALUE)
1039         {
1040             trace("unable to create '%s': err=%d\n", filename, GetLastError());
1041             assert(0);
1042         }
1043         CloseHandle(hfile);
1044         testfile++;
1045     }
1046
1047     /* Setup the test shortcuts */
1048     sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
1049     MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
1050     desc.description=NULL;
1051     desc.workdir=NULL;
1052     sprintf(filename, "%s\\test file.shlexec", tmpdir);
1053     desc.path=filename;
1054     desc.pidl=NULL;
1055     desc.arguments="ignored";
1056     desc.showcmd=0;
1057     desc.icon=NULL;
1058     desc.icon_id=0;
1059     desc.hotkey=0;
1060     create_lnk(lnkfile, &desc, 0);
1061
1062     sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1063     MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
1064     desc.description=NULL;
1065     desc.workdir=NULL;
1066     desc.path=argv0;
1067     desc.pidl=NULL;
1068     sprintf(params, "shlexec \"%s\" Lnk", child_file);
1069     desc.arguments=params;
1070     desc.showcmd=0;
1071     desc.icon=NULL;
1072     desc.icon_id=0;
1073     desc.hotkey=0;
1074     create_lnk(lnkfile, &desc, 0);
1075
1076     /* Create a basic association suitable for most tests */
1077     create_test_association(".shlexec");
1078     create_test_verb(".shlexec", "Open", 0, "Open \"%1\"");
1079     create_test_verb(".shlexec", "NoQuotes", 0, "NoQuotes %1");
1080     create_test_verb(".shlexec", "LowerL", 0, "LowerL %l");
1081     create_test_verb(".shlexec", "QuotedLowerL", 0, "QuotedLowerL \"%l\"");
1082     create_test_verb(".shlexec", "UpperL", 0, "UpperL %L");
1083     create_test_verb(".shlexec", "QuotedUpperL", 0, "QuotedUpperL \"%L\"");
1084 }
1085
1086 static void cleanup_test(void)
1087 {
1088     char filename[MAX_PATH];
1089     const char* const * testfile;
1090
1091     /* Delete the test files */
1092     testfile=testfiles;
1093     while (*testfile)
1094     {
1095         sprintf(filename, *testfile, tmpdir);
1096         DeleteFile(filename);
1097         testfile++;
1098     }
1099     DeleteFile(child_file);
1100
1101     /* Delete the test association */
1102     delete_test_association(".shlexec");
1103
1104     CloseHandle(hEvent);
1105
1106     CoUninitialize();
1107 }
1108
1109 START_TEST(shlexec)
1110 {
1111
1112     myARGC = winetest_get_mainargs(&myARGV);
1113     if (myARGC >= 3)
1114     {
1115         doChild(myARGC, myARGV);
1116         exit(0);
1117     }
1118
1119     init_test();
1120
1121     test_filename();
1122     test_find_executable();
1123     test_lnks();
1124     test_exes();
1125     test_exes_long();
1126
1127     cleanup_test();
1128 }