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 void 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 assert(rc==ERROR_SUCCESS);
214 rc=RegSetValueEx(hkey, NULL, 0, REG_SZ, (LPBYTE) class, strlen(class)+1);
215 assert(rc==ERROR_SUCCESS);
218 rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, class, 0, NULL, 0,
219 KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS, NULL, &hkey, NULL);
220 assert(rc==ERROR_SUCCESS);
221 rc=RegCreateKeyEx(hkey, "shell", 0, NULL, 0,
222 KEY_CREATE_SUB_KEY, NULL, &hkey_shell, NULL);
223 assert(rc==ERROR_SUCCESS);
225 CloseHandle(hkey_shell);
228 /* Based on RegDeleteTreeW from dlls/advapi32/registry.c */
229 static LSTATUS myRegDeleteTreeA(HKEY hKey, LPCSTR lpszSubKey)
232 DWORD dwMaxSubkeyLen, dwMaxValueLen;
233 DWORD dwMaxLen, dwSize;
234 CHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
239 ret = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
243 /* Get highest length for keys, values */
244 ret = RegQueryInfoKeyA(hSubKey, NULL, NULL, NULL, NULL,
245 &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
246 if (ret) goto cleanup;
250 dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
251 if (dwMaxLen > sizeof(szNameBuf)/sizeof(CHAR))
253 /* Name too big: alloc a buffer for it */
254 if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(CHAR))))
256 ret = ERROR_NOT_ENOUGH_MEMORY;
262 /* Recursively delete all the subkeys */
266 if (RegEnumKeyExA(hSubKey, 0, lpszName, &dwSize, NULL,
267 NULL, NULL, NULL)) break;
269 ret = myRegDeleteTreeA(hSubKey, lpszName);
270 if (ret) goto cleanup;
274 ret = RegDeleteKeyA(hKey, lpszSubKey);
279 if (RegEnumValueA(hKey, 0, lpszName, &dwSize,
280 NULL, NULL, NULL, NULL)) break;
282 ret = RegDeleteValueA(hKey, lpszName);
283 if (ret) goto cleanup;
287 /* Free buffer if allocated */
288 if (lpszName != szNameBuf)
289 HeapFree( GetProcessHeap(), 0, lpszName);
291 RegCloseKey(hSubKey);
295 static void delete_test_association(const char* extension)
297 char class[MAX_PATH];
299 sprintf(class, "shlexec%s", extension);
300 myRegDeleteTreeA(HKEY_CLASSES_ROOT, class);
301 myRegDeleteTreeA(HKEY_CLASSES_ROOT, extension);
304 static void create_test_verb_dde(const char* extension, const char* verb,
305 int rawcmd, const char* cmdtail, const char *ddeexec,
306 const char *application, const char *topic,
309 HKEY hkey_shell, hkey_verb, hkey_cmd;
310 char shell[MAX_PATH];
314 sprintf(shell, "shlexec%s\\shell", extension);
315 rc=RegOpenKeyEx(HKEY_CLASSES_ROOT, shell, 0,
316 KEY_CREATE_SUB_KEY, &hkey_shell);
317 assert(rc==ERROR_SUCCESS);
318 rc=RegCreateKeyEx(hkey_shell, verb, 0, NULL, 0, KEY_CREATE_SUB_KEY,
319 NULL, &hkey_verb, NULL);
320 assert(rc==ERROR_SUCCESS);
321 rc=RegCreateKeyEx(hkey_verb, "command", 0, NULL, 0, KEY_SET_VALUE,
322 NULL, &hkey_cmd, NULL);
323 assert(rc==ERROR_SUCCESS);
327 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmdtail, strlen(cmdtail)+1);
331 cmd=malloc(strlen(argv0)+10+strlen(child_file)+2+strlen(cmdtail)+1);
332 sprintf(cmd,"%s shlexec \"%s\" %s", argv0, child_file, cmdtail);
333 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmd, strlen(cmd)+1);
334 assert(rc==ERROR_SUCCESS);
340 HKEY hkey_ddeexec, hkey_application, hkey_topic, hkey_ifexec;
342 rc=RegCreateKeyEx(hkey_verb, "ddeexec", 0, NULL, 0, KEY_SET_VALUE |
343 KEY_CREATE_SUB_KEY, NULL, &hkey_ddeexec, NULL);
344 assert(rc==ERROR_SUCCESS);
345 rc=RegSetValueEx(hkey_ddeexec, NULL, 0, REG_SZ, (LPBYTE)ddeexec,
347 assert(rc==ERROR_SUCCESS);
350 rc=RegCreateKeyEx(hkey_ddeexec, "application", 0, NULL, 0, KEY_SET_VALUE,
351 NULL, &hkey_application, NULL);
352 assert(rc==ERROR_SUCCESS);
353 rc=RegSetValueEx(hkey_application, NULL, 0, REG_SZ, (LPBYTE)application,
354 strlen(application)+1);
355 assert(rc==ERROR_SUCCESS);
356 CloseHandle(hkey_application);
360 rc=RegCreateKeyEx(hkey_ddeexec, "topic", 0, NULL, 0, KEY_SET_VALUE,
361 NULL, &hkey_topic, NULL);
362 assert(rc==ERROR_SUCCESS);
363 rc=RegSetValueEx(hkey_topic, NULL, 0, REG_SZ, (LPBYTE)topic,
365 assert(rc==ERROR_SUCCESS);
366 CloseHandle(hkey_topic);
370 rc=RegCreateKeyEx(hkey_ddeexec, "ifexec", 0, NULL, 0, KEY_SET_VALUE,
371 NULL, &hkey_ifexec, NULL);
372 assert(rc==ERROR_SUCCESS);
373 rc=RegSetValueEx(hkey_ifexec, NULL, 0, REG_SZ, (LPBYTE)ifexec,
375 assert(rc==ERROR_SUCCESS);
376 CloseHandle(hkey_ifexec);
378 CloseHandle(hkey_ddeexec);
381 CloseHandle(hkey_shell);
382 CloseHandle(hkey_verb);
383 CloseHandle(hkey_cmd);
386 static void create_test_verb(const char* extension, const char* verb,
387 int rawcmd, const char* cmdtail)
389 create_test_verb_dde(extension, verb, rawcmd, cmdtail, NULL, NULL,
395 * Functions to check that the child process was started just right
396 * (borrowed from dlls/kernel32/tests/process.c)
400 static const char* encodeA(const char* str)
402 static char encoded[2*1024+1];
407 len = strlen(str) + 1;
408 if (len >= sizeof(encoded)/2)
410 fprintf(stderr, "string is too long!\n");
414 for (i = 0; i < len; i++)
415 sprintf(&ptr[i * 2], "%02x", (unsigned char)str[i]);
420 static unsigned decode_char(char c)
422 if (c >= '0' && c <= '9') return c - '0';
423 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
424 assert(c >= 'A' && c <= 'F');
428 static char* decodeA(const char* str)
430 static char decoded[1024];
434 len = strlen(str) / 2;
435 if (!len--) return NULL;
436 if (len >= sizeof(decoded))
438 fprintf(stderr, "string is too long!\n");
442 for (i = 0; i < len; i++)
443 ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
448 static void childPrintf(HANDLE h, const char* fmt, ...)
454 va_start(valist, fmt);
455 vsprintf(buffer, fmt, valist);
457 WriteFile(h, buffer, strlen(buffer), &w, NULL);
460 static void doChild(int argc, char** argv)
467 hFile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
468 if (hFile == INVALID_HANDLE_VALUE)
472 childPrintf(hFile, "[Arguments]\r\n");
473 if (winetest_debug > 2)
474 trace("argcA=%d\n", argc);
475 childPrintf(hFile, "argcA=%d\r\n", argc);
476 for (i = 0; i < argc; i++)
478 if (winetest_debug > 2)
479 trace("argvA%d=%s\n", i, argv[i]);
480 childPrintf(hFile, "argvA%d=%s\r\n", i, encodeA(argv[i]));
484 init_event(filename);
489 static char* getChildString(const char* sect, const char* key)
494 GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), child_file);
495 if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
496 assert(!(strlen(buf) & 1));
501 static void dump_child(void)
503 if (winetest_debug > 1)
509 c=GetPrivateProfileIntA("Arguments", "argcA", -1, child_file);
510 trace("argcA=%d\n",c);
513 sprintf(key, "argvA%d", i);
514 str=getChildString("Arguments", key);
515 trace("%s=%s\n", key, str);
520 static int StrCmpPath(const char* s1, const char* s2)
522 if (!s1 && !s2) return 0;
533 if ((*s1=='/' || *s1=='\\') && (*s2=='/' || *s2=='\\'))
535 while (*s1=='/' || *s1=='\\')
537 while (*s2=='/' || *s2=='\\')
540 else if (toupper(*s1)==toupper(*s2))
557 static int _okChildString(const char* file, int line, const char* key, const char* expected)
560 result=getChildString("Arguments", key);
561 return ok_(file, line)(lstrcmpiA(result, expected) == 0,
562 "%s expected '%s', got '%s'\n", key, expected, result);
565 static int _okChildPath(const char* file, int line, const char* key, const char* expected)
568 result=getChildString("Arguments", key);
569 return ok_(file, line)(StrCmpPath(result, expected) == 0,
570 "%s expected '%s', got '%s'\n", key, expected, result);
573 static int _okChildInt(const char* file, int line, const char* key, int expected)
576 result=GetPrivateProfileIntA("Arguments", key, expected, child_file);
577 return ok_(file, line)(result == expected,
578 "%s expected %d, but got %d\n", key, expected, result);
581 #define okChildString(key, expected) _okChildString(__FILE__, __LINE__, (key), (expected))
582 #define okChildPath(key, expected) _okChildPath(__FILE__, __LINE__, (key), (expected))
583 #define okChildInt(key, expected) _okChildInt(__FILE__, __LINE__, (key), (expected))
593 static const char* testfiles[]=
595 "%s\\test file.shlexec",
596 "%s\\%%nasty%% $file.shlexec",
597 "%s\\test file.noassoc",
598 "%s\\test file.noassoc.shlexec",
599 "%s\\test file.shlexec.noassoc",
600 "%s\\test_shortcut_shlexec.lnk",
601 "%s\\test_shortcut_exe.lnk",
603 "%s\\test file.shlfoo",
605 "%s\\masked file.shlexec",
616 const char* basename;
621 static filename_tests_t filename_tests[]=
623 /* Test bad / nonexistent filenames */
624 {NULL, "%s\\nonexistent.shlexec", 0x11, SE_ERR_FNF},
625 {NULL, "%s\\nonexistent.noassoc", 0x11, SE_ERR_FNF},
628 {NULL, "%s\\test file.shlexec", 0x0, 33},
629 {NULL, "%s\\test file.shlexec.", 0x0, 33},
630 {NULL, "%s\\%%nasty%% $file.shlexec", 0x0, 33},
631 {NULL, "%s/test file.shlexec", 0x0, 33},
633 /* Test filenames with no association */
634 {NULL, "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
636 /* Test double extensions */
637 {NULL, "%s\\test file.noassoc.shlexec", 0x0, 33},
638 {NULL, "%s\\test file.shlexec.noassoc", 0x0, SE_ERR_NOASSOC},
640 /* Test alternate verbs */
641 {"LowerL", "%s\\nonexistent.shlexec", 0x11, SE_ERR_FNF},
642 {"LowerL", "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
644 {"QuotedLowerL", "%s\\test file.shlexec", 0x0, 33},
645 {"QuotedUpperL", "%s\\test file.shlexec", 0x0, 33},
647 /* Test file masked due to space */
648 {NULL, "%s\\masked file.shlexec", 0x1, 33},
649 /* Test if quoting prevents the masking */
650 {NULL, "%s\\masked file.shlexec", 0x40, 33},
655 static filename_tests_t noquotes_tests[]=
657 /* Test unquoted '%1' thingies */
658 {"NoQuotes", "%s\\test file.shlexec", 0xa, 33},
659 {"LowerL", "%s\\test file.shlexec", 0xa, 33},
660 {"UpperL", "%s\\test file.shlexec", 0xa, 33},
665 static void test_filename(void)
667 char filename[MAX_PATH];
668 const filename_tests_t* test;
673 while (test->basename)
675 sprintf(filename, test->basename, tmpdir);
676 if (strchr(filename, '/'))
686 if ((test->todo & 0x40)==0)
688 rc=shell_execute(test->verb, filename, NULL, NULL);
692 char quoted[MAX_PATH + 2];
693 sprintf(quoted, "\"%s\"", filename);
694 rc=shell_execute(test->verb, quoted, NULL, NULL);
698 if ((test->todo & 0x1)==0)
700 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
705 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
711 if ((test->todo & 0x2)==0)
713 okChildInt("argcA", 5);
717 okChildInt("argcA", 5);
719 verb=(test->verb ? test->verb : "Open");
720 if ((test->todo & 0x4)==0)
722 okChildString("argvA3", verb);
726 okChildString("argvA3", verb);
728 if ((test->todo & 0x8)==0)
730 okChildPath("argvA4", filename);
734 okChildPath("argvA4", filename);
741 while (test->basename)
743 sprintf(filename, test->basename, tmpdir);
744 rc=shell_execute(test->verb, filename, NULL, NULL);
747 if ((test->todo & 0x1)==0)
749 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
754 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
763 verb=(test->verb ? test->verb : "Open");
764 if ((test->todo & 0x4)==0)
766 okChildString("argvA3", verb);
770 okChildString("argvA3", verb);
779 space=strchr(str, ' ');
782 sprintf(attrib, "argvA%d", count);
783 if ((test->todo & 0x8)==0)
785 okChildPath(attrib, str);
789 okChildPath(attrib, str);
796 if ((test->todo & 0x2)==0)
798 okChildInt("argcA", count);
802 okChildInt("argcA", count);
808 if (dllver.dwMajorVersion != 0)
810 /* The more recent versions of shell32.dll accept quoted filenames
811 * while older ones (e.g. 4.00) don't. Still we want to test this
812 * because IE 6 depends on the new behavior.
813 * One day we may need to check the exact version of the dll but for
814 * now making sure DllGetVersion() is present is sufficient.
816 sprintf(filename, "\"%s\\test file.shlexec\"", tmpdir);
817 rc=shell_execute(NULL, filename, NULL, NULL);
818 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
820 okChildInt("argcA", 5);
821 okChildString("argvA3", "Open");
822 sprintf(filename, "%s\\test file.shlexec", tmpdir);
823 okChildPath("argvA4", filename);
827 static void test_find_executable(void)
829 char filename[MAX_PATH];
830 char command[MAX_PATH];
831 const filename_tests_t* test;
834 create_test_association(".sfe");
835 create_test_verb(".sfe", "Open", 1, "%1");
837 /* Don't test FindExecutable(..., NULL), it always crashes */
839 strcpy(command, "your word");
840 rc=(int)FindExecutableA(NULL, NULL, command);
841 ok(rc == SE_ERR_FNF || rc > 32 /* nt4 */, "FindExecutable(NULL) returned %d\n", rc);
842 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
844 strcpy(command, "your word");
845 rc=(int)FindExecutableA(tmpdir, NULL, command);
846 ok(rc == SE_ERR_NOASSOC /* >= win2000 */ || rc > 32 /* win98, nt4 */, "FindExecutable(NULL) returned %d\n", rc);
847 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
849 sprintf(filename, "%s\\test file.sfe", tmpdir);
850 rc=(int)FindExecutableA(filename, NULL, command);
851 ok(rc > 32, "FindExecutable(%s) returned %d\n", filename, rc);
852 /* Depending on the platform, command could be '%1' or 'test file.sfe' */
854 rc=(int)FindExecutableA("test file.sfe", tmpdir, command);
855 ok(rc > 32, "FindExecutable(%s) returned %d\n", filename, rc);
857 rc=(int)FindExecutableA("test file.sfe", NULL, command);
858 todo_wine ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %d\n", filename, rc);
860 delete_test_association(".sfe");
862 create_test_association(".shl");
863 create_test_verb(".shl", "Open", 0, "Open");
865 sprintf(filename, "%s\\test file.shl", tmpdir);
866 rc=(int)FindExecutableA(filename, NULL, command);
867 ok(rc == SE_ERR_FNF /* NT4 */ || rc > 32, "FindExecutable(%s) returned %d\n", filename, rc);
869 sprintf(filename, "%s\\test file.shlfoo", tmpdir);
870 rc=(int)FindExecutableA(filename, NULL, command);
872 delete_test_association(".shl");
876 /* On Windows XP and 2003 FindExecutable() is completely broken.
877 * Probably what it does is convert the filename to 8.3 format,
878 * which as a side effect converts the '.shlfoo' extension to '.shl',
879 * and then tries to find an association for '.shl'. This means it
880 * will normally fail on most extensions with more than 3 characters,
882 * Also it means we cannot do any other test.
884 trace("FindExecutable() is broken -> skipping 4+ character extension tests\n");
889 while (test->basename)
891 sprintf(filename, test->basename, tmpdir);
892 if (strchr(filename, '/'))
903 /* Win98 does not '\0'-terminate command! */
904 memset(command, '\0', sizeof(command));
905 rc=(int)FindExecutableA(filename, NULL, command);
908 if ((test->todo & 0x10)==0)
910 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%d\n", filename, rc);
914 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%d\n", filename, rc);
919 equal=strcmp(command, argv0) == 0 ||
920 /* NT4 returns an extra 0x8 character! */
921 (strlen(command) == strlen(argv0)+1 && strncmp(command, argv0, strlen(argv0)) == 0);
922 if ((test->todo & 0x20)==0)
924 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
925 filename, command, argv0);
929 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
930 filename, command, argv0);
938 static filename_tests_t lnk_tests[]=
940 /* Pass bad / nonexistent filenames as a parameter */
941 {NULL, "%s\\nonexistent.shlexec", 0xa, 33},
942 {NULL, "%s\\nonexistent.noassoc", 0xa, 33},
944 /* Pass regular paths as a parameter */
945 {NULL, "%s\\test file.shlexec", 0xa, 33},
946 {NULL, "%s/%%nasty%% $file.shlexec", 0xa, 33},
948 /* Pass filenames with no association as a parameter */
949 {NULL, "%s\\test file.noassoc", 0xa, 33},
954 static void test_lnks(void)
956 char filename[MAX_PATH];
957 char params[MAX_PATH];
958 const filename_tests_t* test;
961 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
962 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
963 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
965 okChildInt("argcA", 5);
966 okChildString("argvA3", "Open");
967 sprintf(filename, "%s\\test file.shlexec", tmpdir);
968 okChildPath("argvA4", filename);
970 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
971 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
972 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
974 okChildInt("argcA", 4);
975 okChildString("argvA3", "Lnk");
977 if (dllver.dwMajorVersion>=6)
980 /* Recent versions of shell32.dll accept '/'s in shortcut paths.
981 * Older versions don't or are quite buggy in this regard.
983 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
991 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
992 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
994 okChildInt("argcA", 4);
995 okChildString("argvA3", "Lnk");
998 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1000 while (test->basename)
1003 sprintf(params+1, test->basename, tmpdir);
1004 strcat(params,"\"");
1005 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, params,
1009 if ((test->todo & 0x1)==0)
1011 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1012 rc, GetLastError());
1016 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1017 rc, GetLastError());
1021 if ((test->todo & 0x2)==0)
1023 okChildInt("argcA", 5);
1027 okChildInt("argcA", 5);
1029 if ((test->todo & 0x4)==0)
1031 okChildString("argvA3", "Lnk");
1035 okChildString("argvA3", "Lnk");
1037 sprintf(params, test->basename, tmpdir);
1038 if ((test->todo & 0x8)==0)
1040 okChildPath("argvA4", params);
1044 okChildPath("argvA4", params);
1052 static void test_exes(void)
1054 char filename[MAX_PATH];
1058 sprintf(params, "shlexec \"%s\" Exec", child_file);
1060 /* We need NOZONECHECKS on Win2003 to block a dialog */
1061 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
1063 ok(rc > 32, "%s returned %d\n", shell_call, rc);
1064 okChildInt("argcA", 4);
1065 okChildString("argvA3", "Exec");
1067 sprintf(filename, "%s\\test file.noassoc", tmpdir);
1068 if (CopyFile(argv0, filename, FALSE))
1070 rc=shell_execute(NULL, filename, params, NULL);
1072 ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
1077 static void test_exes_long(void)
1079 char filename[MAX_PATH];
1081 char longparam[MAX_PATH];
1084 for (rc = 0; rc < MAX_PATH; rc++)
1085 longparam[rc]='a'+rc%26;
1086 longparam[MAX_PATH-1]=0;
1089 sprintf(params, "shlexec \"%s\" %s", child_file,longparam);
1091 /* We need NOZONECHECKS on Win2003 to block a dialog */
1092 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
1094 ok(rc > 32, "%s returned %d\n", shell_call, rc);
1095 okChildInt("argcA", 4);
1096 okChildString("argvA3", longparam);
1098 sprintf(filename, "%s\\test file.noassoc", tmpdir);
1099 if (CopyFile(argv0, filename, FALSE))
1101 rc=shell_execute(NULL, filename, params, NULL);
1103 ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
1110 const char* command;
1111 const char* ddeexec;
1112 const char* application;
1116 const char* expectedDdeExec;
1121 static dde_tests_t dde_tests[] =
1123 /* Test passing and not passing command-line
1124 * argument, no DDE */
1125 {"", NULL, NULL, NULL, NULL, FALSE, "", 0x0, 33},
1126 {"\"%1\"", NULL, NULL, NULL, NULL, TRUE, "", 0x0, 33},
1128 /* Test passing and not passing command-line
1129 * argument, with DDE */
1130 {"", "[open(\"%1\")]", "shlexec", "dde", NULL, FALSE, "[open(\"%s\")]", 0x0, 33},
1131 {"\"%1\"", "[open(\"%1\")]", "shlexec", "dde", NULL, TRUE, "[open(\"%s\")]", 0x0, 33},
1133 /* Test unquoted %1 in command and ddeexec
1134 * (test filename has space) */
1135 {"%1", "[open(%1)]", "shlexec", "dde", NULL, 2, "[open(%s)]", 0x0, 33},
1137 /* Test ifexec precedence over ddeexec */
1138 {"", "[open(\"%1\")]", "shlexec", "dde", "[ifexec(\"%1\")]", FALSE, "[ifexec(\"%s\")]", 0x0, 33},
1140 /* Test default DDE topic */
1141 {"", "[open(\"%1\")]", "shlexec", NULL, NULL, FALSE, "[open(\"%s\")]", 0x0, 33},
1143 /* Test default DDE application */
1144 {"", "[open(\"%1\")]", NULL, "dde", NULL, FALSE, "[open(\"%s\")]", 0x0, 33},
1146 {NULL, NULL, NULL, NULL, NULL, 0, 0x0, 0}
1149 static DWORD ddeInst;
1150 static HSZ hszTopic;
1151 static char ddeExec[MAX_PATH], ddeApplication[MAX_PATH];
1152 static BOOL denyNextConnection;
1154 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
1155 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
1156 ULONG_PTR dwData1, ULONG_PTR dwData2)
1160 if (winetest_debug > 2)
1161 trace("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
1162 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
1167 if (!DdeCmpStringHandles(hsz1, hszTopic))
1169 if (denyNextConnection)
1170 denyNextConnection = FALSE;
1173 size = DdeQueryString(ddeInst, hsz2, ddeApplication, MAX_PATH, CP_WINANSI);
1174 assert(size < MAX_PATH);
1175 return (HDDEDATA)TRUE;
1178 return (HDDEDATA)FALSE;
1181 size = DdeGetData(hData, (LPBYTE)ddeExec, MAX_PATH, 0L);
1182 assert(size < MAX_PATH);
1183 DdeFreeDataHandle(hData);
1184 return (HDDEDATA)DDE_FACK;
1194 DWORD threadIdParent;
1195 } dde_thread_info_t;
1197 static DWORD CALLBACK ddeThread(LPVOID arg)
1199 dde_thread_info_t *info = (dde_thread_info_t *)arg;
1200 assert(info && info->filename);
1201 PostThreadMessage(info->threadIdParent,
1203 shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, info->filename, NULL, NULL),
1208 /* ShellExecute won't successfully send DDE commands to console applications after starting them,
1209 * so we run a DDE server in this application, deny the first connection request to make
1210 * ShellExecute start the application, and then process the next DDE connection in this application
1211 * to see the execute command that is sent. */
1212 static void test_dde(void)
1214 char filename[MAX_PATH], defApplication[MAX_PATH];
1216 dde_thread_info_t info = { filename, GetCurrentThreadId() };
1217 const dde_tests_t* test;
1224 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
1225 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
1226 assert(rc == DMLERR_NO_ERROR);
1228 sprintf(filename, "%s\\test file.sde", tmpdir);
1230 /* Default service is application name minus path and extension */
1231 strcpy(defApplication, strrchr(argv0, '\\')+1);
1232 *strchr(defApplication, '.') = 0;
1235 while (test->command)
1237 create_test_association(".sde");
1238 create_test_verb_dde(".sde", "Open", 0, test->command, test->ddeexec,
1239 test->application, test->topic, test->ifexec);
1240 hszApplication = DdeCreateStringHandleA(ddeInst, test->application ?
1241 test->application : defApplication, CP_WINANSI);
1242 hszTopic = DdeCreateStringHandleA(ddeInst, test->topic ? test->topic : SZDDESYS_TOPIC,
1244 assert(hszApplication && hszTopic);
1245 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER));
1246 denyNextConnection = TRUE;
1249 assert(CreateThread(NULL, 0, ddeThread, (LPVOID)&info, 0, &threadId));
1250 while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);
1251 rc = msg.wParam > 32 ? 33 : msg.wParam;
1252 if ((test->todo & 0x1)==0)
1254 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1255 rc, GetLastError());
1259 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1260 rc, GetLastError());
1264 if ((test->todo & 0x2)==0)
1266 okChildInt("argcA", test->expectedArgs + 3);
1270 okChildInt("argcA", test->expectedArgs + 3);
1272 if (test->expectedArgs == 1)
1274 if ((test->todo & 0x4) == 0)
1276 okChildPath("argvA3", filename);
1280 okChildPath("argvA3", filename);
1283 if ((test->todo & 0x8) == 0)
1285 sprintf(params, test->expectedDdeExec, filename);
1286 ok(StrCmpPath(params, ddeExec) == 0,
1287 "ddeexec expected '%s', got '%s'\n", params, ddeExec);
1291 sprintf(params, test->expectedDdeExec, filename);
1292 ok(StrCmpPath(params, ddeExec) == 0,
1293 "ddeexec expected '%s', got '%s'\n", params, ddeExec);
1297 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
1298 assert(DdeFreeStringHandle(ddeInst, hszTopic));
1299 assert(DdeFreeStringHandle(ddeInst, hszApplication));
1300 delete_test_association(".sde");
1304 assert(DdeUninitialize(ddeInst));
1307 #define DDE_DEFAULT_APP_VARIANTS 2
1310 const char* command;
1311 const char* expectedDdeApplication[DDE_DEFAULT_APP_VARIANTS];
1313 int rc[DDE_DEFAULT_APP_VARIANTS];
1314 } dde_default_app_tests_t;
1316 static dde_default_app_tests_t dde_default_app_tests[] =
1318 /* Windows XP and 98 handle default DDE app names in different ways.
1319 * The application name we see in the first test determines the pattern
1320 * of application names and return codes we will look for. */
1322 /* Test unquoted existing filename with a space */
1323 {"%s\\test file.exe", {"test file", "test"}, 0x0, {33, 33}},
1324 {"%s\\test file.exe param", {"test file", "test"}, 0x0, {33, 33}},
1326 /* Test quoted existing filename with a space */
1327 {"\"%s\\test file.exe\"", {"test file", "test file"}, 0x0, {33, 33}},
1328 {"\"%s\\test file.exe\" param", {"test file", "test file"}, 0x0, {33, 33}},
1330 /* Test unquoted filename with a space that doesn't exist, but
1332 {"%s\\test2 file.exe", {"test2", "test2"}, 0x0, {33, 33}},
1333 {"%s\\test2 file.exe param", {"test2", "test2"}, 0x0, {33, 33}},
1335 /* Test quoted filename with a space that does not exist */
1336 {"\"%s\\test2 file.exe\"", {"", "test2 file"}, 0x0, {5, 33}},
1337 {"\"%s\\test2 file.exe\" param", {"", "test2 file"}, 0x0, {5, 33}},
1339 /* Test filename supplied without the extension */
1340 {"%s\\test2", {"test2", "test2"}, 0x0, {33, 33}},
1341 {"%s\\test2 param", {"test2", "test2"}, 0x0, {33, 33}},
1343 /* Test an unquoted nonexistent filename */
1344 {"%s\\notexist.exe", {"", "notexist"}, 0x0, {5, 33}},
1345 {"%s\\notexist.exe param", {"", "notexist"}, 0x0, {5, 33}},
1347 /* Test an application that will be found on the path */
1348 {"cmd", {"cmd", "cmd"}, 0x0, {33, 33}},
1349 {"cmd param", {"cmd", "cmd"}, 0x0, {33, 33}},
1351 /* Test an application that will not be found on the path */
1352 {"xyzwxyzwxyz", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
1353 {"xyzwxyzwxyz param", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
1355 {NULL, {NULL}, 0, {0}}
1358 static void test_dde_default_app(void)
1360 char filename[MAX_PATH];
1362 dde_thread_info_t info = { filename, GetCurrentThreadId() };
1363 const dde_default_app_tests_t* test;
1370 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
1371 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
1372 assert(rc == DMLERR_NO_ERROR);
1374 sprintf(filename, "%s\\test file.sde", tmpdir);
1376 /* It is strictly not necessary to register an application name here, but wine's
1377 * DdeNameService implementation complains if 0L is passed instead of
1378 * hszApplication with DNS_FILTEROFF */
1379 hszApplication = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
1380 hszTopic = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
1381 assert(hszApplication && hszTopic);
1382 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER | DNS_FILTEROFF));
1384 test = dde_default_app_tests;
1385 while (test->command)
1387 create_test_association(".sde");
1388 sprintf(params, test->command, tmpdir);
1389 create_test_verb_dde(".sde", "Open", 1, params, "[test]", NULL,
1391 denyNextConnection = FALSE;
1392 ddeApplication[0] = 0;
1394 /* No application will be run as we will respond to the first DDE event,
1395 * so don't wait for it */
1398 assert(CreateThread(NULL, 0, ddeThread, (LPVOID)&info, 0, &threadId));
1399 while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);
1400 rc = msg.wParam > 32 ? 33 : msg.wParam;
1402 /* First test, find which set of test data we expect to see */
1403 if (test == dde_default_app_tests)
1406 for (i=0; i<DDE_DEFAULT_APP_VARIANTS; i++)
1408 if (!strcmp(ddeApplication, test->expectedDdeApplication[i]))
1414 if (i == DDE_DEFAULT_APP_VARIANTS)
1415 skip("Default DDE application test does not match any available results, using first expected data set.\n");
1418 if ((test->todo & 0x1)==0)
1420 ok(rc==test->rc[which], "%s failed: rc=%d err=%d\n", shell_call,
1421 rc, GetLastError());
1425 ok(rc==test->rc[which], "%s failed: rc=%d err=%d\n", shell_call,
1426 rc, GetLastError());
1430 if ((test->todo & 0x2)==0)
1432 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
1433 "Expected application '%s', got '%s'\n",
1434 test->expectedDdeApplication[which], ddeApplication);
1438 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
1439 "Expected application '%s', got '%s'\n",
1440 test->expectedDdeApplication[which], ddeApplication);
1444 delete_test_association(".sde");
1448 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
1449 assert(DdeFreeStringHandle(ddeInst, hszTopic));
1450 assert(DdeFreeStringHandle(ddeInst, hszApplication));
1451 assert(DdeUninitialize(ddeInst));
1454 static void init_test(void)
1457 HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO*);
1458 char filename[MAX_PATH];
1459 WCHAR lnkfile[MAX_PATH];
1461 const char* const * testfile;
1466 hdll=GetModuleHandleA("shell32.dll");
1467 pDllGetVersion=(void*)GetProcAddress(hdll, "DllGetVersion");
1470 dllver.cbSize=sizeof(dllver);
1471 pDllGetVersion(&dllver);
1472 trace("major=%d minor=%d build=%d platform=%d\n",
1473 dllver.dwMajorVersion, dllver.dwMinorVersion,
1474 dllver.dwBuildNumber, dllver.dwPlatformID);
1478 memset(&dllver, 0, sizeof(dllver));
1481 r = CoInitialize(NULL);
1482 ok(SUCCEEDED(r), "CoInitialize failed (0x%08x)\n", r);
1486 rc=GetModuleFileName(NULL, argv0, sizeof(argv0));
1487 assert(rc!=0 && rc<sizeof(argv0));
1488 if (GetFileAttributes(argv0)==INVALID_FILE_ATTRIBUTES)
1490 strcat(argv0, ".so");
1491 ok(GetFileAttributes(argv0)!=INVALID_FILE_ATTRIBUTES,
1492 "unable to find argv0!\n");
1495 GetTempPathA(sizeof(tmpdir)/sizeof(*tmpdir), tmpdir);
1496 assert(GetTempFileNameA(tmpdir, "wt", 0, child_file)!=0);
1497 init_event(child_file);
1499 /* Set up the test files */
1505 sprintf(filename, *testfile, tmpdir);
1506 hfile=CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1507 FILE_ATTRIBUTE_NORMAL, NULL);
1508 if (hfile==INVALID_HANDLE_VALUE)
1510 trace("unable to create '%s': err=%d\n", filename, GetLastError());
1517 /* Setup the test shortcuts */
1518 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
1519 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
1520 desc.description=NULL;
1522 sprintf(filename, "%s\\test file.shlexec", tmpdir);
1525 desc.arguments="ignored";
1530 create_lnk(lnkfile, &desc, 0);
1532 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1533 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
1534 desc.description=NULL;
1538 sprintf(params, "shlexec \"%s\" Lnk", child_file);
1539 desc.arguments=params;
1544 create_lnk(lnkfile, &desc, 0);
1546 /* Create a basic association suitable for most tests */
1547 create_test_association(".shlexec");
1548 create_test_verb(".shlexec", "Open", 0, "Open \"%1\"");
1549 create_test_verb(".shlexec", "NoQuotes", 0, "NoQuotes %1");
1550 create_test_verb(".shlexec", "LowerL", 0, "LowerL %l");
1551 create_test_verb(".shlexec", "QuotedLowerL", 0, "QuotedLowerL \"%l\"");
1552 create_test_verb(".shlexec", "UpperL", 0, "UpperL %L");
1553 create_test_verb(".shlexec", "QuotedUpperL", 0, "QuotedUpperL \"%L\"");
1556 static void cleanup_test(void)
1558 char filename[MAX_PATH];
1559 const char* const * testfile;
1561 /* Delete the test files */
1565 sprintf(filename, *testfile, tmpdir);
1566 DeleteFile(filename);
1569 DeleteFile(child_file);
1571 /* Delete the test association */
1572 delete_test_association(".shlexec");
1574 CloseHandle(hEvent);
1582 myARGC = winetest_get_mainargs(&myARGV);
1585 doChild(myARGC, myARGV);
1592 test_find_executable();
1597 test_dde_default_app();