shell32/tests: Create_test_association() should either succeed or fail due to insuffi...
[wine.git] / dlls / shell32 / tests / shlexec.c
blobb765f7c1d64411bff774038f1ceacf85e891b683
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 "ddeml.h"
49 #include "wine/test.h"
51 #include "shell32_test.h"
54 static char argv0[MAX_PATH];
55 static int myARGC;
56 static char** myARGV;
57 static char tmpdir[MAX_PATH];
58 static char child_file[MAX_PATH];
59 static DLLVERSIONINFO dllver;
60 static BOOL skip_shlexec_tests = FALSE;
61 static BOOL skip_noassoc_tests = FALSE;
62 static HANDLE dde_ready_event;
65 /***
67 * ShellExecute wrappers
69 ***/
70 static void dump_child(void);
72 static HANDLE hEvent;
73 static void init_event(const char* child_file)
75 char* event_name;
76 event_name=strrchr(child_file, '\\')+1;
77 hEvent=CreateEventA(NULL, FALSE, FALSE, event_name);
80 static void strcat_param(char* str, const char* name, const char* param)
82 if (param)
84 if (str[strlen(str)-1] == '"')
85 strcat(str, ", ");
86 strcat(str, name);
87 strcat(str, "=\"");
88 strcat(str, param);
89 strcat(str, "\"");
93 static int _todo_wait = 0;
94 #define todo_wait for (_todo_wait = 1; _todo_wait; _todo_wait = 0)
96 static char shell_call[2048]="";
97 static int bad_shellexecute = 0;
98 static INT_PTR shell_execute(LPCSTR verb, LPCSTR file, LPCSTR parameters, LPCSTR directory)
100 INT_PTR rc, rcEmpty = 0;
102 if(!verb)
103 rcEmpty = shell_execute("", file, parameters, directory);
105 strcpy(shell_call, "ShellExecute(");
106 strcat_param(shell_call, "verb", verb);
107 strcat_param(shell_call, "file", file);
108 strcat_param(shell_call, "params", parameters);
109 strcat_param(shell_call, "dir", directory);
110 strcat(shell_call, ")");
111 if (winetest_debug > 1)
112 trace("%s\n", shell_call);
114 DeleteFileA(child_file);
115 SetLastError(0xcafebabe);
117 /* FIXME: We cannot use ShellExecuteEx() here because if there is no
118 * association it displays the 'Open With' dialog and I could not find
119 * a flag to prevent this.
121 rc=(INT_PTR)ShellExecuteA(NULL, verb, file, parameters, directory, SW_HIDE);
123 if (rc > 32)
125 int wait_rc;
126 wait_rc=WaitForSingleObject(hEvent, 5000);
127 if (wait_rc == WAIT_TIMEOUT)
129 HWND wnd = FindWindowA("#32770", "Windows");
130 if (!wnd)
131 wnd = FindWindowA("Shell_Flyout", "");
132 if (wnd != NULL)
134 SendMessageA(wnd, WM_CLOSE, 0, 0);
135 win_skip("Skipping shellexecute of file with unassociated extension\n");
136 skip_noassoc_tests = TRUE;
137 rc = SE_ERR_NOASSOC;
140 if (!_todo_wait)
141 ok(wait_rc==WAIT_OBJECT_0 || rc <= 32, "%s WaitForSingleObject returned %d\n", shell_call, wait_rc);
142 else todo_wine
143 ok(wait_rc==WAIT_OBJECT_0 || rc <= 32, "%s WaitForSingleObject returned %d\n", shell_call, wait_rc);
145 /* The child process may have changed the result file, so let profile
146 * functions know about it
148 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
149 if (rc > 32)
150 dump_child();
152 if(!verb)
154 if (rc != rcEmpty && rcEmpty == SE_ERR_NOASSOC) /* NT4 */
155 bad_shellexecute = 1;
156 ok(rc == rcEmpty || broken(rc != rcEmpty && rcEmpty == SE_ERR_NOASSOC) /* NT4 */,
157 "%s Got different return value with empty string: %lu %lu\n", shell_call, rc, rcEmpty);
160 return rc;
163 static INT_PTR shell_execute_ex(DWORD mask, LPCSTR verb, LPCSTR file,
164 LPCSTR parameters, LPCSTR directory,
165 LPCSTR class)
167 SHELLEXECUTEINFOA sei;
168 BOOL success;
169 INT_PTR rc;
171 strcpy(shell_call, "ShellExecuteEx(");
172 if (mask)
174 char smask[11];
175 sprintf(smask, "0x%x", mask);
176 strcat_param(shell_call, "mask", smask);
178 strcat_param(shell_call, "verb", verb);
179 strcat_param(shell_call, "file", file);
180 strcat_param(shell_call, "params", parameters);
181 strcat_param(shell_call, "dir", directory);
182 strcat_param(shell_call, "class", class);
183 strcat(shell_call, ")");
184 if (winetest_debug > 1)
185 trace("%s\n", shell_call);
187 sei.cbSize=sizeof(sei);
188 sei.fMask=SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NO_CONSOLE | mask;
189 sei.hwnd=NULL;
190 sei.lpVerb=verb;
191 sei.lpFile=file;
192 sei.lpParameters=parameters;
193 sei.lpDirectory=directory;
194 sei.nShow=SW_SHOWNORMAL;
195 sei.hInstApp=NULL; /* Out */
196 sei.lpIDList=NULL;
197 sei.lpClass=class;
198 sei.hkeyClass=NULL;
199 sei.dwHotKey=0;
200 U(sei).hIcon=NULL;
201 sei.hProcess=(HANDLE)0xdeadbeef; /* Out */
203 DeleteFileA(child_file);
204 SetLastError(0xcafebabe);
205 success=ShellExecuteExA(&sei);
206 rc=(INT_PTR)sei.hInstApp;
207 ok((success && rc > 32) || (!success && rc <= 32),
208 "%s rc=%d and hInstApp=%ld is not allowed\n", shell_call, success, rc);
210 if (rc > 32)
212 int wait_rc;
213 if (sei.hProcess!=NULL)
215 wait_rc=WaitForSingleObject(sei.hProcess, 5000);
216 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject(hProcess) returned %d\n", wait_rc);
218 wait_rc=WaitForSingleObject(hEvent, 5000);
219 if (!_todo_wait)
220 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
221 else todo_wine
222 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
224 else
225 ok(sei.hProcess==NULL, "%s returned a process handle %p\n", shell_call, sei.hProcess);
227 /* The child process may have changed the result file, so let profile
228 * functions know about it
230 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
231 if (rc > 32)
232 dump_child();
234 return rc;
239 /***
241 * Functions to create / delete associations wrappers
243 ***/
245 static BOOL create_test_association(const char* extension)
247 HKEY hkey, hkey_shell;
248 char class[MAX_PATH];
249 LONG rc;
251 sprintf(class, "shlexec%s", extension);
252 rc=RegCreateKeyExA(HKEY_CLASSES_ROOT, extension, 0, NULL, 0, KEY_SET_VALUE,
253 NULL, &hkey, NULL);
254 ok(rc == ERROR_SUCCESS || rc == ERROR_ACCESS_DENIED,
255 "could not create association %s (rc=%d)\n", class, rc);
256 if (rc != ERROR_SUCCESS)
257 return FALSE;
259 rc=RegSetValueExA(hkey, NULL, 0, REG_SZ, (LPBYTE) class, strlen(class)+1);
260 ok(rc==ERROR_SUCCESS, "RegSetValueEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
261 CloseHandle(hkey);
263 rc=RegCreateKeyExA(HKEY_CLASSES_ROOT, class, 0, NULL, 0,
264 KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS, NULL, &hkey, NULL);
265 ok(rc==ERROR_SUCCESS, "RegCreateKeyEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
267 rc=RegCreateKeyExA(hkey, "shell", 0, NULL, 0,
268 KEY_CREATE_SUB_KEY, NULL, &hkey_shell, NULL);
269 ok(rc==ERROR_SUCCESS, "RegCreateKeyEx 'shell' failed, expected ERROR_SUCCESS, got %d\n", rc);
271 CloseHandle(hkey);
272 CloseHandle(hkey_shell);
274 return TRUE;
277 /* Based on RegDeleteTreeW from dlls/advapi32/registry.c */
278 static LSTATUS myRegDeleteTreeA(HKEY hKey, LPCSTR lpszSubKey)
280 LONG ret;
281 DWORD dwMaxSubkeyLen, dwMaxValueLen;
282 DWORD dwMaxLen, dwSize;
283 CHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
284 HKEY hSubKey = hKey;
286 if(lpszSubKey)
288 ret = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
289 if (ret) return ret;
292 /* Get highest length for keys, values */
293 ret = RegQueryInfoKeyA(hSubKey, NULL, NULL, NULL, NULL,
294 &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
295 if (ret) goto cleanup;
297 dwMaxSubkeyLen++;
298 dwMaxValueLen++;
299 dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
300 if (dwMaxLen > sizeof(szNameBuf)/sizeof(CHAR))
302 /* Name too big: alloc a buffer for it */
303 if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(CHAR))))
305 ret = ERROR_NOT_ENOUGH_MEMORY;
306 goto cleanup;
311 /* Recursively delete all the subkeys */
312 while (TRUE)
314 dwSize = dwMaxLen;
315 if (RegEnumKeyExA(hSubKey, 0, lpszName, &dwSize, NULL,
316 NULL, NULL, NULL)) break;
318 ret = myRegDeleteTreeA(hSubKey, lpszName);
319 if (ret) goto cleanup;
322 if (lpszSubKey)
323 ret = RegDeleteKeyA(hKey, lpszSubKey);
324 else
325 while (TRUE)
327 dwSize = dwMaxLen;
328 if (RegEnumValueA(hKey, 0, lpszName, &dwSize,
329 NULL, NULL, NULL, NULL)) break;
331 ret = RegDeleteValueA(hKey, lpszName);
332 if (ret) goto cleanup;
335 cleanup:
336 /* Free buffer if allocated */
337 if (lpszName != szNameBuf)
338 HeapFree( GetProcessHeap(), 0, lpszName);
339 if(lpszSubKey)
340 RegCloseKey(hSubKey);
341 return ret;
344 static void delete_test_association(const char* extension)
346 char class[MAX_PATH];
348 sprintf(class, "shlexec%s", extension);
349 myRegDeleteTreeA(HKEY_CLASSES_ROOT, class);
350 myRegDeleteTreeA(HKEY_CLASSES_ROOT, extension);
353 static void create_test_verb_dde(const char* extension, const char* verb,
354 int rawcmd, const char* cmdtail, const char *ddeexec,
355 const char *application, const char *topic,
356 const char *ifexec)
358 HKEY hkey_shell, hkey_verb, hkey_cmd;
359 char shell[MAX_PATH];
360 char* cmd;
361 LONG rc;
363 sprintf(shell, "shlexec%s\\shell", extension);
364 rc=RegOpenKeyExA(HKEY_CLASSES_ROOT, shell, 0,
365 KEY_CREATE_SUB_KEY, &hkey_shell);
366 ok(rc == ERROR_SUCCESS, "%s key creation failed with %d\n", shell, rc);
368 rc=RegCreateKeyExA(hkey_shell, verb, 0, NULL, 0, KEY_CREATE_SUB_KEY,
369 NULL, &hkey_verb, NULL);
370 ok(rc == ERROR_SUCCESS, "%s verb key creation failed with %d\n", verb, rc);
372 rc=RegCreateKeyExA(hkey_verb, "command", 0, NULL, 0, KEY_SET_VALUE,
373 NULL, &hkey_cmd, NULL);
374 ok(rc == ERROR_SUCCESS, "\'command\' key creation failed with %d\n", rc);
376 if (rawcmd)
378 rc=RegSetValueExA(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmdtail, strlen(cmdtail)+1);
380 else
382 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(child_file)+2+strlen(cmdtail)+1);
383 sprintf(cmd,"%s shlexec \"%s\" %s", argv0, child_file, cmdtail);
384 rc=RegSetValueExA(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmd, strlen(cmd)+1);
385 ok(rc == ERROR_SUCCESS, "setting command failed with %d\n", rc);
386 HeapFree(GetProcessHeap(), 0, cmd);
389 if (ddeexec)
391 HKEY hkey_ddeexec, hkey_application, hkey_topic, hkey_ifexec;
393 rc=RegCreateKeyExA(hkey_verb, "ddeexec", 0, NULL, 0, KEY_SET_VALUE |
394 KEY_CREATE_SUB_KEY, NULL, &hkey_ddeexec, NULL);
395 ok(rc == ERROR_SUCCESS, "\'ddeexec\' key creation failed with %d\n", rc);
396 rc=RegSetValueExA(hkey_ddeexec, NULL, 0, REG_SZ, (LPBYTE)ddeexec,
397 strlen(ddeexec)+1);
398 ok(rc == ERROR_SUCCESS, "set value failed with %d\n", rc);
400 if (application)
402 rc=RegCreateKeyExA(hkey_ddeexec, "application", 0, NULL, 0, KEY_SET_VALUE,
403 NULL, &hkey_application, NULL);
404 ok(rc == ERROR_SUCCESS, "\'application\' key creation failed with %d\n", rc);
406 rc=RegSetValueExA(hkey_application, NULL, 0, REG_SZ, (LPBYTE)application,
407 strlen(application)+1);
408 ok(rc == ERROR_SUCCESS, "set value failed with %d\n", rc);
409 CloseHandle(hkey_application);
411 if (topic)
413 rc=RegCreateKeyExA(hkey_ddeexec, "topic", 0, NULL, 0, KEY_SET_VALUE,
414 NULL, &hkey_topic, NULL);
415 ok(rc == ERROR_SUCCESS, "\'topic\' key creation failed with %d\n", rc);
416 rc=RegSetValueExA(hkey_topic, NULL, 0, REG_SZ, (LPBYTE)topic,
417 strlen(topic)+1);
418 ok(rc == ERROR_SUCCESS, "set value failed with %d\n", rc);
419 CloseHandle(hkey_topic);
421 if (ifexec)
423 rc=RegCreateKeyExA(hkey_ddeexec, "ifexec", 0, NULL, 0, KEY_SET_VALUE,
424 NULL, &hkey_ifexec, NULL);
425 ok(rc == ERROR_SUCCESS, "\'ifexec\' key creation failed with %d\n", rc);
426 rc=RegSetValueExA(hkey_ifexec, NULL, 0, REG_SZ, (LPBYTE)ifexec,
427 strlen(ifexec)+1);
428 ok(rc == ERROR_SUCCESS, "set value failed with %d\n", rc);
429 CloseHandle(hkey_ifexec);
431 CloseHandle(hkey_ddeexec);
434 CloseHandle(hkey_shell);
435 CloseHandle(hkey_verb);
436 CloseHandle(hkey_cmd);
439 static void create_test_verb(const char* extension, const char* verb,
440 int rawcmd, const char* cmdtail)
442 create_test_verb_dde(extension, verb, rawcmd, cmdtail, NULL, NULL,
443 NULL, NULL);
446 /***
448 * Functions to check that the child process was started just right
449 * (borrowed from dlls/kernel32/tests/process.c)
451 ***/
453 static const char* encodeA(const char* str)
455 static char encoded[2*1024+1];
456 char* ptr;
457 size_t len,i;
459 if (!str) return "";
460 len = strlen(str) + 1;
461 if (len >= sizeof(encoded)/2)
463 fprintf(stderr, "string is too long!\n");
464 assert(0);
466 ptr = encoded;
467 for (i = 0; i < len; i++)
468 sprintf(&ptr[i * 2], "%02x", (unsigned char)str[i]);
469 ptr[2 * len] = '\0';
470 return ptr;
473 static unsigned decode_char(char c)
475 if (c >= '0' && c <= '9') return c - '0';
476 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
477 assert(c >= 'A' && c <= 'F');
478 return c - 'A' + 10;
481 static char* decodeA(const char* str)
483 static char decoded[1024];
484 char* ptr;
485 size_t len,i;
487 len = strlen(str) / 2;
488 if (!len--) return NULL;
489 if (len >= sizeof(decoded))
491 fprintf(stderr, "string is too long!\n");
492 assert(0);
494 ptr = decoded;
495 for (i = 0; i < len; i++)
496 ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
497 ptr[len] = '\0';
498 return ptr;
501 static void childPrintf(HANDLE h, const char* fmt, ...)
503 va_list valist;
504 char buffer[1024];
505 DWORD w;
507 va_start(valist, fmt);
508 vsprintf(buffer, fmt, valist);
509 va_end(valist);
510 WriteFile(h, buffer, strlen(buffer), &w, NULL);
513 static DWORD ddeInst;
514 static HSZ hszTopic;
515 static char ddeExec[MAX_PATH], ddeApplication[MAX_PATH];
516 static BOOL post_quit_on_execute;
518 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
519 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
520 ULONG_PTR dwData1, ULONG_PTR dwData2)
522 DWORD size = 0;
524 if (winetest_debug > 2)
525 trace("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
526 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
528 switch (uType)
530 case XTYP_CONNECT:
531 if (!DdeCmpStringHandles(hsz1, hszTopic))
533 size = DdeQueryStringA(ddeInst, hsz2, ddeApplication, MAX_PATH, CP_WINANSI);
534 ok(size < MAX_PATH, "got size %d\n", size);
535 assert(size < MAX_PATH);
536 return (HDDEDATA)TRUE;
538 return (HDDEDATA)FALSE;
540 case XTYP_EXECUTE:
541 size = DdeGetData(hData, (LPBYTE)ddeExec, MAX_PATH, 0);
542 ok(size < MAX_PATH, "got size %d\n", size);
543 assert(size < MAX_PATH);
544 DdeFreeDataHandle(hData);
545 if (post_quit_on_execute)
546 PostQuitMessage(0);
547 return (HDDEDATA)DDE_FACK;
549 default:
550 return NULL;
555 * This is just to make sure the child won't run forever stuck in a GetMessage()
556 * loop when DDE fails for some reason.
558 static void CALLBACK childTimeout(HWND wnd, UINT msg, UINT_PTR timer, DWORD time)
560 trace("childTimeout called\n");
562 PostQuitMessage(0);
565 static void doChild(int argc, char** argv)
567 char *filename, longpath[MAX_PATH] = "";
568 HANDLE hFile, map;
569 int i;
570 int rc;
571 HSZ hszApplication;
572 UINT_PTR timer;
573 HANDLE dde_ready;
574 MSG msg;
575 char *shared_block;
577 filename=argv[2];
578 hFile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
579 if (hFile == INVALID_HANDLE_VALUE)
580 return;
582 /* Arguments */
583 childPrintf(hFile, "[Arguments]\r\n");
584 if (winetest_debug > 2)
586 trace("cmdlineA='%s'\n", GetCommandLineA());
587 trace("argcA=%d\n", argc);
589 childPrintf(hFile, "cmdlineA=%s\r\n", encodeA(GetCommandLineA()));
590 childPrintf(hFile, "argcA=%d\r\n", argc);
591 for (i = 0; i < argc; i++)
593 if (winetest_debug > 2)
594 trace("argvA%d='%s'\n", i, argv[i]);
595 childPrintf(hFile, "argvA%d=%s\r\n", i, encodeA(argv[i]));
597 GetModuleFileNameA(GetModuleHandleA(NULL), longpath, MAX_PATH);
598 childPrintf(hFile, "longPath=%s\r\n", encodeA(longpath));
600 map = OpenFileMappingA(FILE_MAP_READ, FALSE, "winetest_shlexec_dde_map");
601 if (map != NULL)
603 shared_block = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 4096);
604 CloseHandle(map);
605 if (shared_block[0] != '\0' || shared_block[1] != '\0')
607 post_quit_on_execute = TRUE;
608 ddeInst = 0;
609 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
610 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0);
611 ok(rc == DMLERR_NO_ERROR, "got %d\n", rc);
612 hszApplication = DdeCreateStringHandleA(ddeInst, shared_block, CP_WINANSI);
613 hszTopic = DdeCreateStringHandleA(ddeInst, shared_block + strlen(shared_block) + 1, CP_WINANSI);
614 assert(hszApplication && hszTopic);
615 assert(DdeNameService(ddeInst, hszApplication, 0, DNS_REGISTER | DNS_FILTEROFF));
617 timer = SetTimer(NULL, 0, 2500, childTimeout);
619 dde_ready = OpenEventA(EVENT_MODIFY_STATE, FALSE, "winetest_shlexec_dde_ready");
620 SetEvent(dde_ready);
621 CloseHandle(dde_ready);
623 while (GetMessageA(&msg, NULL, 0, 0))
624 DispatchMessageA(&msg);
626 Sleep(500);
627 KillTimer(NULL, timer);
628 assert(DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER));
629 assert(DdeFreeStringHandle(ddeInst, hszTopic));
630 assert(DdeFreeStringHandle(ddeInst, hszApplication));
631 assert(DdeUninitialize(ddeInst));
633 else
635 dde_ready = OpenEventA(EVENT_MODIFY_STATE, FALSE, "winetest_shlexec_dde_ready");
636 SetEvent(dde_ready);
637 CloseHandle(dde_ready);
640 UnmapViewOfFile(shared_block);
642 childPrintf(hFile, "ddeExec=%s\r\n", encodeA(ddeExec));
645 CloseHandle(hFile);
647 init_event(filename);
648 SetEvent(hEvent);
649 CloseHandle(hEvent);
652 static char* getChildString(const char* sect, const char* key)
654 char buf[1024];
655 char* ret;
657 GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), child_file);
658 if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
659 assert(!(strlen(buf) & 1));
660 ret = decodeA(buf);
661 return ret;
664 static void dump_child(void)
666 if (winetest_debug > 1)
668 char key[18];
669 char* str;
670 int i, c;
672 str=getChildString("Arguments", "cmdlineA");
673 trace("cmdlineA='%s'\n", str);
674 c=GetPrivateProfileIntA("Arguments", "argcA", -1, child_file);
675 trace("argcA=%d\n",c);
676 for (i=0;i<c;i++)
678 sprintf(key, "argvA%d", i);
679 str=getChildString("Arguments", key);
680 trace("%s='%s'\n", key, str);
685 static int StrCmpPath(const char* s1, const char* s2)
687 if (!s1 && !s2) return 0;
688 if (!s2) return 1;
689 if (!s1) return -1;
690 while (*s1)
692 if (!*s2)
694 if (*s1=='.')
695 s1++;
696 return (*s1-*s2);
698 if ((*s1=='/' || *s1=='\\') && (*s2=='/' || *s2=='\\'))
700 while (*s1=='/' || *s1=='\\')
701 s1++;
702 while (*s2=='/' || *s2=='\\')
703 s2++;
705 else if (toupper(*s1)==toupper(*s2))
707 s1++;
708 s2++;
710 else
712 return (*s1-*s2);
715 if (*s2=='.')
716 s2++;
717 if (*s2)
718 return -1;
719 return 0;
722 static void _okChildString(const char* file, int line, const char* key, const char* expected, const char* bad)
724 char* result;
725 result=getChildString("Arguments", key);
726 if (!result)
728 ok_(file, line)(FALSE, "%s expected '%s', but key not found or empty\n", key, expected);
729 return;
731 ok_(file, line)(lstrcmpiA(result, expected) == 0 ||
732 broken(lstrcmpiA(result, bad) == 0),
733 "%s expected '%s', got '%s'\n", key, expected, result);
736 static void _okChildPath(const char* file, int line, const char* key, const char* expected)
738 char* result;
739 result=getChildString("Arguments", key);
740 if (!result)
742 ok_(file, line)(FALSE, "%s expected '%s', but key not found or empty\n", key, expected);
743 return;
745 ok_(file, line)(StrCmpPath(result, expected) == 0,
746 "%s expected '%s', got '%s'\n", key, expected, result);
749 static void _okChildInt(const char* file, int line, const char* key, int expected)
751 INT result;
752 result=GetPrivateProfileIntA("Arguments", key, expected, child_file);
753 ok_(file, line)(result == expected,
754 "%s expected %d, but got %d\n", key, expected, result);
757 static void _okChildIntBroken(const char* file, int line, const char* key, int expected)
759 INT result;
760 result=GetPrivateProfileIntA("Arguments", key, expected, child_file);
761 ok_(file, line)(result == expected || broken(result != expected),
762 "%s expected %d, but got %d\n", key, expected, result);
765 #define okChildString(key, expected) _okChildString(__FILE__, __LINE__, (key), (expected), (expected))
766 #define okChildStringBroken(key, expected, broken) _okChildString(__FILE__, __LINE__, (key), (expected), (broken))
767 #define okChildPath(key, expected) _okChildPath(__FILE__, __LINE__, (key), (expected))
768 #define okChildInt(key, expected) _okChildInt(__FILE__, __LINE__, (key), (expected))
769 #define okChildIntBroken(key, expected) _okChildIntBroken(__FILE__, __LINE__, (key), (expected))
771 /***
773 * GetLongPathNameA equivalent that supports Win95 and WinNT
775 ***/
777 static DWORD get_long_path_name(const char* shortpath, char* longpath, DWORD longlen)
779 char tmplongpath[MAX_PATH];
780 const char* p;
781 DWORD sp = 0, lp = 0;
782 DWORD tmplen;
783 WIN32_FIND_DATAA wfd;
784 HANDLE goit;
786 if (!shortpath || !shortpath[0])
787 return 0;
789 if (shortpath[1] == ':')
791 tmplongpath[0] = shortpath[0];
792 tmplongpath[1] = ':';
793 lp = sp = 2;
796 while (shortpath[sp])
798 /* check for path delimiters and reproduce them */
799 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
801 if (!lp || tmplongpath[lp-1] != '\\')
803 /* strip double "\\" */
804 tmplongpath[lp++] = '\\';
806 tmplongpath[lp] = 0; /* terminate string */
807 sp++;
808 continue;
811 p = shortpath + sp;
812 if (sp == 0 && p[0] == '.' && (p[1] == '/' || p[1] == '\\'))
814 tmplongpath[lp++] = *p++;
815 tmplongpath[lp++] = *p++;
817 for (; *p && *p != '/' && *p != '\\'; p++);
818 tmplen = p - (shortpath + sp);
819 lstrcpynA(tmplongpath + lp, shortpath + sp, tmplen + 1);
820 /* Check if the file exists and use the existing file name */
821 goit = FindFirstFileA(tmplongpath, &wfd);
822 if (goit == INVALID_HANDLE_VALUE)
823 return 0;
824 FindClose(goit);
825 strcpy(tmplongpath + lp, wfd.cFileName);
826 lp += strlen(tmplongpath + lp);
827 sp += tmplen;
829 tmplen = strlen(shortpath) - 1;
830 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
831 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
832 tmplongpath[lp++] = shortpath[tmplen];
833 tmplongpath[lp] = 0;
835 tmplen = strlen(tmplongpath) + 1;
836 if (tmplen <= longlen)
838 strcpy(longpath, tmplongpath);
839 tmplen--; /* length without 0 */
842 return tmplen;
845 /***
847 * Tests
849 ***/
851 static const char* testfiles[]=
853 "%s\\test file.shlexec",
854 "%s\\%%nasty%% $file.shlexec",
855 "%s\\test file.noassoc",
856 "%s\\test file.noassoc.shlexec",
857 "%s\\test file.shlexec.noassoc",
858 "%s\\test_shortcut_shlexec.lnk",
859 "%s\\test_shortcut_exe.lnk",
860 "%s\\test file.shl",
861 "%s\\test file.shlfoo",
862 "%s\\test file.sfe",
863 "%s\\masked file.shlexec",
864 "%s\\masked",
865 "%s\\test file.sde",
866 "%s\\test file.exe",
867 "%s\\test2.exe",
868 "%s\\simple.shlexec",
869 "%s\\drawback_file.noassoc",
870 "%s\\drawback_file.noassoc foo.shlexec",
871 "%s\\drawback_nonexist.noassoc foo.shlexec",
872 NULL
875 typedef struct
877 const char* verb;
878 const char* basename;
879 int todo;
880 INT_PTR rc;
881 } filename_tests_t;
883 static filename_tests_t filename_tests[]=
885 /* Test bad / nonexistent filenames */
886 {NULL, "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
887 {NULL, "%s\\nonexistent.noassoc", 0x0, SE_ERR_FNF},
889 /* Standard tests */
890 {NULL, "%s\\test file.shlexec", 0x0, 33},
891 {NULL, "%s\\test file.shlexec.", 0x0, 33},
892 {NULL, "%s\\%%nasty%% $file.shlexec", 0x0, 33},
893 {NULL, "%s/test file.shlexec", 0x0, 33},
895 /* Test filenames with no association */
896 {NULL, "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
898 /* Test double extensions */
899 {NULL, "%s\\test file.noassoc.shlexec", 0x0, 33},
900 {NULL, "%s\\test file.shlexec.noassoc", 0x0, SE_ERR_NOASSOC},
902 /* Test alternate verbs */
903 {"LowerL", "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
904 {"LowerL", "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
906 {"QuotedLowerL", "%s\\test file.shlexec", 0x0, 33},
907 {"QuotedUpperL", "%s\\test file.shlexec", 0x0, 33},
909 /* Test file masked due to space */
910 {NULL, "%s\\masked file.shlexec", 0x0, 33},
911 /* Test if quoting prevents the masking */
912 {NULL, "%s\\masked file.shlexec", 0x40, 33},
913 /* Test with incorrect quote */
914 {NULL, "\"%s\\masked file.shlexec", 0x0, SE_ERR_FNF},
916 {NULL, NULL, 0}
919 static filename_tests_t noquotes_tests[]=
921 /* Test unquoted '%1' thingies */
922 {"NoQuotes", "%s\\test file.shlexec", 0xa, 33},
923 {"LowerL", "%s\\test file.shlexec", 0xa, 33},
924 {"UpperL", "%s\\test file.shlexec", 0xa, 33},
926 {NULL, NULL, 0}
929 static void test_lpFile_parsed(void)
931 char fileA[MAX_PATH];
932 INT_PTR rc;
934 if (skip_shlexec_tests)
936 skip("No filename parsing tests due to lack of .shlexec association\n");
937 return;
940 /* existing "drawback_file.noassoc" prevents finding "drawback_file.noassoc foo.shlexec" on wine */
941 sprintf(fileA, "%s\\drawback_file.noassoc foo.shlexec", tmpdir);
942 rc=shell_execute(NULL, fileA, NULL, NULL);
943 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
945 /* if quoted, existing "drawback_file.noassoc" not prevents finding "drawback_file.noassoc foo.shlexec" on wine */
946 sprintf(fileA, "\"%s\\drawback_file.noassoc foo.shlexec\"", tmpdir);
947 rc=shell_execute(NULL, fileA, NULL, NULL);
948 ok(rc > 32 || broken(rc == SE_ERR_FNF) /* Win95/NT4 */,
949 "%s failed: rc=%lu\n", shell_call, rc);
951 /* error should be SE_ERR_FNF, not SE_ERR_NOASSOC */
952 sprintf(fileA, "\"%s\\drawback_file.noassoc\" foo.shlexec", tmpdir);
953 rc=shell_execute(NULL, fileA, NULL, NULL);
954 ok(rc == SE_ERR_FNF, "%s succeeded: rc=%lu\n", shell_call, rc);
956 /* ""command"" not works on wine (and real win9x and w2k) */
957 sprintf(fileA, "\"\"%s\\simple.shlexec\"\"", tmpdir);
958 rc=shell_execute(NULL, fileA, NULL, NULL);
959 todo_wine ok(rc > 32 || broken(rc == SE_ERR_FNF) /* Win9x/2000 */,
960 "%s failed: rc=%lu\n", shell_call, rc);
962 /* nonexisting "drawback_nonexist.noassoc" not prevents finding "drawback_nonexist.noassoc foo.shlexec" on wine */
963 sprintf(fileA, "%s\\drawback_nonexist.noassoc foo.shlexec", tmpdir);
964 rc=shell_execute(NULL, fileA, NULL, NULL);
965 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
967 /* is SEE_MASK_DOENVSUBST default flag? Should only be when XP emulates 9x (XP bug or real 95 or ME behavior ?) */
968 rc=shell_execute(NULL, "%TMPDIR%\\simple.shlexec", NULL, NULL);
969 todo_wine ok(rc == SE_ERR_FNF, "%s succeeded: rc=%lu\n", shell_call, rc);
971 /* quoted */
972 rc=shell_execute(NULL, "\"%TMPDIR%\\simple.shlexec\"", NULL, NULL);
973 todo_wine ok(rc == SE_ERR_FNF, "%s succeeded: rc=%lu\n", shell_call, rc);
975 /* test SEE_MASK_DOENVSUBST works */
976 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI,
977 NULL, "%TMPDIR%\\simple.shlexec", NULL, NULL, NULL);
978 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
980 /* quoted lpFile does not work on real win95 and nt4 */
981 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI,
982 NULL, "\"%TMPDIR%\\simple.shlexec\"", NULL, NULL, NULL);
983 ok(rc > 32 || broken(rc == SE_ERR_FNF) /* Win95/NT4 */,
984 "%s failed: rc=%lu\n", shell_call, rc);
987 typedef struct
989 const char* cmd;
990 const char* args[11];
991 int todo;
992 } cmdline_tests_t;
994 static const cmdline_tests_t cmdline_tests[] =
996 {"exe",
997 {"exe", NULL}, 0},
999 {"exe arg1 arg2 \"arg three\" 'four five` six\\ $even)",
1000 {"exe", "arg1", "arg2", "arg three", "'four", "five`", "six\\", "$even)", NULL}, 0},
1002 {"exe arg=1 arg-2 three\tfour\rfour\nfour ",
1003 {"exe", "arg=1", "arg-2", "three", "four\rfour\nfour", NULL}, 0},
1005 {"exe arg\"one\" \"second\"arg thirdarg ",
1006 {"exe", "argone", "secondarg", "thirdarg", NULL}, 0},
1008 /* Don't lose unclosed quoted arguments */
1009 {"exe arg1 \"unclosed",
1010 {"exe", "arg1", "unclosed", NULL}, 0},
1012 {"exe arg1 \"",
1013 {"exe", "arg1", "", NULL}, 0},
1015 /* cmd's metacharacters have no special meaning */
1016 {"exe \"one^\" \"arg\"&two three|four",
1017 {"exe", "one^", "arg&two", "three|four", NULL}, 0},
1019 /* Environment variables are not interpreted either */
1020 {"exe %TMPDIR% %2",
1021 {"exe", "%TMPDIR%", "%2", NULL}, 0},
1023 /* If not followed by a quote, backslashes go through as is */
1024 {"exe o\\ne t\\\\wo t\\\\\\ree f\\\\\\\\our ",
1025 {"exe", "o\\ne", "t\\\\wo", "t\\\\\\ree", "f\\\\\\\\our", NULL}, 0},
1027 {"exe \"o\\ne\" \"t\\\\wo\" \"t\\\\\\ree\" \"f\\\\\\\\our\" ",
1028 {"exe", "o\\ne", "t\\\\wo", "t\\\\\\ree", "f\\\\\\\\our", NULL}, 0},
1030 /* When followed by a quote their number is halved and the remainder
1031 * escapes the quote
1033 {"exe \\\"one \\\\\"two\" \\\\\\\"three \\\\\\\\\"four\" end",
1034 {"exe", "\"one", "\\two", "\\\"three", "\\\\four", "end", NULL}, 0},
1036 {"exe \"one\\\" still\" \"two\\\\\" \"three\\\\\\\" still\" \"four\\\\\\\\\" end",
1037 {"exe", "one\" still", "two\\", "three\\\" still", "four\\\\", "end", NULL}, 0},
1039 /* One can put a quote in an unquoted string by tripling it, that is in
1040 * effect quoting it like so """ -> ". The general rule is as follows:
1041 * 3n quotes -> n quotes
1042 * 3n+1 quotes -> n quotes plus start of a quoted string
1043 * 3n+2 quotes -> n quotes (plus an empty string from the remaining pair)
1044 * Nicely, when n is 0 we get the standard rules back.
1046 {"exe two\"\"quotes next",
1047 {"exe", "twoquotes", "next", NULL}, 0},
1049 {"exe three\"\"\"quotes next",
1050 {"exe", "three\"quotes", "next", NULL}, 0},
1052 {"exe four\"\"\"\" quotes\" next 4%3=1",
1053 {"exe", "four\" quotes", "next", "4%3=1", NULL}, 0},
1055 {"exe five\"\"\"\"\"quotes next",
1056 {"exe", "five\"quotes", "next", NULL}, 0},
1058 {"exe six\"\"\"\"\"\"quotes next",
1059 {"exe", "six\"\"quotes", "next", NULL}, 0},
1061 {"exe seven\"\"\"\"\"\"\" quotes\" next 7%3=1",
1062 {"exe", "seven\"\" quotes", "next", "7%3=1", NULL}, 0},
1064 {"exe twelve\"\"\"\"\"\"\"\"\"\"\"\"quotes next",
1065 {"exe", "twelve\"\"\"\"quotes", "next", NULL}, 0},
1067 {"exe thirteen\"\"\"\"\"\"\"\"\"\"\"\"\" quotes\" next 13%3=1",
1068 {"exe", "thirteen\"\"\"\" quotes", "next", "13%3=1", NULL}, 0},
1070 /* Inside a quoted string the opening quote is added to the set of
1071 * consecutive quotes to get the effective quotes count. This gives:
1072 * 1+3n quotes -> n quotes
1073 * 1+3n+1 quotes -> n quotes plus closes the quoted string
1074 * 1+3n+2 quotes -> n+1 quotes plus closes the quoted string
1076 {"exe \"two\"\"quotes next",
1077 {"exe", "two\"quotes", "next", NULL}, 0},
1079 {"exe \"two\"\" next",
1080 {"exe", "two\"", "next", NULL}, 0},
1082 {"exe \"three\"\"\" quotes\" next 4%3=1",
1083 {"exe", "three\" quotes", "next", "4%3=1", NULL}, 0},
1085 {"exe \"four\"\"\"\"quotes next",
1086 {"exe", "four\"quotes", "next", NULL}, 0},
1088 {"exe \"five\"\"\"\"\"quotes next",
1089 {"exe", "five\"\"quotes", "next", NULL}, 0},
1091 {"exe \"six\"\"\"\"\"\" quotes\" next 7%3=1",
1092 {"exe", "six\"\" quotes", "next", "7%3=1", NULL}, 0},
1094 {"exe \"eleven\"\"\"\"\"\"\"\"\"\"\"quotes next",
1095 {"exe", "eleven\"\"\"\"quotes", "next", NULL}, 0},
1097 {"exe \"twelve\"\"\"\"\"\"\"\"\"\"\"\" quotes\" next 13%3=1",
1098 {"exe", "twelve\"\"\"\" quotes", "next", "13%3=1", NULL}, 0},
1100 /* Escaped consecutive quotes are fun */
1101 {"exe \"the crazy \\\\\"\"\"\\\\\" quotes",
1102 {"exe", "the crazy \\\"\\", "quotes", NULL}, 0},
1104 /* The executable path has its own rules!!!
1105 * - Backslashes have no special meaning.
1106 * - If the first character is a quote, then the second quote ends the
1107 * executable path.
1108 * - The previous rule holds even if the next character is not a space!
1109 * - If the first character is not a quote, then quotes have no special
1110 * meaning either and the executable path stops at the first space.
1111 * - The consecutive quotes rules don't apply either.
1112 * - Even if there is no space between the executable path and the first
1113 * argument, the latter is parsed using the regular rules.
1115 {"exe\"file\"path arg1",
1116 {"exe\"file\"path", "arg1", NULL}, 0},
1118 {"exe\"file\"path\targ1",
1119 {"exe\"file\"path", "arg1", NULL}, 0},
1121 {"exe\"path\\ arg1",
1122 {"exe\"path\\", "arg1", NULL}, 0},
1124 {"\\\"exe \"arg one\"",
1125 {"\\\"exe", "arg one", NULL}, 0},
1127 {"\"spaced exe\" \"next arg\"",
1128 {"spaced exe", "next arg", NULL}, 0},
1130 {"\"spaced exe\"\t\"next arg\"",
1131 {"spaced exe", "next arg", NULL}, 0},
1133 {"\"exe\"arg\" one\" argtwo",
1134 {"exe", "arg one", "argtwo", NULL}, 0},
1136 {"\"spaced exe\\\"arg1 arg2",
1137 {"spaced exe\\", "arg1", "arg2", NULL}, 0},
1139 {"\"two\"\" arg1 ",
1140 {"two", " arg1 ", NULL}, 0},
1142 {"\"three\"\"\" arg2",
1143 {"three", "", "arg2", NULL}, 0},
1145 {"\"four\"\"\"\"arg1",
1146 {"four", "\"arg1", NULL}, 0},
1148 /* If the first character is a space then the executable path is empty */
1149 {" \"arg\"one argtwo",
1150 {"", "argone", "argtwo", NULL}, 0},
1152 {NULL, {NULL}, 0}
1155 static BOOL test_one_cmdline(const cmdline_tests_t* test)
1157 WCHAR cmdW[MAX_PATH], argW[MAX_PATH];
1158 LPWSTR *cl2a;
1159 int cl2a_count;
1160 LPWSTR *argsW;
1161 int i, count;
1163 /* trace("----- cmd='%s'\n", test->cmd); */
1164 MultiByteToWideChar(CP_ACP, 0, test->cmd, -1, cmdW, sizeof(cmdW)/sizeof(*cmdW));
1165 argsW = cl2a = CommandLineToArgvW(cmdW, &cl2a_count);
1166 if (argsW == NULL && cl2a_count == -1)
1168 win_skip("CommandLineToArgvW not implemented, skipping\n");
1169 return FALSE;
1171 ok(!argsW[cl2a_count] || broken(argsW[cl2a_count] != NULL) /* before Vista */,
1172 "expected NULL-terminated list of commandline arguments\n");
1174 count = 0;
1175 while (test->args[count])
1176 count++;
1177 if ((test->todo & 0x1) == 0)
1178 ok(cl2a_count == count, "%s: expected %d arguments, but got %d\n", test->cmd, count, cl2a_count);
1179 else todo_wine
1180 ok(cl2a_count == count, "%s: expected %d arguments, but got %d\n", test->cmd, count, cl2a_count);
1182 for (i = 0; i < cl2a_count; i++)
1184 if (i < count)
1186 MultiByteToWideChar(CP_ACP, 0, test->args[i], -1, argW, sizeof(argW)/sizeof(*argW));
1187 if ((test->todo & (1 << (i+4))) == 0)
1188 ok(!lstrcmpW(*argsW, argW), "%s: arg[%d] expected %s but got %s\n", test->cmd, i, wine_dbgstr_w(argW), wine_dbgstr_w(*argsW));
1189 else todo_wine
1190 ok(!lstrcmpW(*argsW, argW), "%s: arg[%d] expected %s but got %s\n", test->cmd, i, wine_dbgstr_w(argW), wine_dbgstr_w(*argsW));
1192 else if ((test->todo & 0x1) == 0)
1193 ok(0, "%s: got extra arg[%d]=%s\n", test->cmd, i, wine_dbgstr_w(*argsW));
1194 else todo_wine
1195 ok(0, "%s: got extra arg[%d]=%s\n", test->cmd, i, wine_dbgstr_w(*argsW));
1196 argsW++;
1198 LocalFree(cl2a);
1199 return TRUE;
1202 static void test_commandline2argv(void)
1204 static const WCHAR exeW[] = {'e','x','e',0};
1205 const cmdline_tests_t* test;
1206 WCHAR strW[MAX_PATH];
1207 LPWSTR *args;
1208 int numargs;
1209 DWORD le;
1211 test = cmdline_tests;
1212 while (test->cmd)
1214 if (!test_one_cmdline(test))
1215 return;
1216 test++;
1219 SetLastError(0xdeadbeef);
1220 args = CommandLineToArgvW(exeW, NULL);
1221 le = GetLastError();
1222 ok(args == NULL && le == ERROR_INVALID_PARAMETER, "expected NULL with ERROR_INVALID_PARAMETER got %p with %u\n", args, le);
1224 SetLastError(0xdeadbeef);
1225 args = CommandLineToArgvW(NULL, NULL);
1226 le = GetLastError();
1227 ok(args == NULL && le == ERROR_INVALID_PARAMETER, "expected NULL with ERROR_INVALID_PARAMETER got %p with %u\n", args, le);
1229 *strW = 0;
1230 args = CommandLineToArgvW(strW, &numargs);
1231 ok(numargs == 1 || broken(numargs > 1), "expected 1 args, got %d\n", numargs);
1232 ok(!args || (!args[numargs] || broken(args[numargs] != NULL) /* before Vista */),
1233 "expected NULL-terminated list of commandline arguments\n");
1234 if (numargs == 1)
1236 GetModuleFileNameW(NULL, strW, sizeof(strW)/sizeof(*strW));
1237 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));
1239 if (args) LocalFree(args);
1242 /* The goal here is to analyze how ShellExecute() builds the command that
1243 * will be run. The tricky part is that there are three transformation
1244 * steps between the 'parameters' string we pass to ShellExecute() and the
1245 * argument list we observe in the child process:
1246 * - The parsing of 'parameters' string into individual arguments. The tests
1247 * show this is done differently from both CreateProcess() and
1248 * CommandLineToArgv()!
1249 * - The way the command 'formatting directives' such as %1, %2, etc are
1250 * handled.
1251 * - And the way the resulting command line is then parsed to yield the
1252 * argument list we check.
1254 typedef struct
1256 const char* verb;
1257 const char* params;
1258 int todo;
1259 cmdline_tests_t cmd;
1260 cmdline_tests_t broken;
1261 } argify_tests_t;
1263 static const argify_tests_t argify_tests[] =
1265 /* Start with three simple parameters. Notice that one can reorder and
1266 * duplicate the parameters. Also notice how %* take the raw input
1267 * parameters string, including the trailing spaces, no matter what
1268 * arguments have already been used.
1270 {"Params232S", "p2 p3 p4 ", 0xc2,
1271 {" p2 p3 \"p2\" \"p2 p3 p4 \"",
1272 {"", "p2", "p3", "p2", "p2 p3 p4 ", NULL}, 0}},
1274 /* Unquoted argument references like %2 don't automatically quote their
1275 * argument. Similarly, when they are quoted they don't escape the quotes
1276 * that their argument may contain.
1278 {"Params232S", "\"p two\" p3 p4 ", 0x3f3,
1279 {" p two p3 \"p two\" \"\"p two\" p3 p4 \"",
1280 {"", "p", "two", "p3", "p two", "p", "two p3 p4 ", NULL}, 0}},
1282 /* Only single digits are supported so only %1 to %9. Shown here with %20
1283 * because %10 is a pain.
1285 {"Params20", "p", 0,
1286 {" \"p0\"",
1287 {"", "p0", NULL}, 0}},
1289 /* Only (double-)quotes have a special meaning. */
1290 {"Params23456", "'p2 p3` p4\\ $even", 0x40,
1291 {" \"'p2\" \"p3`\" \"p4\\\" \"$even\" \"\"",
1292 {"", "'p2", "p3`", "p4\" $even \"", NULL}, 0}},
1294 {"Params23456", "p=2 p-3 p4\tp4\rp4\np4", 0x1c2,
1295 {" \"p=2\" \"p-3\" \"p4\tp4\rp4\np4\" \"\" \"\"",
1296 {"", "p=2", "p-3", "p4\tp4\rp4\np4", "", "", NULL}, 0}},
1298 /* In unquoted strings, quotes are treated are a parameter separator just
1299 * like spaces! However they can be doubled to get a literal quote.
1300 * Specifically:
1301 * 2n quotes -> n quotes
1302 * 2n+1 quotes -> n quotes and a parameter separator
1304 {"Params23456789", "one\"quote \"p four\" one\"quote p7", 0xff3,
1305 {" \"one\" \"quote\" \"p four\" \"one\" \"quote\" \"p7\" \"\" \"\"",
1306 {"", "one", "quote", "p four", "one", "quote", "p7", "", "", NULL}, 0}},
1308 {"Params23456789", "two\"\"quotes \"p three\" two\"\"quotes p5", 0xf2,
1309 {" \"two\"quotes\" \"p three\" \"two\"quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1310 {"", "twoquotes p", "three twoquotes", "p5", "", "", "", "", NULL}, 0}},
1312 {"Params23456789", "three\"\"\"quotes \"p four\" three\"\"\"quotes p6", 0xff3,
1313 {" \"three\"\" \"quotes\" \"p four\" \"three\"\" \"quotes\" \"p6\" \"\" \"\"",
1314 {"", "three\"", "quotes", "p four", "three\"", "quotes", "p6", "", "", NULL}, 0}},
1316 {"Params23456789", "four\"\"\"\"quotes \"p three\" four\"\"\"\"quotes p5", 0xf3,
1317 {" \"four\"\"quotes\" \"p three\" \"four\"\"quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1318 {"", "four\"quotes p", "three fourquotes p5 \"", "", "", "", NULL}, 0}},
1320 /* Quoted strings cannot be continued by tacking on a non space character
1321 * either.
1323 {"Params23456", "\"p two\"p3 \"p four\"p5 p6", 0x1f3,
1324 {" \"p two\" \"p3\" \"p four\" \"p5\" \"p6\"",
1325 {"", "p two", "p3", "p four", "p5", "p6", NULL}, 0}},
1327 /* In quoted strings, the quotes are halved and an odd number closes the
1328 * string. Specifically:
1329 * 2n quotes -> n quotes
1330 * 2n+1 quotes -> n quotes and closes the string and hence the parameter
1332 {"Params23456789", "\"one q\"uote \"p four\" \"one q\"uote p7", 0xff3,
1333 {" \"one q\" \"uote\" \"p four\" \"one q\" \"uote\" \"p7\" \"\" \"\"",
1334 {"", "one q", "uote", "p four", "one q", "uote", "p7", "", "", NULL}, 0}},
1336 {"Params23456789", "\"two \"\" quotes\" \"p three\" \"two \"\" quotes\" p5", 0x1ff3,
1337 {" \"two \" quotes\" \"p three\" \"two \" quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1338 {"", "two ", "quotes p", "three two", " quotes", "p5", "", "", "", "", NULL}, 0}},
1340 {"Params23456789", "\"three q\"\"\"uotes \"p four\" \"three q\"\"\"uotes p7", 0xff3,
1341 {" \"three q\"\" \"uotes\" \"p four\" \"three q\"\" \"uotes\" \"p7\" \"\" \"\"",
1342 {"", "three q\"", "uotes", "p four", "three q\"", "uotes", "p7", "", "", NULL}, 0}},
1344 {"Params23456789", "\"four \"\"\"\" quotes\" \"p three\" \"four \"\"\"\" quotes\" p5", 0xff3,
1345 {" \"four \"\" quotes\" \"p three\" \"four \"\" quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1346 {"", "four \"", "quotes p", "three four", "", "quotes p5 \"", "", "", "", NULL}, 0}},
1348 /* The quoted string rules also apply to consecutive quotes at the start
1349 * of a parameter but don't count the opening quote!
1351 {"Params23456789", "\"\"twoquotes \"p four\" \"\"twoquotes p7", 0xbf3,
1352 {" \"\" \"twoquotes\" \"p four\" \"\" \"twoquotes\" \"p7\" \"\" \"\"",
1353 {"", "", "twoquotes", "p four", "", "twoquotes", "p7", "", "", NULL}, 0}},
1355 {"Params23456789", "\"\"\"three quotes\" \"p three\" \"\"\"three quotes\" p5", 0x6f3,
1356 {" \"\"three quotes\" \"p three\" \"\"three quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1357 {"", "three", "quotes p", "three \"three", "quotes p5 \"", "", "", "", NULL}, 0}},
1359 {"Params23456789", "\"\"\"\"fourquotes \"p four\" \"\"\"\"fourquotes p7", 0xbf3,
1360 {" \"\"\" \"fourquotes\" \"p four\" \"\"\" \"fourquotes\" \"p7\" \"\" \"\"",
1361 {"", "\"", "fourquotes", "p four", "\"", "fourquotes", "p7", "", "", NULL}, 0}},
1363 /* An unclosed quoted string gets lost! */
1364 {"Params23456", "p2 \"p3\" \"p4 is lost", 0x1c3,
1365 {" \"p2\" \"p3\" \"\" \"\" \"\"",
1366 {"", "p2", "p3", "", "", "", NULL}, 0},
1367 {" \"p2\" \"p3\" \"p3\" \"\" \"\"",
1368 {"", "p2", "p3", "p3", "", "", NULL}, 0}},
1370 /* Backslashes have no special meaning even when preceding quotes. All
1371 * they do is start an unquoted string.
1373 {"Params23456", "\\\"p\\three \"pfour\\\" pfive", 0x73,
1374 {" \"\\\" \"p\\three\" \"pfour\\\" \"pfive\" \"\"",
1375 {"", "\" p\\three pfour\"", "pfive", "", NULL}, 0}},
1377 /* Environment variables are left untouched. */
1378 {"Params23456", "%TMPDIR% %t %c", 0,
1379 {" \"%TMPDIR%\" \"%t\" \"%c\" \"\" \"\"",
1380 {"", "%TMPDIR%", "%t", "%c", "", "", NULL}, 0}},
1382 /* %~2 is equivalent to %*. However %~3 and higher include the spaces
1383 * before the parameter!
1384 * (but not the previous parameter's closing quote fortunately)
1386 {"Params2345Etc", "p2 p3 \"p4\" p5 p6 ", 0x3f3,
1387 {" ~2=\"p2 p3 \"p4\" p5 p6 \" ~3=\" p3 \"p4\" p5 p6 \" ~4=\" \"p4\" p5 p6 \" ~5= p5 p6 ",
1388 {"", "~2=p2 p3 p4 p5 p6 ", "~3= p3 p4 p5 p6 ", "~4= p4 p5 p6 ", "~5=", "p5", "p6", NULL}, 0}},
1390 /* %~n works even if there is no nth parameter. */
1391 {"Params9Etc", "p2 p3 p4 p5 p6 p7 p8 ", 0x12,
1392 {" ~9=\" \"",
1393 {"", "~9= ", NULL}, 0}},
1395 {"Params9Etc", "p2 p3 p4 p5 p6 p7 ", 0x12,
1396 {" ~9=\"\"",
1397 {"", "~9=", NULL}, 0}},
1399 /* The %~n directives also transmit the tenth parameter and beyond. */
1400 {"Params9Etc", "p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 and beyond!", 0x12,
1401 {" ~9=\" p9 p10 p11 and beyond!\"",
1402 {"", "~9= p9 p10 p11 and beyond!", NULL}, 0}},
1404 /* Bad formatting directives lose their % sign, except those followed by
1405 * a tilde! Environment variables are not expanded but lose their % sign.
1407 {"ParamsBad", "p2 p3 p4 p5", 0x12,
1408 {" \"% - %~ %~0 %~1 %~a %~* a b c TMPDIR\"",
1409 {"", "% - %~ %~0 %~1 %~a %~* a b c TMPDIR", NULL}, 0}},
1411 {NULL, NULL, 0, {NULL, {NULL}, 0}}
1414 static void test_argify(void)
1416 BOOL has_cl2a = TRUE;
1417 char fileA[MAX_PATH], params[2*MAX_PATH+12];
1418 INT_PTR rc;
1419 const argify_tests_t* test;
1420 const cmdline_tests_t *bad;
1421 const char* cmd;
1422 unsigned i, count;
1424 if (skip_shlexec_tests)
1426 skip("No argify tests due to lack of .shlexec association\n");
1427 return;
1430 create_test_verb(".shlexec", "Params232S", 0, "Params232S %2 %3 \"%2\" \"%*\"");
1431 create_test_verb(".shlexec", "Params23456", 0, "Params23456 \"%2\" \"%3\" \"%4\" \"%5\" \"%6\"");
1432 create_test_verb(".shlexec", "Params23456789", 0, "Params23456789 \"%2\" \"%3\" \"%4\" \"%5\" \"%6\" \"%7\" \"%8\" \"%9\"");
1433 create_test_verb(".shlexec", "Params2345Etc", 0, "Params2345Etc ~2=\"%~2\" ~3=\"%~3\" ~4=\"%~4\" ~5=%~5");
1434 create_test_verb(".shlexec", "Params9Etc", 0, "Params9Etc ~9=\"%~9\"");
1435 create_test_verb(".shlexec", "Params20", 0, "Params20 \"%20\"");
1436 create_test_verb(".shlexec", "ParamsBad", 0, "ParamsBad \"%% %- %~ %~0 %~1 %~a %~* %a %b %c %TMPDIR%\"");
1438 sprintf(fileA, "%s\\test file.shlexec", tmpdir);
1440 test = argify_tests;
1441 while (test->params)
1443 bad = test->broken.cmd ? &test->broken : &test->cmd;
1445 /* trace("***** verb='%s' params='%s'\n", test->verb, test->params); */
1446 rc = shell_execute_ex(SEE_MASK_DOENVSUBST, test->verb, fileA, test->params, NULL, NULL);
1447 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
1449 count = 0;
1450 while (test->cmd.args[count])
1451 count++;
1452 if ((test->todo & 0x1) == 0)
1453 /* +4 for the shlexec arguments, -1 because of the added ""
1454 * argument for the CommandLineToArgvW() tests.
1456 okChildInt("argcA", 4 + count - 1);
1457 else todo_wine
1458 okChildInt("argcA", 4 + count - 1);
1460 cmd = getChildString("Arguments", "cmdlineA");
1461 /* Our commands are such that the verb immediately precedes the
1462 * part we are interested in.
1464 if (cmd) cmd = strstr(cmd, test->verb);
1465 if (cmd) cmd += strlen(test->verb);
1466 if (!cmd) cmd = "(null)";
1467 if ((test->todo & 0x2) == 0)
1468 ok(!strcmp(cmd, test->cmd.cmd) || broken(!strcmp(cmd, bad->cmd)),
1469 "%s: the cmdline is '%s' instead of '%s'\n", shell_call, cmd, test->cmd.cmd);
1470 else todo_wine
1471 ok(!strcmp(cmd, test->cmd.cmd) || broken(!strcmp(cmd, bad->cmd)),
1472 "%s: the cmdline is '%s' instead of '%s'\n", shell_call, cmd, test->cmd.cmd);
1474 for (i = 0; i < count - 1; i++)
1476 char argname[18];
1477 sprintf(argname, "argvA%d", 4 + i);
1478 if ((test->todo & (1 << (i+4))) == 0)
1479 okChildStringBroken(argname, test->cmd.args[i+1], bad->args[i+1]);
1480 else todo_wine
1481 okChildStringBroken(argname, test->cmd.args[i+1], bad->args[i+1]);
1484 if (has_cl2a)
1485 has_cl2a = test_one_cmdline(&(test->cmd));
1486 test++;
1489 /* Test with a long parameter */
1490 for (rc = 0; rc < MAX_PATH; rc++)
1491 fileA[rc] = 'a' + rc % 26;
1492 fileA[MAX_PATH-1] = '\0';
1493 sprintf(params, "shlexec \"%s\" %s", child_file, fileA);
1495 /* We need NOZONECHECKS on Win2003 to block a dialog */
1496 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params, NULL, NULL);
1497 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
1498 okChildInt("argcA", 4);
1499 okChildString("argvA3", fileA);
1502 static void test_filename(void)
1504 char filename[MAX_PATH];
1505 const filename_tests_t* test;
1506 char* c;
1507 INT_PTR rc;
1509 if (skip_shlexec_tests)
1511 skip("No ShellExecute/filename tests due to lack of .shlexec association\n");
1512 return;
1515 test=filename_tests;
1516 while (test->basename)
1518 BOOL quotedfile = FALSE;
1520 if (skip_noassoc_tests && test->rc == SE_ERR_NOASSOC)
1522 win_skip("Skipping shellexecute of file with unassociated extension\n");
1523 test++;
1524 continue;
1527 sprintf(filename, test->basename, tmpdir);
1528 if (strchr(filename, '/'))
1530 c=filename;
1531 while (*c)
1533 if (*c=='\\')
1534 *c='/';
1535 c++;
1538 if ((test->todo & 0x40)==0)
1540 rc=shell_execute(test->verb, filename, NULL, NULL);
1542 else
1544 char quoted[MAX_PATH + 2];
1546 quotedfile = TRUE;
1547 sprintf(quoted, "\"%s\"", filename);
1548 rc=shell_execute(test->verb, quoted, NULL, NULL);
1550 if (rc > 32)
1551 rc=33;
1552 ok(rc==test->rc ||
1553 broken(quotedfile && rc == SE_ERR_FNF), /* NT4 */
1554 "%s failed: rc=%ld err=%u\n", shell_call,
1555 rc, GetLastError());
1556 if (rc == 33)
1558 const char* verb;
1559 if ((test->todo & 0x2)==0)
1561 okChildInt("argcA", 5);
1563 else todo_wine
1565 okChildInt("argcA", 5);
1567 verb=(test->verb ? test->verb : "Open");
1568 if ((test->todo & 0x4)==0)
1570 okChildString("argvA3", verb);
1572 else todo_wine
1574 okChildString("argvA3", verb);
1576 if ((test->todo & 0x8)==0)
1578 okChildPath("argvA4", filename);
1580 else todo_wine
1582 okChildPath("argvA4", filename);
1585 test++;
1588 test=noquotes_tests;
1589 while (test->basename)
1591 sprintf(filename, test->basename, tmpdir);
1592 rc=shell_execute(test->verb, filename, NULL, NULL);
1593 if (rc > 32)
1594 rc=33;
1595 if ((test->todo & 0x1)==0)
1597 ok(rc==test->rc, "%s failed: rc=%ld err=%u\n", shell_call,
1598 rc, GetLastError());
1600 else todo_wine
1602 ok(rc==test->rc, "%s failed: rc=%ld err=%u\n", shell_call,
1603 rc, GetLastError());
1605 if (rc==0)
1607 int count;
1608 const char* verb;
1609 char* str;
1611 verb=(test->verb ? test->verb : "Open");
1612 if ((test->todo & 0x4)==0)
1614 okChildString("argvA3", verb);
1616 else todo_wine
1618 okChildString("argvA3", verb);
1621 count=4;
1622 str=filename;
1623 while (1)
1625 char attrib[18];
1626 char* space;
1627 space=strchr(str, ' ');
1628 if (space)
1629 *space='\0';
1630 sprintf(attrib, "argvA%d", count);
1631 if ((test->todo & 0x8)==0)
1633 okChildPath(attrib, str);
1635 else todo_wine
1637 okChildPath(attrib, str);
1639 count++;
1640 if (!space)
1641 break;
1642 str=space+1;
1644 if ((test->todo & 0x2)==0)
1646 okChildInt("argcA", count);
1648 else todo_wine
1650 okChildInt("argcA", count);
1653 test++;
1656 if (dllver.dwMajorVersion != 0)
1658 /* The more recent versions of shell32.dll accept quoted filenames
1659 * while older ones (e.g. 4.00) don't. Still we want to test this
1660 * because IE 6 depends on the new behavior.
1661 * One day we may need to check the exact version of the dll but for
1662 * now making sure DllGetVersion() is present is sufficient.
1664 sprintf(filename, "\"%s\\test file.shlexec\"", tmpdir);
1665 rc=shell_execute(NULL, filename, NULL, NULL);
1666 ok(rc > 32, "%s failed: rc=%ld err=%u\n", shell_call, rc,
1667 GetLastError());
1668 okChildInt("argcA", 5);
1669 okChildString("argvA3", "Open");
1670 sprintf(filename, "%s\\test file.shlexec", tmpdir);
1671 okChildPath("argvA4", filename);
1675 typedef struct
1677 const char* urlprefix;
1678 const char* basename;
1679 int flags;
1680 int todo;
1681 } fileurl_tests_t;
1683 #define URL_SUCCESS 0x1
1684 #define USE_COLON 0x2
1685 #define USE_BSLASH 0x4
1687 static fileurl_tests_t fileurl_tests[]=
1689 /* How many slashes does it take... */
1690 {"file:", "%s\\test file.shlexec", URL_SUCCESS, 0},
1691 {"file:/", "%s\\test file.shlexec", URL_SUCCESS, 0},
1692 {"file://", "%s\\test file.shlexec", URL_SUCCESS, 0},
1693 {"file:///", "%s\\test file.shlexec", URL_SUCCESS, 0},
1694 {"File:///", "%s\\test file.shlexec", URL_SUCCESS, 0},
1695 {"file:////", "%s\\test file.shlexec", URL_SUCCESS, 0},
1696 {"file://///", "%s\\test file.shlexec", 0, 0},
1698 /* Test with Windows-style paths */
1699 {"file:///", "%s\\test file.shlexec", URL_SUCCESS | USE_COLON, 0},
1700 {"file:///", "%s\\test file.shlexec", URL_SUCCESS | USE_BSLASH, 0},
1702 /* Check handling of hostnames */
1703 {"file://localhost/", "%s\\test file.shlexec", URL_SUCCESS, 0},
1704 {"file://localhost:80/", "%s\\test file.shlexec", 0, 0},
1705 {"file://LocalHost/", "%s\\test file.shlexec", URL_SUCCESS, 0},
1706 {"file://127.0.0.1/", "%s\\test file.shlexec", 0, 0},
1707 {"file://::1/", "%s\\test file.shlexec", 0, 0},
1708 {"file://notahost/", "%s\\test file.shlexec", 0, 0},
1710 /* Environment variables are not expanded in URLs */
1711 {"%urlprefix%", "%s\\test file.shlexec", 0, 0x1},
1712 {"file:///", "%%TMPDIR%%\\test file.shlexec", 0, 0},
1714 /* Test shortcuts vs. URLs */
1715 {"file://///", "%s\\test_shortcut_shlexec.lnk", 0, 0x1d},
1717 {NULL, NULL, 0, 0}
1720 static void test_fileurls(void)
1722 char filename[MAX_PATH], fileurl[MAX_PATH], longtmpdir[MAX_PATH];
1723 char command[MAX_PATH];
1724 const fileurl_tests_t* test;
1725 char *s;
1726 INT_PTR rc;
1728 if (skip_shlexec_tests)
1730 skip("No file URL tests due to lack of .shlexec association\n");
1731 return;
1734 rc = (INT_PTR)ShellExecuteA(NULL, NULL, "file:///nosuchfile.shlexec", NULL, NULL, SW_SHOWNORMAL);
1735 if (rc > 32)
1737 win_skip("shell32 is too old (likely < 4.72). Skipping the file URL tests\n");
1738 return;
1741 get_long_path_name(tmpdir, longtmpdir, sizeof(longtmpdir)/sizeof(*longtmpdir));
1742 SetEnvironmentVariableA("urlprefix", "file:///");
1744 test=fileurl_tests;
1745 while (test->basename)
1747 /* Build the file URL */
1748 sprintf(filename, test->basename, longtmpdir);
1749 strcpy(fileurl, test->urlprefix);
1750 strcat(fileurl, filename);
1751 s = fileurl + strlen(test->urlprefix);
1752 while (*s)
1754 if (!(test->flags & USE_COLON) && *s == ':')
1755 *s = '|';
1756 else if (!(test->flags & USE_BSLASH) && *s == '\\')
1757 *s = '/';
1758 s++;
1761 /* Test it first with FindExecutable() */
1762 rc = (INT_PTR)FindExecutableA(fileurl, NULL, command);
1763 ok(rc == SE_ERR_FNF, "FindExecutable(%s) failed: bad rc=%lu\n", fileurl, rc);
1765 /* Then ShellExecute() */
1766 if ((test->todo & 0x10) == 0)
1767 rc = shell_execute(NULL, fileurl, NULL, NULL);
1768 else todo_wait
1769 rc = shell_execute(NULL, fileurl, NULL, NULL);
1770 if (bad_shellexecute)
1772 win_skip("shell32 is too old (likely 4.72). Skipping the file URL tests\n");
1773 break;
1775 if (test->flags & URL_SUCCESS)
1777 if ((test->todo & 0x1) == 0)
1778 ok(rc > 32, "%s failed: bad rc=%lu\n", shell_call, rc);
1779 else todo_wine
1780 ok(rc > 32, "%s failed: bad rc=%lu\n", shell_call, rc);
1782 else
1784 if ((test->todo & 0x1) == 0)
1785 ok(rc == SE_ERR_FNF || rc == SE_ERR_PNF ||
1786 broken(rc == SE_ERR_ACCESSDENIED) /* win2000 */,
1787 "%s failed: bad rc=%lu\n", shell_call, rc);
1788 else todo_wine
1789 ok(rc == SE_ERR_FNF || rc == SE_ERR_PNF ||
1790 broken(rc == SE_ERR_ACCESSDENIED) /* win2000 */,
1791 "%s failed: bad rc=%lu\n", shell_call, rc);
1793 if (rc == 33)
1795 if ((test->todo & 0x2) == 0)
1796 okChildInt("argcA", 5);
1797 else todo_wine
1798 okChildInt("argcA", 5);
1800 if ((test->todo & 0x4) == 0)
1801 okChildString("argvA3", "Open");
1802 else todo_wine
1803 okChildString("argvA3", "Open");
1805 if ((test->todo & 0x8) == 0)
1806 okChildPath("argvA4", filename);
1807 else todo_wine
1808 okChildPath("argvA4", filename);
1810 test++;
1813 SetEnvironmentVariableA("urlprefix", NULL);
1816 static void test_find_executable(void)
1818 char notepad_path[MAX_PATH];
1819 char filename[MAX_PATH];
1820 char command[MAX_PATH];
1821 const filename_tests_t* test;
1822 INT_PTR rc;
1824 if (!create_test_association(".sfe"))
1826 skip("Unable to create association for '.sfe'\n");
1827 return;
1829 create_test_verb(".sfe", "Open", 1, "%1");
1831 /* Don't test FindExecutable(..., NULL), it always crashes */
1833 strcpy(command, "your word");
1834 if (0) /* Can crash on Vista! */
1836 rc=(INT_PTR)FindExecutableA(NULL, NULL, command);
1837 ok(rc == SE_ERR_FNF || rc > 32 /* nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
1838 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
1841 GetSystemDirectoryA( notepad_path, MAX_PATH );
1842 strcat( notepad_path, "\\notepad.exe" );
1844 /* Search for something that should be in the system-wide search path (no default directory) */
1845 strcpy(command, "your word");
1846 rc=(INT_PTR)FindExecutableA("notepad.exe", NULL, command);
1847 ok(rc > 32, "FindExecutable(%s) returned %ld\n", "notepad.exe", rc);
1848 ok(strcasecmp(command, notepad_path) == 0, "FindExecutable(%s) returned command=[%s]\n", "notepad.exe", command);
1850 /* Search for something that should be in the system-wide search path (with default directory) */
1851 strcpy(command, "your word");
1852 rc=(INT_PTR)FindExecutableA("notepad.exe", tmpdir, command);
1853 ok(rc > 32, "FindExecutable(%s) returned %ld\n", "notepad.exe", rc);
1854 ok(strcasecmp(command, notepad_path) == 0, "FindExecutable(%s) returned command=[%s]\n", "notepad.exe", command);
1856 strcpy(command, "your word");
1857 rc=(INT_PTR)FindExecutableA(tmpdir, NULL, command);
1858 ok(rc == SE_ERR_NOASSOC /* >= win2000 */ || rc > 32 /* win98, nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
1859 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
1861 sprintf(filename, "%s\\test file.sfe", tmpdir);
1862 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1863 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1864 /* Depending on the platform, command could be '%1' or 'test file.sfe' */
1866 rc=(INT_PTR)FindExecutableA("test file.sfe", tmpdir, command);
1867 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1869 rc=(INT_PTR)FindExecutableA("test file.sfe", NULL, command);
1870 ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %ld\n", filename, rc);
1872 delete_test_association(".sfe");
1874 if (!create_test_association(".shl"))
1876 skip("Unable to create association for '.shl'\n");
1877 return;
1879 create_test_verb(".shl", "Open", 0, "Open");
1881 sprintf(filename, "%s\\test file.shl", tmpdir);
1882 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1883 ok(rc == SE_ERR_FNF /* NT4 */ || rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1885 sprintf(filename, "%s\\test file.shlfoo", tmpdir);
1886 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1888 delete_test_association(".shl");
1890 if (rc > 32)
1892 /* On Windows XP and 2003 FindExecutable() is completely broken.
1893 * Probably what it does is convert the filename to 8.3 format,
1894 * which as a side effect converts the '.shlfoo' extension to '.shl',
1895 * and then tries to find an association for '.shl'. This means it
1896 * will normally fail on most extensions with more than 3 characters,
1897 * like '.mpeg', etc.
1898 * Also it means we cannot do any other test.
1900 win_skip("FindExecutable() is broken -> not running 4+ character extension tests\n");
1901 return;
1904 if (skip_shlexec_tests)
1906 skip("No FindExecutable/filename tests due to lack of .shlexec association\n");
1907 return;
1910 test=filename_tests;
1911 while (test->basename)
1913 sprintf(filename, test->basename, tmpdir);
1914 if (strchr(filename, '/'))
1916 char* c;
1917 c=filename;
1918 while (*c)
1920 if (*c=='\\')
1921 *c='/';
1922 c++;
1925 /* Win98 does not '\0'-terminate command! */
1926 memset(command, '\0', sizeof(command));
1927 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1928 if (rc > 32)
1929 rc=33;
1930 if ((test->todo & 0x10)==0)
1932 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
1934 else todo_wine
1936 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
1938 if (rc > 32)
1940 BOOL equal;
1941 equal=strcmp(command, argv0) == 0 ||
1942 /* NT4 returns an extra 0x8 character! */
1943 (strlen(command) == strlen(argv0)+1 && strncmp(command, argv0, strlen(argv0)) == 0);
1944 if ((test->todo & 0x20)==0)
1946 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
1947 filename, command, argv0);
1949 else todo_wine
1951 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
1952 filename, command, argv0);
1955 test++;
1960 static filename_tests_t lnk_tests[]=
1962 /* Pass bad / nonexistent filenames as a parameter */
1963 {NULL, "%s\\nonexistent.shlexec", 0xa, 33},
1964 {NULL, "%s\\nonexistent.noassoc", 0xa, 33},
1966 /* Pass regular paths as a parameter */
1967 {NULL, "%s\\test file.shlexec", 0xa, 33},
1968 {NULL, "%s/%%nasty%% $file.shlexec", 0xa, 33},
1970 /* Pass filenames with no association as a parameter */
1971 {NULL, "%s\\test file.noassoc", 0xa, 33},
1973 {NULL, NULL, 0}
1976 static void test_lnks(void)
1978 char filename[MAX_PATH];
1979 char params[MAX_PATH];
1980 const filename_tests_t* test;
1981 INT_PTR rc;
1983 if (skip_shlexec_tests)
1984 skip("No FindExecutable/filename tests due to lack of .shlexec association\n");
1985 else
1987 /* Should open through our association */
1988 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
1989 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
1990 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
1991 okChildInt("argcA", 5);
1992 okChildString("argvA3", "Open");
1993 sprintf(params, "%s\\test file.shlexec", tmpdir);
1994 get_long_path_name(params, filename, sizeof(filename));
1995 okChildPath("argvA4", filename);
1997 todo_wait rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_DOENVSUBST, NULL, "%TMPDIR%\\test_shortcut_shlexec.lnk", NULL, NULL, NULL);
1998 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
1999 okChildInt("argcA", 5);
2000 todo_wine okChildString("argvA3", "Open");
2001 sprintf(params, "%s\\test file.shlexec", tmpdir);
2002 get_long_path_name(params, filename, sizeof(filename));
2003 todo_wine okChildPath("argvA4", filename);
2006 /* Should just run our executable */
2007 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2008 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
2009 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
2010 okChildInt("argcA", 4);
2011 okChildString("argvA3", "Lnk");
2013 /* Lnk's ContextMenuHandler has priority over an explicit class */
2014 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, "shlexec.shlexec");
2015 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
2016 okChildInt("argcA", 4);
2017 okChildString("argvA3", "Lnk");
2019 if (dllver.dwMajorVersion>=6)
2021 char* c;
2022 /* Recent versions of shell32.dll accept '/'s in shortcut paths.
2023 * Older versions don't or are quite buggy in this regard.
2025 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2026 c=filename;
2027 while (*c)
2029 if (*c=='\\')
2030 *c='/';
2031 c++;
2033 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
2034 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc,
2035 GetLastError());
2036 okChildInt("argcA", 4);
2037 okChildString("argvA3", "Lnk");
2040 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2041 test=lnk_tests;
2042 while (test->basename)
2044 params[0]='\"';
2045 sprintf(params+1, test->basename, tmpdir);
2046 strcat(params,"\"");
2047 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, params,
2048 NULL, NULL);
2049 if (rc > 32)
2050 rc=33;
2051 if ((test->todo & 0x1)==0)
2053 ok(rc==test->rc, "%s failed: rc=%lu err=%u\n", shell_call,
2054 rc, GetLastError());
2056 else todo_wine
2058 ok(rc==test->rc, "%s failed: rc=%lu err=%u\n", shell_call,
2059 rc, GetLastError());
2061 if (rc==0)
2063 if ((test->todo & 0x2)==0)
2065 okChildInt("argcA", 5);
2067 else
2069 okChildInt("argcA", 5);
2071 if ((test->todo & 0x4)==0)
2073 okChildString("argvA3", "Lnk");
2075 else todo_wine
2077 okChildString("argvA3", "Lnk");
2079 sprintf(params, test->basename, tmpdir);
2080 if ((test->todo & 0x8)==0)
2082 okChildPath("argvA4", params);
2084 else
2086 okChildPath("argvA4", params);
2089 test++;
2094 static void test_exes(void)
2096 char filename[MAX_PATH];
2097 char params[1024];
2098 INT_PTR rc;
2100 sprintf(params, "shlexec \"%s\" Exec", child_file);
2102 /* We need NOZONECHECKS on Win2003 to block a dialog */
2103 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
2104 NULL, NULL);
2105 ok(rc > 32, "%s returned %lu\n", shell_call, rc);
2106 okChildInt("argcA", 4);
2107 okChildString("argvA3", "Exec");
2109 if (! skip_noassoc_tests)
2111 sprintf(filename, "%s\\test file.noassoc", tmpdir);
2112 if (CopyFileA(argv0, filename, FALSE))
2114 rc=shell_execute(NULL, filename, params, NULL);
2115 todo_wine {
2116 ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%lu\n", shell_call, rc);
2120 else
2122 win_skip("Skipping shellexecute of file with unassociated extension\n");
2125 /* test combining executable and parameters */
2126 sprintf(filename, "%s shlexec \"%s\" Exec", argv0, child_file);
2127 rc = shell_execute(NULL, filename, NULL, NULL);
2128 ok(rc == SE_ERR_FNF, "%s returned %lu\n", shell_call, rc);
2130 sprintf(filename, "\"%s\" shlexec \"%s\" Exec", argv0, child_file);
2131 rc = shell_execute(NULL, filename, NULL, NULL);
2132 ok(rc == SE_ERR_FNF, "%s returned %lu\n", shell_call, rc);
2135 typedef struct
2137 const char* command;
2138 const char* ddeexec;
2139 const char* application;
2140 const char* topic;
2141 const char* ifexec;
2142 int expectedArgs;
2143 const char* expectedDdeExec;
2144 BOOL broken;
2145 } dde_tests_t;
2147 static dde_tests_t dde_tests[] =
2149 /* Test passing and not passing command-line
2150 * argument, no DDE */
2151 {"", NULL, NULL, NULL, NULL, 0, ""},
2152 {"\"%1\"", NULL, NULL, NULL, NULL, 1, ""},
2154 /* Test passing and not passing command-line
2155 * argument, with DDE */
2156 {"", "[open(\"%1\")]", "shlexec", "dde", NULL, 0, "[open(\"%s\")]"},
2157 {"\"%1\"", "[open(\"%1\")]", "shlexec", "dde", NULL, 1, "[open(\"%s\")]"},
2159 /* Test unquoted %1 in command and ddeexec
2160 * (test filename has space) */
2161 {"%1", "[open(%1)]", "shlexec", "dde", NULL, 2, "[open(%s)]", TRUE /* before vista */},
2163 /* Test ifexec precedence over ddeexec */
2164 {"", "[open(\"%1\")]", "shlexec", "dde", "[ifexec(\"%1\")]", 0, "[ifexec(\"%s\")]"},
2166 /* Test default DDE topic */
2167 {"", "[open(\"%1\")]", "shlexec", NULL, NULL, 0, "[open(\"%s\")]"},
2169 /* Test default DDE application */
2170 {"", "[open(\"%1\")]", NULL, "dde", NULL, 0, "[open(\"%s\")]"},
2172 {NULL}
2175 static DWORD WINAPI hooked_WaitForInputIdle(HANDLE process, DWORD timeout)
2177 return WaitForSingleObject(dde_ready_event, timeout);
2181 * WaitForInputIdle() will normally return immediately for console apps. That's
2182 * a problem for us because ShellExecute will assume that an app is ready to
2183 * receive DDE messages after it has called WaitForInputIdle() on that app.
2184 * To work around that we install our own version of WaitForInputIdle() that
2185 * will wait for the child to explicitly tell us that it is ready. We do that
2186 * by changing the entry for WaitForInputIdle() in the shell32 import address
2187 * table.
2189 static void hook_WaitForInputIdle(DWORD (WINAPI *new_func)(HANDLE, DWORD))
2191 char *base;
2192 PIMAGE_NT_HEADERS nt_headers;
2193 DWORD import_directory_rva;
2194 PIMAGE_IMPORT_DESCRIPTOR import_descriptor;
2196 base = (char *) GetModuleHandleA("shell32.dll");
2197 nt_headers = (PIMAGE_NT_HEADERS)(base + ((PIMAGE_DOS_HEADER) base)->e_lfanew);
2198 import_directory_rva = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
2200 /* Search for the correct imported module by walking the import descriptors */
2201 import_descriptor = (PIMAGE_IMPORT_DESCRIPTOR)(base + import_directory_rva);
2202 while (U(*import_descriptor).OriginalFirstThunk != 0)
2204 char *import_module_name;
2206 import_module_name = base + import_descriptor->Name;
2207 if (lstrcmpiA(import_module_name, "user32.dll") == 0 ||
2208 lstrcmpiA(import_module_name, "user32") == 0)
2210 PIMAGE_THUNK_DATA int_entry;
2211 PIMAGE_THUNK_DATA iat_entry;
2213 /* The import name table and import address table are two parallel
2214 * arrays. We need the import name table to find the imported
2215 * routine and the import address table to patch the address, so
2216 * walk them side by side */
2217 int_entry = (PIMAGE_THUNK_DATA)(base + U(*import_descriptor).OriginalFirstThunk);
2218 iat_entry = (PIMAGE_THUNK_DATA)(base + import_descriptor->FirstThunk);
2219 while (int_entry->u1.Ordinal != 0)
2221 if (! IMAGE_SNAP_BY_ORDINAL(int_entry->u1.Ordinal))
2223 PIMAGE_IMPORT_BY_NAME import_by_name;
2224 import_by_name = (PIMAGE_IMPORT_BY_NAME)(base + int_entry->u1.AddressOfData);
2225 if (lstrcmpA((char *) import_by_name->Name, "WaitForInputIdle") == 0)
2227 /* Found the correct routine in the correct imported module. Patch it. */
2228 DWORD old_prot;
2229 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), PAGE_READWRITE, &old_prot);
2230 iat_entry->u1.Function = (ULONG_PTR) new_func;
2231 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), old_prot, &old_prot);
2232 break;
2235 int_entry++;
2236 iat_entry++;
2238 break;
2241 import_descriptor++;
2245 static void test_dde(void)
2247 char filename[MAX_PATH], defApplication[MAX_PATH];
2248 const dde_tests_t* test;
2249 char params[1024];
2250 INT_PTR rc;
2251 HANDLE map;
2252 char *shared_block;
2254 hook_WaitForInputIdle(hooked_WaitForInputIdle);
2256 sprintf(filename, "%s\\test file.sde", tmpdir);
2258 /* Default service is application name minus path and extension */
2259 strcpy(defApplication, strrchr(argv0, '\\')+1);
2260 *strchr(defApplication, '.') = 0;
2262 map = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
2263 4096, "winetest_shlexec_dde_map");
2264 shared_block = MapViewOfFile(map, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 4096);
2266 test = dde_tests;
2267 while (test->command)
2269 if (!create_test_association(".sde"))
2271 skip("Unable to create association for '.sde'\n");
2272 return;
2274 create_test_verb_dde(".sde", "Open", 0, test->command, test->ddeexec,
2275 test->application, test->topic, test->ifexec);
2277 if (test->application != NULL || test->topic != NULL)
2279 strcpy(shared_block, test->application ? test->application : defApplication);
2280 strcpy(shared_block + strlen(shared_block) + 1, test->topic ? test->topic : SZDDESYS_TOPIC);
2282 else
2284 shared_block[0] = '\0';
2285 shared_block[1] = '\0';
2287 ddeExec[0] = 0;
2289 dde_ready_event = CreateEventA(NULL, FALSE, FALSE, "winetest_shlexec_dde_ready");
2290 rc = shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, filename, NULL, NULL, NULL);
2291 CloseHandle(dde_ready_event);
2292 ok(32 < rc, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
2294 if (32 < rc)
2296 if (test->broken)
2297 okChildIntBroken("argcA", test->expectedArgs + 3);
2298 else
2299 okChildInt("argcA", test->expectedArgs + 3);
2301 if (test->expectedArgs == 1) okChildPath("argvA3", filename);
2303 sprintf(params, test->expectedDdeExec, filename);
2304 okChildPath("ddeExec", params);
2307 delete_test_association(".sde");
2308 test++;
2311 UnmapViewOfFile(shared_block);
2312 CloseHandle(map);
2313 hook_WaitForInputIdle((void *) WaitForInputIdle);
2316 #define DDE_DEFAULT_APP_VARIANTS 2
2317 typedef struct
2319 const char* command;
2320 const char* expectedDdeApplication[DDE_DEFAULT_APP_VARIANTS];
2321 int todo;
2322 int rc[DDE_DEFAULT_APP_VARIANTS];
2323 } dde_default_app_tests_t;
2325 static dde_default_app_tests_t dde_default_app_tests[] =
2327 /* Windows XP and 98 handle default DDE app names in different ways.
2328 * The application name we see in the first test determines the pattern
2329 * of application names and return codes we will look for. */
2331 /* Test unquoted existing filename with a space */
2332 {"%s\\test file.exe", {"test file", "test"}, 0x0, {33, 33}},
2333 {"%s\\test file.exe param", {"test file", "test"}, 0x0, {33, 33}},
2335 /* Test quoted existing filename with a space */
2336 {"\"%s\\test file.exe\"", {"test file", "test file"}, 0x0, {33, 33}},
2337 {"\"%s\\test file.exe\" param", {"test file", "test file"}, 0x0, {33, 33}},
2339 /* Test unquoted filename with a space that doesn't exist, but
2340 * test2.exe does */
2341 {"%s\\test2 file.exe", {"test2", "test2"}, 0x0, {33, 33}},
2342 {"%s\\test2 file.exe param", {"test2", "test2"}, 0x0, {33, 33}},
2344 /* Test quoted filename with a space that does not exist */
2345 {"\"%s\\test2 file.exe\"", {"", "test2 file"}, 0x0, {5, 33}},
2346 {"\"%s\\test2 file.exe\" param", {"", "test2 file"}, 0x0, {5, 33}},
2348 /* Test filename supplied without the extension */
2349 {"%s\\test2", {"test2", "test2"}, 0x0, {33, 33}},
2350 {"%s\\test2 param", {"test2", "test2"}, 0x0, {33, 33}},
2352 /* Test an unquoted nonexistent filename */
2353 {"%s\\notexist.exe", {"", "notexist"}, 0x0, {5, 33}},
2354 {"%s\\notexist.exe param", {"", "notexist"}, 0x0, {5, 33}},
2356 /* Test an application that will be found on the path */
2357 {"cmd", {"cmd", "cmd"}, 0x0, {33, 33}},
2358 {"cmd param", {"cmd", "cmd"}, 0x0, {33, 33}},
2360 /* Test an application that will not be found on the path */
2361 {"xyzwxyzwxyz", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
2362 {"xyzwxyzwxyz param", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
2364 {NULL, {NULL}, 0, {0}}
2367 typedef struct
2369 char *filename;
2370 DWORD threadIdParent;
2371 } dde_thread_info_t;
2373 static DWORD CALLBACK ddeThread(LPVOID arg)
2375 dde_thread_info_t *info = arg;
2376 assert(info && info->filename);
2377 PostThreadMessageA(info->threadIdParent,
2378 WM_QUIT,
2379 shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, info->filename, NULL, NULL, NULL),
2381 ExitThread(0);
2384 static void test_dde_default_app(void)
2386 char filename[MAX_PATH];
2387 HSZ hszApplication;
2388 dde_thread_info_t info = { filename, GetCurrentThreadId() };
2389 const dde_default_app_tests_t* test;
2390 char params[1024];
2391 DWORD threadId;
2392 MSG msg;
2393 INT_PTR rc;
2394 int which = 0;
2395 HDDEDATA ret;
2396 BOOL b;
2398 post_quit_on_execute = FALSE;
2399 ddeInst = 0;
2400 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
2401 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0);
2402 ok(rc == DMLERR_NO_ERROR, "got %lx\n", rc);
2404 sprintf(filename, "%s\\test file.sde", tmpdir);
2406 /* It is strictly not necessary to register an application name here, but wine's
2407 * DdeNameService implementation complains if 0 is passed instead of
2408 * hszApplication with DNS_FILTEROFF */
2409 hszApplication = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
2410 hszTopic = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
2411 ok(hszApplication && hszTopic, "got %p and %p\n", hszApplication, hszTopic);
2412 ret = DdeNameService(ddeInst, hszApplication, 0, DNS_REGISTER | DNS_FILTEROFF);
2413 ok(ret != 0, "got %p\n", ret);
2415 test = dde_default_app_tests;
2416 while (test->command)
2418 HANDLE thread;
2420 if (!create_test_association(".sde"))
2422 skip("Unable to create association for '.sde'\n");
2423 return;
2425 sprintf(params, test->command, tmpdir);
2426 create_test_verb_dde(".sde", "Open", 1, params, "[test]", NULL,
2427 "shlexec", NULL);
2428 ddeApplication[0] = 0;
2430 /* No application will be run as we will respond to the first DDE event,
2431 * so don't wait for it */
2432 SetEvent(hEvent);
2434 thread = CreateThread(NULL, 0, ddeThread, &info, 0, &threadId);
2435 ok(thread != NULL, "got %p\n", thread);
2436 while (GetMessageA(&msg, NULL, 0, 0)) DispatchMessageA(&msg);
2437 rc = msg.wParam > 32 ? 33 : msg.wParam;
2439 /* First test, find which set of test data we expect to see */
2440 if (test == dde_default_app_tests)
2442 int i;
2443 for (i=0; i<DDE_DEFAULT_APP_VARIANTS; i++)
2445 if (!strcmp(ddeApplication, test->expectedDdeApplication[i]))
2447 which = i;
2448 break;
2451 if (i == DDE_DEFAULT_APP_VARIANTS)
2452 skip("Default DDE application test does not match any available results, using first expected data set.\n");
2455 if ((test->todo & 0x1)==0)
2457 ok(rc==test->rc[which], "%s failed: rc=%lu err=%u\n", shell_call,
2458 rc, GetLastError());
2460 else todo_wine
2462 ok(rc==test->rc[which], "%s failed: rc=%lu err=%u\n", shell_call,
2463 rc, GetLastError());
2465 if (rc == 33)
2467 if ((test->todo & 0x2)==0)
2469 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
2470 "Expected application '%s', got '%s'\n",
2471 test->expectedDdeApplication[which], ddeApplication);
2473 else todo_wine
2475 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
2476 "Expected application '%s', got '%s'\n",
2477 test->expectedDdeApplication[which], ddeApplication);
2481 delete_test_association(".sde");
2482 test++;
2485 ret = DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER);
2486 ok(ret != 0, "got %p\n", ret);
2487 b = DdeFreeStringHandle(ddeInst, hszTopic);
2488 ok(b, "got %d\n", b);
2489 b = DdeFreeStringHandle(ddeInst, hszApplication);
2490 ok(b, "got %d\n", b);
2491 b = DdeUninitialize(ddeInst);
2492 ok(b, "got %d\n", b);
2495 static void init_test(void)
2497 HMODULE hdll;
2498 HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO*);
2499 char filename[MAX_PATH];
2500 WCHAR lnkfile[MAX_PATH];
2501 char params[1024];
2502 const char* const * testfile;
2503 lnk_desc_t desc;
2504 DWORD rc;
2505 HRESULT r;
2507 hdll=GetModuleHandleA("shell32.dll");
2508 pDllGetVersion=(void*)GetProcAddress(hdll, "DllGetVersion");
2509 if (pDllGetVersion)
2511 dllver.cbSize=sizeof(dllver);
2512 pDllGetVersion(&dllver);
2513 trace("major=%d minor=%d build=%d platform=%d\n",
2514 dllver.dwMajorVersion, dllver.dwMinorVersion,
2515 dllver.dwBuildNumber, dllver.dwPlatformID);
2517 else
2519 memset(&dllver, 0, sizeof(dllver));
2522 r = CoInitialize(NULL);
2523 ok(r == S_OK, "CoInitialize failed (0x%08x)\n", r);
2524 if (FAILED(r))
2525 exit(1);
2527 rc=GetModuleFileNameA(NULL, argv0, sizeof(argv0));
2528 ok(rc != 0 && rc < sizeof(argv0), "got %d\n", rc);
2529 if (GetFileAttributesA(argv0)==INVALID_FILE_ATTRIBUTES)
2531 strcat(argv0, ".so");
2532 ok(GetFileAttributesA(argv0)!=INVALID_FILE_ATTRIBUTES,
2533 "unable to find argv0!\n");
2536 /* Older versions (win 2k) fail tests if there is a space in
2537 the path. */
2538 if (dllver.dwMajorVersion <= 5)
2539 strcpy(filename, "c:\\");
2540 else
2541 GetTempPathA(sizeof(filename), filename);
2542 GetTempFileNameA(filename, "wt", 0, tmpdir);
2543 GetLongPathNameA(tmpdir, tmpdir, sizeof(tmpdir));
2544 DeleteFileA( tmpdir );
2545 rc = CreateDirectoryA( tmpdir, NULL );
2546 ok( rc, "failed to create %s err %u\n", tmpdir, GetLastError() );
2547 /* Set %TMPDIR% for the tests */
2548 SetEnvironmentVariableA("TMPDIR", tmpdir);
2550 rc = GetTempFileNameA(tmpdir, "wt", 0, child_file);
2551 ok(rc != 0, "got %d\n", rc);
2552 init_event(child_file);
2554 /* Set up the test files */
2555 testfile=testfiles;
2556 while (*testfile)
2558 HANDLE hfile;
2560 sprintf(filename, *testfile, tmpdir);
2561 hfile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2562 FILE_ATTRIBUTE_NORMAL, NULL);
2563 if (hfile==INVALID_HANDLE_VALUE)
2565 trace("unable to create '%s': err=%u\n", filename, GetLastError());
2566 assert(0);
2568 CloseHandle(hfile);
2569 testfile++;
2572 /* Setup the test shortcuts */
2573 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
2574 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
2575 desc.description=NULL;
2576 desc.workdir=NULL;
2577 sprintf(filename, "%s\\test file.shlexec", tmpdir);
2578 desc.path=filename;
2579 desc.pidl=NULL;
2580 desc.arguments="ignored";
2581 desc.showcmd=0;
2582 desc.icon=NULL;
2583 desc.icon_id=0;
2584 desc.hotkey=0;
2585 create_lnk(lnkfile, &desc, 0);
2587 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2588 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
2589 desc.description=NULL;
2590 desc.workdir=NULL;
2591 desc.path=argv0;
2592 desc.pidl=NULL;
2593 sprintf(params, "shlexec \"%s\" Lnk", child_file);
2594 desc.arguments=params;
2595 desc.showcmd=0;
2596 desc.icon=NULL;
2597 desc.icon_id=0;
2598 desc.hotkey=0;
2599 create_lnk(lnkfile, &desc, 0);
2601 /* Create a basic association suitable for most tests */
2602 if (!create_test_association(".shlexec"))
2604 skip_shlexec_tests = TRUE;
2605 skip("Unable to create association for '.shlexec'\n");
2606 return;
2608 create_test_verb(".shlexec", "Open", 0, "Open \"%1\"");
2609 create_test_verb(".shlexec", "NoQuotes", 0, "NoQuotes %1");
2610 create_test_verb(".shlexec", "LowerL", 0, "LowerL %l");
2611 create_test_verb(".shlexec", "QuotedLowerL", 0, "QuotedLowerL \"%l\"");
2612 create_test_verb(".shlexec", "UpperL", 0, "UpperL %L");
2613 create_test_verb(".shlexec", "QuotedUpperL", 0, "QuotedUpperL \"%L\"");
2616 static void cleanup_test(void)
2618 char filename[MAX_PATH];
2619 const char* const * testfile;
2621 /* Delete the test files */
2622 testfile=testfiles;
2623 while (*testfile)
2625 sprintf(filename, *testfile, tmpdir);
2626 /* Make sure we can delete the files ('test file.noassoc' is read-only now) */
2627 SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL);
2628 DeleteFileA(filename);
2629 testfile++;
2631 DeleteFileA(child_file);
2632 RemoveDirectoryA(tmpdir);
2634 /* Delete the test association */
2635 delete_test_association(".shlexec");
2637 CloseHandle(hEvent);
2639 CoUninitialize();
2642 static void test_directory(void)
2644 char path[MAX_PATH], curdir[MAX_PATH];
2645 char params[1024], dirpath[1024];
2646 INT_PTR rc;
2648 sprintf(path, "%s\\test2.exe", tmpdir);
2649 CopyFileA(argv0, path, FALSE);
2651 sprintf(params, "shlexec \"%s\" Exec", child_file);
2653 /* Test with the current directory */
2654 GetCurrentDirectoryA(sizeof(curdir), curdir);
2655 SetCurrentDirectoryA(tmpdir);
2656 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2657 NULL, "test2.exe", params, NULL, NULL);
2658 ok(rc > 32, "%s returned %lu\n", shell_call, rc);
2659 okChildInt("argcA", 4);
2660 okChildString("argvA3", "Exec");
2661 todo_wine okChildPath("longPath", path);
2662 SetCurrentDirectoryA(curdir);
2664 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2665 NULL, "test2.exe", params, NULL, NULL);
2666 ok(rc == SE_ERR_FNF, "%s returned %lu\n", shell_call, rc);
2668 /* Explicitly specify the directory to use */
2669 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2670 NULL, "test2.exe", params, tmpdir, NULL);
2671 ok(rc > 32, "%s returned %lu\n", shell_call, rc);
2672 okChildInt("argcA", 4);
2673 okChildString("argvA3", "Exec");
2674 todo_wine okChildPath("longPath", path);
2676 /* Specify it through an environment variable */
2677 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2678 NULL, "test2.exe", params, "%TMPDIR%", NULL);
2679 todo_wine ok(rc == SE_ERR_FNF, "%s returned %lu\n", shell_call, rc);
2681 rc=shell_execute_ex(SEE_MASK_DOENVSUBST|SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2682 NULL, "test2.exe", params, "%TMPDIR%", NULL);
2683 ok(rc > 32, "%s returned %lu\n", shell_call, rc);
2684 okChildInt("argcA", 4);
2685 okChildString("argvA3", "Exec");
2686 todo_wine okChildPath("longPath", path);
2688 /* Not a colon-separated directory list */
2689 sprintf(dirpath, "%s:%s", curdir, tmpdir);
2690 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2691 NULL, "test2.exe", params, dirpath, NULL);
2692 ok(rc == SE_ERR_FNF, "%s returned %lu\n", shell_call, rc);
2695 START_TEST(shlexec)
2698 myARGC = winetest_get_mainargs(&myARGV);
2699 if (myARGC >= 3)
2701 doChild(myARGC, myARGV);
2702 exit(0);
2705 init_test();
2707 test_commandline2argv();
2708 test_argify();
2709 test_lpFile_parsed();
2710 test_filename();
2711 test_fileurls();
2712 test_find_executable();
2713 test_lnks();
2714 test_exes();
2715 test_dde();
2716 test_dde_default_app();
2717 test_directory();
2719 cleanup_test();