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
41 #define _WIN32_WINNT 0x0501
48 #include "wine/test.h"
50 #include "shell32_test.h"
53 static char argv0[MAX_PATH];
56 static char tmpdir[MAX_PATH];
57 static char child_file[MAX_PATH];
58 static DLLVERSIONINFO dllver;
63 * ShellExecute wrappers
66 static void dump_child(void);
69 static void init_event(const char* child_file)
72 event_name=strrchr(child_file, '\\')+1;
73 hEvent=CreateEvent(NULL, FALSE, FALSE, event_name);
76 static void strcat_param(char* str, const char* param)
90 static char shell_call[2048]="";
91 static int shell_execute(LPCSTR operation, LPCSTR file, LPCSTR parameters, LPCSTR directory)
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);
107 DeleteFile(child_file);
108 SetLastError(0xcafebabe);
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.
114 rc=(int)ShellExecute(NULL, operation, file, parameters, directory,
120 wait_rc=WaitForSingleObject(hEvent, 5000);
121 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
123 /* The child process may have changed the result file, so let profile
124 * functions know about it
126 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
133 static int shell_execute_ex(DWORD mask, LPCSTR operation, LPCSTR file,
134 LPCSTR parameters, LPCSTR directory)
136 SHELLEXECUTEINFO sei;
140 strcpy(shell_call, "ShellExecuteEx(");
141 strcat_param(shell_call, operation);
142 strcat(shell_call, ", ");
143 strcat_param(shell_call, file);
144 strcat(shell_call, ", ");
145 strcat_param(shell_call, parameters);
146 strcat(shell_call, ", ");
147 strcat_param(shell_call, directory);
148 strcat(shell_call, ")");
149 if (winetest_debug > 1)
150 trace("%s\n", shell_call);
152 sei.cbSize=sizeof(sei);
153 sei.fMask=SEE_MASK_NOCLOSEPROCESS | mask;
155 sei.lpVerb=operation;
157 sei.lpParameters=parameters;
158 sei.lpDirectory=directory;
159 sei.nShow=SW_SHOWNORMAL;
160 sei.hInstApp=NULL; /* Out */
166 sei.hProcess=NULL; /* Out */
168 DeleteFile(child_file);
169 SetLastError(0xcafebabe);
170 success=ShellExecuteEx(&sei);
171 rc=(int)sei.hInstApp;
172 ok((success && rc > 32) || (!success && rc <= 32),
173 "%s rc=%d and hInstApp=%d is not allowed\n", shell_call, success, rc);
178 if (sei.hProcess!=NULL)
180 wait_rc=WaitForSingleObject(sei.hProcess, 5000);
181 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject(hProcess) returned %d\n", wait_rc);
183 wait_rc=WaitForSingleObject(hEvent, 5000);
184 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
186 /* The child process may have changed the result file, so let profile
187 * functions know about it
189 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
200 * Functions to create / delete associations wrappers
204 static BOOL create_test_association(const char* extension)
206 HKEY hkey, hkey_shell;
207 char class[MAX_PATH];
210 sprintf(class, "shlexec%s", extension);
211 rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, extension, 0, NULL, 0, KEY_SET_VALUE,
213 if (rc != ERROR_SUCCESS)
216 rc=RegSetValueEx(hkey, NULL, 0, REG_SZ, (LPBYTE) class, strlen(class)+1);
217 ok(rc==ERROR_SUCCESS, "RegSetValueEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
220 rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, class, 0, NULL, 0,
221 KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS, NULL, &hkey, NULL);
222 ok(rc==ERROR_SUCCESS, "RegCreateKeyEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
224 rc=RegCreateKeyEx(hkey, "shell", 0, NULL, 0,
225 KEY_CREATE_SUB_KEY, NULL, &hkey_shell, NULL);
226 ok(rc==ERROR_SUCCESS, "RegCreateKeyEx 'shell' failed, expected ERROR_SUCCESS, got %d\n", rc);
229 CloseHandle(hkey_shell);
234 /* Based on RegDeleteTreeW from dlls/advapi32/registry.c */
235 static LSTATUS myRegDeleteTreeA(HKEY hKey, LPCSTR lpszSubKey)
238 DWORD dwMaxSubkeyLen, dwMaxValueLen;
239 DWORD dwMaxLen, dwSize;
240 CHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
245 ret = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
249 /* Get highest length for keys, values */
250 ret = RegQueryInfoKeyA(hSubKey, NULL, NULL, NULL, NULL,
251 &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
252 if (ret) goto cleanup;
256 dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
257 if (dwMaxLen > sizeof(szNameBuf)/sizeof(CHAR))
259 /* Name too big: alloc a buffer for it */
260 if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(CHAR))))
262 ret = ERROR_NOT_ENOUGH_MEMORY;
268 /* Recursively delete all the subkeys */
272 if (RegEnumKeyExA(hSubKey, 0, lpszName, &dwSize, NULL,
273 NULL, NULL, NULL)) break;
275 ret = myRegDeleteTreeA(hSubKey, lpszName);
276 if (ret) goto cleanup;
280 ret = RegDeleteKeyA(hKey, lpszSubKey);
285 if (RegEnumValueA(hKey, 0, lpszName, &dwSize,
286 NULL, NULL, NULL, NULL)) break;
288 ret = RegDeleteValueA(hKey, lpszName);
289 if (ret) goto cleanup;
293 /* Free buffer if allocated */
294 if (lpszName != szNameBuf)
295 HeapFree( GetProcessHeap(), 0, lpszName);
297 RegCloseKey(hSubKey);
301 static void delete_test_association(const char* extension)
303 char class[MAX_PATH];
305 sprintf(class, "shlexec%s", extension);
306 myRegDeleteTreeA(HKEY_CLASSES_ROOT, class);
307 myRegDeleteTreeA(HKEY_CLASSES_ROOT, extension);
310 static void create_test_verb_dde(const char* extension, const char* verb,
311 int rawcmd, const char* cmdtail, const char *ddeexec,
312 const char *application, const char *topic,
315 HKEY hkey_shell, hkey_verb, hkey_cmd;
316 char shell[MAX_PATH];
320 sprintf(shell, "shlexec%s\\shell", extension);
321 rc=RegOpenKeyEx(HKEY_CLASSES_ROOT, shell, 0,
322 KEY_CREATE_SUB_KEY, &hkey_shell);
323 assert(rc==ERROR_SUCCESS);
324 rc=RegCreateKeyEx(hkey_shell, verb, 0, NULL, 0, KEY_CREATE_SUB_KEY,
325 NULL, &hkey_verb, NULL);
326 assert(rc==ERROR_SUCCESS);
327 rc=RegCreateKeyEx(hkey_verb, "command", 0, NULL, 0, KEY_SET_VALUE,
328 NULL, &hkey_cmd, NULL);
329 assert(rc==ERROR_SUCCESS);
333 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmdtail, strlen(cmdtail)+1);
337 cmd=malloc(strlen(argv0)+10+strlen(child_file)+2+strlen(cmdtail)+1);
338 sprintf(cmd,"%s shlexec \"%s\" %s", argv0, child_file, cmdtail);
339 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmd, strlen(cmd)+1);
340 assert(rc==ERROR_SUCCESS);
346 HKEY hkey_ddeexec, hkey_application, hkey_topic, hkey_ifexec;
348 rc=RegCreateKeyEx(hkey_verb, "ddeexec", 0, NULL, 0, KEY_SET_VALUE |
349 KEY_CREATE_SUB_KEY, NULL, &hkey_ddeexec, NULL);
350 assert(rc==ERROR_SUCCESS);
351 rc=RegSetValueEx(hkey_ddeexec, NULL, 0, REG_SZ, (LPBYTE)ddeexec,
353 assert(rc==ERROR_SUCCESS);
356 rc=RegCreateKeyEx(hkey_ddeexec, "application", 0, NULL, 0, KEY_SET_VALUE,
357 NULL, &hkey_application, NULL);
358 assert(rc==ERROR_SUCCESS);
359 rc=RegSetValueEx(hkey_application, NULL, 0, REG_SZ, (LPBYTE)application,
360 strlen(application)+1);
361 assert(rc==ERROR_SUCCESS);
362 CloseHandle(hkey_application);
366 rc=RegCreateKeyEx(hkey_ddeexec, "topic", 0, NULL, 0, KEY_SET_VALUE,
367 NULL, &hkey_topic, NULL);
368 assert(rc==ERROR_SUCCESS);
369 rc=RegSetValueEx(hkey_topic, NULL, 0, REG_SZ, (LPBYTE)topic,
371 assert(rc==ERROR_SUCCESS);
372 CloseHandle(hkey_topic);
376 rc=RegCreateKeyEx(hkey_ddeexec, "ifexec", 0, NULL, 0, KEY_SET_VALUE,
377 NULL, &hkey_ifexec, NULL);
378 assert(rc==ERROR_SUCCESS);
379 rc=RegSetValueEx(hkey_ifexec, NULL, 0, REG_SZ, (LPBYTE)ifexec,
381 assert(rc==ERROR_SUCCESS);
382 CloseHandle(hkey_ifexec);
384 CloseHandle(hkey_ddeexec);
387 CloseHandle(hkey_shell);
388 CloseHandle(hkey_verb);
389 CloseHandle(hkey_cmd);
392 static void create_test_verb(const char* extension, const char* verb,
393 int rawcmd, const char* cmdtail)
395 create_test_verb_dde(extension, verb, rawcmd, cmdtail, NULL, NULL,
401 * Functions to check that the child process was started just right
402 * (borrowed from dlls/kernel32/tests/process.c)
406 static const char* encodeA(const char* str)
408 static char encoded[2*1024+1];
413 len = strlen(str) + 1;
414 if (len >= sizeof(encoded)/2)
416 fprintf(stderr, "string is too long!\n");
420 for (i = 0; i < len; i++)
421 sprintf(&ptr[i * 2], "%02x", (unsigned char)str[i]);
426 static unsigned decode_char(char c)
428 if (c >= '0' && c <= '9') return c - '0';
429 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
430 assert(c >= 'A' && c <= 'F');
434 static char* decodeA(const char* str)
436 static char decoded[1024];
440 len = strlen(str) / 2;
441 if (!len--) return NULL;
442 if (len >= sizeof(decoded))
444 fprintf(stderr, "string is too long!\n");
448 for (i = 0; i < len; i++)
449 ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
454 static void childPrintf(HANDLE h, const char* fmt, ...)
460 va_start(valist, fmt);
461 vsprintf(buffer, fmt, valist);
463 WriteFile(h, buffer, strlen(buffer), &w, NULL);
466 static void doChild(int argc, char** argv)
473 hFile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
474 if (hFile == INVALID_HANDLE_VALUE)
478 childPrintf(hFile, "[Arguments]\r\n");
479 if (winetest_debug > 2)
480 trace("argcA=%d\n", argc);
481 childPrintf(hFile, "argcA=%d\r\n", argc);
482 for (i = 0; i < argc; i++)
484 if (winetest_debug > 2)
485 trace("argvA%d=%s\n", i, argv[i]);
486 childPrintf(hFile, "argvA%d=%s\r\n", i, encodeA(argv[i]));
490 init_event(filename);
495 static char* getChildString(const char* sect, const char* key)
500 GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), child_file);
501 if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
502 assert(!(strlen(buf) & 1));
507 static void dump_child(void)
509 if (winetest_debug > 1)
515 c=GetPrivateProfileIntA("Arguments", "argcA", -1, child_file);
516 trace("argcA=%d\n",c);
519 sprintf(key, "argvA%d", i);
520 str=getChildString("Arguments", key);
521 trace("%s=%s\n", key, str);
526 static int StrCmpPath(const char* s1, const char* s2)
528 if (!s1 && !s2) return 0;
539 if ((*s1=='/' || *s1=='\\') && (*s2=='/' || *s2=='\\'))
541 while (*s1=='/' || *s1=='\\')
543 while (*s2=='/' || *s2=='\\')
546 else if (toupper(*s1)==toupper(*s2))
563 static int _okChildString(const char* file, int line, const char* key, const char* expected)
566 result=getChildString("Arguments", key);
567 return ok_(file, line)(lstrcmpiA(result, expected) == 0,
568 "%s expected '%s', got '%s'\n", key, expected, result);
571 static int _okChildPath(const char* file, int line, const char* key, const char* expected)
574 result=getChildString("Arguments", key);
575 return ok_(file, line)(StrCmpPath(result, expected) == 0,
576 "%s expected '%s', got '%s'\n", key, expected, result);
579 static int _okChildInt(const char* file, int line, const char* key, int expected)
582 result=GetPrivateProfileIntA("Arguments", key, expected, child_file);
583 return ok_(file, line)(result == expected,
584 "%s expected %d, but got %d\n", key, expected, result);
587 #define okChildString(key, expected) _okChildString(__FILE__, __LINE__, (key), (expected))
588 #define okChildPath(key, expected) _okChildPath(__FILE__, __LINE__, (key), (expected))
589 #define okChildInt(key, expected) _okChildInt(__FILE__, __LINE__, (key), (expected))
599 static const char* testfiles[]=
601 "%s\\test file.shlexec",
602 "%s\\%%nasty%% $file.shlexec",
603 "%s\\test file.noassoc",
604 "%s\\test file.noassoc.shlexec",
605 "%s\\test file.shlexec.noassoc",
606 "%s\\test_shortcut_shlexec.lnk",
607 "%s\\test_shortcut_exe.lnk",
609 "%s\\test file.shlfoo",
611 "%s\\masked file.shlexec",
622 const char* basename;
627 static filename_tests_t filename_tests[]=
629 /* Test bad / nonexistent filenames */
630 {NULL, "%s\\nonexistent.shlexec", 0x11, SE_ERR_FNF},
631 {NULL, "%s\\nonexistent.noassoc", 0x11, SE_ERR_FNF},
634 {NULL, "%s\\test file.shlexec", 0x0, 33},
635 {NULL, "%s\\test file.shlexec.", 0x0, 33},
636 {NULL, "%s\\%%nasty%% $file.shlexec", 0x0, 33},
637 {NULL, "%s/test file.shlexec", 0x0, 33},
639 /* Test filenames with no association */
640 {NULL, "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
642 /* Test double extensions */
643 {NULL, "%s\\test file.noassoc.shlexec", 0x0, 33},
644 {NULL, "%s\\test file.shlexec.noassoc", 0x0, SE_ERR_NOASSOC},
646 /* Test alternate verbs */
647 {"LowerL", "%s\\nonexistent.shlexec", 0x11, SE_ERR_FNF},
648 {"LowerL", "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
650 {"QuotedLowerL", "%s\\test file.shlexec", 0x0, 33},
651 {"QuotedUpperL", "%s\\test file.shlexec", 0x0, 33},
653 /* Test file masked due to space */
654 {NULL, "%s\\masked file.shlexec", 0x1, 33},
655 /* Test if quoting prevents the masking */
656 {NULL, "%s\\masked file.shlexec", 0x40, 33},
661 static filename_tests_t noquotes_tests[]=
663 /* Test unquoted '%1' thingies */
664 {"NoQuotes", "%s\\test file.shlexec", 0xa, 33},
665 {"LowerL", "%s\\test file.shlexec", 0xa, 33},
666 {"UpperL", "%s\\test file.shlexec", 0xa, 33},
671 static void test_filename(void)
673 char filename[MAX_PATH];
674 const filename_tests_t* test;
679 while (test->basename)
681 sprintf(filename, test->basename, tmpdir);
682 if (strchr(filename, '/'))
692 if ((test->todo & 0x40)==0)
694 rc=shell_execute(test->verb, filename, NULL, NULL);
698 char quoted[MAX_PATH + 2];
699 sprintf(quoted, "\"%s\"", filename);
700 rc=shell_execute(test->verb, quoted, NULL, NULL);
704 if ((test->todo & 0x1)==0)
706 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
711 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
717 if ((test->todo & 0x2)==0)
719 okChildInt("argcA", 5);
723 okChildInt("argcA", 5);
725 verb=(test->verb ? test->verb : "Open");
726 if ((test->todo & 0x4)==0)
728 okChildString("argvA3", verb);
732 okChildString("argvA3", verb);
734 if ((test->todo & 0x8)==0)
736 okChildPath("argvA4", filename);
740 okChildPath("argvA4", filename);
747 while (test->basename)
749 sprintf(filename, test->basename, tmpdir);
750 rc=shell_execute(test->verb, filename, NULL, NULL);
753 if ((test->todo & 0x1)==0)
755 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
760 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
769 verb=(test->verb ? test->verb : "Open");
770 if ((test->todo & 0x4)==0)
772 okChildString("argvA3", verb);
776 okChildString("argvA3", verb);
785 space=strchr(str, ' ');
788 sprintf(attrib, "argvA%d", count);
789 if ((test->todo & 0x8)==0)
791 okChildPath(attrib, str);
795 okChildPath(attrib, str);
802 if ((test->todo & 0x2)==0)
804 okChildInt("argcA", count);
808 okChildInt("argcA", count);
814 if (dllver.dwMajorVersion != 0)
816 /* The more recent versions of shell32.dll accept quoted filenames
817 * while older ones (e.g. 4.00) don't. Still we want to test this
818 * because IE 6 depends on the new behavior.
819 * One day we may need to check the exact version of the dll but for
820 * now making sure DllGetVersion() is present is sufficient.
822 sprintf(filename, "\"%s\\test file.shlexec\"", tmpdir);
823 rc=shell_execute(NULL, filename, NULL, NULL);
824 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
826 okChildInt("argcA", 5);
827 okChildString("argvA3", "Open");
828 sprintf(filename, "%s\\test file.shlexec", tmpdir);
829 okChildPath("argvA4", filename);
833 static void test_find_executable(void)
835 char filename[MAX_PATH];
836 char command[MAX_PATH];
837 const filename_tests_t* test;
840 if (!create_test_association(".sfe"))
842 skip("Unable to create association for '.sfe'\n");
845 create_test_verb(".sfe", "Open", 1, "%1");
847 /* Don't test FindExecutable(..., NULL), it always crashes */
849 strcpy(command, "your word");
850 rc=(int)FindExecutableA(NULL, NULL, command);
851 ok(rc == SE_ERR_FNF || rc > 32 /* nt4 */, "FindExecutable(NULL) returned %d\n", rc);
852 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
854 strcpy(command, "your word");
855 rc=(int)FindExecutableA(tmpdir, NULL, command);
856 ok(rc == SE_ERR_NOASSOC /* >= win2000 */ || rc > 32 /* win98, nt4 */, "FindExecutable(NULL) returned %d\n", rc);
857 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
859 sprintf(filename, "%s\\test file.sfe", tmpdir);
860 rc=(int)FindExecutableA(filename, NULL, command);
861 ok(rc > 32, "FindExecutable(%s) returned %d\n", filename, rc);
862 /* Depending on the platform, command could be '%1' or 'test file.sfe' */
864 rc=(int)FindExecutableA("test file.sfe", tmpdir, command);
865 ok(rc > 32, "FindExecutable(%s) returned %d\n", filename, rc);
867 rc=(int)FindExecutableA("test file.sfe", NULL, command);
868 todo_wine ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %d\n", filename, rc);
870 delete_test_association(".sfe");
872 if (!create_test_association(".shl"))
874 skip("Unable to create association for '.shl'\n");
877 create_test_verb(".shl", "Open", 0, "Open");
879 sprintf(filename, "%s\\test file.shl", tmpdir);
880 rc=(int)FindExecutableA(filename, NULL, command);
881 ok(rc == SE_ERR_FNF /* NT4 */ || rc > 32, "FindExecutable(%s) returned %d\n", filename, rc);
883 sprintf(filename, "%s\\test file.shlfoo", tmpdir);
884 rc=(int)FindExecutableA(filename, NULL, command);
886 delete_test_association(".shl");
890 /* On Windows XP and 2003 FindExecutable() is completely broken.
891 * Probably what it does is convert the filename to 8.3 format,
892 * which as a side effect converts the '.shlfoo' extension to '.shl',
893 * and then tries to find an association for '.shl'. This means it
894 * will normally fail on most extensions with more than 3 characters,
896 * Also it means we cannot do any other test.
898 trace("FindExecutable() is broken -> skipping 4+ character extension tests\n");
903 while (test->basename)
905 sprintf(filename, test->basename, tmpdir);
906 if (strchr(filename, '/'))
917 /* Win98 does not '\0'-terminate command! */
918 memset(command, '\0', sizeof(command));
919 rc=(int)FindExecutableA(filename, NULL, command);
922 if ((test->todo & 0x10)==0)
924 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%d\n", filename, rc);
928 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%d\n", filename, rc);
933 equal=strcmp(command, argv0) == 0 ||
934 /* NT4 returns an extra 0x8 character! */
935 (strlen(command) == strlen(argv0)+1 && strncmp(command, argv0, strlen(argv0)) == 0);
936 if ((test->todo & 0x20)==0)
938 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
939 filename, command, argv0);
943 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
944 filename, command, argv0);
952 static filename_tests_t lnk_tests[]=
954 /* Pass bad / nonexistent filenames as a parameter */
955 {NULL, "%s\\nonexistent.shlexec", 0xa, 33},
956 {NULL, "%s\\nonexistent.noassoc", 0xa, 33},
958 /* Pass regular paths as a parameter */
959 {NULL, "%s\\test file.shlexec", 0xa, 33},
960 {NULL, "%s/%%nasty%% $file.shlexec", 0xa, 33},
962 /* Pass filenames with no association as a parameter */
963 {NULL, "%s\\test file.noassoc", 0xa, 33},
968 static void test_lnks(void)
970 char filename[MAX_PATH];
971 char params[MAX_PATH];
972 const filename_tests_t* test;
975 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
976 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
977 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
979 okChildInt("argcA", 5);
980 okChildString("argvA3", "Open");
981 sprintf(filename, "%s\\test file.shlexec", tmpdir);
982 okChildPath("argvA4", filename);
984 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
985 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
986 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
988 okChildInt("argcA", 4);
989 okChildString("argvA3", "Lnk");
991 if (dllver.dwMajorVersion>=6)
994 /* Recent versions of shell32.dll accept '/'s in shortcut paths.
995 * Older versions don't or are quite buggy in this regard.
997 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1005 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
1006 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
1008 okChildInt("argcA", 4);
1009 okChildString("argvA3", "Lnk");
1012 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1014 while (test->basename)
1017 sprintf(params+1, test->basename, tmpdir);
1018 strcat(params,"\"");
1019 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, params,
1023 if ((test->todo & 0x1)==0)
1025 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1026 rc, GetLastError());
1030 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1031 rc, GetLastError());
1035 if ((test->todo & 0x2)==0)
1037 okChildInt("argcA", 5);
1041 okChildInt("argcA", 5);
1043 if ((test->todo & 0x4)==0)
1045 okChildString("argvA3", "Lnk");
1049 okChildString("argvA3", "Lnk");
1051 sprintf(params, test->basename, tmpdir);
1052 if ((test->todo & 0x8)==0)
1054 okChildPath("argvA4", params);
1058 okChildPath("argvA4", params);
1066 static void test_exes(void)
1068 char filename[MAX_PATH];
1072 sprintf(params, "shlexec \"%s\" Exec", child_file);
1074 /* We need NOZONECHECKS on Win2003 to block a dialog */
1075 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
1077 ok(rc > 32, "%s returned %d\n", shell_call, rc);
1078 okChildInt("argcA", 4);
1079 okChildString("argvA3", "Exec");
1081 sprintf(filename, "%s\\test file.noassoc", tmpdir);
1082 if (CopyFile(argv0, filename, FALSE))
1084 rc=shell_execute(NULL, filename, params, NULL);
1086 ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
1091 static void test_exes_long(void)
1093 char filename[MAX_PATH];
1095 char longparam[MAX_PATH];
1098 for (rc = 0; rc < MAX_PATH; rc++)
1099 longparam[rc]='a'+rc%26;
1100 longparam[MAX_PATH-1]=0;
1103 sprintf(params, "shlexec \"%s\" %s", child_file,longparam);
1105 /* We need NOZONECHECKS on Win2003 to block a dialog */
1106 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
1108 ok(rc > 32, "%s returned %d\n", shell_call, rc);
1109 okChildInt("argcA", 4);
1110 okChildString("argvA3", longparam);
1112 sprintf(filename, "%s\\test file.noassoc", tmpdir);
1113 if (CopyFile(argv0, filename, FALSE))
1115 rc=shell_execute(NULL, filename, params, NULL);
1117 ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
1124 const char* command;
1125 const char* ddeexec;
1126 const char* application;
1130 const char* expectedDdeExec;
1135 static dde_tests_t dde_tests[] =
1137 /* Test passing and not passing command-line
1138 * argument, no DDE */
1139 {"", NULL, NULL, NULL, NULL, FALSE, "", 0x0, 33},
1140 {"\"%1\"", NULL, NULL, NULL, NULL, TRUE, "", 0x0, 33},
1142 /* Test passing and not passing command-line
1143 * argument, with DDE */
1144 {"", "[open(\"%1\")]", "shlexec", "dde", NULL, FALSE, "[open(\"%s\")]", 0x0, 33},
1145 {"\"%1\"", "[open(\"%1\")]", "shlexec", "dde", NULL, TRUE, "[open(\"%s\")]", 0x0, 33},
1147 /* Test unquoted %1 in command and ddeexec
1148 * (test filename has space) */
1149 {"%1", "[open(%1)]", "shlexec", "dde", NULL, 2, "[open(%s)]", 0x0, 33},
1151 /* Test ifexec precedence over ddeexec */
1152 {"", "[open(\"%1\")]", "shlexec", "dde", "[ifexec(\"%1\")]", FALSE, "[ifexec(\"%s\")]", 0x0, 33},
1154 /* Test default DDE topic */
1155 {"", "[open(\"%1\")]", "shlexec", NULL, NULL, FALSE, "[open(\"%s\")]", 0x0, 33},
1157 /* Test default DDE application */
1158 {"", "[open(\"%1\")]", NULL, "dde", NULL, FALSE, "[open(\"%s\")]", 0x0, 33},
1160 {NULL, NULL, NULL, NULL, NULL, 0, 0x0, 0}
1163 static DWORD ddeInst;
1164 static HSZ hszTopic;
1165 static char ddeExec[MAX_PATH], ddeApplication[MAX_PATH];
1166 static BOOL denyNextConnection;
1168 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
1169 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
1170 ULONG_PTR dwData1, ULONG_PTR dwData2)
1174 if (winetest_debug > 2)
1175 trace("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
1176 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
1181 if (!DdeCmpStringHandles(hsz1, hszTopic))
1183 if (denyNextConnection)
1184 denyNextConnection = FALSE;
1187 size = DdeQueryString(ddeInst, hsz2, ddeApplication, MAX_PATH, CP_WINANSI);
1188 assert(size < MAX_PATH);
1189 return (HDDEDATA)TRUE;
1192 return (HDDEDATA)FALSE;
1195 size = DdeGetData(hData, (LPBYTE)ddeExec, MAX_PATH, 0L);
1196 assert(size < MAX_PATH);
1197 DdeFreeDataHandle(hData);
1198 return (HDDEDATA)DDE_FACK;
1208 DWORD threadIdParent;
1209 } dde_thread_info_t;
1211 static DWORD CALLBACK ddeThread(LPVOID arg)
1213 dde_thread_info_t *info = (dde_thread_info_t *)arg;
1214 assert(info && info->filename);
1215 PostThreadMessage(info->threadIdParent,
1217 shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, info->filename, NULL, NULL),
1222 /* ShellExecute won't successfully send DDE commands to console applications after starting them,
1223 * so we run a DDE server in this application, deny the first connection request to make
1224 * ShellExecute start the application, and then process the next DDE connection in this application
1225 * to see the execute command that is sent. */
1226 static void test_dde(void)
1228 char filename[MAX_PATH], defApplication[MAX_PATH];
1230 dde_thread_info_t info = { filename, GetCurrentThreadId() };
1231 const dde_tests_t* test;
1238 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
1239 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
1240 assert(rc == DMLERR_NO_ERROR);
1242 sprintf(filename, "%s\\test file.sde", tmpdir);
1244 /* Default service is application name minus path and extension */
1245 strcpy(defApplication, strrchr(argv0, '\\')+1);
1246 *strchr(defApplication, '.') = 0;
1249 while (test->command)
1251 if (!create_test_association(".sde"))
1253 skip("Unable to create association for '.sfe'\n");
1256 create_test_verb_dde(".sde", "Open", 0, test->command, test->ddeexec,
1257 test->application, test->topic, test->ifexec);
1258 hszApplication = DdeCreateStringHandleA(ddeInst, test->application ?
1259 test->application : defApplication, CP_WINANSI);
1260 hszTopic = DdeCreateStringHandleA(ddeInst, test->topic ? test->topic : SZDDESYS_TOPIC,
1262 assert(hszApplication && hszTopic);
1263 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER));
1264 denyNextConnection = TRUE;
1267 assert(CreateThread(NULL, 0, ddeThread, (LPVOID)&info, 0, &threadId));
1268 while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);
1269 rc = msg.wParam > 32 ? 33 : msg.wParam;
1270 if ((test->todo & 0x1)==0)
1272 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1273 rc, GetLastError());
1277 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1278 rc, GetLastError());
1282 if ((test->todo & 0x2)==0)
1284 okChildInt("argcA", test->expectedArgs + 3);
1288 okChildInt("argcA", test->expectedArgs + 3);
1290 if (test->expectedArgs == 1)
1292 if ((test->todo & 0x4) == 0)
1294 okChildPath("argvA3", filename);
1298 okChildPath("argvA3", filename);
1301 if ((test->todo & 0x8) == 0)
1303 sprintf(params, test->expectedDdeExec, filename);
1304 ok(StrCmpPath(params, ddeExec) == 0,
1305 "ddeexec expected '%s', got '%s'\n", params, ddeExec);
1309 sprintf(params, test->expectedDdeExec, filename);
1310 ok(StrCmpPath(params, ddeExec) == 0,
1311 "ddeexec expected '%s', got '%s'\n", params, ddeExec);
1315 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
1316 assert(DdeFreeStringHandle(ddeInst, hszTopic));
1317 assert(DdeFreeStringHandle(ddeInst, hszApplication));
1318 delete_test_association(".sde");
1322 assert(DdeUninitialize(ddeInst));
1325 #define DDE_DEFAULT_APP_VARIANTS 2
1328 const char* command;
1329 const char* expectedDdeApplication[DDE_DEFAULT_APP_VARIANTS];
1331 int rc[DDE_DEFAULT_APP_VARIANTS];
1332 } dde_default_app_tests_t;
1334 static dde_default_app_tests_t dde_default_app_tests[] =
1336 /* Windows XP and 98 handle default DDE app names in different ways.
1337 * The application name we see in the first test determines the pattern
1338 * of application names and return codes we will look for. */
1340 /* Test unquoted existing filename with a space */
1341 {"%s\\test file.exe", {"test file", "test"}, 0x0, {33, 33}},
1342 {"%s\\test file.exe param", {"test file", "test"}, 0x0, {33, 33}},
1344 /* Test quoted existing filename with a space */
1345 {"\"%s\\test file.exe\"", {"test file", "test file"}, 0x0, {33, 33}},
1346 {"\"%s\\test file.exe\" param", {"test file", "test file"}, 0x0, {33, 33}},
1348 /* Test unquoted filename with a space that doesn't exist, but
1350 {"%s\\test2 file.exe", {"test2", "test2"}, 0x0, {33, 33}},
1351 {"%s\\test2 file.exe param", {"test2", "test2"}, 0x0, {33, 33}},
1353 /* Test quoted filename with a space that does not exist */
1354 {"\"%s\\test2 file.exe\"", {"", "test2 file"}, 0x0, {5, 33}},
1355 {"\"%s\\test2 file.exe\" param", {"", "test2 file"}, 0x0, {5, 33}},
1357 /* Test filename supplied without the extension */
1358 {"%s\\test2", {"test2", "test2"}, 0x0, {33, 33}},
1359 {"%s\\test2 param", {"test2", "test2"}, 0x0, {33, 33}},
1361 /* Test an unquoted nonexistent filename */
1362 {"%s\\notexist.exe", {"", "notexist"}, 0x0, {5, 33}},
1363 {"%s\\notexist.exe param", {"", "notexist"}, 0x0, {5, 33}},
1365 /* Test an application that will be found on the path */
1366 {"cmd", {"cmd", "cmd"}, 0x0, {33, 33}},
1367 {"cmd param", {"cmd", "cmd"}, 0x0, {33, 33}},
1369 /* Test an application that will not be found on the path */
1370 {"xyzwxyzwxyz", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
1371 {"xyzwxyzwxyz param", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
1373 {NULL, {NULL}, 0, {0}}
1376 static void test_dde_default_app(void)
1378 char filename[MAX_PATH];
1380 dde_thread_info_t info = { filename, GetCurrentThreadId() };
1381 const dde_default_app_tests_t* test;
1388 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
1389 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
1390 assert(rc == DMLERR_NO_ERROR);
1392 sprintf(filename, "%s\\test file.sde", tmpdir);
1394 /* It is strictly not necessary to register an application name here, but wine's
1395 * DdeNameService implementation complains if 0L is passed instead of
1396 * hszApplication with DNS_FILTEROFF */
1397 hszApplication = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
1398 hszTopic = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
1399 assert(hszApplication && hszTopic);
1400 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER | DNS_FILTEROFF));
1402 test = dde_default_app_tests;
1403 while (test->command)
1405 if (!create_test_association(".sde"))
1407 skip("Unable to create association for '.sde'\n");
1410 sprintf(params, test->command, tmpdir);
1411 create_test_verb_dde(".sde", "Open", 1, params, "[test]", NULL,
1413 denyNextConnection = FALSE;
1414 ddeApplication[0] = 0;
1416 /* No application will be run as we will respond to the first DDE event,
1417 * so don't wait for it */
1420 assert(CreateThread(NULL, 0, ddeThread, (LPVOID)&info, 0, &threadId));
1421 while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);
1422 rc = msg.wParam > 32 ? 33 : msg.wParam;
1424 /* First test, find which set of test data we expect to see */
1425 if (test == dde_default_app_tests)
1428 for (i=0; i<DDE_DEFAULT_APP_VARIANTS; i++)
1430 if (!strcmp(ddeApplication, test->expectedDdeApplication[i]))
1436 if (i == DDE_DEFAULT_APP_VARIANTS)
1437 skip("Default DDE application test does not match any available results, using first expected data set.\n");
1440 if ((test->todo & 0x1)==0)
1442 ok(rc==test->rc[which], "%s failed: rc=%d err=%d\n", shell_call,
1443 rc, GetLastError());
1447 ok(rc==test->rc[which], "%s failed: rc=%d err=%d\n", shell_call,
1448 rc, GetLastError());
1452 if ((test->todo & 0x2)==0)
1454 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
1455 "Expected application '%s', got '%s'\n",
1456 test->expectedDdeApplication[which], ddeApplication);
1460 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
1461 "Expected application '%s', got '%s'\n",
1462 test->expectedDdeApplication[which], ddeApplication);
1466 delete_test_association(".sde");
1470 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
1471 assert(DdeFreeStringHandle(ddeInst, hszTopic));
1472 assert(DdeFreeStringHandle(ddeInst, hszApplication));
1473 assert(DdeUninitialize(ddeInst));
1476 static void init_test(void)
1479 HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO*);
1480 char filename[MAX_PATH];
1481 WCHAR lnkfile[MAX_PATH];
1483 const char* const * testfile;
1488 hdll=GetModuleHandleA("shell32.dll");
1489 pDllGetVersion=(void*)GetProcAddress(hdll, "DllGetVersion");
1492 dllver.cbSize=sizeof(dllver);
1493 pDllGetVersion(&dllver);
1494 trace("major=%d minor=%d build=%d platform=%d\n",
1495 dllver.dwMajorVersion, dllver.dwMinorVersion,
1496 dllver.dwBuildNumber, dllver.dwPlatformID);
1500 memset(&dllver, 0, sizeof(dllver));
1503 r = CoInitialize(NULL);
1504 ok(SUCCEEDED(r), "CoInitialize failed (0x%08x)\n", r);
1508 rc=GetModuleFileName(NULL, argv0, sizeof(argv0));
1509 assert(rc!=0 && rc<sizeof(argv0));
1510 if (GetFileAttributes(argv0)==INVALID_FILE_ATTRIBUTES)
1512 strcat(argv0, ".so");
1513 ok(GetFileAttributes(argv0)!=INVALID_FILE_ATTRIBUTES,
1514 "unable to find argv0!\n");
1517 GetTempPathA(sizeof(tmpdir)/sizeof(*tmpdir), tmpdir);
1518 assert(GetTempFileNameA(tmpdir, "wt", 0, child_file)!=0);
1519 init_event(child_file);
1521 /* Set up the test files */
1527 sprintf(filename, *testfile, tmpdir);
1528 hfile=CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1529 FILE_ATTRIBUTE_NORMAL, NULL);
1530 if (hfile==INVALID_HANDLE_VALUE)
1532 trace("unable to create '%s': err=%d\n", filename, GetLastError());
1539 /* Setup the test shortcuts */
1540 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
1541 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
1542 desc.description=NULL;
1544 sprintf(filename, "%s\\test file.shlexec", tmpdir);
1547 desc.arguments="ignored";
1552 create_lnk(lnkfile, &desc, 0);
1554 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1555 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
1556 desc.description=NULL;
1560 sprintf(params, "shlexec \"%s\" Lnk", child_file);
1561 desc.arguments=params;
1566 create_lnk(lnkfile, &desc, 0);
1568 /* Create a basic association suitable for most tests */
1569 if (!create_test_association(".shlexec"))
1571 skip("Unable to create association for '.shlexec'\n");
1574 create_test_verb(".shlexec", "Open", 0, "Open \"%1\"");
1575 create_test_verb(".shlexec", "NoQuotes", 0, "NoQuotes %1");
1576 create_test_verb(".shlexec", "LowerL", 0, "LowerL %l");
1577 create_test_verb(".shlexec", "QuotedLowerL", 0, "QuotedLowerL \"%l\"");
1578 create_test_verb(".shlexec", "UpperL", 0, "UpperL %L");
1579 create_test_verb(".shlexec", "QuotedUpperL", 0, "QuotedUpperL \"%L\"");
1582 static void cleanup_test(void)
1584 char filename[MAX_PATH];
1585 const char* const * testfile;
1587 /* Delete the test files */
1591 sprintf(filename, *testfile, tmpdir);
1592 DeleteFile(filename);
1595 DeleteFile(child_file);
1597 /* Delete the test association */
1598 delete_test_association(".shlexec");
1600 CloseHandle(hEvent);
1608 myARGC = winetest_get_mainargs(&myARGV);
1611 doChild(myARGC, myARGV);
1618 test_find_executable();
1623 test_dde_default_app();