shell32/tests: Check the child process exit code and close the process handle.
[wine.git] / dlls / shell32 / tests / shlexec.c
blob063081537696b849785cd964f4c47d0a80e8907d
1 /*
2 * Unit test of the ShellExecute function.
4 * Copyright 2005, 2016 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 * - ShellExecuteEx() also calls SetLastError() with meaningful values which
30 * we could check
33 /* Needed to get SEE_MASK_NOZONECHECKS with the PSDK */
34 #define NTDDI_WINXPSP1 0x05010100
35 #define NTDDI_VERSION NTDDI_WINXPSP1
36 #define _WIN32_WINNT 0x0501
38 #include <stdio.h>
39 #include <assert.h>
41 #include "wtypes.h"
42 #include "winbase.h"
43 #include "windef.h"
44 #include "shellapi.h"
45 #include "shlwapi.h"
46 #include "ddeml.h"
47 #include "wine/test.h"
49 #include "shell32_test.h"
52 static char argv0[MAX_PATH];
53 static int myARGC;
54 static char** myARGV;
55 static char tmpdir[MAX_PATH];
56 static char child_file[MAX_PATH];
57 static DLLVERSIONINFO dllver;
58 static BOOL skip_shlexec_tests = FALSE;
59 static BOOL skip_noassoc_tests = FALSE;
60 static HANDLE dde_ready_event;
63 /***
65 * Helpers to read from / write to the child process results file.
66 * (borrowed from dlls/kernel32/tests/process.c)
68 ***/
70 static const char* encodeA(const char* str)
72 static char encoded[2*1024+1];
73 char* ptr;
74 size_t len,i;
76 if (!str) return "";
77 len = strlen(str) + 1;
78 if (len >= sizeof(encoded)/2)
80 fprintf(stderr, "string is too long!\n");
81 assert(0);
83 ptr = encoded;
84 for (i = 0; i < len; i++)
85 sprintf(&ptr[i * 2], "%02x", (unsigned char)str[i]);
86 ptr[2 * len] = '\0';
87 return ptr;
90 static unsigned decode_char(char c)
92 if (c >= '0' && c <= '9') return c - '0';
93 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
94 assert(c >= 'A' && c <= 'F');
95 return c - 'A' + 10;
98 static char* decodeA(const char* str)
100 static char decoded[1024];
101 char* ptr;
102 size_t len,i;
104 len = strlen(str) / 2;
105 if (!len--) return NULL;
106 if (len >= sizeof(decoded))
108 fprintf(stderr, "string is too long!\n");
109 assert(0);
111 ptr = decoded;
112 for (i = 0; i < len; i++)
113 ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
114 ptr[len] = '\0';
115 return ptr;
118 static void WINETEST_PRINTF_ATTR(2,3) childPrintf(HANDLE h, const char* fmt, ...)
120 va_list valist;
121 char buffer[1024];
122 DWORD w;
124 va_start(valist, fmt);
125 vsprintf(buffer, fmt, valist);
126 va_end(valist);
127 WriteFile(h, buffer, strlen(buffer), &w, NULL);
130 static char* getChildString(const char* sect, const char* key)
132 char buf[1024];
133 char* ret;
135 GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), child_file);
136 if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
137 assert(!(strlen(buf) & 1));
138 ret = decodeA(buf);
139 return ret;
143 /***
145 * Child code
147 ***/
149 static DWORD ddeInst;
150 static HSZ hszTopic;
151 static char ddeExec[MAX_PATH], ddeApplication[MAX_PATH];
152 static BOOL post_quit_on_execute;
154 /* Handle DDE for doChild() and test_dde_default_app() */
155 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
156 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
157 ULONG_PTR dwData1, ULONG_PTR dwData2)
159 DWORD size = 0;
161 if (winetest_debug > 2)
162 trace("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
163 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
165 switch (uType)
167 case XTYP_CONNECT:
168 if (!DdeCmpStringHandles(hsz1, hszTopic))
170 size = DdeQueryStringA(ddeInst, hsz2, ddeApplication, MAX_PATH, CP_WINANSI);
171 ok(size < MAX_PATH, "got size %d\n", size);
172 assert(size < MAX_PATH);
173 return (HDDEDATA)TRUE;
175 return (HDDEDATA)FALSE;
177 case XTYP_EXECUTE:
178 size = DdeGetData(hData, (LPBYTE)ddeExec, MAX_PATH, 0);
179 ok(size < MAX_PATH, "got size %d\n", size);
180 assert(size < MAX_PATH);
181 DdeFreeDataHandle(hData);
182 if (post_quit_on_execute)
183 PostQuitMessage(0);
184 return (HDDEDATA)DDE_FACK;
186 default:
187 return NULL;
191 static HANDLE hEvent;
192 static void init_event(const char* child_file)
194 char* event_name;
195 event_name=strrchr(child_file, '\\')+1;
196 hEvent=CreateEventA(NULL, FALSE, FALSE, event_name);
200 * This is just to make sure the child won't run forever stuck in a
201 * GetMessage() loop when DDE fails for some reason.
203 static void CALLBACK childTimeout(HWND wnd, UINT msg, UINT_PTR timer, DWORD time)
205 trace("childTimeout called\n");
207 PostQuitMessage(0);
210 static void doChild(int argc, char** argv)
212 char *filename, longpath[MAX_PATH] = "";
213 HANDLE hFile, map;
214 int i;
215 int rc;
216 HSZ hszApplication;
217 UINT_PTR timer;
218 HANDLE dde_ready;
219 MSG msg;
220 char *shared_block;
222 filename=argv[2];
223 hFile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
224 if (hFile == INVALID_HANDLE_VALUE)
225 return;
227 /* Arguments */
228 childPrintf(hFile, "[Arguments]\r\n");
229 if (winetest_debug > 2)
231 trace("cmdlineA='%s'\n", GetCommandLineA());
232 trace("argcA=%d\n", argc);
234 childPrintf(hFile, "cmdlineA=%s\r\n", encodeA(GetCommandLineA()));
235 childPrintf(hFile, "argcA=%d\r\n", argc);
236 for (i = 0; i < argc; i++)
238 if (winetest_debug > 2)
239 trace("argvA%d='%s'\n", i, argv[i]);
240 childPrintf(hFile, "argvA%d=%s\r\n", i, encodeA(argv[i]));
242 GetModuleFileNameA(GetModuleHandleA(NULL), longpath, MAX_PATH);
243 childPrintf(hFile, "longPath=%s\r\n", encodeA(longpath));
245 map = OpenFileMappingA(FILE_MAP_READ, FALSE, "winetest_shlexec_dde_map");
246 if (map != NULL)
248 shared_block = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 4096);
249 CloseHandle(map);
250 if (shared_block[0] != '\0' || shared_block[1] != '\0')
252 post_quit_on_execute = TRUE;
253 ddeInst = 0;
254 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
255 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0);
256 ok(rc == DMLERR_NO_ERROR, "got %d\n", rc);
257 hszApplication = DdeCreateStringHandleA(ddeInst, shared_block, CP_WINANSI);
258 hszTopic = DdeCreateStringHandleA(ddeInst, shared_block + strlen(shared_block) + 1, CP_WINANSI);
259 assert(hszApplication && hszTopic);
260 assert(DdeNameService(ddeInst, hszApplication, 0, DNS_REGISTER | DNS_FILTEROFF));
262 timer = SetTimer(NULL, 0, 2500, childTimeout);
264 dde_ready = OpenEventA(EVENT_MODIFY_STATE, FALSE, "winetest_shlexec_dde_ready");
265 SetEvent(dde_ready);
266 CloseHandle(dde_ready);
268 while (GetMessageA(&msg, NULL, 0, 0))
269 DispatchMessageA(&msg);
271 Sleep(500);
272 KillTimer(NULL, timer);
273 assert(DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER));
274 assert(DdeFreeStringHandle(ddeInst, hszTopic));
275 assert(DdeFreeStringHandle(ddeInst, hszApplication));
276 assert(DdeUninitialize(ddeInst));
278 else
280 dde_ready = OpenEventA(EVENT_MODIFY_STATE, FALSE, "winetest_shlexec_dde_ready");
281 SetEvent(dde_ready);
282 CloseHandle(dde_ready);
285 UnmapViewOfFile(shared_block);
287 childPrintf(hFile, "ddeExec=%s\r\n", encodeA(ddeExec));
290 CloseHandle(hFile);
292 init_event(filename);
293 SetEvent(hEvent);
294 CloseHandle(hEvent);
297 static void dump_child_(const char* file, int line)
299 if (winetest_debug > 1)
301 char key[18];
302 char* str;
303 int i, c;
305 str=getChildString("Arguments", "cmdlineA");
306 trace_(file, line)("cmdlineA='%s'\n", str);
307 c=GetPrivateProfileIntA("Arguments", "argcA", -1, child_file);
308 trace_(file, line)("argcA=%d\n",c);
309 for (i=0;i<c;i++)
311 sprintf(key, "argvA%d", i);
312 str=getChildString("Arguments", key);
313 trace_(file, line)("%s='%s'\n", key, str);
319 /***
321 * Helpers to check the ShellExecute() / child process results.
323 ***/
325 static char shell_call[2048];
326 static char assoc_desc[2048];
327 static int shell_call_traced;
328 static void WINETEST_PRINTF_ATTR(2,3) _okShell(int condition, const char *msg, ...)
330 va_list valist;
332 /* Note: if winetest_debug > 1 the ShellExecute() command has already been
333 * traced.
335 if (!condition && winetest_debug <= 1 && !shell_call_traced)
337 printf("Called %s\n", shell_call);
338 if (*assoc_desc)
339 printf("%s\n", assoc_desc);
340 shell_call_traced=1;
343 va_start(valist, msg);
344 winetest_vok(condition, msg, valist);
345 va_end(valist);
347 #define okShell_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : _okShell
348 #define okShell okShell_(__FILE__, __LINE__)
350 void reset_association_description(void)
352 *assoc_desc = '\0';
355 static void okChildString_(const char* file, int line, const char* key, const char* expected, const char* bad)
357 char* result;
358 result=getChildString("Arguments", key);
359 if (!result)
361 okShell_(file, line)(FALSE, "%s expected '%s', but key not found or empty\n", key, expected);
362 return;
364 okShell_(file, line)(lstrcmpiA(result, expected) == 0 ||
365 broken(lstrcmpiA(result, bad) == 0),
366 "%s expected '%s', got '%s'\n", key, expected, result);
368 #define okChildString(key, expected) okChildString_(__FILE__, __LINE__, (key), (expected), (expected))
369 #define okChildStringBroken(key, expected, broken) okChildString_(__FILE__, __LINE__, (key), (expected), (broken))
371 static int StrCmpPath(const char* s1, const char* s2)
373 if (!s1 && !s2) return 0;
374 if (!s2) return 1;
375 if (!s1) return -1;
376 while (*s1)
378 if (!*s2)
380 if (*s1=='.')
381 s1++;
382 return (*s1-*s2);
384 if ((*s1=='/' || *s1=='\\') && (*s2=='/' || *s2=='\\'))
386 while (*s1=='/' || *s1=='\\')
387 s1++;
388 while (*s2=='/' || *s2=='\\')
389 s2++;
391 else if (toupper(*s1)==toupper(*s2))
393 s1++;
394 s2++;
396 else
398 return (*s1-*s2);
401 if (*s2=='.')
402 s2++;
403 if (*s2)
404 return -1;
405 return 0;
408 static void okChildPath_(const char* file, int line, const char* key, const char* expected)
410 char* result;
411 result=getChildString("Arguments", key);
412 if (!result)
414 okShell_(file,line)(FALSE, "%s expected '%s', but key not found or empty\n", key, expected);
415 return;
417 okShell_(file,line)(StrCmpPath(result, expected) == 0,
418 "%s expected '%s', got '%s'\n", key, expected, result);
420 #define okChildPath(key, expected) okChildPath_(__FILE__, __LINE__, (key), (expected))
422 static void okChildInt_(const char* file, int line, const char* key, int expected)
424 INT result;
425 result=GetPrivateProfileIntA("Arguments", key, expected, child_file);
426 okShell_(file,line)(result == expected,
427 "%s expected %d, but got %d\n", key, expected, result);
429 #define okChildInt(key, expected) okChildInt_(__FILE__, __LINE__, (key), (expected))
431 static void okChildIntBroken_(const char* file, int line, const char* key, int expected)
433 INT result;
434 result=GetPrivateProfileIntA("Arguments", key, expected, child_file);
435 okShell_(file,line)(result == expected || broken(result != expected),
436 "%s expected %d, but got %d\n", key, expected, result);
438 #define okChildIntBroken(key, expected) okChildIntBroken_(__FILE__, __LINE__, (key), (expected))
441 /***
443 * ShellExecute wrappers
445 ***/
447 static void strcat_param(char* str, const char* name, const char* param)
449 if (param)
451 if (str[strlen(str)-1] == '"')
452 strcat(str, ", ");
453 strcat(str, name);
454 strcat(str, "=\"");
455 strcat(str, param);
456 strcat(str, "\"");
460 static int _todo_wait = 0;
461 #define todo_wait for (_todo_wait = 1; _todo_wait; _todo_wait = 0)
463 static int bad_shellexecute = 0;
465 static INT_PTR shell_execute_(const char* file, int line, LPCSTR verb, LPCSTR filename, LPCSTR parameters, LPCSTR directory)
467 INT_PTR rc, rcEmpty = 0;
469 if(!verb)
470 rcEmpty = shell_execute_(file, line, "", filename, parameters, directory);
472 shell_call_traced=0;
473 strcpy(shell_call, "ShellExecute(");
474 strcat_param(shell_call, "verb", verb);
475 strcat_param(shell_call, "file", filename);
476 strcat_param(shell_call, "params", parameters);
477 strcat_param(shell_call, "dir", directory);
478 strcat(shell_call, ")");
479 if (winetest_debug > 1)
481 trace_(file, line)("Called %s\n", shell_call);
482 if (*assoc_desc)
483 trace_(file, line)("%s\n", assoc_desc);
484 shell_call_traced=1;
487 DeleteFileA(child_file);
488 SetLastError(0xcafebabe);
490 /* FIXME: We cannot use ShellExecuteEx() here because if there is no
491 * association it displays the 'Open With' dialog and I could not find
492 * a flag to prevent this.
494 rc=(INT_PTR)ShellExecuteA(NULL, verb, filename, parameters, directory, SW_HIDE);
496 if (rc > 32)
498 int wait_rc;
499 wait_rc=WaitForSingleObject(hEvent, 5000);
500 if (wait_rc == WAIT_TIMEOUT)
502 HWND wnd = FindWindowA("#32770", "Windows");
503 if (!wnd)
504 wnd = FindWindowA("Shell_Flyout", "");
505 if (wnd != NULL)
507 SendMessageA(wnd, WM_CLOSE, 0, 0);
508 win_skip("Skipping shellexecute of file with unassociated extension\n");
509 skip_noassoc_tests = TRUE;
510 rc = SE_ERR_NOASSOC;
513 if (!_todo_wait)
514 okShell_(file, line)(wait_rc==WAIT_OBJECT_0 || rc <= 32,
515 "WaitForSingleObject returned %d\n", wait_rc);
516 else todo_wine
517 okShell_(file, line)(wait_rc==WAIT_OBJECT_0 || rc <= 32,
518 "WaitForSingleObject returned %d\n", wait_rc);
520 /* The child process may have changed the result file, so let profile
521 * functions know about it
523 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
524 if (rc > 32)
525 dump_child_(file, line);
527 if(!verb)
529 if (rc != rcEmpty && rcEmpty == SE_ERR_NOASSOC) /* NT4 */
530 bad_shellexecute = 1;
531 okShell_(file, line)(rc == rcEmpty ||
532 broken(rc != rcEmpty && rcEmpty == SE_ERR_NOASSOC) /* NT4 */,
533 "Got different return value with empty string: %lu %lu\n", rc, rcEmpty);
536 return rc;
538 #define shell_execute(verb, filename, parameters, directory) \
539 shell_execute_(__FILE__, __LINE__, verb, filename, parameters, directory)
541 static INT_PTR shell_execute_ex_(const char* file, int line,
542 DWORD mask, LPCSTR verb, LPCSTR filename,
543 LPCSTR parameters, LPCSTR directory,
544 LPCSTR class)
546 char smask[11];
547 SHELLEXECUTEINFOA sei;
548 BOOL success;
549 INT_PTR rc;
551 /* Add some flags so we can wait for the child process */
552 mask |= SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NO_CONSOLE;
554 shell_call_traced=0;
555 strcpy(shell_call, "ShellExecuteEx(");
556 sprintf(smask, "0x%x", mask);
557 strcat_param(shell_call, "mask", smask);
558 strcat_param(shell_call, "verb", verb);
559 strcat_param(shell_call, "file", filename);
560 strcat_param(shell_call, "params", parameters);
561 strcat_param(shell_call, "dir", directory);
562 strcat_param(shell_call, "class", class);
563 strcat(shell_call, ")");
564 if (winetest_debug > 1)
566 trace_(file, line)("Called %s\n", shell_call);
567 if (*assoc_desc)
568 trace_(file, line)("%s\n", assoc_desc);
569 shell_call_traced=1;
572 sei.cbSize=sizeof(sei);
573 sei.fMask=mask;
574 sei.hwnd=NULL;
575 sei.lpVerb=verb;
576 sei.lpFile=filename;
577 sei.lpParameters=parameters;
578 sei.lpDirectory=directory;
579 sei.nShow=SW_SHOWNORMAL;
580 sei.hInstApp=NULL; /* Out */
581 sei.lpIDList=NULL;
582 sei.lpClass=class;
583 sei.hkeyClass=NULL;
584 sei.dwHotKey=0;
585 U(sei).hIcon=NULL;
586 sei.hProcess=(HANDLE)0xdeadbeef; /* Out */
588 DeleteFileA(child_file);
589 SetLastError(0xcafebabe);
590 success=ShellExecuteExA(&sei);
591 rc=(INT_PTR)sei.hInstApp;
592 okShell_(file, line)((success && rc > 32) || (!success && rc <= 32),
593 "rc=%d and hInstApp=%ld is not allowed\n",
594 success, rc);
596 if (rc > 32)
598 DWORD wait_rc, rc;
599 if (sei.hProcess!=NULL)
601 wait_rc=WaitForSingleObject(sei.hProcess, 5000);
602 okShell_(file, line)(wait_rc==WAIT_OBJECT_0,
603 "WaitForSingleObject(hProcess) returned %d\n",
604 wait_rc);
605 wait_rc = GetExitCodeProcess(sei.hProcess, &rc);
606 okShell_(file, line)(wait_rc, "GetExitCodeProcess() failed le=%u\n", GetLastError());
607 if (!_todo_wait)
608 okShell_(file, line)(rc == 0, "child returned %u\n", rc);
609 else todo_wine
610 okShell_(file, line)(rc == 0, "child returned %u\n", rc);
611 CloseHandle(sei.hProcess);
613 wait_rc=WaitForSingleObject(hEvent, 5000);
614 if (!_todo_wait)
615 okShell_(file, line)(wait_rc==WAIT_OBJECT_0,
616 "WaitForSingleObject returned %d\n", wait_rc);
617 else todo_wine
618 okShell_(file, line)(wait_rc==WAIT_OBJECT_0,
619 "WaitForSingleObject returned %d\n", wait_rc);
621 else
622 okShell_(file, line)(sei.hProcess==NULL,
623 "returned a process handle %p\n", sei.hProcess);
625 /* The child process may have changed the result file, so let profile
626 * functions know about it
628 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
629 if (rc > 32)
630 dump_child_(file, line);
632 return rc;
634 #define shell_execute_ex(mask, verb, filename, parameters, directory, class) \
635 shell_execute_ex_(__FILE__, __LINE__, mask, verb, filename, parameters, directory, class)
638 /***
640 * Functions to create / delete associations wrappers
642 ***/
644 static BOOL create_test_association(const char* extension)
646 HKEY hkey, hkey_shell;
647 char class[MAX_PATH];
648 LONG rc;
650 sprintf(class, "shlexec%s", extension);
651 rc=RegCreateKeyExA(HKEY_CLASSES_ROOT, extension, 0, NULL, 0, KEY_SET_VALUE,
652 NULL, &hkey, NULL);
653 ok(rc == ERROR_SUCCESS || rc == ERROR_ACCESS_DENIED,
654 "could not create association %s (rc=%d)\n", class, rc);
655 if (rc != ERROR_SUCCESS)
656 return FALSE;
658 rc=RegSetValueExA(hkey, NULL, 0, REG_SZ, (LPBYTE) class, strlen(class)+1);
659 ok(rc==ERROR_SUCCESS, "RegSetValueEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
660 CloseHandle(hkey);
662 rc=RegCreateKeyExA(HKEY_CLASSES_ROOT, class, 0, NULL, 0,
663 KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS, NULL, &hkey, NULL);
664 ok(rc==ERROR_SUCCESS, "RegCreateKeyEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
666 rc=RegCreateKeyExA(hkey, "shell", 0, NULL, 0,
667 KEY_CREATE_SUB_KEY, NULL, &hkey_shell, NULL);
668 ok(rc==ERROR_SUCCESS, "RegCreateKeyEx 'shell' failed, expected ERROR_SUCCESS, got %d\n", rc);
670 CloseHandle(hkey);
671 CloseHandle(hkey_shell);
673 return TRUE;
676 /* Based on RegDeleteTreeW from dlls/advapi32/registry.c */
677 static LSTATUS myRegDeleteTreeA(HKEY hKey, LPCSTR lpszSubKey)
679 LONG ret;
680 DWORD dwMaxSubkeyLen, dwMaxValueLen;
681 DWORD dwMaxLen, dwSize;
682 CHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
683 HKEY hSubKey = hKey;
685 if(lpszSubKey)
687 ret = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
688 if (ret) return ret;
691 /* Get highest length for keys, values */
692 ret = RegQueryInfoKeyA(hSubKey, NULL, NULL, NULL, NULL,
693 &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
694 if (ret) goto cleanup;
696 dwMaxSubkeyLen++;
697 dwMaxValueLen++;
698 dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
699 if (dwMaxLen > sizeof(szNameBuf)/sizeof(CHAR))
701 /* Name too big: alloc a buffer for it */
702 if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(CHAR))))
704 ret = ERROR_NOT_ENOUGH_MEMORY;
705 goto cleanup;
710 /* Recursively delete all the subkeys */
711 while (TRUE)
713 dwSize = dwMaxLen;
714 if (RegEnumKeyExA(hSubKey, 0, lpszName, &dwSize, NULL,
715 NULL, NULL, NULL)) break;
717 ret = myRegDeleteTreeA(hSubKey, lpszName);
718 if (ret) goto cleanup;
721 if (lpszSubKey)
722 ret = RegDeleteKeyA(hKey, lpszSubKey);
723 else
724 while (TRUE)
726 dwSize = dwMaxLen;
727 if (RegEnumValueA(hKey, 0, lpszName, &dwSize,
728 NULL, NULL, NULL, NULL)) break;
730 ret = RegDeleteValueA(hKey, lpszName);
731 if (ret) goto cleanup;
734 cleanup:
735 /* Free buffer if allocated */
736 if (lpszName != szNameBuf)
737 HeapFree( GetProcessHeap(), 0, lpszName);
738 if(lpszSubKey)
739 RegCloseKey(hSubKey);
740 return ret;
743 static void delete_test_association(const char* extension)
745 char class[MAX_PATH];
747 sprintf(class, "shlexec%s", extension);
748 myRegDeleteTreeA(HKEY_CLASSES_ROOT, class);
749 myRegDeleteTreeA(HKEY_CLASSES_ROOT, extension);
752 static void create_test_verb_dde(const char* extension, const char* verb,
753 int rawcmd, const char* cmdtail, const char *ddeexec,
754 const char *application, const char *topic,
755 const char *ifexec)
757 HKEY hkey_shell, hkey_verb, hkey_cmd;
758 char shell[MAX_PATH];
759 char* cmd;
760 LONG rc;
762 strcpy(assoc_desc, "Assoc ");
763 strcat_param(assoc_desc, "ext", extension);
764 strcat_param(assoc_desc, "verb", verb);
765 sprintf(shell, "%d", rawcmd);
766 strcat_param(assoc_desc, "rawcmd", shell);
767 strcat_param(assoc_desc, "cmdtail", cmdtail);
768 strcat_param(assoc_desc, "ddeexec", ddeexec);
769 strcat_param(assoc_desc, "app", application);
770 strcat_param(assoc_desc, "topic", topic);
771 strcat_param(assoc_desc, "ifexec", ifexec);
773 sprintf(shell, "shlexec%s\\shell", extension);
774 rc=RegOpenKeyExA(HKEY_CLASSES_ROOT, shell, 0,
775 KEY_CREATE_SUB_KEY, &hkey_shell);
776 ok(rc == ERROR_SUCCESS, "%s key creation failed with %d\n", shell, rc);
778 rc=RegCreateKeyExA(hkey_shell, verb, 0, NULL, 0, KEY_CREATE_SUB_KEY,
779 NULL, &hkey_verb, NULL);
780 ok(rc == ERROR_SUCCESS, "%s verb key creation failed with %d\n", verb, rc);
782 rc=RegCreateKeyExA(hkey_verb, "command", 0, NULL, 0, KEY_SET_VALUE,
783 NULL, &hkey_cmd, NULL);
784 ok(rc == ERROR_SUCCESS, "\'command\' key creation failed with %d\n", rc);
786 if (rawcmd)
788 rc=RegSetValueExA(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmdtail, strlen(cmdtail)+1);
790 else
792 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(child_file)+2+strlen(cmdtail)+1);
793 sprintf(cmd,"%s shlexec \"%s\" %s", argv0, child_file, cmdtail);
794 rc=RegSetValueExA(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmd, strlen(cmd)+1);
795 ok(rc == ERROR_SUCCESS, "setting command failed with %d\n", rc);
796 HeapFree(GetProcessHeap(), 0, cmd);
799 if (ddeexec)
801 HKEY hkey_ddeexec, hkey_application, hkey_topic, hkey_ifexec;
803 rc=RegCreateKeyExA(hkey_verb, "ddeexec", 0, NULL, 0, KEY_SET_VALUE |
804 KEY_CREATE_SUB_KEY, NULL, &hkey_ddeexec, NULL);
805 ok(rc == ERROR_SUCCESS, "\'ddeexec\' key creation failed with %d\n", rc);
806 rc=RegSetValueExA(hkey_ddeexec, NULL, 0, REG_SZ, (LPBYTE)ddeexec,
807 strlen(ddeexec)+1);
808 ok(rc == ERROR_SUCCESS, "set value failed with %d\n", rc);
810 if (application)
812 rc=RegCreateKeyExA(hkey_ddeexec, "application", 0, NULL, 0, KEY_SET_VALUE,
813 NULL, &hkey_application, NULL);
814 ok(rc == ERROR_SUCCESS, "\'application\' key creation failed with %d\n", rc);
816 rc=RegSetValueExA(hkey_application, NULL, 0, REG_SZ, (LPBYTE)application,
817 strlen(application)+1);
818 ok(rc == ERROR_SUCCESS, "set value failed with %d\n", rc);
819 CloseHandle(hkey_application);
821 if (topic)
823 rc=RegCreateKeyExA(hkey_ddeexec, "topic", 0, NULL, 0, KEY_SET_VALUE,
824 NULL, &hkey_topic, NULL);
825 ok(rc == ERROR_SUCCESS, "\'topic\' key creation failed with %d\n", rc);
826 rc=RegSetValueExA(hkey_topic, NULL, 0, REG_SZ, (LPBYTE)topic,
827 strlen(topic)+1);
828 ok(rc == ERROR_SUCCESS, "set value failed with %d\n", rc);
829 CloseHandle(hkey_topic);
831 if (ifexec)
833 rc=RegCreateKeyExA(hkey_ddeexec, "ifexec", 0, NULL, 0, KEY_SET_VALUE,
834 NULL, &hkey_ifexec, NULL);
835 ok(rc == ERROR_SUCCESS, "\'ifexec\' key creation failed with %d\n", rc);
836 rc=RegSetValueExA(hkey_ifexec, NULL, 0, REG_SZ, (LPBYTE)ifexec,
837 strlen(ifexec)+1);
838 ok(rc == ERROR_SUCCESS, "set value failed with %d\n", rc);
839 CloseHandle(hkey_ifexec);
841 CloseHandle(hkey_ddeexec);
844 CloseHandle(hkey_shell);
845 CloseHandle(hkey_verb);
846 CloseHandle(hkey_cmd);
849 /* Creates a class' non-DDE test verb.
850 * This function is meant to be used to create long term test verbs and thus
851 * does not trace them.
853 static void create_test_verb(const char* extension, const char* verb,
854 int rawcmd, const char* cmdtail)
856 create_test_verb_dde(extension, verb, rawcmd, cmdtail, NULL, NULL,
857 NULL, NULL);
858 reset_association_description();
862 /***
864 * GetLongPathNameA equivalent that supports Win95 and WinNT
866 ***/
868 static DWORD get_long_path_name(const char* shortpath, char* longpath, DWORD longlen)
870 char tmplongpath[MAX_PATH];
871 const char* p;
872 DWORD sp = 0, lp = 0;
873 DWORD tmplen;
874 WIN32_FIND_DATAA wfd;
875 HANDLE goit;
877 if (!shortpath || !shortpath[0])
878 return 0;
880 if (shortpath[1] == ':')
882 tmplongpath[0] = shortpath[0];
883 tmplongpath[1] = ':';
884 lp = sp = 2;
887 while (shortpath[sp])
889 /* check for path delimiters and reproduce them */
890 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
892 if (!lp || tmplongpath[lp-1] != '\\')
894 /* strip double "\\" */
895 tmplongpath[lp++] = '\\';
897 tmplongpath[lp] = 0; /* terminate string */
898 sp++;
899 continue;
902 p = shortpath + sp;
903 if (sp == 0 && p[0] == '.' && (p[1] == '/' || p[1] == '\\'))
905 tmplongpath[lp++] = *p++;
906 tmplongpath[lp++] = *p++;
908 for (; *p && *p != '/' && *p != '\\'; p++);
909 tmplen = p - (shortpath + sp);
910 lstrcpynA(tmplongpath + lp, shortpath + sp, tmplen + 1);
911 /* Check if the file exists and use the existing file name */
912 goit = FindFirstFileA(tmplongpath, &wfd);
913 if (goit == INVALID_HANDLE_VALUE)
914 return 0;
915 FindClose(goit);
916 strcpy(tmplongpath + lp, wfd.cFileName);
917 lp += strlen(tmplongpath + lp);
918 sp += tmplen;
920 tmplen = strlen(shortpath) - 1;
921 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
922 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
923 tmplongpath[lp++] = shortpath[tmplen];
924 tmplongpath[lp] = 0;
926 tmplen = strlen(tmplongpath) + 1;
927 if (tmplen <= longlen)
929 strcpy(longpath, tmplongpath);
930 tmplen--; /* length without 0 */
933 return tmplen;
937 /***
939 * Tests
941 ***/
943 static const char* testfiles[]=
945 "%s\\test file.shlexec",
946 "%s\\%%nasty%% $file.shlexec",
947 "%s\\test file.noassoc",
948 "%s\\test file.noassoc.shlexec",
949 "%s\\test file.shlexec.noassoc",
950 "%s\\test_shortcut_shlexec.lnk",
951 "%s\\test_shortcut_exe.lnk",
952 "%s\\test file.shl",
953 "%s\\test file.shlfoo",
954 "%s\\test file.sfe",
955 "%s\\masked file.shlexec",
956 "%s\\masked",
957 "%s\\test file.sde",
958 "%s\\test file.exe",
959 "%s\\test2.exe",
960 "%s\\simple.shlexec",
961 "%s\\drawback_file.noassoc",
962 "%s\\drawback_file.noassoc foo.shlexec",
963 "%s\\drawback_nonexist.noassoc foo.shlexec",
964 NULL
967 typedef struct
969 const char* verb;
970 const char* basename;
971 int todo;
972 INT_PTR rc;
973 } filename_tests_t;
975 static filename_tests_t filename_tests[]=
977 /* Test bad / nonexistent filenames */
978 {NULL, "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
979 {NULL, "%s\\nonexistent.noassoc", 0x0, SE_ERR_FNF},
981 /* Standard tests */
982 {NULL, "%s\\test file.shlexec", 0x0, 33},
983 {NULL, "%s\\test file.shlexec.", 0x0, 33},
984 {NULL, "%s\\%%nasty%% $file.shlexec", 0x0, 33},
985 {NULL, "%s/test file.shlexec", 0x0, 33},
987 /* Test filenames with no association */
988 {NULL, "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
990 /* Test double extensions */
991 {NULL, "%s\\test file.noassoc.shlexec", 0x0, 33},
992 {NULL, "%s\\test file.shlexec.noassoc", 0x0, SE_ERR_NOASSOC},
994 /* Test alternate verbs */
995 {"LowerL", "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
996 {"LowerL", "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
998 {"QuotedLowerL", "%s\\test file.shlexec", 0x0, 33},
999 {"QuotedUpperL", "%s\\test file.shlexec", 0x0, 33},
1001 {"notaverb", "%s\\test file.shlexec", 0x10, SE_ERR_NOASSOC},
1003 /* Test file masked due to space */
1004 {NULL, "%s\\masked file.shlexec", 0x0, 33},
1005 /* Test if quoting prevents the masking */
1006 {NULL, "%s\\masked file.shlexec", 0x40, 33},
1007 /* Test with incorrect quote */
1008 {NULL, "\"%s\\masked file.shlexec", 0x0, SE_ERR_FNF},
1010 {NULL, NULL, 0}
1013 static filename_tests_t noquotes_tests[]=
1015 /* Test unquoted '%1' thingies */
1016 {"NoQuotes", "%s\\test file.shlexec", 0xa, 33},
1017 {"LowerL", "%s\\test file.shlexec", 0xa, 33},
1018 {"UpperL", "%s\\test file.shlexec", 0xa, 33},
1020 {NULL, NULL, 0}
1023 static void test_lpFile_parsed(void)
1025 char fileA[MAX_PATH];
1026 INT_PTR rc;
1028 if (skip_shlexec_tests)
1030 skip("No filename parsing tests due to lack of .shlexec association\n");
1031 return;
1034 /* existing "drawback_file.noassoc" prevents finding "drawback_file.noassoc foo.shlexec" on wine */
1035 sprintf(fileA, "%s\\drawback_file.noassoc foo.shlexec", tmpdir);
1036 rc=shell_execute(NULL, fileA, NULL, NULL);
1037 okShell(rc > 32, "failed: rc=%lu\n", rc);
1039 /* if quoted, existing "drawback_file.noassoc" not prevents finding "drawback_file.noassoc foo.shlexec" on wine */
1040 sprintf(fileA, "\"%s\\drawback_file.noassoc foo.shlexec\"", tmpdir);
1041 rc=shell_execute(NULL, fileA, NULL, NULL);
1042 okShell(rc > 32 || broken(rc == SE_ERR_FNF) /* Win95/NT4 */,
1043 "failed: rc=%lu\n", rc);
1045 /* error should be SE_ERR_FNF, not SE_ERR_NOASSOC */
1046 sprintf(fileA, "\"%s\\drawback_file.noassoc\" foo.shlexec", tmpdir);
1047 rc=shell_execute(NULL, fileA, NULL, NULL);
1048 okShell(rc == SE_ERR_FNF, "returned %lu\n", rc);
1050 /* ""command"" not works on wine (and real win9x and w2k) */
1051 sprintf(fileA, "\"\"%s\\simple.shlexec\"\"", tmpdir);
1052 rc=shell_execute(NULL, fileA, NULL, NULL);
1053 todo_wine okShell(rc > 32 || broken(rc == SE_ERR_FNF) /* Win9x/2000 */,
1054 "failed: rc=%lu\n", rc);
1056 /* nonexisting "drawback_nonexist.noassoc" not prevents finding "drawback_nonexist.noassoc foo.shlexec" on wine */
1057 sprintf(fileA, "%s\\drawback_nonexist.noassoc foo.shlexec", tmpdir);
1058 rc=shell_execute(NULL, fileA, NULL, NULL);
1059 okShell(rc > 32, "failed: rc=%lu\n", rc);
1061 /* is SEE_MASK_DOENVSUBST default flag? Should only be when XP emulates 9x (XP bug or real 95 or ME behavior ?) */
1062 rc=shell_execute(NULL, "%TMPDIR%\\simple.shlexec", NULL, NULL);
1063 todo_wine okShell(rc == SE_ERR_FNF, "returned %lu\n", rc);
1065 /* quoted */
1066 rc=shell_execute(NULL, "\"%TMPDIR%\\simple.shlexec\"", NULL, NULL);
1067 todo_wine okShell(rc == SE_ERR_FNF, "returned %lu\n", rc);
1069 /* test SEE_MASK_DOENVSUBST works */
1070 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI,
1071 NULL, "%TMPDIR%\\simple.shlexec", NULL, NULL, NULL);
1072 okShell(rc > 32, "failed: rc=%lu\n", rc);
1074 /* quoted lpFile does not work on real win95 and nt4 */
1075 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI,
1076 NULL, "\"%TMPDIR%\\simple.shlexec\"", NULL, NULL, NULL);
1077 okShell(rc > 32 || broken(rc == SE_ERR_FNF) /* Win95/NT4 */,
1078 "failed: rc=%lu\n", rc);
1081 typedef struct
1083 const char* cmd;
1084 const char* args[11];
1085 int todo;
1086 } cmdline_tests_t;
1088 static const cmdline_tests_t cmdline_tests[] =
1090 {"exe",
1091 {"exe", NULL}, 0},
1093 {"exe arg1 arg2 \"arg three\" 'four five` six\\ $even)",
1094 {"exe", "arg1", "arg2", "arg three", "'four", "five`", "six\\", "$even)", NULL}, 0},
1096 {"exe arg=1 arg-2 three\tfour\rfour\nfour ",
1097 {"exe", "arg=1", "arg-2", "three", "four\rfour\nfour", NULL}, 0},
1099 {"exe arg\"one\" \"second\"arg thirdarg ",
1100 {"exe", "argone", "secondarg", "thirdarg", NULL}, 0},
1102 /* Don't lose unclosed quoted arguments */
1103 {"exe arg1 \"unclosed",
1104 {"exe", "arg1", "unclosed", NULL}, 0},
1106 {"exe arg1 \"",
1107 {"exe", "arg1", "", NULL}, 0},
1109 /* cmd's metacharacters have no special meaning */
1110 {"exe \"one^\" \"arg\"&two three|four",
1111 {"exe", "one^", "arg&two", "three|four", NULL}, 0},
1113 /* Environment variables are not interpreted either */
1114 {"exe %TMPDIR% %2",
1115 {"exe", "%TMPDIR%", "%2", NULL}, 0},
1117 /* If not followed by a quote, backslashes go through as is */
1118 {"exe o\\ne t\\\\wo t\\\\\\ree f\\\\\\\\our ",
1119 {"exe", "o\\ne", "t\\\\wo", "t\\\\\\ree", "f\\\\\\\\our", NULL}, 0},
1121 {"exe \"o\\ne\" \"t\\\\wo\" \"t\\\\\\ree\" \"f\\\\\\\\our\" ",
1122 {"exe", "o\\ne", "t\\\\wo", "t\\\\\\ree", "f\\\\\\\\our", NULL}, 0},
1124 /* When followed by a quote their number is halved and the remainder
1125 * escapes the quote
1127 {"exe \\\"one \\\\\"two\" \\\\\\\"three \\\\\\\\\"four\" end",
1128 {"exe", "\"one", "\\two", "\\\"three", "\\\\four", "end", NULL}, 0},
1130 {"exe \"one\\\" still\" \"two\\\\\" \"three\\\\\\\" still\" \"four\\\\\\\\\" end",
1131 {"exe", "one\" still", "two\\", "three\\\" still", "four\\\\", "end", NULL}, 0},
1133 /* One can put a quote in an unquoted string by tripling it, that is in
1134 * effect quoting it like so """ -> ". The general rule is as follows:
1135 * 3n quotes -> n quotes
1136 * 3n+1 quotes -> n quotes plus start of a quoted string
1137 * 3n+2 quotes -> n quotes (plus an empty string from the remaining pair)
1138 * Nicely, when n is 0 we get the standard rules back.
1140 {"exe two\"\"quotes next",
1141 {"exe", "twoquotes", "next", NULL}, 0},
1143 {"exe three\"\"\"quotes next",
1144 {"exe", "three\"quotes", "next", NULL}, 0},
1146 {"exe four\"\"\"\" quotes\" next 4%3=1",
1147 {"exe", "four\" quotes", "next", "4%3=1", NULL}, 0},
1149 {"exe five\"\"\"\"\"quotes next",
1150 {"exe", "five\"quotes", "next", NULL}, 0},
1152 {"exe six\"\"\"\"\"\"quotes next",
1153 {"exe", "six\"\"quotes", "next", NULL}, 0},
1155 {"exe seven\"\"\"\"\"\"\" quotes\" next 7%3=1",
1156 {"exe", "seven\"\" quotes", "next", "7%3=1", NULL}, 0},
1158 {"exe twelve\"\"\"\"\"\"\"\"\"\"\"\"quotes next",
1159 {"exe", "twelve\"\"\"\"quotes", "next", NULL}, 0},
1161 {"exe thirteen\"\"\"\"\"\"\"\"\"\"\"\"\" quotes\" next 13%3=1",
1162 {"exe", "thirteen\"\"\"\" quotes", "next", "13%3=1", NULL}, 0},
1164 /* Inside a quoted string the opening quote is added to the set of
1165 * consecutive quotes to get the effective quotes count. This gives:
1166 * 1+3n quotes -> n quotes
1167 * 1+3n+1 quotes -> n quotes plus closes the quoted string
1168 * 1+3n+2 quotes -> n+1 quotes plus closes the quoted string
1170 {"exe \"two\"\"quotes next",
1171 {"exe", "two\"quotes", "next", NULL}, 0},
1173 {"exe \"two\"\" next",
1174 {"exe", "two\"", "next", NULL}, 0},
1176 {"exe \"three\"\"\" quotes\" next 4%3=1",
1177 {"exe", "three\" quotes", "next", "4%3=1", NULL}, 0},
1179 {"exe \"four\"\"\"\"quotes next",
1180 {"exe", "four\"quotes", "next", NULL}, 0},
1182 {"exe \"five\"\"\"\"\"quotes next",
1183 {"exe", "five\"\"quotes", "next", NULL}, 0},
1185 {"exe \"six\"\"\"\"\"\" quotes\" next 7%3=1",
1186 {"exe", "six\"\" quotes", "next", "7%3=1", NULL}, 0},
1188 {"exe \"eleven\"\"\"\"\"\"\"\"\"\"\"quotes next",
1189 {"exe", "eleven\"\"\"\"quotes", "next", NULL}, 0},
1191 {"exe \"twelve\"\"\"\"\"\"\"\"\"\"\"\" quotes\" next 13%3=1",
1192 {"exe", "twelve\"\"\"\" quotes", "next", "13%3=1", NULL}, 0},
1194 /* Escaped consecutive quotes are fun */
1195 {"exe \"the crazy \\\\\"\"\"\\\\\" quotes",
1196 {"exe", "the crazy \\\"\\", "quotes", NULL}, 0},
1198 /* The executable path has its own rules!!!
1199 * - Backslashes have no special meaning.
1200 * - If the first character is a quote, then the second quote ends the
1201 * executable path.
1202 * - The previous rule holds even if the next character is not a space!
1203 * - If the first character is not a quote, then quotes have no special
1204 * meaning either and the executable path stops at the first space.
1205 * - The consecutive quotes rules don't apply either.
1206 * - Even if there is no space between the executable path and the first
1207 * argument, the latter is parsed using the regular rules.
1209 {"exe\"file\"path arg1",
1210 {"exe\"file\"path", "arg1", NULL}, 0},
1212 {"exe\"file\"path\targ1",
1213 {"exe\"file\"path", "arg1", NULL}, 0},
1215 {"exe\"path\\ arg1",
1216 {"exe\"path\\", "arg1", NULL}, 0},
1218 {"\\\"exe \"arg one\"",
1219 {"\\\"exe", "arg one", NULL}, 0},
1221 {"\"spaced exe\" \"next arg\"",
1222 {"spaced exe", "next arg", NULL}, 0},
1224 {"\"spaced exe\"\t\"next arg\"",
1225 {"spaced exe", "next arg", NULL}, 0},
1227 {"\"exe\"arg\" one\" argtwo",
1228 {"exe", "arg one", "argtwo", NULL}, 0},
1230 {"\"spaced exe\\\"arg1 arg2",
1231 {"spaced exe\\", "arg1", "arg2", NULL}, 0},
1233 {"\"two\"\" arg1 ",
1234 {"two", " arg1 ", NULL}, 0},
1236 {"\"three\"\"\" arg2",
1237 {"three", "", "arg2", NULL}, 0},
1239 {"\"four\"\"\"\"arg1",
1240 {"four", "\"arg1", NULL}, 0},
1242 /* If the first character is a space then the executable path is empty */
1243 {" \"arg\"one argtwo",
1244 {"", "argone", "argtwo", NULL}, 0},
1246 {NULL, {NULL}, 0}
1249 static BOOL test_one_cmdline(const cmdline_tests_t* test)
1251 WCHAR cmdW[MAX_PATH], argW[MAX_PATH];
1252 LPWSTR *cl2a;
1253 int cl2a_count;
1254 LPWSTR *argsW;
1255 int i, count;
1257 /* trace("----- cmd='%s'\n", test->cmd); */
1258 MultiByteToWideChar(CP_ACP, 0, test->cmd, -1, cmdW, sizeof(cmdW)/sizeof(*cmdW));
1259 argsW = cl2a = CommandLineToArgvW(cmdW, &cl2a_count);
1260 if (argsW == NULL && cl2a_count == -1)
1262 win_skip("CommandLineToArgvW not implemented, skipping\n");
1263 return FALSE;
1265 ok(!argsW[cl2a_count] || broken(argsW[cl2a_count] != NULL) /* before Vista */,
1266 "expected NULL-terminated list of commandline arguments\n");
1268 count = 0;
1269 while (test->args[count])
1270 count++;
1271 if ((test->todo & 0x1) == 0)
1272 ok(cl2a_count == count, "%s: expected %d arguments, but got %d\n", test->cmd, count, cl2a_count);
1273 else todo_wine
1274 ok(cl2a_count == count, "%s: expected %d arguments, but got %d\n", test->cmd, count, cl2a_count);
1276 for (i = 0; i < cl2a_count; i++)
1278 if (i < count)
1280 MultiByteToWideChar(CP_ACP, 0, test->args[i], -1, argW, sizeof(argW)/sizeof(*argW));
1281 if ((test->todo & (1 << (i+4))) == 0)
1282 ok(!lstrcmpW(*argsW, argW), "%s: arg[%d] expected %s but got %s\n", test->cmd, i, wine_dbgstr_w(argW), wine_dbgstr_w(*argsW));
1283 else todo_wine
1284 ok(!lstrcmpW(*argsW, argW), "%s: arg[%d] expected %s but got %s\n", test->cmd, i, wine_dbgstr_w(argW), wine_dbgstr_w(*argsW));
1286 else if ((test->todo & 0x1) == 0)
1287 ok(0, "%s: got extra arg[%d]=%s\n", test->cmd, i, wine_dbgstr_w(*argsW));
1288 else todo_wine
1289 ok(0, "%s: got extra arg[%d]=%s\n", test->cmd, i, wine_dbgstr_w(*argsW));
1290 argsW++;
1292 LocalFree(cl2a);
1293 return TRUE;
1296 static void test_commandline2argv(void)
1298 static const WCHAR exeW[] = {'e','x','e',0};
1299 const cmdline_tests_t* test;
1300 WCHAR strW[MAX_PATH];
1301 LPWSTR *args;
1302 int numargs;
1303 DWORD le;
1305 test = cmdline_tests;
1306 while (test->cmd)
1308 if (!test_one_cmdline(test))
1309 return;
1310 test++;
1313 SetLastError(0xdeadbeef);
1314 args = CommandLineToArgvW(exeW, NULL);
1315 le = GetLastError();
1316 ok(args == NULL && le == ERROR_INVALID_PARAMETER, "expected NULL with ERROR_INVALID_PARAMETER got %p with %u\n", args, le);
1318 SetLastError(0xdeadbeef);
1319 args = CommandLineToArgvW(NULL, NULL);
1320 le = GetLastError();
1321 ok(args == NULL && le == ERROR_INVALID_PARAMETER, "expected NULL with ERROR_INVALID_PARAMETER got %p with %u\n", args, le);
1323 *strW = 0;
1324 args = CommandLineToArgvW(strW, &numargs);
1325 ok(numargs == 1 || broken(numargs > 1), "expected 1 args, got %d\n", numargs);
1326 ok(!args || (!args[numargs] || broken(args[numargs] != NULL) /* before Vista */),
1327 "expected NULL-terminated list of commandline arguments\n");
1328 if (numargs == 1)
1330 GetModuleFileNameW(NULL, strW, sizeof(strW)/sizeof(*strW));
1331 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));
1333 if (args) LocalFree(args);
1336 /* The goal here is to analyze how ShellExecute() builds the command that
1337 * will be run. The tricky part is that there are three transformation
1338 * steps between the 'parameters' string we pass to ShellExecute() and the
1339 * argument list we observe in the child process:
1340 * - The parsing of 'parameters' string into individual arguments. The tests
1341 * show this is done differently from both CreateProcess() and
1342 * CommandLineToArgv()!
1343 * - The way the command 'formatting directives' such as %1, %2, etc are
1344 * handled.
1345 * - And the way the resulting command line is then parsed to yield the
1346 * argument list we check.
1348 typedef struct
1350 const char* verb;
1351 const char* params;
1352 int todo;
1353 cmdline_tests_t cmd;
1354 cmdline_tests_t broken;
1355 } argify_tests_t;
1357 static const argify_tests_t argify_tests[] =
1359 /* Start with three simple parameters. Notice that one can reorder and
1360 * duplicate the parameters. Also notice how %* take the raw input
1361 * parameters string, including the trailing spaces, no matter what
1362 * arguments have already been used.
1364 {"Params232S", "p2 p3 p4 ", 0xc2,
1365 {" p2 p3 \"p2\" \"p2 p3 p4 \"",
1366 {"", "p2", "p3", "p2", "p2 p3 p4 ", NULL}, 0}},
1368 /* Unquoted argument references like %2 don't automatically quote their
1369 * argument. Similarly, when they are quoted they don't escape the quotes
1370 * that their argument may contain.
1372 {"Params232S", "\"p two\" p3 p4 ", 0x3f3,
1373 {" p two p3 \"p two\" \"\"p two\" p3 p4 \"",
1374 {"", "p", "two", "p3", "p two", "p", "two p3 p4 ", NULL}, 0}},
1376 /* Only single digits are supported so only %1 to %9. Shown here with %20
1377 * because %10 is a pain.
1379 {"Params20", "p", 0,
1380 {" \"p0\"",
1381 {"", "p0", NULL}, 0}},
1383 /* Only (double-)quotes have a special meaning. */
1384 {"Params23456", "'p2 p3` p4\\ $even", 0x40,
1385 {" \"'p2\" \"p3`\" \"p4\\\" \"$even\" \"\"",
1386 {"", "'p2", "p3`", "p4\" $even \"", NULL}, 0}},
1388 {"Params23456", "p=2 p-3 p4\tp4\rp4\np4", 0x1c2,
1389 {" \"p=2\" \"p-3\" \"p4\tp4\rp4\np4\" \"\" \"\"",
1390 {"", "p=2", "p-3", "p4\tp4\rp4\np4", "", "", NULL}, 0}},
1392 /* In unquoted strings, quotes are treated are a parameter separator just
1393 * like spaces! However they can be doubled to get a literal quote.
1394 * Specifically:
1395 * 2n quotes -> n quotes
1396 * 2n+1 quotes -> n quotes and a parameter separator
1398 {"Params23456789", "one\"quote \"p four\" one\"quote p7", 0xff3,
1399 {" \"one\" \"quote\" \"p four\" \"one\" \"quote\" \"p7\" \"\" \"\"",
1400 {"", "one", "quote", "p four", "one", "quote", "p7", "", "", NULL}, 0}},
1402 {"Params23456789", "two\"\"quotes \"p three\" two\"\"quotes p5", 0xf2,
1403 {" \"two\"quotes\" \"p three\" \"two\"quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1404 {"", "twoquotes p", "three twoquotes", "p5", "", "", "", "", NULL}, 0}},
1406 {"Params23456789", "three\"\"\"quotes \"p four\" three\"\"\"quotes p6", 0xff3,
1407 {" \"three\"\" \"quotes\" \"p four\" \"three\"\" \"quotes\" \"p6\" \"\" \"\"",
1408 {"", "three\"", "quotes", "p four", "three\"", "quotes", "p6", "", "", NULL}, 0}},
1410 {"Params23456789", "four\"\"\"\"quotes \"p three\" four\"\"\"\"quotes p5", 0xf3,
1411 {" \"four\"\"quotes\" \"p three\" \"four\"\"quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1412 {"", "four\"quotes p", "three fourquotes p5 \"", "", "", "", NULL}, 0}},
1414 /* Quoted strings cannot be continued by tacking on a non space character
1415 * either.
1417 {"Params23456", "\"p two\"p3 \"p four\"p5 p6", 0x1f3,
1418 {" \"p two\" \"p3\" \"p four\" \"p5\" \"p6\"",
1419 {"", "p two", "p3", "p four", "p5", "p6", NULL}, 0}},
1421 /* In quoted strings, the quotes are halved and an odd number closes the
1422 * string. Specifically:
1423 * 2n quotes -> n quotes
1424 * 2n+1 quotes -> n quotes and closes the string and hence the parameter
1426 {"Params23456789", "\"one q\"uote \"p four\" \"one q\"uote p7", 0xff3,
1427 {" \"one q\" \"uote\" \"p four\" \"one q\" \"uote\" \"p7\" \"\" \"\"",
1428 {"", "one q", "uote", "p four", "one q", "uote", "p7", "", "", NULL}, 0}},
1430 {"Params23456789", "\"two \"\" quotes\" \"p three\" \"two \"\" quotes\" p5", 0x1ff3,
1431 {" \"two \" quotes\" \"p three\" \"two \" quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1432 {"", "two ", "quotes p", "three two", " quotes", "p5", "", "", "", "", NULL}, 0}},
1434 {"Params23456789", "\"three q\"\"\"uotes \"p four\" \"three q\"\"\"uotes p7", 0xff3,
1435 {" \"three q\"\" \"uotes\" \"p four\" \"three q\"\" \"uotes\" \"p7\" \"\" \"\"",
1436 {"", "three q\"", "uotes", "p four", "three q\"", "uotes", "p7", "", "", NULL}, 0}},
1438 {"Params23456789", "\"four \"\"\"\" quotes\" \"p three\" \"four \"\"\"\" quotes\" p5", 0xff3,
1439 {" \"four \"\" quotes\" \"p three\" \"four \"\" quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1440 {"", "four \"", "quotes p", "three four", "", "quotes p5 \"", "", "", "", NULL}, 0}},
1442 /* The quoted string rules also apply to consecutive quotes at the start
1443 * of a parameter but don't count the opening quote!
1445 {"Params23456789", "\"\"twoquotes \"p four\" \"\"twoquotes p7", 0xbf3,
1446 {" \"\" \"twoquotes\" \"p four\" \"\" \"twoquotes\" \"p7\" \"\" \"\"",
1447 {"", "", "twoquotes", "p four", "", "twoquotes", "p7", "", "", NULL}, 0}},
1449 {"Params23456789", "\"\"\"three quotes\" \"p three\" \"\"\"three quotes\" p5", 0x6f3,
1450 {" \"\"three quotes\" \"p three\" \"\"three quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1451 {"", "three", "quotes p", "three \"three", "quotes p5 \"", "", "", "", NULL}, 0}},
1453 {"Params23456789", "\"\"\"\"fourquotes \"p four\" \"\"\"\"fourquotes p7", 0xbf3,
1454 {" \"\"\" \"fourquotes\" \"p four\" \"\"\" \"fourquotes\" \"p7\" \"\" \"\"",
1455 {"", "\"", "fourquotes", "p four", "\"", "fourquotes", "p7", "", "", NULL}, 0}},
1457 /* An unclosed quoted string gets lost! */
1458 {"Params23456", "p2 \"p3\" \"p4 is lost", 0x1c3,
1459 {" \"p2\" \"p3\" \"\" \"\" \"\"",
1460 {"", "p2", "p3", "", "", "", NULL}, 0},
1461 {" \"p2\" \"p3\" \"p3\" \"\" \"\"",
1462 {"", "p2", "p3", "p3", "", "", NULL}, 0}},
1464 /* Backslashes have no special meaning even when preceding quotes. All
1465 * they do is start an unquoted string.
1467 {"Params23456", "\\\"p\\three \"pfour\\\" pfive", 0x73,
1468 {" \"\\\" \"p\\three\" \"pfour\\\" \"pfive\" \"\"",
1469 {"", "\" p\\three pfour\"", "pfive", "", NULL}, 0}},
1471 /* Environment variables are left untouched. */
1472 {"Params23456", "%TMPDIR% %t %c", 0,
1473 {" \"%TMPDIR%\" \"%t\" \"%c\" \"\" \"\"",
1474 {"", "%TMPDIR%", "%t", "%c", "", "", NULL}, 0}},
1476 /* %~2 is equivalent to %*. However %~3 and higher include the spaces
1477 * before the parameter!
1478 * (but not the previous parameter's closing quote fortunately)
1480 {"Params2345Etc", "p2 p3 \"p4\" p5 p6 ", 0x3f3,
1481 {" ~2=\"p2 p3 \"p4\" p5 p6 \" ~3=\" p3 \"p4\" p5 p6 \" ~4=\" \"p4\" p5 p6 \" ~5= p5 p6 ",
1482 {"", "~2=p2 p3 p4 p5 p6 ", "~3= p3 p4 p5 p6 ", "~4= p4 p5 p6 ", "~5=", "p5", "p6", NULL}, 0}},
1484 /* %~n works even if there is no nth parameter. */
1485 {"Params9Etc", "p2 p3 p4 p5 p6 p7 p8 ", 0x12,
1486 {" ~9=\" \"",
1487 {"", "~9= ", NULL}, 0}},
1489 {"Params9Etc", "p2 p3 p4 p5 p6 p7 ", 0x12,
1490 {" ~9=\"\"",
1491 {"", "~9=", NULL}, 0}},
1493 /* The %~n directives also transmit the tenth parameter and beyond. */
1494 {"Params9Etc", "p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 and beyond!", 0x12,
1495 {" ~9=\" p9 p10 p11 and beyond!\"",
1496 {"", "~9= p9 p10 p11 and beyond!", NULL}, 0}},
1498 /* Bad formatting directives lose their % sign, except those followed by
1499 * a tilde! Environment variables are not expanded but lose their % sign.
1501 {"ParamsBad", "p2 p3 p4 p5", 0x12,
1502 {" \"% - %~ %~0 %~1 %~a %~* a b c TMPDIR\"",
1503 {"", "% - %~ %~0 %~1 %~a %~* a b c TMPDIR", NULL}, 0}},
1505 {NULL, NULL, 0, {NULL, {NULL}, 0}}
1508 static void test_argify(void)
1510 BOOL has_cl2a = TRUE;
1511 char fileA[MAX_PATH], params[2*MAX_PATH+12];
1512 INT_PTR rc;
1513 const argify_tests_t* test;
1514 const cmdline_tests_t *bad;
1515 const char* cmd;
1516 unsigned i, count;
1518 if (skip_shlexec_tests)
1520 skip("No argify tests due to lack of .shlexec association\n");
1521 return;
1524 create_test_verb(".shlexec", "Params232S", 0, "Params232S %2 %3 \"%2\" \"%*\"");
1525 create_test_verb(".shlexec", "Params23456", 0, "Params23456 \"%2\" \"%3\" \"%4\" \"%5\" \"%6\"");
1526 create_test_verb(".shlexec", "Params23456789", 0, "Params23456789 \"%2\" \"%3\" \"%4\" \"%5\" \"%6\" \"%7\" \"%8\" \"%9\"");
1527 create_test_verb(".shlexec", "Params2345Etc", 0, "Params2345Etc ~2=\"%~2\" ~3=\"%~3\" ~4=\"%~4\" ~5=%~5");
1528 create_test_verb(".shlexec", "Params9Etc", 0, "Params9Etc ~9=\"%~9\"");
1529 create_test_verb(".shlexec", "Params20", 0, "Params20 \"%20\"");
1530 create_test_verb(".shlexec", "ParamsBad", 0, "ParamsBad \"%% %- %~ %~0 %~1 %~a %~* %a %b %c %TMPDIR%\"");
1532 sprintf(fileA, "%s\\test file.shlexec", tmpdir);
1534 test = argify_tests;
1535 while (test->params)
1537 bad = test->broken.cmd ? &test->broken : &test->cmd;
1539 /* trace("***** verb='%s' params='%s'\n", test->verb, test->params); */
1540 rc = shell_execute_ex(SEE_MASK_DOENVSUBST, test->verb, fileA, test->params, NULL, NULL);
1541 okShell(rc > 32, "failed: rc=%lu\n", rc);
1543 count = 0;
1544 while (test->cmd.args[count])
1545 count++;
1546 if ((test->todo & 0x1) == 0)
1547 /* +4 for the shlexec arguments, -1 because of the added ""
1548 * argument for the CommandLineToArgvW() tests.
1550 okChildInt("argcA", 4 + count - 1);
1551 else todo_wine
1552 okChildInt("argcA", 4 + count - 1);
1554 cmd = getChildString("Arguments", "cmdlineA");
1555 /* Our commands are such that the verb immediately precedes the
1556 * part we are interested in.
1558 if (cmd) cmd = strstr(cmd, test->verb);
1559 if (cmd) cmd += strlen(test->verb);
1560 if (!cmd) cmd = "(null)";
1561 if ((test->todo & 0x2) == 0)
1562 okShell(!strcmp(cmd, test->cmd.cmd) || broken(!strcmp(cmd, bad->cmd)),
1563 "the cmdline is '%s' instead of '%s'\n", cmd, test->cmd.cmd);
1564 else todo_wine
1565 okShell(!strcmp(cmd, test->cmd.cmd) || broken(!strcmp(cmd, bad->cmd)),
1566 "the cmdline is '%s' instead of '%s'\n", cmd, test->cmd.cmd);
1568 for (i = 0; i < count - 1; i++)
1570 char argname[18];
1571 sprintf(argname, "argvA%d", 4 + i);
1572 if ((test->todo & (1 << (i+4))) == 0)
1573 okChildStringBroken(argname, test->cmd.args[i+1], bad->args[i+1]);
1574 else todo_wine
1575 okChildStringBroken(argname, test->cmd.args[i+1], bad->args[i+1]);
1578 if (has_cl2a)
1579 has_cl2a = test_one_cmdline(&(test->cmd));
1580 test++;
1583 /* Test with a long parameter */
1584 for (rc = 0; rc < MAX_PATH; rc++)
1585 fileA[rc] = 'a' + rc % 26;
1586 fileA[MAX_PATH-1] = '\0';
1587 sprintf(params, "shlexec \"%s\" %s", child_file, fileA);
1589 /* We need NOZONECHECKS on Win2003 to block a dialog */
1590 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params, NULL, NULL);
1591 okShell(rc > 32, "failed: rc=%lu\n", rc);
1592 okChildInt("argcA", 4);
1593 okChildPath("argvA3", fileA);
1596 static void test_filename(void)
1598 char filename[MAX_PATH];
1599 const filename_tests_t* test;
1600 char* c;
1601 INT_PTR rc;
1603 if (skip_shlexec_tests)
1605 skip("No ShellExecute/filename tests due to lack of .shlexec association\n");
1606 return;
1609 test=filename_tests;
1610 while (test->basename)
1612 BOOL quotedfile = FALSE;
1614 if (skip_noassoc_tests && test->rc == SE_ERR_NOASSOC)
1616 win_skip("Skipping shellexecute of file with unassociated extension\n");
1617 test++;
1618 continue;
1621 sprintf(filename, test->basename, tmpdir);
1622 if (strchr(filename, '/'))
1624 c=filename;
1625 while (*c)
1627 if (*c=='\\')
1628 *c='/';
1629 c++;
1632 if ((test->todo & 0x40)==0)
1634 rc=shell_execute(test->verb, filename, NULL, NULL);
1636 else
1638 char quoted[MAX_PATH + 2];
1640 quotedfile = TRUE;
1641 sprintf(quoted, "\"%s\"", filename);
1642 rc=shell_execute(test->verb, quoted, NULL, NULL);
1644 if (rc > 32)
1645 rc=33;
1646 okShell(rc==test->rc ||
1647 broken(quotedfile && rc == SE_ERR_FNF), /* NT4 */
1648 "failed: rc=%ld err=%u\n", rc, GetLastError());
1649 if (rc == 33)
1651 const char* verb;
1652 if ((test->todo & 0x2)==0)
1654 okChildInt("argcA", 5);
1656 else todo_wine
1658 okChildInt("argcA", 5);
1660 verb=(test->verb ? test->verb : "Open");
1661 if ((test->todo & 0x4)==0)
1663 okChildString("argvA3", verb);
1665 else todo_wine
1667 okChildString("argvA3", verb);
1669 if ((test->todo & 0x8)==0)
1671 okChildPath("argvA4", filename);
1673 else todo_wine
1675 okChildPath("argvA4", filename);
1678 test++;
1681 test=noquotes_tests;
1682 while (test->basename)
1684 sprintf(filename, test->basename, tmpdir);
1685 rc=shell_execute(test->verb, filename, NULL, NULL);
1686 if (rc > 32)
1687 rc=33;
1688 if ((test->todo & 0x1)==0)
1690 okShell(rc==test->rc, "failed: rc=%ld err=%u\n", rc, GetLastError());
1692 else todo_wine
1694 okShell(rc==test->rc, "failed: rc=%ld err=%u\n", rc, GetLastError());
1696 if (rc==0)
1698 int count;
1699 const char* verb;
1700 char* str;
1702 verb=(test->verb ? test->verb : "Open");
1703 if ((test->todo & 0x4)==0)
1705 okChildString("argvA3", verb);
1707 else todo_wine
1709 okChildString("argvA3", verb);
1712 count=4;
1713 str=filename;
1714 while (1)
1716 char attrib[18];
1717 char* space;
1718 space=strchr(str, ' ');
1719 if (space)
1720 *space='\0';
1721 sprintf(attrib, "argvA%d", count);
1722 if ((test->todo & 0x8)==0)
1724 okChildPath(attrib, str);
1726 else todo_wine
1728 okChildPath(attrib, str);
1730 count++;
1731 if (!space)
1732 break;
1733 str=space+1;
1735 if ((test->todo & 0x2)==0)
1737 okChildInt("argcA", count);
1739 else todo_wine
1741 okChildInt("argcA", count);
1744 test++;
1747 if (dllver.dwMajorVersion != 0)
1749 /* The more recent versions of shell32.dll accept quoted filenames
1750 * while older ones (e.g. 4.00) don't. Still we want to test this
1751 * because IE 6 depends on the new behavior.
1752 * One day we may need to check the exact version of the dll but for
1753 * now making sure DllGetVersion() is present is sufficient.
1755 sprintf(filename, "\"%s\\test file.shlexec\"", tmpdir);
1756 rc=shell_execute(NULL, filename, NULL, NULL);
1757 okShell(rc > 32, "failed: rc=%ld err=%u\n", rc, GetLastError());
1758 okChildInt("argcA", 5);
1759 okChildString("argvA3", "Open");
1760 sprintf(filename, "%s\\test file.shlexec", tmpdir);
1761 okChildPath("argvA4", filename);
1765 typedef struct
1767 const char* urlprefix;
1768 const char* basename;
1769 int flags;
1770 int todo;
1771 } fileurl_tests_t;
1773 #define URL_SUCCESS 0x1
1774 #define USE_COLON 0x2
1775 #define USE_BSLASH 0x4
1777 static fileurl_tests_t fileurl_tests[]=
1779 /* How many slashes does it take... */
1780 {"file:", "%s\\test file.shlexec", URL_SUCCESS, 0},
1781 {"file:/", "%s\\test file.shlexec", URL_SUCCESS, 0},
1782 {"file://", "%s\\test file.shlexec", URL_SUCCESS, 0},
1783 {"file:///", "%s\\test file.shlexec", URL_SUCCESS, 0},
1784 {"File:///", "%s\\test file.shlexec", URL_SUCCESS, 0},
1785 {"file:////", "%s\\test file.shlexec", URL_SUCCESS, 0},
1786 {"file://///", "%s\\test file.shlexec", 0, 0},
1788 /* Test with Windows-style paths */
1789 {"file:///", "%s\\test file.shlexec", URL_SUCCESS | USE_COLON, 0},
1790 {"file:///", "%s\\test file.shlexec", URL_SUCCESS | USE_BSLASH, 0},
1792 /* Check handling of hostnames */
1793 {"file://localhost/", "%s\\test file.shlexec", URL_SUCCESS, 0},
1794 {"file://localhost:80/", "%s\\test file.shlexec", 0, 0},
1795 {"file://LocalHost/", "%s\\test file.shlexec", URL_SUCCESS, 0},
1796 {"file://127.0.0.1/", "%s\\test file.shlexec", 0, 0},
1797 {"file://::1/", "%s\\test file.shlexec", 0, 0},
1798 {"file://notahost/", "%s\\test file.shlexec", 0, 0},
1800 /* Environment variables are not expanded in URLs */
1801 {"%urlprefix%", "%s\\test file.shlexec", 0, 0x1},
1802 {"file:///", "%%TMPDIR%%\\test file.shlexec", 0, 0},
1804 /* Test shortcuts vs. URLs */
1805 {"file://///", "%s\\test_shortcut_shlexec.lnk", 0, 0x1d},
1807 {NULL, NULL, 0, 0}
1810 static void test_fileurls(void)
1812 char filename[MAX_PATH], fileurl[MAX_PATH], longtmpdir[MAX_PATH];
1813 char command[MAX_PATH];
1814 const fileurl_tests_t* test;
1815 char *s;
1816 INT_PTR rc;
1818 if (skip_shlexec_tests)
1820 skip("No file URL tests due to lack of .shlexec association\n");
1821 return;
1824 rc = (INT_PTR)ShellExecuteA(NULL, NULL, "file:///nosuchfile.shlexec", NULL, NULL, SW_SHOWNORMAL);
1825 if (rc > 32)
1827 win_skip("shell32 is too old (likely < 4.72). Skipping the file URL tests\n");
1828 return;
1831 get_long_path_name(tmpdir, longtmpdir, sizeof(longtmpdir)/sizeof(*longtmpdir));
1832 SetEnvironmentVariableA("urlprefix", "file:///");
1834 test=fileurl_tests;
1835 while (test->basename)
1837 /* Build the file URL */
1838 sprintf(filename, test->basename, longtmpdir);
1839 strcpy(fileurl, test->urlprefix);
1840 strcat(fileurl, filename);
1841 s = fileurl + strlen(test->urlprefix);
1842 while (*s)
1844 if (!(test->flags & USE_COLON) && *s == ':')
1845 *s = '|';
1846 else if (!(test->flags & USE_BSLASH) && *s == '\\')
1847 *s = '/';
1848 s++;
1851 /* Test it first with FindExecutable() */
1852 rc = (INT_PTR)FindExecutableA(fileurl, NULL, command);
1853 ok(rc == SE_ERR_FNF, "FindExecutable(%s) failed: bad rc=%lu\n", fileurl, rc);
1855 /* Then ShellExecute() */
1856 if ((test->todo & 0x10) == 0)
1857 rc = shell_execute(NULL, fileurl, NULL, NULL);
1858 else todo_wait
1859 rc = shell_execute(NULL, fileurl, NULL, NULL);
1860 if (bad_shellexecute)
1862 win_skip("shell32 is too old (likely 4.72). Skipping the file URL tests\n");
1863 break;
1865 if (test->flags & URL_SUCCESS)
1867 if ((test->todo & 0x1) == 0)
1868 okShell(rc > 32, "failed: bad rc=%lu\n", rc);
1869 else todo_wine
1870 okShell(rc > 32, "failed: bad rc=%lu\n", rc);
1872 else
1874 if ((test->todo & 0x1) == 0)
1875 okShell(rc == SE_ERR_FNF || rc == SE_ERR_PNF ||
1876 broken(rc == SE_ERR_ACCESSDENIED) /* win2000 */,
1877 "failed: bad rc=%lu\n", rc);
1878 else todo_wine
1879 okShell(rc == SE_ERR_FNF || rc == SE_ERR_PNF ||
1880 broken(rc == SE_ERR_ACCESSDENIED) /* win2000 */,
1881 "failed: bad rc=%lu\n", rc);
1883 if (rc == 33)
1885 if ((test->todo & 0x2) == 0)
1886 okChildInt("argcA", 5);
1887 else todo_wine
1888 okChildInt("argcA", 5);
1890 if ((test->todo & 0x4) == 0)
1891 okChildString("argvA3", "Open");
1892 else todo_wine
1893 okChildString("argvA3", "Open");
1895 if ((test->todo & 0x8) == 0)
1896 okChildPath("argvA4", filename);
1897 else todo_wine
1898 okChildPath("argvA4", filename);
1900 test++;
1903 SetEnvironmentVariableA("urlprefix", NULL);
1906 static void test_find_executable(void)
1908 char notepad_path[MAX_PATH];
1909 char filename[MAX_PATH];
1910 char command[MAX_PATH];
1911 const filename_tests_t* test;
1912 INT_PTR rc;
1914 if (!create_test_association(".sfe"))
1916 skip("Unable to create association for '.sfe'\n");
1917 return;
1919 create_test_verb(".sfe", "Open", 1, "%1");
1921 /* Don't test FindExecutable(..., NULL), it always crashes */
1923 strcpy(command, "your word");
1924 if (0) /* Can crash on Vista! */
1926 rc=(INT_PTR)FindExecutableA(NULL, NULL, command);
1927 ok(rc == SE_ERR_FNF || rc > 32 /* nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
1928 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
1931 GetSystemDirectoryA( notepad_path, MAX_PATH );
1932 strcat( notepad_path, "\\notepad.exe" );
1934 /* Search for something that should be in the system-wide search path (no default directory) */
1935 strcpy(command, "your word");
1936 rc=(INT_PTR)FindExecutableA("notepad.exe", NULL, command);
1937 ok(rc > 32, "FindExecutable(%s) returned %ld\n", "notepad.exe", rc);
1938 ok(strcasecmp(command, notepad_path) == 0, "FindExecutable(%s) returned command=[%s]\n", "notepad.exe", command);
1940 /* Search for something that should be in the system-wide search path (with default directory) */
1941 strcpy(command, "your word");
1942 rc=(INT_PTR)FindExecutableA("notepad.exe", tmpdir, command);
1943 ok(rc > 32, "FindExecutable(%s) returned %ld\n", "notepad.exe", rc);
1944 ok(strcasecmp(command, notepad_path) == 0, "FindExecutable(%s) returned command=[%s]\n", "notepad.exe", command);
1946 strcpy(command, "your word");
1947 rc=(INT_PTR)FindExecutableA(tmpdir, NULL, command);
1948 ok(rc == SE_ERR_NOASSOC /* >= win2000 */ || rc > 32 /* win98, nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
1949 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
1951 sprintf(filename, "%s\\test file.sfe", tmpdir);
1952 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1953 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1954 /* Depending on the platform, command could be '%1' or 'test file.sfe' */
1956 rc=(INT_PTR)FindExecutableA("test file.sfe", tmpdir, command);
1957 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1959 rc=(INT_PTR)FindExecutableA("test file.sfe", NULL, command);
1960 ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %ld\n", filename, rc);
1962 delete_test_association(".sfe");
1964 if (!create_test_association(".shl"))
1966 skip("Unable to create association for '.shl'\n");
1967 return;
1969 create_test_verb(".shl", "Open", 0, "Open");
1971 sprintf(filename, "%s\\test file.shl", tmpdir);
1972 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1973 ok(rc == SE_ERR_FNF /* NT4 */ || rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1975 sprintf(filename, "%s\\test file.shlfoo", tmpdir);
1976 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1978 delete_test_association(".shl");
1980 if (rc > 32)
1982 /* On Windows XP and 2003 FindExecutable() is completely broken.
1983 * Probably what it does is convert the filename to 8.3 format,
1984 * which as a side effect converts the '.shlfoo' extension to '.shl',
1985 * and then tries to find an association for '.shl'. This means it
1986 * will normally fail on most extensions with more than 3 characters,
1987 * like '.mpeg', etc.
1988 * Also it means we cannot do any other test.
1990 win_skip("FindExecutable() is broken -> not running 4+ character extension tests\n");
1991 return;
1994 if (skip_shlexec_tests)
1996 skip("No FindExecutable/filename tests due to lack of .shlexec association\n");
1997 return;
2000 test=filename_tests;
2001 while (test->basename)
2003 sprintf(filename, test->basename, tmpdir);
2004 if (strchr(filename, '/'))
2006 char* c;
2007 c=filename;
2008 while (*c)
2010 if (*c=='\\')
2011 *c='/';
2012 c++;
2015 /* Win98 does not '\0'-terminate command! */
2016 memset(command, '\0', sizeof(command));
2017 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
2018 if (rc > 32)
2019 rc=33;
2020 if ((test->todo & 0x10)==0)
2022 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
2024 else todo_wine
2026 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
2028 if (rc > 32)
2030 BOOL equal;
2031 equal=strcmp(command, argv0) == 0 ||
2032 /* NT4 returns an extra 0x8 character! */
2033 (strlen(command) == strlen(argv0)+1 && strncmp(command, argv0, strlen(argv0)) == 0);
2034 if ((test->todo & 0x20)==0)
2036 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
2037 filename, command, argv0);
2039 else todo_wine
2041 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
2042 filename, command, argv0);
2045 test++;
2050 static filename_tests_t lnk_tests[]=
2052 /* Pass bad / nonexistent filenames as a parameter */
2053 {NULL, "%s\\nonexistent.shlexec", 0xa, 33},
2054 {NULL, "%s\\nonexistent.noassoc", 0xa, 33},
2056 /* Pass regular paths as a parameter */
2057 {NULL, "%s\\test file.shlexec", 0xa, 33},
2058 {NULL, "%s/%%nasty%% $file.shlexec", 0xa, 33},
2060 /* Pass filenames with no association as a parameter */
2061 {NULL, "%s\\test file.noassoc", 0xa, 33},
2063 {NULL, NULL, 0}
2066 static void test_lnks(void)
2068 char filename[MAX_PATH];
2069 char params[MAX_PATH];
2070 const filename_tests_t* test;
2071 INT_PTR rc;
2073 if (skip_shlexec_tests)
2074 skip("No FindExecutable/filename tests due to lack of .shlexec association\n");
2075 else
2077 /* Should open through our association */
2078 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
2079 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
2080 okShell(rc > 32, "failed: rc=%lu err=%u\n", rc, GetLastError());
2081 okChildInt("argcA", 5);
2082 okChildString("argvA3", "Open");
2083 sprintf(params, "%s\\test file.shlexec", tmpdir);
2084 get_long_path_name(params, filename, sizeof(filename));
2085 okChildPath("argvA4", filename);
2087 todo_wait rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_DOENVSUBST, NULL, "%TMPDIR%\\test_shortcut_shlexec.lnk", NULL, NULL, NULL);
2088 okShell(rc > 32, "failed: rc=%lu err=%u\n", rc, GetLastError());
2089 okChildInt("argcA", 5);
2090 todo_wine okChildString("argvA3", "Open");
2091 sprintf(params, "%s\\test file.shlexec", tmpdir);
2092 get_long_path_name(params, filename, sizeof(filename));
2093 todo_wine okChildPath("argvA4", filename);
2096 /* Should just run our executable */
2097 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2098 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
2099 okShell(rc > 32, "failed: rc=%lu err=%u\n", rc, GetLastError());
2100 okChildInt("argcA", 4);
2101 okChildString("argvA3", "Lnk");
2103 /* An explicit class overrides lnk's ContextMenuHandler */
2104 rc=shell_execute_ex(SEE_MASK_CLASSNAME | SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, "shlexec.shlexec");
2105 okShell(rc > 32, "failed: rc=%lu err=%u\n", rc, GetLastError());
2106 okChildInt("argcA", 5);
2107 okChildString("argvA3", "Open");
2108 okChildPath("argvA4", filename);
2110 if (dllver.dwMajorVersion>=6)
2112 char* c;
2113 /* Recent versions of shell32.dll accept '/'s in shortcut paths.
2114 * Older versions don't or are quite buggy in this regard.
2116 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2117 c=filename;
2118 while (*c)
2120 if (*c=='\\')
2121 *c='/';
2122 c++;
2124 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
2125 okShell(rc > 32, "failed: rc=%lu err=%u\n", rc, GetLastError());
2126 okChildInt("argcA", 4);
2127 okChildString("argvA3", "Lnk");
2130 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2131 test=lnk_tests;
2132 while (test->basename)
2134 params[0]='\"';
2135 sprintf(params+1, test->basename, tmpdir);
2136 strcat(params,"\"");
2137 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, params,
2138 NULL, NULL);
2139 if (rc > 32)
2140 rc=33;
2141 if ((test->todo & 0x1)==0)
2143 okShell(rc==test->rc, "failed: rc=%lu err=%u\n", rc, GetLastError());
2145 else todo_wine
2147 okShell(rc==test->rc, "failed: rc=%lu err=%u\n", rc, GetLastError());
2149 if (rc==0)
2151 if ((test->todo & 0x2)==0)
2153 okChildInt("argcA", 5);
2155 else
2157 okChildInt("argcA", 5);
2159 if ((test->todo & 0x4)==0)
2161 okChildString("argvA3", "Lnk");
2163 else todo_wine
2165 okChildString("argvA3", "Lnk");
2167 sprintf(params, test->basename, tmpdir);
2168 if ((test->todo & 0x8)==0)
2170 okChildPath("argvA4", params);
2172 else
2174 okChildPath("argvA4", params);
2177 test++;
2182 static void test_exes(void)
2184 char filename[MAX_PATH];
2185 char params[1024];
2186 INT_PTR rc;
2188 sprintf(params, "shlexec \"%s\" Exec", child_file);
2190 /* We need NOZONECHECKS on Win2003 to block a dialog */
2191 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
2192 NULL, NULL);
2193 okShell(rc > 32, "returned %lu\n", rc);
2194 okChildInt("argcA", 4);
2195 okChildString("argvA3", "Exec");
2197 if (! skip_noassoc_tests)
2199 sprintf(filename, "%s\\test file.noassoc", tmpdir);
2200 if (CopyFileA(argv0, filename, FALSE))
2202 rc=shell_execute(NULL, filename, params, NULL);
2203 todo_wine {
2204 okShell(rc==SE_ERR_NOASSOC, "returned %lu\n", rc);
2208 else
2210 win_skip("Skipping shellexecute of file with unassociated extension\n");
2213 /* test combining executable and parameters */
2214 sprintf(filename, "%s shlexec \"%s\" Exec", argv0, child_file);
2215 rc = shell_execute(NULL, filename, NULL, NULL);
2216 okShell(rc == SE_ERR_FNF, "returned %lu\n", rc);
2218 sprintf(filename, "\"%s\" shlexec \"%s\" Exec", argv0, child_file);
2219 rc = shell_execute(NULL, filename, NULL, NULL);
2220 okShell(rc == SE_ERR_FNF, "returned %lu\n", rc);
2222 /* A verb, even if invalid, overrides the normal handling of executables */
2223 todo_wait rc = shell_execute_ex(SEE_MASK_FLAG_NO_UI,
2224 "notaverb", argv0, NULL, NULL, NULL);
2225 todo_wine okShell(rc == SE_ERR_NOASSOC, "returned %lu\n", rc);
2227 /* A class overrides the normal handling of executables too */
2228 /* FIXME SEE_MASK_FLAG_NO_UI is only needed due to Wine's bug */
2229 rc = shell_execute_ex(SEE_MASK_CLASSNAME | SEE_MASK_FLAG_NO_UI,
2230 NULL, argv0, NULL, NULL, ".shlexec");
2231 todo_wine okShell(rc > 32, "returned %lu\n", rc);
2232 okChildInt("argcA", 5);
2233 todo_wine okChildString("argvA3", "Open");
2234 todo_wine okChildPath("argvA4", argv0);
2237 typedef struct
2239 const char* command;
2240 const char* ddeexec;
2241 const char* application;
2242 const char* topic;
2243 const char* ifexec;
2244 int expectedArgs;
2245 const char* expectedDdeExec;
2246 BOOL broken;
2247 } dde_tests_t;
2249 static dde_tests_t dde_tests[] =
2251 /* Test passing and not passing command-line
2252 * argument, no DDE */
2253 {"", NULL, NULL, NULL, NULL, 0, ""},
2254 {"\"%1\"", NULL, NULL, NULL, NULL, 1, ""},
2256 /* Test passing and not passing command-line
2257 * argument, with DDE */
2258 {"", "[open(\"%1\")]", "shlexec", "dde", NULL, 0, "[open(\"%s\")]"},
2259 {"\"%1\"", "[open(\"%1\")]", "shlexec", "dde", NULL, 1, "[open(\"%s\")]"},
2261 /* Test unquoted %1 in command and ddeexec
2262 * (test filename has space) */
2263 {"%1", "[open(%1)]", "shlexec", "dde", NULL, 2, "[open(%s)]", TRUE /* before vista */},
2265 /* Test ifexec precedence over ddeexec */
2266 {"", "[open(\"%1\")]", "shlexec", "dde", "[ifexec(\"%1\")]", 0, "[ifexec(\"%s\")]"},
2268 /* Test default DDE topic */
2269 {"", "[open(\"%1\")]", "shlexec", NULL, NULL, 0, "[open(\"%s\")]"},
2271 /* Test default DDE application */
2272 {"", "[open(\"%1\")]", NULL, "dde", NULL, 0, "[open(\"%s\")]"},
2274 {NULL}
2277 static DWORD WINAPI hooked_WaitForInputIdle(HANDLE process, DWORD timeout)
2279 if (winetest_debug > 1)
2280 trace("WaitForInputIdle() waiting for dde event\n");
2281 return WaitForSingleObject(dde_ready_event, timeout);
2285 * WaitForInputIdle() will normally return immediately for console apps. That's
2286 * a problem for us because ShellExecute will assume that an app is ready to
2287 * receive DDE messages after it has called WaitForInputIdle() on that app.
2288 * To work around that we install our own version of WaitForInputIdle() that
2289 * will wait for the child to explicitly tell us that it is ready. We do that
2290 * by changing the entry for WaitForInputIdle() in the shell32 import address
2291 * table.
2293 static void hook_WaitForInputIdle(DWORD (WINAPI *new_func)(HANDLE, DWORD))
2295 char *base;
2296 PIMAGE_NT_HEADERS nt_headers;
2297 DWORD import_directory_rva;
2298 PIMAGE_IMPORT_DESCRIPTOR import_descriptor;
2299 int hook_count = 0;
2301 base = (char *) GetModuleHandleA("shell32.dll");
2302 nt_headers = (PIMAGE_NT_HEADERS)(base + ((PIMAGE_DOS_HEADER) base)->e_lfanew);
2303 import_directory_rva = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
2305 /* Search for the correct imported module by walking the import descriptors */
2306 import_descriptor = (PIMAGE_IMPORT_DESCRIPTOR)(base + import_directory_rva);
2307 while (U(*import_descriptor).OriginalFirstThunk != 0)
2309 char *import_module_name;
2311 import_module_name = base + import_descriptor->Name;
2312 if (lstrcmpiA(import_module_name, "user32.dll") == 0 ||
2313 lstrcmpiA(import_module_name, "user32") == 0)
2315 PIMAGE_THUNK_DATA int_entry;
2316 PIMAGE_THUNK_DATA iat_entry;
2318 /* The import name table and import address table are two parallel
2319 * arrays. We need the import name table to find the imported
2320 * routine and the import address table to patch the address, so
2321 * walk them side by side */
2322 int_entry = (PIMAGE_THUNK_DATA)(base + U(*import_descriptor).OriginalFirstThunk);
2323 iat_entry = (PIMAGE_THUNK_DATA)(base + import_descriptor->FirstThunk);
2324 while (int_entry->u1.Ordinal != 0)
2326 if (! IMAGE_SNAP_BY_ORDINAL(int_entry->u1.Ordinal))
2328 PIMAGE_IMPORT_BY_NAME import_by_name;
2329 import_by_name = (PIMAGE_IMPORT_BY_NAME)(base + int_entry->u1.AddressOfData);
2330 if (lstrcmpA((char *) import_by_name->Name, "WaitForInputIdle") == 0)
2332 /* Found the correct routine in the correct imported module. Patch it. */
2333 DWORD old_prot;
2334 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), PAGE_READWRITE, &old_prot);
2335 iat_entry->u1.Function = (ULONG_PTR) new_func;
2336 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), old_prot, &old_prot);
2337 if (winetest_debug > 1)
2338 trace("Hooked %s.WaitForInputIdle\n", import_module_name);
2339 hook_count++;
2340 break;
2343 int_entry++;
2344 iat_entry++;
2346 break;
2349 import_descriptor++;
2351 ok(hook_count, "Could not hook WaitForInputIdle()\n");
2354 static void test_dde(void)
2356 char filename[MAX_PATH], defApplication[MAX_PATH];
2357 const dde_tests_t* test;
2358 char params[1024];
2359 INT_PTR rc;
2360 HANDLE map;
2361 char *shared_block;
2363 hook_WaitForInputIdle(hooked_WaitForInputIdle);
2365 sprintf(filename, "%s\\test file.sde", tmpdir);
2367 /* Default service is application name minus path and extension */
2368 strcpy(defApplication, strrchr(argv0, '\\')+1);
2369 *strchr(defApplication, '.') = 0;
2371 map = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
2372 4096, "winetest_shlexec_dde_map");
2373 shared_block = MapViewOfFile(map, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 4096);
2375 test = dde_tests;
2376 while (test->command)
2378 if (!create_test_association(".sde"))
2380 skip("Unable to create association for '.sde'\n");
2381 return;
2383 create_test_verb_dde(".sde", "Open", 0, test->command, test->ddeexec,
2384 test->application, test->topic, test->ifexec);
2386 if (test->application != NULL || test->topic != NULL)
2388 strcpy(shared_block, test->application ? test->application : defApplication);
2389 strcpy(shared_block + strlen(shared_block) + 1, test->topic ? test->topic : SZDDESYS_TOPIC);
2391 else
2393 shared_block[0] = '\0';
2394 shared_block[1] = '\0';
2396 ddeExec[0] = 0;
2398 dde_ready_event = CreateEventA(NULL, FALSE, FALSE, "winetest_shlexec_dde_ready");
2399 rc = shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, filename, NULL, NULL, NULL);
2400 CloseHandle(dde_ready_event);
2401 okShell(32 < rc, "failed: rc=%lu err=%u\n", rc, GetLastError());
2403 if (32 < rc)
2405 if (test->broken)
2406 okChildIntBroken("argcA", test->expectedArgs + 3);
2407 else
2408 okChildInt("argcA", test->expectedArgs + 3);
2410 if (test->expectedArgs == 1) okChildPath("argvA3", filename);
2412 sprintf(params, test->expectedDdeExec, filename);
2413 okChildPath("ddeExec", params);
2415 reset_association_description();
2417 delete_test_association(".sde");
2418 test++;
2421 UnmapViewOfFile(shared_block);
2422 CloseHandle(map);
2423 hook_WaitForInputIdle((void *) WaitForInputIdle);
2426 #define DDE_DEFAULT_APP_VARIANTS 2
2427 typedef struct
2429 const char* command;
2430 const char* expectedDdeApplication[DDE_DEFAULT_APP_VARIANTS];
2431 int todo;
2432 int rc[DDE_DEFAULT_APP_VARIANTS];
2433 } dde_default_app_tests_t;
2435 static dde_default_app_tests_t dde_default_app_tests[] =
2437 /* Windows XP and 98 handle default DDE app names in different ways.
2438 * The application name we see in the first test determines the pattern
2439 * of application names and return codes we will look for. */
2441 /* Test unquoted existing filename with a space */
2442 {"%s\\test file.exe", {"test file", "test"}, 0x0, {33, 33}},
2443 {"%s\\test file.exe param", {"test file", "test"}, 0x0, {33, 33}},
2445 /* Test quoted existing filename with a space */
2446 {"\"%s\\test file.exe\"", {"test file", "test file"}, 0x0, {33, 33}},
2447 {"\"%s\\test file.exe\" param", {"test file", "test file"}, 0x0, {33, 33}},
2449 /* Test unquoted filename with a space that doesn't exist, but
2450 * test2.exe does */
2451 {"%s\\test2 file.exe", {"test2", "test2"}, 0x0, {33, 33}},
2452 {"%s\\test2 file.exe param", {"test2", "test2"}, 0x0, {33, 33}},
2454 /* Test quoted filename with a space that does not exist */
2455 {"\"%s\\test2 file.exe\"", {"", "test2 file"}, 0x0, {5, 33}},
2456 {"\"%s\\test2 file.exe\" param", {"", "test2 file"}, 0x0, {5, 33}},
2458 /* Test filename supplied without the extension */
2459 {"%s\\test2", {"test2", "test2"}, 0x0, {33, 33}},
2460 {"%s\\test2 param", {"test2", "test2"}, 0x0, {33, 33}},
2462 /* Test an unquoted nonexistent filename */
2463 {"%s\\notexist.exe", {"", "notexist"}, 0x0, {5, 33}},
2464 {"%s\\notexist.exe param", {"", "notexist"}, 0x0, {5, 33}},
2466 /* Test an application that will be found on the path */
2467 {"cmd", {"cmd", "cmd"}, 0x0, {33, 33}},
2468 {"cmd param", {"cmd", "cmd"}, 0x0, {33, 33}},
2470 /* Test an application that will not be found on the path */
2471 {"xyzwxyzwxyz", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
2472 {"xyzwxyzwxyz param", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
2474 {NULL, {NULL}, 0, {0}}
2477 typedef struct
2479 char *filename;
2480 DWORD threadIdParent;
2481 } dde_thread_info_t;
2483 static DWORD CALLBACK ddeThread(LPVOID arg)
2485 dde_thread_info_t *info = arg;
2486 assert(info && info->filename);
2487 PostThreadMessageA(info->threadIdParent,
2488 WM_QUIT,
2489 shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, info->filename, NULL, NULL, NULL),
2491 ExitThread(0);
2494 static void test_dde_default_app(void)
2496 char filename[MAX_PATH];
2497 HSZ hszApplication;
2498 dde_thread_info_t info = { filename, GetCurrentThreadId() };
2499 const dde_default_app_tests_t* test;
2500 char params[1024];
2501 DWORD threadId;
2502 MSG msg;
2503 INT_PTR rc;
2504 int which = 0;
2505 HDDEDATA ret;
2506 BOOL b;
2508 post_quit_on_execute = FALSE;
2509 ddeInst = 0;
2510 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
2511 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0);
2512 ok(rc == DMLERR_NO_ERROR, "got %lx\n", rc);
2514 sprintf(filename, "%s\\test file.sde", tmpdir);
2516 /* It is strictly not necessary to register an application name here, but wine's
2517 * DdeNameService implementation complains if 0 is passed instead of
2518 * hszApplication with DNS_FILTEROFF */
2519 hszApplication = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
2520 hszTopic = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
2521 ok(hszApplication && hszTopic, "got %p and %p\n", hszApplication, hszTopic);
2522 ret = DdeNameService(ddeInst, hszApplication, 0, DNS_REGISTER | DNS_FILTEROFF);
2523 ok(ret != 0, "got %p\n", ret);
2525 test = dde_default_app_tests;
2526 while (test->command)
2528 HANDLE thread;
2530 if (!create_test_association(".sde"))
2532 skip("Unable to create association for '.sde'\n");
2533 return;
2535 sprintf(params, test->command, tmpdir);
2536 create_test_verb_dde(".sde", "Open", 1, params, "[test]", NULL,
2537 "shlexec", NULL);
2538 ddeApplication[0] = 0;
2540 /* No application will be run as we will respond to the first DDE event,
2541 * so don't wait for it */
2542 SetEvent(hEvent);
2544 thread = CreateThread(NULL, 0, ddeThread, &info, 0, &threadId);
2545 ok(thread != NULL, "got %p\n", thread);
2546 while (GetMessageA(&msg, NULL, 0, 0)) DispatchMessageA(&msg);
2547 rc = msg.wParam > 32 ? 33 : msg.wParam;
2549 /* First test, find which set of test data we expect to see */
2550 if (test == dde_default_app_tests)
2552 int i;
2553 for (i=0; i<DDE_DEFAULT_APP_VARIANTS; i++)
2555 if (!strcmp(ddeApplication, test->expectedDdeApplication[i]))
2557 which = i;
2558 break;
2561 if (i == DDE_DEFAULT_APP_VARIANTS)
2562 skip("Default DDE application test does not match any available results, using first expected data set.\n");
2565 if ((test->todo & 0x1)==0)
2567 okShell(rc==test->rc[which], "failed: rc=%lu err=%u\n",
2568 rc, GetLastError());
2570 else todo_wine
2572 okShell(rc==test->rc[which], "failed: rc=%lu err=%u\n",
2573 rc, GetLastError());
2575 if (rc == 33)
2577 if ((test->todo & 0x2)==0)
2579 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
2580 "Expected application '%s', got '%s'\n",
2581 test->expectedDdeApplication[which], ddeApplication);
2583 else todo_wine
2585 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
2586 "Expected application '%s', got '%s'\n",
2587 test->expectedDdeApplication[which], ddeApplication);
2590 reset_association_description();
2592 delete_test_association(".sde");
2593 test++;
2596 ret = DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER);
2597 ok(ret != 0, "got %p\n", ret);
2598 b = DdeFreeStringHandle(ddeInst, hszTopic);
2599 ok(b, "got %d\n", b);
2600 b = DdeFreeStringHandle(ddeInst, hszApplication);
2601 ok(b, "got %d\n", b);
2602 b = DdeUninitialize(ddeInst);
2603 ok(b, "got %d\n", b);
2606 static void init_test(void)
2608 HMODULE hdll;
2609 HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO*);
2610 char filename[MAX_PATH];
2611 WCHAR lnkfile[MAX_PATH];
2612 char params[1024];
2613 const char* const * testfile;
2614 lnk_desc_t desc;
2615 DWORD rc;
2616 HRESULT r;
2618 hdll=GetModuleHandleA("shell32.dll");
2619 pDllGetVersion=(void*)GetProcAddress(hdll, "DllGetVersion");
2620 if (pDllGetVersion)
2622 dllver.cbSize=sizeof(dllver);
2623 pDllGetVersion(&dllver);
2624 trace("major=%d minor=%d build=%d platform=%d\n",
2625 dllver.dwMajorVersion, dllver.dwMinorVersion,
2626 dllver.dwBuildNumber, dllver.dwPlatformID);
2628 else
2630 memset(&dllver, 0, sizeof(dllver));
2633 r = CoInitialize(NULL);
2634 ok(r == S_OK, "CoInitialize failed (0x%08x)\n", r);
2635 if (FAILED(r))
2636 exit(1);
2638 rc=GetModuleFileNameA(NULL, argv0, sizeof(argv0));
2639 ok(rc != 0 && rc < sizeof(argv0), "got %d\n", rc);
2640 if (GetFileAttributesA(argv0)==INVALID_FILE_ATTRIBUTES)
2642 strcat(argv0, ".so");
2643 ok(GetFileAttributesA(argv0)!=INVALID_FILE_ATTRIBUTES,
2644 "unable to find argv0!\n");
2647 /* Older versions (win 2k) fail tests if there is a space in
2648 the path. */
2649 if (dllver.dwMajorVersion <= 5)
2650 strcpy(filename, "c:\\");
2651 else
2652 GetTempPathA(sizeof(filename), filename);
2653 GetTempFileNameA(filename, "wt", 0, tmpdir);
2654 GetLongPathNameA(tmpdir, tmpdir, sizeof(tmpdir));
2655 DeleteFileA( tmpdir );
2656 rc = CreateDirectoryA( tmpdir, NULL );
2657 ok( rc, "failed to create %s err %u\n", tmpdir, GetLastError() );
2658 /* Set %TMPDIR% for the tests */
2659 SetEnvironmentVariableA("TMPDIR", tmpdir);
2661 rc = GetTempFileNameA(tmpdir, "wt", 0, child_file);
2662 ok(rc != 0, "got %d\n", rc);
2663 init_event(child_file);
2665 /* Set up the test files */
2666 testfile=testfiles;
2667 while (*testfile)
2669 HANDLE hfile;
2671 sprintf(filename, *testfile, tmpdir);
2672 hfile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2673 FILE_ATTRIBUTE_NORMAL, NULL);
2674 if (hfile==INVALID_HANDLE_VALUE)
2676 trace("unable to create '%s': err=%u\n", filename, GetLastError());
2677 assert(0);
2679 CloseHandle(hfile);
2680 testfile++;
2683 /* Setup the test shortcuts */
2684 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
2685 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
2686 desc.description=NULL;
2687 desc.workdir=NULL;
2688 sprintf(filename, "%s\\test file.shlexec", tmpdir);
2689 desc.path=filename;
2690 desc.pidl=NULL;
2691 desc.arguments="ignored";
2692 desc.showcmd=0;
2693 desc.icon=NULL;
2694 desc.icon_id=0;
2695 desc.hotkey=0;
2696 create_lnk(lnkfile, &desc, 0);
2698 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2699 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
2700 desc.description=NULL;
2701 desc.workdir=NULL;
2702 desc.path=argv0;
2703 desc.pidl=NULL;
2704 sprintf(params, "shlexec \"%s\" Lnk", child_file);
2705 desc.arguments=params;
2706 desc.showcmd=0;
2707 desc.icon=NULL;
2708 desc.icon_id=0;
2709 desc.hotkey=0;
2710 create_lnk(lnkfile, &desc, 0);
2712 /* Create a basic association suitable for most tests */
2713 if (!create_test_association(".shlexec"))
2715 skip_shlexec_tests = TRUE;
2716 skip("Unable to create association for '.shlexec'\n");
2717 return;
2719 create_test_verb(".shlexec", "Open", 0, "Open \"%1\"");
2720 create_test_verb(".shlexec", "NoQuotes", 0, "NoQuotes %1");
2721 create_test_verb(".shlexec", "LowerL", 0, "LowerL %l");
2722 create_test_verb(".shlexec", "QuotedLowerL", 0, "QuotedLowerL \"%l\"");
2723 create_test_verb(".shlexec", "UpperL", 0, "UpperL %L");
2724 create_test_verb(".shlexec", "QuotedUpperL", 0, "QuotedUpperL \"%L\"");
2727 static void cleanup_test(void)
2729 char filename[MAX_PATH];
2730 const char* const * testfile;
2732 /* Delete the test files */
2733 testfile=testfiles;
2734 while (*testfile)
2736 sprintf(filename, *testfile, tmpdir);
2737 /* Make sure we can delete the files ('test file.noassoc' is read-only now) */
2738 SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL);
2739 DeleteFileA(filename);
2740 testfile++;
2742 DeleteFileA(child_file);
2743 RemoveDirectoryA(tmpdir);
2745 /* Delete the test association */
2746 delete_test_association(".shlexec");
2748 CloseHandle(hEvent);
2750 CoUninitialize();
2753 static void test_directory(void)
2755 char path[MAX_PATH], curdir[MAX_PATH];
2756 char params[1024], dirpath[1024];
2757 INT_PTR rc;
2759 sprintf(path, "%s\\test2.exe", tmpdir);
2760 CopyFileA(argv0, path, FALSE);
2762 sprintf(params, "shlexec \"%s\" Exec", child_file);
2764 /* Test with the current directory */
2765 GetCurrentDirectoryA(sizeof(curdir), curdir);
2766 SetCurrentDirectoryA(tmpdir);
2767 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2768 NULL, "test2.exe", params, NULL, NULL);
2769 okShell(rc > 32, "returned %lu\n", rc);
2770 okChildInt("argcA", 4);
2771 okChildString("argvA3", "Exec");
2772 todo_wine okChildPath("longPath", path);
2773 SetCurrentDirectoryA(curdir);
2775 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2776 NULL, "test2.exe", params, NULL, NULL);
2777 okShell(rc == SE_ERR_FNF, "returned %lu\n", rc);
2779 /* Explicitly specify the directory to use */
2780 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2781 NULL, "test2.exe", params, tmpdir, NULL);
2782 okShell(rc > 32, "returned %lu\n", rc);
2783 okChildInt("argcA", 4);
2784 okChildString("argvA3", "Exec");
2785 todo_wine okChildPath("longPath", path);
2787 /* Specify it through an environment variable */
2788 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2789 NULL, "test2.exe", params, "%TMPDIR%", NULL);
2790 todo_wine okShell(rc == SE_ERR_FNF, "returned %lu\n", rc);
2792 rc=shell_execute_ex(SEE_MASK_DOENVSUBST|SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2793 NULL, "test2.exe", params, "%TMPDIR%", NULL);
2794 okShell(rc > 32, "returned %lu\n", rc);
2795 okChildInt("argcA", 4);
2796 okChildString("argvA3", "Exec");
2797 todo_wine okChildPath("longPath", path);
2799 /* Not a colon-separated directory list */
2800 sprintf(dirpath, "%s:%s", curdir, tmpdir);
2801 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2802 NULL, "test2.exe", params, dirpath, NULL);
2803 okShell(rc == SE_ERR_FNF, "returned %lu\n", rc);
2806 START_TEST(shlexec)
2809 myARGC = winetest_get_mainargs(&myARGV);
2810 if (myARGC >= 3)
2812 doChild(myARGC, myARGV);
2813 exit(0);
2816 init_test();
2818 test_commandline2argv();
2819 test_argify();
2820 test_lpFile_parsed();
2821 test_filename();
2822 test_fileurls();
2823 test_find_executable();
2824 test_lnks();
2825 test_exes();
2826 test_dde();
2827 test_dde_default_app();
2828 test_directory();
2830 cleanup_test();