push 149f0a5527ac85057a8ef03858d34d91c36f97e8
[wine/hacks.git] / dlls / shell32 / tests / shlexec.c
blobc673f0a31b540dd25b0b53052e64c211044f8425
1 /*
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
21 /* TODO:
22 * - test the default verb selection
23 * - test selection of an alternate class
24 * - try running executables in more ways
25 * - try passing arguments to executables
26 * - ShellExecute("foo.shlexec") with no path should work if foo.shlexec is
27 * in the PATH
28 * - test associations that use %l, %L or "%1" instead of %1
29 * - we may want to test ShellExecuteEx() instead of ShellExecute()
30 * and then we could also check its return value
31 * - ShellExecuteEx() also calls SetLastError() with meaningful values which
32 * we could check
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
40 #include <stdio.h>
41 #include <assert.h>
43 #include "wtypes.h"
44 #include "winbase.h"
45 #include "windef.h"
46 #include "shellapi.h"
47 #include "shlwapi.h"
48 #include "wine/test.h"
50 #include "shell32_test.h"
53 static char argv0[MAX_PATH];
54 static int myARGC;
55 static char** myARGV;
56 static char tmpdir[MAX_PATH];
57 static char child_file[MAX_PATH];
58 static DLLVERSIONINFO dllver;
59 static BOOL skip_noassoc_tests = FALSE;
62 /***
64 * ShellExecute wrappers
66 ***/
67 static void dump_child(void);
69 static HANDLE hEvent;
70 static void init_event(const char* child_file)
72 char* event_name;
73 event_name=strrchr(child_file, '\\')+1;
74 hEvent=CreateEvent(NULL, FALSE, FALSE, event_name);
77 static void strcat_param(char* str, const char* param)
79 if (param!=NULL)
81 strcat(str, "\"");
82 strcat(str, param);
83 strcat(str, "\"");
85 else
87 strcat(str, "null");
91 static char shell_call[2048]="";
92 static int shell_execute(LPCSTR operation, LPCSTR file, LPCSTR parameters, LPCSTR directory)
94 INT_PTR rc;
96 strcpy(shell_call, "ShellExecute(");
97 strcat_param(shell_call, operation);
98 strcat(shell_call, ", ");
99 strcat_param(shell_call, file);
100 strcat(shell_call, ", ");
101 strcat_param(shell_call, parameters);
102 strcat(shell_call, ", ");
103 strcat_param(shell_call, directory);
104 strcat(shell_call, ")");
105 if (winetest_debug > 1)
106 trace("%s\n", shell_call);
108 DeleteFile(child_file);
109 SetLastError(0xcafebabe);
111 /* FIXME: We cannot use ShellExecuteEx() here because if there is no
112 * association it displays the 'Open With' dialog and I could not find
113 * a flag to prevent this.
115 rc=(INT_PTR)ShellExecute(NULL, operation, file, parameters, directory, SW_SHOWNORMAL);
117 if (rc > 32)
119 int wait_rc;
120 wait_rc=WaitForSingleObject(hEvent, 5000);
121 if (wait_rc == WAIT_TIMEOUT)
123 HWND wnd = FindWindowA("#32770", "Windows");
124 if (wnd != NULL)
126 SendMessage(wnd, WM_CLOSE, 0, 0);
127 win_skip("Skipping shellexecute of file with unassociated extension\n");
128 skip_noassoc_tests = TRUE;
129 rc = SE_ERR_NOASSOC;
132 ok(wait_rc==WAIT_OBJECT_0 || rc <= 32, "WaitForSingleObject returned %d\n", wait_rc);
134 /* The child process may have changed the result file, so let profile
135 * functions know about it
137 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
138 if (rc > 32)
139 dump_child();
141 return rc;
144 static int shell_execute_ex(DWORD mask, LPCSTR operation, LPCSTR file,
145 LPCSTR parameters, LPCSTR directory)
147 SHELLEXECUTEINFO sei;
148 BOOL success;
149 INT_PTR rc;
151 strcpy(shell_call, "ShellExecuteEx(");
152 strcat_param(shell_call, operation);
153 strcat(shell_call, ", ");
154 strcat_param(shell_call, file);
155 strcat(shell_call, ", ");
156 strcat_param(shell_call, parameters);
157 strcat(shell_call, ", ");
158 strcat_param(shell_call, directory);
159 strcat(shell_call, ")");
160 if (winetest_debug > 1)
161 trace("%s\n", shell_call);
163 sei.cbSize=sizeof(sei);
164 sei.fMask=SEE_MASK_NOCLOSEPROCESS | mask;
165 sei.hwnd=NULL;
166 sei.lpVerb=operation;
167 sei.lpFile=file;
168 sei.lpParameters=parameters;
169 sei.lpDirectory=directory;
170 sei.nShow=SW_SHOWNORMAL;
171 sei.hInstApp=NULL; /* Out */
172 sei.lpIDList=NULL;
173 sei.lpClass=NULL;
174 sei.hkeyClass=NULL;
175 sei.dwHotKey=0;
176 U(sei).hIcon=NULL;
177 sei.hProcess=NULL; /* Out */
179 DeleteFile(child_file);
180 SetLastError(0xcafebabe);
181 success=ShellExecuteEx(&sei);
182 rc=(INT_PTR)sei.hInstApp;
183 ok((success && rc > 32) || (!success && rc <= 32),
184 "%s rc=%d and hInstApp=%ld is not allowed\n", shell_call, success, rc);
186 if (rc > 32)
188 int wait_rc;
189 if (sei.hProcess!=NULL)
191 wait_rc=WaitForSingleObject(sei.hProcess, 5000);
192 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject(hProcess) returned %d\n", wait_rc);
194 wait_rc=WaitForSingleObject(hEvent, 5000);
195 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
197 /* The child process may have changed the result file, so let profile
198 * functions know about it
200 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
201 if (rc > 32)
202 dump_child();
204 return rc;
209 /***
211 * Functions to create / delete associations wrappers
213 ***/
215 static BOOL create_test_association(const char* extension)
217 HKEY hkey, hkey_shell;
218 char class[MAX_PATH];
219 LONG rc;
221 sprintf(class, "shlexec%s", extension);
222 rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, extension, 0, NULL, 0, KEY_SET_VALUE,
223 NULL, &hkey, NULL);
224 if (rc != ERROR_SUCCESS)
225 return FALSE;
227 rc=RegSetValueEx(hkey, NULL, 0, REG_SZ, (LPBYTE) class, strlen(class)+1);
228 ok(rc==ERROR_SUCCESS, "RegSetValueEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
229 CloseHandle(hkey);
231 rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, class, 0, NULL, 0,
232 KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS, NULL, &hkey, NULL);
233 ok(rc==ERROR_SUCCESS, "RegCreateKeyEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
235 rc=RegCreateKeyEx(hkey, "shell", 0, NULL, 0,
236 KEY_CREATE_SUB_KEY, NULL, &hkey_shell, NULL);
237 ok(rc==ERROR_SUCCESS, "RegCreateKeyEx 'shell' failed, expected ERROR_SUCCESS, got %d\n", rc);
239 CloseHandle(hkey);
240 CloseHandle(hkey_shell);
242 return TRUE;
245 /* Based on RegDeleteTreeW from dlls/advapi32/registry.c */
246 static LSTATUS myRegDeleteTreeA(HKEY hKey, LPCSTR lpszSubKey)
248 LONG ret;
249 DWORD dwMaxSubkeyLen, dwMaxValueLen;
250 DWORD dwMaxLen, dwSize;
251 CHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
252 HKEY hSubKey = hKey;
254 if(lpszSubKey)
256 ret = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
257 if (ret) return ret;
260 /* Get highest length for keys, values */
261 ret = RegQueryInfoKeyA(hSubKey, NULL, NULL, NULL, NULL,
262 &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
263 if (ret) goto cleanup;
265 dwMaxSubkeyLen++;
266 dwMaxValueLen++;
267 dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
268 if (dwMaxLen > sizeof(szNameBuf)/sizeof(CHAR))
270 /* Name too big: alloc a buffer for it */
271 if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(CHAR))))
273 ret = ERROR_NOT_ENOUGH_MEMORY;
274 goto cleanup;
279 /* Recursively delete all the subkeys */
280 while (TRUE)
282 dwSize = dwMaxLen;
283 if (RegEnumKeyExA(hSubKey, 0, lpszName, &dwSize, NULL,
284 NULL, NULL, NULL)) break;
286 ret = myRegDeleteTreeA(hSubKey, lpszName);
287 if (ret) goto cleanup;
290 if (lpszSubKey)
291 ret = RegDeleteKeyA(hKey, lpszSubKey);
292 else
293 while (TRUE)
295 dwSize = dwMaxLen;
296 if (RegEnumValueA(hKey, 0, lpszName, &dwSize,
297 NULL, NULL, NULL, NULL)) break;
299 ret = RegDeleteValueA(hKey, lpszName);
300 if (ret) goto cleanup;
303 cleanup:
304 /* Free buffer if allocated */
305 if (lpszName != szNameBuf)
306 HeapFree( GetProcessHeap(), 0, lpszName);
307 if(lpszSubKey)
308 RegCloseKey(hSubKey);
309 return ret;
312 static void delete_test_association(const char* extension)
314 char class[MAX_PATH];
316 sprintf(class, "shlexec%s", extension);
317 myRegDeleteTreeA(HKEY_CLASSES_ROOT, class);
318 myRegDeleteTreeA(HKEY_CLASSES_ROOT, extension);
321 static void create_test_verb_dde(const char* extension, const char* verb,
322 int rawcmd, const char* cmdtail, const char *ddeexec,
323 const char *application, const char *topic,
324 const char *ifexec)
326 HKEY hkey_shell, hkey_verb, hkey_cmd;
327 char shell[MAX_PATH];
328 char* cmd;
329 LONG rc;
331 sprintf(shell, "shlexec%s\\shell", extension);
332 rc=RegOpenKeyEx(HKEY_CLASSES_ROOT, shell, 0,
333 KEY_CREATE_SUB_KEY, &hkey_shell);
334 assert(rc==ERROR_SUCCESS);
335 rc=RegCreateKeyEx(hkey_shell, verb, 0, NULL, 0, KEY_CREATE_SUB_KEY,
336 NULL, &hkey_verb, NULL);
337 assert(rc==ERROR_SUCCESS);
338 rc=RegCreateKeyEx(hkey_verb, "command", 0, NULL, 0, KEY_SET_VALUE,
339 NULL, &hkey_cmd, NULL);
340 assert(rc==ERROR_SUCCESS);
342 if (rawcmd)
344 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmdtail, strlen(cmdtail)+1);
346 else
348 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(child_file)+2+strlen(cmdtail)+1);
349 sprintf(cmd,"%s shlexec \"%s\" %s", argv0, child_file, cmdtail);
350 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmd, strlen(cmd)+1);
351 assert(rc==ERROR_SUCCESS);
352 HeapFree(GetProcessHeap(), 0, cmd);
355 if (ddeexec)
357 HKEY hkey_ddeexec, hkey_application, hkey_topic, hkey_ifexec;
359 rc=RegCreateKeyEx(hkey_verb, "ddeexec", 0, NULL, 0, KEY_SET_VALUE |
360 KEY_CREATE_SUB_KEY, NULL, &hkey_ddeexec, NULL);
361 assert(rc==ERROR_SUCCESS);
362 rc=RegSetValueEx(hkey_ddeexec, NULL, 0, REG_SZ, (LPBYTE)ddeexec,
363 strlen(ddeexec)+1);
364 assert(rc==ERROR_SUCCESS);
365 if (application)
367 rc=RegCreateKeyEx(hkey_ddeexec, "application", 0, NULL, 0, KEY_SET_VALUE,
368 NULL, &hkey_application, NULL);
369 assert(rc==ERROR_SUCCESS);
370 rc=RegSetValueEx(hkey_application, NULL, 0, REG_SZ, (LPBYTE)application,
371 strlen(application)+1);
372 assert(rc==ERROR_SUCCESS);
373 CloseHandle(hkey_application);
375 if (topic)
377 rc=RegCreateKeyEx(hkey_ddeexec, "topic", 0, NULL, 0, KEY_SET_VALUE,
378 NULL, &hkey_topic, NULL);
379 assert(rc==ERROR_SUCCESS);
380 rc=RegSetValueEx(hkey_topic, NULL, 0, REG_SZ, (LPBYTE)topic,
381 strlen(topic)+1);
382 assert(rc==ERROR_SUCCESS);
383 CloseHandle(hkey_topic);
385 if (ifexec)
387 rc=RegCreateKeyEx(hkey_ddeexec, "ifexec", 0, NULL, 0, KEY_SET_VALUE,
388 NULL, &hkey_ifexec, NULL);
389 assert(rc==ERROR_SUCCESS);
390 rc=RegSetValueEx(hkey_ifexec, NULL, 0, REG_SZ, (LPBYTE)ifexec,
391 strlen(ifexec)+1);
392 assert(rc==ERROR_SUCCESS);
393 CloseHandle(hkey_ifexec);
395 CloseHandle(hkey_ddeexec);
398 CloseHandle(hkey_shell);
399 CloseHandle(hkey_verb);
400 CloseHandle(hkey_cmd);
403 static void create_test_verb(const char* extension, const char* verb,
404 int rawcmd, const char* cmdtail)
406 create_test_verb_dde(extension, verb, rawcmd, cmdtail, NULL, NULL,
407 NULL, NULL);
410 /***
412 * Functions to check that the child process was started just right
413 * (borrowed from dlls/kernel32/tests/process.c)
415 ***/
417 static const char* encodeA(const char* str)
419 static char encoded[2*1024+1];
420 char* ptr;
421 size_t len,i;
423 if (!str) return "";
424 len = strlen(str) + 1;
425 if (len >= sizeof(encoded)/2)
427 fprintf(stderr, "string is too long!\n");
428 assert(0);
430 ptr = encoded;
431 for (i = 0; i < len; i++)
432 sprintf(&ptr[i * 2], "%02x", (unsigned char)str[i]);
433 ptr[2 * len] = '\0';
434 return ptr;
437 static unsigned decode_char(char c)
439 if (c >= '0' && c <= '9') return c - '0';
440 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
441 assert(c >= 'A' && c <= 'F');
442 return c - 'A' + 10;
445 static char* decodeA(const char* str)
447 static char decoded[1024];
448 char* ptr;
449 size_t len,i;
451 len = strlen(str) / 2;
452 if (!len--) return NULL;
453 if (len >= sizeof(decoded))
455 fprintf(stderr, "string is too long!\n");
456 assert(0);
458 ptr = decoded;
459 for (i = 0; i < len; i++)
460 ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
461 ptr[len] = '\0';
462 return ptr;
465 static void childPrintf(HANDLE h, const char* fmt, ...)
467 va_list valist;
468 char buffer[1024];
469 DWORD w;
471 va_start(valist, fmt);
472 vsprintf(buffer, fmt, valist);
473 va_end(valist);
474 WriteFile(h, buffer, strlen(buffer), &w, NULL);
477 static DWORD ddeInst;
478 static HSZ hszTopic;
479 static char ddeExec[MAX_PATH], ddeApplication[MAX_PATH];
480 static BOOL post_quit_on_execute;
482 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
483 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
484 ULONG_PTR dwData1, ULONG_PTR dwData2)
486 DWORD size = 0;
488 if (winetest_debug > 2)
489 trace("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
490 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
492 switch (uType)
494 case XTYP_CONNECT:
495 if (!DdeCmpStringHandles(hsz1, hszTopic))
497 size = DdeQueryString(ddeInst, hsz2, ddeApplication, MAX_PATH, CP_WINANSI);
498 assert(size < MAX_PATH);
499 return (HDDEDATA)TRUE;
501 return (HDDEDATA)FALSE;
503 case XTYP_EXECUTE:
504 size = DdeGetData(hData, (LPBYTE)ddeExec, MAX_PATH, 0L);
505 assert(size < MAX_PATH);
506 DdeFreeDataHandle(hData);
507 if (post_quit_on_execute)
508 PostQuitMessage(0);
509 return (HDDEDATA)DDE_FACK;
511 default:
512 return NULL;
517 * This is just to make sure the child won't run forever stuck in a GetMessage()
518 * loop when DDE fails for some reason.
520 static void CALLBACK childTimeout(HWND wnd, UINT msg, UINT_PTR timer, DWORD time)
522 trace("childTimeout called\n");
524 PostQuitMessage(0);
527 static void doChild(int argc, char** argv)
529 char* filename;
530 HANDLE hFile, map;
531 int i;
532 int rc;
533 HSZ hszApplication;
534 UINT_PTR timer;
535 HANDLE dde_ready;
536 MSG msg;
537 char *shared_block;
539 filename=argv[2];
540 hFile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
541 if (hFile == INVALID_HANDLE_VALUE)
542 return;
544 /* Arguments */
545 childPrintf(hFile, "[Arguments]\r\n");
546 if (winetest_debug > 2)
547 trace("argcA=%d\n", argc);
548 childPrintf(hFile, "argcA=%d\r\n", argc);
549 for (i = 0; i < argc; i++)
551 if (winetest_debug > 2)
552 trace("argvA%d=%s\n", i, argv[i]);
553 childPrintf(hFile, "argvA%d=%s\r\n", i, encodeA(argv[i]));
556 map = OpenFileMappingA(FILE_MAP_READ, FALSE, "winetest_shlexec_dde_map");
557 if (map != NULL)
559 shared_block = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 4096);
560 CloseHandle(map);
561 if (shared_block[0] != '\0' || shared_block[1] != '\0')
563 post_quit_on_execute = TRUE;
564 ddeInst = 0;
565 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
566 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
567 assert(rc == DMLERR_NO_ERROR);
568 hszApplication = DdeCreateStringHandleA(ddeInst, shared_block, CP_WINANSI);
569 hszTopic = DdeCreateStringHandleA(ddeInst, shared_block + strlen(shared_block) + 1, CP_WINANSI);
570 assert(hszApplication && hszTopic);
571 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER | DNS_FILTEROFF));
573 timer = SetTimer(NULL, 0, 2500, childTimeout);
575 dde_ready = CreateEvent(NULL, FALSE, FALSE, "winetest_shlexec_dde_ready");
576 SetEvent(dde_ready);
577 CloseHandle(dde_ready);
579 while (GetMessage(&msg, NULL, 0, 0))
580 DispatchMessage(&msg);
582 KillTimer(NULL, timer);
583 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
584 assert(DdeFreeStringHandle(ddeInst, hszTopic));
585 assert(DdeFreeStringHandle(ddeInst, hszApplication));
586 assert(DdeUninitialize(ddeInst));
588 else
590 dde_ready = CreateEvent(NULL, FALSE, FALSE, "winetest_shlexec_dde_ready");
591 SetEvent(dde_ready);
592 CloseHandle(dde_ready);
595 UnmapViewOfFile(shared_block);
597 childPrintf(hFile, "ddeExec=%s\r\n", encodeA(ddeExec));
600 CloseHandle(hFile);
602 init_event(filename);
603 SetEvent(hEvent);
604 CloseHandle(hEvent);
607 static char* getChildString(const char* sect, const char* key)
609 char buf[1024];
610 char* ret;
612 GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), child_file);
613 if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
614 assert(!(strlen(buf) & 1));
615 ret = decodeA(buf);
616 return ret;
619 static void dump_child(void)
621 if (winetest_debug > 1)
623 char key[18];
624 char* str;
625 int i, c;
627 c=GetPrivateProfileIntA("Arguments", "argcA", -1, child_file);
628 trace("argcA=%d\n",c);
629 for (i=0;i<c;i++)
631 sprintf(key, "argvA%d", i);
632 str=getChildString("Arguments", key);
633 trace("%s=%s\n", key, str);
638 static int StrCmpPath(const char* s1, const char* s2)
640 if (!s1 && !s2) return 0;
641 if (!s2) return 1;
642 if (!s1) return -1;
643 while (*s1)
645 if (!*s2)
647 if (*s1=='.')
648 s1++;
649 return (*s1-*s2);
651 if ((*s1=='/' || *s1=='\\') && (*s2=='/' || *s2=='\\'))
653 while (*s1=='/' || *s1=='\\')
654 s1++;
655 while (*s2=='/' || *s2=='\\')
656 s2++;
658 else if (toupper(*s1)==toupper(*s2))
660 s1++;
661 s2++;
663 else
665 return (*s1-*s2);
668 if (*s2=='.')
669 s2++;
670 if (*s2)
671 return -1;
672 return 0;
675 static int _okChildString(const char* file, int line, const char* key, const char* expected)
677 char* result;
678 result=getChildString("Arguments", key);
679 return ok_(file, line)(lstrcmpiA(result, expected) == 0,
680 "%s expected '%s', got '%s'\n", key, expected, result);
683 static int _okChildPath(const char* file, int line, const char* key, const char* expected)
685 char* result;
686 result=getChildString("Arguments", key);
687 return ok_(file, line)(StrCmpPath(result, expected) == 0,
688 "%s expected '%s', got '%s'\n", key, expected, result);
691 static int _okChildInt(const char* file, int line, const char* key, int expected)
693 INT result;
694 result=GetPrivateProfileIntA("Arguments", key, expected, child_file);
695 return ok_(file, line)(result == expected,
696 "%s expected %d, but got %d\n", key, expected, result);
699 #define okChildString(key, expected) _okChildString(__FILE__, __LINE__, (key), (expected))
700 #define okChildPath(key, expected) _okChildPath(__FILE__, __LINE__, (key), (expected))
701 #define okChildInt(key, expected) _okChildInt(__FILE__, __LINE__, (key), (expected))
703 /***
705 * GetLongPathNameA equivalent that supports Win95 and WinNT
707 ***/
709 static DWORD get_long_path_name(const char* shortpath, char* longpath, DWORD longlen)
711 char tmplongpath[MAX_PATH];
712 const char* p;
713 DWORD sp = 0, lp = 0;
714 DWORD tmplen;
715 WIN32_FIND_DATAA wfd;
716 HANDLE goit;
718 if (!shortpath || !shortpath[0])
719 return 0;
721 if (shortpath[1] == ':')
723 tmplongpath[0] = shortpath[0];
724 tmplongpath[1] = ':';
725 lp = sp = 2;
728 while (shortpath[sp])
730 /* check for path delimiters and reproduce them */
731 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
733 if (!lp || tmplongpath[lp-1] != '\\')
735 /* strip double "\\" */
736 tmplongpath[lp++] = '\\';
738 tmplongpath[lp] = 0; /* terminate string */
739 sp++;
740 continue;
743 p = shortpath + sp;
744 if (sp == 0 && p[0] == '.' && (p[1] == '/' || p[1] == '\\'))
746 tmplongpath[lp++] = *p++;
747 tmplongpath[lp++] = *p++;
749 for (; *p && *p != '/' && *p != '\\'; p++);
750 tmplen = p - (shortpath + sp);
751 lstrcpyn(tmplongpath + lp, shortpath + sp, tmplen + 1);
752 /* Check if the file exists and use the existing file name */
753 goit = FindFirstFileA(tmplongpath, &wfd);
754 if (goit == INVALID_HANDLE_VALUE)
755 return 0;
756 FindClose(goit);
757 strcpy(tmplongpath + lp, wfd.cFileName);
758 lp += strlen(tmplongpath + lp);
759 sp += tmplen;
761 tmplen = strlen(shortpath) - 1;
762 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
763 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
764 tmplongpath[lp++] = shortpath[tmplen];
765 tmplongpath[lp] = 0;
767 tmplen = strlen(tmplongpath) + 1;
768 if (tmplen <= longlen)
770 strcpy(longpath, tmplongpath);
771 tmplen--; /* length without 0 */
774 return tmplen;
777 /***
779 * Tests
781 ***/
783 static const char* testfiles[]=
785 "%s\\test file.shlexec",
786 "%s\\%%nasty%% $file.shlexec",
787 "%s\\test file.noassoc",
788 "%s\\test file.noassoc.shlexec",
789 "%s\\test file.shlexec.noassoc",
790 "%s\\test_shortcut_shlexec.lnk",
791 "%s\\test_shortcut_exe.lnk",
792 "%s\\test file.shl",
793 "%s\\test file.shlfoo",
794 "%s\\test file.sfe",
795 "%s\\masked file.shlexec",
796 "%s\\masked",
797 "%s\\test file.sde",
798 "%s\\test file.exe",
799 "%s\\test2.exe",
800 NULL
803 typedef struct
805 const char* verb;
806 const char* basename;
807 int todo;
808 int rc;
809 } filename_tests_t;
811 static filename_tests_t filename_tests[]=
813 /* Test bad / nonexistent filenames */
814 {NULL, "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
815 {NULL, "%s\\nonexistent.noassoc", 0x0, SE_ERR_FNF},
817 /* Standard tests */
818 {NULL, "%s\\test file.shlexec", 0x0, 33},
819 {NULL, "%s\\test file.shlexec.", 0x0, 33},
820 {NULL, "%s\\%%nasty%% $file.shlexec", 0x0, 33},
821 {NULL, "%s/test file.shlexec", 0x0, 33},
823 /* Test filenames with no association */
824 {NULL, "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
826 /* Test double extensions */
827 {NULL, "%s\\test file.noassoc.shlexec", 0x0, 33},
828 {NULL, "%s\\test file.shlexec.noassoc", 0x0, SE_ERR_NOASSOC},
830 /* Test alternate verbs */
831 {"LowerL", "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
832 {"LowerL", "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
834 {"QuotedLowerL", "%s\\test file.shlexec", 0x0, 33},
835 {"QuotedUpperL", "%s\\test file.shlexec", 0x0, 33},
837 /* Test file masked due to space */
838 {NULL, "%s\\masked file.shlexec", 0x1, 33},
839 /* Test if quoting prevents the masking */
840 {NULL, "%s\\masked file.shlexec", 0x40, 33},
842 {NULL, NULL, 0}
845 static filename_tests_t noquotes_tests[]=
847 /* Test unquoted '%1' thingies */
848 {"NoQuotes", "%s\\test file.shlexec", 0xa, 33},
849 {"LowerL", "%s\\test file.shlexec", 0xa, 33},
850 {"UpperL", "%s\\test file.shlexec", 0xa, 33},
852 {NULL, NULL, 0}
855 static void test_filename(void)
857 char filename[MAX_PATH];
858 const filename_tests_t* test;
859 char* c;
860 int rc;
862 test=filename_tests;
863 while (test->basename)
865 BOOL quotedfile = FALSE;
867 if (skip_noassoc_tests && test->rc == SE_ERR_NOASSOC)
869 win_skip("Skipping shellexecute of file with unassociated extension\n");
870 test++;
871 continue;
874 sprintf(filename, test->basename, tmpdir);
875 if (strchr(filename, '/'))
877 c=filename;
878 while (*c)
880 if (*c=='\\')
881 *c='/';
882 c++;
885 if ((test->todo & 0x40)==0)
887 rc=shell_execute(test->verb, filename, NULL, NULL);
889 else
891 char quoted[MAX_PATH + 2];
893 quotedfile = TRUE;
894 sprintf(quoted, "\"%s\"", filename);
895 rc=shell_execute(test->verb, quoted, NULL, NULL);
897 if (rc > 32)
898 rc=33;
899 if ((test->todo & 0x1)==0)
901 ok(rc==test->rc ||
902 broken(quotedfile && rc == 2), /* NT4 */
903 "%s failed: rc=%d err=%d\n", shell_call,
904 rc, GetLastError());
906 else todo_wine
908 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
909 rc, GetLastError());
911 if (rc == 33)
913 const char* verb;
914 if ((test->todo & 0x2)==0)
916 okChildInt("argcA", 5);
918 else todo_wine
920 okChildInt("argcA", 5);
922 verb=(test->verb ? test->verb : "Open");
923 if ((test->todo & 0x4)==0)
925 okChildString("argvA3", verb);
927 else todo_wine
929 okChildString("argvA3", verb);
931 if ((test->todo & 0x8)==0)
933 okChildPath("argvA4", filename);
935 else todo_wine
937 okChildPath("argvA4", filename);
940 test++;
943 test=noquotes_tests;
944 while (test->basename)
946 sprintf(filename, test->basename, tmpdir);
947 rc=shell_execute(test->verb, filename, NULL, NULL);
948 if (rc > 32)
949 rc=33;
950 if ((test->todo & 0x1)==0)
952 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
953 rc, GetLastError());
955 else todo_wine
957 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
958 rc, GetLastError());
960 if (rc==0)
962 int count;
963 const char* verb;
964 char* str;
966 verb=(test->verb ? test->verb : "Open");
967 if ((test->todo & 0x4)==0)
969 okChildString("argvA3", verb);
971 else todo_wine
973 okChildString("argvA3", verb);
976 count=4;
977 str=filename;
978 while (1)
980 char attrib[18];
981 char* space;
982 space=strchr(str, ' ');
983 if (space)
984 *space='\0';
985 sprintf(attrib, "argvA%d", count);
986 if ((test->todo & 0x8)==0)
988 okChildPath(attrib, str);
990 else todo_wine
992 okChildPath(attrib, str);
994 count++;
995 if (!space)
996 break;
997 str=space+1;
999 if ((test->todo & 0x2)==0)
1001 okChildInt("argcA", count);
1003 else todo_wine
1005 okChildInt("argcA", count);
1008 test++;
1011 if (dllver.dwMajorVersion != 0)
1013 /* The more recent versions of shell32.dll accept quoted filenames
1014 * while older ones (e.g. 4.00) don't. Still we want to test this
1015 * because IE 6 depends on the new behavior.
1016 * One day we may need to check the exact version of the dll but for
1017 * now making sure DllGetVersion() is present is sufficient.
1019 sprintf(filename, "\"%s\\test file.shlexec\"", tmpdir);
1020 rc=shell_execute(NULL, filename, NULL, NULL);
1021 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
1022 GetLastError());
1023 okChildInt("argcA", 5);
1024 okChildString("argvA3", "Open");
1025 sprintf(filename, "%s\\test file.shlexec", tmpdir);
1026 okChildPath("argvA4", filename);
1030 static void test_find_executable(void)
1032 char filename[MAX_PATH];
1033 char command[MAX_PATH];
1034 const filename_tests_t* test;
1035 INT_PTR rc;
1037 if (!create_test_association(".sfe"))
1039 skip("Unable to create association for '.sfe'\n");
1040 return;
1042 create_test_verb(".sfe", "Open", 1, "%1");
1044 /* Don't test FindExecutable(..., NULL), it always crashes */
1046 strcpy(command, "your word");
1047 if (0) /* Can crash on Vista! */
1049 rc=(INT_PTR)FindExecutableA(NULL, NULL, command);
1050 ok(rc == SE_ERR_FNF || rc > 32 /* nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
1051 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
1054 strcpy(command, "your word");
1055 rc=(INT_PTR)FindExecutableA(tmpdir, NULL, command);
1056 ok(rc == SE_ERR_NOASSOC /* >= win2000 */ || rc > 32 /* win98, nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
1057 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
1059 sprintf(filename, "%s\\test file.sfe", tmpdir);
1060 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1061 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1062 /* Depending on the platform, command could be '%1' or 'test file.sfe' */
1064 rc=(INT_PTR)FindExecutableA("test file.sfe", tmpdir, command);
1065 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1067 rc=(INT_PTR)FindExecutableA("test file.sfe", NULL, command);
1068 ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %ld\n", filename, rc);
1070 delete_test_association(".sfe");
1072 if (!create_test_association(".shl"))
1074 skip("Unable to create association for '.shl'\n");
1075 return;
1077 create_test_verb(".shl", "Open", 0, "Open");
1079 sprintf(filename, "%s\\test file.shl", tmpdir);
1080 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1081 ok(rc == SE_ERR_FNF /* NT4 */ || rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1083 sprintf(filename, "%s\\test file.shlfoo", tmpdir);
1084 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1086 delete_test_association(".shl");
1088 if (rc > 32)
1090 /* On Windows XP and 2003 FindExecutable() is completely broken.
1091 * Probably what it does is convert the filename to 8.3 format,
1092 * which as a side effect converts the '.shlfoo' extension to '.shl',
1093 * and then tries to find an association for '.shl'. This means it
1094 * will normally fail on most extensions with more than 3 characters,
1095 * like '.mpeg', etc.
1096 * Also it means we cannot do any other test.
1098 win_skip("FindExecutable() is broken -> not running 4+ character extension tests\n");
1099 return;
1102 test=filename_tests;
1103 while (test->basename)
1105 sprintf(filename, test->basename, tmpdir);
1106 if (strchr(filename, '/'))
1108 char* c;
1109 c=filename;
1110 while (*c)
1112 if (*c=='\\')
1113 *c='/';
1114 c++;
1117 /* Win98 does not '\0'-terminate command! */
1118 memset(command, '\0', sizeof(command));
1119 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1120 if (rc > 32)
1121 rc=33;
1122 if ((test->todo & 0x10)==0)
1124 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
1126 else todo_wine
1128 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
1130 if (rc > 32)
1132 int equal;
1133 equal=strcmp(command, argv0) == 0 ||
1134 /* NT4 returns an extra 0x8 character! */
1135 (strlen(command) == strlen(argv0)+1 && strncmp(command, argv0, strlen(argv0)) == 0);
1136 if ((test->todo & 0x20)==0)
1138 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
1139 filename, command, argv0);
1141 else todo_wine
1143 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
1144 filename, command, argv0);
1147 test++;
1152 static filename_tests_t lnk_tests[]=
1154 /* Pass bad / nonexistent filenames as a parameter */
1155 {NULL, "%s\\nonexistent.shlexec", 0xa, 33},
1156 {NULL, "%s\\nonexistent.noassoc", 0xa, 33},
1158 /* Pass regular paths as a parameter */
1159 {NULL, "%s\\test file.shlexec", 0xa, 33},
1160 {NULL, "%s/%%nasty%% $file.shlexec", 0xa, 33},
1162 /* Pass filenames with no association as a parameter */
1163 {NULL, "%s\\test file.noassoc", 0xa, 33},
1165 {NULL, NULL, 0}
1168 static void test_lnks(void)
1170 char filename[MAX_PATH];
1171 char params[MAX_PATH];
1172 const filename_tests_t* test;
1173 int rc;
1175 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
1176 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
1177 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
1178 GetLastError());
1179 okChildInt("argcA", 5);
1180 okChildString("argvA3", "Open");
1181 sprintf(params, "%s\\test file.shlexec", tmpdir);
1182 get_long_path_name(params, filename, sizeof(filename));
1183 okChildPath("argvA4", filename);
1185 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1186 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
1187 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
1188 GetLastError());
1189 okChildInt("argcA", 4);
1190 okChildString("argvA3", "Lnk");
1192 if (dllver.dwMajorVersion>=6)
1194 char* c;
1195 /* Recent versions of shell32.dll accept '/'s in shortcut paths.
1196 * Older versions don't or are quite buggy in this regard.
1198 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1199 c=filename;
1200 while (*c)
1202 if (*c=='\\')
1203 *c='/';
1204 c++;
1206 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
1207 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
1208 GetLastError());
1209 okChildInt("argcA", 4);
1210 okChildString("argvA3", "Lnk");
1213 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1214 test=lnk_tests;
1215 while (test->basename)
1217 params[0]='\"';
1218 sprintf(params+1, test->basename, tmpdir);
1219 strcat(params,"\"");
1220 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, params,
1221 NULL);
1222 if (rc > 32)
1223 rc=33;
1224 if ((test->todo & 0x1)==0)
1226 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1227 rc, GetLastError());
1229 else todo_wine
1231 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1232 rc, GetLastError());
1234 if (rc==0)
1236 if ((test->todo & 0x2)==0)
1238 okChildInt("argcA", 5);
1240 else
1242 okChildInt("argcA", 5);
1244 if ((test->todo & 0x4)==0)
1246 okChildString("argvA3", "Lnk");
1248 else todo_wine
1250 okChildString("argvA3", "Lnk");
1252 sprintf(params, test->basename, tmpdir);
1253 if ((test->todo & 0x8)==0)
1255 okChildPath("argvA4", params);
1257 else
1259 okChildPath("argvA4", params);
1262 test++;
1267 static void test_exes(void)
1269 char filename[MAX_PATH];
1270 char params[1024];
1271 int rc;
1273 sprintf(params, "shlexec \"%s\" Exec", child_file);
1275 /* We need NOZONECHECKS on Win2003 to block a dialog */
1276 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
1277 NULL);
1278 ok(rc > 32, "%s returned %d\n", shell_call, rc);
1279 okChildInt("argcA", 4);
1280 okChildString("argvA3", "Exec");
1282 if (! skip_noassoc_tests)
1284 sprintf(filename, "%s\\test file.noassoc", tmpdir);
1285 if (CopyFile(argv0, filename, FALSE))
1287 rc=shell_execute(NULL, filename, params, NULL);
1288 todo_wine {
1289 ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
1293 else
1295 win_skip("Skipping shellexecute of file with unassociated extension\n");
1299 static void test_exes_long(void)
1301 char filename[MAX_PATH];
1302 char params[2024];
1303 char longparam[MAX_PATH];
1304 int rc;
1306 for (rc = 0; rc < MAX_PATH; rc++)
1307 longparam[rc]='a'+rc%26;
1308 longparam[MAX_PATH-1]=0;
1311 sprintf(params, "shlexec \"%s\" %s", child_file,longparam);
1313 /* We need NOZONECHECKS on Win2003 to block a dialog */
1314 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
1315 NULL);
1316 ok(rc > 32, "%s returned %d\n", shell_call, rc);
1317 okChildInt("argcA", 4);
1318 okChildString("argvA3", longparam);
1320 if (! skip_noassoc_tests)
1322 sprintf(filename, "%s\\test file.noassoc", tmpdir);
1323 if (CopyFile(argv0, filename, FALSE))
1325 rc=shell_execute(NULL, filename, params, NULL);
1326 todo_wine {
1327 ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
1331 else
1333 win_skip("Skipping shellexecute of file with unassociated extension\n");
1337 typedef struct
1339 const char* command;
1340 const char* ddeexec;
1341 const char* application;
1342 const char* topic;
1343 const char* ifexec;
1344 int expectedArgs;
1345 const char* expectedDdeExec;
1346 int todo;
1347 } dde_tests_t;
1349 static dde_tests_t dde_tests[] =
1351 /* Test passing and not passing command-line
1352 * argument, no DDE */
1353 {"", NULL, NULL, NULL, NULL, FALSE, "", 0x0},
1354 {"\"%1\"", NULL, NULL, NULL, NULL, TRUE, "", 0x0},
1356 /* Test passing and not passing command-line
1357 * argument, with DDE */
1358 {"", "[open(\"%1\")]", "shlexec", "dde", NULL, FALSE, "[open(\"%s\")]", 0x0},
1359 {"\"%1\"", "[open(\"%1\")]", "shlexec", "dde", NULL, TRUE, "[open(\"%s\")]", 0x0},
1361 /* Test unquoted %1 in command and ddeexec
1362 * (test filename has space) */
1363 {"%1", "[open(%1)]", "shlexec", "dde", NULL, 2, "[open(%s)]", 0x0},
1365 /* Test ifexec precedence over ddeexec */
1366 {"", "[open(\"%1\")]", "shlexec", "dde", "[ifexec(\"%1\")]", FALSE, "[ifexec(\"%s\")]", 0x0},
1368 /* Test default DDE topic */
1369 {"", "[open(\"%1\")]", "shlexec", NULL, NULL, FALSE, "[open(\"%s\")]", 0x0},
1371 /* Test default DDE application */
1372 {"", "[open(\"%1\")]", NULL, "dde", NULL, FALSE, "[open(\"%s\")]", 0x0},
1374 {NULL, NULL, NULL, NULL, NULL, 0, 0x0}
1377 static DWORD WINAPI hooked_WaitForInputIdle(HANDLE process, DWORD timeout)
1379 HANDLE dde_ready;
1380 DWORD wait_result;
1382 dde_ready = CreateEventA(NULL, FALSE, FALSE, "winetest_shlexec_dde_ready");
1383 wait_result = WaitForSingleObject(dde_ready, timeout);
1384 CloseHandle(dde_ready);
1386 return wait_result;
1390 * WaitForInputIdle() will normally return immediately for console apps. That's
1391 * a problem for us because ShellExecute will assume that an app is ready to
1392 * receive DDE messages after it has called WaitForInputIdle() on that app.
1393 * To work around that we install our own version of WaitForInputIdle() that
1394 * will wait for the child to explicitly tell us that it is ready. We do that
1395 * by changing the entry for WaitForInputIdle() in the shell32 import address
1396 * table.
1398 static void hook_WaitForInputIdle(void *new_func)
1400 char *base;
1401 PIMAGE_NT_HEADERS nt_headers;
1402 DWORD import_directory_rva;
1403 PIMAGE_IMPORT_DESCRIPTOR import_descriptor;
1405 base = (char *) GetModuleHandleA("shell32.dll");
1406 nt_headers = (PIMAGE_NT_HEADERS)(base + ((PIMAGE_DOS_HEADER) base)->e_lfanew);
1407 import_directory_rva = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
1409 /* Search for the correct imported module by walking the import descriptors */
1410 import_descriptor = (PIMAGE_IMPORT_DESCRIPTOR)(base + import_directory_rva);
1411 while (U(*import_descriptor).OriginalFirstThunk != 0)
1413 char *import_module_name;
1415 import_module_name = base + import_descriptor->Name;
1416 if (lstrcmpiA(import_module_name, "user32.dll") == 0 ||
1417 lstrcmpiA(import_module_name, "user32") == 0)
1419 PIMAGE_THUNK_DATA int_entry;
1420 PIMAGE_THUNK_DATA iat_entry;
1422 /* The import name table and import address table are two parallel
1423 * arrays. We need the import name table to find the imported
1424 * routine and the import address table to patch the address, so
1425 * walk them side by side */
1426 int_entry = (PIMAGE_THUNK_DATA)(base + U(*import_descriptor).OriginalFirstThunk);
1427 iat_entry = (PIMAGE_THUNK_DATA)(base + import_descriptor->FirstThunk);
1428 while (int_entry->u1.Ordinal != 0)
1430 if (! IMAGE_SNAP_BY_ORDINAL(int_entry->u1.Ordinal))
1432 PIMAGE_IMPORT_BY_NAME import_by_name;
1433 import_by_name = (PIMAGE_IMPORT_BY_NAME)(base + int_entry->u1.AddressOfData);
1434 if (lstrcmpA((char *) import_by_name->Name, "WaitForInputIdle") == 0)
1436 /* Found the correct routine in the correct imported module. Patch it. */
1437 DWORD old_prot;
1438 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), PAGE_READWRITE, &old_prot);
1439 iat_entry->u1.Function = (ULONG_PTR) new_func;
1440 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), old_prot, &old_prot);
1441 break;
1444 int_entry++;
1445 iat_entry++;
1447 break;
1450 import_descriptor++;
1454 static void test_dde(void)
1456 char filename[MAX_PATH], defApplication[MAX_PATH];
1457 const dde_tests_t* test;
1458 char params[1024];
1459 int rc;
1460 HANDLE map;
1461 char *shared_block;
1463 hook_WaitForInputIdle((void *) hooked_WaitForInputIdle);
1465 sprintf(filename, "%s\\test file.sde", tmpdir);
1467 /* Default service is application name minus path and extension */
1468 strcpy(defApplication, strrchr(argv0, '\\')+1);
1469 *strchr(defApplication, '.') = 0;
1471 map = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
1472 4096, "winetest_shlexec_dde_map");
1473 shared_block = MapViewOfFile(map, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 4096);
1475 test = dde_tests;
1476 while (test->command)
1478 if (!create_test_association(".sde"))
1480 skip("Unable to create association for '.sfe'\n");
1481 return;
1483 create_test_verb_dde(".sde", "Open", 0, test->command, test->ddeexec,
1484 test->application, test->topic, test->ifexec);
1486 if (test->application != NULL || test->topic != NULL)
1488 strcpy(shared_block, test->application ? test->application : defApplication);
1489 strcpy(shared_block + strlen(shared_block) + 1, test->topic ? test->topic : SZDDESYS_TOPIC);
1491 else
1493 shared_block[0] = '\0';
1494 shared_block[1] = '\0';
1496 ddeExec[0] = 0;
1498 rc = shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, filename, NULL, NULL);
1499 if ((test->todo & 0x1)==0)
1501 ok(32 < rc, "%s failed: rc=%d err=%d\n", shell_call,
1502 rc, GetLastError());
1504 else todo_wine
1506 ok(32 < rc, "%s failed: rc=%d err=%d\n", shell_call,
1507 rc, GetLastError());
1509 if (32 < rc)
1511 if ((test->todo & 0x2)==0)
1513 okChildInt("argcA", test->expectedArgs + 3);
1515 else todo_wine
1517 okChildInt("argcA", test->expectedArgs + 3);
1519 if (test->expectedArgs == 1)
1521 if ((test->todo & 0x4) == 0)
1523 okChildPath("argvA3", filename);
1525 else todo_wine
1527 okChildPath("argvA3", filename);
1530 if ((test->todo & 0x8) == 0)
1532 sprintf(params, test->expectedDdeExec, filename);
1533 okChildPath("ddeExec", params);
1535 else todo_wine
1537 sprintf(params, test->expectedDdeExec, filename);
1538 okChildPath("ddeExec", params);
1542 delete_test_association(".sde");
1543 test++;
1546 UnmapViewOfFile(shared_block);
1547 CloseHandle(map);
1548 hook_WaitForInputIdle((void *) WaitForInputIdle);
1551 #define DDE_DEFAULT_APP_VARIANTS 2
1552 typedef struct
1554 const char* command;
1555 const char* expectedDdeApplication[DDE_DEFAULT_APP_VARIANTS];
1556 int todo;
1557 int rc[DDE_DEFAULT_APP_VARIANTS];
1558 } dde_default_app_tests_t;
1560 static dde_default_app_tests_t dde_default_app_tests[] =
1562 /* Windows XP and 98 handle default DDE app names in different ways.
1563 * The application name we see in the first test determines the pattern
1564 * of application names and return codes we will look for. */
1566 /* Test unquoted existing filename with a space */
1567 {"%s\\test file.exe", {"test file", "test"}, 0x0, {33, 33}},
1568 {"%s\\test file.exe param", {"test file", "test"}, 0x0, {33, 33}},
1570 /* Test quoted existing filename with a space */
1571 {"\"%s\\test file.exe\"", {"test file", "test file"}, 0x0, {33, 33}},
1572 {"\"%s\\test file.exe\" param", {"test file", "test file"}, 0x0, {33, 33}},
1574 /* Test unquoted filename with a space that doesn't exist, but
1575 * test2.exe does */
1576 {"%s\\test2 file.exe", {"test2", "test2"}, 0x0, {33, 33}},
1577 {"%s\\test2 file.exe param", {"test2", "test2"}, 0x0, {33, 33}},
1579 /* Test quoted filename with a space that does not exist */
1580 {"\"%s\\test2 file.exe\"", {"", "test2 file"}, 0x0, {5, 33}},
1581 {"\"%s\\test2 file.exe\" param", {"", "test2 file"}, 0x0, {5, 33}},
1583 /* Test filename supplied without the extension */
1584 {"%s\\test2", {"test2", "test2"}, 0x0, {33, 33}},
1585 {"%s\\test2 param", {"test2", "test2"}, 0x0, {33, 33}},
1587 /* Test an unquoted nonexistent filename */
1588 {"%s\\notexist.exe", {"", "notexist"}, 0x0, {5, 33}},
1589 {"%s\\notexist.exe param", {"", "notexist"}, 0x0, {5, 33}},
1591 /* Test an application that will be found on the path */
1592 {"cmd", {"cmd", "cmd"}, 0x0, {33, 33}},
1593 {"cmd param", {"cmd", "cmd"}, 0x0, {33, 33}},
1595 /* Test an application that will not be found on the path */
1596 {"xyzwxyzwxyz", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
1597 {"xyzwxyzwxyz param", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
1599 {NULL, {NULL}, 0, {0}}
1602 typedef struct
1604 char *filename;
1605 DWORD threadIdParent;
1606 } dde_thread_info_t;
1608 static DWORD CALLBACK ddeThread(LPVOID arg)
1610 dde_thread_info_t *info = arg;
1611 assert(info && info->filename);
1612 PostThreadMessage(info->threadIdParent,
1613 WM_QUIT,
1614 shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, info->filename, NULL, NULL),
1615 0L);
1616 ExitThread(0);
1619 static void test_dde_default_app(void)
1621 char filename[MAX_PATH];
1622 HSZ hszApplication;
1623 dde_thread_info_t info = { filename, GetCurrentThreadId() };
1624 const dde_default_app_tests_t* test;
1625 char params[1024];
1626 DWORD threadId;
1627 MSG msg;
1628 int rc, which = 0;
1630 post_quit_on_execute = FALSE;
1631 ddeInst = 0;
1632 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
1633 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
1634 assert(rc == DMLERR_NO_ERROR);
1636 sprintf(filename, "%s\\test file.sde", tmpdir);
1638 /* It is strictly not necessary to register an application name here, but wine's
1639 * DdeNameService implementation complains if 0L is passed instead of
1640 * hszApplication with DNS_FILTEROFF */
1641 hszApplication = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
1642 hszTopic = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
1643 assert(hszApplication && hszTopic);
1644 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER | DNS_FILTEROFF));
1646 test = dde_default_app_tests;
1647 while (test->command)
1649 if (!create_test_association(".sde"))
1651 skip("Unable to create association for '.sde'\n");
1652 return;
1654 sprintf(params, test->command, tmpdir);
1655 create_test_verb_dde(".sde", "Open", 1, params, "[test]", NULL,
1656 "shlexec", NULL);
1657 ddeApplication[0] = 0;
1659 /* No application will be run as we will respond to the first DDE event,
1660 * so don't wait for it */
1661 SetEvent(hEvent);
1663 assert(CreateThread(NULL, 0, ddeThread, &info, 0, &threadId));
1664 while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);
1665 rc = msg.wParam > 32 ? 33 : msg.wParam;
1667 /* First test, find which set of test data we expect to see */
1668 if (test == dde_default_app_tests)
1670 int i;
1671 for (i=0; i<DDE_DEFAULT_APP_VARIANTS; i++)
1673 if (!strcmp(ddeApplication, test->expectedDdeApplication[i]))
1675 which = i;
1676 break;
1679 if (i == DDE_DEFAULT_APP_VARIANTS)
1680 skip("Default DDE application test does not match any available results, using first expected data set.\n");
1683 if ((test->todo & 0x1)==0)
1685 ok(rc==test->rc[which], "%s failed: rc=%d err=%d\n", shell_call,
1686 rc, GetLastError());
1688 else todo_wine
1690 ok(rc==test->rc[which], "%s failed: rc=%d err=%d\n", shell_call,
1691 rc, GetLastError());
1693 if (rc == 33)
1695 if ((test->todo & 0x2)==0)
1697 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
1698 "Expected application '%s', got '%s'\n",
1699 test->expectedDdeApplication[which], ddeApplication);
1701 else todo_wine
1703 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
1704 "Expected application '%s', got '%s'\n",
1705 test->expectedDdeApplication[which], ddeApplication);
1709 delete_test_association(".sde");
1710 test++;
1713 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
1714 assert(DdeFreeStringHandle(ddeInst, hszTopic));
1715 assert(DdeFreeStringHandle(ddeInst, hszApplication));
1716 assert(DdeUninitialize(ddeInst));
1719 static void init_test(void)
1721 HMODULE hdll;
1722 HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO*);
1723 char filename[MAX_PATH];
1724 WCHAR lnkfile[MAX_PATH];
1725 char params[1024];
1726 const char* const * testfile;
1727 lnk_desc_t desc;
1728 DWORD rc;
1729 HRESULT r;
1731 hdll=GetModuleHandleA("shell32.dll");
1732 pDllGetVersion=(void*)GetProcAddress(hdll, "DllGetVersion");
1733 if (pDllGetVersion)
1735 dllver.cbSize=sizeof(dllver);
1736 pDllGetVersion(&dllver);
1737 trace("major=%d minor=%d build=%d platform=%d\n",
1738 dllver.dwMajorVersion, dllver.dwMinorVersion,
1739 dllver.dwBuildNumber, dllver.dwPlatformID);
1741 else
1743 memset(&dllver, 0, sizeof(dllver));
1746 r = CoInitialize(NULL);
1747 ok(SUCCEEDED(r), "CoInitialize failed (0x%08x)\n", r);
1748 if (FAILED(r))
1749 exit(1);
1751 rc=GetModuleFileName(NULL, argv0, sizeof(argv0));
1752 assert(rc!=0 && rc<sizeof(argv0));
1753 if (GetFileAttributes(argv0)==INVALID_FILE_ATTRIBUTES)
1755 strcat(argv0, ".so");
1756 ok(GetFileAttributes(argv0)!=INVALID_FILE_ATTRIBUTES,
1757 "unable to find argv0!\n");
1760 GetTempPathA(sizeof(filename), filename);
1761 GetTempFileNameA(filename, "wt", 0, tmpdir);
1762 DeleteFileA( tmpdir );
1763 rc = CreateDirectoryA( tmpdir, NULL );
1764 ok( rc, "failed to create %s err %u\n", tmpdir, GetLastError() );
1765 rc = GetTempFileNameA(tmpdir, "wt", 0, child_file);
1766 assert(rc != 0);
1767 init_event(child_file);
1769 /* Set up the test files */
1770 testfile=testfiles;
1771 while (*testfile)
1773 HANDLE hfile;
1775 sprintf(filename, *testfile, tmpdir);
1776 hfile=CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1777 FILE_ATTRIBUTE_NORMAL, NULL);
1778 if (hfile==INVALID_HANDLE_VALUE)
1780 trace("unable to create '%s': err=%d\n", filename, GetLastError());
1781 assert(0);
1783 CloseHandle(hfile);
1784 testfile++;
1787 /* Setup the test shortcuts */
1788 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
1789 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
1790 desc.description=NULL;
1791 desc.workdir=NULL;
1792 sprintf(filename, "%s\\test file.shlexec", tmpdir);
1793 desc.path=filename;
1794 desc.pidl=NULL;
1795 desc.arguments="ignored";
1796 desc.showcmd=0;
1797 desc.icon=NULL;
1798 desc.icon_id=0;
1799 desc.hotkey=0;
1800 create_lnk(lnkfile, &desc, 0);
1802 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1803 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
1804 desc.description=NULL;
1805 desc.workdir=NULL;
1806 desc.path=argv0;
1807 desc.pidl=NULL;
1808 sprintf(params, "shlexec \"%s\" Lnk", child_file);
1809 desc.arguments=params;
1810 desc.showcmd=0;
1811 desc.icon=NULL;
1812 desc.icon_id=0;
1813 desc.hotkey=0;
1814 create_lnk(lnkfile, &desc, 0);
1816 /* Create a basic association suitable for most tests */
1817 if (!create_test_association(".shlexec"))
1819 skip("Unable to create association for '.shlexec'\n");
1820 return;
1822 create_test_verb(".shlexec", "Open", 0, "Open \"%1\"");
1823 create_test_verb(".shlexec", "NoQuotes", 0, "NoQuotes %1");
1824 create_test_verb(".shlexec", "LowerL", 0, "LowerL %l");
1825 create_test_verb(".shlexec", "QuotedLowerL", 0, "QuotedLowerL \"%l\"");
1826 create_test_verb(".shlexec", "UpperL", 0, "UpperL %L");
1827 create_test_verb(".shlexec", "QuotedUpperL", 0, "QuotedUpperL \"%L\"");
1830 static void cleanup_test(void)
1832 char filename[MAX_PATH];
1833 const char* const * testfile;
1835 /* Delete the test files */
1836 testfile=testfiles;
1837 while (*testfile)
1839 sprintf(filename, *testfile, tmpdir);
1840 /* Make sure we can delete the files ('test file.noassoc' is read-only now) */
1841 SetFileAttributes(filename, FILE_ATTRIBUTE_NORMAL);
1842 DeleteFile(filename);
1843 testfile++;
1845 DeleteFile(child_file);
1846 RemoveDirectoryA(tmpdir);
1848 /* Delete the test association */
1849 delete_test_association(".shlexec");
1851 CloseHandle(hEvent);
1853 CoUninitialize();
1856 static void test_commandline(void)
1858 static const WCHAR one[] = {'o','n','e',0};
1859 static const WCHAR two[] = {'t','w','o',0};
1860 static const WCHAR three[] = {'t','h','r','e','e',0};
1861 static const WCHAR four[] = {'f','o','u','r',0};
1863 static const WCHAR fmt1[] = {'%','s',' ','%','s',' ','%','s',' ','%','s',0};
1864 static const WCHAR fmt2[] = {' ','%','s',' ','%','s',' ','%','s',' ','%','s',0};
1865 static const WCHAR fmt3[] = {'%','s','=','%','s',' ','%','s','=','\"','%','s','\"',0};
1866 static const WCHAR fmt4[] = {'\"','%','s','\"',' ','\"','%','s',' ','%','s','\"',' ','%','s',0};
1867 static const WCHAR fmt5[] = {'\\','\"','%','s','\"',' ','%','s','=','\"','%','s','\\','\"',' ','\"','%','s','\\','\"',0};
1868 static const WCHAR fmt6[] = {0};
1870 static const WCHAR chkfmt1[] = {'%','s','=','%','s',0};
1871 static const WCHAR chkfmt2[] = {'%','s',' ','%','s',0};
1872 static const WCHAR chkfmt3[] = {'\\','\"','%','s','\"',0};
1873 static const WCHAR chkfmt4[] = {'%','s','=','%','s','\"',' ','%','s','\"',0};
1874 WCHAR cmdline[255];
1875 LPWSTR *args = (LPWSTR*)0xdeadcafe;
1876 INT numargs = -1;
1878 wsprintfW(cmdline,fmt1,one,two,three,four);
1879 args=CommandLineToArgvW(cmdline,&numargs);
1880 if (args == NULL && numargs == -1)
1882 win_skip("CommandLineToArgvW not implemented, skipping\n");
1883 return;
1885 ok(numargs == 4, "expected 4 args, got %i\n",numargs);
1886 ok(lstrcmpW(args[0],one)==0,"arg0 is not as expected\n");
1887 ok(lstrcmpW(args[1],two)==0,"arg1 is not as expected\n");
1888 ok(lstrcmpW(args[2],three)==0,"arg2 is not as expected\n");
1889 ok(lstrcmpW(args[3],four)==0,"arg3 is not as expected\n");
1891 wsprintfW(cmdline,fmt2,one,two,three,four);
1892 args=CommandLineToArgvW(cmdline,&numargs);
1893 ok(numargs == 5, "expected 5 args, got %i\n",numargs);
1894 ok(args[0][0]==0,"arg0 is not as expected\n");
1895 ok(lstrcmpW(args[1],one)==0,"arg1 is not as expected\n");
1896 ok(lstrcmpW(args[2],two)==0,"arg2 is not as expected\n");
1897 ok(lstrcmpW(args[3],three)==0,"arg3 is not as expected\n");
1898 ok(lstrcmpW(args[4],four)==0,"arg4 is not as expected\n");
1900 wsprintfW(cmdline,fmt3,one,two,three,four);
1901 args=CommandLineToArgvW(cmdline,&numargs);
1902 ok(numargs == 2, "expected 2 args, got %i\n",numargs);
1903 wsprintfW(cmdline,chkfmt1,one,two);
1904 ok(lstrcmpW(args[0],cmdline)==0,"arg0 is not as expected\n");
1905 wsprintfW(cmdline,chkfmt1,three,four);
1906 ok(lstrcmpW(args[1],cmdline)==0,"arg1 is not as expected\n");
1908 wsprintfW(cmdline,fmt4,one,two,three,four);
1909 args=CommandLineToArgvW(cmdline,&numargs);
1910 ok(numargs == 3, "expected 3 args, got %i\n",numargs);
1911 ok(lstrcmpW(args[0],one)==0,"arg0 is not as expected\n");
1912 wsprintfW(cmdline,chkfmt2,two,three);
1913 ok(lstrcmpW(args[1],cmdline)==0,"arg1 is not as expected\n");
1914 ok(lstrcmpW(args[2],four)==0,"arg2 is not as expected\n");
1916 wsprintfW(cmdline,fmt5,one,two,three,four);
1917 args=CommandLineToArgvW(cmdline,&numargs);
1918 ok(numargs == 2, "expected 2 args, got %i\n",numargs);
1919 wsprintfW(cmdline,chkfmt3,one);
1920 todo_wine ok(lstrcmpW(args[0],cmdline)==0,"arg0 is not as expected\n");
1921 wsprintfW(cmdline,chkfmt4,two,three,four);
1922 todo_wine ok(lstrcmpW(args[1],cmdline)==0,"arg1 is not as expected\n");
1924 wsprintfW(cmdline,fmt6);
1925 args=CommandLineToArgvW(cmdline,&numargs);
1926 ok(numargs == 1, "expected 1 args, got %i\n",numargs);
1929 START_TEST(shlexec)
1932 myARGC = winetest_get_mainargs(&myARGV);
1933 if (myARGC >= 3)
1935 doChild(myARGC, myARGV);
1936 exit(0);
1939 init_test();
1941 test_filename();
1942 test_find_executable();
1943 test_lnks();
1944 test_exes();
1945 test_exes_long();
1946 test_dde();
1947 test_dde_default_app();
1948 test_commandline();
1950 cleanup_test();