2 * Unit test of the ShellExecute function.
4 * Copyright 2005 Francois Gouget for CodeWeavers
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.
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.
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
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
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
38 /* Needed to get SEE_MASK_NOZONECHECKS with the PSDK */
39 #define NTDDI_WINXPSP1 0x05010100
40 #define NTDDI_VERSION NTDDI_WINXPSP1
47 #include "wine/test.h"
49 #include "shell32_test.h"
52 static char argv0[MAX_PATH];
55 static char tmpdir[MAX_PATH];
56 static char child_file[MAX_PATH];
57 static DLLVERSIONINFO dllver;
62 * ShellExecute wrappers
65 static void dump_child(void);
68 static void init_event(const char* child_file)
71 event_name=strrchr(child_file, '\\')+1;
72 hEvent=CreateEvent(NULL, FALSE, FALSE, event_name);
75 static void strcat_param(char* str, const char* param)
89 static char shell_call[2048]="";
90 static int shell_execute(LPCSTR operation, LPCSTR file, LPCSTR parameters, LPCSTR directory)
94 strcpy(shell_call, "ShellExecute(");
95 strcat_param(shell_call, operation);
96 strcat(shell_call, ", ");
97 strcat_param(shell_call, file);
98 strcat(shell_call, ", ");
99 strcat_param(shell_call, parameters);
100 strcat(shell_call, ", ");
101 strcat_param(shell_call, directory);
102 strcat(shell_call, ")");
103 if (winetest_debug > 1)
104 trace("%s\n", shell_call);
106 DeleteFile(child_file);
107 SetLastError(0xcafebabe);
109 /* FIXME: We cannot use ShellExecuteEx() here because if there is no
110 * association it displays the 'Open With' dialog and I could not find
111 * a flag to prevent this.
113 rc=(int)ShellExecute(NULL, operation, file, parameters, directory,
119 wait_rc=WaitForSingleObject(hEvent, 5000);
120 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
122 /* The child process may have changed the result file, so let profile
123 * functions know about it
125 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
132 static int shell_execute_ex(DWORD mask, LPCSTR operation, LPCSTR file,
133 LPCSTR parameters, LPCSTR directory)
135 SHELLEXECUTEINFO sei;
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);
151 sei.cbSize=sizeof(sei);
152 sei.fMask=SEE_MASK_NOCLOSEPROCESS | mask;
154 sei.lpVerb=operation;
156 sei.lpParameters=parameters;
157 sei.lpDirectory=directory;
158 sei.nShow=SW_SHOWNORMAL;
159 sei.hInstApp=NULL; /* Out */
165 sei.hProcess=NULL; /* Out */
167 DeleteFile(child_file);
168 SetLastError(0xcafebabe);
169 success=ShellExecuteEx(&sei);
170 rc=(int)sei.hInstApp;
171 ok((success && rc > 32) || (!success && rc <= 32),
172 "%s rc=%d and hInstApp=%d is not allowed\n", shell_call, success, rc);
177 if (sei.hProcess!=NULL)
179 wait_rc=WaitForSingleObject(sei.hProcess, 5000);
180 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject(hProcess) returned %d\n", wait_rc);
182 wait_rc=WaitForSingleObject(hEvent, 5000);
183 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
185 /* The child process may have changed the result file, so let profile
186 * functions know about it
188 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
199 * Functions to create / delete associations wrappers
203 static void create_test_association(const char* extension)
205 HKEY hkey, hkey_shell;
206 char class[MAX_PATH];
209 sprintf(class, "shlexec%s", extension);
210 rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, extension, 0, NULL, 0, KEY_SET_VALUE,
212 assert(rc==ERROR_SUCCESS);
213 rc=RegSetValueEx(hkey, NULL, 0, REG_SZ, (LPBYTE) class, strlen(class)+1);
214 assert(rc==ERROR_SUCCESS);
217 rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, class, 0, NULL, 0,
218 KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS, NULL, &hkey, NULL);
219 assert(rc==ERROR_SUCCESS);
220 rc=RegCreateKeyEx(hkey, "shell", 0, NULL, 0,
221 KEY_CREATE_SUB_KEY, NULL, &hkey_shell, NULL);
222 assert(rc==ERROR_SUCCESS);
224 CloseHandle(hkey_shell);
227 static void delete_test_association(const char* extension)
229 char class[MAX_PATH];
231 sprintf(class, "shlexec%s", extension);
232 SHDeleteKey(HKEY_CLASSES_ROOT, class);
233 SHDeleteKey(HKEY_CLASSES_ROOT, extension);
236 static void create_test_verb(const char* extension, const char* verb,
237 int rawcmd, const char* cmdtail)
239 HKEY hkey_shell, hkey_verb, hkey_cmd;
240 char shell[MAX_PATH];
244 sprintf(shell, "shlexec%s\\shell", extension);
245 rc=RegOpenKeyEx(HKEY_CLASSES_ROOT, shell, 0,
246 KEY_CREATE_SUB_KEY, &hkey_shell);
247 assert(rc==ERROR_SUCCESS);
248 rc=RegCreateKeyEx(hkey_shell, verb, 0, NULL, 0, KEY_CREATE_SUB_KEY,
249 NULL, &hkey_verb, NULL);
250 assert(rc==ERROR_SUCCESS);
251 rc=RegCreateKeyEx(hkey_verb, "command", 0, NULL, 0, KEY_SET_VALUE,
252 NULL, &hkey_cmd, NULL);
253 assert(rc==ERROR_SUCCESS);
257 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmdtail, strlen(cmdtail)+1);
261 cmd=malloc(strlen(argv0)+10+strlen(child_file)+2+strlen(cmdtail)+1);
262 sprintf(cmd,"%s shlexec \"%s\" %s", argv0, child_file, cmdtail);
263 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmd, strlen(cmd)+1);
264 assert(rc==ERROR_SUCCESS);
268 CloseHandle(hkey_shell);
269 CloseHandle(hkey_verb);
270 CloseHandle(hkey_cmd);
273 static void create_test_verb_dde(const char* extension, const char* verb,
274 int rawcmd, const char* cmdtail, const char *ddeexec,
275 const char *application, const char *topic,
278 HKEY hkey_shell, hkey_verb, hkey_cmd;
279 char shell[MAX_PATH];
283 sprintf(shell, "shlexec%s\\shell", extension);
284 rc=RegOpenKeyEx(HKEY_CLASSES_ROOT, shell, 0,
285 KEY_CREATE_SUB_KEY, &hkey_shell);
286 assert(rc==ERROR_SUCCESS);
287 rc=RegCreateKeyEx(hkey_shell, verb, 0, NULL, 0, KEY_CREATE_SUB_KEY,
288 NULL, &hkey_verb, NULL);
289 assert(rc==ERROR_SUCCESS);
290 rc=RegCreateKeyEx(hkey_verb, "command", 0, NULL, 0, KEY_SET_VALUE,
291 NULL, &hkey_cmd, NULL);
292 assert(rc==ERROR_SUCCESS);
296 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmdtail, strlen(cmdtail)+1);
300 cmd=malloc(strlen(argv0)+10+strlen(child_file)+2+strlen(cmdtail)+1);
301 sprintf(cmd,"%s shlexec \"%s\" %s", argv0, child_file, cmdtail);
302 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmd, strlen(cmd)+1);
303 assert(rc==ERROR_SUCCESS);
309 HKEY hkey_ddeexec, hkey_application, hkey_topic, hkey_ifexec;
311 rc=RegCreateKeyEx(hkey_verb, "ddeexec", 0, NULL, 0, KEY_SET_VALUE |
312 KEY_CREATE_SUB_KEY, NULL, &hkey_ddeexec, NULL);
313 assert(rc==ERROR_SUCCESS);
314 rc=RegSetValueEx(hkey_ddeexec, NULL, 0, REG_SZ, (LPBYTE)ddeexec,
316 assert(rc==ERROR_SUCCESS);
319 rc=RegCreateKeyEx(hkey_ddeexec, "application", 0, NULL, 0, KEY_SET_VALUE,
320 NULL, &hkey_application, NULL);
321 assert(rc==ERROR_SUCCESS);
322 rc=RegSetValueEx(hkey_application, NULL, 0, REG_SZ, (LPBYTE)application,
323 strlen(application)+1);
324 assert(rc==ERROR_SUCCESS);
325 CloseHandle(hkey_application);
329 rc=RegCreateKeyEx(hkey_ddeexec, "topic", 0, NULL, 0, KEY_SET_VALUE,
330 NULL, &hkey_topic, NULL);
331 assert(rc==ERROR_SUCCESS);
332 rc=RegSetValueEx(hkey_topic, NULL, 0, REG_SZ, (LPBYTE)topic,
334 assert(rc==ERROR_SUCCESS);
335 CloseHandle(hkey_topic);
339 rc=RegCreateKeyEx(hkey_ddeexec, "ifexec", 0, NULL, 0, KEY_SET_VALUE,
340 NULL, &hkey_ifexec, NULL);
341 assert(rc==ERROR_SUCCESS);
342 rc=RegSetValueEx(hkey_ifexec, NULL, 0, REG_SZ, (LPBYTE)ifexec,
344 assert(rc==ERROR_SUCCESS);
345 CloseHandle(hkey_ifexec);
347 CloseHandle(hkey_ddeexec);
350 CloseHandle(hkey_shell);
351 CloseHandle(hkey_verb);
352 CloseHandle(hkey_cmd);
357 * Functions to check that the child process was started just right
358 * (borrowed from dlls/kernel32/tests/process.c)
362 static const char* encodeA(const char* str)
364 static char encoded[2*1024+1];
369 len = strlen(str) + 1;
370 if (len >= sizeof(encoded)/2)
372 fprintf(stderr, "string is too long!\n");
376 for (i = 0; i < len; i++)
377 sprintf(&ptr[i * 2], "%02x", (unsigned char)str[i]);
382 static unsigned decode_char(char c)
384 if (c >= '0' && c <= '9') return c - '0';
385 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
386 assert(c >= 'A' && c <= 'F');
390 static char* decodeA(const char* str)
392 static char decoded[1024];
396 len = strlen(str) / 2;
397 if (!len--) return NULL;
398 if (len >= sizeof(decoded))
400 fprintf(stderr, "string is too long!\n");
404 for (i = 0; i < len; i++)
405 ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
410 static void childPrintf(HANDLE h, const char* fmt, ...)
416 va_start(valist, fmt);
417 vsprintf(buffer, fmt, valist);
419 WriteFile(h, buffer, strlen(buffer), &w, NULL);
422 static void doChild(int argc, char** argv)
429 hFile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
430 if (hFile == INVALID_HANDLE_VALUE)
434 childPrintf(hFile, "[Arguments]\r\n");
435 if (winetest_debug > 2)
436 trace("argcA=%d\n", argc);
437 childPrintf(hFile, "argcA=%d\r\n", argc);
438 for (i = 0; i < argc; i++)
440 if (winetest_debug > 2)
441 trace("argvA%d=%s\n", i, argv[i]);
442 childPrintf(hFile, "argvA%d=%s\r\n", i, encodeA(argv[i]));
446 init_event(filename);
451 static char* getChildString(const char* sect, const char* key)
456 GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), child_file);
457 if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
458 assert(!(strlen(buf) & 1));
463 static void dump_child(void)
465 if (winetest_debug > 1)
471 c=GetPrivateProfileIntA("Arguments", "argcA", -1, child_file);
472 trace("argcA=%d\n",c);
475 sprintf(key, "argvA%d", i);
476 str=getChildString("Arguments", key);
477 trace("%s=%s\n", key, str);
482 static int StrCmpPath(const char* s1, const char* s2)
484 if (!s1 && !s2) return 0;
495 if ((*s1=='/' || *s1=='\\') && (*s2=='/' || *s2=='\\'))
497 while (*s1=='/' || *s1=='\\')
499 while (*s2=='/' || *s2=='\\')
502 else if (toupper(*s1)==toupper(*s2))
519 static int _okChildString(const char* file, int line, const char* key, const char* expected)
522 result=getChildString("Arguments", key);
523 return ok_(file, line)(lstrcmpiA(result, expected) == 0,
524 "%s expected '%s', got '%s'\n", key, expected, result);
527 static int _okChildPath(const char* file, int line, const char* key, const char* expected)
530 result=getChildString("Arguments", key);
531 return ok_(file, line)(StrCmpPath(result, expected) == 0,
532 "%s expected '%s', got '%s'\n", key, expected, result);
535 static int _okChildInt(const char* file, int line, const char* key, int expected)
538 result=GetPrivateProfileIntA("Arguments", key, expected, child_file);
539 return ok_(file, line)(result == expected,
540 "%s expected %d, but got %d\n", key, expected, result);
543 #define okChildString(key, expected) _okChildString(__FILE__, __LINE__, (key), (expected))
544 #define okChildPath(key, expected) _okChildPath(__FILE__, __LINE__, (key), (expected))
545 #define okChildInt(key, expected) _okChildInt(__FILE__, __LINE__, (key), (expected))
555 static const char* testfiles[]=
557 "%s\\test file.shlexec",
558 "%s\\%%nasty%% $file.shlexec",
559 "%s\\test file.noassoc",
560 "%s\\test file.noassoc.shlexec",
561 "%s\\test file.shlexec.noassoc",
562 "%s\\test_shortcut_shlexec.lnk",
563 "%s\\test_shortcut_exe.lnk",
565 "%s\\test file.shlfoo",
567 "%s\\masked file.shlexec",
578 const char* basename;
583 static filename_tests_t filename_tests[]=
585 /* Test bad / nonexistent filenames */
586 {NULL, "%s\\nonexistent.shlexec", 0x11, SE_ERR_FNF},
587 {NULL, "%s\\nonexistent.noassoc", 0x11, SE_ERR_FNF},
590 {NULL, "%s\\test file.shlexec", 0x0, 33},
591 {NULL, "%s\\test file.shlexec.", 0x0, 33},
592 {NULL, "%s\\%%nasty%% $file.shlexec", 0x0, 33},
593 {NULL, "%s/test file.shlexec", 0x0, 33},
595 /* Test filenames with no association */
596 {NULL, "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
598 /* Test double extensions */
599 {NULL, "%s\\test file.noassoc.shlexec", 0x0, 33},
600 {NULL, "%s\\test file.shlexec.noassoc", 0x0, SE_ERR_NOASSOC},
602 /* Test alternate verbs */
603 {"LowerL", "%s\\nonexistent.shlexec", 0x11, SE_ERR_FNF},
604 {"LowerL", "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
606 {"QuotedLowerL", "%s\\test file.shlexec", 0x0, 33},
607 {"QuotedUpperL", "%s\\test file.shlexec", 0x0, 33},
609 /* Test file masked due to space */
610 {NULL, "%s\\masked file.shlexec", 0x1, 33},
611 /* Test if quoting prevents the masking */
612 {NULL, "%s\\masked file.shlexec", 0x40, 33},
617 static filename_tests_t noquotes_tests[]=
619 /* Test unquoted '%1' thingies */
620 {"NoQuotes", "%s\\test file.shlexec", 0xa, 33},
621 {"LowerL", "%s\\test file.shlexec", 0xa, 33},
622 {"UpperL", "%s\\test file.shlexec", 0xa, 33},
627 static void test_filename(void)
629 char filename[MAX_PATH];
630 const filename_tests_t* test;
635 while (test->basename)
637 sprintf(filename, test->basename, tmpdir);
638 if (strchr(filename, '/'))
648 if ((test->todo & 0x40)==0)
650 rc=shell_execute(test->verb, filename, NULL, NULL);
654 char quoted[MAX_PATH + 2];
655 sprintf(quoted, "\"%s\"", filename);
656 rc=shell_execute(test->verb, quoted, NULL, NULL);
660 if ((test->todo & 0x1)==0)
662 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
667 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
673 if ((test->todo & 0x2)==0)
675 okChildInt("argcA", 5);
679 okChildInt("argcA", 5);
681 verb=(test->verb ? test->verb : "Open");
682 if ((test->todo & 0x4)==0)
684 okChildString("argvA3", verb);
688 okChildString("argvA3", verb);
690 if ((test->todo & 0x8)==0)
692 okChildPath("argvA4", filename);
696 okChildPath("argvA4", filename);
703 while (test->basename)
705 sprintf(filename, test->basename, tmpdir);
706 rc=shell_execute(test->verb, filename, NULL, NULL);
709 if ((test->todo & 0x1)==0)
711 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
716 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
725 verb=(test->verb ? test->verb : "Open");
726 if ((test->todo & 0x4)==0)
728 okChildString("argvA3", verb);
732 okChildString("argvA3", verb);
741 space=strchr(str, ' ');
744 sprintf(attrib, "argvA%d", count);
745 if ((test->todo & 0x8)==0)
747 okChildPath(attrib, str);
751 okChildPath(attrib, str);
758 if ((test->todo & 0x2)==0)
760 okChildInt("argcA", count);
764 okChildInt("argcA", count);
770 if (dllver.dwMajorVersion != 0)
772 /* The more recent versions of shell32.dll accept quoted filenames
773 * while older ones (e.g. 4.00) don't. Still we want to test this
774 * because IE 6 depends on the new behavior.
775 * One day we may need to check the exact version of the dll but for
776 * now making sure DllGetVersion() is present is sufficient.
778 sprintf(filename, "\"%s\\test file.shlexec\"", tmpdir);
779 rc=shell_execute(NULL, filename, NULL, NULL);
780 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
782 okChildInt("argcA", 5);
783 okChildString("argvA3", "Open");
784 sprintf(filename, "%s\\test file.shlexec", tmpdir);
785 okChildPath("argvA4", filename);
789 static void test_find_executable(void)
791 char filename[MAX_PATH];
792 char command[MAX_PATH];
793 const filename_tests_t* test;
796 create_test_association(".sfe");
797 create_test_verb(".sfe", "Open", 1, "%1");
799 /* Don't test FindExecutable(..., NULL), it always crashes */
801 strcpy(command, "your word");
802 rc=(int)FindExecutableA(NULL, NULL, command);
803 ok(rc == SE_ERR_FNF || rc > 32 /* nt4 */, "FindExecutable(NULL) returned %d\n", rc);
804 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
806 strcpy(command, "your word");
807 rc=(int)FindExecutableA(tmpdir, NULL, command);
808 ok(rc == SE_ERR_NOASSOC /* >= win2000 */ || rc > 32 /* win98, nt4 */, "FindExecutable(NULL) returned %d\n", rc);
809 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
811 sprintf(filename, "%s\\test file.sfe", tmpdir);
812 rc=(int)FindExecutableA(filename, NULL, command);
813 ok(rc > 32, "FindExecutable(%s) returned %d\n", filename, rc);
814 /* Depending on the platform, command could be '%1' or 'test file.sfe' */
816 rc=(int)FindExecutableA("test file.sfe", tmpdir, command);
817 ok(rc > 32, "FindExecutable(%s) returned %d\n", filename, rc);
819 rc=(int)FindExecutableA("test file.sfe", NULL, command);
820 todo_wine ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %d\n", filename, rc);
822 delete_test_association(".sfe");
824 create_test_association(".shl");
825 create_test_verb(".shl", "Open", 0, "Open");
827 sprintf(filename, "%s\\test file.shl", tmpdir);
828 rc=(int)FindExecutableA(filename, NULL, command);
829 ok(rc == SE_ERR_FNF /* NT4 */ || rc > 32, "FindExecutable(%s) returned %d\n", filename, rc);
831 sprintf(filename, "%s\\test file.shlfoo", tmpdir);
832 rc=(int)FindExecutableA(filename, NULL, command);
834 delete_test_association(".shl");
838 /* On Windows XP and 2003 FindExecutable() is completely broken.
839 * Probably what it does is convert the filename to 8.3 format,
840 * which as a side effect converts the '.shlfoo' extension to '.shl',
841 * and then tries to find an association for '.shl'. This means it
842 * will normally fail on most extensions with more than 3 characters,
844 * Also it means we cannot do any other test.
846 trace("FindExecutable() is broken -> skipping 4+ character extension tests\n");
851 while (test->basename)
853 sprintf(filename, test->basename, tmpdir);
854 if (strchr(filename, '/'))
865 /* Win98 does not '\0'-terminate command! */
866 memset(command, '\0', sizeof(command));
867 rc=(int)FindExecutableA(filename, NULL, command);
870 if ((test->todo & 0x10)==0)
872 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%d\n", filename, rc);
876 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%d\n", filename, rc);
881 equal=strcmp(command, argv0) == 0 ||
882 /* NT4 returns an extra 0x8 character! */
883 (strlen(command) == strlen(argv0)+1 && strncmp(command, argv0, strlen(argv0)) == 0);
884 if ((test->todo & 0x20)==0)
886 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
887 filename, command, argv0);
891 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
892 filename, command, argv0);
900 static filename_tests_t lnk_tests[]=
902 /* Pass bad / nonexistent filenames as a parameter */
903 {NULL, "%s\\nonexistent.shlexec", 0xa, 33},
904 {NULL, "%s\\nonexistent.noassoc", 0xa, 33},
906 /* Pass regular paths as a parameter */
907 {NULL, "%s\\test file.shlexec", 0xa, 33},
908 {NULL, "%s/%%nasty%% $file.shlexec", 0xa, 33},
910 /* Pass filenames with no association as a parameter */
911 {NULL, "%s\\test file.noassoc", 0xa, 33},
916 static void test_lnks(void)
918 char filename[MAX_PATH];
919 char params[MAX_PATH];
920 const filename_tests_t* test;
923 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
924 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
925 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
927 okChildInt("argcA", 5);
928 okChildString("argvA3", "Open");
929 sprintf(filename, "%s\\test file.shlexec", tmpdir);
930 okChildPath("argvA4", filename);
932 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
933 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
934 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
936 okChildInt("argcA", 4);
937 okChildString("argvA3", "Lnk");
939 if (dllver.dwMajorVersion>=6)
942 /* Recent versions of shell32.dll accept '/'s in shortcut paths.
943 * Older versions don't or are quite buggy in this regard.
945 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
953 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
954 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
956 okChildInt("argcA", 4);
957 okChildString("argvA3", "Lnk");
960 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
962 while (test->basename)
965 sprintf(params+1, test->basename, tmpdir);
967 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, params,
971 if ((test->todo & 0x1)==0)
973 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
978 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
983 if ((test->todo & 0x2)==0)
985 okChildInt("argcA", 5);
989 okChildInt("argcA", 5);
991 if ((test->todo & 0x4)==0)
993 okChildString("argvA3", "Lnk");
997 okChildString("argvA3", "Lnk");
999 sprintf(params, test->basename, tmpdir);
1000 if ((test->todo & 0x8)==0)
1002 okChildPath("argvA4", params);
1006 okChildPath("argvA4", params);
1014 static void test_exes(void)
1016 char filename[MAX_PATH];
1020 sprintf(params, "shlexec \"%s\" Exec", child_file);
1022 /* We need NOZONECHECKS on Win2003 to block a dialog */
1023 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
1025 ok(rc > 32, "%s returned %d\n", shell_call, rc);
1026 okChildInt("argcA", 4);
1027 okChildString("argvA3", "Exec");
1029 sprintf(filename, "%s\\test file.noassoc", tmpdir);
1030 if (CopyFile(argv0, filename, FALSE))
1032 rc=shell_execute(NULL, filename, params, NULL);
1034 ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
1039 static void test_exes_long(void)
1041 char filename[MAX_PATH];
1043 char longparam[MAX_PATH];
1046 for (rc = 0; rc < MAX_PATH; rc++)
1047 longparam[rc]='a'+rc%26;
1048 longparam[MAX_PATH-1]=0;
1051 sprintf(params, "shlexec \"%s\" %s", child_file,longparam);
1053 /* We need NOZONECHECKS on Win2003 to block a dialog */
1054 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
1056 ok(rc > 32, "%s returned %d\n", shell_call, rc);
1057 okChildInt("argcA", 4);
1058 okChildString("argvA3", longparam);
1060 sprintf(filename, "%s\\test file.noassoc", tmpdir);
1061 if (CopyFile(argv0, filename, FALSE))
1063 rc=shell_execute(NULL, filename, params, NULL);
1065 ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
1072 const char* command;
1073 const char* ddeexec;
1074 const char* application;
1077 BOOL bExpectCmdLine;
1078 const char* expectedDdeExec;
1083 static dde_tests_t dde_tests[] =
1085 /* Test passing and not passing command-line
1086 * argument, no DDE */
1087 {"", NULL, NULL, NULL, NULL, FALSE, "", 0x0, 33},
1088 {"\"%1\"", NULL, NULL, NULL, NULL, TRUE, "", 0x0, 33},
1090 /* Test passing and not passing command-line
1091 * argument, with DDE */
1092 {"", "[open(\"%1\")]", "shlexec", "dde", NULL, FALSE, "[open(\"%s\")]", 0x0, 33},
1093 {"\"%1\"", "[open(\"%1\")]", "shlexec", "dde", NULL, TRUE, "[open(\"%s\")]", 0x0, 33},
1095 /* Test ifexec precedence over ddeexec */
1096 {"", "[open(\"%1\")]", "shlexec", "dde", "[ifexec(\"%1\")]", FALSE, "[ifexec(\"%s\")]", 0x0, 33},
1098 /* Test default DDE topic */
1099 {"", "[open(\"%1\")]", "shlexec", NULL, NULL, FALSE, "[open(\"%s\")]", 0x0, 33},
1101 /* Test default DDE application */
1102 {"", "[open(\"%1\")]", NULL, "dde", NULL, FALSE, "[open(\"%s\")]", 0x1, 33},
1104 {NULL, NULL, NULL, NULL, 0x0, 0}
1107 static DWORD ddeInst;
1108 static HSZ hszTopic;
1109 static char ddeExec[MAX_PATH], ddeApplication[MAX_PATH];
1110 static BOOL denyNextConnection;
1112 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
1113 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
1114 ULONG_PTR dwData1, ULONG_PTR dwData2)
1118 if (winetest_debug > 2)
1119 trace("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
1120 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
1125 if (!DdeCmpStringHandles(hsz1, hszTopic))
1127 if (denyNextConnection)
1128 denyNextConnection = FALSE;
1131 size = DdeQueryString(ddeInst, hsz2, ddeApplication, MAX_PATH, CP_WINANSI);
1132 assert(size < MAX_PATH);
1133 return (HDDEDATA)TRUE;
1136 return (HDDEDATA)FALSE;
1139 size = DdeGetData(hData, (LPBYTE)ddeExec, MAX_PATH, 0L);
1140 assert(size < MAX_PATH);
1141 DdeFreeDataHandle(hData);
1142 return (HDDEDATA)DDE_FACK;
1145 return (HDDEDATA)NULL;
1152 DWORD threadIdParent;
1153 } dde_thread_info_t;
1155 static DWORD CALLBACK ddeThread(LPVOID arg)
1157 dde_thread_info_t *info = (dde_thread_info_t *)arg;
1160 assert(info && info->filename);
1161 rc=shell_execute(NULL, info->filename, NULL, NULL);
1162 PostThreadMessage(info->threadIdParent, WM_QUIT, 0, 0L);
1166 /* ShellExecute won't succesfully send DDE commands to console applications after starting them,
1167 * so we run a DDE server in this application, deny the first connection request to make
1168 * ShellExecute start the application, and then process the next DDE connection in this application
1169 * to see the execute command that is sent. */
1170 static void test_dde(void)
1172 char filename[MAX_PATH], defApplication[MAX_PATH];
1174 dde_thread_info_t info = { filename, GetCurrentThreadId() };
1175 const dde_tests_t* test;
1182 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
1183 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
1184 assert(rc == DMLERR_NO_ERROR);
1186 sprintf(filename, "%s\\test file.sde", tmpdir);
1188 /* Default service is application name minus path and extension */
1189 strcpy(defApplication, strrchr(argv0, '\\')+1);
1190 *strchr(defApplication, '.') = 0;
1193 while (test->command)
1195 create_test_association(".sde");
1196 create_test_verb_dde(".sde", "Open", 0, test->command, test->ddeexec,
1197 test->application, test->topic, test->ifexec);
1198 hszApplication = DdeCreateStringHandleA(ddeInst, test->application ?
1199 test->application : defApplication, CP_WINANSI);
1200 hszTopic = DdeCreateStringHandleA(ddeInst, test->topic ? test->topic : SZDDESYS_TOPIC,
1202 assert(hszApplication && hszTopic);
1203 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER));
1204 denyNextConnection = TRUE;
1207 hThread = CreateThread(NULL, 0, ddeThread, (LPVOID)&info, 0, NULL);
1209 while (GetMessage(&msg, NULL, 0, 0))
1211 /* Need a message loop for DDE server */
1212 TranslateMessage(&msg);
1213 DispatchMessage(&msg);
1215 rc = WaitForSingleObject(hThread, 5000);
1216 assert(rc == WAIT_OBJECT_0);
1218 GetExitCodeThread(hThread, (DWORD *)&rc);
1221 if ((test->todo & 0x1)==0)
1223 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1224 rc, GetLastError());
1228 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1229 rc, GetLastError());
1233 if ((test->todo & 0x2)==0)
1235 okChildInt("argcA", test->bExpectCmdLine ? 4 : 3);
1239 okChildInt("argcA", test->bExpectCmdLine ? 4 : 3);
1241 if (test->bExpectCmdLine)
1243 if ((test->todo & 0x4) == 0)
1245 okChildPath("argvA3", filename);
1249 okChildPath("argvA3", filename);
1252 if ((test->todo & 0x8) == 0)
1254 sprintf(params, test->expectedDdeExec, filename);
1255 ok(StrCmpPath(params, ddeExec) == 0,
1256 "ddeexec expected '%s', got '%s'\n", params, ddeExec);
1260 sprintf(params, test->expectedDdeExec, filename);
1261 ok(StrCmpPath(params, ddeExec) == 0,
1262 "ddeexec expected '%s', got '%s'\n", params, ddeExec);
1266 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
1267 assert(DdeFreeStringHandle(ddeInst, hszTopic));
1268 assert(DdeFreeStringHandle(ddeInst, hszApplication));
1269 delete_test_association(".sde");
1273 assert(DdeUninitialize(ddeInst));
1278 const char* command;
1279 const char* expectedDdeApplication;
1282 } dde_default_app_tests_t;
1284 static dde_default_app_tests_t dde_default_app_tests[] =
1286 /* Test unquoted existing filename with a space */
1287 {"%s\\test file.exe", "test file", 0x1, 33},
1288 {"%s\\test file.exe param", "test file", 0x1, 33},
1290 /* Test quoted existing filename with a space */
1291 {"\"%s\\test file.exe\"", "test file", 0x1, 33},
1292 {"\"%s\\test file.exe\" param", "test file", 0x1, 33},
1294 /* Test unquoted filename with a space that doesn't exist, but
1296 {"%s\\test2 file.exe", "test2", 0x1, 33},
1297 {"%s\\test2 file.exe param", "test2", 0x1, 33},
1299 /* Test quoted filename with a space that does not exist */
1300 {"\"%s\\test2 file.exe\"", "", 0x1, 5},
1301 {"\"%s\\test2 file.exe\" param", "", 0x1, 5},
1303 /* Test filename supplied without the extension */
1304 {"%s\\test2", "test2", 0x1, 33},
1305 {"%s\\test2 param", "test2", 0x1, 33},
1307 /* Test an unquoted non-existent filename */
1308 {"%s\\notexist.exe", "", 0x1, 5},
1309 {"%s\\notexist.exe param", "", 0x1, 5},
1311 /* Test an application that will be found on the path */
1312 {"cmd", "cmd", 0x1, 33},
1313 {"cmd param", "cmd", 0x1, 33},
1315 /* Test an application that will not be found on the path */
1316 {"xyzwxyzwxyz", "", 0x1, 5},
1317 {"xyzwxyzwxyz param", "", 0x1, 5},
1322 static void test_dde_default_app(void)
1324 char filename[MAX_PATH];
1326 dde_thread_info_t info = { filename, GetCurrentThreadId() };
1327 const dde_default_app_tests_t* test;
1334 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
1335 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
1336 assert(rc == DMLERR_NO_ERROR);
1338 sprintf(filename, "%s\\test file.sde", tmpdir);
1340 /* It is strictly not necessary to register an application name here, but wine's
1341 * DdeNameService implementation complains if 0L is passed instead of
1342 * hszApplication with DNS_FILTEROFF */
1343 hszApplication = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
1344 hszTopic = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
1345 assert(hszApplication && hszTopic);
1346 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER | DNS_FILTEROFF));
1348 test = dde_default_app_tests;
1349 while (test->command)
1351 create_test_association(".sde");
1352 sprintf(params, test->command, tmpdir);
1353 create_test_verb_dde(".sde", "Open", 1, params, "[test]", NULL,
1355 denyNextConnection = FALSE;
1356 ddeApplication[0] = 0;
1358 /* No application will be run as we will respond to the first DDE event,
1359 * so don't wait for it */
1362 hThread = CreateThread(NULL, 0, ddeThread, (LPVOID)&info, 0, NULL);
1364 while (GetMessage(&msg, NULL, 0, 0))
1366 /* Need a message loop for DDE server */
1367 TranslateMessage(&msg);
1368 DispatchMessage(&msg);
1370 rc = WaitForSingleObject(hThread, 5000);
1371 assert(rc == WAIT_OBJECT_0);
1373 GetExitCodeThread(hThread, (DWORD *)&rc);
1376 if ((test->todo & 0x1)==0)
1378 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1379 rc, GetLastError());
1383 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1384 rc, GetLastError());
1388 if ((test->todo & 0x2)==0)
1390 ok(!strcmp(ddeApplication, test->expectedDdeApplication),
1391 "Expected application '%s', got '%s'\n",
1392 test->expectedDdeApplication, ddeApplication);
1396 ok(!strcmp(ddeApplication, test->expectedDdeApplication),
1397 "Expected application '%s', got '%s'\n",
1398 test->expectedDdeApplication, ddeApplication);
1402 delete_test_association(".sde");
1406 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
1407 assert(DdeFreeStringHandle(ddeInst, hszTopic));
1408 assert(DdeFreeStringHandle(ddeInst, hszApplication));
1409 assert(DdeUninitialize(ddeInst));
1412 static void init_test(void)
1415 HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO*);
1416 char filename[MAX_PATH];
1417 WCHAR lnkfile[MAX_PATH];
1419 const char* const * testfile;
1424 hdll=GetModuleHandleA("shell32.dll");
1425 pDllGetVersion=(void*)GetProcAddress(hdll, "DllGetVersion");
1428 dllver.cbSize=sizeof(dllver);
1429 pDllGetVersion(&dllver);
1430 trace("major=%d minor=%d build=%d platform=%d\n",
1431 dllver.dwMajorVersion, dllver.dwMinorVersion,
1432 dllver.dwBuildNumber, dllver.dwPlatformID);
1436 memset(&dllver, 0, sizeof(dllver));
1439 r = CoInitialize(NULL);
1440 ok(SUCCEEDED(r), "CoInitialize failed (0x%08x)\n", r);
1444 rc=GetModuleFileName(NULL, argv0, sizeof(argv0));
1445 assert(rc!=0 && rc<sizeof(argv0));
1446 if (GetFileAttributes(argv0)==INVALID_FILE_ATTRIBUTES)
1448 strcat(argv0, ".so");
1449 ok(GetFileAttributes(argv0)!=INVALID_FILE_ATTRIBUTES,
1450 "unable to find argv0!\n");
1453 GetTempPathA(sizeof(tmpdir)/sizeof(*tmpdir), tmpdir);
1454 assert(GetTempFileNameA(tmpdir, "wt", 0, child_file)!=0);
1455 init_event(child_file);
1457 /* Set up the test files */
1463 sprintf(filename, *testfile, tmpdir);
1464 hfile=CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1465 FILE_ATTRIBUTE_NORMAL, NULL);
1466 if (hfile==INVALID_HANDLE_VALUE)
1468 trace("unable to create '%s': err=%d\n", filename, GetLastError());
1475 /* Setup the test shortcuts */
1476 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
1477 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
1478 desc.description=NULL;
1480 sprintf(filename, "%s\\test file.shlexec", tmpdir);
1483 desc.arguments="ignored";
1488 create_lnk(lnkfile, &desc, 0);
1490 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1491 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
1492 desc.description=NULL;
1496 sprintf(params, "shlexec \"%s\" Lnk", child_file);
1497 desc.arguments=params;
1502 create_lnk(lnkfile, &desc, 0);
1504 /* Create a basic association suitable for most tests */
1505 create_test_association(".shlexec");
1506 create_test_verb(".shlexec", "Open", 0, "Open \"%1\"");
1507 create_test_verb(".shlexec", "NoQuotes", 0, "NoQuotes %1");
1508 create_test_verb(".shlexec", "LowerL", 0, "LowerL %l");
1509 create_test_verb(".shlexec", "QuotedLowerL", 0, "QuotedLowerL \"%l\"");
1510 create_test_verb(".shlexec", "UpperL", 0, "UpperL %L");
1511 create_test_verb(".shlexec", "QuotedUpperL", 0, "QuotedUpperL \"%L\"");
1514 static void cleanup_test(void)
1516 char filename[MAX_PATH];
1517 const char* const * testfile;
1519 /* Delete the test files */
1523 sprintf(filename, *testfile, tmpdir);
1524 DeleteFile(filename);
1527 DeleteFile(child_file);
1529 /* Delete the test association */
1530 delete_test_association(".shlexec");
1532 CloseHandle(hEvent);
1540 myARGC = winetest_get_mainargs(&myARGV);
1543 doChild(myARGC, myARGV);
1550 test_find_executable();
1555 test_dde_default_app();