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
35 /* Needed to get SEE_MASK_NOZONECHECKS with the PSDK */
36 #define NTDDI_WINXPSP1 0x05010100
37 #define NTDDI_VERSION NTDDI_WINXPSP1
38 #define _WIN32_WINNT 0x0501
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;
59 static BOOL skip_noassoc_tests = FALSE;
60 static HANDLE dde_ready_event;
65 * ShellExecute wrappers
68 static void dump_child(void);
71 static void init_event(const char* child_file)
74 event_name=strrchr(child_file, '\\')+1;
75 hEvent=CreateEvent(NULL, FALSE, FALSE, event_name);
78 static void strcat_param(char* str, const char* name, const char* param)
82 if (str[strlen(str)-1] == '"')
91 static int _todo_wait = 0;
92 #define todo_wait for (_todo_wait = 1; _todo_wait; _todo_wait = 0)
94 static char shell_call[2048]="";
95 static int bad_shellexecute = 0;
96 static INT_PTR shell_execute(LPCSTR operation, LPCSTR file, LPCSTR parameters, LPCSTR directory)
98 INT_PTR rc, rcEmpty = 0;
101 rcEmpty = shell_execute("", file, parameters, directory);
103 strcpy(shell_call, "ShellExecute(");
104 strcat_param(shell_call, "verb", operation);
105 strcat_param(shell_call, "file", file);
106 strcat_param(shell_call, "params", parameters);
107 strcat_param(shell_call, "dir", directory);
108 strcat(shell_call, ")");
109 if (winetest_debug > 1)
110 trace("%s\n", shell_call);
112 DeleteFile(child_file);
113 SetLastError(0xcafebabe);
115 /* FIXME: We cannot use ShellExecuteEx() here because if there is no
116 * association it displays the 'Open With' dialog and I could not find
117 * a flag to prevent this.
119 rc=(INT_PTR)ShellExecute(NULL, operation, file, parameters, directory, SW_SHOWNORMAL);
124 wait_rc=WaitForSingleObject(hEvent, 5000);
125 if (wait_rc == WAIT_TIMEOUT)
127 HWND wnd = FindWindowA("#32770", "Windows");
130 SendMessage(wnd, WM_CLOSE, 0, 0);
131 win_skip("Skipping shellexecute of file with unassociated extension\n");
132 skip_noassoc_tests = TRUE;
137 ok(wait_rc==WAIT_OBJECT_0 || rc <= 32, "%s WaitForSingleObject returned %d\n", shell_call, wait_rc);
139 ok(wait_rc==WAIT_OBJECT_0 || rc <= 32, "%s WaitForSingleObject returned %d\n", shell_call, wait_rc);
141 /* The child process may have changed the result file, so let profile
142 * functions know about it
144 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
150 if (rc != rcEmpty && rcEmpty == SE_ERR_NOASSOC) /* NT4 */
151 bad_shellexecute = 1;
152 ok(rc == rcEmpty || broken(rc != rcEmpty && rcEmpty == SE_ERR_NOASSOC) /* NT4 */,
153 "%s Got different return value with empty string: %lu %lu\n", shell_call, rc, rcEmpty);
159 static INT_PTR shell_execute_ex(DWORD mask, LPCSTR operation, LPCSTR file,
160 LPCSTR parameters, LPCSTR directory,
163 SHELLEXECUTEINFO sei;
167 strcpy(shell_call, "ShellExecuteEx(");
171 sprintf(smask, "0x%x", mask);
172 strcat_param(shell_call, "mask", smask);
174 strcat_param(shell_call, "verb", operation);
175 strcat_param(shell_call, "file", file);
176 strcat_param(shell_call, "params", parameters);
177 strcat_param(shell_call, "dir", directory);
178 strcat_param(shell_call, "class", class);
179 strcat(shell_call, ")");
180 if (winetest_debug > 1)
181 trace("%s\n", shell_call);
183 sei.cbSize=sizeof(sei);
184 sei.fMask=SEE_MASK_NOCLOSEPROCESS | mask;
186 sei.lpVerb=operation;
188 sei.lpParameters=parameters;
189 sei.lpDirectory=directory;
190 sei.nShow=SW_SHOWNORMAL;
191 sei.hInstApp=NULL; /* Out */
197 sei.hProcess=NULL; /* Out */
199 DeleteFile(child_file);
200 SetLastError(0xcafebabe);
201 success=ShellExecuteEx(&sei);
202 rc=(INT_PTR)sei.hInstApp;
203 ok((success && rc > 32) || (!success && rc <= 32),
204 "%s rc=%d and hInstApp=%ld is not allowed\n", shell_call, success, rc);
209 if (sei.hProcess!=NULL)
211 wait_rc=WaitForSingleObject(sei.hProcess, 5000);
212 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject(hProcess) returned %d\n", wait_rc);
214 wait_rc=WaitForSingleObject(hEvent, 5000);
216 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
218 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
220 /* The child process may have changed the result file, so let profile
221 * functions know about it
223 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
234 * Functions to create / delete associations wrappers
238 static BOOL create_test_association(const char* extension)
240 HKEY hkey, hkey_shell;
241 char class[MAX_PATH];
244 sprintf(class, "shlexec%s", extension);
245 rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, extension, 0, NULL, 0, KEY_SET_VALUE,
247 if (rc != ERROR_SUCCESS)
250 rc=RegSetValueEx(hkey, NULL, 0, REG_SZ, (LPBYTE) class, strlen(class)+1);
251 ok(rc==ERROR_SUCCESS, "RegSetValueEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
254 rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, class, 0, NULL, 0,
255 KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS, NULL, &hkey, NULL);
256 ok(rc==ERROR_SUCCESS, "RegCreateKeyEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
258 rc=RegCreateKeyEx(hkey, "shell", 0, NULL, 0,
259 KEY_CREATE_SUB_KEY, NULL, &hkey_shell, NULL);
260 ok(rc==ERROR_SUCCESS, "RegCreateKeyEx 'shell' failed, expected ERROR_SUCCESS, got %d\n", rc);
263 CloseHandle(hkey_shell);
268 /* Based on RegDeleteTreeW from dlls/advapi32/registry.c */
269 static LSTATUS myRegDeleteTreeA(HKEY hKey, LPCSTR lpszSubKey)
272 DWORD dwMaxSubkeyLen, dwMaxValueLen;
273 DWORD dwMaxLen, dwSize;
274 CHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
279 ret = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
283 /* Get highest length for keys, values */
284 ret = RegQueryInfoKeyA(hSubKey, NULL, NULL, NULL, NULL,
285 &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
286 if (ret) goto cleanup;
290 dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
291 if (dwMaxLen > sizeof(szNameBuf)/sizeof(CHAR))
293 /* Name too big: alloc a buffer for it */
294 if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(CHAR))))
296 ret = ERROR_NOT_ENOUGH_MEMORY;
302 /* Recursively delete all the subkeys */
306 if (RegEnumKeyExA(hSubKey, 0, lpszName, &dwSize, NULL,
307 NULL, NULL, NULL)) break;
309 ret = myRegDeleteTreeA(hSubKey, lpszName);
310 if (ret) goto cleanup;
314 ret = RegDeleteKeyA(hKey, lpszSubKey);
319 if (RegEnumValueA(hKey, 0, lpszName, &dwSize,
320 NULL, NULL, NULL, NULL)) break;
322 ret = RegDeleteValueA(hKey, lpszName);
323 if (ret) goto cleanup;
327 /* Free buffer if allocated */
328 if (lpszName != szNameBuf)
329 HeapFree( GetProcessHeap(), 0, lpszName);
331 RegCloseKey(hSubKey);
335 static void delete_test_association(const char* extension)
337 char class[MAX_PATH];
339 sprintf(class, "shlexec%s", extension);
340 myRegDeleteTreeA(HKEY_CLASSES_ROOT, class);
341 myRegDeleteTreeA(HKEY_CLASSES_ROOT, extension);
344 static void create_test_verb_dde(const char* extension, const char* verb,
345 int rawcmd, const char* cmdtail, const char *ddeexec,
346 const char *application, const char *topic,
349 HKEY hkey_shell, hkey_verb, hkey_cmd;
350 char shell[MAX_PATH];
354 sprintf(shell, "shlexec%s\\shell", extension);
355 rc=RegOpenKeyEx(HKEY_CLASSES_ROOT, shell, 0,
356 KEY_CREATE_SUB_KEY, &hkey_shell);
357 assert(rc==ERROR_SUCCESS);
358 rc=RegCreateKeyEx(hkey_shell, verb, 0, NULL, 0, KEY_CREATE_SUB_KEY,
359 NULL, &hkey_verb, NULL);
360 assert(rc==ERROR_SUCCESS);
361 rc=RegCreateKeyEx(hkey_verb, "command", 0, NULL, 0, KEY_SET_VALUE,
362 NULL, &hkey_cmd, NULL);
363 assert(rc==ERROR_SUCCESS);
367 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmdtail, strlen(cmdtail)+1);
371 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(child_file)+2+strlen(cmdtail)+1);
372 sprintf(cmd,"%s shlexec \"%s\" %s", argv0, child_file, cmdtail);
373 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmd, strlen(cmd)+1);
374 assert(rc==ERROR_SUCCESS);
375 HeapFree(GetProcessHeap(), 0, cmd);
380 HKEY hkey_ddeexec, hkey_application, hkey_topic, hkey_ifexec;
382 rc=RegCreateKeyEx(hkey_verb, "ddeexec", 0, NULL, 0, KEY_SET_VALUE |
383 KEY_CREATE_SUB_KEY, NULL, &hkey_ddeexec, NULL);
384 assert(rc==ERROR_SUCCESS);
385 rc=RegSetValueEx(hkey_ddeexec, NULL, 0, REG_SZ, (LPBYTE)ddeexec,
387 assert(rc==ERROR_SUCCESS);
390 rc=RegCreateKeyEx(hkey_ddeexec, "application", 0, NULL, 0, KEY_SET_VALUE,
391 NULL, &hkey_application, NULL);
392 assert(rc==ERROR_SUCCESS);
393 rc=RegSetValueEx(hkey_application, NULL, 0, REG_SZ, (LPBYTE)application,
394 strlen(application)+1);
395 assert(rc==ERROR_SUCCESS);
396 CloseHandle(hkey_application);
400 rc=RegCreateKeyEx(hkey_ddeexec, "topic", 0, NULL, 0, KEY_SET_VALUE,
401 NULL, &hkey_topic, NULL);
402 assert(rc==ERROR_SUCCESS);
403 rc=RegSetValueEx(hkey_topic, NULL, 0, REG_SZ, (LPBYTE)topic,
405 assert(rc==ERROR_SUCCESS);
406 CloseHandle(hkey_topic);
410 rc=RegCreateKeyEx(hkey_ddeexec, "ifexec", 0, NULL, 0, KEY_SET_VALUE,
411 NULL, &hkey_ifexec, NULL);
412 assert(rc==ERROR_SUCCESS);
413 rc=RegSetValueEx(hkey_ifexec, NULL, 0, REG_SZ, (LPBYTE)ifexec,
415 assert(rc==ERROR_SUCCESS);
416 CloseHandle(hkey_ifexec);
418 CloseHandle(hkey_ddeexec);
421 CloseHandle(hkey_shell);
422 CloseHandle(hkey_verb);
423 CloseHandle(hkey_cmd);
426 static void create_test_verb(const char* extension, const char* verb,
427 int rawcmd, const char* cmdtail)
429 create_test_verb_dde(extension, verb, rawcmd, cmdtail, NULL, NULL,
435 * Functions to check that the child process was started just right
436 * (borrowed from dlls/kernel32/tests/process.c)
440 static const char* encodeA(const char* str)
442 static char encoded[2*1024+1];
447 len = strlen(str) + 1;
448 if (len >= sizeof(encoded)/2)
450 fprintf(stderr, "string is too long!\n");
454 for (i = 0; i < len; i++)
455 sprintf(&ptr[i * 2], "%02x", (unsigned char)str[i]);
460 static unsigned decode_char(char c)
462 if (c >= '0' && c <= '9') return c - '0';
463 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
464 assert(c >= 'A' && c <= 'F');
468 static char* decodeA(const char* str)
470 static char decoded[1024];
474 len = strlen(str) / 2;
475 if (!len--) return NULL;
476 if (len >= sizeof(decoded))
478 fprintf(stderr, "string is too long!\n");
482 for (i = 0; i < len; i++)
483 ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
488 static void childPrintf(HANDLE h, const char* fmt, ...)
494 va_start(valist, fmt);
495 vsprintf(buffer, fmt, valist);
497 WriteFile(h, buffer, strlen(buffer), &w, NULL);
500 static DWORD ddeInst;
502 static char ddeExec[MAX_PATH], ddeApplication[MAX_PATH];
503 static BOOL post_quit_on_execute;
505 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
506 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
507 ULONG_PTR dwData1, ULONG_PTR dwData2)
511 if (winetest_debug > 2)
512 trace("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
513 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
518 if (!DdeCmpStringHandles(hsz1, hszTopic))
520 size = DdeQueryString(ddeInst, hsz2, ddeApplication, MAX_PATH, CP_WINANSI);
521 assert(size < MAX_PATH);
522 return (HDDEDATA)TRUE;
524 return (HDDEDATA)FALSE;
527 size = DdeGetData(hData, (LPBYTE)ddeExec, MAX_PATH, 0L);
528 assert(size < MAX_PATH);
529 DdeFreeDataHandle(hData);
530 if (post_quit_on_execute)
532 return (HDDEDATA)DDE_FACK;
540 * This is just to make sure the child won't run forever stuck in a GetMessage()
541 * loop when DDE fails for some reason.
543 static void CALLBACK childTimeout(HWND wnd, UINT msg, UINT_PTR timer, DWORD time)
545 trace("childTimeout called\n");
550 static void doChild(int argc, char** argv)
552 char *filename, longpath[MAX_PATH] = "";
563 hFile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
564 if (hFile == INVALID_HANDLE_VALUE)
568 childPrintf(hFile, "[Arguments]\r\n");
569 if (winetest_debug > 2)
571 trace("cmdlineA='%s'\n", GetCommandLineA());
572 trace("argcA=%d\n", argc);
574 childPrintf(hFile, "cmdlineA=%s\r\n", encodeA(GetCommandLineA()));
575 childPrintf(hFile, "argcA=%d\r\n", argc);
576 for (i = 0; i < argc; i++)
578 if (winetest_debug > 2)
579 trace("argvA%d='%s'\n", i, argv[i]);
580 childPrintf(hFile, "argvA%d=%s\r\n", i, encodeA(argv[i]));
582 GetModuleFileNameA(GetModuleHandleA(NULL), longpath, MAX_PATH);
583 childPrintf(hFile, "longPath=%s\r\n", encodeA(longpath));
585 map = OpenFileMappingA(FILE_MAP_READ, FALSE, "winetest_shlexec_dde_map");
588 shared_block = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 4096);
590 if (shared_block[0] != '\0' || shared_block[1] != '\0')
592 post_quit_on_execute = TRUE;
594 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
595 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
596 assert(rc == DMLERR_NO_ERROR);
597 hszApplication = DdeCreateStringHandleA(ddeInst, shared_block, CP_WINANSI);
598 hszTopic = DdeCreateStringHandleA(ddeInst, shared_block + strlen(shared_block) + 1, CP_WINANSI);
599 assert(hszApplication && hszTopic);
600 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER | DNS_FILTEROFF));
602 timer = SetTimer(NULL, 0, 2500, childTimeout);
604 dde_ready = OpenEvent(EVENT_MODIFY_STATE, FALSE, "winetest_shlexec_dde_ready");
606 CloseHandle(dde_ready);
608 while (GetMessage(&msg, NULL, 0, 0))
609 DispatchMessage(&msg);
612 KillTimer(NULL, timer);
613 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
614 assert(DdeFreeStringHandle(ddeInst, hszTopic));
615 assert(DdeFreeStringHandle(ddeInst, hszApplication));
616 assert(DdeUninitialize(ddeInst));
620 dde_ready = OpenEvent(EVENT_MODIFY_STATE, FALSE, "winetest_shlexec_dde_ready");
622 CloseHandle(dde_ready);
625 UnmapViewOfFile(shared_block);
627 childPrintf(hFile, "ddeExec=%s\r\n", encodeA(ddeExec));
632 init_event(filename);
637 static char* getChildString(const char* sect, const char* key)
642 GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), child_file);
643 if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
644 assert(!(strlen(buf) & 1));
649 static void dump_child(void)
651 if (winetest_debug > 1)
657 str=getChildString("Arguments", "cmdlineA");
658 trace("cmdlineA='%s'\n", str);
659 c=GetPrivateProfileIntA("Arguments", "argcA", -1, child_file);
660 trace("argcA=%d\n",c);
663 sprintf(key, "argvA%d", i);
664 str=getChildString("Arguments", key);
665 trace("%s='%s'\n", key, str);
670 static int StrCmpPath(const char* s1, const char* s2)
672 if (!s1 && !s2) return 0;
683 if ((*s1=='/' || *s1=='\\') && (*s2=='/' || *s2=='\\'))
685 while (*s1=='/' || *s1=='\\')
687 while (*s2=='/' || *s2=='\\')
690 else if (toupper(*s1)==toupper(*s2))
707 static void _okChildString(const char* file, int line, const char* key, const char* expected)
710 result=getChildString("Arguments", key);
713 ok_(file, line)(FALSE, "%s expected '%s', but key not found or empty\n", key, expected);
716 ok_(file, line)(lstrcmpiA(result, expected) == 0,
717 "%s expected '%s', got '%s'\n", key, expected, result);
720 static void _okChildPath(const char* file, int line, const char* key, const char* expected)
723 result=getChildString("Arguments", key);
726 ok_(file, line)(FALSE, "%s expected '%s', but key not found or empty\n", key, expected);
729 ok_(file, line)(StrCmpPath(result, expected) == 0,
730 "%s expected '%s', got '%s'\n", key, expected, result);
733 static void _okChildInt(const char* file, int line, const char* key, int expected)
736 result=GetPrivateProfileIntA("Arguments", key, expected, child_file);
737 ok_(file, line)(result == expected,
738 "%s expected %d, but got %d\n", key, expected, result);
741 #define okChildString(key, expected) _okChildString(__FILE__, __LINE__, (key), (expected))
742 #define okChildPath(key, expected) _okChildPath(__FILE__, __LINE__, (key), (expected))
743 #define okChildInt(key, expected) _okChildInt(__FILE__, __LINE__, (key), (expected))
747 * GetLongPathNameA equivalent that supports Win95 and WinNT
751 static DWORD get_long_path_name(const char* shortpath, char* longpath, DWORD longlen)
753 char tmplongpath[MAX_PATH];
755 DWORD sp = 0, lp = 0;
757 WIN32_FIND_DATAA wfd;
760 if (!shortpath || !shortpath[0])
763 if (shortpath[1] == ':')
765 tmplongpath[0] = shortpath[0];
766 tmplongpath[1] = ':';
770 while (shortpath[sp])
772 /* check for path delimiters and reproduce them */
773 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
775 if (!lp || tmplongpath[lp-1] != '\\')
777 /* strip double "\\" */
778 tmplongpath[lp++] = '\\';
780 tmplongpath[lp] = 0; /* terminate string */
786 if (sp == 0 && p[0] == '.' && (p[1] == '/' || p[1] == '\\'))
788 tmplongpath[lp++] = *p++;
789 tmplongpath[lp++] = *p++;
791 for (; *p && *p != '/' && *p != '\\'; p++);
792 tmplen = p - (shortpath + sp);
793 lstrcpyn(tmplongpath + lp, shortpath + sp, tmplen + 1);
794 /* Check if the file exists and use the existing file name */
795 goit = FindFirstFileA(tmplongpath, &wfd);
796 if (goit == INVALID_HANDLE_VALUE)
799 strcpy(tmplongpath + lp, wfd.cFileName);
800 lp += strlen(tmplongpath + lp);
803 tmplen = strlen(shortpath) - 1;
804 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
805 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
806 tmplongpath[lp++] = shortpath[tmplen];
809 tmplen = strlen(tmplongpath) + 1;
810 if (tmplen <= longlen)
812 strcpy(longpath, tmplongpath);
813 tmplen--; /* length without 0 */
825 static const char* testfiles[]=
827 "%s\\test file.shlexec",
828 "%s\\%%nasty%% $file.shlexec",
829 "%s\\test file.noassoc",
830 "%s\\test file.noassoc.shlexec",
831 "%s\\test file.shlexec.noassoc",
832 "%s\\test_shortcut_shlexec.lnk",
833 "%s\\test_shortcut_exe.lnk",
835 "%s\\test file.shlfoo",
837 "%s\\masked file.shlexec",
842 "%s\\simple.shlexec",
843 "%s\\drawback_file.noassoc",
844 "%s\\drawback_file.noassoc foo.shlexec",
845 "%s\\drawback_nonexist.noassoc foo.shlexec",
852 const char* basename;
857 static filename_tests_t filename_tests[]=
859 /* Test bad / nonexistent filenames */
860 {NULL, "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
861 {NULL, "%s\\nonexistent.noassoc", 0x0, SE_ERR_FNF},
864 {NULL, "%s\\test file.shlexec", 0x0, 33},
865 {NULL, "%s\\test file.shlexec.", 0x0, 33},
866 {NULL, "%s\\%%nasty%% $file.shlexec", 0x0, 33},
867 {NULL, "%s/test file.shlexec", 0x0, 33},
869 /* Test filenames with no association */
870 {NULL, "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
872 /* Test double extensions */
873 {NULL, "%s\\test file.noassoc.shlexec", 0x0, 33},
874 {NULL, "%s\\test file.shlexec.noassoc", 0x0, SE_ERR_NOASSOC},
876 /* Test alternate verbs */
877 {"LowerL", "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
878 {"LowerL", "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
880 {"QuotedLowerL", "%s\\test file.shlexec", 0x0, 33},
881 {"QuotedUpperL", "%s\\test file.shlexec", 0x0, 33},
883 /* Test file masked due to space */
884 {NULL, "%s\\masked file.shlexec", 0x1, 33},
885 /* Test if quoting prevents the masking */
886 {NULL, "%s\\masked file.shlexec", 0x40, 33},
891 static filename_tests_t noquotes_tests[]=
893 /* Test unquoted '%1' thingies */
894 {"NoQuotes", "%s\\test file.shlexec", 0xa, 33},
895 {"LowerL", "%s\\test file.shlexec", 0xa, 33},
896 {"UpperL", "%s\\test file.shlexec", 0xa, 33},
901 static void test_lpFile_parsed(void)
903 char fileA[MAX_PATH];
906 /* existing "drawback_file.noassoc" prevents finding "drawback_file.noassoc foo.shlexec" on wine */
907 sprintf(fileA, "%s\\drawback_file.noassoc foo.shlexec", tmpdir);
908 rc=shell_execute(NULL, fileA, NULL, NULL);
909 todo_wine ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
911 /* if quoted, existing "drawback_file.noassoc" not prevents finding "drawback_file.noassoc foo.shlexec" on wine */
912 sprintf(fileA, "\"%s\\drawback_file.noassoc foo.shlexec\"", tmpdir);
913 rc=shell_execute(NULL, fileA, NULL, NULL);
914 ok(rc > 32 || broken(rc == SE_ERR_FNF) /* Win95/NT4 */,
915 "%s failed: rc=%lu\n", shell_call, rc);
917 /* error should be SE_ERR_FNF, not SE_ERR_NOASSOC */
918 sprintf(fileA, "\"%s\\drawback_file.noassoc\" foo.shlexec", tmpdir);
919 rc=shell_execute(NULL, fileA, NULL, NULL);
920 ok(rc == SE_ERR_FNF, "%s succeeded: rc=%lu\n", shell_call, rc);
922 /* ""command"" not works on wine (and real win9x and w2k) */
923 sprintf(fileA, "\"\"%s\\simple.shlexec\"\"", tmpdir);
924 rc=shell_execute(NULL, fileA, NULL, NULL);
925 todo_wine ok(rc > 32 || broken(rc == SE_ERR_FNF) /* Win9x/2000 */,
926 "%s failed: rc=%lu\n", shell_call, rc);
928 /* nonexisting "drawback_nonexist.noassoc" not prevents finding "drawback_nonexist.noassoc foo.shlexec" on wine */
929 sprintf(fileA, "%s\\drawback_nonexist.noassoc foo.shlexec", tmpdir);
930 rc=shell_execute(NULL, fileA, NULL, NULL);
931 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
933 /* is SEE_MASK_DOENVSUBST default flag? Should only be when XP emulates 9x (XP bug or real 95 or ME behavior ?) */
934 rc=shell_execute(NULL, "%TMPDIR%\\simple.shlexec", NULL, NULL);
935 todo_wine ok(rc == SE_ERR_FNF, "%s succeeded: rc=%lu\n", shell_call, rc);
938 rc=shell_execute(NULL, "\"%TMPDIR%\\simple.shlexec\"", NULL, NULL);
939 todo_wine ok(rc == SE_ERR_FNF, "%s succeeded: rc=%lu\n", shell_call, rc);
941 /* test SEE_MASK_DOENVSUBST works */
942 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI,
943 NULL, "%TMPDIR%\\simple.shlexec", NULL, NULL, NULL);
944 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
946 /* quoted lpFile does not work on real win95 and nt4 */
947 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI,
948 NULL, "\"%TMPDIR%\\simple.shlexec\"", NULL, NULL, NULL);
949 ok(rc > 32 || broken(rc == SE_ERR_FNF) /* Win95/NT4 */,
950 "%s failed: rc=%lu\n", shell_call, rc);
956 const char* args[11];
960 static const cmdline_tests_t cmdline_tests[] =
965 {"exe arg1 arg2 \"arg three\" 'four five` six\\ $even)",
966 {"exe", "arg1", "arg2", "arg three", "'four", "five`", "six\\", "$even)", NULL}, 0},
968 {"exe arg=1 arg-2 three\tfour\rfour\nfour ",
969 {"exe", "arg=1", "arg-2", "three", "four\rfour\nfour", NULL}, 0},
971 {"exe arg\"one\" \"second\"arg thirdarg ",
972 {"exe", "argone", "secondarg", "thirdarg", NULL}, 0},
974 /* Don't lose unclosed quoted arguments */
975 {"exe arg1 \"unclosed",
976 {"exe", "arg1", "unclosed", NULL}, 0},
979 {"exe", "arg1", "", NULL}, 0},
981 /* cmd's metacharacters have no special meaning */
982 {"exe \"one^\" \"arg\"&two three|four",
983 {"exe", "one^", "arg&two", "three|four", NULL}, 0},
985 /* Environment variables are not interpreted either */
987 {"exe", "%TMPDIR%", "%2", NULL}, 0},
989 /* If not followed by a quote, backslashes go through as is */
990 {"exe o\\ne t\\\\wo t\\\\\\ree f\\\\\\\\our ",
991 {"exe", "o\\ne", "t\\\\wo", "t\\\\\\ree", "f\\\\\\\\our", NULL}, 0},
993 {"exe \"o\\ne\" \"t\\\\wo\" \"t\\\\\\ree\" \"f\\\\\\\\our\" ",
994 {"exe", "o\\ne", "t\\\\wo", "t\\\\\\ree", "f\\\\\\\\our", NULL}, 0},
996 /* When followed by a quote their number is halved and the remainder
999 {"exe \\\"one \\\\\"two\" \\\\\\\"three \\\\\\\\\"four\" end",
1000 {"exe", "\"one", "\\two", "\\\"three", "\\\\four", "end", NULL}, 0},
1002 {"exe \"one\\\" still\" \"two\\\\\" \"three\\\\\\\" still\" \"four\\\\\\\\\" end",
1003 {"exe", "one\" still", "two\\", "three\\\" still", "four\\\\", "end", NULL}, 0},
1005 /* One can put a quote in an unquoted string by tripling it, that is in
1006 * effect quoting it like so """ -> ". The general rule is as follows:
1007 * 3n quotes -> n quotes
1008 * 3n+1 quotes -> n quotes plus start of a quoted string
1009 * 3n+2 quotes -> n quotes (plus an empty string from the remaining pair)
1010 * Nicely, when n is 0 we get the standard rules back.
1012 {"exe two\"\"quotes next",
1013 {"exe", "twoquotes", "next", NULL}, 0},
1015 {"exe three\"\"\"quotes next",
1016 {"exe", "three\"quotes", "next", NULL}, 0},
1018 {"exe four\"\"\"\" quotes\" next 4%3=1",
1019 {"exe", "four\" quotes", "next", "4%3=1", NULL}, 0},
1021 {"exe five\"\"\"\"\"quotes next",
1022 {"exe", "five\"quotes", "next", NULL}, 0},
1024 {"exe six\"\"\"\"\"\"quotes next",
1025 {"exe", "six\"\"quotes", "next", NULL}, 0},
1027 {"exe seven\"\"\"\"\"\"\" quotes\" next 7%3=1",
1028 {"exe", "seven\"\" quotes", "next", "7%3=1", NULL}, 0},
1030 {"exe twelve\"\"\"\"\"\"\"\"\"\"\"\"quotes next",
1031 {"exe", "twelve\"\"\"\"quotes", "next", NULL}, 0},
1033 {"exe thirteen\"\"\"\"\"\"\"\"\"\"\"\"\" quotes\" next 13%3=1",
1034 {"exe", "thirteen\"\"\"\" quotes", "next", "13%3=1", NULL}, 0},
1036 /* Inside a quoted string the opening quote is added to the set of
1037 * consecutive quotes to get the effective quotes count. This gives:
1038 * 1+3n quotes -> n quotes
1039 * 1+3n+1 quotes -> n quotes plus closes the quoted string
1040 * 1+3n+2 quotes -> n+1 quotes plus closes the quoted string
1042 {"exe \"two\"\"quotes next",
1043 {"exe", "two\"quotes", "next", NULL}, 0},
1045 {"exe \"two\"\" next",
1046 {"exe", "two\"", "next", NULL}, 0},
1048 {"exe \"three\"\"\" quotes\" next 4%3=1",
1049 {"exe", "three\" quotes", "next", "4%3=1", NULL}, 0},
1051 {"exe \"four\"\"\"\"quotes next",
1052 {"exe", "four\"quotes", "next", NULL}, 0},
1054 {"exe \"five\"\"\"\"\"quotes next",
1055 {"exe", "five\"\"quotes", "next", NULL}, 0},
1057 {"exe \"six\"\"\"\"\"\" quotes\" next 7%3=1",
1058 {"exe", "six\"\" quotes", "next", "7%3=1", NULL}, 0},
1060 {"exe \"eleven\"\"\"\"\"\"\"\"\"\"\"quotes next",
1061 {"exe", "eleven\"\"\"\"quotes", "next", NULL}, 0},
1063 {"exe \"twelve\"\"\"\"\"\"\"\"\"\"\"\" quotes\" next 13%3=1",
1064 {"exe", "twelve\"\"\"\" quotes", "next", "13%3=1", NULL}, 0},
1066 /* Escaped consecutive quotes are fun */
1067 {"exe \"the crazy \\\\\"\"\"\\\\\" quotes",
1068 {"exe", "the crazy \\\"\\", "quotes", NULL}, 0},
1070 /* The executable path has its own rules!!!
1071 * - Backslashes have no special meaning.
1072 * - If the first character is a quote, then the second quote ends the
1074 * - The previous rule holds even if the next character is not a space!
1075 * - If the first character is not a quote, then quotes have no special
1076 * meaning either and the executable path stops at the first space.
1077 * - The consecutive quotes rules don't apply either.
1078 * - Even if there is no space between the executable path and the first
1079 * argument, the latter is parsed using the regular rules.
1081 {"exe\"file\"path arg1",
1082 {"exe\"file\"path", "arg1", NULL}, 0},
1084 {"exe\"file\"path\targ1",
1085 {"exe\"file\"path", "arg1", NULL}, 0},
1087 {"exe\"path\\ arg1",
1088 {"exe\"path\\", "arg1", NULL}, 0},
1090 {"\\\"exe \"arg one\"",
1091 {"\\\"exe", "arg one", NULL}, 0},
1093 {"\"spaced exe\" \"next arg\"",
1094 {"spaced exe", "next arg", NULL}, 0},
1096 {"\"spaced exe\"\t\"next arg\"",
1097 {"spaced exe", "next arg", NULL}, 0},
1099 {"\"exe\"arg\" one\" argtwo",
1100 {"exe", "arg one", "argtwo", NULL}, 0},
1102 {"\"spaced exe\\\"arg1 arg2",
1103 {"spaced exe\\", "arg1", "arg2", NULL}, 0},
1106 {"two", " arg1 ", NULL}, 0},
1108 {"\"three\"\"\" arg2",
1109 {"three", "", "arg2", NULL}, 0},
1111 {"\"four\"\"\"\"arg1",
1112 {"four", "\"arg1", NULL}, 0},
1114 /* If the first character is a space then the executable path is empty */
1115 {" \"arg\"one argtwo",
1116 {"", "argone", "argtwo", NULL}, 0},
1121 static BOOL test_one_cmdline(const cmdline_tests_t* test)
1123 WCHAR cmdW[MAX_PATH], argW[MAX_PATH];
1129 /* trace("----- cmd='%s'\n", test->cmd); */
1130 MultiByteToWideChar(CP_ACP, 0, test->cmd, -1, cmdW, sizeof(cmdW)/sizeof(*cmdW));
1131 argsW = cl2a = CommandLineToArgvW(cmdW, &cl2a_count);
1132 if (argsW == NULL && cl2a_count == -1)
1134 win_skip("CommandLineToArgvW not implemented, skipping\n");
1139 while (test->args[count])
1141 if ((test->todo & 0x1) == 0)
1142 ok(cl2a_count == count, "%s: expected %d arguments, but got %d\n", test->cmd, count, cl2a_count);
1144 ok(cl2a_count == count, "%s: expected %d arguments, but got %d\n", test->cmd, count, cl2a_count);
1146 for (i = 0; i < cl2a_count; i++)
1150 MultiByteToWideChar(CP_ACP, 0, test->args[i], -1, argW, sizeof(argW)/sizeof(*argW));
1151 if ((test->todo & (1 << (i+4))) == 0)
1152 ok(!lstrcmpW(*argsW, argW), "%s: arg[%d] expected %s but got %s\n", test->cmd, i, wine_dbgstr_w(argW), wine_dbgstr_w(*argsW));
1154 ok(!lstrcmpW(*argsW, argW), "%s: arg[%d] expected %s but got %s\n", test->cmd, i, wine_dbgstr_w(argW), wine_dbgstr_w(*argsW));
1156 else if ((test->todo & 0x1) == 0)
1157 ok(0, "%s: got extra arg[%d]=%s\n", test->cmd, i, wine_dbgstr_w(*argsW));
1159 ok(0, "%s: got extra arg[%d]=%s\n", test->cmd, i, wine_dbgstr_w(*argsW));
1166 static void test_commandline2argv(void)
1168 static const WCHAR exeW[] = {'e','x','e',0};
1169 const cmdline_tests_t* test;
1170 WCHAR strW[MAX_PATH];
1175 test = cmdline_tests;
1178 if (!test_one_cmdline(test))
1183 SetLastError(0xdeadbeef);
1184 args = CommandLineToArgvW(exeW, NULL);
1185 le = GetLastError();
1186 ok(args == NULL && le == ERROR_INVALID_PARAMETER, "expected NULL with ERROR_INVALID_PARAMETER got %p with %u\n", args, le);
1188 SetLastError(0xdeadbeef);
1189 args = CommandLineToArgvW(NULL, NULL);
1190 le = GetLastError();
1191 ok(args == NULL && le == ERROR_INVALID_PARAMETER, "expected NULL with ERROR_INVALID_PARAMETER got %p with %u\n", args, le);
1194 args = CommandLineToArgvW(strW, &numargs);
1195 ok(numargs == 1, "expected 1 args, got %d\n", numargs);
1198 GetModuleFileNameW(NULL, strW, sizeof(strW)/sizeof(*strW));
1199 ok(!lstrcmpW(args[0], strW), "wrong path to the current executable: %s instead of %s\n", wine_dbgstr_w(args[0]), wine_dbgstr_w(strW));
1201 if (args) LocalFree(args);
1204 /* The goal here is to analyze how ShellExecute() builds the command that
1205 * will be run. The tricky part is that there are three transformation
1206 * steps between the 'parameters' string we pass to ShellExecute() and the
1207 * argument list we observe in the child process:
1208 * - The parsing of 'parameters' string into individual arguments. The tests
1209 * show this is done differently from both CreateProcess() and
1210 * CommandLineToArgv()!
1211 * - The way the command 'formatting directives' such as %1, %2, etc are
1213 * - And the way the resulting command line is then parsed to yield the
1214 * argument list we check.
1221 cmdline_tests_t cmd;
1224 static const argify_tests_t argify_tests[] =
1226 /* Start with three simple parameters. Notice that one can reorder and
1227 * duplicate the parameters. Also notice how %* take the raw input
1228 * parameters string, including the trailing spaces, no matter what
1229 * arguments have already been used.
1231 {"Params232S", "p2 p3 p4 ", 0xc2,
1232 {" p2 p3 \"p2\" \"p2 p3 p4 \"",
1233 {"", "p2", "p3", "p2", "p2 p3 p4 ", NULL}, 0}},
1235 /* Unquoted argument references like %2 don't automatically quote their
1236 * argument. Similarly, when they are quoted they don't escape the quotes
1237 * that their argument may contain.
1239 {"Params232S", "\"p two\" p3 p4 ", 0x3f3,
1240 {" p two p3 \"p two\" \"\"p two\" p3 p4 \"",
1241 {"", "p", "two", "p3", "p two", "p", "two p3 p4 ", NULL}, 0}},
1243 /* Only single digits are supported so only %1 to %9. Shown here with %20
1244 * because %10 is a pain.
1246 {"Params20", "p", 0,
1248 {"", "p0", NULL}, 0}},
1250 /* Only (double-)quotes have a special meaning. */
1251 {"Params23456", "'p2 p3` p4\\ $even", 0x40,
1252 {" \"'p2\" \"p3`\" \"p4\\\" \"$even\" \"\"",
1253 {"", "'p2", "p3`", "p4\" $even \"", NULL}, 0}},
1255 {"Params23456", "p=2 p-3 p4\tp4\rp4\np4", 0x1c2,
1256 {" \"p=2\" \"p-3\" \"p4\tp4\rp4\np4\" \"\" \"\"",
1257 {"", "p=2", "p-3", "p4\tp4\rp4\np4", "", "", NULL}, 0}},
1259 /* In unquoted strings, quotes are treated are a parameter separator just
1260 * like spaces! However they can be doubled to get a literal quote.
1262 * 2n quotes -> n quotes
1263 * 2n+1 quotes -> n quotes and a parameter separator
1265 {"Params23456789", "one\"quote \"p four\" one\"quote p7", 0xff3,
1266 {" \"one\" \"quote\" \"p four\" \"one\" \"quote\" \"p7\" \"\" \"\"",
1267 {"", "one", "quote", "p four", "one", "quote", "p7", "", "", NULL}, 0}},
1269 {"Params23456789", "two\"\"quotes \"p three\" two\"\"quotes p5", 0xf2,
1270 {" \"two\"quotes\" \"p three\" \"two\"quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1271 {"", "twoquotes p", "three twoquotes", "p5", "", "", "", "", NULL}, 0}},
1273 {"Params23456789", "three\"\"\"quotes \"p four\" three\"\"\"quotes p6", 0xff3,
1274 {" \"three\"\" \"quotes\" \"p four\" \"three\"\" \"quotes\" \"p6\" \"\" \"\"",
1275 {"", "three\"", "quotes", "p four", "three\"", "quotes", "p6", "", "", NULL}, 0}},
1277 {"Params23456789", "four\"\"\"\"quotes \"p three\" four\"\"\"\"quotes p5", 0xf3,
1278 {" \"four\"\"quotes\" \"p three\" \"four\"\"quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1279 {"", "four\"quotes p", "three fourquotes p5 \"", "", "", "", NULL}, 0}},
1281 /* Quoted strings cannot be continued by tacking on a non space character
1284 {"Params23456", "\"p two\"p3 \"p four\"p5 p6", 0x1f3,
1285 {" \"p two\" \"p3\" \"p four\" \"p5\" \"p6\"",
1286 {"", "p two", "p3", "p four", "p5", "p6", NULL}, 0}},
1288 /* In quoted strings, the quotes are halved and an odd number closes the
1289 * string. Specifically:
1290 * 2n quotes -> n quotes
1291 * 2n+1 quotes -> n quotes and closes the string and hence the parameter
1293 {"Params23456789", "\"one q\"uote \"p four\" \"one q\"uote p7", 0xff3,
1294 {" \"one q\" \"uote\" \"p four\" \"one q\" \"uote\" \"p7\" \"\" \"\"",
1295 {"", "one q", "uote", "p four", "one q", "uote", "p7", "", "", NULL}, 0}},
1297 {"Params23456789", "\"two \"\" quotes\" \"p three\" \"two \"\" quotes\" p5", 0x1ff3,
1298 {" \"two \" quotes\" \"p three\" \"two \" quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1299 {"", "two ", "quotes p", "three two", " quotes", "p5", "", "", "", "", NULL}, 0}},
1301 {"Params23456789", "\"three q\"\"\"uotes \"p four\" \"three q\"\"\"uotes p7", 0xff3,
1302 {" \"three q\"\" \"uotes\" \"p four\" \"three q\"\" \"uotes\" \"p7\" \"\" \"\"",
1303 {"", "three q\"", "uotes", "p four", "three q\"", "uotes", "p7", "", "", NULL}, 0}},
1305 {"Params23456789", "\"four \"\"\"\" quotes\" \"p three\" \"four \"\"\"\" quotes\" p5", 0xff3,
1306 {" \"four \"\" quotes\" \"p three\" \"four \"\" quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1307 {"", "four \"", "quotes p", "three four", "", "quotes p5 \"", "", "", "", NULL}, 0}},
1309 /* The quoted string rules also apply to consecutive quotes at the start
1310 * of a parameter but don't count the opening quote!
1312 {"Params23456789", "\"\"twoquotes \"p four\" \"\"twoquotes p7", 0xbf3,
1313 {" \"\" \"twoquotes\" \"p four\" \"\" \"twoquotes\" \"p7\" \"\" \"\"",
1314 {"", "", "twoquotes", "p four", "", "twoquotes", "p7", "", "", NULL}, 0}},
1316 {"Params23456789", "\"\"\"three quotes\" \"p three\" \"\"\"three quotes\" p5", 0x6f3,
1317 {" \"\"three quotes\" \"p three\" \"\"three quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1318 {"", "three", "quotes p", "three \"three", "quotes p5 \"", "", "", "", NULL}, 0}},
1320 {"Params23456789", "\"\"\"\"fourquotes \"p four\" \"\"\"\"fourquotes p7", 0xbf3,
1321 {" \"\"\" \"fourquotes\" \"p four\" \"\"\" \"fourquotes\" \"p7\" \"\" \"\"",
1322 {"", "\"", "fourquotes", "p four", "\"", "fourquotes", "p7", "", "", NULL}, 0}},
1324 /* An unclosed quoted string gets lost! */
1325 {"Params23456", "p2 \"p3\" \"p4 is lost", 0x1c3,
1326 {" \"p2\" \"p3\" \"\" \"\" \"\"",
1327 {"", "p2", "p3", "", "", "", NULL}, 0}},
1329 /* Backslashes have no special meaning even when preceding quotes. All
1330 * they do is start an unquoted string.
1332 {"Params23456", "\\\"p\\three \"pfour\\\" pfive", 0x73,
1333 {" \"\\\" \"p\\three\" \"pfour\\\" \"pfive\" \"\"",
1334 {"", "\" p\\three pfour\"", "pfive", "", NULL}, 0}},
1336 /* Environment variables are left untouched. */
1337 {"Params23456", "%TMPDIR% %t %c", 0,
1338 {" \"%TMPDIR%\" \"%t\" \"%c\" \"\" \"\"",
1339 {"", "%TMPDIR%", "%t", "%c", "", "", NULL}, 0}},
1341 /* %~2 is equivalent to %*. However %~3 and higher include the spaces
1342 * before the parameter!
1343 * (but not the previous parameter's closing quote fortunately)
1345 {"Params2345Etc", "p2 p3 \"p4\" p5 p6 ", 0x3f3,
1346 {" ~2=\"p2 p3 \"p4\" p5 p6 \" ~3=\" p3 \"p4\" p5 p6 \" ~4=\" \"p4\" p5 p6 \" ~5= p5 p6 ",
1347 {"", "~2=p2 p3 p4 p5 p6 ", "~3= p3 p4 p5 p6 ", "~4= p4 p5 p6 ", "~5=", "p5", "p6", NULL}, 0}},
1349 /* %~n works even if there is no nth parameter. */
1350 {"Params9Etc", "p2 p3 p4 p5 p6 p7 p8 ", 0x12,
1352 {"", "~9= ", NULL}, 0}},
1354 {"Params9Etc", "p2 p3 p4 p5 p6 p7 ", 0x12,
1356 {"", "~9=", NULL}, 0}},
1358 /* The %~n directives also transmit the tenth parameter and beyond. */
1359 {"Params9Etc", "p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 and beyond!", 0x12,
1360 {" ~9=\" p9 p10 p11 and beyond!\"",
1361 {"", "~9= p9 p10 p11 and beyond!", NULL}, 0}},
1363 /* Bad formatting directives lose their % sign, except those followed by
1364 * a tilde! Environment variables are not expanded but lose their % sign.
1366 {"ParamsBad", "p2 p3 p4 p5", 0x12,
1367 {" \"% - %~ %~0 %~1 %~a %~* a b c TMPDIR\"",
1368 {"", "% - %~ %~0 %~1 %~a %~* a b c TMPDIR", NULL}, 0}},
1370 {NULL, NULL, 0, {NULL, {NULL}, 0}}
1373 static void test_argify(void)
1375 BOOL has_cl2a = TRUE;
1376 char fileA[MAX_PATH], params[2*MAX_PATH+12];
1378 const argify_tests_t* test;
1382 create_test_verb(".shlexec", "Params232S", 0, "Params232S %2 %3 \"%2\" \"%*\"");
1383 create_test_verb(".shlexec", "Params23456", 0, "Params23456 \"%2\" \"%3\" \"%4\" \"%5\" \"%6\"");
1384 create_test_verb(".shlexec", "Params23456789", 0, "Params23456789 \"%2\" \"%3\" \"%4\" \"%5\" \"%6\" \"%7\" \"%8\" \"%9\"");
1385 create_test_verb(".shlexec", "Params2345Etc", 0, "Params2345Etc ~2=\"%~2\" ~3=\"%~3\" ~4=\"%~4\" ~5=%~5");
1386 create_test_verb(".shlexec", "Params9Etc", 0, "Params9Etc ~9=\"%~9\"");
1387 create_test_verb(".shlexec", "Params20", 0, "Params20 \"%20\"");
1388 create_test_verb(".shlexec", "ParamsBad", 0, "ParamsBad \"%% %- %~ %~0 %~1 %~a %~* %a %b %c %TMPDIR%\"");
1390 sprintf(fileA, "%s\\test file.shlexec", tmpdir);
1392 test = argify_tests;
1393 while (test->params)
1395 /* trace("***** verb='%s' params='%s'\n", test->verb, test->params); */
1396 rc = shell_execute_ex(SEE_MASK_DOENVSUBST, test->verb, fileA, test->params, NULL, NULL);
1397 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
1400 while (test->cmd.args[count])
1402 if ((test->todo & 0x1) == 0)
1403 /* +4 for the shlexec arguments, -1 because of the added ""
1404 * argument for the CommandLineToArgvW() tests.
1406 okChildInt("argcA", 4 + count - 1);
1408 okChildInt("argcA", 4 + count - 1);
1410 cmd = getChildString("Arguments", "cmdlineA");
1411 /* Our commands are such that the verb immediately precedes the
1412 * part we are interested in.
1414 if (cmd) cmd = strstr(cmd, test->verb);
1415 if (cmd) cmd += strlen(test->verb);
1416 if (!cmd) cmd = "(null)";
1417 if ((test->todo & 0x2) == 0)
1418 ok(!strcmp(cmd, test->cmd.cmd), "%s: the cmdline is '%s' instead of '%s'\n", shell_call, cmd, test->cmd.cmd);
1420 ok(!strcmp(cmd, test->cmd.cmd), "%s: the cmdline is '%s' instead of '%s'\n", shell_call, cmd, test->cmd.cmd);
1422 for (i = 0; i < count - 1; i++)
1425 sprintf(argname, "argvA%d", 4 + i);
1426 if ((test->todo & (1 << (i+4))) == 0)
1427 okChildString(argname, test->cmd.args[i+1]);
1429 okChildString(argname, test->cmd.args[i+1]);
1433 has_cl2a = test_one_cmdline(&(test->cmd));
1437 /* Test with a long parameter */
1438 for (rc = 0; rc < MAX_PATH; rc++)
1439 fileA[rc] = 'a' + rc % 26;
1440 fileA[MAX_PATH-1] = '\0';
1441 sprintf(params, "shlexec \"%s\" %s", child_file, fileA);
1443 /* We need NOZONECHECKS on Win2003 to block a dialog */
1444 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params, NULL, NULL);
1445 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
1446 okChildInt("argcA", 4);
1447 okChildString("argvA3", fileA);
1450 static void test_filename(void)
1452 char filename[MAX_PATH];
1453 const filename_tests_t* test;
1457 test=filename_tests;
1458 while (test->basename)
1460 BOOL quotedfile = FALSE;
1462 if (skip_noassoc_tests && test->rc == SE_ERR_NOASSOC)
1464 win_skip("Skipping shellexecute of file with unassociated extension\n");
1469 sprintf(filename, test->basename, tmpdir);
1470 if (strchr(filename, '/'))
1480 if ((test->todo & 0x40)==0)
1482 rc=shell_execute(test->verb, filename, NULL, NULL);
1486 char quoted[MAX_PATH + 2];
1489 sprintf(quoted, "\"%s\"", filename);
1490 rc=shell_execute(test->verb, quoted, NULL, NULL);
1494 if ((test->todo & 0x1)==0)
1497 broken(quotedfile && rc == SE_ERR_FNF), /* NT4 */
1498 "%s failed: rc=%ld err=%u\n", shell_call,
1499 rc, GetLastError());
1503 ok(rc==test->rc, "%s failed: rc=%ld err=%u\n", shell_call,
1504 rc, GetLastError());
1509 if ((test->todo & 0x2)==0)
1511 okChildInt("argcA", 5);
1515 okChildInt("argcA", 5);
1517 verb=(test->verb ? test->verb : "Open");
1518 if ((test->todo & 0x4)==0)
1520 okChildString("argvA3", verb);
1524 okChildString("argvA3", verb);
1526 if ((test->todo & 0x8)==0)
1528 okChildPath("argvA4", filename);
1532 okChildPath("argvA4", filename);
1538 test=noquotes_tests;
1539 while (test->basename)
1541 sprintf(filename, test->basename, tmpdir);
1542 rc=shell_execute(test->verb, filename, NULL, NULL);
1545 if ((test->todo & 0x1)==0)
1547 ok(rc==test->rc, "%s failed: rc=%ld err=%u\n", shell_call,
1548 rc, GetLastError());
1552 ok(rc==test->rc, "%s failed: rc=%ld err=%u\n", shell_call,
1553 rc, GetLastError());
1561 verb=(test->verb ? test->verb : "Open");
1562 if ((test->todo & 0x4)==0)
1564 okChildString("argvA3", verb);
1568 okChildString("argvA3", verb);
1577 space=strchr(str, ' ');
1580 sprintf(attrib, "argvA%d", count);
1581 if ((test->todo & 0x8)==0)
1583 okChildPath(attrib, str);
1587 okChildPath(attrib, str);
1594 if ((test->todo & 0x2)==0)
1596 okChildInt("argcA", count);
1600 okChildInt("argcA", count);
1606 if (dllver.dwMajorVersion != 0)
1608 /* The more recent versions of shell32.dll accept quoted filenames
1609 * while older ones (e.g. 4.00) don't. Still we want to test this
1610 * because IE 6 depends on the new behavior.
1611 * One day we may need to check the exact version of the dll but for
1612 * now making sure DllGetVersion() is present is sufficient.
1614 sprintf(filename, "\"%s\\test file.shlexec\"", tmpdir);
1615 rc=shell_execute(NULL, filename, NULL, NULL);
1616 ok(rc > 32, "%s failed: rc=%ld err=%u\n", shell_call, rc,
1618 okChildInt("argcA", 5);
1619 okChildString("argvA3", "Open");
1620 sprintf(filename, "%s\\test file.shlexec", tmpdir);
1621 okChildPath("argvA4", filename);
1627 const char* urlprefix;
1628 const char* basename;
1633 #define URL_SUCCESS 0x1
1634 #define USE_COLON 0x2
1635 #define USE_BSLASH 0x4
1637 static fileurl_tests_t fileurl_tests[]=
1639 /* How many slashes does it take... */
1640 {"file:", "%s\\test file.shlexec", URL_SUCCESS, 0},
1641 {"file:/", "%s\\test file.shlexec", URL_SUCCESS, 0},
1642 {"file://", "%s\\test file.shlexec", URL_SUCCESS, 0},
1643 {"file:///", "%s\\test file.shlexec", URL_SUCCESS, 0},
1644 {"File:///", "%s\\test file.shlexec", URL_SUCCESS, 0},
1645 {"file:////", "%s\\test file.shlexec", URL_SUCCESS, 0},
1646 {"file://///", "%s\\test file.shlexec", 0, 0},
1648 /* Test with Windows-style paths */
1649 {"file:///", "%s\\test file.shlexec", URL_SUCCESS | USE_COLON, 0},
1650 {"file:///", "%s\\test file.shlexec", URL_SUCCESS | USE_BSLASH, 0},
1652 /* Check handling of hostnames */
1653 {"file://localhost/", "%s\\test file.shlexec", URL_SUCCESS, 0},
1654 {"file://localhost:80/", "%s\\test file.shlexec", 0, 0},
1655 {"file://LocalHost/", "%s\\test file.shlexec", URL_SUCCESS, 0},
1656 {"file://127.0.0.1/", "%s\\test file.shlexec", 0, 0},
1657 {"file://::1/", "%s\\test file.shlexec", 0, 0},
1658 {"file://notahost/", "%s\\test file.shlexec", 0, 0},
1660 /* Environment variables are not expanded in URLs */
1661 {"%urlprefix%", "%s\\test file.shlexec", 0, 0x1},
1662 {"file:///", "%%TMPDIR%%\\test file.shlexec", 0, 0},
1664 /* Test shortcuts vs. URLs */
1665 {"file://///", "%s\\test_shortcut_shlexec.lnk", 0, 0x1d},
1670 static void test_fileurls(void)
1672 char filename[MAX_PATH], fileurl[MAX_PATH], longtmpdir[MAX_PATH];
1673 char command[MAX_PATH];
1674 const fileurl_tests_t* test;
1678 rc = (INT_PTR)ShellExecute(NULL, NULL, "file:///nosuchfile.shlexec", NULL, NULL, SW_SHOWNORMAL);
1681 win_skip("shell32 is too old (likely < 4.72). Skipping the file URL tests\n");
1685 get_long_path_name(tmpdir, longtmpdir, sizeof(longtmpdir)/sizeof(*longtmpdir));
1686 SetEnvironmentVariable("urlprefix", "file:///");
1689 while (test->basename)
1691 /* Build the file URL */
1692 sprintf(filename, test->basename, longtmpdir);
1693 strcpy(fileurl, test->urlprefix);
1694 strcat(fileurl, filename);
1695 s = fileurl + strlen(test->urlprefix);
1698 if (!(test->flags & USE_COLON) && *s == ':')
1700 else if (!(test->flags & USE_BSLASH) && *s == '\\')
1705 /* Test it first with FindExecutable() */
1706 rc = (INT_PTR)FindExecutableA(fileurl, NULL, command);
1707 ok(rc == SE_ERR_FNF, "FindExecutable(%s) failed: bad rc=%lu\n", fileurl, rc);
1709 /* Then ShellExecute() */
1710 if ((test->todo & 0x10) == 0)
1711 rc = shell_execute(NULL, fileurl, NULL, NULL);
1713 rc = shell_execute(NULL, fileurl, NULL, NULL);
1714 if (bad_shellexecute)
1716 win_skip("shell32 is too old (likely 4.72). Skipping the file URL tests\n");
1719 if (test->flags & URL_SUCCESS)
1721 if ((test->todo & 0x1) == 0)
1722 ok(rc > 32, "%s failed: bad rc=%lu\n", shell_call, rc);
1724 ok(rc > 32, "%s failed: bad rc=%lu\n", shell_call, rc);
1728 if ((test->todo & 0x1) == 0)
1729 ok(rc == SE_ERR_FNF || rc == SE_ERR_PNF ||
1730 broken(rc == SE_ERR_ACCESSDENIED) /* win2000 */,
1731 "%s failed: bad rc=%lu\n", shell_call, rc);
1733 ok(rc == SE_ERR_FNF || rc == SE_ERR_PNF ||
1734 broken(rc == SE_ERR_ACCESSDENIED) /* win2000 */,
1735 "%s failed: bad rc=%lu\n", shell_call, rc);
1739 if ((test->todo & 0x2) == 0)
1740 okChildInt("argcA", 5);
1742 okChildInt("argcA", 5);
1744 if ((test->todo & 0x4) == 0)
1745 okChildString("argvA3", "Open");
1747 okChildString("argvA3", "Open");
1749 if ((test->todo & 0x8) == 0)
1750 okChildPath("argvA4", filename);
1752 okChildPath("argvA4", filename);
1757 SetEnvironmentVariable("urlprefix", NULL);
1760 static void test_find_executable(void)
1762 char notepad_path[MAX_PATH];
1763 char filename[MAX_PATH];
1764 char command[MAX_PATH];
1765 const filename_tests_t* test;
1768 if (!create_test_association(".sfe"))
1770 skip("Unable to create association for '.sfe'\n");
1773 create_test_verb(".sfe", "Open", 1, "%1");
1775 /* Don't test FindExecutable(..., NULL), it always crashes */
1777 strcpy(command, "your word");
1778 if (0) /* Can crash on Vista! */
1780 rc=(INT_PTR)FindExecutableA(NULL, NULL, command);
1781 ok(rc == SE_ERR_FNF || rc > 32 /* nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
1782 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
1785 GetSystemDirectoryA( notepad_path, MAX_PATH );
1786 strcat( notepad_path, "\\notepad.exe" );
1788 /* Search for something that should be in the system-wide search path (no default directory) */
1789 strcpy(command, "your word");
1790 rc=(INT_PTR)FindExecutableA("notepad.exe", NULL, command);
1791 ok(rc > 32, "FindExecutable(%s) returned %ld\n", "notepad.exe", rc);
1792 ok(strcasecmp(command, notepad_path) == 0, "FindExecutable(%s) returned command=[%s]\n", "notepad.exe", command);
1794 /* Search for something that should be in the system-wide search path (with default directory) */
1795 strcpy(command, "your word");
1796 rc=(INT_PTR)FindExecutableA("notepad.exe", tmpdir, command);
1797 ok(rc > 32, "FindExecutable(%s) returned %ld\n", "notepad.exe", rc);
1798 ok(strcasecmp(command, notepad_path) == 0, "FindExecutable(%s) returned command=[%s]\n", "notepad.exe", command);
1800 strcpy(command, "your word");
1801 rc=(INT_PTR)FindExecutableA(tmpdir, NULL, command);
1802 ok(rc == SE_ERR_NOASSOC /* >= win2000 */ || rc > 32 /* win98, nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
1803 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
1805 sprintf(filename, "%s\\test file.sfe", tmpdir);
1806 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1807 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1808 /* Depending on the platform, command could be '%1' or 'test file.sfe' */
1810 rc=(INT_PTR)FindExecutableA("test file.sfe", tmpdir, command);
1811 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1813 rc=(INT_PTR)FindExecutableA("test file.sfe", NULL, command);
1814 ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %ld\n", filename, rc);
1816 delete_test_association(".sfe");
1818 if (!create_test_association(".shl"))
1820 skip("Unable to create association for '.shl'\n");
1823 create_test_verb(".shl", "Open", 0, "Open");
1825 sprintf(filename, "%s\\test file.shl", tmpdir);
1826 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1827 ok(rc == SE_ERR_FNF /* NT4 */ || rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1829 sprintf(filename, "%s\\test file.shlfoo", tmpdir);
1830 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1832 delete_test_association(".shl");
1836 /* On Windows XP and 2003 FindExecutable() is completely broken.
1837 * Probably what it does is convert the filename to 8.3 format,
1838 * which as a side effect converts the '.shlfoo' extension to '.shl',
1839 * and then tries to find an association for '.shl'. This means it
1840 * will normally fail on most extensions with more than 3 characters,
1841 * like '.mpeg', etc.
1842 * Also it means we cannot do any other test.
1844 win_skip("FindExecutable() is broken -> not running 4+ character extension tests\n");
1848 test=filename_tests;
1849 while (test->basename)
1851 sprintf(filename, test->basename, tmpdir);
1852 if (strchr(filename, '/'))
1863 /* Win98 does not '\0'-terminate command! */
1864 memset(command, '\0', sizeof(command));
1865 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1868 if ((test->todo & 0x10)==0)
1870 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
1874 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
1879 equal=strcmp(command, argv0) == 0 ||
1880 /* NT4 returns an extra 0x8 character! */
1881 (strlen(command) == strlen(argv0)+1 && strncmp(command, argv0, strlen(argv0)) == 0);
1882 if ((test->todo & 0x20)==0)
1884 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
1885 filename, command, argv0);
1889 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
1890 filename, command, argv0);
1898 static filename_tests_t lnk_tests[]=
1900 /* Pass bad / nonexistent filenames as a parameter */
1901 {NULL, "%s\\nonexistent.shlexec", 0xa, 33},
1902 {NULL, "%s\\nonexistent.noassoc", 0xa, 33},
1904 /* Pass regular paths as a parameter */
1905 {NULL, "%s\\test file.shlexec", 0xa, 33},
1906 {NULL, "%s/%%nasty%% $file.shlexec", 0xa, 33},
1908 /* Pass filenames with no association as a parameter */
1909 {NULL, "%s\\test file.noassoc", 0xa, 33},
1914 static void test_lnks(void)
1916 char filename[MAX_PATH];
1917 char params[MAX_PATH];
1918 const filename_tests_t* test;
1921 /* Should open through our association */
1922 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
1923 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
1924 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
1925 okChildInt("argcA", 5);
1926 okChildString("argvA3", "Open");
1927 sprintf(params, "%s\\test file.shlexec", tmpdir);
1928 get_long_path_name(params, filename, sizeof(filename));
1929 okChildPath("argvA4", filename);
1931 todo_wait rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_DOENVSUBST, NULL, "%TMPDIR%\\test_shortcut_shlexec.lnk", NULL, NULL, NULL);
1932 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
1933 okChildInt("argcA", 5);
1934 todo_wine okChildString("argvA3", "Open");
1935 sprintf(params, "%s\\test file.shlexec", tmpdir);
1936 get_long_path_name(params, filename, sizeof(filename));
1937 todo_wine okChildPath("argvA4", filename);
1939 /* Should just run our executable */
1940 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1941 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
1942 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
1943 okChildInt("argcA", 4);
1944 okChildString("argvA3", "Lnk");
1946 /* Lnk's ContextMenuHandler has priority over an explicit class */
1947 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, "shlexec.shlexec");
1948 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
1949 okChildInt("argcA", 4);
1950 okChildString("argvA3", "Lnk");
1952 if (dllver.dwMajorVersion>=6)
1955 /* Recent versions of shell32.dll accept '/'s in shortcut paths.
1956 * Older versions don't or are quite buggy in this regard.
1958 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1966 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
1967 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc,
1969 okChildInt("argcA", 4);
1970 okChildString("argvA3", "Lnk");
1973 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1975 while (test->basename)
1978 sprintf(params+1, test->basename, tmpdir);
1979 strcat(params,"\"");
1980 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, params,
1984 if ((test->todo & 0x1)==0)
1986 ok(rc==test->rc, "%s failed: rc=%lu err=%u\n", shell_call,
1987 rc, GetLastError());
1991 ok(rc==test->rc, "%s failed: rc=%lu err=%u\n", shell_call,
1992 rc, GetLastError());
1996 if ((test->todo & 0x2)==0)
1998 okChildInt("argcA", 5);
2002 okChildInt("argcA", 5);
2004 if ((test->todo & 0x4)==0)
2006 okChildString("argvA3", "Lnk");
2010 okChildString("argvA3", "Lnk");
2012 sprintf(params, test->basename, tmpdir);
2013 if ((test->todo & 0x8)==0)
2015 okChildPath("argvA4", params);
2019 okChildPath("argvA4", params);
2027 static void test_exes(void)
2029 char filename[MAX_PATH];
2033 sprintf(params, "shlexec \"%s\" Exec", child_file);
2035 /* We need NOZONECHECKS on Win2003 to block a dialog */
2036 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
2038 ok(rc > 32, "%s returned %lu\n", shell_call, rc);
2039 okChildInt("argcA", 4);
2040 okChildString("argvA3", "Exec");
2042 if (! skip_noassoc_tests)
2044 sprintf(filename, "%s\\test file.noassoc", tmpdir);
2045 if (CopyFile(argv0, filename, FALSE))
2047 rc=shell_execute(NULL, filename, params, NULL);
2049 ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%lu\n", shell_call, rc);
2055 win_skip("Skipping shellexecute of file with unassociated extension\n");
2061 const char* command;
2062 const char* ddeexec;
2063 const char* application;
2067 const char* expectedDdeExec;
2071 static dde_tests_t dde_tests[] =
2073 /* Test passing and not passing command-line
2074 * argument, no DDE */
2075 {"", NULL, NULL, NULL, NULL, FALSE, "", 0x0},
2076 {"\"%1\"", NULL, NULL, NULL, NULL, TRUE, "", 0x0},
2078 /* Test passing and not passing command-line
2079 * argument, with DDE */
2080 {"", "[open(\"%1\")]", "shlexec", "dde", NULL, FALSE, "[open(\"%s\")]", 0x0},
2081 {"\"%1\"", "[open(\"%1\")]", "shlexec", "dde", NULL, TRUE, "[open(\"%s\")]", 0x0},
2083 /* Test unquoted %1 in command and ddeexec
2084 * (test filename has space) */
2085 {"%1", "[open(%1)]", "shlexec", "dde", NULL, 2, "[open(%s)]", 0x0},
2087 /* Test ifexec precedence over ddeexec */
2088 {"", "[open(\"%1\")]", "shlexec", "dde", "[ifexec(\"%1\")]", FALSE, "[ifexec(\"%s\")]", 0x0},
2090 /* Test default DDE topic */
2091 {"", "[open(\"%1\")]", "shlexec", NULL, NULL, FALSE, "[open(\"%s\")]", 0x0},
2093 /* Test default DDE application */
2094 {"", "[open(\"%1\")]", NULL, "dde", NULL, FALSE, "[open(\"%s\")]", 0x0},
2096 {NULL, NULL, NULL, NULL, NULL, 0, 0x0}
2099 static DWORD WINAPI hooked_WaitForInputIdle(HANDLE process, DWORD timeout)
2101 return WaitForSingleObject(dde_ready_event, timeout);
2105 * WaitForInputIdle() will normally return immediately for console apps. That's
2106 * a problem for us because ShellExecute will assume that an app is ready to
2107 * receive DDE messages after it has called WaitForInputIdle() on that app.
2108 * To work around that we install our own version of WaitForInputIdle() that
2109 * will wait for the child to explicitly tell us that it is ready. We do that
2110 * by changing the entry for WaitForInputIdle() in the shell32 import address
2113 static void hook_WaitForInputIdle(DWORD (WINAPI *new_func)(HANDLE, DWORD))
2116 PIMAGE_NT_HEADERS nt_headers;
2117 DWORD import_directory_rva;
2118 PIMAGE_IMPORT_DESCRIPTOR import_descriptor;
2120 base = (char *) GetModuleHandleA("shell32.dll");
2121 nt_headers = (PIMAGE_NT_HEADERS)(base + ((PIMAGE_DOS_HEADER) base)->e_lfanew);
2122 import_directory_rva = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
2124 /* Search for the correct imported module by walking the import descriptors */
2125 import_descriptor = (PIMAGE_IMPORT_DESCRIPTOR)(base + import_directory_rva);
2126 while (U(*import_descriptor).OriginalFirstThunk != 0)
2128 char *import_module_name;
2130 import_module_name = base + import_descriptor->Name;
2131 if (lstrcmpiA(import_module_name, "user32.dll") == 0 ||
2132 lstrcmpiA(import_module_name, "user32") == 0)
2134 PIMAGE_THUNK_DATA int_entry;
2135 PIMAGE_THUNK_DATA iat_entry;
2137 /* The import name table and import address table are two parallel
2138 * arrays. We need the import name table to find the imported
2139 * routine and the import address table to patch the address, so
2140 * walk them side by side */
2141 int_entry = (PIMAGE_THUNK_DATA)(base + U(*import_descriptor).OriginalFirstThunk);
2142 iat_entry = (PIMAGE_THUNK_DATA)(base + import_descriptor->FirstThunk);
2143 while (int_entry->u1.Ordinal != 0)
2145 if (! IMAGE_SNAP_BY_ORDINAL(int_entry->u1.Ordinal))
2147 PIMAGE_IMPORT_BY_NAME import_by_name;
2148 import_by_name = (PIMAGE_IMPORT_BY_NAME)(base + int_entry->u1.AddressOfData);
2149 if (lstrcmpA((char *) import_by_name->Name, "WaitForInputIdle") == 0)
2151 /* Found the correct routine in the correct imported module. Patch it. */
2153 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), PAGE_READWRITE, &old_prot);
2154 iat_entry->u1.Function = (ULONG_PTR) new_func;
2155 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), old_prot, &old_prot);
2165 import_descriptor++;
2169 static void test_dde(void)
2171 char filename[MAX_PATH], defApplication[MAX_PATH];
2172 const dde_tests_t* test;
2178 hook_WaitForInputIdle(hooked_WaitForInputIdle);
2180 sprintf(filename, "%s\\test file.sde", tmpdir);
2182 /* Default service is application name minus path and extension */
2183 strcpy(defApplication, strrchr(argv0, '\\')+1);
2184 *strchr(defApplication, '.') = 0;
2186 map = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
2187 4096, "winetest_shlexec_dde_map");
2188 shared_block = MapViewOfFile(map, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 4096);
2191 while (test->command)
2193 if (!create_test_association(".sde"))
2195 skip("Unable to create association for '.sde'\n");
2198 create_test_verb_dde(".sde", "Open", 0, test->command, test->ddeexec,
2199 test->application, test->topic, test->ifexec);
2201 if (test->application != NULL || test->topic != NULL)
2203 strcpy(shared_block, test->application ? test->application : defApplication);
2204 strcpy(shared_block + strlen(shared_block) + 1, test->topic ? test->topic : SZDDESYS_TOPIC);
2208 shared_block[0] = '\0';
2209 shared_block[1] = '\0';
2213 dde_ready_event = CreateEventA(NULL, FALSE, FALSE, "winetest_shlexec_dde_ready");
2214 rc = shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, filename, NULL, NULL, NULL);
2215 CloseHandle(dde_ready_event);
2216 if ((test->todo & 0x1)==0)
2218 ok(32 < rc, "%s failed: rc=%lu err=%u\n", shell_call,
2219 rc, GetLastError());
2223 ok(32 < rc, "%s failed: rc=%lu err=%u\n", shell_call,
2224 rc, GetLastError());
2228 if ((test->todo & 0x2)==0)
2230 okChildInt("argcA", test->expectedArgs + 3);
2234 okChildInt("argcA", test->expectedArgs + 3);
2236 if (test->expectedArgs == 1)
2238 if ((test->todo & 0x4) == 0)
2240 okChildPath("argvA3", filename);
2244 okChildPath("argvA3", filename);
2247 if ((test->todo & 0x8) == 0)
2249 sprintf(params, test->expectedDdeExec, filename);
2250 okChildPath("ddeExec", params);
2254 sprintf(params, test->expectedDdeExec, filename);
2255 okChildPath("ddeExec", params);
2259 delete_test_association(".sde");
2263 UnmapViewOfFile(shared_block);
2265 hook_WaitForInputIdle((void *) WaitForInputIdle);
2268 #define DDE_DEFAULT_APP_VARIANTS 2
2271 const char* command;
2272 const char* expectedDdeApplication[DDE_DEFAULT_APP_VARIANTS];
2274 int rc[DDE_DEFAULT_APP_VARIANTS];
2275 } dde_default_app_tests_t;
2277 static dde_default_app_tests_t dde_default_app_tests[] =
2279 /* Windows XP and 98 handle default DDE app names in different ways.
2280 * The application name we see in the first test determines the pattern
2281 * of application names and return codes we will look for. */
2283 /* Test unquoted existing filename with a space */
2284 {"%s\\test file.exe", {"test file", "test"}, 0x0, {33, 33}},
2285 {"%s\\test file.exe param", {"test file", "test"}, 0x0, {33, 33}},
2287 /* Test quoted existing filename with a space */
2288 {"\"%s\\test file.exe\"", {"test file", "test file"}, 0x0, {33, 33}},
2289 {"\"%s\\test file.exe\" param", {"test file", "test file"}, 0x0, {33, 33}},
2291 /* Test unquoted filename with a space that doesn't exist, but
2293 {"%s\\test2 file.exe", {"test2", "test2"}, 0x0, {33, 33}},
2294 {"%s\\test2 file.exe param", {"test2", "test2"}, 0x0, {33, 33}},
2296 /* Test quoted filename with a space that does not exist */
2297 {"\"%s\\test2 file.exe\"", {"", "test2 file"}, 0x0, {5, 33}},
2298 {"\"%s\\test2 file.exe\" param", {"", "test2 file"}, 0x0, {5, 33}},
2300 /* Test filename supplied without the extension */
2301 {"%s\\test2", {"test2", "test2"}, 0x0, {33, 33}},
2302 {"%s\\test2 param", {"test2", "test2"}, 0x0, {33, 33}},
2304 /* Test an unquoted nonexistent filename */
2305 {"%s\\notexist.exe", {"", "notexist"}, 0x0, {5, 33}},
2306 {"%s\\notexist.exe param", {"", "notexist"}, 0x0, {5, 33}},
2308 /* Test an application that will be found on the path */
2309 {"cmd", {"cmd", "cmd"}, 0x0, {33, 33}},
2310 {"cmd param", {"cmd", "cmd"}, 0x0, {33, 33}},
2312 /* Test an application that will not be found on the path */
2313 {"xyzwxyzwxyz", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
2314 {"xyzwxyzwxyz param", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
2316 {NULL, {NULL}, 0, {0}}
2322 DWORD threadIdParent;
2323 } dde_thread_info_t;
2325 static DWORD CALLBACK ddeThread(LPVOID arg)
2327 dde_thread_info_t *info = arg;
2328 assert(info && info->filename);
2329 PostThreadMessage(info->threadIdParent,
2331 shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, info->filename, NULL, NULL, NULL),
2336 static void test_dde_default_app(void)
2338 char filename[MAX_PATH];
2340 dde_thread_info_t info = { filename, GetCurrentThreadId() };
2341 const dde_default_app_tests_t* test;
2348 post_quit_on_execute = FALSE;
2350 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
2351 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
2352 assert(rc == DMLERR_NO_ERROR);
2354 sprintf(filename, "%s\\test file.sde", tmpdir);
2356 /* It is strictly not necessary to register an application name here, but wine's
2357 * DdeNameService implementation complains if 0L is passed instead of
2358 * hszApplication with DNS_FILTEROFF */
2359 hszApplication = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
2360 hszTopic = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
2361 assert(hszApplication && hszTopic);
2362 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER | DNS_FILTEROFF));
2364 test = dde_default_app_tests;
2365 while (test->command)
2367 if (!create_test_association(".sde"))
2369 skip("Unable to create association for '.sde'\n");
2372 sprintf(params, test->command, tmpdir);
2373 create_test_verb_dde(".sde", "Open", 1, params, "[test]", NULL,
2375 ddeApplication[0] = 0;
2377 /* No application will be run as we will respond to the first DDE event,
2378 * so don't wait for it */
2381 assert(CreateThread(NULL, 0, ddeThread, &info, 0, &threadId));
2382 while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);
2383 rc = msg.wParam > 32 ? 33 : msg.wParam;
2385 /* First test, find which set of test data we expect to see */
2386 if (test == dde_default_app_tests)
2389 for (i=0; i<DDE_DEFAULT_APP_VARIANTS; i++)
2391 if (!strcmp(ddeApplication, test->expectedDdeApplication[i]))
2397 if (i == DDE_DEFAULT_APP_VARIANTS)
2398 skip("Default DDE application test does not match any available results, using first expected data set.\n");
2401 if ((test->todo & 0x1)==0)
2403 ok(rc==test->rc[which], "%s failed: rc=%lu err=%u\n", shell_call,
2404 rc, GetLastError());
2408 ok(rc==test->rc[which], "%s failed: rc=%lu err=%u\n", shell_call,
2409 rc, GetLastError());
2413 if ((test->todo & 0x2)==0)
2415 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
2416 "Expected application '%s', got '%s'\n",
2417 test->expectedDdeApplication[which], ddeApplication);
2421 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
2422 "Expected application '%s', got '%s'\n",
2423 test->expectedDdeApplication[which], ddeApplication);
2427 delete_test_association(".sde");
2431 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
2432 assert(DdeFreeStringHandle(ddeInst, hszTopic));
2433 assert(DdeFreeStringHandle(ddeInst, hszApplication));
2434 assert(DdeUninitialize(ddeInst));
2437 static void init_test(void)
2440 HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO*);
2441 char filename[MAX_PATH];
2442 WCHAR lnkfile[MAX_PATH];
2444 const char* const * testfile;
2449 hdll=GetModuleHandleA("shell32.dll");
2450 pDllGetVersion=(void*)GetProcAddress(hdll, "DllGetVersion");
2453 dllver.cbSize=sizeof(dllver);
2454 pDllGetVersion(&dllver);
2455 trace("major=%d minor=%d build=%d platform=%d\n",
2456 dllver.dwMajorVersion, dllver.dwMinorVersion,
2457 dllver.dwBuildNumber, dllver.dwPlatformID);
2461 memset(&dllver, 0, sizeof(dllver));
2464 r = CoInitialize(NULL);
2465 ok(r == S_OK, "CoInitialize failed (0x%08x)\n", r);
2469 rc=GetModuleFileName(NULL, argv0, sizeof(argv0));
2470 assert(rc!=0 && rc<sizeof(argv0));
2471 if (GetFileAttributes(argv0)==INVALID_FILE_ATTRIBUTES)
2473 strcat(argv0, ".so");
2474 ok(GetFileAttributes(argv0)!=INVALID_FILE_ATTRIBUTES,
2475 "unable to find argv0!\n");
2478 GetTempPathA(sizeof(filename), filename);
2479 GetTempFileNameA(filename, "wt", 0, tmpdir);
2480 DeleteFileA( tmpdir );
2481 rc = CreateDirectoryA( tmpdir, NULL );
2482 ok( rc, "failed to create %s err %u\n", tmpdir, GetLastError() );
2483 /* Set %TMPDIR% for the tests */
2484 SetEnvironmentVariableA("TMPDIR", tmpdir);
2486 rc = GetTempFileNameA(tmpdir, "wt", 0, child_file);
2488 init_event(child_file);
2490 /* Set up the test files */
2496 sprintf(filename, *testfile, tmpdir);
2497 hfile=CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2498 FILE_ATTRIBUTE_NORMAL, NULL);
2499 if (hfile==INVALID_HANDLE_VALUE)
2501 trace("unable to create '%s': err=%u\n", filename, GetLastError());
2508 /* Setup the test shortcuts */
2509 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
2510 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
2511 desc.description=NULL;
2513 sprintf(filename, "%s\\test file.shlexec", tmpdir);
2516 desc.arguments="ignored";
2521 create_lnk(lnkfile, &desc, 0);
2523 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2524 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
2525 desc.description=NULL;
2529 sprintf(params, "shlexec \"%s\" Lnk", child_file);
2530 desc.arguments=params;
2535 create_lnk(lnkfile, &desc, 0);
2537 /* Create a basic association suitable for most tests */
2538 if (!create_test_association(".shlexec"))
2540 skip("Unable to create association for '.shlexec'\n");
2543 create_test_verb(".shlexec", "Open", 0, "Open \"%1\"");
2544 create_test_verb(".shlexec", "NoQuotes", 0, "NoQuotes %1");
2545 create_test_verb(".shlexec", "LowerL", 0, "LowerL %l");
2546 create_test_verb(".shlexec", "QuotedLowerL", 0, "QuotedLowerL \"%l\"");
2547 create_test_verb(".shlexec", "UpperL", 0, "UpperL %L");
2548 create_test_verb(".shlexec", "QuotedUpperL", 0, "QuotedUpperL \"%L\"");
2551 static void cleanup_test(void)
2553 char filename[MAX_PATH];
2554 const char* const * testfile;
2556 /* Delete the test files */
2560 sprintf(filename, *testfile, tmpdir);
2561 /* Make sure we can delete the files ('test file.noassoc' is read-only now) */
2562 SetFileAttributes(filename, FILE_ATTRIBUTE_NORMAL);
2563 DeleteFile(filename);
2566 DeleteFile(child_file);
2567 RemoveDirectoryA(tmpdir);
2569 /* Delete the test association */
2570 delete_test_association(".shlexec");
2572 CloseHandle(hEvent);
2577 static void test_directory(void)
2579 char path[MAX_PATH], curdir[MAX_PATH];
2580 char params[1024], dirpath[1024];
2583 sprintf(path, "%s\\test2.exe", tmpdir);
2584 CopyFileA(argv0, path, FALSE);
2586 sprintf(params, "shlexec \"%s\" Exec", child_file);
2588 /* Test with the current directory */
2589 GetCurrentDirectoryA(sizeof(curdir), curdir);
2590 SetCurrentDirectoryA(tmpdir);
2591 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2592 NULL, "test2.exe", params, NULL, NULL);
2593 ok(rc > 32, "%s returned %lu\n", shell_call, rc);
2594 okChildInt("argcA", 4);
2595 okChildString("argvA3", "Exec");
2596 todo_wine okChildPath("longPath", path);
2597 SetCurrentDirectoryA(curdir);
2599 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2600 NULL, "test2.exe", params, NULL, NULL);
2601 ok(rc == SE_ERR_FNF, "%s returned %lu\n", shell_call, rc);
2603 /* Explicitly specify the directory to use */
2604 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2605 NULL, "test2.exe", params, tmpdir, NULL);
2606 ok(rc > 32, "%s returned %lu\n", shell_call, rc);
2607 okChildInt("argcA", 4);
2608 okChildString("argvA3", "Exec");
2609 todo_wine okChildPath("longPath", path);
2611 /* Specify it through an environment variable */
2612 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2613 NULL, "test2.exe", params, "%TMPDIR%", NULL);
2614 todo_wine ok(rc == SE_ERR_FNF, "%s returned %lu\n", shell_call, rc);
2616 rc=shell_execute_ex(SEE_MASK_DOENVSUBST|SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2617 NULL, "test2.exe", params, "%TMPDIR%", NULL);
2618 ok(rc > 32, "%s returned %lu\n", shell_call, rc);
2619 okChildInt("argcA", 4);
2620 okChildString("argvA3", "Exec");
2621 todo_wine okChildPath("longPath", path);
2623 /* Not a colon-separated directory list */
2624 sprintf(dirpath, "%s:%s", curdir, tmpdir);
2625 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2626 NULL, "test2.exe", params, dirpath, NULL);
2627 ok(rc == SE_ERR_FNF, "%s returned %lu\n", shell_call, rc);
2633 myARGC = winetest_get_mainargs(&myARGV);
2636 doChild(myARGC, myARGV);
2642 test_commandline2argv();
2644 test_lpFile_parsed();
2647 test_find_executable();
2651 test_dde_default_app();